]>
code.delx.au - pymsnt/blob - src/groupchat.py
2eeef61a5b22fdff0ac3ca18f9c60cdb31371ce4
1 # Copyright 2004 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 if(utils
.checkTwisted()):
7 from twisted
.xish
.domish
import Element
9 from tlib
.domish
import Element
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
)
26 self
.session
.pytrans
.reserveID(self
.ID
)
28 self
.ID
= self
.session
.pytrans
.makeID()
30 self
.ready
= False # Is True only after the user has joined
31 self
.messageBuffer
= []
34 self
.checkTimer
= reactor
.callLater(60.0*2, self
.checkUserJoined
, None)
36 debug
.log("BaseGroupchat: \"%s\" created" % (self
.roomJID()))
39 """ Cleanly removes the object """
40 self
.session
.groupchats
.remove(self
)
42 self
.session
.sendPresence(to
=self
.user(), fro
=self
.roomJID() + "/" + self
.nick
, ptype
="unavailable")
46 if(self
.checkTimer
and not self
.checkTimer
.called
):
47 self
.checkTimer
.cancel()
48 self
.checkTimer
= None
50 utils
.mutilateMe(self
)
52 debug
.log("BaseGroupchat: \"%s\" destroyed" % (self
.roomJID()))
55 """ Returns the room JID """
56 return self
.ID
+ "@" + config
.jid
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)
63 # jid += "/" + self.resource
66 def checkUserJoined(self
, ignored
=None):
67 self
.checkTimer
= None
69 debug
.log("BaseGroupchat: \"%s\" User hasn't joined after two minutes. Removing them from the room.")
72 text
.append(lang
.get(self
.session
.lang
).groupchatFailJoin1
% (self
.roomJID()))
73 for contact
in self
.contacts
:
74 text
.append("\t%s" % (contact
))
76 text
.append(lang
.get(self
.session
.lang
).groupchatFailJoin2
)
78 for (source
, message
, timestamp
) in self
.messageBuffer
:
80 text
.append("%s says: %s" % (source
, message
))
84 body
= string
.join(text
, "\n")
86 self
.session
.sendMessage(to
=self
.user(), fro
=config
.jid
, body
=body
)
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())
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
)
104 def userJoined(self
, nick
):
105 # Send any buffered messages
108 self
.nick
= self
.session
.username
109 self
.session
.sendPresence(to
=self
.user(), fro
=self
.roomJID() + "/" + self
.nick
)
111 debug
.log("BaseGroupchat: \"%s\" user has joined us!" % (self
.roomJID()))
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
)
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
))
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
))
133 def messageReceived(self
, source
, message
, timestamp
=None):
135 timestamp
= time
.strftime("%Y%m%dT%H:%M:%S")
136 self
.messageBuffer
.append((source
, message
, timestamp
))
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
)
144 def contactPresenceChanged(self
, contact
, ptype
=None):
146 fro
= self
.roomJID() + "/" + contact
147 self
.session
.sendPresence(to
=self
.user(), fro
=fro
, ptype
=ptype
)
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
)
154 def sendLegacyMessage(self
, text
):
155 """ Reimplement this to send the packet to the legacy service """
158 def sendContactInvite(self
, contact
):
159 """ Reimplement this to send the packet to the legacy service """