Package common :: Package xmpp :: Module features_nb
[hide private]
[frames] | no frames]

Source Code for Module common.xmpp.features_nb

  1  ##   features.py 
  2  ## 
  3  ##   Copyright (C) 2003-2004 Alexey "Snake" Nezhdanov 
  4  ##   Copyright (C) 2007 Julien Pivotto <roidelapluie@gmail.com> 
  5  ## 
  6  ##   This program is free software; you can redistribute it and/or modify 
  7  ##   it under the terms of the GNU General Public License as published by 
  8  ##   the Free Software Foundation; either version 2, or (at your option) 
  9  ##   any later version. 
 10  ## 
 11  ##   This program is distributed in the hope that it will be useful, 
 12  ##   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  ##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  ##   GNU General Public License for more details. 
 15   
 16  # $Id: features.py,v 1.22 2005/09/30 20:13:04 mikealbon Exp $ 
 17   
 18  """ 
 19  Different stuff that wasn't worth separating it into modules 
 20  (Registration, Privacy Lists, ...) 
 21  """ 
 22   
 23  from protocol import NS_REGISTER, NS_PRIVACY, NS_DATA, Iq, isResultNode, Node 
 24   
25 -def _on_default_response(disp, iq, cb):
26 def _on_response(resp): 27 if isResultNode(resp): 28 if cb: 29 cb(True) 30 elif cb: 31 cb(False)
32 disp.SendAndCallForResponse(iq, _on_response) 33 34 ############################################################################### 35 ### Registration 36 ############################################################################### 37 38 REGISTER_DATA_RECEIVED = 'REGISTER DATA RECEIVED' 39
40 -def getRegInfo(disp, host, info={}, sync=True):
41 """ 42 Get registration form from remote host. Info dict can be prefilled 43 :param disp: plugged dispatcher instance 44 :param info: dict, like {'username':'joey'}. 45 46 See JEP-0077 for details. 47 """ 48 iq=Iq('get', NS_REGISTER, to=host) 49 for i in info.keys(): 50 iq.setTagData(i, info[i]) 51 if sync: 52 disp.SendAndCallForResponse(iq, lambda resp: 53 _ReceivedRegInfo(disp.Dispatcher, resp, host)) 54 else: 55 disp.SendAndCallForResponse(iq, _ReceivedRegInfo, {'agent': host })
56
57 -def _ReceivedRegInfo(con, resp, agent):
58 Iq('get', NS_REGISTER, to=agent) 59 if not isResultNode(resp): 60 error_msg = resp.getErrorMsg() 61 con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent, None, False, error_msg)) 62 return 63 tag=resp.getTag('query', namespace=NS_REGISTER) 64 if not tag: 65 error_msg = resp.getErrorMsg() 66 con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent, None, False, error_msg)) 67 return 68 df=tag.getTag('x', namespace=NS_DATA) 69 if df: 70 con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent, df, True, '')) 71 return 72 df={} 73 for i in resp.getQueryPayload(): 74 if not isinstance(i, Node): 75 continue 76 df[i.getName()] = i.getData() 77 con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent, df, False, ''))
78
79 -def register(disp, host, info, cb, args=None):
80 """ 81 Perform registration on remote server with provided info 82 83 If registration fails you can get additional info from the dispatcher's 84 owner attributes lastErrNode, lastErr and lastErrCode. 85 """ 86 iq=Iq('set', NS_REGISTER, to=host) 87 if not isinstance(info, dict): 88 info=info.asDict() 89 for i in info.keys(): 90 iq.setTag('query').setTagData(i, info[i]) 91 disp.SendAndCallForResponse(iq, cb, args)
92
93 -def unregister(disp, host, cb):
94 """ 95 Unregisters with host (permanently removes account). Returns true on success 96 """ 97 iq = Iq('set', NS_REGISTER, to=host, payload=[Node('remove')]) 98 _on_default_response(disp, iq, cb)
99
100 -def changePasswordTo(disp, newpassword, host=None, cb = None):
101 """ 102 Changes password on specified or current (if not specified) server. Returns 103 true on success. 104 """ 105 if not host: 106 host = disp._owner.Server 107 iq = Iq('set', NS_REGISTER, to=host, payload=[Node('username', 108 payload=[disp._owner.Server]), Node('password', payload=[newpassword])]) 109 _on_default_response(disp, iq, cb)
110 111 ############################################################################### 112 ### Privacy List 113 ############################################################################### 114 115 PL_TYPE_JID = 'jid' 116 PL_TYPE_GROUP = 'group' 117 PL_TYPE_SUBC = 'subscription' 118 PL_ACT_ALLOW = 'allow' 119 PL_ACT_DENY = 'deny' 120 121 PRIVACY_LISTS_RECEIVED = 'PRIVACY LISTS RECEIVED' 122 PRIVACY_LIST_RECEIVED = 'PRIVACY LIST RECEIVED' 123 PRIVACY_LISTS_ACTIVE_DEFAULT = 'PRIVACY LISTS ACTIVE DEFAULT' 124
125 -def getPrivacyLists(disp):
126 """ 127 Request privacy lists from connected server. Returns dictionary of existing 128 lists on success. 129 """ 130 iq = Iq('get', NS_PRIVACY) 131 def _on_response(resp): 132 dict_ = {'lists': []} 133 if not isResultNode(resp): 134 disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (False)) 135 return 136 for list_ in resp.getQueryPayload(): 137 if list_.getName()=='list': 138 dict_['lists'].append(list_.getAttr('name')) 139 else: 140 dict_[list_.getName()]=list_.getAttr('name') 141 disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (dict_))
142 disp.SendAndCallForResponse(iq, _on_response) 143
144 -def getActiveAndDefaultPrivacyLists(disp):
145 iq = Iq('get', NS_PRIVACY) 146 def _on_response(resp): 147 dict_ = {'active': '', 'default': ''} 148 if not isResultNode(resp): 149 disp.Event(NS_PRIVACY, PRIVACY_LISTS_ACTIVE_DEFAULT, (False)) 150 return 151 for list_ in resp.getQueryPayload(): 152 if list_.getName() == 'active': 153 dict_['active'] = list_.getAttr('name') 154 elif list_.getName() == 'default': 155 dict_['default'] = list_.getAttr('name') 156 disp.Event(NS_PRIVACY, PRIVACY_LISTS_ACTIVE_DEFAULT, (dict_))
157 disp.SendAndCallForResponse(iq, _on_response) 158
159 -def getPrivacyList(disp, listname):
160 """ 161 Request specific privacy list listname. Returns list of XML nodes (rules) 162 taken from the server responce. 163 """ 164 def _on_response(resp): 165 if not isResultNode(resp): 166 disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (False)) 167 return 168 disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (resp))
169 iq = Iq('get', NS_PRIVACY, payload=[Node('list', {'name': listname})]) 170 disp.SendAndCallForResponse(iq, _on_response) 171
172 -def setActivePrivacyList(disp, listname=None, typ='active', cb=None):
173 """ 174 Switch privacy list 'listname' to specified type. By default the type is 175 'active'. Returns true on success. 176 """ 177 if listname: 178 attrs={'name':listname} 179 else: 180 attrs={} 181 iq = Iq('set', NS_PRIVACY, payload=[Node(typ, attrs)]) 182 _on_default_response(disp, iq, cb)
183
184 -def setDefaultPrivacyList(disp, listname=None):
185 """ 186 Set the default privacy list as 'listname'. Returns true on success 187 """ 188 return setActivePrivacyList(disp, listname, 'default')
189
190 -def setPrivacyList(disp, listname, tags):
191 """ 192 Set the ruleset 193 194 'list' should be the simpleXML node formatted according to RFC 3921 195 (XMPP-IM) I.e. Node('list',{'name':listname},payload=[...]). 196 197 Returns true on success. 198 """ 199 iq = Iq('set', NS_PRIVACY, xmlns = '') 200 list_query = iq.getTag('query').setTag('list', {'name': listname}) 201 for item in tags: 202 if 'type' in item and 'value' in item: 203 item_tag = list_query.setTag('item', {'action': item['action'], 204 'order': item['order'], 'type': item['type'], 205 'value': item['value']}) 206 else: 207 item_tag = list_query.setTag('item', {'action': item['action'], 208 'order': item['order']}) 209 if 'child' in item: 210 for child_tag in item['child']: 211 item_tag.setTag(child_tag) 212 _on_default_response(disp, iq, None)
213
214 -def delPrivacyList(disp, listname, cb=None):
215 ''' Deletes privacy list 'listname'. Returns true on success. ''' 216 iq = Iq('set', NS_PRIVACY, payload=[Node('list', {'name':listname})]) 217 _on_default_response(disp, iq, cb)
218