/
xmpp_client.py
93 lines (62 loc) · 2.29 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
A very simple twisted xmpp-client (Jabber ID)
To run the script:
$ python xmpp_client.py <jid> <secret>
"""
from __future__ import print_function
import sys
from twisted.internet.defer import Deferred
from twisted.internet.task import react
from twisted.names.srvconnect import SRVConnector
from twisted.words.xish import domish
from twisted.words.protocols.jabber import xmlstream, client
from twisted.words.protocols.jabber.jid import JID
class Client(object):
def __init__(self, reactor, jid, secret):
self.reactor = reactor
f = client.XMPPClientFactory(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', jid.host, f, defaultPort=5222)
connector.connect()
self.finished = Deferred()
def rawDataIn(self, buf):
print("RECV: %r" % buf)
def rawDataOut(self, buf):
print("SEND: %r" % buf)
def connected(self, xs):
print('Connected.')
self.xmlstream = xs
# Log all traffic
xs.rawDataInFn = self.rawDataIn
xs.rawDataOutFn = self.rawDataOut
def disconnected(self, reason):
print('Disconnected.')
print(reason)
self.finished.callback(None)
def authenticated(self, xs):
print("Authenticated.")
presence = domish.Element((None, 'presence'))
xs.send(presence)
self.reactor.callLater(5, xs.sendFooter)
def init_failed(self, failure):
print("Initialization failed.")
print(failure)
self.xmlstream.sendFooter()
def main(reactor, jid, secret):
"""
Connect to the given Jabber ID and return a L{Deferred} which will be
called back when the connection is over.
@param reactor: The reactor to use for the connection.
@param jid: A L{JID} to connect to.
@param secret: A C{str}
"""
return Client(reactor, JID(jid), secret).finished
if __name__ == '__main__':
react(main, sys.argv[1:])