gajim-plural/src/common/xmpp/features_nb.py

215 lines
7 KiB
Python
Raw Normal View History

2008-12-03 21:56:12 +00:00
## features.py
##
## Copyright (C) 2003-2004 Alexey "Snake" Nezhdanov
2007-07-11 21:39:27 +00:00
## Copyright (C) 2007 Julien Pivotto <roidelapluie@gmail.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
# $Id: features.py,v 1.22 2005/09/30 20:13:04 mikealbon Exp $
2008-12-27 11:45:58 +00:00
'''
Different stuff that wasn't worth separating it into modules
(Registration, Privacy Lists, ...)
2008-12-27 11:45:58 +00:00
'''
from protocol import NS_REGISTER, NS_PRIVACY, NS_DATA, Iq, isResultNode, Node
def _on_default_response(disp, iq, cb):
def _on_response(resp):
2008-12-03 21:56:12 +00:00
if isResultNode(resp):
if cb:
cb(True)
elif cb:
cb(False)
disp.SendAndCallForResponse(iq, _on_response)
###############################################################################
### Registration
###############################################################################
REGISTER_DATA_RECEIVED = 'REGISTER DATA RECEIVED'
2008-12-03 21:56:12 +00:00
def getRegInfo(disp, host, info={}, sync=True):
'''
Gets registration form from remote host. Info dict can be prefilled
:param disp: plugged dispatcher instance
:param info: dict, like {'username':'joey'}.
See JEP-0077 for details.
'''
iq=Iq('get',NS_REGISTER,to=host)
2008-12-03 21:56:12 +00:00
for i in info.keys():
iq.setTagData(i,info[i])
if sync:
disp.SendAndCallForResponse(iq, lambda resp:
_ReceivedRegInfo(disp.Dispatcher, resp, host))
2008-12-03 21:56:12 +00:00
else:
disp.SendAndCallForResponse(iq, _ReceivedRegInfo, {'agent': host })
def _ReceivedRegInfo(con, resp, agent):
2008-12-02 15:53:23 +00:00
Iq('get',NS_REGISTER,to=agent)
if not isResultNode(resp):
error_msg = resp.getErrorMsg()
con.Event(NS_REGISTER,REGISTER_DATA_RECEIVED,(agent,None,False,error_msg))
return
tag=resp.getTag('query',namespace=NS_REGISTER)
if not tag:
error_msg = resp.getErrorMsg()
con.Event(NS_REGISTER,REGISTER_DATA_RECEIVED,(agent,None,False,error_msg))
return
df=tag.getTag('x',namespace=NS_DATA)
if df:
2007-06-06 21:40:56 +00:00
con.Event(NS_REGISTER,REGISTER_DATA_RECEIVED,(agent,df,True,''))
return
2007-06-06 21:40:56 +00:00
df={}
for i in resp.getQueryPayload():
2006-11-19 23:58:14 +00:00
if not isinstance(i, Node):
2007-06-06 21:40:56 +00:00
continue
df[i.getName()] = i.getData()
con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent,df,False,''))
def register(disp, host, info, cb):
'''
Perform registration on remote server with provided info.
If registration fails you can get additional info from the dispatcher's
owner attributes lastErrNode, lastErr and lastErrCode.
'''
iq=Iq('set', NS_REGISTER, to=host)
if not isinstance(info, dict):
info=info.asDict()
for i in info.keys():
iq.setTag('query').setTagData(i,info[i])
disp.SendAndCallForResponse(iq, cb)
def unregister(disp, host, cb):
'''
Unregisters with host (permanently removes account). Returns true on success
'''
iq = Iq('set', NS_REGISTER, to=host, payload=[Node('remove')])
_on_default_response(disp, iq, cb)
def changePasswordTo(disp, newpassword, host=None, cb = None):
'''
Changes password on specified or current (if not specified) server.
Returns true on success.
'''
if not host:
host = disp._owner.Server
iq = Iq('set',NS_REGISTER,to=host, payload=[Node('username',
payload=[disp._owner.Server]),Node('password',payload=[newpassword])])
_on_default_response(disp, iq, cb)
###############################################################################
### Privacy List
###############################################################################
PL_TYPE_JID = 'jid'
PL_TYPE_GROUP = 'group'
PL_TYPE_SUBC = 'subscription'
PL_ACT_ALLOW = 'allow'
PL_ACT_DENY = 'deny'
PRIVACY_LISTS_RECEIVED = 'PRIVACY LISTS RECEIVED'
PRIVACY_LIST_RECEIVED = 'PRIVACY LIST RECEIVED'
PRIVACY_LISTS_ACTIVE_DEFAULT = 'PRIVACY LISTS ACTIVE DEFAULT'
def getPrivacyLists(disp):
'''
Requests privacy lists from connected server.
Returns dictionary of existing lists on success.
'''
iq = Iq('get', NS_PRIVACY)
def _on_response(resp):
2008-12-03 17:16:04 +00:00
dict_ = {'lists': []}
if not isResultNode(resp):
disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (False))
return
2008-12-03 17:16:04 +00:00
for list_ in resp.getQueryPayload():
if list_.getName()=='list':
dict_['lists'].append(list_.getAttr('name'))
else:
2008-12-03 17:16:04 +00:00
dict_[list_.getName()]=list_.getAttr('name')
disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (dict_))
disp.SendAndCallForResponse(iq, _on_response)
def getActiveAndDefaultPrivacyLists(disp):
iq = Iq('get', NS_PRIVACY)
def _on_response(resp):
2008-12-03 17:16:04 +00:00
dict_ = {'active': '', 'default': ''}
if not isResultNode(resp):
disp.Event(NS_PRIVACY, PRIVACY_LISTS_ACTIVE_DEFAULT, (False))
return
2008-12-03 17:16:04 +00:00
for list_ in resp.getQueryPayload():
if list_.getName() == 'active':
dict_['active'] = list_.getAttr('name')
elif list_.getName() == 'default':
dict_['default'] = list_.getAttr('name')
disp.Event(NS_PRIVACY, PRIVACY_LISTS_ACTIVE_DEFAULT, (dict_))
disp.SendAndCallForResponse(iq, _on_response)
def getPrivacyList(disp, listname):
'''
Requests specific privacy list listname. Returns list of XML nodes (rules)
taken from the server responce.
'''
def _on_response(resp):
2008-12-03 21:56:12 +00:00
if not isResultNode(resp):
disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (False))
return
disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (resp))
iq = Iq('get', NS_PRIVACY, payload=[Node('list', {'name': listname})])
disp.SendAndCallForResponse(iq, _on_response)
def setActivePrivacyList(disp, listname=None, typ='active', cb=None):
'''
Switches privacy list 'listname' to specified type.
By default the type is 'active'. Returns true on success.
'''
2008-12-03 21:56:12 +00:00
if listname:
attrs={'name':listname}
2008-12-03 21:56:12 +00:00
else:
attrs={}
iq = Iq('set',NS_PRIVACY,payload=[Node(typ,attrs)])
_on_default_response(disp, iq, cb)
def setDefaultPrivacyList(disp, listname=None):
''' Sets the default privacy list as 'listname'. Returns true on success. '''
return setActivePrivacyList(disp, listname,'default')
def setPrivacyList(disp, listname, tags):
'''
Set the ruleset.
'list' should be the simpleXML node formatted according to RFC 3921 (XMPP-IM) I.e. Node('list',{'name':listname},payload=[...]). Returns true on success.
'''
iq = Iq('set', NS_PRIVACY, xmlns = '')
list_query = iq.getTag('query').setTag('list', {'name': listname})
for item in tags:
if 'type' in item and 'value' in item:
item_tag = list_query.setTag('item', {'action': item['action'],
'order': item['order'], 'type': item['type'],
'value': item['value']})
else:
item_tag = list_query.setTag('item', {'action': item['action'],
'order': item['order']})
if 'child' in item:
for child_tag in item['child']:
item_tag.setTag(child_tag)
_on_default_response(disp, iq, None)
def delPrivacyList(disp, listname, cb=None):
''' Deletes privacy list 'listname'. Returns true on success. '''
iq = Iq('set',NS_PRIVACY,payload=[Node('list',{'name':listname})])
_on_default_response(disp, iq, cb)
# vim: se ts=3: