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
|
import sys
sys.path.append("../3rdParty")
import sleekxmpp.componentxmpp
class SimpleComponent :
def __init__(self, jid, password, server, port, backend) :
## BEGIN NEW
self.xmpp = sleekxmpp.componentxmpp.ComponentXMPP(jid, password, server, port)
## END NEW
self.xmpp.add_event_handler("session_start", self.handleXMPPConnected)
## BEGIN NEW
self.xmpp.add_event_handler("changed_subscription",
self.handleXMPPPresenceSubscription)
self.xmpp.add_event_handler("got_presence_probe",
self.handleXMPPPresenceProbe)
## END NEW
for event in ["message", "got_online", "got_offline", "changed_status"] :
self.xmpp.add_event_handler(event, self.handleIncomingXMPPEvent)
self.backend = backend
self.backend.addMessageHandler(self.handleMessageAddedToBackend)
def handleXMPPConnected(self, event) :
## BEGIN NEW
for user in self.backend.getAllUsers() :
self.xmpp.sendPresence(pto = self.backend.getJIDForUser(user))
## END NEW
def handleIncomingXMPPEvent(self, event) :
message = event["message"]
user = self.backend.getUserFromJID(event["jid"])
self.backend.addMessageFromUser(message, user)
## BEGIN NEW
def handleXMPPPresenceProbe(self, event) :
self.xmpp.sendPresence(pto = self.backend.getJIDForUser(user))
## END NEW
## BEGIN NEW
def handleXMPPPresenceSubscription(self, subscription) :
if subscription["type"] == "subscribe" :
userJID = subscription["from"]
self.xmpp.sendPresenceSubscription(pto=userJID, ptype="subscribed")
self.xmpp.sendPresence(pto = userJID)
self.xmpp.sendPresenceSubscription(pto=userJID, ptype="subscribe")
## END NEW
def handleMessageAddedToBackend(self, message) :
body = message.user + ": " + message.text
for subscriberJID in self.backend.getSubscriberJIDs(message.user) :
self.xmpp.sendMessage(subscriberJID, body)
def start(self) :
self.xmpp.connect()
self.xmpp.process()
|