Posted on October 31, 2009 in Computers by seadogNo Comments »

338px-Farsi.svgOne of the funniest things of living in a country that you don't speak the native language is translating cooking instructions. You need a fast and convenient way to translate the suggested recipe written on an iced package, while the water is boiling!

That's why I coded another jabber bot to make my life easier. Farsilou is an interface google's translate service. Add as a buddy farsilou@jabber.org and feed it some text. It will get immediately translated to english.

You can even select the language of preference by sending '$$to <two letter language code>'. For example sending

$$to el

will translate everything in greek. All languages supported by google translate are of course supported in farsilou. Check the help by sending $$help for other nice features.

You even get source language detection for free, as google's language detection works like a charm (most of the times).

It's faster than browsing to http://translate.google.com, sits on your favourite IM app along with your friends and now I can even walk around with my jabber enabled mobile and read the recipes in the supermarket. (Hey somebody code an augmented reality translator for mobile phones. That's cool!)

Tip: Google Talk is actually a Jabber service. So all you gtalk users you can add farsilou@jabber.org as a friend ;)

Posted on September 18, 2007 in Computers by seadogNo Comments »

Ένα μικρό jabber client module που έγραψα, βασισμένο στο twisted python framework. To client παρέχει όλες τις βασικές συναρτήσεις για login στο jabber server, για επικοινωνία με άλλα jabber accounts και εγκρίνει αυτόματα τις αιτήσεις για "αναγνώριση" (presence notifications)

Στο συγκεκριμένο client έχουν βασιστεί και τα echo, fortune και marx bots (σχετικό post).

#!/usr/bin/env python

from twisted.words.protocols.jabber import jid,xmlstream,client
from twisted.words.xish import domish
from twisted.internet import reactor

from string import join, letters
from random import sample

class Jclient:

def __init__(self, server, username, password, resource="Jclient", port=5222, observers=None):
self.username = username
self.password = password
self.resource = resource
self.server = server
self.port = port
self.observers = observers

def connect(self):
self.myJid = jid.JID('%s/%s' % (self.username, self.resource))
factory = client.basicClientFactory(self.myJid, self.password)
factory.addBootstrap('//event/stream/authd', self.authd)

reactor.connectTCP(self.server, self.port, factory)
reactor.run()

def authd(self, xmlstream):
#save xmlstream
self.xmlstream = xmlstream

# send presence
presence = domish.Element(('jabber:client', 'presence'))
xmlstream.send(presence)

self.watchdog()

def watchdog(self):

if not self.observers:
self.observers = {}
#self.observers['/*'] = self.logger
self.observers['/message'] = self.messageHandler
self.observers['/presence'] = self.presenceHandler

for trigger, function in self.observers.items():
self.xmlstream.addObserver(trigger, function)

def logger(self, xmlstream):
print xmlstream.toXml()

def messageHandler(self, xmlstream):
"""
Returns sender's jid, receiver's jid and message
"""

sender = xmlstream['from']
receiver = xmlstream['to']
message = xmlstream.body.children[0]
id = xmlstream['id']

return sender, receiver, id, message

def send(self, msg):

self.xmlstream.send(msg)

def newMsg(self, to, message, id=None, type="chat"):

if not id:
# generate id
id = 'jc' + join(sample(letters, 5), '')

msg = domish.Element(('jabber:client', 'message'), attribs={'to':to, 'from':self.myJid.full(), 'type': type, 'id': id})
msg.addElement('body', content=message)

return msg

def replyMsg(self, xmlstream, message, type=None):
"""
Creates reply AND sends it
"""
msg = self.newMsg(xmlstream['from'], message, None, type or xmlstream['type'])
self.send(msg)

def presenceHandler(self, xmlstream):
"""
Should implement auto add and auto delete from
presence lists
"""

if 'type' in xmlstream.attributes:
if xmlstream['type'] == 'error':
pass
elif xmlstream['type'] == 'probe':
msg = domish.Element(('jabber:client', 'presence'), attribs = {'to': xmlstream['from'], 'from':xmlstream['to']})
# always free to chat
msg.addElement('show', content="chat")
else:
msg = domish.Element(('jabber:client', 'presence'), attribs = {'type':xmlstream['type'], 'to':xmlstream['from'], 'from':xmlstream['to']})

if msg:
self.xmlstream.send(msg)

def __del__(self):
reactor.stop()

Φτιάχνοντας ένα echo bot σε 22 γραμμές με χρήση του Jclient


#!/usr/bin/env python

from jclient import Jclient
from twisted.words.xish import domish
from twisted.internet import reactor

class Jecho(Jclient):
def __init__(self, server, username, password, resource="Jecho", port=5222):
Jclient.__init__(self,server, username, password, resource, port)
self.connect()

def messageHandler(self, xmlstream):
"""
echoes the same message to sender
"""

(sender, receiver, id, message) = Jclient.messageHandler(self,xmlstream)
self.replyMsg(xmlstream,message)

jecho = Jecho("localhost", "server@localhost", "1")

Posted on September 12, 2007 in Computers by seadog3 Comments »


Έπαιξα τις τελευταίες δύο εβδομάδες με το xmpp το πρωτόκολλο πίσω από το δίκτυο jabber το μεγαλύτερο free as in speech instant messaging δίκτυο. Το jabber πήρε τα πάνω του από τότε που το gmail αποφάσισε να υποστήριξει instant messaging τεχνολογίες με το jabber μέσα από το webmail client .

Το ίδιο το jabber είναι αποκεντρωποιημένο δίκτυο -στην λογική που λειτουργεί και το email- οπότε ο καθένας μπορεί να στήσει έναν jabber server και όλοι οι servers επικοινωνούν μεταξύ τους για να ανταλλάξουν τα μηνύματά τους οι χρήστες.

Επίσης αξίζει να σημειωθεί ότι το xmpp είναι ουσιαστικά ανταλλαγή xml μηνυμάτων κάτι που το κάνει φοβερά extensible. Μπορείς για παράδειγμα να το χρησιμοποιήσεις για application2application messaging για να δημιουργήσεις ένα message queue σε κατανεμημένα συστήματα κτλ.

Στο ψητό τώρα, χρησιμοποιήσα την twisted python, έναν ενδιαφέρον programming framework για network (και όχι μόνο) programming σε python.

Έγραψά ένα jabber client class βασισμένο στο twisted python jabber protocol και βάση αυτού έφτιαξα τρία services.

  • To echo service (echo@jabber.sealabs.net): που κάνει απλά echo ότι του λες
  • Το fortune service (fortune@jabber.sealabs.net): που σου στέλνει ένα fortune cookie (από το πρόγραμμα fortune του linux) κάθε φορά που γίνεσαι available
  • και... το καλύτερο τον Karl Marx (marx@jabber.sealabs.net) ένα chatbot βασισμένο στο megahal και εκπαιδευμένο με το Κομμουνιστικό Μανιφέστο

Μπορείτε να κάνετε add buddy τα παραπάνω και θα κάνουν άμεσα authorise. Οι συζητήσεις σας με τα παραπάνω bot και κυρίως με τον Marx μπορεί να καταγράφονται για όλους ψυχαγωγίας ;)

Θα ανεβάσω και τους σχετικούς κώδικές γιατί αυτό το ριμάδι το twisted python δεν έχει ολοκληρωμένο documentation