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