#!/usr/bin/env python # Loading of libraries {{{1 import xmpp # }}}1 # Function executed when buddy list change {{{1 def presenceHandler(conn,presence_node): if (presence_node.getStatus()==None): # For all the list of knonw users available (buddies) for targetJID in getUserJIDList(): # Check if the user is connected if presence_node.getFrom().bareMatch(targetJID): # If is connected, look for his name (name, email) = searchUserJID(targetJID) # Say hello!!! ^_^ #cl.send(xmpp.Message(targetJID, "Hi %s. What can I do for you today?" % (name))) #cl.send(xmpp.Message(targetJID, "Hi %s. Try me now. ;-)" % (name))) # }}}1 # Function executed when message arrives {{{1 def messageHandler(conn,mess): mfrom=mess.getFrom() mto=mess.getTo() mensaje=mess.getBody() jidfrom_tmp = "%s" % (mfrom) jidfrom = jidfrom_tmp.split("/") if (mensaje == None): return # Ignore it if user is not on the list if jidfrom[0] not in getUserJIDList(): return # Log the message print "(%s@%s:%s) -> (%s@%s:%s): %s [Check:%s]" % (mfrom.getNode(),mfrom.getDomain(),mfrom.getResource(),mto.getNode(),mto.getDomain(),mto.getResource(),mensaje, jidfrom[0] in getUserJIDList() ) # Just in case, to work with Kopete (and possibly others IM clients) # Graphs if (len(mensaje.upper().split("graficas para".upper(),1))==2): salida="Entregando graficas a: %s" % (mensaje.lower().split(" ")[2]) cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()),salida)) # Calculator elif (len(mensaje.upper().split("calculadora".upper(),1))==2): num1=float((mensaje.lower().split(" ")[1])) oper=(mensaje.lower().split(" ")[2]) num2=float((mensaje.lower().split(" ")[3])) if (oper=='+'): result=num1+num2 elif (oper=='-'): result=num1-num2 elif (oper=='*'): result=num1*num2 elif (oper=='/'): result=num1/num2 salida="%s(%s,%s)=%s" % (oper,num1,num2,result) cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()),salida)) # Counter elif (len(mensaje.upper().split("contador hasta".upper(),1))==2): num=int((mensaje.lower().split(" ")[2])) if (num<=0): salida="Contador: The given number is too small. Only between 1 and 20." cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()),salida)) elif (num>20): salida="Contador: The given number is to big. Only from 1 to 20." cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()),salida)) else: for i in range(num,-1,-1): # Message for counting cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()),"%s" % (i))) # Sleep 1 second time.sleep(1) else: # cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()),"I don't understand..... :m")) #mensaje = sre.sub("\"", '\\\"', mensaje) #message=os.popen('./test.py "%s"' % mensaje).read() ia=COMPREHENSION.COMPREHENSION("etc/comprehension.conf") #tableOfArguments = sys.argv[1:] #text = ' '.join(tableOfArguments) #text = sre.sub('"', "\\\"", text) #text='Please execute ls command' ia.analize(mensaje) message=ia.run() if (message==''): ia2=COMPREHENSION.COMPREHENSION("autoLearn/memorize.conf") ia2.analize(mensaje) message=ia2.run() if(message==''): message="Be more clear, I don't understand you." print "=> %s@%s : %s" % (mfrom.getNode(),mfrom.getDomain(),message) cl.send(xmpp.Message("%s@%s" % (mfrom.getNode(),mfrom.getDomain()), "%s" % message)) # }}}1i # Function executed... {{{1 def iqHandler(conn,iq_node): """ Handler for processing some "get" query from custom namespace""" reply=Iq('result',iq_node.getNS(),to=iq_node.getFrom()) # ... put some content into reply node conn.send(reply) raise NodeProcessed # This stanza is fully processed # }}}1 class ACTION: def __init__(self,username,password,home="Likindoy"): # Define the protocol jid=xmpp.protocol.JID(username) # Start connection cl=xmpp.Client(jid.getDomain(),debug=[]) # If we don't connect if not cl.connect(server=(jid.getDomain(),5222)): raise IOError('I cant connect to the server.') #if not cl.auth('monkey','monkey','notebook'): if not cl.auth(jid.getNode(),password, home): raise IOError('Can not auth with server.') # Handers # Handler of presence of the buddy cl.RegisterHandler('presence',presenceHandler) # Register of IQ handler #cl.RegisterHandler('iq',iqHandler) # Register of Message Handler cl.RegisterHandler('message',messageHandler) # I set myself present cl.sendInitPresence() # I set the CODEPAGE xmpp.simplexml.ENCODING='iso8859-1' # Save cl in the class self.cl=cl def __end__(self): self.cl.disconnect() def start(self,text): # If the connection is down, I recover it if not self.cl.isConnected(): self.cl.reconnectAndReauth() self.cl.send(xmpp.Message('juanmi@elsa',text,'chat'))