import some improvements from xmpppy and use it where it's usefull

This commit is contained in:
Yann Leboulanger 2011-10-31 10:47:28 +01:00
parent 006791d836
commit 3b646fac4a
3 changed files with 60 additions and 35 deletions

View File

@ -1503,14 +1503,14 @@ class Connection(CommonConnection, ConnectionHandlers):
Build a Privacy rule stanza for invisibility Build a Privacy rule stanza for invisibility
""" """
iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '') iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
l = iq.getTag('query').setTag('list', {'name': name}) l = iq.setQuery().setTag('list', {'name': name})
i = l.setTag('item', {'action': action, 'order': str(order)}) i = l.setTag('item', {'action': action, 'order': str(order)})
i.setTag('presence-out') i.setTag('presence-out')
return iq return iq
def build_invisible_rule(self): def build_invisible_rule(self):
iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '') iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
l = iq.getTag('query').setTag('list', {'name': 'invisible'}) l = iq.setQuery().setTag('list', {'name': 'invisible'})
if self.name in gajim.interface.status_sent_to_groups and \ if self.name in gajim.interface.status_sent_to_groups and \
len(gajim.interface.status_sent_to_groups[self.name]) > 0: len(gajim.interface.status_sent_to_groups[self.name]) > 0:
for group in gajim.interface.status_sent_to_groups[self.name]: for group in gajim.interface.status_sent_to_groups[self.name]:
@ -1540,7 +1540,7 @@ class Connection(CommonConnection, ConnectionHandlers):
if not gajim.account_is_connected(self.name): if not gajim.account_is_connected(self.name):
return return
iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '') iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
iq.getTag('query').setTag('active', {'name': name}) iq.setQuery().setTag('active', {'name': name})
self.connection.send(iq) self.connection.send(iq)
def send_invisible_presence(self, msg, signed, initial = False): def send_invisible_presence(self, msg, signed, initial = False):
@ -1892,7 +1892,7 @@ class Connection(CommonConnection, ConnectionHandlers):
if name: if name:
infos['name'] = name infos['name'] = name
iq = common.xmpp.Iq('set', common.xmpp.NS_ROSTER) iq = common.xmpp.Iq('set', common.xmpp.NS_ROSTER)
q = iq.getTag('query') q = iq.setQuery()
item = q.addChild('item', attrs=infos) item = q.addChild('item', attrs=infos)
for g in groups: for g in groups:
item.addChild('group').setData(g) item.addChild('group').setData(g)
@ -1937,7 +1937,7 @@ class Connection(CommonConnection, ConnectionHandlers):
if not gajim.account_is_connected(self.name): if not gajim.account_is_connected(self.name):
return return
iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to = agent) iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to = agent)
iq.getTag('query').setTag('remove') iq.setQuery().setTag('remove')
id_ = self.connection.getAnID() id_ = self.connection.getAnID()
iq.setID(id_) iq.setID(id_)
self.awaiting_answers[id_] = (AGENT_REMOVED, agent) self.awaiting_answers[id_] = (AGENT_REMOVED, agent)
@ -2395,7 +2395,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
iq = common.xmpp.Iq(typ = 'set', queryNS = common.xmpp.NS_MUC_OWNER, iq = common.xmpp.Iq(typ = 'set', queryNS = common.xmpp.NS_MUC_OWNER,
to = room_jid) to = room_jid)
destroy = iq.getTag('query').setTag('destroy') destroy = iq.setQuery().setTag('destroy')
if reason: if reason:
destroy.setTagData('reason', reason) destroy.setTagData('reason', reason)
if jid: if jid:
@ -2442,7 +2442,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
common.xmpp.NS_MUC_ADMIN) common.xmpp.NS_MUC_ADMIN)
item = iq.getTag('query').setTag('item') item = iq.setQuery().setTag('item')
item.setAttr('nick', nick) item.setAttr('nick', nick)
item.setAttr('role', role) item.setAttr('role', role)
if reason: if reason:
@ -2457,7 +2457,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
common.xmpp.NS_MUC_ADMIN) common.xmpp.NS_MUC_ADMIN)
item = iq.getTag('query').setTag('item') item = iq.setQuery().setTag('item')
item.setAttr('jid', jid) item.setAttr('jid', jid)
item.setAttr('affiliation', affiliation) item.setAttr('affiliation', affiliation)
if reason: if reason:
@ -2469,7 +2469,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS = \ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS = \
common.xmpp.NS_MUC_ADMIN) common.xmpp.NS_MUC_ADMIN)
item = iq.getTag('query') item = iq.setQuery()
for jid in users_dict: for jid in users_dict:
item_tag = item.addChild('item', {'jid': jid, item_tag = item.addChild('item', {'jid': jid,
'affiliation': users_dict[jid]['affiliation']}) 'affiliation': users_dict[jid]['affiliation']})
@ -2482,7 +2482,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
iq = common.xmpp.Iq(typ = 'get', to = room_jid, queryNS = \ iq = common.xmpp.Iq(typ = 'get', to = room_jid, queryNS = \
common.xmpp.NS_MUC_ADMIN) common.xmpp.NS_MUC_ADMIN)
item = iq.getTag('query').setTag('item') item = iq.setQuery().setTag('item')
item.setAttr('affiliation', affiliation) item.setAttr('affiliation', affiliation)
self.connection.send(iq) self.connection.send(iq)
@ -2491,7 +2491,7 @@ class Connection(CommonConnection, ConnectionHandlers):
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
common.xmpp.NS_MUC_OWNER) common.xmpp.NS_MUC_OWNER)
query = iq.getTag('query') query = iq.setQuery()
form.setAttr('type', 'submit') form.setAttr('type', 'submit')
query.addChild(node = form) query.addChild(node = form)
self.connection.send(iq) self.connection.send(iq)
@ -2616,7 +2616,7 @@ class Connection(CommonConnection, ConnectionHandlers):
def send_search_form(self, jid, form, is_form): def send_search_form(self, jid, form, is_form):
iq = common.xmpp.Iq(typ = 'set', to = jid, queryNS = \ iq = common.xmpp.Iq(typ = 'set', to = jid, queryNS = \
common.xmpp.NS_SEARCH) common.xmpp.NS_SEARCH)
item = iq.getTag('query') item = iq.setQuery()
if is_form: if is_form:
item.addChild(node=form) item.addChild(node=form)
else: else:

View File

@ -151,7 +151,7 @@ class ConnectionDisco:
return return
if is_form: if is_form:
iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to=agent) iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to=agent)
query = iq.getTag('query') query = iq.setQuery()
info.setAttr('type', 'submit') info.setAttr('type', 'submit')
query.addChild(node=info) query.addChild(node=info)
self.connection.SendAndCallForResponse(iq, self.connection.SendAndCallForResponse(iq,
@ -233,8 +233,7 @@ class ConnectionDisco:
log.debug('DiscoverInfoGetCB') log.debug('DiscoverInfoGetCB')
if not self.connection or self.connected < 2: if not self.connection or self.connected < 2:
return return
q = iq_obj.getTag('query') node = iq_obj.getQuerynode()
node = q.getAttr('node')
if self.commandInfoQuery(con, iq_obj): if self.commandInfoQuery(con, iq_obj):
raise common.xmpp.NodeProcessed raise common.xmpp.NodeProcessed
@ -245,7 +244,7 @@ class ConnectionDisco:
raise common.xmpp.NodeProcessed raise common.xmpp.NodeProcessed
iq = iq_obj.buildReply('result') iq = iq_obj.buildReply('result')
q = iq.getTag('query') q = iq.setQuery()
if node: if node:
q.setAttr('node', node) q.setAttr('node', node)
q.addChild('identity', attrs=gajim.gajim_identity) q.addChild('identity', attrs=gajim.gajim_identity)
@ -390,7 +389,7 @@ class ConnectionVcard:
iq = common.xmpp.Iq(typ='get') iq = common.xmpp.Iq(typ='get')
if jid: if jid:
iq.setTo(jid) iq.setTo(jid)
iq.setTag(common.xmpp.NS_VCARD + ' vCard') iq.setQuery('vCard').setNamespace(common.xmpp.NS_VCARD)
id_ = self.connection.getAnID() id_ = self.connection.getAnID()
iq.setID(id_) iq.setID(id_)
@ -532,6 +531,8 @@ class ConnectionVcard:
return return
if iq_obj.getType() == 'result': if iq_obj.getType() == 'result':
query = iq_obj.getTag('query') query = iq_obj.getTag('query')
if not query:
return
delimiter = query.getTagData('roster') delimiter = query.getTagData('roster')
if delimiter: if delimiter:
self.nested_group_delimiter = delimiter self.nested_group_delimiter = delimiter
@ -651,9 +652,8 @@ class ConnectionVcard:
with_ = element.getAttr('with') with_ = element.getAttr('with')
start_ = element.getAttr('start') start_ = element.getAttr('start')
self.request_collection_page(with_, start_) self.request_collection_page(with_, start_)
elif element.getName() == 'removed': #elif element.getName() == 'removed':
# do nothing # do nothing
pass
del self.awaiting_answers[id_] del self.awaiting_answers[id_]
@ -1470,7 +1470,7 @@ ConnectionJingle, ConnectionIBBytestream):
if obj.conn.name != self.name: if obj.conn.name != self.name:
return return
iq_obj = obj.stanza.buildReply('result') iq_obj = obj.stanza.buildReply('result')
qp = iq_obj.getTag('query') qp = iq_obj.setQuery()
qp.setTagData('name', 'Gajim') qp.setTagData('name', 'Gajim')
qp.setTagData('version', gajim.version) qp.setTagData('version', gajim.version)
send_os = gajim.config.get_per('accounts', self.name, 'send_os_info') send_os = gajim.config.get_per('accounts', self.name, 'send_os_info')
@ -1493,7 +1493,7 @@ ConnectionJingle, ConnectionIBBytestream):
if HAS_IDLE and gajim.config.get_per('accounts', self.name, if HAS_IDLE and gajim.config.get_per('accounts', self.name,
'send_idle_time'): 'send_idle_time'):
iq_obj = obj.stanza.buildReply('result') iq_obj = obj.stanza.buildReply('result')
qp = iq_obj.getTag('query') qp = iq_obj.setQuery()
qp.attrs['seconds'] = int(self.sleeper.getIdleSec()) qp.attrs['seconds'] = int(self.sleeper.getIdleSec())
else: else:
iq_obj = obj.stanza.buildReply('error') iq_obj = obj.stanza.buildReply('error')
@ -1520,7 +1520,7 @@ ConnectionJingle, ConnectionIBBytestream):
return return
if gajim.config.get_per('accounts', self.name, 'send_time_info'): if gajim.config.get_per('accounts', self.name, 'send_time_info'):
iq_obj = obj.stanza.buildReply('result') iq_obj = obj.stanza.buildReply('result')
qp = iq_obj.getTag('query') qp = iq_obj.setQuery()
qp.setTagData('utc', strftime('%Y%m%dT%H:%M:%S', gmtime())) qp.setTagData('utc', strftime('%Y%m%dT%H:%M:%S', gmtime()))
qp.setTagData('tz', helpers.decode_string(tzname[daylight])) qp.setTagData('tz', helpers.decode_string(tzname[daylight]))
qp.setTagData('display', helpers.decode_string(strftime('%c', qp.setTagData('display', helpers.decode_string(strftime('%c',

View File

@ -779,10 +779,11 @@ class Message(Protocol):
def buildReply(self, text=None): def buildReply(self, text=None):
""" """
Builds and returns another message object with specified text. The to, Builds and returns another message object with specified text. The to,
from and thread properties of new message are pre-set as reply to this from, thread and type properties of new message are pre-set as reply to
message this message
""" """
m = Message(to=self.getFrom(), frm=self.getTo(), body=text) m = Message(to=self.getFrom(), frm=self.getTo(), body=text,
typ=self.getType())
th = self.getThread() th = self.getThread()
if th: if th:
m.setThread(th) m.setThread(th)
@ -939,11 +940,20 @@ class Iq(Protocol):
if queryNS: if queryNS:
self.setQueryNS(queryNS) self.setQueryNS(queryNS)
def getQuery(self):
"""
Return the IQ's child element if it exists, None otherwise.
"""
children = self.getChildren()
if children and self.getType() != 'error' and \
children[0].getName() != 'error':
return children[0]
def getQueryNS(self): def getQueryNS(self):
""" """
Return the namespace of the 'query' child element Return the namespace of the 'query' child element
""" """
tag = self.getTag('query') tag = self.getQuery()
if tag: if tag:
return tag.getNamespace() return tag.getNamespace()
@ -951,13 +961,15 @@ class Iq(Protocol):
""" """
Return the 'node' attribute value of the 'query' child element Return the 'node' attribute value of the 'query' child element
""" """
return self.getTagAttr('query', 'node') tag = self.getQuery()
if tag:
return tag.getAttr('node')
def getQueryPayload(self): def getQueryPayload(self):
""" """
Return the 'query' child element payload Return the 'query' child element payload
""" """
tag = self.getTag('query') tag = self.getQuery()
if tag: if tag:
return tag.getPayload() return tag.getPayload()
@ -965,36 +977,49 @@ class Iq(Protocol):
""" """
Return the 'query' child element child nodes Return the 'query' child element child nodes
""" """
tag = self.getTag('query') tag = self.getQuery()
if tag: if tag:
return tag.getChildren() return tag.getChildren()
def setQuery(self, name=None):
"""
Change the name of the query node, creating it if needed. Keep the
existing name if none is given (use 'query' if it's a creation).
Return the query node.
"""
query = self.getQuery()
if query is None:
query = self.addChild('query')
if name is not None:
query.setName(name)
return query
def setQueryNS(self, namespace): def setQueryNS(self, namespace):
""" """
Set the namespace of the 'query' child element Set the namespace of the 'query' child element
""" """
self.setTag('query').setNamespace(namespace) self.setQuery().setNamespace(namespace)
def setQueryPayload(self, payload): def setQueryPayload(self, payload):
""" """
Set the 'query' child element payload Set the 'query' child element payload
""" """
self.setTag('query').setPayload(payload) self.setQuery().setPayload(payload)
def setQuerynode(self, node): def setQuerynode(self, node):
""" """
Set the 'node' attribute value of the 'query' child element Set the 'node' attribute value of the 'query' child element
""" """
self.setTagAttr('query', 'node', node) self.setQuery().setAttr('node', node)
def buildReply(self, typ): def buildReply(self, typ):
""" """
Build and return another Iq object of specified type. The to, from and Build and return another Iq object of specified type. The to, from and
query child node of new Iq are pre-set as reply to this Iq. query child node of new Iq are pre-set as reply to this Iq.
""" """
iq = Iq(typ, to=self.getFrom(), frm=self.getTo(), attrs={'id': self.getID()}) iq = Iq(typ, to=self.getFrom(), frm=self.getTo(),
if self.getTag('query'): attrs={'id': self.getID()})
iq.setQueryNS(self.getQueryNS()) iq.setQuery(self.getQuery().getName()).setNamespace(self.getQueryNS())
return iq return iq
class Acks(Node): class Acks(Node):