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