PageRenderTime 29ms CodeModel.GetById 9ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/src/legacy/services/ConfirmAccount.py

http://pyaimt.googlecode.com/
Python | 67 lines | 54 code | 11 blank | 2 comment | 10 complexity | 6b541dc4fd6867462e49e1a22432858f MD5 | raw file
 1# Copyright 2005-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
 7from debug import LogEvent, INFO, WARN, ERROR
 8import config
 9import lang
10import globals
11
12class ConfirmAccount:
13	def __init__(self, pytrans):
14		self.pytrans = pytrans
15		self.pytrans.adhoc.addCommand("confirmaccount", self.incomingIq, "command_ConfirmAccount")
16
17	def incomingIq(self, el):
18		to = el.getAttribute("from")
19		toj = internJID(to)
20		ID = el.getAttribute("id")
21		ulang = utils.getLang(el)
22
23		sessionid = None
24
25		for command in el.elements():
26			sessionid = command.getAttribute("sessionid")
27			if command.getAttribute("action") == "cancel":
28				self.pytrans.adhoc.sendCancellation("confirmaccount", el, sessionid)
29				return
30
31		if not self.pytrans.sessions.has_key(toj.userhost()) or not hasattr(self.pytrans.sessions[toj.userhost()].legacycon, "bos"):
32			self.pytrans.adhoc.sendError("confirmaccount", el, errormsg=lang.get("command_NoSession", ulang), sessionid=sessionid)
33		else:
34			self.pytrans.sessions[toj.userhost()].legacycon.bos.confirmAccount().addCallback(self.sendResponse, el, sessionid)
35
36	def sendResponse(self, failure, el, sessionid=None):
37		LogEvent(INFO)
38		to = el.getAttribute("from")
39		toj = internJID(to)
40		ID = el.getAttribute("id")
41		ulang = utils.getLang(el)
42
43		iq = Element((None, "iq"))
44		iq.attributes["to"] = to
45		iq.attributes["from"] = config.jid
46		if ID:
47			iq.attributes["id"] = ID
48		iq.attributes["type"] = "result"
49
50		command = iq.addElement("command")
51		if sessionid:
52			command.attributes["sessionid"] = sessionid
53		else:
54			command.attributes["sessionid"] = self.pytrans.makeMessageID()
55		command.attributes["node"] = "confirmaccount"
56		command.attributes["xmlns"] = globals.COMMANDS
57		command.attributes["status"] = "completed"
58
59		note = command.addElement("note")
60		if failure:
61			note.attributes["type"] = "error"
62			note.addContent(lang.get("command_ConfirmAccount_Failed", ulang))
63		else:
64			note.attributes["type"] = "info"
65			note.addContent(lang.get("command_ConfirmAccount_Complete", ulang))
66
67		self.pytrans.send(iq)