PageRenderTime 48ms CodeModel.GetById 12ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/groupchat.py

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