]>
code.delx.au - pymsnt/blob - src/disco.py
1 # Copyright 2004-2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
4 from debug
import LogEvent
, INFO
, WARN
, ERROR
6 from twisted
.internet
.defer
import Deferred
7 from twisted
.internet
import reactor
8 from tlib
.xmlw
import Element
, jid
16 XMPP_STANZAS
= "urn:ietf:params:xml:ns:xmpp-stanzas"
17 DISCO
= "http://jabber.org/protocol/disco"
18 DISCO_ITEMS
= DISCO
+ "#items"
19 DISCO_INFO
= DISCO
+ "#info"
20 COMMANDS
= "http://jabber.org/protocol/commands"
21 CAPS
= "http://jabber.org/protocol/caps"
22 SUBSYNC
= "http://delx.cjb.net/protocol/roster-subsync"
23 MUC
= "http://jabber.org/protocol/muc"
24 MUC_USER
= MUC
+ "#user"
25 FEATURE_NEG
= "http://jabber.org/protocol/feature-neg"
26 SI
= "http://jabber.org/protocol/si"
27 FT
= "http://jabber.org/protocol/si/profile/file-transfer"
28 S5B
= "http://jabber.org/protocol/bytestreams"
29 IBB
= "http://jabber.org/protocol/ibb"
30 IQGATEWAY
= "jabber:iq:gateway"
31 IQVERSION
= "jabber:iq:version"
32 IQREGISTER
= "jabber:iq:register"
33 IQROSTER
= "jabber:iq:roster"
34 IQAVATAR
= "jabber:iq:avatar"
35 IQOOB
= "jabber:iq:oob"
37 XCONFERENCE
= "jabber:x:conference"
38 XEVENT
= "jabber:x:event"
39 XDELAY
= "jabber:x:delay"
40 XAVATAR
= "jabber:x:avatar"
41 XDATA
= "jabber:x:data"
42 STORAGEAVATAR
= "storage:client:avatar"
43 XVCARDUPDATE
= "vcard-temp:x:update"
44 VCARDTEMP
= "vcard-temp"
49 class ServerDiscovery
:
50 """ Handles everything IQ related. You can send IQ stanzas and receive a Deferred
51 to notify you when a response comes, or if there's a timeout.
52 Also manages discovery for server & client """
54 # TODO rename this file & class to something more sensible
56 def __init__ (self
, pytrans
):
58 self
.pytrans
= pytrans
62 self
.deferredIqs
= {} # A dict indexed by (jid, id) of deferreds to fire
64 self
.addFeature(DISCO
, None, config
.jid
)
65 self
.addFeature(DISCO
, None, "USER")
67 def sendIq(self
, el
, timeout
=15):
68 """ Used for sending IQ packets.
69 The id attribute for the IQ will be autogenerated if it is not there yet.
70 Returns a deferred which will fire with the matching IQ response as it's sole argument. """
73 d
.errback(Exception("Timeout"))
74 del self
.deferredIqs
[(jid
, ID
)]
76 jid
= el
.getAttribute("to")
77 ID
= el
.getAttribute("id")
79 ID
= self
.pytrans
.makeMessageID()
80 el
.attributes
["id"] = ID
83 self
.deferredIqs
[(jid
, ID
)] = d
84 reactor
.callLater(timeout
, checkDeferred
)
87 def addIdentity(self
, category
, ctype
, name
, jid
):
88 """ Adds an identity to this JID's discovery profile. If jid == "USER" then MSN users will get this identity. """
90 if not self
.identities
.has_key(jid
):
91 self
.identities
[jid
] = []
92 self
.identities
[jid
].append((category
, ctype
, name
))
94 def addFeature(self
, var
, handler
, jid
):
95 """ Adds a feature to this JID's discovery profile. If jid == "USER" then MSN users will get this feature. """
97 if not self
.features
.has_key(jid
):
98 self
.features
[jid
] = []
99 self
.features
[jid
].append((var
, handler
))
101 def addNode(self
, node
, handler
, name
, jid
, rootnode
):
102 """ Adds a node to this JID's discovery profile. If jid == "USER" then MSN users will get this node. """
104 if not self
.nodes
.has_key(jid
):
106 self
.nodes
[jid
][node
] = (handler
, name
, rootnode
)
109 """ Decides what to do with an IQ """
110 fro
= el
.getAttribute("from")
111 to
= el
.getAttribute("to")
112 ID
= el
.getAttribute("id")
113 iqType
= el
.getAttribute("type")
114 ulang
= utils
.getLang(el
)
116 froj
= jid
.intern(fro
)
117 to
= jid
.intern(to
).full()
119 LogEvent(WARN
, "", "Dropping IQ because of stringprep error")
121 # Check if it's a response to a send IQ
122 if self
.deferredIqs
.has_key((fro
, ID
)) and (iqType
== "error" or iqType
== "result"):
123 LogEvent(INFO
, "", "Doing callback")
124 self
.deferredIqs
[(fro
, ID
)].callback(el
)
125 del self
.deferredIqs
[(fro
, ID
)]
128 if not (iqType
== "get" or iqType
== "set"): return # Not interested
130 LogEvent(INFO
, "", "Looking for handler")
132 for query
in el
.elements():
134 node
= query
.getAttribute("node")
136 if xmlns
.startswith(DISCO
) and node
:
137 if self
.nodes
.has_key(to
) and self
.nodes
[to
].has_key(node
) and self
.nodes
[to
][node
][0]:
138 self
.nodes
[to
][node
][0](el
)
141 # If the node we're browsing wasn't found, fall through and display the root disco
142 self
.sendDiscoInfoResponse(to
=fro
, ID
=ID
, ulang
=ulang
, jid
=to
)
144 elif xmlns
== DISCO_INFO
:
145 self
.sendDiscoInfoResponse(to
=fro
, ID
=ID
, ulang
=ulang
, jid
=to
)
147 elif xmlns
== DISCO_ITEMS
:
148 self
.sendDiscoItemsResponse(to
=fro
, ID
=ID
, ulang
=ulang
, jid
=to
)
153 elif config
.compjid
and to
== config
.compjid
:
154 searchjid
= config
.jid
157 for (feature
, handler
) in self
.features
.get(searchjid
, []):
158 if feature
== xmlns
and handler
:
159 LogEvent(INFO
, "Handler found")
163 # Still hasn't been handled
164 LogEvent(WARN
, "", "Unknown Iq request")
165 self
.sendIqError(to
=fro
, fro
=to
, ID
=ID
, xmlns
=DISCO
, etype
="cancel", condition
="feature-not-implemented")
167 def sendDiscoInfoResponse(self
, to
, ID
, ulang
, jid
):
168 """ Send a service discovery disco#info stanza to the given 'to'. 'jid' is the JID that was queried. """
170 iq
= Element((None, "iq"))
171 iq
.attributes
["type"] = "result"
172 iq
.attributes
["from"] = jid
173 iq
.attributes
["to"] = to
175 iq
.attributes
["id"] = ID
176 query
= iq
.addElement("query")
177 query
.attributes
["xmlns"] = DISCO_INFO
180 if jid
.find('@') > 0: searchjid
= "USER"
181 if config
.compjid
and jid
== config
.compjid
: searchjid
= config
.jid
183 for (category
, ctype
, name
) in self
.identities
.get(searchjid
, []):
184 identity
= query
.addElement("identity")
185 identity
.attributes
["category"] = category
186 identity
.attributes
["type"] = ctype
187 identity
.attributes
["name"] = name
189 # Add any supported features
190 for (var
, handler
) in self
.features
.get(searchjid
, []):
191 feature
= query
.addElement("feature")
192 feature
.attributes
["var"] = var
194 self
.pytrans
.send(iq
)
196 def sendDiscoItemsResponse(self
, to
, ID
, ulang
, jid
):
197 """ Send a service discovery disco#items stanza to the given 'to'. 'jid' is the JID that was queried. """
199 iq
= Element((None, "iq"))
200 iq
.attributes
["type"] = "result"
201 iq
.attributes
["from"] = jid
202 iq
.attributes
["to"] = to
204 iq
.attributes
["id"] = ID
205 query
= iq
.addElement("query")
206 query
.attributes
["xmlns"] = DISCO_ITEMS
209 if jid
.find('@') > 0: searchjid
= "USER"
210 if config
.compjid
and jid
== config
.compjid
: searchjid
= config
.jid
211 for node
in self
.nodes
.get(searchjid
, []):
212 handler
, name
, rootnode
= self
.nodes
[jid
][node
]
214 name
= getattr(lang
.get(ulang
), name
)
215 item
= query
.addElement("item")
216 item
.attributes
["jid"] = jid
217 item
.attributes
["node"] = node
218 item
.attributes
["name"] = name
220 self
.pytrans
.send(iq
)
223 def sendIqError(self
, to
, fro
, ID
, xmlns
, etype
, condition
):
224 """ Sends an IQ error response. See the XMPP RFC for details on the fields. """
225 el
= Element((None, "iq"))
226 el
.attributes
["to"] = to
227 el
.attributes
["from"] = fro
229 el
.attributes
["id"] = ID
230 el
.attributes
["type"] = "error"
231 error
= el
.addElement("error")
232 error
.attributes
["type"] = etype
233 error
.attributes
["code"] = str(utils
.errorCodeMap
[condition
])
234 cond
= error
.addElement(condition
)
235 cond
.attributes
["xmlns"] = XMPP_STANZAS
236 self
.pytrans
.send(el
)
240 def __init__(self
, pytrans
, jid
):
242 self
.pytrans
, self
.jid
= pytrans
, jid
245 ID
= self
.pytrans
.makeMessageID()
246 iq
= Element((None, "iq"))
247 iq
.attributes
["to"] = self
.jid
248 iq
.attributes
["from"] = config
.jid
249 iq
.attributes
["type"] = "get"
250 query
= iq
.addElement("query")
251 query
.attributes
["xmlns"] = DISCO_INFO
253 d
= self
.pytrans
.discovery
.sendIq(iq
)
254 d
.addCallback(self
.discoResponse
)
255 d
.addErrback(self
.discoFail
)
258 def discoResponse(self
, el
):
259 iqType
= el
.getAttribute("type")
260 if iqType
!= "result":
263 fro
= el
.getAttribute("from")
267 for child
in el
.elements():
268 if child
.name
== "query":
274 for child
in query
.elements():
275 if child
.name
== "feature":
276 features
.append(child
.getAttribute("var"))