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

/src/services/RosterRetrieval.py

http://pyaimt.googlecode.com/
Python | 74 lines | 59 code | 13 blank | 2 comment | 10 complexity | 9c9045409c38d46a84080851e6f03995 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
 6from twisted.words.protocols.jabber.jid import internJID
 7import config
 8import lang
 9from debug import LogEvent, INFO, WARN, ERROR
10import globals
11
12class RosterRetrieval:
13	def __init__(self, pytrans):
14		self.pytrans = pytrans
15		self.pytrans.adhoc.addCommand("retrieveroster", self.incomingIq, "command_RosterRetrieval")
16
17	def incomingIq(self, el):
18		to = el.getAttribute("from")
19		fro = el.getAttribute("from")
20		froj = internJID(fro)
21		ID = el.getAttribute("id")
22		if not hasattr(self.pytrans, "legacycon"):
23			self.pytrans.iq.sendIqError(to=to, fro=config.jid, ID=ID, xmlns=globals.COMMANDS, etype="cancel", condition="service-unavailable")
24		ulang = utils.getLang(el)
25
26		if not self.pytrans.sessions.has_key(froj.userhost()):
27			self.pytrans.iq.sendIqError(to=fro, fro=config.jid, ID=ID, xmlns=globals.COMMANDS, etype="cancel", condition="service-unavailable")
28			return
29		s = self.pytrans.sessions[froj.userhost()]
30		if not s.ready:
31			self.pytrans.iq.sendIqError(to=fro, fro=config.jid, ID=ID, xmlns=globals.COMMANDS, etype="cancel", condition="service-unavailable")
32			return
33
34		iq = Element((None, "iq"))
35		iq.attributes["to"] = to
36		iq.attributes["from"] = config.jid
37		if ID:
38			iq.attributes["id"] = ID
39		iq.attributes["type"] = "result"
40
41		command = iq.addElement("command")
42		command.attributes["sessionid"] = self.pytrans.makeMessageID()
43		command.attributes["node"] = "retrieveroster"
44		command.attributes["xmlns"] = globals.COMMANDS
45		command.attributes["status"] = "completed"
46
47		x = command.addElement("x")
48		x.attributes["xmlns"] = globals.XDATA
49		x.attributes["type"] = "result"
50
51		title = x.addElement("title")
52		title.addContent(lang.get("command_RosterRetrieval", ulang))
53
54		reported = x.addElement("reported")
55		reported.addChild(utils.makeDataFormElement(None, "legacyid", "Legacy ID"))
56		reported.addChild(utils.makeDataFormElement(None, "nick", "Nickname"))
57
58		entities = s.pytrans.xdb.getList("roster", s.jabberID)
59		if entities != None:
60			for e in entities:
61				name = e[0]
62				attrs = e[1]
63
64				item = x.addElement("item")
65
66				field = item.addElement("field")
67				field.attributes["var"] = "legacyid"
68				field.addElement("value").addContent(name)
69
70				field = item.addElement("field")
71				field.attributes["var"] = "nick"
72				field.addElement("value").addContent(attrs.get('nickname',''))
73
74		self.pytrans.send(iq)