forked from mehulsbhatt/xmppserver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
xmpp_client.py
66 lines (43 loc) · 1.73 KB
/
xmpp_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
import sys
from twisted.internet import reactor
from twisted.names.srvconnect import SRVConnector
from twisted.words.xish import domish
from twisted.words.protocols.jabber import xmlstream, client, jid
class Client(object):
def __init__(self, client_jid, secret):
f = client.XMPPClientFactory(client_jid, secret)
f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.connected)
f.addBootstrap(xmlstream.STREAM_END_EVENT, self.disconnected)
f.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authenticated)
f.addBootstrap(xmlstream.INIT_FAILED_EVENT, self.init_failed)
connector = SRVConnector(reactor, 'xmpp-client', client_jid.host, f,
defaultPort=5222)
connector.connect()
def rawDataIn(self, buf):
print "RECV: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace')
def rawDataOut(self, buf):
print "SEND: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace')
def connected(self, xs):
print 'Connected.'
self.xmlstream = xs
# Log all traffic
xs.rawDataInFn = self.rawDataIn
xs.rawDataOutFn = self.rawDataOut
def disconnected(self, xs):
print 'Disconnected.'
reactor.stop()
def authenticated(self, xs):
print "Authenticated."
presence = domish.Element((None, 'presence'))
xs.send(presence)
reactor.callLater(5, xs.sendFooter)
def init_failed(self, failure):
print "Initialization failed."
print failure
self.xmlstream.sendFooter()
client_jid = jid.JID(sys.argv[1])
secret = sys.argv[2]
c = Client(client_jid, secret)
reactor.run()