Add initial support for XEP-0016 Privacy Lists

This commit is contained in:
Lance Stout 2012-07-30 22:07:24 -07:00
parent f8856467d5
commit 7c6ef18e4f
5 changed files with 231 additions and 0 deletions

View File

@ -60,6 +60,7 @@ packages = [ 'sleekxmpp',
'sleekxmpp/plugins/xep_0009', 'sleekxmpp/plugins/xep_0009',
'sleekxmpp/plugins/xep_0009/stanza', 'sleekxmpp/plugins/xep_0009/stanza',
'sleekxmpp/plugins/xep_0012', 'sleekxmpp/plugins/xep_0012',
'sleekxmpp/plugins/xep_0016',
'sleekxmpp/plugins/xep_0027', 'sleekxmpp/plugins/xep_0027',
'sleekxmpp/plugins/xep_0030', 'sleekxmpp/plugins/xep_0030',
'sleekxmpp/plugins/xep_0030/stanza', 'sleekxmpp/plugins/xep_0030/stanza',

View File

@ -18,6 +18,7 @@ __all__ = [
'xep_0004', # Data Forms 'xep_0004', # Data Forms
'xep_0009', # Jabber-RPC 'xep_0009', # Jabber-RPC
'xep_0012', # Last Activity 'xep_0012', # Last Activity
'xep_0016', # Privacy Lists
'xep_0027', # Current Jabber OpenPGP Usage 'xep_0027', # Current Jabber OpenPGP Usage
'xep_0030', # Service Discovery 'xep_0030', # Service Discovery
'xep_0033', # Extended Stanza Addresses 'xep_0033', # Extended Stanza Addresses

View File

@ -0,0 +1,16 @@
"""
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
This file is part of SleekXMPP.
See the file LICENSE for copying permission.
"""
from sleekxmpp.plugins.base import register_plugin
from sleekxmpp.plugins.xep_0016 import stanza
from sleekxmpp.plugins.xep_0016.stanza import Privacy
from sleekxmpp.plugins.xep_0016.privacy import XEP_0016
register_plugin(XEP_0016)

View File

@ -0,0 +1,110 @@
"""
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
This file is part of SleekXMPP.
See the file LICENSE for copying permission.
"""
from sleekxmpp import Iq
from sleekxmpp.xmlstream import register_stanza_plugin
from sleekxmpp.plugins import BasePlugin
from sleekxmpp.plugins.xep_0016 import stanza
from sleekxmpp.plugins.xep_0016.stanza import Privacy, Item
class XEP_0016(BasePlugin):
name = 'xep_0016'
description = 'XEP-0016: Privacy Lists'
dependencies = set(['xep_0030'])
stanza = stanza
def plugin_init(self):
register_stanza_plugin(Iq, Privacy)
def plugin_end(self):
self.xmpp['xep_0030'].del_feature(feature=Privacy.namespace)
def session_bind(self, jid):
self.xmpp['xep_0030'].add_feature(Privacy.namespace)
def get_privacy_lists(self, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'get'
iq.enable('privacy')
return iq.send(block=block, timeout=timeout, callback=callback)
def get_list(self, name, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'get'
iq['privacy']['list']['name'] = name
return iq.send(block=block, timeout=timeout, callback=callback)
def get_active(self, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'get'
iq['privacy'].enable('active')
return iq.send(block=block, timeout=timeout, callback=callback)
def get_default(self, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'get'
iq['privacy'].enable('default')
return iq.send(block=block, timeout=timeout, callback=callback)
def activate(self, name, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'set'
iq['privacy']['active']['name'] = name
return iq.send(block=block, timeout=timeout, callback=callback)
def deactivate(self, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'set'
iq['privacy'].enable('active')
return iq.send(block=block, timeout=timeout, callback=callback)
def make_default(self, name, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'set'
iq['privacy']['default']['name'] = name
return iq.send(block=block, timeout=timeout, callback=callback)
def remove_default(self, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'set'
iq['privacy'].enable('default')
return iq.send(block=block, timeout=timeout, callback=callback)
def edit_list(self, name, rules, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'set'
iq['privacy']['list']['name'] = name
priv_list = iq['privacy']['list']
if not rules:
rules = []
for rule in rules:
if isinstance(rule, Item):
priv_list.append(rule)
continue
priv_list.add_item(
rule['value'],
rule['action'],
rule['order'],
itype=rule.get('type', None),
iq=rule.get('iq', None),
message=rule.get('message', None),
presence_in=rule.get('presence_in',
rule.get('presence-in', None)),
presence_out=rule.get('presence_out',
rule.get('presence-out', None)))
def remove_list(self, name, block=True, timeout=None, callback=None):
iq = self.xmpp.Iq()
iq['type'] = 'set'
iq['privacy']['list']['name'] = name
return iq.send(block=block, timeout=timeout, callback=callback)

View File

@ -0,0 +1,103 @@
from sleekxmpp.xmlstream import ET, ElementBase, register_stanza_plugin
class Privacy(ElementBase):
name = 'query'
namespace = 'jabber:iq:privacy'
plugin_attrib = 'privacy'
interfaces = set()
def add_list(self, name):
priv_list = List()
priv_list['name'] = name
self.append(priv_list)
return priv_list
class Active(ElementBase):
name = 'active'
namespace = 'jabber:iq:privacy'
plugin_attrib = name
interfaces = set(['name'])
class Default(ElementBase):
name = 'default'
namespace = 'jabber:iq:privacy'
plugin_attrib = name
interfaces = set(['name'])
class List(ElementBase):
name = 'list'
namespace = 'jabber:iq:privacy'
plugin_attrib = name
plugin_multi_attrib = 'lists'
interfaces = set(['name'])
def add_item(self, value, action, order, itype=None, iq=False,
message=False, presence_in=False, presence_out=False):
item = Item()
item.values = {'type': itype,
'value': value,
'action': action,
'order': order,
'message': message,
'iq': iq,
'presence_in': presence_in,
'presence_out': presence_out}
self.append(item)
return item
class Item(ElementBase):
name = 'item'
namespace = 'jabber:iq:privacy'
plugin_attrib = name
plugin_multi_attrib = 'items'
interfaces = set(['type', 'value', 'action', 'order', 'iq',
'message', 'presence_in', 'presence_out'])
bool_interfaces = set(['message', 'iq', 'presence_in', 'presence_out'])
type_values = ('', 'jid', 'group', 'subscription')
action_values = ('allow', 'deny')
def set_type(self, value):
if value and value not in self.type_values:
raise ValueError('Unknown type value: %s' % value)
else:
self._set_attr('type', value)
def set_action(self, value):
if value not in self.action_values:
raise ValueError('Unknown action value: %s' % value)
else:
self._set_attr('action', value)
def set_presence_in(self, value):
keep = True if value else False
self._set_sub_text('presence-in', '', keep=keep)
def get_presence_in(self):
pres = self.xml.find('{%s}presence-in' % self.namespace)
return pres is not None
def del_presence_in(self):
self._del_sub('{%s}presence-in' % self.namespace)
def set_presence_out(self, value):
keep = True if value else False
self._set_sub_text('presence-in', '', keep=keep)
def get_presence_out(self):
pres = self.xml.find('{%s}presence-in' % self.namespace)
return pres is not None
def del_presence_out(self):
self._del_sub('{%s}presence-in' % self.namespace)
register_stanza_plugin(Privacy, Active)
register_stanza_plugin(Privacy, Default)
register_stanza_plugin(Privacy, List, iterable=True)
register_stanza_plugin(List, Item, iterable=True)