Ticket #107: c2s_crash.py

File c2s_crash.py, 3.8 KB (added by bmuller, 18 months ago)

python twisted script to crash c2s

Line 
1import sys
2from twisted.words.protocols import jabber
3from twisted.words.protocols.jabber import xmlstream, client, jid
4from twisted.words.protocols.jabber.xmlstream import IQ
5from twisted.words.xish import domish
6from twisted.internet import reactor, protocol, threads
7from twisted.python import log
8from twisted.python.logfile import DailyLogFile
9from twisted.names.srvconnect import SRVConnector
10import time
11
12# namespace constants
13NS_CLIENT = 'jabber:client'
14NS_MUC = 'http://jabber.org/protocol/muc'
15NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
16NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
17NS_DELAY = 'jabber:x:delay'
18
19class XMPPClientConnector(SRVConnector):
20    """
21    Use xmpp-client SRV records to connect.
22    """
23    def __init__(self, reactor, domain, factory):
24        SRVConnector.__init__(self, reactor, 'xmpp-client', domain, factory)
25
26class Bot:
27    def __init__(self, jabberid, password, debug=False):
28        self.jid = jid.internJID(jabberid)
29        self.auth = client.XMPPAuthenticator(self.jid, password)
30        self.loggedIn = False       
31        self.debug = debug
32        self.cpf = xmlstream.XmlStreamFactory(self.auth)
33        self.cpf.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self._handleAuthd)
34        self.cpf.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self._handleConnected)
35        self.cpf.addBootstrap(xmlstream.INIT_FAILED_EVENT, self._handleInitFailed)
36        self.connector = XMPPClientConnector(reactor, self.jid.host, self.cpf)
37
38
39    def connect(self):
40        log.msg('%s connecting.' % (self.jid.user,))
41        self.connector.connect()
42
43
44    def _rawDataIn(self, buf):
45        log.msg('%s RECV: %s' % (self.jid.user, unicode(buf, 'utf-8').encode('ascii', 'replace')))
46
47
48    def _rawDataOut(self, buf):
49        log.msg('%s SENT: %s' % (self.jid.user, unicode(buf, 'utf-8').encode('ascii', 'replace')))
50
51
52    def disconnect(self):
53        log.msg('%s disconnecting.' % (self.jid.user,))
54        if self.connected:
55            # send unavailable presence
56            pres = domish.Element((NS_CLIENT, 'presence'))
57            pres['type'] = 'unavailable'
58            self.xmlstream.send(pres)   
59            self.loggedIn = False
60           
61            # send </stream:stream> and disconnect
62            self.xmlstream.sendFooter()
63            self.connector.disconnect()
64            log.msg('%s disconnected.' % (self.jid.user,))               
65        else:
66            log.msg('%s disconnected.' % (self.jid.user,))   
67
68
69    def reconnect(self):
70        self.disconnect()
71        self.connect()
72       
73       
74    def _handleConnected(self, xmlstream):
75        self.connected = True
76        self.xmlstream = xmlstream
77
78        if self.debug:
79            self.xmlstream.rawDataInFn = self._rawDataIn
80            self.xmlstream.rawDataOutFn = self._rawDataOut
81        log.msg('%s connected.' % (self.jid.user,))
82
83
84                                                           
85    def _handleAuthd(self, _):
86        log.msg('%s authenticated.' % (self.jid.user,))
87       
88        # send initial presence
89        pres = domish.Element((NS_CLIENT, 'presence'))
90        self.xmlstream.send(pres)
91       
92        self.say("Hello there", USER_NOT_LOGGED_IN)
93        self.reconnect()
94
95
96    def _handleInitFailed(self, f):
97        log.msg('%s failed init.' % (self.jid.user,))
98        log.err(f)             
99
100
101    def say(self, text, tojid):
102        msg = domish.Element((None, 'message'))
103        msg['from'] = self.jid.full()
104        msg['to'] = tojid
105        msg['type'] = 'chat'
106        msg.addElement('body').addContent(text)
107        self.xmlstream.send(msg)
108
109#jid of some user not currently logged in
110USER_NOT_LOGGED_IN = ""
111user_jid = ""
112user_passwd = ""
113log.startLogging(sys.stdout)
114bot = Bot(user_jid, user_passwd, True)
115bot.connect()
116reactor.addSystemEventTrigger('before', 'shutdown', bot.disconnect)
117reactor.run()
118