]>
code.delx.au - pymsnt/blob - 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
5 from twisted
.internet
import reactor
6 from twisted
.words
.xish
.domish
import Element
8 from debug
import LogEvent
, INFO
, WARN
, ERROR
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
)
25 self
.session
.pytrans
.reserveID(self
.ID
)
27 self
.ID
= self
.session
.pytrans
.makeID()
29 self
.ready
= False # Is True only after the user has joined
30 self
.messageBuffer
= []
33 self
.checkTimer
= reactor
.callLater(float(config
.groupchatTimeout
), self
.checkUserJoined
, None)
35 LogEvent(INFO
, self
.roomJID())
38 """ Cleanly removes the object """
39 self
.session
.groupchats
.remove(self
)
41 self
.session
.sendPresence(to
=self
.user(), fro
=self
.roomJID() + "/" + self
.nick
, ptype
="unavailable")
45 if(self
.checkTimer
and not self
.checkTimer
.called
):
46 self
.checkTimer
.cancel()
47 self
.checkTimer
= None
49 LogEvent(INFO
, self
.roomJID())
52 """ Returns the room JID """
53 return self
.ID
+ "@" + config
.jid
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)
60 # jid += "/" + self.resource
63 def checkUserJoined(self
, ignored
=None):
64 self
.checkTimer
= None
66 LogEvent(INFO
, self
.roomJID(), "User hasn't joined after two minutes. Removing them from the room.")
69 text
.append(lang
.get(self
.session
.lang
).groupchatFailJoin1
% (self
.roomJID()))
70 for contact
in self
.contacts
:
71 text
.append("\t%s" % (contact
))
73 text
.append(lang
.get(self
.session
.lang
).groupchatFailJoin2
)
75 for (source
, message
, timestamp
) in self
.messageBuffer
:
77 text
.append("%s says: %s" % (source
, message
))
81 body
= string
.join(text
, "\n")
83 self
.session
.sendMessage(to
=self
.user(), fro
=config
.jid
, body
=body
)
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())
96 x
= el
.addElement("x")
97 x
.attributes
["jid"] = self
.roomJID()
98 x
.attributes
["xmlns"] = disco
.XCONFERENCE
99 self
.session
.pytrans
.send(el
)
101 def userJoined(self
, nick
):
102 # Send any buffered messages
105 self
.nick
= self
.session
.username
106 self
.session
.sendPresence(to
=self
.user(), fro
=self
.roomJID() + "/" + self
.nick
)
108 LogEvent(INFO
, self
.roomJID())
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
)
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
))
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
))
130 def messageReceived(self
, source
, message
, timestamp
=None):
132 timestamp
= time
.strftime("%Y%m%dT%H:%M:%S")
133 self
.messageBuffer
.append((source
, message
, timestamp
))
135 self
.session
.pytrans
.statistics
.stats
["MessageCount"] += 1
139 LogEvent(INFO
, self
.roomJID())
140 self
.session
.sendMessage(to
=self
.user(), fro
=fro
, body
=message
, mtype
="groupchat", delay
=timestamp
)
142 def contactPresenceChanged(self
, contact
, ptype
=None):
144 fro
= self
.roomJID() + "/" + contact
145 self
.session
.sendPresence(to
=self
.user(), fro
=fro
, ptype
=ptype
)
147 def sendMessage(self
, text
, noerror
):
148 LogEvent(INFO
, self
.roomJID())
149 self
.messageReceived(self
.nick
, text
)
150 self
.sendLegacyMessage(text
, noerror
)
152 def sendLegacyMessage(self
, text
):
153 """ Reimplement this to send the packet to the legacy service """
156 def sendContactInvite(self
, contact
):
157 """ Reimplement this to send the packet to the legacy service """