1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 from twisted.internet import reactor
6 from twisted.names.srvconnect import SRVConnector
7 from twisted.words.xish import domish
8 from twisted.words.protocols.jabber import xmlstream, client, jid
11 class XMPPClientConnector(SRVConnector):
12 def __init__(self, reactor, domain, factory):
13 SRVConnector.__init__(self, reactor, 'xmpp-client', domain, factory)
17 host, port = SRVConnector.pickServer(self)
19 if not self.servers and not self.orderedServers:
20 # no SRV record, fall back..
28 def __init__(self, client_jid, secret):
29 f = client.XMPPClientFactory(client_jid, secret)
30 f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.connected)
31 f.addBootstrap(xmlstream.STREAM_END_EVENT, self.disconnected)
32 f.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authenticated)
33 f.addBootstrap(xmlstream.INIT_FAILED_EVENT, self.init_failed)
34 connector = XMPPClientConnector(reactor, client_jid.host, f)
38 def rawDataIn(self, buf):
39 print "RECV: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace')
42 def rawDataOut(self, buf):
43 print "SEND: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace')
46 def connected(self, xs):
52 xs.rawDataInFn = self.rawDataIn
53 xs.rawDataOutFn = self.rawDataOut
56 def disconnected(self, xs):
62 def authenticated(self, xs):
63 print "Authenticated."
65 presence = domish.Element((None, 'presence'))
68 reactor.callLater(5, xs.sendFooter)
71 def init_failed(self, failure):
72 print "Initialization failed."
75 self.xmlstream.sendFooter()
78 client_jid = jid.JID(sys.argv[1])
80 c = Client(client_jid, secret)