-
Notifications
You must be signed in to change notification settings - Fork 0
/
qubot
executable file
·76 lines (52 loc) · 2.1 KB
/
qubot
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
#!/usr/bin/env python
import re
from twisted.internet import reactor
from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.xish import domish
def rawDataIn(data):
print 'RECV: ' + data
def rawDataOut(data):
print 'SEND: ' + data
class Qubot:
def initFailed(self, failure):
self.xmlstream.sendFooter()
def message(self, element):
if element['from'].startswith(unicode(jid)) and element.body:
message = element
del message['from']
message['to'] = 'qubit@conference.jabber.org'
message['type'] = 'groupchat'
self.xmlstream.send(message)
if 'qubit@conference.jabber.org/qubot' != element['from'] and element.body and re.search('qubot', str(element.body), re.I):
message = domish.Element((None, 'message'))
message['to'] = 'qubit@conference.jabber.org'
message['type'] = 'groupchat'
message.addElement('body', content='don\'t taunt qubot')
self.xmlstream.send(message)
def authd(self, xmlstream):
self.xmlstream = xmlstream
# Send initial presence
# http://xmpp.org/internet-drafts/draft-saintandre-rfc3921bis-07.html#presence-initial
presence = domish.Element((None, 'presence'))
self.xmlstream.send(presence)
self.xmlstream.addObserver('/message', self.message)
presence = domish.Element((None, 'presence'))
presence['to'] = 'qubit@conference.jabber.org/qubot'
self.xmlstream.send(presence)
def connected(self, xmlstream):
self.xmlstream = xmlstream
self.xmlstream.rawDataInFn = rawDataIn
self.xmlstream.rawDataOutFn = rawDataOut
def end(self, xmlstream):
self.xmlstream = xmlstream
reactor.stop()
qubot = Qubot()
jid = jid.JID('administrator@example.com')
password = 'example'
factory = client.XMPPClientFactory(jid, password)
factory.addBootstrap(xmlstream.INIT_FAILED_EVENT, qubot.initFailed)
factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, qubot.authd)
factory.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, qubot.connected)
factory.addBootstrap(xmlstream.STREAM_END_EVENT, qubot.end)
reactor.connectTCP('talk.google.com', 5222, factory)
reactor.run()