summaryrefslogtreecommitdiffstats
blob: 725ec8244676b2f38a039fae9bda34d201916084 (plain)
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
import sys
sys.path.append("../3rdParty")
import sleekxmpp.componentxmpp

class Component :
  def __init__(self, jid, password, server, port, backend) :
    self.componentDomain = jid
    self.xmpp = sleekxmpp.componentxmpp.ComponentXMPP(jid, password, server, port)
    self.xmpp.add_event_handler("session_start", self.handleXMPPConnected)
    self.xmpp.add_event_handler("changed_subscription",
        self.handleXMPPPresenceSubscription)
    self.xmpp.add_event_handler("got_presence_probe",
        self.handleXMPPPresenceProbe)
    self.xmpp.add_event_handler("message", self.handleIncomingXMPPMessage)
    for event in ["got_online", "got_offline", "changed_status"] :
      self.xmpp.add_event_handler(event, self.handleIncomingXMPPPresence)
    self.backend = backend
    self.backend.addMessageHandler(self.handleMessageAddedToBackend)

  def handleXMPPConnected(self, event) :
    for user in self.backend.getAllUsers() :
      self.sendPresenceOfAllContactsForUser(user)

  def handleIncomingXMPPMessage(self, event) :
    message = self.addRecipientToMessage(event["message"], event["to"])
    user = self.backend.getUserFromJID(event["jid"])
    self.backend.addMessageFromUser(message, user)

  def handleIncomingXMPPPresence(self, event) :
    if event["to"] == self.componentDomain :
      user = self.backend.getUserFromJID(event["jid"])
      self.backend.addMessageFromUser(event["message"], user)

  def handleXMPPPresenceProbe(self, event) :
    self.sendPresenceOfContactToUser(contactJID=event["to"], userJID=event["from"])

  def handleXMPPPresenceSubscription(self, subscription) :
    if subscription["type"] == "subscribe" :
      userJID = subscription["from"]
      user = self.backend.getUserFromJID(userJID)
      contactJID = subscription["to"]
      self.xmpp.sendPresenceSubscription(
          pfrom=contactJID, pto=userJID, ptype="subscribed", pnick=user)
      self.sendPresenceOfContactToUser(contactJID=contactJID, userJID=userJID)
      if contactJID == self.componentDomain :
        self.sendAllContactSubscriptionRequestsToUser(userJID)

  def handleMessageAddedToBackend(self, message) :
    userJID = self.getComponentJIDFromUser(message.user)
    for subscriberJID in self.backend.getSubscriberJIDs(message.user) :
      self.xmpp.sendMessage(mfrom=userJID, mto=subscriberJID, mbody=message.text)
      self.xmpp.sendPresence(pfrom=userJID, pto=subscriberJID, pstatus=message.text)
  
  ## ...
  
  def sendPresenceOfAllContactsForUser(self, user) :
    userJID = self.backend.getJIDForUser(user)
    for contact in self.backend.getContacts(user) :
      contactJID = self.getComponentJIDFromUser(contact)
      self.sendPresenceOfContactToUser(contactJID = contactJID, userJID = userJID)

  def sendPresenceOfContactToUser(self, contactJID, userJID) :
    message = self.backend.getLastMessage(contactJID).text
    self.xmpp.sendPresence(pto = userJID, pfrom = contactJID, pshow = message)

  def sendAllContactSubscriptionRequestsToUser(self, userJID) :
    user = self.backend.getUserFromJID(userJID)
    for contact in self.backend.getContacts(user) :
      contactJID = self.getComponentJIDFromUser(contact)
      self.xmpp.sendPresenceSubscription(
          pfrom=contactJID, pto=userJID, ptype="subscribe", pnick=contact)
  
  def addRecipientToMessage(self, message, recipientJID) :
    contact = self.getUserFromComponentJID(recipientJID)
    return ("@" + contact if contact else "") + " " + message

  def getUserFromComponentJID(self, jid) :
    return jid.split("@",1)[0] if "@" in jid else None

  def getComponentJIDFromUser(self, user) :
    return user + "@" + self.componentDomain

  def start(self) :
    self.xmpp.connect()
    self.xmpp.process()