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