/usr/share/pymsnt/src/disco.py is in pymsnt 0.11.3+hg224-1.
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | # Copyright 2004-2006 James Bunton <james@delx.cjb.net>
# Licensed for distribution under the GPL version 2, check COPYING for details
from debug import LogEvent, INFO, WARN, ERROR
from twisted.internet.defer import Deferred
from twisted.internet import reactor
from twisted.words.xish.domish import Element
from twisted.words.protocols.jabber.jid import internJID
import sys
import lang
import utils
import config
XMPP_STANZAS = "urn:ietf:params:xml:ns:xmpp-stanzas"
DISCO = "http://jabber.org/protocol/disco"
DISCO_ITEMS = DISCO + "#items"
DISCO_INFO = DISCO + "#info"
COMMANDS = "http://jabber.org/protocol/commands"
CAPS = "http://jabber.org/protocol/caps"
SUBSYNC = "http://delx.cjb.net/protocol/roster-subsync"
MUC = "http://jabber.org/protocol/muc"
MUC_USER = MUC + "#user"
FEATURE_NEG = "http://jabber.org/protocol/feature-neg"
SI = "http://jabber.org/protocol/si"
FT = "http://jabber.org/protocol/si/profile/file-transfer"
S5B = "http://jabber.org/protocol/bytestreams"
IBB = "http://jabber.org/protocol/ibb"
IQGATEWAY = "jabber:iq:gateway"
IQVERSION = "jabber:iq:version"
IQREGISTER = "jabber:iq:register"
IQROSTER = "jabber:iq:roster"
IQAVATAR = "jabber:iq:avatar"
IQOOB = "jabber:iq:oob"
XOOB = "jabber:x:oob"
XCONFERENCE = "jabber:x:conference"
XEVENT = "jabber:x:event"
XDELAY = "jabber:x:delay"
XAVATAR = "jabber:x:avatar"
XDATA = "jabber:x:data"
STORAGEAVATAR = "storage:client:avatar"
XVCARDUPDATE = "vcard-temp:x:update"
VCARDTEMP = "vcard-temp"
class ServerDiscovery:
""" Handles everything IQ related. You can send IQ stanzas and receive a Deferred
to notify you when a response comes, or if there's a timeout.
Also manages discovery for server & client """
# TODO rename this file & class to something more sensible
def __init__ (self, pytrans):
LogEvent(INFO)
self.pytrans = pytrans
self.identities = {}
self.features = {}
self.nodes = {}
self.deferredIqs = {} # A dict indexed by (jid, id) of deferreds to fire
self.addFeature(DISCO, None, config.jid)
self.addFeature(DISCO, None, "USER")
self.addFeature(DISCO, None, "ROOM")
def _makeSearchJID(self, jid):
if jid.find('@') > 0:
if jid.find('%') > 0:
return "USER"
else:
return "ROOM"
elif config.compjid and to == config.compjid:
return config.jid
else:
return jid
def sendIq(self, el, timeout=15):
""" Used for sending IQ packets.
The id attribute for the IQ will be autogenerated if it is not there yet.
Returns a deferred which will fire with the matching IQ response as it's sole argument. """
def checkDeferred():
if(not d.called):
d.errback(Exception("Timeout"))
del self.deferredIqs[(jid, ID)]
jid = el.getAttribute("to")
ID = el.getAttribute("id")
if(not ID):
ID = self.pytrans.makeMessageID()
el.attributes["id"] = ID
self.pytrans.send(el)
d = Deferred()
self.deferredIqs[(jid, ID)] = d
reactor.callLater(timeout, checkDeferred)
return d
def addIdentity(self, category, ctype, name, jid):
""" Adds an identity to this JID's discovery profile. If jid == "USER" then MSN users will get this identity, jid == "ROOM" is for groupchat rooms. """
LogEvent(INFO)
if not self.identities.has_key(jid):
self.identities[jid] = []
self.identities[jid].append((category, ctype, name))
def addFeature(self, var, handler, jid):
""" Adds a feature to this JID's discovery profile. If jid == "USER" then MSN users will get this feature, jid == "ROOM" is for groupchat rooms. """
LogEvent(INFO)
if not self.features.has_key(jid):
self.features[jid] = []
self.features[jid].append((var, handler))
def addNode(self, node, handler, name, jid, rootnode):
""" Adds a node to this JID's discovery profile. If jid == "USER" then MSN users will get this node, jid == "ROOM" is for groupchat rooms. """
LogEvent(INFO)
if not self.nodes.has_key(jid):
self.nodes[jid] = {}
self.nodes[jid][node] = (handler, name, rootnode)
def onIq(self, el):
""" Decides what to do with an IQ """
fro = el.getAttribute("from")
to = el.getAttribute("to")
ID = el.getAttribute("id")
iqType = el.getAttribute("type")
ulang = utils.getLang(el)
try: # Stringprep
froj = internJID(fro)
to = internJID(to).full()
except Exception:
LogEvent(WARN, "", "Dropping IQ because of stringprep error")
# Check if it's a response to a send IQ
if self.deferredIqs.has_key((fro, ID)) and (iqType == "error" or iqType == "result"):
LogEvent(INFO, "", "Doing callback")
self.deferredIqs[(fro, ID)].callback(el)
del self.deferredIqs[(fro, ID)]
return
if not (iqType == "get" or iqType == "set"): return # Not interested
LogEvent(INFO, "", "Looking for handler")
for query in el.elements():
xmlns = query.uri
node = query.getAttribute("node")
if xmlns.startswith(DISCO) and node:
if self.nodes.has_key(to) and self.nodes[to].has_key(node) and self.nodes[to][node][0]:
self.nodes[to][node][0](el)
return
else:
# If the node we're browsing wasn't found, fall through and display the root disco
self.sendDiscoInfoResponse(to=fro, ID=ID, ulang=ulang, jid=to)
return
elif xmlns == DISCO_INFO:
self.sendDiscoInfoResponse(to=fro, ID=ID, ulang=ulang, jid=to)
return
elif xmlns == DISCO_ITEMS:
self.sendDiscoItemsResponse(to=fro, ID=ID, ulang=ulang, jid=to)
return
for (feature, handler) in self.features.get(self._makeSearchJID(to), []):
if feature == xmlns and handler:
LogEvent(INFO, "Handler found")
handler(el)
return
# Still hasn't been handled
LogEvent(WARN, "", "Unknown Iq request")
self.sendIqError(to=fro, fro=to, ID=ID, xmlns=DISCO, etype="cancel", condition="feature-not-implemented")
def sendDiscoInfoResponse(self, to, ID, ulang, jid):
""" Send a service discovery disco#info stanza to the given 'to'. 'jid' is the JID that was queried. """
LogEvent(INFO)
iq = Element((None, "iq"))
iq.attributes["type"] = "result"
iq.attributes["from"] = jid
iq.attributes["to"] = to
if(ID):
iq.attributes["id"] = ID
query = iq.addElement("query")
query.attributes["xmlns"] = DISCO_INFO
searchjid = self._makeSearchJID(jid)
# Add any identities
for (category, ctype, name) in self.identities.get(searchjid, []):
identity = query.addElement("identity")
identity.attributes["category"] = category
identity.attributes["type"] = ctype
identity.attributes["name"] = name
# Add any supported features
for (var, handler) in self.features.get(searchjid, []):
feature = query.addElement("feature")
feature.attributes["var"] = var
self.pytrans.send(iq)
def sendDiscoItemsResponse(self, to, ID, ulang, jid):
""" Send a service discovery disco#items stanza to the given 'to'. 'jid' is the JID that was queried. """
LogEvent(INFO)
iq = Element((None, "iq"))
iq.attributes["type"] = "result"
iq.attributes["from"] = jid
iq.attributes["to"] = to
if(ID):
iq.attributes["id"] = ID
query = iq.addElement("query")
query.attributes["xmlns"] = DISCO_ITEMS
searchjid = self._makeSearchJID(jid)
for node in self.nodes.get(searchjid, []):
handler, name, rootnode = self.nodes[jid][node]
if rootnode:
name = getattr(lang.get(ulang), name)
item = query.addElement("item")
item.attributes["jid"] = jid
item.attributes["node"] = node
item.attributes["name"] = name
self.pytrans.send(iq)
def sendIqError(self, to, fro, ID, xmlns, etype, condition):
""" Sends an IQ error response. See the XMPP RFC for details on the fields. """
el = Element((None, "iq"))
el.attributes["to"] = to
el.attributes["from"] = fro
if(ID):
el.attributes["id"] = ID
el.attributes["type"] = "error"
error = el.addElement("error")
error.attributes["type"] = etype
error.attributes["code"] = str(utils.errorCodeMap[condition])
cond = error.addElement(condition)
cond.attributes["xmlns"] = XMPP_STANZAS
self.pytrans.send(el)
class DiscoRequest:
def __init__(self, pytrans, jid):
LogEvent(INFO)
self.pytrans, self.jid = pytrans, jid
def doDisco(self):
ID = self.pytrans.makeMessageID()
iq = Element((None, "iq"))
iq.attributes["to"] = self.jid
iq.attributes["from"] = config.jid
iq.attributes["type"] = "get"
query = iq.addElement("query")
query.attributes["xmlns"] = DISCO_INFO
d = self.pytrans.discovery.sendIq(iq)
d.addCallback(self.discoResponse)
d.addErrback(self.discoFail)
return d
def discoResponse(self, el):
iqType = el.getAttribute("type")
if iqType != "result":
return []
fro = el.getAttribute("from")
features = []
for child in el.elements():
if child.name == "query":
query = child
break
else:
return []
for child in query.elements():
if child.name == "feature":
features.append(child.getAttribute("var"))
return features
def discoFail(self, err):
return []
|