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

Posted by gustavo on 02:39 03. Dec.
Syntax: ActionScript

xmpp client

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

New comment