PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/services/GatewayTranslator.py

http://pyaimt.googlecode.com/
Python | 75 lines | 59 code | 13 blank | 3 comment | 9 complexity | 98573ee7228b1188e7abd5ffab4e4c23 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.words.xish.domish import Element
 6import legacy
 7import config
 8import lang
 9from debug import LogEvent, INFO, WARN, ERROR
10import globals
11
12class GatewayTranslator:
13	def __init__(self, pytrans):
14		self.pytrans = pytrans
15		self.pytrans.disco.addFeature(globals.IQGATEWAY, self.incomingIq, config.jid)
16	
17	def incomingIq(self, el):
18		fro = el.getAttribute("from")
19		ID = el.getAttribute("id")
20		itype = el.getAttribute("type")
21		if itype == "get":
22			self.sendPrompt(fro, ID, utils.getLang(el))
23		elif itype == "set":
24			self.sendTranslation(fro, ID, el)
25	
26	def sendPrompt(self, to, ID, ulang):
27		LogEvent(INFO)
28		
29		iq = Element((None, "iq"))
30		
31		iq.attributes["type"] = "result"
32		iq.attributes["from"] = config.jid
33		iq.attributes["to"] = to
34		iq.attributes["id"] = ID
35		query = iq.addElement("query")
36		query.attributes["xmlns"] = globals.IQGATEWAY
37		desc = query.addElement("desc")
38		desc.addContent(lang.get("gatewaytranslator", ulang))
39		prompt = query.addElement("prompt")
40		
41		self.pytrans.send(iq)
42	
43	def sendTranslation(self, to, ID, el):
44		LogEvent(INFO)
45		
46		# Find the user's legacy account
47		legacyaccount = None
48		for query in el.elements():
49			if query.name == "query":
50				for child in query.elements():
51					if child.name == "prompt":
52						legacyaccount = str(child)
53						break
54				break
55		
56		
57		if legacyaccount and len(legacyaccount) > 0:
58			LogEvent(INFO, msg="Sending translated account")
59			iq = Element((None, "iq"))
60			iq.attributes["type"] = "result"
61			iq.attributes["from"] = config.jid
62			iq.attributes["to"] = to
63			iq.attributes["id"] = ID
64			query = iq.addElement("query")
65			query.attributes["xmlns"] = globals.IQGATEWAY
66			prompt = query.addElement("prompt")
67			prompt.addContent(legacy.translateAccount(legacyaccount))
68			jid = query.addElement("jid")
69			jid.addContent(legacy.translateAccount(legacyaccount))
70			
71			self.pytrans.send(iq)
72		
73		else:
74			self.pytrans.iq.sendIqError(to, ID, globals.IQGATEWAY)
75			self.pytrans.iq.sendIqError(to=to, fro=config.jid, ID=ID, xmlns="jabber:iq:gateway", etype="retry", condition="bad-request")