| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 23 |
#~ Imports |
|---|
| 24 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 25 |
|
|---|
| 26 |
import weakref |
|---|
| 27 |
from xml.sax.saxutils import escape, quoteattr |
|---|
| 28 |
from RBFoundation.BindCallable import BindCallable |
|---|
| 29 |
import JabberObserver as JObs |
|---|
| 30 |
import JID |
|---|
| 31 |
|
|---|
| 32 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 33 |
#~ Definitions |
|---|
| 34 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 35 |
|
|---|
| 36 |
class iqQueryBase(JObs.JabberObserver): |
|---|
| 37 |
def __init__(self, JC, callback=None, BidValue=1): |
|---|
| 38 |
JObs.JabberObserver.__init__(self, callback, BidValue=BidValue) |
|---|
| 39 |
if isinstance(JC, weakref.ReferenceType): |
|---|
| 40 |
self.JC = JC |
|---|
| 41 |
else: self.JC = weakref.ref(JC) |
|---|
| 42 |
self.JC().stream.AddObserver('iq', self) |
|---|
| 43 |
|
|---|
| 44 |
def SendQuery(self, namespace, jid='', type='get', xml='', idQuery=None, bSetLink=1): |
|---|
| 45 |
# Get a new id |
|---|
| 46 |
idQuery = idQuery or self.JC()._GetNextID() |
|---|
| 47 |
|
|---|
| 48 |
# Setup our match attributes |
|---|
| 49 |
del self.Match[:] |
|---|
| 50 |
if jid: self.Match.append(JObs.MatchAttributes(from_=JID.JID(jid))) |
|---|
| 51 |
if idQuery: self.Match.append(JObs.MatchAttributes(id=idQuery)) |
|---|
| 52 |
self.Match.append(JObs.MatchHasChildNamespace(namespace)) |
|---|
| 53 |
|
|---|
| 54 |
# and finally send the query |
|---|
| 55 |
self.JC().SendXML(self._BuildQuery(namespace, xml, type=type, id=idQuery, to=jid)) |
|---|
| 56 |
return self |
|---|
| 57 |
|
|---|
| 58 |
def _BuildQuery(self, namespace, xml, **attrQuery): |
|---|
| 59 |
# Build the XML attribute string |
|---|
| 60 |
strAttributes = ' '.join([ ('%s=%s' % (a[0], quoteattr(a[1]))) for a in attrQuery.iteritems() if a[1]] ) |
|---|
| 61 |
# Return the built xml |
|---|
| 62 |
return '''<iq %s ><query xmlns=%s>%s</query></iq>''' % (strAttributes, quoteattr(namespace), xml) |
|---|
| 63 |
|
|---|
| 64 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 65 |
|
|---|
| 66 |
class iqQuery(iqQueryBase): |
|---|
| 67 |
_serviced = 0 |
|---|
| 68 |
def __nonzero__(self): |
|---|
| 69 |
if self._serviced: return 0 |
|---|
| 70 |
else: return super(iqQuery, self).__nonzero__() |
|---|
| 71 |
def __call__(self, *args, **kw): |
|---|
| 72 |
self._serviced = 1 |
|---|
| 73 |
return super(iqQuery, self).__call__(*args, **kw) |
|---|