Package nbxmpp :: Module features_nb |
[hide private]
[frames] | no frames]

# Source Code for Module nbxmpp.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              tag))
     72          return
     73      df={}
     74      for i in resp.getQueryPayload():
     75          if not isinstance(i, Node):
     76              continue
     77          df[i.getName()] = i.getData()
     78      con.Event(NS_REGISTER, REGISTER_DATA_RECEIVED, (agent, df, False, '', ''))
     79
     80 -def register(disp, host, info, cb, args=None):
     81      """
     82      Perform registration on remote server with provided info
     83
     84      If registration fails you can get additional info from the dispatcher's
     85      owner   attributes lastErrNode, lastErr and lastErrCode.
     86      """
     87      iq=Iq('set', NS_REGISTER, to=host)
     88      if not isinstance(info, dict):
     89          info=info.asDict()
     90      for i in info.keys():
     91          iq.setTag('query').setTagData(i, info[i])
     92      disp.SendAndCallForResponse(iq, cb, args)
     93
     94 -def unregister(disp, host, cb):
     95      """
     96      Unregisters with host (permanently removes account). Returns true on success
     97      """
     98      iq = Iq('set', NS_REGISTER, to=host, payload=[Node('remove')])
     99      _on_default_response(disp, iq, cb)
    100
    101 -def changePasswordTo(disp, newpassword, host=None, cb = None):
    102      """
    103      Changes password on specified or current (if not specified) server. Returns
    104      true on success.
    105      """
    106      if not host:
    107          host = disp._owner.Server
    108      iq = Iq('set', NS_REGISTER, to=host, payload=[Node('username',
    109                      payload=[disp._owner.Server]), Node('password', payload=[newpassword])])
    110      _on_default_response(disp, iq, cb)
    111
    112  ###############################################################################
    113  ### Privacy List
    114  ###############################################################################
    115
    116  PL_TYPE_JID = 'jid'
    117  PL_TYPE_GROUP = 'group'
    118  PL_TYPE_SUBC = 'subscription'
    119  PL_ACT_ALLOW = 'allow'
    120  PL_ACT_DENY = 'deny'
    121
    122  PRIVACY_LISTS_RECEIVED = 'PRIVACY LISTS RECEIVED'
    123  PRIVACY_LIST_RECEIVED = 'PRIVACY LIST RECEIVED'
    124  PRIVACY_LISTS_ACTIVE_DEFAULT = 'PRIVACY LISTS ACTIVE DEFAULT'
    125
    126 -def getPrivacyLists(disp):
    127      """
    128      Request privacy lists from connected server. Returns dictionary of existing
    129      lists on success.
    130      """
    131      iq = Iq('get', NS_PRIVACY)
    132      def _on_response(resp):
    133          dict_ = {'lists': []}
    134          if not isResultNode(resp):
    135              disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (False))
    136              return
    137          for list_ in resp.getQueryPayload():
    138              if list_.getName()=='list':
    139                  dict_['lists'].append(list_.getAttr('name'))
    140              else:
    141                  dict_[list_.getName()]=list_.getAttr('name')
    142          disp.Event(NS_PRIVACY, PRIVACY_LISTS_RECEIVED, (dict_))
    143      disp.SendAndCallForResponse(iq, _on_response)
    144
    145 -def getActiveAndDefaultPrivacyLists(disp):
    146      iq = Iq('get', NS_PRIVACY)
    147      def _on_response(resp):
    148          dict_ = {'active': '', 'default': ''}
    149          if not isResultNode(resp):
    150              disp.Event(NS_PRIVACY, PRIVACY_LISTS_ACTIVE_DEFAULT, (False))
    151              return
    152          for list_ in resp.getQueryPayload():
    153              if list_.getName() == 'active':
    154                  dict_['active'] = list_.getAttr('name')
    155              elif list_.getName() == 'default':
    156                  dict_['default'] = list_.getAttr('name')
    157          disp.Event(NS_PRIVACY, PRIVACY_LISTS_ACTIVE_DEFAULT, (dict_))
    158      disp.SendAndCallForResponse(iq, _on_response)
    159
    160 -def getPrivacyList(disp, listname):
    161      """
    162      Request specific privacy list listname. Returns list of XML nodes (rules)
    163      taken from the server responce.
    164      """
    165      def _on_response(resp):
    166          if not isResultNode(resp):
    167              disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (False))
    168              return
    169          disp.Event(NS_PRIVACY, PRIVACY_LIST_RECEIVED, (resp))
    170      iq = Iq('get', NS_PRIVACY, payload=[Node('list', {'name': listname})])
    171      disp.SendAndCallForResponse(iq, _on_response)
    172
    173 -def setActivePrivacyList(disp, listname=None, typ='active', cb=None):
    174      """
    175      Switch privacy list 'listname' to specified type. By default the type is
    176      'active'. Returns true on success.
    177      """
    178      if listname:
    179          attrs={'name':listname}
    180      else:
    181          attrs={}
    182      iq = Iq('set', NS_PRIVACY, payload=[Node(typ, attrs)])
    183      _on_default_response(disp, iq, cb)
    184
    185 -def setDefaultPrivacyList(disp, listname=None):
    186      """
    187      Set the default privacy list as 'listname'. Returns true on success
    188      """
    189      return setActivePrivacyList(disp, listname, 'default')
    190
    191 -def setPrivacyList(disp, listname, tags):
    192      """
    193      Set the ruleset
    194
    195      'list' should be the simpleXML node formatted according to RFC 3921
    196      (XMPP-IM) I.e. Node('list',{'name':listname},payload=[...]).
    197
    198      Returns true on success.
    199      """
    200      iq = Iq('set', NS_PRIVACY, xmlns = '')
    201      list_query = iq.getTag('query').setTag('list', {'name': listname})
    202      for item in tags:
    203          if 'type' in item and 'value' in item:
    204              item_tag = list_query.setTag('item', {'action': item['action'],
    205                      'order': item['order'], 'type': item['type'],
    206                      'value': item['value']})
    207          else:
    208              item_tag = list_query.setTag('item', {'action': item['action'],
    209                      'order': item['order']})
    210          if 'child' in item:
    211              for child_tag in item['child']:
    212                  item_tag.setTag(child_tag)
    213      _on_default_response(disp, iq, None)
    214
    215 -def delPrivacyList(disp, listname, cb=None):
    216      ''' Deletes privacy list 'listname'. Returns true on success. '''
    217      iq = Iq('set', NS_PRIVACY, payload=[Node('list', {'name':listname})])
    218      _on_default_response(disp, iq, cb)
    219