]>
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 if(utils
.checkTwisted()):
7 from twisted
.xish
.domish
import Element
9 from tlib
.domish
import Element
10 from debug
import LogEvent
, INFO
, WARN
, ERROR
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
)
27 self
.session
.pytrans
.reserveID(self
.ID
)
29 self
.ID
= self
.session
.pytrans
.makeID()
31 self
.ready
= False # Is True only after the user has joined
32 self
.messageBuffer
= []
35 self
.checkTimer
= reactor
.callLater(60.0*2, self
.checkUserJoined
, None)
37 LogEvent(INFO
, self
.roomJID())
40 """ Cleanly removes the object """
41 self
.session
.groupchats
.remove(self
)
43 self
.session
.sendPresence(to
=self
.user(), fro
=self
.roomJID() + "/" + self
.nick
, ptype
="unavailable")
47 if(self
.checkTimer
and not self
.checkTimer
.called
):
48 self
.checkTimer
.cancel()
49 self
.checkTimer
= None
51 LogEvent(INFO
, self
.roomJID())
53 utils
.mutilateMe(self
)
56 """ Returns the room JID """
57 return self
.ID
+ "@" + config
.jid
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)
64 # jid += "/" + self.resource
67 def checkUserJoined(self
, ignored
=None):
68 self
.checkTimer
= None
70 LogEvent(INFO
, self
.roomJID(), "User hasn't joined after two minutes. Removing them from the room.")
73 text
.append(lang
.get(self
.session
.lang
).groupchatFailJoin1
% (self
.roomJID()))
74 for contact
in self
.contacts
:
75 text
.append("\t%s" % (contact
))
77 text
.append(lang
.get(self
.session
.lang
).groupchatFailJoin2
)
79 for (source
, message
, timestamp
) in self
.messageBuffer
:
81 text
.append("%s says: %s" % (source
, message
))
85 body
= string
.join(text
, "\n")
87 self
.session
.sendMessage(to
=self
.user(), fro
=config
.jid
, body
=body
)
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())
100 x
= el
.addElement("x")
101 x
.attributes
["jid"] = self
.roomJID()
102 x
.attributes
["xmlns"] = disco
.XCONFERENCE
103 self
.session
.pytrans
.send(el
)
105 def userJoined(self
, nick
):
106 # Send any buffered messages
109 self
.nick
= self
.session
.username
110 self
.session
.sendPresence(to
=self
.user(), fro
=self
.roomJID() + "/" + self
.nick
)
112 LogEvent(INFO
, self
.roomJID())
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
)
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
))
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
))
134 def messageReceived(self
, source
, message
, timestamp
=None):
136 timestamp
= time
.strftime("%Y%m%dT%H:%M:%S")
137 self
.messageBuffer
.append((source
, message
, timestamp
))
139 self
.session
.pytrans
.statistics
.stats
["MessageCount"] += 1
143 LogEvent(INFO
, self
.roomJID())
144 self
.session
.sendMessage(to
=self
.user(), fro
=fro
, body
=message
, mtype
="groupchat", delay
=timestamp
)
146 def contactPresenceChanged(self
, contact
, ptype
=None):
148 fro
= self
.roomJID() + "/" + contact
149 self
.session
.sendPresence(to
=self
.user(), fro
=fro
, ptype
=ptype
)
151 def sendMessage(self
, text
, noerror
):
152 LogEvent(INFO
, self
.roomJID())
153 self
.messageReceived(self
.nick
, text
)
154 self
.sendLegacyMessage(text
, noerror
)
156 def sendLegacyMessage(self
, text
):
157 """ Reimplement this to send the packet to the legacy service """
160 def sendContactInvite(self
, contact
):
161 """ Reimplement this to send the packet to the legacy service """