2008-12-03 21:56:12 +00:00
|
|
|
## features.py
|
2006-02-03 12:17:34 +00:00
|
|
|
##
|
|
|
|
## Copyright (C) 2003-2004 Alexey "Snake" Nezhdanov
|
2007-07-11 21:39:27 +00:00
|
|
|
## Copyright (C) 2007 Julien Pivotto <roidelapluie@gmail.com>
|
2006-02-03 12:17:34 +00:00
|
|
|
##
|
|
|
|
## 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 $
|
|
|
|
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2008-12-27 11:45:58 +00:00
|
|
|
Different stuff that wasn't worth separating it into modules
|
2008-12-28 01:29:33 +00:00
|
|
|
(Registration, Privacy Lists, ...)
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2008-12-27 11:45:58 +00:00
|
|
|
|
2008-12-28 01:29:33 +00:00
|
|
|
from protocol import NS_REGISTER, NS_PRIVACY, NS_DATA, Iq, isResultNode, Node
|
2008-06-09 00:32:02 +00:00
|
|
|
|
2006-02-03 12:17:34 +00:00
|
|
|
def _on_default_response(disp, iq, cb):
|
|
|
|
def _on_response(resp):
|
2008-12-03 21:56:12 +00:00
|
|
|
if isResultNode(resp):
|
2006-02-03 12:17:34 +00:00
|
|
|
if cb:
|
2008-12-28 01:29:33 +00:00
|
|
|
cb(True)
|
2006-02-03 12:17:34 +00:00
|
|
|
elif cb:
|
|
|
|
cb(False)
|
|
|
|
disp.SendAndCallForResponse(iq, _on_response)
|
|
|
|
|
2008-12-28 01:29:33 +00:00
|
|
|
###############################################################################
|
|
|
|
### Registration
|
|
|
|
###############################################################################
|
2006-02-03 12:17:34 +00:00
|
|
|
|
2008-12-28 01:29:33 +00:00
|
|
|
REGISTER_DATA_RECEIVED = 'REGISTER DATA RECEIVED'
|
2008-12-03 21:56:12 +00:00
|
|
|
|
2006-02-03 12:17:34 +00:00
|
|
|
def getRegInfo(disp, host, info={}, sync=True):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Get registration form from remote host. Info dict can be prefilled
|
2008-12-28 01:29:33 +00:00
|
|
|
:param disp: plugged dispatcher instance
|
2009-06-15 21:00:14 +02:00
|
|
|
:param info: dict, like {'username':'joey'}.
|
2008-12-28 01:29:33 +00:00
|
|
|
|
|
|
|
See JEP-0077 for details.
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2006-02-03 12:17:34 +00:00
|
|
|
iq=Iq('get',NS_REGISTER,to=host)
|
2008-12-03 21:56:12 +00:00
|
|
|
for i in info.keys():
|
2006-02-03 12:17:34 +00:00
|
|
|
iq.setTagData(i,info[i])
|
|
|
|
if sync:
|
2008-12-28 01:29:33 +00:00
|
|
|
disp.SendAndCallForResponse(iq, lambda resp:
|
2008-12-28 14:53:20 +00:00
|
|
|
_ReceivedRegInfo(disp.Dispatcher, resp, host))
|
2008-12-03 21:56:12 +00:00
|
|
|
else:
|
2006-02-03 12:17:34 +00:00
|
|
|
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)
|
2006-11-22 19:49:41 +00:00
|
|
|
if not isResultNode(resp):
|
|
|
|
error_msg = resp.getErrorMsg()
|
|
|
|
con.Event(NS_REGISTER,REGISTER_DATA_RECEIVED,(agent,None,False,error_msg))
|
2006-02-03 12:17:34 +00:00
|
|
|
return
|
2006-03-15 08:56:36 +00:00
|
|
|
tag=resp.getTag('query',namespace=NS_REGISTER)
|
|
|
|
if not tag:
|
2006-11-22 19:49:41 +00:00
|
|
|
error_msg = resp.getErrorMsg()
|
|
|
|
con.Event(NS_REGISTER,REGISTER_DATA_RECEIVED,(agent,None,False,error_msg))
|
2006-03-15 08:56:36 +00:00
|
|
|
return
|
|
|
|
df=tag.getTag('x',namespace=NS_DATA)
|
2006-02-03 12:17:34 +00:00
|
|
|
if df:
|
2007-06-06 21:40:56 +00:00
|
|
|
con.Event(NS_REGISTER,REGISTER_DATA_RECEIVED,(agent,df,True,''))
|
2006-02-03 12:17:34 +00:00
|
|
|
return
|
2007-06-06 21:40:56 +00:00
|
|
|
df={}
|
2006-02-03 12:17:34 +00:00
|
|
|
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()
|
2006-11-22 19:49:41 +00:00
|
|
|
con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent,df,False,''))
|
2006-02-03 12:17:34 +00:00
|
|
|
|
|
|
|
def register(disp, host, info, cb):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Perform registration on remote server with provided info
|
2009-06-15 21:00:14 +02:00
|
|
|
|
2008-12-28 01:29:33 +00:00
|
|
|
If registration fails you can get additional info from the dispatcher's
|
|
|
|
owner attributes lastErrNode, lastErr and lastErrCode.
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2006-02-03 12:17:34 +00:00
|
|
|
iq=Iq('set', NS_REGISTER, to=host)
|
2006-07-03 20:49:20 +00:00
|
|
|
if not isinstance(info, dict):
|
2006-02-03 12:17:34 +00:00
|
|
|
info=info.asDict()
|
2006-07-03 20:49:20 +00:00
|
|
|
for i in info.keys():
|
2006-02-03 12:17:34 +00:00
|
|
|
iq.setTag('query').setTagData(i,info[i])
|
2006-03-26 23:42:36 +00:00
|
|
|
disp.SendAndCallForResponse(iq, cb)
|
2006-02-03 12:17:34 +00:00
|
|
|
|
|
|
|
def unregister(disp, host, cb):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2008-12-28 01:29:33 +00:00
|
|
|
Unregisters with host (permanently removes account). Returns true on success
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2006-02-03 12:17:34 +00:00
|
|
|
iq = Iq('set', NS_REGISTER, to=host, payload=[Node('remove')])
|
|
|
|
_on_default_response(disp, iq, cb)
|
|
|
|
|
|
|
|
def changePasswordTo(disp, newpassword, host=None, cb = None):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Changes password on specified or current (if not specified) server. Returns
|
|
|
|
true on success.
|
|
|
|
"""
|
2008-12-28 01:29:33 +00:00
|
|
|
if not host:
|
|
|
|
host = disp._owner.Server
|
2006-02-03 12:17:34 +00:00
|
|
|
iq = Iq('set',NS_REGISTER,to=host, payload=[Node('username',
|
|
|
|
payload=[disp._owner.Server]),Node('password',payload=[newpassword])])
|
|
|
|
_on_default_response(disp, iq, cb)
|
|
|
|
|
2008-12-28 01:29:33 +00:00
|
|
|
###############################################################################
|
|
|
|
### 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'
|
2006-02-03 12:17:34 +00:00
|
|
|
|
2006-07-03 20:49:20 +00:00
|
|
|
def getPrivacyLists(disp):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Request privacy lists from connected server. Returns dictionary of existing
|
|
|
|
lists on success.
|
|
|
|
"""
|
2006-02-03 12:17:34 +00:00
|
|
|
iq = Iq('get', NS_PRIVACY)
|
|
|
|
def _on_response(resp):
|
2008-12-03 17:16:04 +00:00
|
|
|
dict_ = {'lists': []}
|
2006-07-03 20:49:20 +00:00
|
|
|
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'))
|
2006-07-03 20:49:20 +00:00
|
|
|
else:
|
2008-12-03 17:16:04 +00:00
|
|
|
dict_[list_.getName()]=list_.getAttr('name')
|
|
|
|
disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (dict_))
|
2006-07-03 20:49:20 +00:00
|
|
|
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': ''}
|
2006-07-03 20:49:20 +00:00
|
|
|
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_))
|
2006-07-03 20:49:20 +00:00
|
|
|
disp.SendAndCallForResponse(iq, _on_response)
|
2006-02-03 12:17:34 +00:00
|
|
|
|
2006-07-03 20:49:20 +00:00
|
|
|
def getPrivacyList(disp, listname):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Request specific privacy list listname. Returns list of XML nodes (rules)
|
2008-12-28 01:29:33 +00:00
|
|
|
taken from the server responce.
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
2006-02-03 12:17:34 +00:00
|
|
|
def _on_response(resp):
|
2008-12-03 21:56:12 +00:00
|
|
|
if not isResultNode(resp):
|
2006-07-03 20:49:20 +00:00
|
|
|
disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (False))
|
|
|
|
return
|
|
|
|
disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (resp))
|
2006-02-03 12:17:34 +00:00
|
|
|
iq = Iq('get', NS_PRIVACY, payload=[Node('list', {'name': listname})])
|
|
|
|
disp.SendAndCallForResponse(iq, _on_response)
|
|
|
|
|
|
|
|
def setActivePrivacyList(disp, listname=None, typ='active', cb=None):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Switch 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:
|
2006-02-03 12:17:34 +00:00
|
|
|
attrs={'name':listname}
|
2008-12-03 21:56:12 +00:00
|
|
|
else:
|
2006-02-03 12:17:34 +00:00
|
|
|
attrs={}
|
|
|
|
iq = Iq('set',NS_PRIVACY,payload=[Node(typ,attrs)])
|
|
|
|
_on_default_response(disp, iq, cb)
|
|
|
|
|
|
|
|
def setDefaultPrivacyList(disp, listname=None):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Set the default privacy list as 'listname'. Returns true on success
|
|
|
|
"""
|
2006-02-03 12:17:34 +00:00
|
|
|
return setActivePrivacyList(disp, listname,'default')
|
|
|
|
|
2006-07-03 20:49:20 +00:00
|
|
|
def setPrivacyList(disp, listname, tags):
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
Set the ruleset
|
|
|
|
|
|
|
|
'list' should be the simpleXML node formatted according to RFC 3921
|
|
|
|
(XMPP-IM) I.e. Node('list',{'name':listname},payload=[...]).
|
2009-06-15 21:00:14 +02:00
|
|
|
|
2009-11-26 16:32:56 +02:00
|
|
|
Returns true on success.
|
|
|
|
"""
|
2006-07-03 20:49:20 +00:00
|
|
|
iq = Iq('set', NS_PRIVACY, xmlns = '')
|
|
|
|
list_query = iq.getTag('query').setTag('list', {'name': listname})
|
|
|
|
for item in tags:
|
2008-10-07 20:41:59 +00:00
|
|
|
if 'type' in item and 'value' in item:
|
2006-07-03 20:49:20 +00:00
|
|
|
item_tag = list_query.setTag('item', {'action': item['action'],
|
2009-06-15 21:00:14 +02:00
|
|
|
'order': item['order'], 'type': item['type'],
|
2008-12-28 01:29:33 +00:00
|
|
|
'value': item['value']})
|
2006-07-03 20:49:20 +00:00
|
|
|
else:
|
|
|
|
item_tag = list_query.setTag('item', {'action': item['action'],
|
|
|
|
'order': item['order']})
|
2008-10-07 20:41:59 +00:00
|
|
|
if 'child' in item:
|
2006-07-03 20:49:20 +00:00
|
|
|
for child_tag in item['child']:
|
|
|
|
item_tag.setTag(child_tag)
|
|
|
|
_on_default_response(disp, iq, None)
|
|
|
|
|
2008-12-28 01:29:33 +00:00
|
|
|
def delPrivacyList(disp, listname, cb=None):
|
|
|
|
''' Deletes privacy list 'listname'. Returns true on success. '''
|
2006-02-03 12:17:34 +00:00
|
|
|
iq = Iq('set',NS_PRIVACY,payload=[Node('list',{'name':listname})])
|
2007-01-11 20:21:53 +00:00
|
|
|
_on_default_response(disp, iq, cb)
|
2008-07-29 19:49:31 +00:00
|
|
|
|
2008-12-02 14:09:18 +00:00
|
|
|
# vim: se ts=3:
|