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

View File

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

View File

@ -779,10 +779,11 @@ class Message(Protocol):
def buildReply(self, text=None):
"""
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
message
from, thread and type properties of new message are pre-set as reply to
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()
if th:
m.setThread(th)
@ -939,11 +940,20 @@ class Iq(Protocol):
if 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):
"""
Return the namespace of the 'query' child element
"""
tag = self.getTag('query')
tag = self.getQuery()
if tag:
return tag.getNamespace()
@ -951,13 +961,15 @@ class Iq(Protocol):
"""
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):
"""
Return the 'query' child element payload
"""
tag = self.getTag('query')
tag = self.getQuery()
if tag:
return tag.getPayload()
@ -965,36 +977,49 @@ class Iq(Protocol):
"""
Return the 'query' child element child nodes
"""
tag = self.getTag('query')
tag = self.getQuery()
if tag:
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):
"""
Set the namespace of the 'query' child element
"""
self.setTag('query').setNamespace(namespace)
self.setQuery().setNamespace(namespace)
def setQueryPayload(self, payload):
"""
Set the 'query' child element payload
"""
self.setTag('query').setPayload(payload)
self.setQuery().setPayload(payload)
def setQuerynode(self, node):
"""
Set the 'node' attribute value of the 'query' child element
"""
self.setTagAttr('query', 'node', node)
self.setQuery().setAttr('node', node)
def buildReply(self, typ):
"""
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.
"""
iq = Iq(typ, to=self.getFrom(), frm=self.getTo(), attrs={'id': self.getID()})
if self.getTag('query'):
iq.setQueryNS(self.getQueryNS())
iq = Iq(typ, to=self.getFrom(), frm=self.getTo(),
attrs={'id': self.getID()})
iq.setQuery(self.getQuery().getName()).setNamespace(self.getQueryNS())
return iq
class Acks(Node):