Copy Paste Code . com

personal pastebin - debug, share, develop, store

Post Code
Archive

Latest code
ToTeste
06. Jan - C
lasseters online casino
06. Jan - JavaScript
guide
06. Jan - Smalltalk
tips
06. Jan - C#
asd
06. Jan - C
Project
05. Jan - C++
oracle de merdouille :/
05. Jan - MySQL
Wiremod - Jumper
05. Jan - Text
visit now
05. Jan - CSS
yiPPpAfkW
04. Jan - JavaScript

Code: xmpp client

Posted by gustavo on 02:48 03. Dec.
Syntax: Python

xmpp client

  1. #!/usr/bin/python
  2. # -*- coding: ISO-8859-1 -*-
  3.  
  4. import sys, xmpp, threading, time
  5.  
  6. class events(threading.Thread):
  7.     # thread for listening to events
  8.     global cl
  9.     def __init__(self, cl):
  10.         self.cl = cl
  11.         threading.Thread.__init__ (self)
  12.    
  13.     def run(self):
  14.         self.keepGoing = True
  15.        
  16.         # waiting for events
  17.         while self.keepGoing:
  18.             self.cl.Process(1)
  19.                
  20.     def stop(self):
  21.         # stop the loop for events
  22.         self.keepGoing = False
  23.         # self.cl.disconnect()  # there were errors on terminal
  24.  
  25. class waitCommand(threading.Thread):
  26.     # thread that waits for user input
  27.     def __init__(self, cl):
  28.         self.cl = cl
  29.         self.keepGoing = True
  30.         threading.Thread.__init__ (self)
  31.    
  32.     def run(self):
  33.         while self.keepGoing:
  34.             cmd = raw_input()
  35.             if cmd == "bye":
  36.                 self.keepGoing = False
  37.             elif cmd != "" and cmd.split()[0] == "send":
  38.                 sendMessage(self.cl, cmd.split("\"")[2].split("to ")[1], \
  39.                                                         cmd.split("\"")[1])
  40.  
  41. def sendMessage(cl, to, msg):
  42.     # send messages
  43.     cl.send(xmpp.Message(to, msg))
  44.  
  45. def messageHandler(cl, msg):
  46.     # handling income messages
  47.     presence_type=msg.getType()
  48.     if presence_type == "chat":
  49.         print ""
  50.         print "------------- New Message -------------"
  51.         print "From: " + str(msg.getFrom()).split("/")[0]
  52.         print "Message: " + str(msg.getBody())
  53.         print "---------------------------------------"
  54.         print ""
  55.  
  56. def main(argv=None):
  57.     if argv is None:
  58.         argv = sys.argv
  59.  
  60.     if len(argv) < 3:
  61.         print ""
  62.         print "Usage:"
  63.         print "python client.py <username> <password>"
  64.         print ""
  65.         print "* Attention: the username must be provided with '@domain.com'. *"
  66.         print ""
  67.         sys.exit(2)  
  68.        
  69.     # parameters from args
  70.     jabber_user  = argv[1]
  71.     jabber_pw    = argv[2]
  72.    
  73.     jid=xmpp.protocol.JID(jabber_user)
  74.     cl=xmpp.Client(jid.getDomain(), debug=[])
  75.        
  76.     # possible servers
  77.     if jid.getDomain() == "gmail.com":
  78.         jabber_server = "talk.google.com"
  79.     else:
  80.         print "Server not yet supported."
  81.         sys.exit(2)
  82.  
  83.     # try to connect and login
  84.     print "Connecting..."
  85.     if not cl.connect((jabber_server,5222)):
  86.         raise IOError(1, "Couldn't connect to the server.")
  87.     print "Connected, trying to login...,"    
  88.     if not cl.auth(jid.getNode(),jabber_pw):
  89.         raise IOError(2, "Login attempt failed. \
  90. Are the provided username and password correct?")
  91.     print "Logged in! Enjoy your stay ;)."
  92.  
  93.     # send a "hi there".. comment this for invisible connection
  94.     cl.sendInitPresence()
  95.        
  96.     # handlers
  97.     cl.RegisterHandler("message", messageHandler)
  98.  
  99.     # start the thread for events
  100.     threadEvents = events(cl)
  101.     threadEvents.start()
  102.    
  103.     # start the thread for user input
  104.     threadWaitCommand = waitCommand(cl)
  105.     threadWaitCommand.start()
  106.  
  107.     # main loop
  108.     while 1:
  109.         try:
  110.             if threadWaitCommand.keepGoing == True:
  111.                 pass
  112.             else:
  113.                 sys.exit(0)
  114.         except (SystemExit, KeyboardInterrupt):
  115.             threadEvents.stop()
  116.             print ""
  117.             print "Exiting, please wait..."
  118.             time.sleep(1)
  119.             sys.exit(0)
  120.  
  121. if __name__=='__main__':
  122.     main()

New comment