Advertisement
Guest User

Twisted with XMPP and XMLRPC-Server

a guest
Dec 21st, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. from twisted.words.xish import domish
  2. from wokkel.xmppim import MessageProtocol, AvailablePresence
  3. from twisted.words.protocols.jabber import jid
  4. from wokkel.client import XMPPClient
  5.  
  6. from twisted.application import internet, service
  7. from twisted.web import server, xmlrpc
  8. from twisted.internet import ssl
  9. import myxml
  10. import traceback
  11. import linecache, random
  12. import string
  13. import ConfigParser, io
  14. """
  15. ATTENTION : This one using ConfigParser !!
  16. """
  17. config = ConfigParser.RawConfigParser()
  18.  
  19.  
  20. class EchoBotProtocol(MessageProtocol):
  21.  
  22.     def connectionMade(self):
  23.         print "Connected! : "
  24.         print self.send(AvailablePresence())
  25.  
  26.  
  27.         mystatus = AvailablePresence()
  28.         mystatus["to"]="ourroom@conference.myjabber.internal/bino02"
  29.         print self.send(mystatus)
  30.        
  31.     def connectionLost(self, reason):
  32.         print "Disconnected!"
  33.  
  34.     def onMessage(self, msg):
  35.         #Will just print the msg body to stdout
  36.         print '<<<<>>>>>>>\n'
  37.         print msg.body
  38.         print '\n<<<<>>>>>>>\n'
  39.                
  40.  
  41. class MyRPC(xmlrpc.XMLRPC):
  42.     def __init__(self):
  43.         self.allowNone = True
  44.         self.useDateTime = False
  45.  
  46.         #a var just as container of XMPP Client
  47.         self.xc = None
  48.        
  49.     def xmlrpc_dispatch(self, msg):
  50.         myto = msg['to']
  51.         myxbody = msg['xbody']
  52.         mybody = xmlrpclib.dumps((myxbody,))
  53.         mytype = msg['type']
  54.  
  55.         if mytype == None:
  56.             mytype == 'chat'
  57.         if self.xc <> None:
  58.             self.m = domish.Element((None, "message"))
  59.             self.m["to"] = myto
  60.             self.m["type"] = mytype
  61.             self.m.addElement("body", content=mybody)
  62.             self.xc.send(self.m)
  63.  
  64.  
  65.  
  66. application = service.Application("newdispatch")
  67. svcCollection = service.IServiceCollection(application)
  68.  
  69. xmppclient = XMPPClient(jid.internJID("bino02@myjabber.internal/bino02"), "bino002")
  70. xmppclient.logTraffic = True
  71.  
  72.  
  73. #just for receive XMPP
  74. echobot = EchoBotProtocol()
  75. echobot.setHandlerParent(xmppclient)
  76.  
  77.  
  78. #Make xmlrpc site, and tell xmppclient to use
  79. rpc = MyRPC()
  80. rpc.xc = xmppclient
  81. site = server.Site(rpc)
  82. i = internet.TCPServer(50008, site, interface='localhost')
  83.  
  84.  
  85. #Add both service to collection
  86. i.setServiceParent(svcCollection)
  87. xmppclient.setServiceParent(svcCollection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement