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

/src/adhoc.py

http://pyaimt.googlecode.com/
Python | 169 lines | 134 code | 30 blank | 5 comment | 27 complexity | d7dae7c61fd1fcea0efc6664684f57fb 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 AdHocCommands:
 13	def __init__(self, pytrans):
 14		self.pytrans = pytrans
 15		self.pytrans.disco.addFeature(globals.COMMANDS, self.incomingIq, config.jid)
 16		self.pytrans.disco.addNode(globals.COMMANDS, self.sendCommandList, "command_CommandList", config.jid, True)
 17
 18		self.commands = {} # Dict of handlers indexed by node
 19		self.commandNames = {} # Dict of names indexed by node
 20
 21	def addCommand(self, command, handler, name):
 22		self.commands[command] = handler
 23		self.commandNames[command] = name
 24		self.pytrans.disco.addNode(command, self.incomingIq, name, config.jid, False)
 25
 26	def incomingIq(self, el):
 27		itype = el.getAttribute("type")
 28		fro = el.getAttribute("from")
 29		froj = internJID(fro)
 30		to = el.getAttribute("to")
 31		ID = el.getAttribute("id")
 32		ulang = utils.getLang(el)
 33
 34		LogEvent(INFO, msg="Looking for handler")
 35
 36		node = None
 37		for child in el.elements():
 38			xmlns = child.uri
 39			node = child.getAttribute("node")
 40
 41			handled = False
 42			if child.name == "query" and xmlns == globals.DISCO_INFO:
 43				if node and self.commands.has_key(node) and itype == "get":
 44					self.sendCommandInfoResponse(to=fro, ID=ID, node=node, ulang=ulang)
 45					handled = True
 46			elif child.name == "query" and xmlns == globals.DISCO_ITEMS:
 47				if node and self.commands.has_key(node) and itype == "get":
 48					self.sendCommandItemsResponse(to=fro, ID=ID, node=node, ulang=ulang)
 49					handled = True
 50			elif child.name == "command" and xmlns == globals.COMMANDS:
 51				if node and self.commands.has_key(node) and (itype == "set" or itype == "error"):
 52					self.commands[node](el)
 53					handled = True
 54			if not handled:
 55				LogEvent(WARN, msg="Unknown Ad-Hoc command received")
 56				self.pytrans.iq.sendIqError(to=fro, fro=config.jid, ID=ID, xmlns=xmlns, etype="cancel", condition="feature-not-implemented")
 57
 58
 59	def sendCommandList(self, el):
 60		to = el.getAttribute("from")
 61		ID = el.getAttribute("id")
 62		ulang = utils.getLang(el)
 63
 64		iq = Element((None, "iq"))
 65		iq.attributes["to"] = to
 66		iq.attributes["from"] = config.jid
 67		if ID:
 68			iq.attributes["id"] = ID
 69		iq.attributes["type"] = "result"
 70
 71		query = iq.addElement("query")
 72		query.attributes["xmlns"] = globals.DISCO_ITEMS
 73		query.attributes["node"] = globals.COMMANDS
 74
 75		for command in self.commands:
 76			item = query.addElement("item")
 77			item.attributes["jid"] = config.jid
 78			item.attributes["node"] = command
 79			item.attributes["name"] = lang.get(self.commandNames[command], ulang)
 80
 81		self.pytrans.send(iq)
 82
 83	def sendCommandInfoResponse(self, to, ID, node, ulang):
 84		LogEvent(INFO, msg="Replying to disco#info")
 85		iq = Element((None, "iq"))
 86		iq.attributes["type"] = "result"
 87		iq.attributes["from"] = config.jid
 88		iq.attributes["to"] = to
 89		if ID: iq.attributes["id"] = ID
 90		query = iq.addElement("query")
 91		query.attributes["xmlns"] = globals.DISCO_INFO
 92
 93		# Add identity
 94		identity = query.addElement("identity")
 95		identity.attributes["name"] = lang.get(self.commandNames[node], ulang)
 96		identity.attributes["category"] = "automation"
 97		identity.attributes["type"] = "command-node"
 98
 99		# Add supported feature
100		feature = query.addElement("feature")
101		feature.attributes["var"] = globals.COMMANDS
102
103		# Add supported feature
104		feature = query.addElement("feature")
105		feature.attributes["var"] = globals.XDATA
106
107		self.pytrans.send(iq)
108
109	def sendCommandItemsResponse(self, to, ID, node, ulang):
110		LogEvent(INFO, msg="Replying to disco#items")
111		iq = Element((None, "iq"))
112		iq.attributes["type"] = "result"
113		iq.attributes["from"] = config.jid
114		iq.attributes["to"] = to
115		if ID: iq.attributes["id"] = ID
116		query = iq.addElement("query")
117		query.attributes["xmlns"] = globals.DISCO_ITEMS
118
119		self.pytrans.send(iq)
120
121	def sendCancellation(self, node, el, sessionid=None):
122		to = el.getAttribute("from")
123		ID = el.getAttribute("id")
124		ulang = utils.getLang(el)
125
126		iq = Element((None, "iq"))
127		iq.attributes["to"] = to
128		iq.attributes["from"] = config.jid
129		if ID:
130			iq.attributes["id"] = ID
131		iq.attributes["type"] = "result"
132
133		command = iq.addElement("command")
134		if sessionid:
135			command.attributes["sessionid"] = sessionid
136		else:
137			command.attributes["sessionid"] = self.pytrans.makeMessageID()
138		command.attributes["node"] = node
139		command.attributes["xmlns"] = globals.COMMANDS
140		command.attributes["status"] = "canceled"
141
142		self.pytrans.send(iq)
143
144	def sendError(self, node, el, errormsg, sessionid=None):
145		to = el.getAttribute("from")
146		ID = el.getAttribute("id")
147		ulang = utils.getLang(el)
148
149		iq = Element((None, "iq"))
150		iq.attributes["to"] = to
151		iq.attributes["from"] = config.jid
152		if ID:
153			iq.attributes["id"] = ID
154		iq.attributes["type"] = "result"
155
156		command = iq.addElement("command")
157		if sessionid:
158			command.attributes["sessionid"] = sessionid
159		else:
160			command.attributes["sessionid"] = self.pytrans.makeMessageID()
161		command.attributes["node"] = node
162		command.attributes["xmlns"] = globals.COMMANDS
163		command.attributes["status"] = "completed"
164
165		note = command.addElement("note")
166		note.attributes["type"] = "error"
167		note.addContent(errormsg)
168
169		self.pytrans.send(iq)