]> code.delx.au - pymsnt/blob - src/groupchat.py
2eeef61a5b22fdff0ac3ca18f9c60cdb31371ce4
[pymsnt] / src / groupchat.py
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 import utils
5 from twisted.internet import reactor
6 if(utils.checkTwisted()):
7 from twisted.xish.domish import Element
8 else:
9 from tlib.domish import Element
10 import jabw
11 import config
12 import debug
13 import lang
14 import string
15 import time
16
17
18 class BaseGroupchat:
19 """ A class to map a groupchat from a legacy service back to the Jabber user """
20 def __init__(self, session, resource, ID=None):
21 self.session = session
22 self.session.groupchats.append(self)
23 self.nick = resource
24 if(ID):
25 self.ID = ID
26 self.session.pytrans.reserveID(self.ID)
27 else:
28 self.ID = self.session.pytrans.makeID()
29
30 self.ready = False # Is True only after the user has joined
31 self.messageBuffer = []
32 self.contacts = []
33
34 self.checkTimer = reactor.callLater(60.0*2, self.checkUserJoined, None)
35
36 debug.log("BaseGroupchat: \"%s\" created" % (self.roomJID()))
37
38 def removeMe(self):
39 """ Cleanly removes the object """
40 self.session.groupchats.remove(self)
41 if(self.ready):
42 self.session.sendPresence(to=self.user(), fro=self.roomJID() + "/" + self.nick, ptype="unavailable")
43 self.ready = False
44 self.session = None
45
46 if(self.checkTimer and not self.checkTimer.called):
47 self.checkTimer.cancel()
48 self.checkTimer = None
49
50 utils.mutilateMe(self)
51
52 debug.log("BaseGroupchat: \"%s\" destroyed" % (self.roomJID()))
53
54 def roomJID(self):
55 """ Returns the room JID """
56 return self.ID + "@" + config.jid
57
58 def user(self):
59 """ Returns the full JID of the Jabber user in this groupchat """
60 jid = self.session.jabberID
61 # FIXME, this probably won't work with multiple resources (unless you're using the highest resource)
62 # if(self.resource):
63 # jid += "/" + self.resource
64 return jid
65
66 def checkUserJoined(self, ignored=None):
67 self.checkTimer = None
68 if(not self.ready):
69 debug.log("BaseGroupchat: \"%s\" User hasn't joined after two minutes. Removing them from the room.")
70
71 text = []
72 text.append(lang.get(self.session.lang).groupchatFailJoin1 % (self.roomJID()))
73 for contact in self.contacts:
74 text.append("\t%s" % (contact))
75 text.append("")
76 text.append(lang.get(self.session.lang).groupchatFailJoin2)
77 text.append("")
78 for (source, message, timestamp) in self.messageBuffer:
79 if(source):
80 text.append("%s says: %s" % (source, message))
81 else:
82 text.append(message)
83
84 body = string.join(text, "\n")
85
86 self.session.sendMessage(to=self.user(), fro=config.jid, body=body)
87
88 self.removeMe()
89
90 def sendUserInvite(self, fro):
91 """ Sends the invitation out to the Jabber user to join this room """
92 el = Element((None, "message"))
93 el.attributes["from"] = fro
94 el.attributes["to"] = self.user()
95 body = el.addElement("body")
96 text = lang.get(self.session.lang).groupchatInvite % (self.roomJID())
97 body.addContent(text)
98 x = el.addElement("x")
99 x.attributes["jid"] = self.roomJID()
100 x.attributes["xmlns"] = "jabber:x:conference"
101 debug.log("BaseGroupchat: \"%s\" sending invitation to \"%s\" to join" % (self.roomJID(), self.user()))
102 self.session.pytrans.send(el)
103
104 def userJoined(self, nick):
105 # Send any buffered messages
106 self.nick = nick
107 if(not self.nick):
108 self.nick = self.session.username
109 self.session.sendPresence(to=self.user(), fro=self.roomJID() + "/" + self.nick)
110 if(not self.ready):
111 debug.log("BaseGroupchat: \"%s\" user has joined us!" % (self.roomJID()))
112 self.ready = True
113 for (source, text, timestamp) in self.messageBuffer:
114 self.messageReceived(source, text, timestamp)
115 self.messageBuffer = None
116 for contact in self.contacts:
117 self.contactPresenceChanged(contact)
118
119 def contactJoined(self, contact):
120 if(self.contacts.count(contact) == 0):
121 self.contacts.append(contact)
122 debug.log("BaseGroupchat: \"%s\" Legacy contact has joined \"%s\"" % (self.roomJID(), contact))
123 self.contactPresenceChanged(contact)
124 self.messageReceived(None, "%s has joined the conference." % (contact))
125
126 def contactLeft(self, contact):
127 if(self.contacts.count(contact) > 0):
128 self.contacts.remove(contact)
129 debug.log("BaseGroupchat: \"%s\" Legacy contact has left \"%s\"" % (self.roomJID(), contact))
130 self.contactPresenceChanged(contact, ptype="unavailable")
131 self.messageReceived(None, "%s has left the conference." % (contact))
132
133 def messageReceived(self, source, message, timestamp=None):
134 if(not self.ready):
135 timestamp = time.strftime("%Y%m%dT%H:%M:%S")
136 self.messageBuffer.append((source, message, timestamp))
137 else:
138 fro = self.roomJID()
139 if(source):
140 fro += "/" + source
141 debug.log("BaseGroupchat: \"%s\" messageReceived(\"%s\", \"%s\", \"%s\")" % (self.roomJID(), source, message, timestamp))
142 self.session.sendMessage(to=self.user(), fro=fro, body=message, mtype="groupchat", delay=timestamp)
143
144 def contactPresenceChanged(self, contact, ptype=None):
145 if(self.session):
146 fro = self.roomJID() + "/" + contact
147 self.session.sendPresence(to=self.user(), fro=fro, ptype=ptype)
148
149 def sendMessage(self, text, noerror):
150 debug.log("BaseGroupchat: \"%s\" sendMessage(\"%s\")" % (self.roomJID(), text))
151 self.messageReceived(self.nick, text)
152 self.sendLegacyMessage(text, noerror)
153
154 def sendLegacyMessage(self, text):
155 """ Reimplement this to send the packet to the legacy service """
156 pass
157
158 def sendContactInvite(self, contact):
159 """ Reimplement this to send the packet to the legacy service """
160 pass