code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503
81 changed files with 21394 additions and 10488 deletions

View file

@ -2,6 +2,7 @@
# XXX: the Monkey Patch is here and not in src/__init__ to avoir issues with pyjamas compilation
import wokkel
from sat_tmp.wokkel import pubsub as tmp_pubsub, rsm as tmp_rsm, mam as tmp_mam
wokkel.pubsub = tmp_pubsub
wokkel.rsm = tmp_rsm
wokkel.mam = tmp_mam

View file

@ -20,18 +20,23 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from twisted.internet import defer
from wokkel import data_form
try:
from lxml import etree
except ImportError:
raise exceptions.MissingModule(u"Missing module lxml, please download/install it from http://lxml.de/")
raise exceptions.MissingModule(
u"Missing module lxml, please download/install it from http://lxml.de/"
)
import os.path
import uuid
import dbus
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
FD_NAME = "org.freedesktop.DBus"
@ -39,8 +44,12 @@ FD_PATH = "/org/freedekstop/DBus"
INTROSPECT_IFACE = "org.freedesktop.DBus.Introspectable"
INTROSPECT_METHOD = "Introspect"
IGNORED_IFACES_START = ('org.freedesktop', 'org.qtproject', 'org.kde.KMainWindow') # commands in interface starting with these values will be ignored
FLAG_LOOP = 'LOOP'
IGNORED_IFACES_START = (
"org.freedesktop",
"org.qtproject",
"org.kde.KMainWindow",
) # commands in interface starting with these values will be ignored
FLAG_LOOP = "LOOP"
PLUGIN_INFO = {
C.PI_NAME: "Ad-Hoc Commands - D-Bus",
@ -50,21 +59,25 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0050"],
C.PI_MAIN: "AdHocDBus",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Add D-Bus management to Ad-Hoc commands""")
C.PI_DESCRIPTION: _("""Add D-Bus management to Ad-Hoc commands"""),
}
class AdHocDBus(object):
def __init__(self, host):
log.info(_("plugin Ad-Hoc D-Bus initialization"))
self.host = host
host.bridge.addMethod("adHocDBusAddAuto", ".plugin", in_sign='sasasasasasass', out_sign='(sa(sss))',
method=self._adHocDBusAddAuto,
async=True)
host.bridge.addMethod(
"adHocDBusAddAuto",
".plugin",
in_sign="sasasasasasass",
out_sign="(sa(sss))",
method=self._adHocDBusAddAuto,
async=True,
)
self.session_bus = dbus.SessionBus()
self.fd_object = self.session_bus.get_object(FD_NAME, FD_PATH, introspect=False)
self.XEP_0050 = host.plugins['XEP-0050']
self.XEP_0050 = host.plugins["XEP-0050"]
def _DBusAsyncCall(self, proxy, method, *args, **kwargs):
""" Call a DBus method asynchronously and return a deferred
@ -77,9 +90,9 @@ class AdHocDBus(object):
"""
d = defer.Deferred()
interface = kwargs.pop('interface', None)
kwargs['reply_handler'] = lambda ret=None: d.callback(ret)
kwargs['error_handler'] = d.errback
interface = kwargs.pop("interface", None)
kwargs["reply_handler"] = lambda ret=None: d.callback(ret)
kwargs["error_handler"] = d.errback
proxy.get_dbus_method(method, dbus_interface=interface)(*args, **kwargs)
return d
@ -95,7 +108,9 @@ class AdHocDBus(object):
@return: True if the method is acceptable
"""
if method.xpath("arg[@direction='in']"): # we don't accept method with argument for the moment
if method.xpath(
"arg[@direction='in']"
): # we don't accept method with argument for the moment
return False
return True
@ -104,30 +119,61 @@ class AdHocDBus(object):
log.debug("introspecting path [%s]" % proxy.object_path)
introspect_xml = yield self._DBusIntrospect(proxy)
el = etree.fromstring(introspect_xml)
for node in el.iterchildren('node', 'interface'):
if node.tag == 'node':
new_path = os.path.join(proxy.object_path, node.get('name'))
new_proxy = self.session_bus.get_object(bus_name, new_path, introspect=False)
for node in el.iterchildren("node", "interface"):
if node.tag == "node":
new_path = os.path.join(proxy.object_path, node.get("name"))
new_proxy = self.session_bus.get_object(
bus_name, new_path, introspect=False
)
yield self._introspect(methods, bus_name, new_proxy)
elif node.tag == 'interface':
name = node.get('name')
elif node.tag == "interface":
name = node.get("name")
if any(name.startswith(ignored) for ignored in IGNORED_IFACES_START):
log.debug('interface [%s] is ignored' % name)
log.debug("interface [%s] is ignored" % name)
continue
log.debug("introspecting interface [%s]" % name)
for method in node.iterchildren('method'):
for method in node.iterchildren("method"):
if self._acceptMethod(method):
method_name = method.get('name')
method_name = method.get("name")
log.debug("method accepted: [%s]" % method_name)
methods.add((proxy.object_path, name, method_name))
def _adHocDBusAddAuto(self, prog_name, allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups, flags, profile_key):
return self.adHocDBusAddAuto(prog_name, allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups, flags, profile_key)
def _adHocDBusAddAuto(
self,
prog_name,
allowed_jids,
allowed_groups,
allowed_magics,
forbidden_jids,
forbidden_groups,
flags,
profile_key,
):
return self.adHocDBusAddAuto(
prog_name,
allowed_jids,
allowed_groups,
allowed_magics,
forbidden_jids,
forbidden_groups,
flags,
profile_key,
)
@defer.inlineCallbacks
def adHocDBusAddAuto(self, prog_name, allowed_jids=None, allowed_groups=None, allowed_magics=None, forbidden_jids=None, forbidden_groups=None, flags=None, profile_key=C.PROF_KEY_NONE):
def adHocDBusAddAuto(
self,
prog_name,
allowed_jids=None,
allowed_groups=None,
allowed_magics=None,
forbidden_jids=None,
forbidden_groups=None,
flags=None,
profile_key=C.PROF_KEY_NONE,
):
bus_names = yield self._DBusListNames()
bus_names = [bus_name for bus_name in bus_names if '.' + prog_name in bus_name]
bus_names = [bus_name for bus_name in bus_names if "." + prog_name in bus_name]
if not bus_names:
log.info("Can't find any bus for [%s]" % prog_name)
defer.returnValue(("", []))
@ -136,45 +182,62 @@ class AdHocDBus(object):
if bus_name.endswith(prog_name):
break
log.info("bus name found: [%s]" % bus_name)
proxy = self.session_bus.get_object(bus_name, '/', introspect=False)
proxy = self.session_bus.get_object(bus_name, "/", introspect=False)
methods = set()
yield self._introspect(methods, bus_name, proxy)
if methods:
self._addCommand(prog_name, bus_name, methods,
allowed_jids = allowed_jids,
allowed_groups = allowed_groups,
allowed_magics = allowed_magics,
forbidden_jids = forbidden_jids,
forbidden_groups = forbidden_groups,
flags = flags,
profile_key = profile_key)
self._addCommand(
prog_name,
bus_name,
methods,
allowed_jids=allowed_jids,
allowed_groups=allowed_groups,
allowed_magics=allowed_magics,
forbidden_jids=forbidden_jids,
forbidden_groups=forbidden_groups,
flags=flags,
profile_key=profile_key,
)
defer.returnValue((bus_name, methods))
def _addCommand(self, adhoc_name, bus_name, methods, allowed_jids=None, allowed_groups=None, allowed_magics=None, forbidden_jids=None, forbidden_groups=None, flags=None, profile_key=C.PROF_KEY_NONE):
def _addCommand(
self,
adhoc_name,
bus_name,
methods,
allowed_jids=None,
allowed_groups=None,
allowed_magics=None,
forbidden_jids=None,
forbidden_groups=None,
flags=None,
profile_key=C.PROF_KEY_NONE,
):
if flags is None:
flags = set()
def DBusCallback(command_elt, session_data, action, node, profile):
actions = session_data.setdefault('actions',[])
names_map = session_data.setdefault('names_map', {})
actions = session_data.setdefault("actions", [])
names_map = session_data.setdefault("names_map", {})
actions.append(action)
if len(actions) == 1:
# it's our first request, we ask the desired new status
status = self.XEP_0050.STATUS.EXECUTING
form = data_form.Form('form', title=_('Command selection'))
form = data_form.Form("form", title=_("Command selection"))
options = []
for path, iface, command in methods:
label = command.rsplit('.',1)[-1]
label = command.rsplit(".", 1)[-1]
name = str(uuid.uuid4())
names_map[name] = (path, iface, command)
options.append(data_form.Option(name, label))
field = data_form.Field('list-single', 'command', options=options, required=True)
field = data_form.Field(
"list-single", "command", options=options, required=True
)
form.addField(field)
payload = form.toElement()
@ -183,9 +246,9 @@ class AdHocDBus(object):
elif len(actions) == 2:
# we should have the answer here
try:
x_elt = command_elt.elements(data_form.NS_X_DATA,'x').next()
x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next()
answer_form = data_form.Form.fromElement(x_elt)
command = answer_form['command']
command = answer_form["command"]
except (KeyError, StopIteration):
raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD)
@ -202,9 +265,11 @@ class AdHocDBus(object):
# We have a loop, so we clear everything and we execute again the command as we had a first call (command_elt is not used, so None is OK)
del actions[:]
names_map.clear()
return DBusCallback(None, session_data, self.XEP_0050.ACTION.EXECUTE, node, profile)
form = data_form.Form('form', title=_(u'Updated'))
form.addField(data_form.Field('fixed', u'Command sent'))
return DBusCallback(
None, session_data, self.XEP_0050.ACTION.EXECUTE, node, profile
)
form = data_form.Form("form", title=_(u"Updated"))
form.addField(data_form.Field("fixed", u"Command sent"))
status = self.XEP_0050.STATUS.COMPLETED
payload = None
note = (self.XEP_0050.NOTE.INFO, _(u"Command sent"))
@ -213,10 +278,13 @@ class AdHocDBus(object):
return (payload, status, None, note)
self.XEP_0050.addAdHocCommand(DBusCallback, adhoc_name,
allowed_jids = allowed_jids,
allowed_groups = allowed_groups,
allowed_magics = allowed_magics,
forbidden_jids = forbidden_jids,
forbidden_groups = forbidden_groups,
profile_key = profile_key)
self.XEP_0050.addAdHocCommand(
DBusCallback,
adhoc_name,
allowed_jids=allowed_jids,
allowed_groups=allowed_groups,
allowed_magics=allowed_magics,
forbidden_jids=forbidden_jids,
forbidden_groups=forbidden_groups,
profile_key=profile_key,
)

View file

@ -21,6 +21,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.internet import defer
from twisted.web import client as web_client
@ -41,33 +42,36 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["IMPORT", "XEP-0060", "XEP-0277", "TEXT-SYNTAXES", "UPLOAD"],
C.PI_MAIN: "BlogImportPlugin",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Blog import management:
This plugin manage the different blog importers which can register to it, and handle generic importing tasks.""")
C.PI_DESCRIPTION: _(
u"""Blog import management:
This plugin manage the different blog importers which can register to it, and handle generic importing tasks."""
),
}
OPT_HOST = 'host'
OPT_UPLOAD_IMAGES = 'upload_images'
OPT_UPLOAD_IGNORE_HOST = 'upload_ignore_host'
OPT_IGNORE_TLS = 'ignore_tls_errors'
URL_REDIRECT_PREFIX = 'url_redirect_'
OPT_HOST = "host"
OPT_UPLOAD_IMAGES = "upload_images"
OPT_UPLOAD_IGNORE_HOST = "upload_ignore_host"
OPT_IGNORE_TLS = "ignore_tls_errors"
URL_REDIRECT_PREFIX = "url_redirect_"
class BlogImportPlugin(object):
BOOL_OPTIONS = (OPT_UPLOAD_IMAGES, OPT_IGNORE_TLS)
JSON_OPTIONS = ()
OPT_DEFAULTS = {OPT_UPLOAD_IMAGES: True,
OPT_IGNORE_TLS: False}
OPT_DEFAULTS = {OPT_UPLOAD_IMAGES: True, OPT_IGNORE_TLS: False}
def __init__(self, host):
log.info(_("plugin Blog Import initialization"))
self.host = host
self._u = host.plugins['UPLOAD']
self._p = host.plugins['XEP-0060']
self._m = host.plugins['XEP-0277']
self._s = self.host.plugins['TEXT-SYNTAXES']
host.plugins['IMPORT'].initialize(self, u'blog')
self._u = host.plugins["UPLOAD"]
self._p = host.plugins["XEP-0060"]
self._m = host.plugins["XEP-0277"]
self._s = self.host.plugins["TEXT-SYNTAXES"]
host.plugins["IMPORT"].initialize(self, u"blog")
def importItem(self, client, item_import_data, session, options, return_data, service, node):
def importItem(
self, client, item_import_data, session, options, return_data, service, node
):
"""importItem specialized for blog import
@param item_import_data(dict):
@ -99,51 +103,58 @@ class BlogImportPlugin(object):
@param return_data(dict): will contain link between former posts and new items
"""
mb_data = item_import_data['blog']
mb_data = item_import_data["blog"]
try:
item_id = mb_data['id']
item_id = mb_data["id"]
except KeyError:
item_id = mb_data['id'] = unicode(shortuuid.uuid())
item_id = mb_data["id"] = unicode(shortuuid.uuid())
try:
# we keep the link between old url and new blog item
# so the user can redirect its former blog urls
old_uri = item_import_data['url']
old_uri = item_import_data["url"]
except KeyError:
pass
else:
new_uri = return_data[URL_REDIRECT_PREFIX + old_uri] = self._p.getNodeURI(
service if service is not None else client.jid.userhostJID(),
node or self._m.namespace,
item_id)
log.info(u"url link from {old} to {new}".format(
old=old_uri, new=new_uri))
item_id,
)
log.info(u"url link from {old} to {new}".format(old=old_uri, new=new_uri))
return mb_data
@defer.inlineCallbacks
def importSubItems(self, client, item_import_data, mb_data, session, options):
# comments data
if len(item_import_data['comments']) != 1:
if len(item_import_data["comments"]) != 1:
raise NotImplementedError(u"can't manage multiple comment links")
allow_comments = C.bool(mb_data.get('allow_comments', C.BOOL_FALSE))
allow_comments = C.bool(mb_data.get("allow_comments", C.BOOL_FALSE))
if allow_comments:
comments_service = yield self._m.getCommentsService(client)
comments_node = self._m.getCommentsNode(mb_data['id'])
mb_data['comments_service'] = comments_service.full()
mb_data['comments_node'] = comments_node
comments_node = self._m.getCommentsNode(mb_data["id"])
mb_data["comments_service"] = comments_service.full()
mb_data["comments_node"] = comments_node
recurse_kwargs = {
'items_import_data':item_import_data['comments'][0],
'service':comments_service,
'node':comments_node}
"items_import_data": item_import_data["comments"][0],
"service": comments_service,
"node": comments_node,
}
defer.returnValue(recurse_kwargs)
else:
if item_import_data['comments'][0]:
raise exceptions.DataError(u"allow_comments set to False, but comments are there")
if item_import_data["comments"][0]:
raise exceptions.DataError(
u"allow_comments set to False, but comments are there"
)
defer.returnValue(None)
def publishItem(self, client, mb_data, service, node, session):
log.debug(u"uploading item [{id}]: {title}".format(id=mb_data['id'], title=mb_data.get('title','')))
log.debug(
u"uploading item [{id}]: {title}".format(
id=mb_data["id"], title=mb_data.get("title", "")
)
)
return self._m.send(client, mb_data, service, node)
@defer.inlineCallbacks
@ -161,54 +172,80 @@ class BlogImportPlugin(object):
return
# we want only XHTML content
for prefix in ('content',): # a tuple is use, if title need to be added in the future
for prefix in (
"content",
): # a tuple is use, if title need to be added in the future
try:
rich = mb_data['{}_rich'.format(prefix)]
rich = mb_data["{}_rich".format(prefix)]
except KeyError:
pass
else:
if '{}_xhtml'.format(prefix) in mb_data:
raise exceptions.DataError(u"importer gave {prefix}_rich and {prefix}_xhtml at the same time, this is not allowed".format(prefix=prefix))
if "{}_xhtml".format(prefix) in mb_data:
raise exceptions.DataError(
u"importer gave {prefix}_rich and {prefix}_xhtml at the same time, this is not allowed".format(
prefix=prefix
)
)
# we convert rich syntax to XHTML here, so we can handle filters easily
converted = yield self._s.convert(rich, self._s.getCurrentSyntax(client.profile), safe=False)
mb_data['{}_xhtml'.format(prefix)] = converted
del mb_data['{}_rich'.format(prefix)]
converted = yield self._s.convert(
rich, self._s.getCurrentSyntax(client.profile), safe=False
)
mb_data["{}_xhtml".format(prefix)] = converted
del mb_data["{}_rich".format(prefix)]
try:
mb_data['txt']
mb_data["txt"]
except KeyError:
pass
else:
if '{}_xhtml'.format(prefix) in mb_data:
log.warning(u"{prefix}_text will be replaced by converted {prefix}_xhtml, so filters can be handled".format(prefix=prefix))
del mb_data['{}_text'.format(prefix)]
if "{}_xhtml".format(prefix) in mb_data:
log.warning(
u"{prefix}_text will be replaced by converted {prefix}_xhtml, so filters can be handled".format(
prefix=prefix
)
)
del mb_data["{}_text".format(prefix)]
else:
log.warning(u"importer gave a text {prefix}, blog filters don't work on text {prefix}".format(prefix=prefix))
log.warning(
u"importer gave a text {prefix}, blog filters don't work on text {prefix}".format(
prefix=prefix
)
)
return
# at this point, we have only XHTML version of content
try:
top_elt = xml_tools.ElementParser()(mb_data['content_xhtml'], namespace=C.NS_XHTML)
top_elt = xml_tools.ElementParser()(
mb_data["content_xhtml"], namespace=C.NS_XHTML
)
except domish.ParserError:
# we clean the xml and try again our luck
cleaned = yield self._s.cleanXHTML(mb_data['content_xhtml'])
cleaned = yield self._s.cleanXHTML(mb_data["content_xhtml"])
top_elt = xml_tools.ElementParser()(cleaned, namespace=C.NS_XHTML)
opt_host = options.get(OPT_HOST)
if opt_host:
# we normalise the domain
parsed_host = urlparse.urlsplit(opt_host)
opt_host = urlparse.urlunsplit((parsed_host.scheme or 'http', parsed_host.netloc or parsed_host.path, '', '', ''))
opt_host = urlparse.urlunsplit(
(
parsed_host.scheme or "http",
parsed_host.netloc or parsed_host.path,
"",
"",
"",
)
)
tmp_dir = tempfile.mkdtemp()
try:
# TODO: would be nice to also update the hyperlinks to these images, e.g. when you have <a href="{url}"><img src="{url}"></a>
for img_elt in xml_tools.findAll(top_elt, names=[u'img']):
for img_elt in xml_tools.findAll(top_elt, names=[u"img"]):
yield self.imgFilters(client, img_elt, options, opt_host, tmp_dir)
finally:
os.rmdir(tmp_dir) # XXX: tmp_dir should be empty, or something went wrong
os.rmdir(tmp_dir) # XXX: tmp_dir should be empty, or something went wrong
# we now replace the content with filtered one
mb_data['content_xhtml'] = top_elt.toXml()
mb_data["content_xhtml"] = top_elt.toXml()
@defer.inlineCallbacks
def imgFilters(self, client, img_elt, options, opt_host, tmp_dir):
@ -222,15 +259,18 @@ class BlogImportPlugin(object):
@param tmp_dir(str): path to temp directory
"""
try:
url = img_elt['src']
if url[0] == u'/':
url = img_elt["src"]
if url[0] == u"/":
if not opt_host:
log.warning(u"host was not specified, we can't deal with src without host ({url}) and have to ignore the following <img/>:\n{xml}"
.format(url=url, xml=img_elt.toXml()))
log.warning(
u"host was not specified, we can't deal with src without host ({url}) and have to ignore the following <img/>:\n{xml}".format(
url=url, xml=img_elt.toXml()
)
)
return
else:
url = urlparse.urljoin(opt_host, url)
filename = url.rsplit('/',1)[-1].strip()
filename = url.rsplit("/", 1)[-1].strip()
if not filename:
raise KeyError
except (KeyError, IndexError):
@ -238,7 +278,7 @@ class BlogImportPlugin(object):
return
# we change the url for the normalized one
img_elt['src'] = url
img_elt["src"] = url
if options.get(OPT_UPLOAD_IMAGES, False):
# upload is requested
@ -250,23 +290,32 @@ class BlogImportPlugin(object):
# host is the ignored one, we skip
parsed_url = urlparse.urlsplit(url)
if ignore_host in parsed_url.hostname:
log.info(u"Don't upload image at {url} because of {opt} option".format(
url=url, opt=OPT_UPLOAD_IGNORE_HOST))
log.info(
u"Don't upload image at {url} because of {opt} option".format(
url=url, opt=OPT_UPLOAD_IGNORE_HOST
)
)
return
# we download images and re-upload them via XMPP
tmp_file = os.path.join(tmp_dir, filename).encode('utf-8')
upload_options = {'ignore_tls_errors': options.get(OPT_IGNORE_TLS, False)}
tmp_file = os.path.join(tmp_dir, filename).encode("utf-8")
upload_options = {"ignore_tls_errors": options.get(OPT_IGNORE_TLS, False)}
try:
yield web_client.downloadPage(url.encode('utf-8'), tmp_file)
filename = filename.replace(u'%', u'_') # FIXME: tmp workaround for a bug in prosody http upload
dummy, download_d = yield self._u.upload(client, tmp_file, filename, options=upload_options)
yield web_client.downloadPage(url.encode("utf-8"), tmp_file)
filename = filename.replace(
u"%", u"_"
) # FIXME: tmp workaround for a bug in prosody http upload
dummy, download_d = yield self._u.upload(
client, tmp_file, filename, options=upload_options
)
download_url = yield download_d
except Exception as e:
log.warning(u"can't download image at {url}: {reason}".format(url=url, reason=e))
log.warning(
u"can't download image at {url}: {reason}".format(url=url, reason=e)
)
else:
img_elt['src'] = download_url
img_elt["src"] = download_url
try:
os.unlink(tmp_file)

View file

@ -21,6 +21,7 @@
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from sat.tools import xml_tools
@ -33,14 +34,19 @@ import tempfile
import re
import time
import os.path
try:
from dokuwiki import DokuWiki, DokuWikiError # this is a new dependency
except ImportError:
raise exceptions.MissingModule(u'Missing module dokuwiki, please install it with "pip install dokuwiki"')
raise exceptions.MissingModule(
u'Missing module dokuwiki, please install it with "pip install dokuwiki"'
)
try:
from PIL import Image # this is already needed by plugin XEP-0054
except:
raise exceptions.MissingModule(u"Missing module pillow, please download/install it from https://python-pillow.github.io")
raise exceptions.MissingModule(
u"Missing module pillow, please download/install it from https://python-pillow.github.io"
)
PLUGIN_INFO = {
C.PI_NAME: "Dokuwiki import",
@ -49,12 +55,13 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["BLOG_IMPORT"],
C.PI_MAIN: "DokuwikiImport",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Blog importer for Dokuwiki blog engine.""")
C.PI_DESCRIPTION: _("""Blog importer for Dokuwiki blog engine."""),
}
SHORT_DESC = D_(u"import posts from Dokuwiki blog engine")
LONG_DESC = D_(u"""This importer handle Dokuwiki blog engine.
LONG_DESC = D_(
u"""This importer handle Dokuwiki blog engine.
To use it, you need an admin access to a running Dokuwiki website
(local or on the Internet). The importer retrieves the data using
@ -91,15 +98,17 @@ are inside the namespace "public:2015:10" using the Dokuwiki user
"souliane", and it imports them to sat profile dave's microblog node.
Internal Dokuwiki media that were hosted on http://127.0.1.1 are now
pointing to http://media.diekulturvermittlung.at.
""")
"""
)
DEFAULT_MEDIA_REPO = ""
DEFAULT_NAMESPACE = "/"
DEFAULT_LIMIT = 100 # you might get a DBUS timeout (no reply) if it lasts too long
class Importer(DokuWiki):
def __init__(self, url, user, passwd, media_repo=DEFAULT_MEDIA_REPO, limit=DEFAULT_LIMIT):
def __init__(
self, url, user, passwd, media_repo=DEFAULT_MEDIA_REPO, limit=DEFAULT_LIMIT
):
"""
@param url (unicode): DokuWiki site URL
@ -120,7 +129,7 @@ class Importer(DokuWiki):
@param post(dict): parsed post data
@return (unicode): post unique item id
"""
return unicode(post['id'])
return unicode(post["id"])
def getPostUpdated(self, post):
"""Return the update date.
@ -128,7 +137,7 @@ class Importer(DokuWiki):
@param post(dict): parsed post data
@return (unicode): update date
"""
return unicode(post['mtime'])
return unicode(post["mtime"])
def getPostPublished(self, post):
"""Try to parse the date from the message ID, else use "mtime".
@ -179,22 +188,23 @@ class Importer(DokuWiki):
# title = content.split("\n")[0].strip(u"\ufeff= ")
# build the extra data dictionary
mb_data = {"id": id_,
"published": published,
"updated": updated,
"author": profile_jid.user,
# "content": content, # when passed, it is displayed in Libervia instead of content_xhtml
"content_xhtml": content_xhtml,
# "title": title,
"allow_comments": "true",
}
mb_data = {
"id": id_,
"published": published,
"updated": updated,
"author": profile_jid.user,
# "content": content, # when passed, it is displayed in Libervia instead of content_xhtml
"content_xhtml": content_xhtml,
# "title": title,
"allow_comments": "true",
}
# find out if the message access is public or restricted
namespace = id_.split(":")[0]
if namespace and namespace.lower() not in ("public", "/"):
mb_data["group"] = namespace # roster group must exist
self.posts_data[id_] = {'blog': mb_data, 'comments':[[]]}
self.posts_data[id_] = {"blog": mb_data, "comments": [[]]}
def process(self, client, namespace=DEFAULT_NAMESPACE):
"""Process a namespace or a single page.
@ -206,7 +216,9 @@ class Importer(DokuWiki):
try:
pages_list = self.pages.list(namespace)
except DokuWikiError:
log.warning('Could not list Dokuwiki pages: please turn the "display_errors" setting to "Off" in the php.ini of the webserver hosting DokuWiki.')
log.warning(
'Could not list Dokuwiki pages: please turn the "display_errors" setting to "Off" in the php.ini of the webserver hosting DokuWiki.'
)
return
if not pages_list: # namespace is actually a page?
@ -220,7 +232,7 @@ class Importer(DokuWiki):
for page in pages_list:
self.processPost(page, profile_jid)
count += 1
if count >= self.limit :
if count >= self.limit:
break
return (self.posts_data.itervalues(), len(self.posts_data))
@ -334,14 +346,12 @@ class Importer(DokuWiki):
log.error("Cannot create DokuWiki media thumbnail %s" % dest)
class DokuwikiImport(object):
def __init__(self, host):
log.info(_("plugin Dokuwiki Import initialization"))
self.host = host
self._blog_import = host.plugins['BLOG_IMPORT']
self._blog_import.register('dokuwiki', self.DkImport, SHORT_DESC, LONG_DESC)
self._blog_import = host.plugins["BLOG_IMPORT"]
self._blog_import.register("dokuwiki", self.DkImport, SHORT_DESC, LONG_DESC)
def DkImport(self, client, location, options=None):
"""Import from DokuWiki to PubSub
@ -367,14 +377,22 @@ class DokuwikiImport(object):
try:
media_repo = options["media_repo"]
if opt_upload_images:
options[self._blog_import.OPT_UPLOAD_IMAGES] = False # force using --no-images-upload
info_msg = _("DokuWiki media files will be *downloaded* to {temp_dir} - to finish the import you have to upload them *manually* to {media_repo}")
options[
self._blog_import.OPT_UPLOAD_IMAGES
] = False # force using --no-images-upload
info_msg = _(
"DokuWiki media files will be *downloaded* to {temp_dir} - to finish the import you have to upload them *manually* to {media_repo}"
)
except KeyError:
media_repo = DEFAULT_MEDIA_REPO
if opt_upload_images:
info_msg = _("DokuWiki media files will be *uploaded* to the XMPP server. Hyperlinks to these media may not been updated though.")
info_msg = _(
"DokuWiki media files will be *uploaded* to the XMPP server. Hyperlinks to these media may not been updated though."
)
else:
info_msg = _("DokuWiki media files will *stay* on {location} - some of them may be protected by DokuWiki ACL and will not be accessible.")
info_msg = _(
"DokuWiki media files will *stay* on {location} - some of them may be protected by DokuWiki ACL and will not be accessible."
)
try:
namespace = options["namespace"]
@ -386,7 +404,11 @@ class DokuwikiImport(object):
limit = DEFAULT_LIMIT
dk_importer = Importer(location, user, passwd, media_repo, limit)
info_msg = info_msg.format(temp_dir=dk_importer.temp_dir, media_repo=media_repo, location=location)
self.host.actionNew({'xmlui': xml_tools.note(info_msg).toXml()}, profile=client.profile)
info_msg = info_msg.format(
temp_dir=dk_importer.temp_dir, media_repo=media_repo, location=location
)
self.host.actionNew(
{"xmlui": xml_tools.note(info_msg).toXml()}, profile=client.profile
)
d = threads.deferToThread(dk_importer.process, client, namespace)
return d

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from sat.tools.common import data_format
@ -38,12 +39,13 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["BLOG_IMPORT"],
C.PI_MAIN: "DotclearImport",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Blog importer for Dotclear blog engine.""")
C.PI_DESCRIPTION: _("""Blog importer for Dotclear blog engine."""),
}
SHORT_DESC = D_(u"import posts from Dotclear blog engine")
LONG_DESC = D_(u"""This importer handle Dotclear blog engine.
LONG_DESC = D_(
u"""This importer handle Dotclear blog engine.
To use it, you'll need to export your blog to a flat file.
You must go in your admin interface and select Plugins/Maintenance then Backup.
@ -51,15 +53,20 @@ Export only one blog if you have many, i.e. select "Download database of current
Depending on your configuration, your may need to use Import/Export plugin and export as a flat file.
location: you must use the absolute path to your backup for the location parameter
""")
"""
)
POST_ID_PREFIX = u"sat_dc_"
KNOWN_DATA_TYPES = ('link', 'setting', 'post', 'meta', 'media', 'post_media', 'comment', 'captcha')
ESCAPE_MAP = {
'r': u'\r',
'n': u'\n',
'"': u'"',
'\\': u'\\',
}
KNOWN_DATA_TYPES = (
"link",
"setting",
"post",
"meta",
"media",
"post_media",
"comment",
"captcha",
)
ESCAPE_MAP = {"r": u"\r", "n": u"\n", '"': u'"', "\\": u"\\"}
class DotclearParser(object):
@ -76,7 +83,13 @@ class DotclearParser(object):
@param post(dict): parsed post data
@return (unicode): post unique item id
"""
return u"{}_{}_{}_{}:{}".format(POST_ID_PREFIX, post['blog_id'], post['user_id'], post['post_id'], post['post_url'])
return u"{}_{}_{}_{}:{}".format(
POST_ID_PREFIX,
post["blog_id"],
post["user_id"],
post["post_id"],
post["post_url"],
)
def getCommentId(self, comment):
"""Return a unique and constant comment id
@ -84,9 +97,9 @@ class DotclearParser(object):
@param comment(dict): parsed comment
@return (unicode): comment unique comment id
"""
post_id = comment['post_id']
parent_item_id = self.posts_data[post_id]['blog']['id']
return u"{}_comment_{}".format(parent_item_id, comment['comment_id'])
post_id = comment["post_id"]
parent_item_id = self.posts_data[post_id]["blog"]["id"]
return u"{}_comment_{}".format(parent_item_id, comment["comment_id"])
def getTime(self, data, key):
"""Parse time as given by dotclear, with timezone handling
@ -112,18 +125,18 @@ class DotclearParser(object):
if char == '"':
# we have reached the end of this field,
# we try to parse a new one
yield u''.join(buf)
yield u"".join(buf)
buf = []
idx += 1
try:
separator = fields_data[idx]
except IndexError:
return
if separator != u',':
if separator != u",":
raise exceptions.ParsingError("Field separator was expeceted")
idx += 1
break # we have a new field
elif char == u'\\':
break # we have a new field
elif char == u"\\":
idx += 1
try:
char = ESCAPE_MAP[fields_data[idx]]
@ -139,55 +152,65 @@ class DotclearParser(object):
def postHandler(self, headers, data, index):
post = self.parseFields(headers, data)
log.debug(u'({}) post found: {}'.format(index, post['post_title']))
mb_data = {'id': self.getPostId(post),
'published': self.getTime(post, 'post_creadt'),
'updated': self.getTime(post, 'post_upddt'),
'author': post['user_id'], # there use info are not in the archive
# TODO: option to specify user info
'content_xhtml': u"{}{}".format(post['post_content_xhtml'], post['post_excerpt_xhtml']),
'title': post['post_title'],
'allow_comments': C.boolConst(bool(int(post['post_open_comment']))),
}
self.posts_data[post['post_id']] = {'blog': mb_data, 'comments':[[]], 'url': u'/post/{}'.format(post['post_url'])}
log.debug(u"({}) post found: {}".format(index, post["post_title"]))
mb_data = {
"id": self.getPostId(post),
"published": self.getTime(post, "post_creadt"),
"updated": self.getTime(post, "post_upddt"),
"author": post["user_id"], # there use info are not in the archive
# TODO: option to specify user info
"content_xhtml": u"{}{}".format(
post["post_content_xhtml"], post["post_excerpt_xhtml"]
),
"title": post["post_title"],
"allow_comments": C.boolConst(bool(int(post["post_open_comment"]))),
}
self.posts_data[post["post_id"]] = {
"blog": mb_data,
"comments": [[]],
"url": u"/post/{}".format(post["post_url"]),
}
def metaHandler(self, headers, data, index):
meta = self.parseFields(headers, data)
if meta['meta_type'] == 'tag':
tags = self.tags.setdefault(meta['post_id'], set())
tags.add(meta['meta_id'])
if meta["meta_type"] == "tag":
tags = self.tags.setdefault(meta["post_id"], set())
tags.add(meta["meta_id"])
def metaFinishedHandler(self):
for post_id, tags in self.tags.iteritems():
data_format.iter2dict('tag', tags, self.posts_data[post_id]['blog'])
data_format.iter2dict("tag", tags, self.posts_data[post_id]["blog"])
del self.tags
def commentHandler(self, headers, data, index):
comment = self.parseFields(headers, data)
if comment['comment_site']:
if comment["comment_site"]:
# we don't use atom:uri because it's used for jid in XMPP
content = u'{}\n<hr>\n<a href="{}">author website</a>'.format(
comment['comment_content'],
cgi.escape(comment['comment_site']).replace('"', u'%22'))
comment["comment_content"],
cgi.escape(comment["comment_site"]).replace('"', u"%22"),
)
else:
content = comment['comment_content']
mb_data = {'id': self.getCommentId(comment),
'published': self.getTime(comment, 'comment_dt'),
'updated': self.getTime(comment, 'comment_upddt'),
'author': comment['comment_author'],
# we don't keep email addresses to avoid the author to be spammed
# (they would be available publicly else)
# 'author_email': comment['comment_email'],
'content_xhtml': content,
}
self.posts_data[comment['post_id']]['comments'][0].append(
{'blog': mb_data, 'comments': [[]]})
content = comment["comment_content"]
mb_data = {
"id": self.getCommentId(comment),
"published": self.getTime(comment, "comment_dt"),
"updated": self.getTime(comment, "comment_upddt"),
"author": comment["comment_author"],
# we don't keep email addresses to avoid the author to be spammed
# (they would be available publicly else)
# 'author_email': comment['comment_email'],
"content_xhtml": content,
}
self.posts_data[comment["post_id"]]["comments"][0].append(
{"blog": mb_data, "comments": [[]]}
)
def parse(self, db_path):
with open(db_path) as f:
signature = f.readline().decode('utf-8')
signature = f.readline().decode("utf-8")
try:
version = signature.split('|')[1]
version = signature.split("|")[1]
except IndexError:
version = None
log.debug(u"Dotclear version: {}".format(version))
@ -195,20 +218,20 @@ class DotclearParser(object):
data_headers = None
index = None
while True:
buf = f.readline().decode('utf-8')
buf = f.readline().decode("utf-8")
if not buf:
break
if buf.startswith('['):
header = buf.split(' ', 1)
if buf.startswith("["):
header = buf.split(" ", 1)
data_type = header[0][1:]
if data_type not in KNOWN_DATA_TYPES:
log.warning(u"unkown data type: {}".format(data_type))
index = 0
try:
data_headers = header[1].split(',')
data_headers = header[1].split(",")
# we need to remove the ']' from the last header
last_header = data_headers[-1]
data_headers[-1] = last_header[:last_header.rfind(']')]
data_headers[-1] = last_header[: last_header.rfind("]")]
except IndexError:
log.warning(u"Can't read data)")
else:
@ -217,7 +240,9 @@ class DotclearParser(object):
buf = buf.strip()
if not buf and data_type in KNOWN_DATA_TYPES:
try:
finished_handler = getattr(self, '{}FinishedHandler'.format(data_type))
finished_handler = getattr(
self, "{}FinishedHandler".format(data_type)
)
except AttributeError:
pass
else:
@ -227,7 +252,7 @@ class DotclearParser(object):
continue
assert data_type
try:
fields_handler = getattr(self, '{}Handler'.format(data_type))
fields_handler = getattr(self, "{}Handler".format(data_type))
except AttributeError:
pass
else:
@ -237,15 +262,18 @@ class DotclearParser(object):
class DotclearImport(object):
def __init__(self, host):
log.info(_("plugin Dotclear Import initialization"))
self.host = host
host.plugins['BLOG_IMPORT'].register('dotclear', self.DcImport, SHORT_DESC, LONG_DESC)
host.plugins["BLOG_IMPORT"].register(
"dotclear", self.DcImport, SHORT_DESC, LONG_DESC
)
def DcImport(self, client, location, options=None):
if not os.path.isabs(location):
raise exceptions.DataError(u"An absolute path to backup data need to be given as location")
raise exceptions.DataError(
u"An absolute path to backup data need to be given as location"
)
dc_parser = DotclearParser()
d = threads.deferToThread(dc_parser.parse, location)
return d

View file

@ -21,6 +21,7 @@ from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core import exceptions
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools.common import regex
from sat.tools.common import uri
@ -41,29 +42,38 @@ PLUGIN_INFO = {
C.PI_MODES: [C.PLUG_MODE_COMPONENT],
C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["FILE", "XEP-0231", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0264", "XEP-0329"],
C.PI_DEPENDENCIES: [
"FILE",
"XEP-0231",
"XEP-0234",
"XEP-0260",
"XEP-0261",
"XEP-0264",
"XEP-0329",
],
C.PI_RECOMMENDATIONS: [],
C.PI_MAIN: "FileSharing",
C.PI_HANDLER: C.BOOL_TRUE,
C.PI_DESCRIPTION: _(u"""Component hosting and sharing files""")
C.PI_DESCRIPTION: _(u"""Component hosting and sharing files"""),
}
HASH_ALGO = u'sha-256'
NS_COMMENTS = 'org.salut-a-toi.comments'
COMMENT_NODE_PREFIX = 'org.salut-a-toi.file_comments/'
HASH_ALGO = u"sha-256"
NS_COMMENTS = "org.salut-a-toi.comments"
COMMENT_NODE_PREFIX = "org.salut-a-toi.file_comments/"
class FileSharing(object):
def __init__(self, host):
log.info(_(u"File Sharing initialization"))
self.host = host
self._f = host.plugins['FILE']
self._jf = host.plugins['XEP-0234']
self._h = host.plugins['XEP-0300']
self._t = host.plugins['XEP-0264']
self._f = host.plugins["FILE"]
self._jf = host.plugins["XEP-0234"]
self._h = host.plugins["XEP-0300"]
self._t = host.plugins["XEP-0264"]
host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000)
host.trigger.add(
"XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000
)
host.trigger.add("XEP-0234_buildFileElement", self._addFileComments)
host.trigger.add("XEP-0234_parseFileElement", self._getFileComments)
host.trigger.add("XEP-0329_compGetFilesFromNode", self._addCommentsData)
@ -74,9 +84,10 @@ class FileSharing(object):
def profileConnected(self, client):
path = client.file_tmp_dir = os.path.join(
self.host.memory.getConfig('', 'local_dir'),
self.host.memory.getConfig("", "local_dir"),
C.FILES_TMP_DIR,
regex.pathEscape(client.profile))
regex.pathEscape(client.profile),
)
if not os.path.exists(path):
os.makedirs(path)
@ -87,155 +98,195 @@ class FileSharing(object):
on file is received, this method create hash/thumbnails if necessary
move the file to the right location, and create metadata entry in database
"""
name = file_data[u'name']
name = file_data[u"name"]
extra = {}
if file_data[u'hash_algo'] == HASH_ALGO:
if file_data[u"hash_algo"] == HASH_ALGO:
log.debug(_(u"Reusing already generated hash"))
file_hash = file_data[u'hash_hasher'].hexdigest()
file_hash = file_data[u"hash_hasher"].hexdigest()
else:
hasher = self._h.getHasher(HASH_ALGO)
with open('file_path') as f:
with open("file_path") as f:
file_hash = yield self._h.calculateHash(f, hasher)
final_path = os.path.join(self.files_path, file_hash)
if os.path.isfile(final_path):
log.debug(u"file [{file_hash}] already exists, we can remove temporary one".format(file_hash = file_hash))
log.debug(
u"file [{file_hash}] already exists, we can remove temporary one".format(
file_hash=file_hash
)
)
os.unlink(file_path)
else:
os.rename(file_path, final_path)
log.debug(u"file [{file_hash}] moved to {files_path}".format(file_hash=file_hash, files_path=self.files_path))
log.debug(
u"file [{file_hash}] moved to {files_path}".format(
file_hash=file_hash, files_path=self.files_path
)
)
mime_type = file_data.get(u'mime_type')
if not mime_type or mime_type == u'application/octet-stream':
mime_type = file_data.get(u"mime_type")
if not mime_type or mime_type == u"application/octet-stream":
mime_type = mimetypes.guess_type(name)[0]
if mime_type is not None and mime_type.startswith(u'image'):
if mime_type is not None and mime_type.startswith(u"image"):
thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
for max_thumb_size in (self._t.SIZE_SMALL, self._t.SIZE_MEDIUM):
try:
thumb_size, thumb_id = yield self._t.generateThumbnail(final_path,
max_thumb_size,
# we keep thumbnails for 6 months
60*60*24*31*6)
thumb_size, thumb_id = yield self._t.generateThumbnail(
final_path,
max_thumb_size,
#  we keep thumbnails for 6 months
60 * 60 * 24 * 31 * 6,
)
except Exception as e:
log.warning(_(u"Can't create thumbnail: {reason}").format(reason=e))
break
thumbnails.append({u'id': thumb_id, u'size': thumb_size})
thumbnails.append({u"id": thumb_id, u"size": thumb_size})
self.host.memory.setFile(client,
name=name,
version=u'',
file_hash=file_hash,
hash_algo=HASH_ALGO,
size=file_data[u'size'],
path=file_data.get(u'path'),
namespace=file_data.get(u'namespace'),
mime_type=mime_type,
owner=peer_jid,
extra=extra)
self.host.memory.setFile(
client,
name=name,
version=u"",
file_hash=file_hash,
hash_algo=HASH_ALGO,
size=file_data[u"size"],
path=file_data.get(u"path"),
namespace=file_data.get(u"namespace"),
mime_type=mime_type,
owner=peer_jid,
extra=extra,
)
def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object):
def _getDestDirTrigger(
self, client, peer_jid, transfer_data, file_data, stream_object
):
"""This trigger accept file sending request, and store file locally"""
if not client.is_component:
return True, None
assert stream_object
assert 'stream_object' not in transfer_data
assert "stream_object" not in transfer_data
assert C.KEY_PROGRESS_ID in file_data
filename = file_data['name']
assert filename and not '/' in filename
file_tmp_dir = self.host.getLocalPath(client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False)
file_tmp_path = file_data['file_path'] = os.path.join(file_tmp_dir, file_data['name'])
filename = file_data["name"]
assert filename and not "/" in filename
file_tmp_dir = self.host.getLocalPath(
client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False
)
file_tmp_path = file_data["file_path"] = os.path.join(
file_tmp_dir, file_data["name"]
)
transfer_data['finished_d'].addCallback(self._fileTransferedCb, client, peer_jid, file_data, file_tmp_path)
transfer_data["finished_d"].addCallback(
self._fileTransferedCb, client, peer_jid, file_data, file_tmp_path
)
self._f.openFileWrite(client, file_tmp_path, transfer_data, file_data, stream_object)
self._f.openFileWrite(
client, file_tmp_path, transfer_data, file_data, stream_object
)
return False, defer.succeed(True)
@defer.inlineCallbacks
def _retrieveFiles(self, client, session, content_data, content_name, file_data, file_elt):
def _retrieveFiles(
self, client, session, content_data, content_name, file_data, file_elt
):
"""This method retrieve a file on request, and send if after checking permissions"""
peer_jid = session[u'peer_jid']
peer_jid = session[u"peer_jid"]
try:
found_files = yield self.host.memory.getFiles(client,
peer_jid=peer_jid,
name=file_data.get(u'name'),
file_hash=file_data.get(u'file_hash'),
hash_algo=file_data.get(u'hash_algo'),
path=file_data.get(u'path'),
namespace=file_data.get(u'namespace'))
found_files = yield self.host.memory.getFiles(
client,
peer_jid=peer_jid,
name=file_data.get(u"name"),
file_hash=file_data.get(u"file_hash"),
hash_algo=file_data.get(u"hash_algo"),
path=file_data.get(u"path"),
namespace=file_data.get(u"namespace"),
)
except exceptions.NotFound:
found_files = None
except exceptions.PermissionError:
log.warning(_(u"{peer_jid} is trying to access an unauthorized file: {name}").format(
peer_jid=peer_jid, name=file_data.get(u'name')))
log.warning(
_(u"{peer_jid} is trying to access an unauthorized file: {name}").format(
peer_jid=peer_jid, name=file_data.get(u"name")
)
)
defer.returnValue(False)
if not found_files:
log.warning(_(u"no matching file found ({file_data})").format(file_data=file_data))
log.warning(
_(u"no matching file found ({file_data})").format(file_data=file_data)
)
defer.returnValue(False)
# we only use the first found file
found_file = found_files[0]
file_hash = found_file[u'file_hash']
file_hash = found_file[u"file_hash"]
file_path = os.path.join(self.files_path, file_hash)
file_data[u'hash_hasher'] = hasher = self._h.getHasher(found_file[u'hash_algo'])
size = file_data[u'size'] = found_file[u'size']
file_data[u'file_hash'] = file_hash
file_data[u'hash_algo'] = found_file[u'hash_algo']
file_data[u"hash_hasher"] = hasher = self._h.getHasher(found_file[u"hash_algo"])
size = file_data[u"size"] = found_file[u"size"]
file_data[u"file_hash"] = file_hash
file_data[u"hash_algo"] = found_file[u"hash_algo"]
# we complete file_elt so peer can have some details on the file
if u'name' not in file_data:
file_elt.addElement(u'name', content=found_file[u'name'])
file_elt.addElement(u'size', content=unicode(size))
content_data['stream_object'] = stream.FileStreamObject(
if u"name" not in file_data:
file_elt.addElement(u"name", content=found_file[u"name"])
file_elt.addElement(u"size", content=unicode(size))
content_data["stream_object"] = stream.FileStreamObject(
self.host,
client,
file_path,
uid=self._jf.getProgressId(session, content_name),
size=size,
data_cb=lambda data: hasher.update(data),
)
)
defer.returnValue(True)
def _fileSendingRequestTrigger(self, client, session, content_data, content_name, file_data, file_elt):
def _fileSendingRequestTrigger(
self, client, session, content_data, content_name, file_data, file_elt
):
if not client.is_component:
return True, None
else:
return False, self._retrieveFiles(client, session, content_data, content_name, file_data, file_elt)
return (
False,
self._retrieveFiles(
client, session, content_data, content_name, file_data, file_elt
),
)
## comments triggers ##
def _addFileComments(self, file_elt, extra_args):
try:
comments_url = extra_args.pop('comments_url')
comments_url = extra_args.pop("comments_url")
except KeyError:
return
comment_elt = file_elt.addElement((NS_COMMENTS, 'comments'), content=comments_url)
comment_elt = file_elt.addElement((NS_COMMENTS, "comments"), content=comments_url)
try:
count = len(extra_args[u'extra'][u'comments'])
count = len(extra_args[u"extra"][u"comments"])
except KeyError:
count = 0
comment_elt['count'] = unicode(count)
comment_elt["count"] = unicode(count)
return True
def _getFileComments(self, file_elt, file_data):
try:
comments_elt = next(file_elt.elements(NS_COMMENTS, 'comments'))
comments_elt = next(file_elt.elements(NS_COMMENTS, "comments"))
except StopIteration:
return
file_data['comments_url'] = unicode(comments_elt)
file_data['comments_count'] = comments_elt['count']
file_data["comments_url"] = unicode(comments_elt)
file_data["comments_count"] = comments_elt["count"]
return True
def _addCommentsData(self, client, iq_elt, owner, node_path, files_data):
for file_data in files_data:
file_data['comments_url'] = uri.buildXMPPUri('pubsub',
path=client.jid.full(),
node=COMMENT_NODE_PREFIX + file_data['id'])
file_data["comments_url"] = uri.buildXMPPUri(
"pubsub",
path=client.jid.full(),
node=COMMENT_NODE_PREFIX + file_data["id"],
)
return True
@ -243,19 +294,21 @@ class Comments_handler(pubsub.PubSubService):
"""This class is a minimal Pubsub service handling virtual nodes for comments"""
def __init__(self, plugin_parent):
super(Comments_handler, self).__init__() # PubsubVirtualResource())
super(Comments_handler, self).__init__() # PubsubVirtualResource())
self.host = plugin_parent.host
self.plugin_parent = plugin_parent
self.discoIdentity = {'category': 'pubsub',
'type': 'virtual', # FIXME: non standard, here to avoid this service being considered as main pubsub one
'name': 'files commenting service'}
self.discoIdentity = {
"category": "pubsub",
"type": "virtual", # FIXME: non standard, here to avoid this service being considered as main pubsub one
"name": "files commenting service",
}
def _getFileId(self, nodeIdentifier):
if not nodeIdentifier.startswith(COMMENT_NODE_PREFIX):
raise error.StanzaError('item-not-found')
file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX):]
raise error.StanzaError("item-not-found")
file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX) :]
if not file_id:
raise error.StanzaError('item-not-found')
raise error.StanzaError("item-not-found")
return file_id
@defer.inlineCallbacks
@ -266,11 +319,11 @@ class Comments_handler(pubsub.PubSubService):
except (exceptions.NotFound, exceptions.PermissionError):
# we don't differenciate between NotFound and PermissionError
# to avoid leaking information on existing files
raise error.StanzaError('item-not-found')
raise error.StanzaError("item-not-found")
if not files:
raise error.StanzaError('item-not-found')
raise error.StanzaError("item-not-found")
if len(files) > 1:
raise error.InternalError('there should be only one file')
raise error.InternalError("there should be only one file")
defer.returnValue(files[0])
def commentsUpdate(self, extra, new_comments, peer_jid):
@ -280,8 +333,8 @@ class Comments_handler(pubsub.PubSubService):
@param new_comments(list[tuple(unicode, unicode, unicode)]): comments to update or insert
@param peer_jid(unicode, None): bare jid of the requestor, or None if request is done by owner
"""
current_comments = extra.setdefault('comments', [])
new_comments_by_id = {c[0]:c for c in new_comments}
current_comments = extra.setdefault("comments", [])
new_comments_by_id = {c[0]: c for c in new_comments}
updated = []
# we now check every current comment, to see if one id in new ones
# exist, in which case we must update
@ -306,7 +359,7 @@ class Comments_handler(pubsub.PubSubService):
def commentsDelete(self, extra, comments):
try:
comments_dict = extra['comments']
comments_dict = extra["comments"]
except KeyError:
return
for comment in comments:
@ -324,42 +377,43 @@ class Comments_handler(pubsub.PubSubService):
iq_elt = item_elt
while iq_elt.parent != None:
iq_elt = iq_elt.parent
return iq_elt['from']
return iq_elt["from"]
@defer.inlineCallbacks
def publish(self, requestor, service, nodeIdentifier, items):
# we retrieve file a first time to check authorisations
#  we retrieve file a first time to check authorisations
file_data = yield self.getFileData(requestor, nodeIdentifier)
file_id = file_data['id']
comments = [(item['id'], self._getFrom(item), item.toXml()) for item in items]
if requestor.userhostJID() == file_data['owner']:
file_id = file_data["id"]
comments = [(item["id"], self._getFrom(item), item.toXml()) for item in items]
if requestor.userhostJID() == file_data["owner"]:
peer_jid = None
else:
peer_jid = requestor.userhost()
update_cb = partial(self.commentsUpdate, new_comments=comments, peer_jid=peer_jid)
try:
yield self.host.memory.fileUpdate(file_id, 'extra', update_cb)
yield self.host.memory.fileUpdate(file_id, "extra", update_cb)
except exceptions.PermissionError:
raise error.StanzaError('not-authorized')
raise error.StanzaError("not-authorized")
@defer.inlineCallbacks
def items(self, requestor, service, nodeIdentifier, maxItems,
itemIdentifiers):
def items(self, requestor, service, nodeIdentifier, maxItems, itemIdentifiers):
file_data = yield self.getFileData(requestor, nodeIdentifier)
comments = file_data['extra'].get('comments', [])
comments = file_data["extra"].get("comments", [])
if itemIdentifiers:
defer.returnValue([generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers])
defer.returnValue(
[generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers]
)
else:
defer.returnValue([generic.parseXml(c[2]) for c in comments])
@defer.inlineCallbacks
def retract(self, requestor, service, nodeIdentifier, itemIdentifiers):
file_data = yield self.getFileData(requestor, nodeIdentifier)
file_id = file_data['id']
file_id = file_data["id"]
try:
comments = file_data['extra']['comments']
comments = file_data["extra"]["comments"]
except KeyError:
raise error.StanzaError('item-not-found')
raise error.StanzaError("item-not-found")
to_remove = []
for comment in comments:
@ -372,11 +426,11 @@ class Comments_handler(pubsub.PubSubService):
if itemIdentifiers:
# not all items have been to_remove, we can't continue
raise error.StanzaError('item-not-found')
raise error.StanzaError("item-not-found")
if requestor.userhostJID() != file_data['owner']:
if requestor.userhostJID() != file_data["owner"]:
if not all([c[1] == requestor.userhost() for c in to_remove]):
raise error.StanzaError('not-authorized')
raise error.StanzaError("not-authorized")
remove_cb = partial(self.commentsDelete, comments=to_remove)
yield self.host.memory.fileUpdate(file_id, 'extra', remove_cb)
yield self.host.memory.fileUpdate(file_id, "extra", remove_cb)

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.protocols.jabber import jid
from twisted.internet import reactor, protocol
@ -35,9 +36,10 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "CommandExport",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Implementation of command export""")
C.PI_DESCRIPTION: _("""Implementation of command export"""),
}
class ExportCommandProtocol(protocol.ProcessProtocol):
""" Try to register an account with prosody """
@ -49,26 +51,26 @@ class ExportCommandProtocol(protocol.ProcessProtocol):
def _clean(self, data):
if not data:
log.error ("data should not be empty !")
log.error("data should not be empty !")
return u""
decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None]
decoded = data.decode("utf-8", "ignore")[: -1 if data[-1] == "\n" else None]
return clean_ustr(decoded)
def connectionMade(self):
log.info("connectionMade :)")
def outReceived(self, data):
self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True)
self.client.sendMessage(self.target, {"": self._clean(data)}, no_trigger=True)
def errReceived(self, data):
self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True)
self.client.sendMessage(self.target, {"": self._clean(data)}, no_trigger=True)
def processEnded(self, reason):
log.info (u"process finished: %d" % (reason.value.exitCode,))
log.info(u"process finished: %d" % (reason.value.exitCode,))
self.parent.removeProcess(self.target, self)
def write(self, message):
self.transport.write(message.encode('utf-8'))
self.transport.write(message.encode("utf-8"))
def boolOption(self, key):
""" Get boolean value from options
@ -81,6 +83,7 @@ class ExportCommandProtocol(protocol.ProcessProtocol):
class CommandExport(object):
"""Command export plugin: export a command to an entity"""
# XXX: This plugin can be potentially dangerous if we don't trust entities linked
# this is specially true if we have other triggers.
# FIXME: spawned should be a client attribute, not a class one
@ -88,9 +91,15 @@ class CommandExport(object):
def __init__(self, host):
log.info(_("Plugin command export initialization"))
self.host = host
self.spawned = {} # key = entity
self.spawned = {} # key = entity
host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000)
host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand)
host.bridge.addMethod(
"exportCommand",
".plugin",
in_sign="sasasa{ss}s",
out_sign="",
method=self._exportCommand,
)
def removeProcess(self, entity, process):
""" Called when the process is finished
@ -100,7 +109,7 @@ class CommandExport(object):
processes_set = self.spawned[(entity, process.client.profile)]
processes_set.discard(process)
if not processes_set:
del(self.spawned[(entity, process.client.profile)])
del (self.spawned[(entity, process.client.profile)])
except ValueError:
pass
@ -111,12 +120,12 @@ class CommandExport(object):
if spawned_key in self.spawned:
try:
body = message_elt.elements(C.NS_CLIENT, 'body').next()
body = message_elt.elements(C.NS_CLIENT, "body").next()
except StopIteration:
# do not block message without body (chat state notification...)
return True
mess_data = unicode(body) + '\n'
mess_data = unicode(body) + "\n"
processes_set = self.spawned[spawned_key]
_continue = False
exclusive = False
@ -152,5 +161,7 @@ class CommandExport(object):
log.info(u"invalid target ignored: %s" % (target,))
continue
process_prot = ExportCommandProtocol(self, client, _jid, options)
self.spawned.setdefault((_jid, client.profile),set()).add(process_prot)
reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty'))
self.spawned.setdefault((_jid, client.profile), set()).add(process_prot)
reactor.spawnProcess(
process_prot, command, args, usePTY=process_prot.boolOption("pty")
)

View file

@ -21,6 +21,7 @@ from sat.core.i18n import _
from sat.core import exceptions
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools import utils
from sat.tools.common import uri as xmpp_uri
@ -45,14 +46,15 @@ PLUGIN_INFO = {
C.PI_RECOMMENDATIONS: ["INVITATIONS", "XEP-0277"],
C.PI_MAIN: "Events",
C.PI_HANDLER: "yes",
C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management""")
C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management"""),
}
NS_EVENT = 'org.salut-a-toi.event:0'
NS_EVENT_LIST = NS_EVENT + '#list'
NS_EVENT_INVIT = NS_EVENT + '#invitation'
NS_EVENT = "org.salut-a-toi.event:0"
NS_EVENT_LIST = NS_EVENT + "#list"
NS_EVENT_INVIT = NS_EVENT + "#invitation"
INVITATION = '/message[@type="chat"]/invitation[@xmlns="{ns_invit}"]'.format(
ns_invit=NS_EVENT_INVIT)
ns_invit=NS_EVENT_INVIT
)
class Events(object):
@ -64,40 +66,78 @@ class Events(object):
self._p = self.host.plugins["XEP-0060"]
self._i = self.host.plugins.get("INVITATIONS")
self._b = self.host.plugins.get("XEP-0277")
host.bridge.addMethod("eventGet", ".plugin",
in_sign='ssss', out_sign='(ia{ss})',
method=self._eventGet,
async=True)
host.bridge.addMethod("eventCreate", ".plugin",
in_sign='ia{ss}ssss', out_sign='s',
method=self._eventCreate,
async=True)
host.bridge.addMethod("eventModify", ".plugin",
in_sign='sssia{ss}s', out_sign='',
method=self._eventModify,
async=True)
host.bridge.addMethod("eventsList", ".plugin",
in_sign='sss', out_sign='aa{ss}',
method=self._eventsList,
async=True)
host.bridge.addMethod("eventInviteeGet", ".plugin",
in_sign='sss', out_sign='a{ss}',
method=self._eventInviteeGet,
async=True)
host.bridge.addMethod("eventInviteeSet", ".plugin",
in_sign='ssa{ss}s', out_sign='',
method=self._eventInviteeSet,
async=True)
host.bridge.addMethod("eventInviteesList", ".plugin",
in_sign='sss', out_sign='a{sa{ss}}',
method=self._eventInviteesList,
async=True),
host.bridge.addMethod("eventInvite", ".plugin", in_sign='sssss', out_sign='',
method=self._invite,
async=True)
host.bridge.addMethod("eventInviteByEmail", ".plugin", in_sign='ssssassssssss', out_sign='',
method=self._inviteByEmail,
async=True)
host.bridge.addMethod(
"eventGet",
".plugin",
in_sign="ssss",
out_sign="(ia{ss})",
method=self._eventGet,
async=True,
)
host.bridge.addMethod(
"eventCreate",
".plugin",
in_sign="ia{ss}ssss",
out_sign="s",
method=self._eventCreate,
async=True,
)
host.bridge.addMethod(
"eventModify",
".plugin",
in_sign="sssia{ss}s",
out_sign="",
method=self._eventModify,
async=True,
)
host.bridge.addMethod(
"eventsList",
".plugin",
in_sign="sss",
out_sign="aa{ss}",
method=self._eventsList,
async=True,
)
host.bridge.addMethod(
"eventInviteeGet",
".plugin",
in_sign="sss",
out_sign="a{ss}",
method=self._eventInviteeGet,
async=True,
)
host.bridge.addMethod(
"eventInviteeSet",
".plugin",
in_sign="ssa{ss}s",
out_sign="",
method=self._eventInviteeSet,
async=True,
)
host.bridge.addMethod(
"eventInviteesList",
".plugin",
in_sign="sss",
out_sign="a{sa{ss}}",
method=self._eventInviteesList,
async=True,
),
host.bridge.addMethod(
"eventInvite",
".plugin",
in_sign="sssss",
out_sign="",
method=self._invite,
async=True,
)
host.bridge.addMethod(
"eventInviteByEmail",
".plugin",
in_sign="ssssassssssss",
out_sign="",
method=self._inviteByEmail,
async=True,
)
def getHandler(self, client):
return EventsHandler(self)
@ -115,13 +155,13 @@ class Events(object):
data = {}
for key in (u'name',):
for key in (u"name",):
try:
data[key] = event_elt[key]
except KeyError:
continue
for elt_name in (u'description',):
for elt_name in (u"description",):
try:
elt = next(event_elt.elements(NS_EVENT, elt_name))
except StopIteration:
@ -129,21 +169,21 @@ class Events(object):
else:
data[elt_name] = unicode(elt)
for elt_name in (u'image', 'background-image'):
for elt_name in (u"image", "background-image"):
try:
image_elt = next(event_elt.elements(NS_EVENT, elt_name))
data[elt_name] = image_elt['src']
data[elt_name] = image_elt["src"]
except StopIteration:
continue
except KeyError:
log.warning(_(u'no src found for image'))
log.warning(_(u"no src found for image"))
for uri_type in (u'invitees', u'blog'):
for uri_type in (u"invitees", u"blog"):
try:
elt = next(event_elt.elements(NS_EVENT, uri_type))
uri = data[uri_type + u'_uri'] = elt['uri']
uri = data[uri_type + u"_uri"] = elt["uri"]
uri_data = xmpp_uri.parseXMPPUri(uri)
if uri_data[u'type'] != u'pubsub':
if uri_data[u"type"] != u"pubsub":
raise ValueError
except StopIteration:
log.warning(_(u"no {uri_type} element found!").format(uri_type=uri_type))
@ -152,22 +192,26 @@ class Events(object):
except ValueError:
log.warning(_(u"bad {uri_type} element").format(uri_type=uri_type))
else:
data[uri_type + u'_service'] = uri_data[u'path']
data[uri_type + u'_node'] = uri_data[u'node']
data[uri_type + u"_service"] = uri_data[u"path"]
data[uri_type + u"_node"] = uri_data[u"node"]
for meta_elt in event_elt.elements(NS_EVENT, 'meta'):
key = meta_elt[u'name']
for meta_elt in event_elt.elements(NS_EVENT, "meta"):
key = meta_elt[u"name"]
if key in data:
log.warning(u'Ignoring conflicting meta element: {xml}'.format(xml=meta_elt.toXml()))
log.warning(
u"Ignoring conflicting meta element: {xml}".format(
xml=meta_elt.toXml()
)
)
continue
data[key] = unicode(meta_elt)
if event_elt.link:
link_elt = event_elt.link
data['service'] = link_elt['service']
data['node'] = link_elt['node']
data['item'] = link_elt['item']
if event_elt.getAttribute('creator') == 'true':
data['creator'] = True
data["service"] = link_elt["service"]
data["node"] = link_elt["node"]
data["item"] = link_elt["item"]
if event_elt.getAttribute("creator") == "true":
data["creator"] = True
return timestamp, data
@defer.inlineCallbacks
@ -184,7 +228,7 @@ class Events(object):
id_ = NS_EVENT
items, metadata = yield self._p.getItems(client, service, node, item_ids=[id_])
try:
event_elt = next(items[0].elements(NS_EVENT, u'event'))
event_elt = next(items[0].elements(NS_EVENT, u"event"))
except IndexError:
raise exceptions.NotFound(_(u"No event with this id has been found"))
defer.returnValue(event_elt)
@ -204,30 +248,30 @@ class Events(object):
try:
# TODO: check auto-create, no need to create node first if available
options = {self._p.OPT_ACCESS_MODEL: self._p.ACCESS_WHITELIST}
yield self._p.createNode(client,
client.jid.userhostJID(),
nodeIdentifier=NS_EVENT_LIST,
options=options)
yield self._p.createNode(
client,
client.jid.userhostJID(),
nodeIdentifier=NS_EVENT_LIST,
options=options,
)
except error.StanzaError as e:
if e.condition == u'conflict':
if e.condition == u"conflict":
log.debug(_(u"requested node already exists"))
link_elt = event_elt.addElement((NS_EVENT_LIST, 'link'))
link_elt = event_elt.addElement((NS_EVENT_LIST, "link"))
link_elt["service"] = service.full()
link_elt["node"] = node
link_elt["item"] = event_id
item_id = xmpp_uri.buildXMPPUri(u'pubsub',
path=service.full(),
node=node,
item=event_id)
item_id = xmpp_uri.buildXMPPUri(
u"pubsub", path=service.full(), node=node, item=event_id
)
if creator:
event_elt['creator'] = 'true'
event_elt["creator"] = "true"
item_elt = pubsub.Item(id=item_id, payload=event_elt)
yield self._p.publish(client,
client.jid.userhostJID(),
NS_EVENT_LIST,
items=[item_elt])
yield self._p.publish(
client, client.jid.userhostJID(), NS_EVENT_LIST, items=[item_elt]
)
def _eventGet(self, service, node, id_=u'', profile_key=C.PROF_KEY_NONE):
def _eventGet(self, service, node, id_=u"", profile_key=C.PROF_KEY_NONE):
service = jid.JID(service) if service else None
node = node if node else NS_EVENT
client = self.host.getClient(profile_key)
@ -251,11 +295,13 @@ class Events(object):
defer.returnValue(self._parseEventElt(event_elt))
def _eventCreate(self, timestamp, data, service, node, id_=u'', profile_key=C.PROF_KEY_NONE):
def _eventCreate(
self, timestamp, data, service, node, id_=u"", profile_key=C.PROF_KEY_NONE
):
service = jid.JID(service) if service else None
node = node or None
client = self.host.getClient(profile_key)
data[u'register'] = C.bool(data.get(u'register', C.BOOL_FALSE))
data[u"register"] = C.bool(data.get(u"register", C.BOOL_FALSE))
return self.eventCreate(client, timestamp, data, service, node, id_ or NS_EVENT)
@defer.inlineCallbacks
@ -282,57 +328,66 @@ class Events(object):
if not service:
service = client.jid.userhostJID()
if not node:
node = NS_EVENT + u'__' + shortuuid.uuid()
event_elt = domish.Element((NS_EVENT, 'event'))
node = NS_EVENT + u"__" + shortuuid.uuid()
event_elt = domish.Element((NS_EVENT, "event"))
if timestamp is not None and timestamp != -1:
formatted_date = utils.xmpp_date(timestamp)
event_elt.addElement((NS_EVENT, 'date'), content=formatted_date)
register = data.pop('register', False)
for key in (u'name',):
event_elt.addElement((NS_EVENT, "date"), content=formatted_date)
register = data.pop("register", False)
for key in (u"name",):
if key in data:
event_elt[key] = data.pop(key)
for key in (u'description',):
for key in (u"description",):
if key in data:
event_elt.addElement((NS_EVENT, key), content=data.pop(key))
for key in (u'image', u'background-image'):
for key in (u"image", u"background-image"):
if key in data:
elt = event_elt.addElement((NS_EVENT, key))
elt['src'] = data.pop(key)
elt["src"] = data.pop(key)
# we first create the invitees and blog nodes (if not specified in data)
for uri_type in (u'invitees', u'blog'):
key = uri_type + u'_uri'
for to_delete in (u'service', u'node'):
k = uri_type + u'_' + to_delete
for uri_type in (u"invitees", u"blog"):
key = uri_type + u"_uri"
for to_delete in (u"service", u"node"):
k = uri_type + u"_" + to_delete
if k in data:
del data[k]
if key not in data:
# FIXME: affiliate invitees
uri_node = yield self._p.createNode(client, service)
yield self._p.setConfiguration(client, service, uri_node, {self._p.OPT_ACCESS_MODEL: self._p.ACCESS_WHITELIST})
yield self._p.setConfiguration(
client,
service,
uri_node,
{self._p.OPT_ACCESS_MODEL: self._p.ACCESS_WHITELIST},
)
uri_service = service
else:
uri = data.pop(key)
uri_data = xmpp_uri.parseXMPPUri(uri)
if uri_data[u'type'] != u'pubsub':
raise ValueError(_(u'The given URI is not valid: {uri}').format(uri=uri))
uri_service = jid.JID(uri_data[u'path'])
uri_node = uri_data[u'node']
if uri_data[u"type"] != u"pubsub":
raise ValueError(
_(u"The given URI is not valid: {uri}").format(uri=uri)
)
uri_service = jid.JID(uri_data[u"path"])
uri_node = uri_data[u"node"]
elt = event_elt.addElement((NS_EVENT, uri_type))
elt['uri'] = xmpp_uri.buildXMPPUri('pubsub', path=uri_service.full(), node=uri_node)
elt["uri"] = xmpp_uri.buildXMPPUri(
"pubsub", path=uri_service.full(), node=uri_node
)
# remaining data are put in <meta> elements
for key in data.keys():
elt = event_elt.addElement((NS_EVENT, 'meta'), content = data.pop(key))
elt['name'] = key
elt = event_elt.addElement((NS_EVENT, "meta"), content=data.pop(key))
elt["name"] = key
item_elt = pubsub.Item(id=event_id, payload=event_elt)
try:
# TODO: check auto-create, no need to create node first if available
node = yield self._p.createNode(client, service, nodeIdentifier=node)
except error.StanzaError as e:
if e.condition == u'conflict':
if e.condition == u"conflict":
log.debug(_(u"requested node already exists"))
yield self._p.publish(client, service, node, items=[item_elt])
@ -341,14 +396,26 @@ class Events(object):
yield self.register(client, service, node, event_id, event_elt, creator=True)
defer.returnValue(node)
def _eventModify(self, service, node, id_, timestamp_update, data_update, profile_key=C.PROF_KEY_NONE):
def _eventModify(
self,
service,
node,
id_,
timestamp_update,
data_update,
profile_key=C.PROF_KEY_NONE,
):
service = jid.JID(service) if service else None
node = node if node else NS_EVENT
client = self.host.getClient(profile_key)
return self.eventModify(client, service, node, id_ or NS_EVENT, timestamp_update or None, data_update)
return self.eventModify(
client, service, node, id_ or NS_EVENT, timestamp_update or None, data_update
)
@defer.inlineCallbacks
def eventModify(self, client, service, node, id_=NS_EVENT, timestamp_update=None, data_update=None):
def eventModify(
self, client, service, node, id_=NS_EVENT, timestamp_update=None, data_update=None
):
"""Update an event
Similar as create instead that it update existing item instead of
@ -364,8 +431,8 @@ class Events(object):
def _eventsListSerialise(self, events):
for timestamp, data in events:
data['date'] = unicode(timestamp)
data['creator'] = C.boolConst(data.get('creator', False))
data["date"] = unicode(timestamp)
data["creator"] = C.boolConst(data.get("creator", False))
return [e[1] for e in events]
def _eventsList(self, service, node, profile):
@ -388,10 +455,11 @@ class Events(object):
events = []
for item in items[0]:
try:
event_elt = next(item.elements(NS_EVENT, u'event'))
event_elt = next(item.elements(NS_EVENT, u"event"))
except IndexError:
log.error(_(u"No event found in item {item_id}").format(
item_id = item['id']))
log.error(
_(u"No event found in item {item_id}").format(item_id=item["id"])
)
timestamp, data = self._parseEventElt(event_elt)
events.append((timestamp, data))
defer.returnValue(events)
@ -411,21 +479,23 @@ class Events(object):
@return (dict): a dict with current attendance status,
an empty dict is returned if nothing has been answered yed
"""
items, metadata = yield self._p.getItems(client, service, node, item_ids=[client.jid.userhost()])
items, metadata = yield self._p.getItems(
client, service, node, item_ids=[client.jid.userhost()]
)
try:
event_elt = next(items[0].elements(NS_EVENT, u'invitee'))
event_elt = next(items[0].elements(NS_EVENT, u"invitee"))
except IndexError:
# no item found, event data are not set yet
defer.returnValue({})
data = {}
for key in (u'attend', u'guests'):
for key in (u"attend", u"guests"):
try:
data[key] = event_elt[key]
except KeyError:
continue
defer.returnValue(data)
def _eventInviteeSet(self, service, node, event_data, profile_key):
def _eventInviteeSet(self, service, node, event_data, profile_key):
service = jid.JID(service) if service else None
node = node if node else NS_EVENT
client = self.host.getClient(profile_key)
@ -441,8 +511,8 @@ class Events(object):
attend: one of "yes", "no", "maybe"
guests: an int
"""
event_elt = domish.Element((NS_EVENT, 'invitee'))
for key in (u'attend', u'guests'):
event_elt = domish.Element((NS_EVENT, "invitee"))
for key in (u"attend", u"guests"):
try:
event_elt[key] = data.pop(key)
except KeyError:
@ -469,22 +539,24 @@ class Events(object):
invitees = {}
for item in items:
try:
event_elt = next(item.elements(NS_EVENT, u'invitee'))
event_elt = next(item.elements(NS_EVENT, u"invitee"))
except StopIteration:
# no item found, event data are not set yet
log.warning(_(u"no data found for {item_id} (service: {service}, node: {node})".format(
item_id=item['id'],
service=service,
node=node
)))
log.warning(
_(
u"no data found for {item_id} (service: {service}, node: {node})".format(
item_id=item["id"], service=service, node=node
)
)
)
else:
data = {}
for key in (u'attend', u'guests'):
for key in (u"attend", u"guests"):
try:
data[key] = event_elt[key]
except KeyError:
continue
invitees[item['id']] = data
invitees[item["id"]] = data
defer.returnValue(invitees)
def sendMessageInvitation(self, client, invitee_jid, service, node, item_id):
@ -496,20 +568,20 @@ class Events(object):
@param item_id(unicode): id of the event
"""
mess_data = {
'from': client.jid,
'to': invitee_jid,
'uid': '',
'message': {},
'type': C.MESS_TYPE_CHAT,
'subject': {},
'extra': {},
}
"from": client.jid,
"to": invitee_jid,
"uid": "",
"message": {},
"type": C.MESS_TYPE_CHAT,
"subject": {},
"extra": {},
}
client.generateMessageXML(mess_data)
event_elt = mess_data['xml'].addElement('invitation', NS_EVENT_INVIT)
event_elt['service'] = service.full()
event_elt['node'] = node
event_elt['item'] = item_id
client.send(mess_data['xml'])
event_elt = mess_data["xml"].addElement("invitation", NS_EVENT_INVIT)
event_elt["service"] = service.full()
event_elt["node"] = node
event_elt["item"] = item_id
client.send(mess_data["xml"])
def _invite(self, invitee_jid, service, node, item_id, profile):
client = self.host.getClient(profile)
@ -531,51 +603,83 @@ class Events(object):
@param item_id(unicode): event id
"""
if self._b is None:
raise exceptions.FeatureNotFound(_(u'"XEP-0277" (blog) plugin is needed for this feature'))
raise exceptions.FeatureNotFound(
_(u'"XEP-0277" (blog) plugin is needed for this feature')
)
if item_id is None:
item_id = NS_EVENT
# first we authorize our invitee to see the nodes of interest
yield self._p.setNodeAffiliations(client, service, node, {invitee_jid: u'member'})
log.debug(_(u'affiliation set on event node'))
yield self._p.setNodeAffiliations(client, service, node, {invitee_jid: u"member"})
log.debug(_(u"affiliation set on event node"))
dummy, event_data = yield self.eventGet(client, service, node, item_id)
log.debug(_(u'got event data'))
invitees_service = jid.JID(event_data['invitees_service'])
invitees_node = event_data['invitees_node']
blog_service = jid.JID(event_data['blog_service'])
blog_node = event_data['blog_node']
yield self._p.setNodeAffiliations(client, invitees_service, invitees_node, {invitee_jid: u'publisher'})
log.debug(_(u'affiliation set on invitee node'))
yield self._p.setNodeAffiliations(client, blog_service, blog_node, {invitee_jid: u'member'})
log.debug(_(u"got event data"))
invitees_service = jid.JID(event_data["invitees_service"])
invitees_node = event_data["invitees_node"]
blog_service = jid.JID(event_data["blog_service"])
blog_node = event_data["blog_node"]
yield self._p.setNodeAffiliations(
client, invitees_service, invitees_node, {invitee_jid: u"publisher"}
)
log.debug(_(u"affiliation set on invitee node"))
yield self._p.setNodeAffiliations(
client, blog_service, blog_node, {invitee_jid: u"member"}
)
blog_items, dummy = yield self._b.mbGet(client, blog_service, blog_node, None)
for item in blog_items:
try:
comments_service = jid.JID(item['comments_service'])
comments_node = item['comments_node']
comments_service = jid.JID(item["comments_service"])
comments_node = item["comments_node"]
except KeyError:
log.debug(u"no comment service set for item {item_id}".format(item_id=item['id']))
log.debug(
u"no comment service set for item {item_id}".format(
item_id=item["id"]
)
)
else:
yield self._p.setNodeAffiliations(client, comments_service, comments_node, {invitee_jid: u'publisher'})
log.debug(_(u'affiliation set on blog and comments nodes'))
yield self._p.setNodeAffiliations(
client, comments_service, comments_node, {invitee_jid: u"publisher"}
)
log.debug(_(u"affiliation set on blog and comments nodes"))
# now we send the invitation
self.sendMessageInvitation(client, invitee_jid, service, node, item_id)
def _inviteByEmail(self, service, node, id_=NS_EVENT, email=u'', emails_extra=None, name=u'', host_name=u'', language=u'', url_template=u'',
message_subject=u'', message_body=u'', profile_key=C.PROF_KEY_NONE):
def _inviteByEmail(
self,
service,
node,
id_=NS_EVENT,
email=u"",
emails_extra=None,
name=u"",
host_name=u"",
language=u"",
url_template=u"",
message_subject=u"",
message_body=u"",
profile_key=C.PROF_KEY_NONE,
):
client = self.host.getClient(profile_key)
kwargs = {u'profile': client.profile,
u'emails_extra': [unicode(e) for e in emails_extra]
}
for key in ("email", "name", "host_name", "language", "url_template", "message_subject", "message_body"):
kwargs = {
u"profile": client.profile,
u"emails_extra": [unicode(e) for e in emails_extra],
}
for key in (
"email",
"name",
"host_name",
"language",
"url_template",
"message_subject",
"message_body",
):
value = locals()[key]
kwargs[key] = unicode(value)
return self.inviteByEmail(client,
jid.JID(service) if service else None,
node,
id_ or NS_EVENT,
**kwargs)
return self.inviteByEmail(
client, jid.JID(service) if service else None, node, id_ or NS_EVENT, **kwargs
)
@defer.inlineCallbacks
def inviteByEmail(self, client, service, node, id_=NS_EVENT, **kwargs):
@ -586,18 +690,21 @@ class Events(object):
@param id_(unicode): id_ with even data
"""
if self._i is None:
raise exceptions.FeatureNotFound(_(u'"Invitations" plugin is needed for this feature'))
raise exceptions.FeatureNotFound(
_(u'"Invitations" plugin is needed for this feature')
)
if self._b is None:
raise exceptions.FeatureNotFound(_(u'"XEP-0277" (blog) plugin is needed for this feature'))
raise exceptions.FeatureNotFound(
_(u'"XEP-0277" (blog) plugin is needed for this feature')
)
service = service or client.jid.userhostJID()
event_uri = xmpp_uri.buildXMPPUri('pubsub',
path=service.full(),
node=node,
item=id_)
kwargs['extra'] = {u'event_uri': event_uri}
event_uri = xmpp_uri.buildXMPPUri(
"pubsub", path=service.full(), node=node, item=id_
)
kwargs["extra"] = {u"event_uri": event_uri}
invitation_data = yield self._i.create(**kwargs)
invitee_jid = invitation_data[u'jid']
log.debug(_(u'invitation created'))
invitee_jid = invitation_data[u"jid"]
log.debug(_(u"invitation created"))
# now that we have a jid, we can send normal invitation
yield self.invite(client, invitee_jid, service, node, id_)
@ -605,9 +712,9 @@ class Events(object):
def onInvitation(self, message_elt, client):
invitation_elt = message_elt.invitation
try:
service = jid.JID(invitation_elt['service'])
node = invitation_elt['node']
event_id = invitation_elt['item']
service = jid.JID(invitation_elt["service"])
node = invitation_elt["node"]
event_id = invitation_elt["item"]
except (RuntimeError, KeyError):
log.warning(_(u"Bad invitation: {xml}").format(xml=message_elt.toXml()))
@ -626,14 +733,16 @@ class EventsHandler(XMPPHandler):
return self.plugin_parent.host
def connectionInitialized(self):
self.xmlstream.addObserver(INVITATION,
self.plugin_parent.onInvitation,
client=self.parent)
self.xmlstream.addObserver(
INVITATION, self.plugin_parent.onInvitation, client=self.parent
)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
return [disco.DiscoFeature(NS_EVENT),
disco.DiscoFeature(NS_EVENT_LIST),
disco.DiscoFeature(NS_EVENT_INVIT)]
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
return [
disco.DiscoFeature(NS_EVENT),
disco.DiscoFeature(NS_EVENT_LIST),
disco.DiscoFeature(NS_EVENT_INVIT),
]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
def getDiscoItems(self, requestor, target, nodeIdentifier=""):
return []

View file

@ -21,6 +21,7 @@ from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core import exceptions
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.tools import xml_tools
from sat.tools import stream
@ -35,8 +36,8 @@ from twisted.internet import interfaces
from zope import interface
import errno
NS_STREAM = 'http://salut-a-toi.org/protocol/stream'
SECURITY_LIMIT=30
NS_STREAM = "http://salut-a-toi.org/protocol/stream"
SECURITY_LIMIT = 30
START_PORT = 8888
PLUGIN_INFO = {
@ -47,7 +48,7 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0166"],
C.PI_MAIN: "JingleStream",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Jingle Stream plugin""")
C.PI_DESCRIPTION: _("""Jingle Stream plugin"""),
}
CONFIRM = D_(u"{peer} wants to send you a stream, do you accept ?")
@ -55,7 +56,6 @@ CONFIRM_TITLE = D_(u"Stream Request")
class StreamProtocol(protocol.Protocol):
def __init__(self):
self.pause = False
@ -118,7 +118,9 @@ class StreamFactory(protocol.Factory):
def startStream(self, consumer):
if self.consumer is not None:
raise exceptions.InternalError(_(u"stream can't be used with multiple consumers"))
raise exceptions.InternalError(
_(u"stream can't be used with multiple consumers")
)
assert self.deferred is None
self.consumer = consumer
consumer.registerProducer(self, True)
@ -176,9 +178,17 @@ class JingleStream(object):
def __init__(self, host):
log.info(_("Plugin Stream initialization"))
self.host = host
self._j = host.plugins["XEP-0166"] # shortcut to access jingle
self._j = host.plugins["XEP-0166"] # shortcut to access jingle
self._j.registerApplication(NS_STREAM, self)
host.bridge.addMethod("streamOut", ".plugin", in_sign='ss', out_sign='s', method=self._streamOut, async=True)
host.bridge.addMethod(
"streamOut",
".plugin",
in_sign="ss",
out_sign="s",
method=self._streamOut,
async=True,
)
# jingle callbacks
def _streamOut(self, to_jid_s, profile_key):
@ -206,64 +216,75 @@ class JingleStream(object):
else:
factory.port_listening = port_listening
break
self._j.initiate(client,
to_jid,
[{'app_ns': NS_STREAM,
'senders': self._j.ROLE_INITIATOR,
'app_kwargs': {'stream_object': factory},
}])
self._j.initiate(
client,
to_jid,
[
{
"app_ns": NS_STREAM,
"senders": self._j.ROLE_INITIATOR,
"app_kwargs": {"stream_object": factory},
}
],
)
defer.returnValue(unicode(port))
def jingleSessionInit(self, client, session, content_name, stream_object):
content_data = session['contents'][content_name]
application_data = content_data['application_data']
assert 'stream_object' not in application_data
application_data['stream_object'] = stream_object
desc_elt = domish.Element((NS_STREAM, 'description'))
content_data = session["contents"][content_name]
application_data = content_data["application_data"]
assert "stream_object" not in application_data
application_data["stream_object"] = stream_object
desc_elt = domish.Element((NS_STREAM, "description"))
return desc_elt
@defer.inlineCallbacks
def jingleRequestConfirmation(self, client, action, session, content_name, desc_elt):
"""This method request confirmation for a jingle session"""
content_data = session['contents'][content_name]
if content_data['senders'] not in (self._j.ROLE_INITIATOR, self._j.ROLE_RESPONDER):
content_data = session["contents"][content_name]
if content_data["senders"] not in (
self._j.ROLE_INITIATOR,
self._j.ROLE_RESPONDER,
):
log.warning(u"Bad sender, assuming initiator")
content_data['senders'] = self._j.ROLE_INITIATOR
content_data["senders"] = self._j.ROLE_INITIATOR
confirm_data = yield xml_tools.deferDialog(self.host,
_(CONFIRM).format(peer=session['peer_jid'].full()),
confirm_data = yield xml_tools.deferDialog(
self.host,
_(CONFIRM).format(peer=session["peer_jid"].full()),
_(CONFIRM_TITLE),
type_=C.XMLUI_DIALOG_CONFIRM,
action_extra={'meta_from_jid': session['peer_jid'].full(),
'meta_type': "STREAM",
},
action_extra={
"meta_from_jid": session["peer_jid"].full(),
"meta_type": "STREAM",
},
security_limit=SECURITY_LIMIT,
profile=client.profile)
profile=client.profile,
)
if not C.bool(confirm_data['answer']):
if not C.bool(confirm_data["answer"]):
defer.returnValue(False)
try:
port = int(confirm_data['port'])
port = int(confirm_data["port"])
except (ValueError, KeyError):
raise exceptions.DataError(_(u'given port is invalid'))
endpoint = endpoints.TCP4ClientEndpoint(reactor, 'localhost', port)
raise exceptions.DataError(_(u"given port is invalid"))
endpoint = endpoints.TCP4ClientEndpoint(reactor, "localhost", port)
factory = StreamFactory()
yield endpoint.connect(factory)
content_data['stream_object'] = factory
finished_d = content_data['finished_d'] = defer.Deferred()
content_data["stream_object"] = factory
finished_d = content_data["finished_d"] = defer.Deferred()
args = [client, session, content_name, content_data]
finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
defer.returnValue(True)
def jingleHandler(self, client, action, session, content_name, desc_elt):
content_data = session['contents'][content_name]
application_data = content_data['application_data']
content_data = session["contents"][content_name]
application_data = content_data["application_data"]
if action in (self._j.A_ACCEPTED_ACK, self._j.A_SESSION_INITIATE):
pass
elif action == self._j.A_SESSION_ACCEPT:
assert not 'stream_object' in content_data
content_data['stream_object'] = application_data['stream_object']
finished_d = content_data['finished_d'] = defer.Deferred()
assert not "stream_object" in content_data
content_data["stream_object"] = application_data["stream_object"]
finished_d = content_data["finished_d"] = defer.Deferred()
args = [client, session, content_name, content_data]
finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
else:
@ -273,9 +294,11 @@ class JingleStream(object):
def _finishedCb(self, dummy, client, session, content_name, content_data):
log.info(u"Pipe transfer completed")
self._j.contentTerminate(client, session, content_name)
content_data['stream_object'].stopStream()
content_data["stream_object"].stopStream()
def _finishedEb(self, failure, client, session, content_name, content_data):
log.warning(u"Error while streaming pipe: {}".format(failure))
self._j.contentTerminate(client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT)
content_data['stream_object'].stopStream()
self._j.contentTerminate(
client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT
)
content_data["stream_object"].stopStream()

View file

@ -20,13 +20,16 @@
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
try:
from langid.langid import LanguageIdentifier, model
except ImportError:
raise exceptions.MissingModule(u'Missing module langid, please download/install it with "pip install langid")')
raise exceptions.MissingModule(
u'Missing module langid, please download/install it with "pip install langid")'
)
identifier = LanguageIdentifier.from_modelstring(model, norm_probs=False)
@ -39,7 +42,7 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "LangDetect",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Detect and set message language when unknown""")
C.PI_DESCRIPTION: _("""Detect and set message language when unknown"""),
}
CATEGORY = D_(u"Misc")
@ -53,14 +56,12 @@ PARAMS = """
</category>
</individual>
</params>
""".format(category_name=CATEGORY,
name=NAME,
label=_(LABEL),
)
""".format(
category_name=CATEGORY, name=NAME, label=_(LABEL)
)
class LangDetect(object):
def __init__(self, host):
log.info(_(u"Language detection plugin initialization"))
self.host = host
@ -69,8 +70,8 @@ class LangDetect(object):
host.trigger.add("sendMessage", self.MessageSendTrigger)
def addLanguage(self, mess_data):
message = mess_data['message']
if len(message) == 1 and message.keys()[0] == '':
message = mess_data["message"]
if len(message) == 1 and message.keys()[0] == "":
msg = message.values()[0]
lang = identifier.classify(msg)[0]
mess_data["message"] = {lang: msg}
@ -79,13 +80,17 @@ class LangDetect(object):
def MessageReceivedTrigger(self, client, message_elt, post_treat):
""" Check if source is linked and repeat message, else do nothing """
lang_detect = self.host.memory.getParamA(NAME, CATEGORY, profile_key=client.profile)
lang_detect = self.host.memory.getParamA(
NAME, CATEGORY, profile_key=client.profile
)
if lang_detect:
post_treat.addCallback(self.addLanguage)
return True
def MessageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
lang_detect = self.host.memory.getParamA(NAME, CATEGORY, profile_key=client.profile)
lang_detect = self.host.memory.getParamA(
NAME, CATEGORY, profile_key=client.profile
)
if lang_detect:
self.addLanguage(data)
return True

View file

@ -20,11 +20,13 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.protocols.jabber import jid
from sat.core.exceptions import UnknownEntityError
#from sat.tools import trigger
# from sat.tools import trigger
PLUGIN_INFO = {
C.PI_NAME: "Parrot Plugin",
@ -35,12 +37,15 @@ PLUGIN_INFO = {
C.PI_RECOMMENDATIONS: [C.TEXT_CMDS],
C.PI_MAIN: "Exp_Parrot",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Implementation of parrot mode (repeat messages between 2 entities)""")
C.PI_DESCRIPTION: _(
u"""Implementation of parrot mode (repeat messages between 2 entities)"""
),
}
class Exp_Parrot(object):
"""Parrot mode plugin: repeat messages from one entity or MUC room to another one"""
# XXX: This plugin can be potentially dangerous if we don't trust entities linked
# this is specially true if we have other triggers.
# sendMessageTrigger avoid other triggers execution, it's deactivated to allow
@ -51,13 +56,13 @@ class Exp_Parrot(object):
log.info(_("Plugin Parrot initialization"))
self.host = host
host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
#host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
# host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
try:
self.host.plugins[C.TEXT_CMDS].registerTextCommands(self)
except KeyError:
log.info(_(u"Text commands not available"))
#def sendMessageTrigger(self, client, mess_data, treatments):
# def sendMessageTrigger(self, client, mess_data, treatments):
# """ Deactivate other triggers if recipient is in parrot links """
# try:
# _links = client.parrot_links
@ -84,18 +89,22 @@ class Exp_Parrot(object):
return True
message = {}
for e in message_elt.elements(C.NS_CLIENT, 'body'):
for e in message_elt.elements(C.NS_CLIENT, "body"):
body = unicode(e)
lang = e.getAttribute('lang') or ''
lang = e.getAttribute("lang") or ""
try:
entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"]
entity_type = self.host.memory.getEntityData(from_jid, ["type"], profile)[
"type"
]
except (UnknownEntityError, KeyError):
entity_type = "contact"
if entity_type == 'chatroom':
if entity_type == "chatroom":
src_txt = from_jid.resource
if src_txt == self.host.plugins["XEP-0045"].getRoomNick(client, from_jid.userhostJID()):
#we won't repeat our own messages
if src_txt == self.host.plugins["XEP-0045"].getRoomNick(
client, from_jid.userhostJID()
):
# we won't repeat our own messages
return True
else:
src_txt = from_jid.user
@ -103,7 +112,9 @@ class Exp_Parrot(object):
linked = _links[from_jid.userhostJID()]
client.sendMessage(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True)
client.sendMessage(
jid.JID(unicode(linked)), message, None, "auto", no_trigger=True
)
return True
@ -119,7 +130,10 @@ class Exp_Parrot(object):
_links = client.parrot_links = {}
_links[source_jid.userhostJID()] = dest_jid
log.info(u"Parrot mode: %s will be repeated to %s" % (source_jid.userhost(), unicode(dest_jid)))
log.info(
u"Parrot mode: %s will be repeated to %s"
% (source_jid.userhost(), unicode(dest_jid))
)
def removeParrot(self, client, source_jid):
"""Remove parrot link
@ -141,15 +155,21 @@ class Exp_Parrot(object):
if not link_left_jid.user or not link_left_jid.host:
raise jid.InvalidFormat
except (RuntimeError, jid.InvalidFormat, AttributeError):
txt_cmd.feedBack(client, "Can't activate Parrot mode for invalid jid", mess_data)
txt_cmd.feedBack(
client, "Can't activate Parrot mode for invalid jid", mess_data
)
return False
link_right_jid = mess_data['to']
link_right_jid = mess_data["to"]
self.addParrot(client, link_left_jid, link_right_jid)
self.addParrot(client, link_right_jid, link_left_jid)
txt_cmd.feedBack(client, "Parrot mode activated for {}".format(unicode(link_left_jid)), mess_data)
txt_cmd.feedBack(
client,
"Parrot mode activated for {}".format(unicode(link_left_jid)),
mess_data,
)
return False
@ -163,14 +183,22 @@ class Exp_Parrot(object):
if not link_left_jid.user or not link_left_jid.host:
raise jid.InvalidFormat
except jid.InvalidFormat:
txt_cmd.feedBack(client, u"Can't deactivate Parrot mode for invalid jid", mess_data)
txt_cmd.feedBack(
client, u"Can't deactivate Parrot mode for invalid jid", mess_data
)
return False
link_right_jid = mess_data['to']
link_right_jid = mess_data["to"]
self.removeParrot(client, link_left_jid)
self.removeParrot(client, link_right_jid)
txt_cmd.feedBack(client, u"Parrot mode deactivated for {} and {}".format(unicode(link_left_jid), unicode(link_right_jid)), mess_data)
txt_cmd.feedBack(
client,
u"Parrot mode deactivated for {} and {}".format(
unicode(link_left_jid), unicode(link_right_jid)
),
mess_data,
)
return False

View file

@ -24,9 +24,10 @@ from sat.core.log import getLogger
from sat.memory import persistent
from twisted.words.protocols.jabber import jid
from twisted.internet import defer
log = getLogger(__name__)
NS_PUBSUB_HOOK = 'PUBSUB_HOOK'
NS_PUBSUB_HOOK = "PUBSUB_HOOK"
PLUGIN_INFO = {
C.PI_NAME: "PubSub Hook",
@ -36,40 +37,48 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0060"],
C.PI_MAIN: "PubsubHook",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Experimental plugin to launch on action on Pubsub notifications""")
C.PI_DESCRIPTION: _(
"""Experimental plugin to launch on action on Pubsub notifications"""
),
}
# python module
HOOK_TYPE_PYTHON = u'python'
#  python module
HOOK_TYPE_PYTHON = u"python"
# python file path
HOOK_TYPE_PYTHON_FILE = u'python_file'
HOOK_TYPE_PYTHON_FILE = u"python_file"
# python code directly
HOOK_TYPE_PYTHON_CODE = u'python_code'
HOOK_TYPE_PYTHON_CODE = u"python_code"
HOOK_TYPES = (HOOK_TYPE_PYTHON, HOOK_TYPE_PYTHON_FILE, HOOK_TYPE_PYTHON_CODE)
class PubsubHook(object):
def __init__(self, host):
log.info(_(u"PubSub Hook initialization"))
self.host = host
self.node_hooks = {} # keep track of the number of hooks per node (for all profiles)
host.bridge.addMethod("psHookAdd", ".plugin",
in_sign='ssssbs', out_sign='',
method=self._addHook
)
host.bridge.addMethod("psHookRemove", ".plugin",
in_sign='sssss', out_sign='i',
method=self._removeHook
)
host.bridge.addMethod("psHookList", ".plugin",
in_sign='s', out_sign='aa{ss}',
method=self._listHooks
)
host.bridge.addMethod(
"psHookAdd", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook
)
host.bridge.addMethod(
"psHookRemove",
".plugin",
in_sign="sssss",
out_sign="i",
method=self._removeHook,
)
host.bridge.addMethod(
"psHookList",
".plugin",
in_sign="s",
out_sign="aa{ss}",
method=self._listHooks,
)
@defer.inlineCallbacks
def profileConnected(self, client):
hooks = client._hooks = persistent.PersistentBinaryDict(NS_PUBSUB_HOOK, client.profile)
hooks = client._hooks = persistent.PersistentBinaryDict(
NS_PUBSUB_HOOK, client.profile
)
client._hooks_temporary = {}
yield hooks.load()
for node in hooks:
@ -85,10 +94,11 @@ class PubsubHook(object):
self.node_hooks[node] += 1
else:
# first hook on this node
self.host.plugins['XEP-0060'].addManagedNode(node, items_cb=self._itemsReceived)
self.host.plugins["XEP-0060"].addManagedNode(
node, items_cb=self._itemsReceived
)
self.node_hooks[node] = 0
log.info(_(u"node manager installed on {node}").format(
node = node))
log.info(_(u"node manager installed on {node}").format(node=node))
def _removeNodeManager(self, client, node):
try:
@ -98,34 +108,40 @@ class PubsubHook(object):
else:
if self.node_hooks[node] == 0:
del self.node_hooks[node]
self.host.plugins['XEP-0060'].removeManagedNode(node, self._itemsReceived)
self.host.plugins["XEP-0060"].removeManagedNode(node, self._itemsReceived)
log.debug(_(u"hook removed"))
else:
log.debug(_(u"node still needed for an other hook"))
def installHook(self, client, service, node, hook_type, hook_arg, persistent):
if hook_type not in HOOK_TYPES:
raise exceptions.DataError(_(u'{hook_type} is not handled').format(hook_type=hook_type))
raise exceptions.DataError(
_(u"{hook_type} is not handled").format(hook_type=hook_type)
)
if hook_type != HOOK_TYPE_PYTHON_FILE:
raise NotImplementedError(_(u'{hook_type} hook type not implemented yet').format(hook_type=hook_type))
raise NotImplementedError(
_(u"{hook_type} hook type not implemented yet").format(
hook_type=hook_type
)
)
self._installNodeManager(client, node)
hook_data = {'service': service,
'type': hook_type,
'arg': hook_arg
}
hook_data = {"service": service, "type": hook_type, "arg": hook_arg}
if persistent:
hooks_list = client._hooks.setdefault(node,[])
hooks_list = client._hooks.setdefault(node, [])
hooks_list.append(hook_data)
client._hooks.force(node)
else:
hooks_list = client._hooks_temporary.setdefault(node,[])
hooks_list = client._hooks_temporary.setdefault(node, [])
hooks_list.append(hook_data)
log.info(_(u"{persistent} hook installed on {node} for {profile}").format(
persistent = _(u'persistent') if persistent else _(u'temporary'),
node = node,
profile = client.profile))
log.info(
_(u"{persistent} hook installed on {node} for {profile}").format(
persistent=_(u"persistent") if persistent else _(u"temporary"),
node=node,
profile=client.profile,
)
)
def _itemsReceived(self, client, itemsEvent):
node = itemsEvent.nodeIdentifier
@ -134,24 +150,30 @@ class PubsubHook(object):
continue
hooks_list = hooks[node]
for hook_data in hooks_list[:]:
if hook_data['service'] != itemsEvent.sender.userhostJID():
if hook_data["service"] != itemsEvent.sender.userhostJID():
continue
try:
callback = hook_data['callback']
callback = hook_data["callback"]
except KeyError:
# first time we get this hook, we create the callback
hook_type = hook_data['type']
hook_type = hook_data["type"]
try:
if hook_type == HOOK_TYPE_PYTHON_FILE:
hook_globals = {}
execfile(hook_data['arg'], hook_globals)
callback = hook_globals['hook']
execfile(hook_data["arg"], hook_globals)
callback = hook_globals["hook"]
else:
raise NotImplementedError(_(u'{hook_type} hook type not implemented yet').format(
hook_type=hook_type))
raise NotImplementedError(
_(u"{hook_type} hook type not implemented yet").format(
hook_type=hook_type
)
)
except Exception as e:
log.warning(_(u"Can't load Pubsub hook at node {node}, it will be removed: {reason}").format(
node=node, reason=e))
log.warning(
_(
u"Can't load Pubsub hook at node {node}, it will be removed: {reason}"
).format(node=node, reason=e)
)
hooks_list.remove(hook_data)
continue
@ -159,14 +181,23 @@ class PubsubHook(object):
try:
callback(self.host, client, item)
except Exception as e:
log.warning(_(u"Error while running Pubsub hook for node {node}: {msg}").format(
node = node,
msg = e))
log.warning(
_(
u"Error while running Pubsub hook for node {node}: {msg}"
).format(node=node, msg=e)
)
def _addHook(self, service, node, hook_type, hook_arg, persistent, profile):
client = self.host.getClient(profile)
service = jid.JID(service) if service else client.jid.userhostJID()
return self.addHook(client, service, unicode(node), unicode(hook_type), unicode(hook_arg), persistent)
return self.addHook(
client,
service,
unicode(node),
unicode(hook_type),
unicode(hook_arg),
persistent,
)
def addHook(self, client, service, node, hook_type, hook_arg, persistent):
r"""Add a hook which will be triggered on a pubsub notification
@ -210,14 +241,18 @@ class PubsubHook(object):
for hooks in (client._hooks, client._hooks_temporary):
if node in hooks:
for hook_data in hooks[node]:
if (service != hook_data[u'service']
or hook_type is not None and hook_type != hook_data[u'type']
or hook_arg is not None and hook_arg != hook_data[u'arg']):
if (
service != hook_data[u"service"]
or hook_type is not None
and hook_type != hook_data[u"type"]
or hook_arg is not None
and hook_arg != hook_data[u"arg"]
):
continue
hooks[node].remove(hook_data)
removed += 1
if not hooks[node]:
# no more hooks, we can remove the node
#  no more hooks, we can remove the node
del hooks[node]
self._removeNodeManager(client, node)
else:
@ -228,8 +263,8 @@ class PubsubHook(object):
def _listHooks(self, profile):
hooks_list = self.listHooks(self.host.getClient(profile))
for hook in hooks_list:
hook[u'service'] = hook[u'service'].full()
hook[u'persistent'] = C.boolConst(hook[u'persistent'])
hook[u"service"] = hook[u"service"].full()
hook[u"persistent"] = C.boolConst(hook[u"persistent"])
return hooks_list
def listHooks(self, client):
@ -239,11 +274,13 @@ class PubsubHook(object):
persistent = hooks is client._hooks
for node, hooks_data in hooks.iteritems():
for hook_data in hooks_data:
hooks_list.append({u'service': hook_data[u'service'],
u'node': node,
u'type': hook_data[u'type'],
u'arg': hook_data[u'arg'],
u'persistent': persistent
})
hooks_list.append(
{
u"service": hook_data[u"service"],
u"node": node,
u"type": hook_data[u"type"],
u"arg": hook_data[u"arg"],
u"persistent": persistent,
}
)
return hooks_list

View file

@ -27,6 +27,7 @@ from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
from twisted.internet import defer
from sat.core.log import getLogger
log = getLogger(__name__)
from wokkel import disco, iwokkel
from wokkel import data_form
@ -36,7 +37,7 @@ from collections import Iterable
import copy
import itertools
NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0'
NS_SCHEMA = "https://salut-a-toi/protocol/schema:0"
PLUGIN_INFO = {
C.PI_NAME: "PubSub Schema",
@ -46,47 +47,63 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"],
C.PI_MAIN: "PubsubSchema",
C.PI_HANDLER: "yes",
C.PI_DESCRIPTION: _("""Handle Pubsub data schemas""")
C.PI_DESCRIPTION: _("""Handle Pubsub data schemas"""),
}
class PubsubSchema(object):
def __init__(self, host):
log.info(_(u"PubSub Schema initialization"))
self.host = host
self._p = self.host.plugins["XEP-0060"]
self._i = self.host.plugins["IDENTITY"]
host.bridge.addMethod("psSchemaGet", ".plugin",
in_sign='sss', out_sign='s',
method=self._getSchema,
async=True
)
host.bridge.addMethod("psSchemaSet", ".plugin",
in_sign='ssss', out_sign='',
method=self._setSchema,
async=True
)
host.bridge.addMethod("psSchemaUIGet", ".plugin",
in_sign='sss', out_sign='s',
method=utils.partial(self._getUISchema, default_node=None),
async=True
)
host.bridge.addMethod("psItemsFormGet", ".plugin",
in_sign='ssssiassa{ss}s', out_sign='(asa{ss})',
method=self._getDataFormItems,
async=True)
host.bridge.addMethod("psItemFormSend", ".plugin",
in_sign='ssa{sas}ssa{ss}s', out_sign='s',
method=self._sendDataFormItem,
async=True)
host.bridge.addMethod(
"psSchemaGet",
".plugin",
in_sign="sss",
out_sign="s",
method=self._getSchema,
async=True,
)
host.bridge.addMethod(
"psSchemaSet",
".plugin",
in_sign="ssss",
out_sign="",
method=self._setSchema,
async=True,
)
host.bridge.addMethod(
"psSchemaUIGet",
".plugin",
in_sign="sss",
out_sign="s",
method=utils.partial(self._getUISchema, default_node=None),
async=True,
)
host.bridge.addMethod(
"psItemsFormGet",
".plugin",
in_sign="ssssiassa{ss}s",
out_sign="(asa{ss})",
method=self._getDataFormItems,
async=True,
)
host.bridge.addMethod(
"psItemFormSend",
".plugin",
in_sign="ssa{sas}ssa{ss}s",
out_sign="s",
method=self._sendDataFormItem,
async=True,
)
def getHandler(self, client):
return SchemaHandler()
def _getSchemaBridgeCb(self, schema_elt):
if schema_elt is None:
return u''
return u""
return schema_elt.toXml()
def _getSchema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
@ -98,11 +115,11 @@ class PubsubSchema(object):
def _getSchemaCb(self, iq_elt):
try:
schema_elt = next(iq_elt.elements(NS_SCHEMA, 'schema'))
schema_elt = next(iq_elt.elements(NS_SCHEMA, "schema"))
except StopIteration:
raise exceptions.DataError('missing <schema> element')
raise exceptions.DataError("missing <schema> element")
try:
x_elt = next(schema_elt.elements((data_form.NS_X_DATA, 'x')))
x_elt = next(schema_elt.elements((data_form.NS_X_DATA, "x")))
except StopIteration:
# there is not schema on this node
return None
@ -117,18 +134,26 @@ class PubsubSchema(object):
@return (domish.Element, None): schema (<x> element)
None if not schema has been set on this node
"""
iq_elt = client.IQ(u'get')
iq_elt = client.IQ(u"get")
if service is not None:
iq_elt['to'] = service.full()
pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub'))
schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema'))
schema_elt['node'] = nodeIdentifier
iq_elt["to"] = service.full()
pubsub_elt = iq_elt.addElement((NS_SCHEMA, "pubsub"))
schema_elt = pubsub_elt.addElement((NS_SCHEMA, "schema"))
schema_elt["node"] = nodeIdentifier
d = iq_elt.send()
d.addCallback(self._getSchemaCb)
return d
@defer.inlineCallbacks
def getSchemaForm(self, client, service, nodeIdentifier, schema=None, form_type='form', copy_form=True):
def getSchemaForm(
self,
client,
service,
nodeIdentifier,
schema=None,
form_type="form",
copy_form=True,
):
"""get data form from node's schema
@param service(None, jid.JID): PubSub service
@ -147,7 +172,11 @@ class PubsubSchema(object):
log.debug(_(u"unspecified schema, we need to request it"))
schema = yield self.getSchema(client, service, nodeIdentifier)
if schema is None:
raise exceptions.DataError(_(u"no schema specified, and this node has no schema either, we can't construct the data form"))
raise exceptions.DataError(
_(
u"no schema specified, and this node has no schema either, we can't construct the data form"
)
)
elif isinstance(schema, data_form.Form):
if copy_form:
schema = copy.deepcopy(schema)
@ -156,17 +185,18 @@ class PubsubSchema(object):
try:
form = data_form.Form.fromElement(schema)
except data_form.Error as e:
raise exceptions.DataError(_(u"Invalid Schema: {msg}").format(
msg = e))
raise exceptions.DataError(_(u"Invalid Schema: {msg}").format(msg=e))
form.formType = form_type
defer.returnValue(form)
def schema2XMLUI(self, schema_elt):
form = data_form.Form.fromElement(schema_elt)
xmlui = xml_tools.dataForm2XMLUI(form, '')
xmlui = xml_tools.dataForm2XMLUI(form, "")
return xmlui
def _getUISchema(self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE):
def _getUISchema(
self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE
):
if not nodeIdentifier:
if not default_node:
raise ValueError(_(u"nodeIndentifier needs to be set"))
@ -185,7 +215,7 @@ class PubsubSchema(object):
def _setSchema(self, service, nodeIdentifier, schema, profile_key=C.PROF_KEY_NONE):
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
schema = generic.parseXml(schema.encode('utf-8'))
schema = generic.parseXml(schema.encode("utf-8"))
return self.setSchema(client, service, nodeIdentifier, schema)
def setSchema(self, client, service, nodeIdentifier, schema):
@ -196,31 +226,67 @@ class PubsubSchema(object):
"""
iq_elt = client.IQ()
if service is not None:
iq_elt['to'] = service.full()
pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub'))
schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema'))
schema_elt['node'] = nodeIdentifier
iq_elt["to"] = service.full()
pubsub_elt = iq_elt.addElement((NS_SCHEMA, "pubsub"))
schema_elt = pubsub_elt.addElement((NS_SCHEMA, "schema"))
schema_elt["node"] = nodeIdentifier
if schema is not None:
schema_elt.addChild(schema)
return iq_elt.send()
def _getDataFormItems(self, form_ns='', service='', node='', schema='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, profile_key=C.PROF_KEY_NONE):
def _getDataFormItems(
self,
form_ns="",
service="",
node="",
schema="",
max_items=10,
item_ids=None,
sub_id=None,
extra_dict=None,
profile_key=C.PROF_KEY_NONE,
):
client = self.host.getClient(profile_key)
service = jid.JID(service) if service else None
if not node:
raise exceptions.DataError(_(u'empty node is not allowed'))
raise exceptions.DataError(_(u"empty node is not allowed"))
if schema:
schema = generic.parseXml(schema.encode('utf-8'))
schema = generic.parseXml(schema.encode("utf-8"))
else:
schema = None
max_items = None if max_items == C.NO_LIMIT else max_items
extra = self._p.parseExtra(extra_dict)
d = self.getDataFormItems(client, service, node, schema, max_items or None, item_ids, sub_id or None, extra.rsm_request, extra.extra, form_ns=form_ns or None)
d = self.getDataFormItems(
client,
service,
node,
schema,
max_items or None,
item_ids,
sub_id or None,
extra.rsm_request,
extra.extra,
form_ns=form_ns or None,
)
d.addCallback(self._p.serItemsData)
return d
@defer.inlineCallbacks
def getDataFormItems(self, client, service, nodeIdentifier, schema=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None, default_node=None, form_ns=None, filters=None):
def getDataFormItems(
self,
client,
service,
nodeIdentifier,
schema=None,
max_items=None,
item_ids=None,
sub_id=None,
rsm_request=None,
extra=None,
default_node=None,
form_ns=None,
filters=None,
):
"""Get items known as being data forms, and convert them to XMLUI
@param schema(domish.Element, data_form.Form, None): schema of the node if known
@ -237,15 +303,28 @@ class PubsubSchema(object):
"""
if not nodeIdentifier:
if not default_node:
raise ValueError(_(u"default_node must be set if nodeIdentifier is not set"))
raise ValueError(
_(u"default_node must be set if nodeIdentifier is not set")
)
nodeIdentifier = default_node
# we need the initial form to get options of fields when suitable
schema_form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='result', copy_form=False)
items_data = yield self._p.getItems(client, service, nodeIdentifier, max_items, item_ids, sub_id, rsm_request, extra)
schema_form = yield self.getSchemaForm(
client, service, nodeIdentifier, schema, form_type="result", copy_form=False
)
items_data = yield self._p.getItems(
client,
service,
nodeIdentifier,
max_items,
item_ids,
sub_id,
rsm_request,
extra,
)
items, metadata = items_data
items_xmlui = []
for item_elt in items:
for x_elt in item_elt.elements((data_form.NS_X_DATA, u'x')):
for x_elt in item_elt.elements((data_form.NS_X_DATA, u"x")):
form = data_form.Form.fromElement(x_elt)
if form_ns and form.formNamespace != form_ns:
continue
@ -254,28 +333,59 @@ class PubsubSchema(object):
schema_form,
# FIXME: conflicts with schema (i.e. if "id" or "publisher" already exists)
# are not checked
prepend = (('label', 'id'),('text', item_elt['id'], u'id'),
('label', 'publisher'),('text', item_elt.getAttribute('publisher',''), u'publisher')),
filters = filters,
)
prepend=(
("label", "id"),
("text", item_elt["id"], u"id"),
("label", "publisher"),
("text", item_elt.getAttribute("publisher", ""), u"publisher"),
),
filters=filters,
)
items_xmlui.append(xmlui)
break
defer.returnValue((items_xmlui, metadata))
def _sendDataFormItem(self, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
def _sendDataFormItem(
self,
service,
nodeIdentifier,
values,
schema=None,
item_id=None,
extra=None,
profile_key=C.PROF_KEY_NONE,
):
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
if schema:
schema = generic.parseXml(schema.encode('utf-8'))
schema = generic.parseXml(schema.encode("utf-8"))
else:
schema = None
d = self.sendDataFormItem(client, service, nodeIdentifier, values, schema, item_id or None, extra, deserialise=True)
d.addCallback(lambda ret: ret or u'')
d = self.sendDataFormItem(
client,
service,
nodeIdentifier,
values,
schema,
item_id or None,
extra,
deserialise=True,
)
d.addCallback(lambda ret: ret or u"")
return d
@defer.inlineCallbacks
def sendDataFormItem(self, client, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, deserialise=False):
def sendDataFormItem(
self,
client,
service,
nodeIdentifier,
values,
schema=None,
item_id=None,
extra=None,
deserialise=False,
):
"""Publish an item as a dataform when we know that there is a schema
@param values(dict[key(unicode), [iterable[object], object]]): values set for the form
@ -289,26 +399,34 @@ class PubsubSchema(object):
other parameters as the same as for [self._p.sendItem]
@return (unicode): id of the created item
"""
form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='submit')
form = yield self.getSchemaForm(
client, service, nodeIdentifier, schema, form_type="submit"
)
for name, values_list in values.iteritems():
try:
field = form.fields[name]
except KeyError:
log.warning(_(u"field {name} doesn't exist, ignoring it").format(name=name))
log.warning(
_(u"field {name} doesn't exist, ignoring it").format(name=name)
)
continue
if isinstance(values_list, basestring) or not isinstance(values_list, Iterable):
if isinstance(values_list, basestring) or not isinstance(
values_list, Iterable
):
values_list = [values_list]
if deserialise:
if field.fieldType == 'boolean':
if field.fieldType == "boolean":
values_list = [C.bool(v) for v in values_list]
elif field.fieldType == 'text-multi':
elif field.fieldType == "text-multi":
# for text-multi, lines must be put on separate values
values_list = list(itertools.chain(*[v.splitlines() for v in values_list]))
values_list = list(
itertools.chain(*[v.splitlines() for v in values_list])
)
elif 'jid' in field.fieldType:
elif "jid" in field.fieldType:
values_list = [jid.JID(v) for v in values_list]
if 'list' in field.fieldType:
if "list" in field.fieldType:
# for lists, we check that given values are allowed in form
allowed_values = [o.value for o in field.options]
values_list = [v for v in values_list if v in allowed_values]
@ -317,7 +435,9 @@ class PubsubSchema(object):
values_list = field.values
field.values = values_list
yield self._p.sendItem(client, service, nodeIdentifier, form.toElement(), item_id, extra)
yield self._p.sendItem(
client, service, nodeIdentifier, form.toElement(), item_id, extra
)
## filters ##
# filters useful for data form to XMLUI conversion #
@ -327,7 +447,7 @@ class PubsubSchema(object):
if not args[0]:
# value is not filled: we use user part of publisher (if we have it)
try:
publisher = jid.JID(form_xmlui.named_widgets['publisher'].value)
publisher = jid.JID(form_xmlui.named_widgets["publisher"].value)
except (KeyError, RuntimeError):
pass
else:
@ -339,18 +459,20 @@ class PubsubSchema(object):
main use case is using a textbox for labels
"""
if widget_type != u'textbox':
if widget_type != u"textbox":
return widget_type, args, kwargs
widget_type = u'list'
options = [o for o in args.pop(0).split(u'\n') if o]
kwargs = {'options': options,
'name': kwargs.get('name'),
'styles': (u'noselect', u'extensible', u'reducible')}
widget_type = u"list"
options = [o for o in args.pop(0).split(u"\n") if o]
kwargs = {
"options": options,
"name": kwargs.get("name"),
"styles": (u"noselect", u"extensible", u"reducible"),
}
return widget_type, args, kwargs
def dateFilter(self, form_xmlui, widget_type, args, kwargs):
"""Convert a string with a date to a unix timestamp"""
if widget_type != u'string' or not args[0]:
if widget_type != u"string" or not args[0]:
return widget_type, args, kwargs
# we convert XMPP date to timestamp
try:
@ -377,7 +499,19 @@ class PubsubSchema(object):
return client, service, node, max_items, extra, sub_id
def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra=None, default_node=None, form_ns=None, filters=None, profile_key=C.PROF_KEY_NONE):
def _get(
self,
service="",
node="",
max_items=10,
item_ids=None,
sub_id=None,
extra=None,
default_node=None,
form_ns=None,
filters=None,
profile_key=C.PROF_KEY_NONE,
):
"""Bridge method to retrieve data from node with schema
this method is a helper so dependant plugins can use it directly
@ -392,11 +526,16 @@ class PubsubSchema(object):
extra = {}
# XXX: Q&D way to get list for labels when displaying them, but text when we
# have to modify them
if C.bool(extra.get('labels_as_list', C.BOOL_FALSE)):
if C.bool(extra.get("labels_as_list", C.BOOL_FALSE)):
filters = filters.copy()
filters[u'labels'] = self.textbox2ListFilter
client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(service, node, max_items, sub_id, extra, profile_key)
d = self.getDataFormItems(client, service, node or None,
filters[u"labels"] = self.textbox2ListFilter
client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(
service, node, max_items, sub_id, extra, profile_key
)
d = self.getDataFormItems(
client,
service,
node or None,
max_items=max_items,
item_ids=item_ids,
sub_id=sub_id,
@ -404,7 +543,8 @@ class PubsubSchema(object):
extra=extra.extra,
default_node=default_node,
form_ns=form_ns,
filters=filters)
filters=filters,
)
d.addCallback(self._p.serItemsData)
return d
@ -416,30 +556,65 @@ class PubsubSchema(object):
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
if schema:
schema = generic.parseXml(schema.encode('utf-8'))
schema = generic.parseXml(schema.encode("utf-8"))
else:
schema = None
if extra and u'update' in extra:
extra[u'update'] = C.bool(extra[u'update'])
if extra and u"update" in extra:
extra[u"update"] = C.bool(extra[u"update"])
return client, service, node or None, schema, item_id or None, extra
def _set(self, service, node, values, schema=None, item_id=None, extra=None, default_node=None, form_ns=None, fill_author=True, profile_key=C.PROF_KEY_NONE):
def _set(
self,
service,
node,
values,
schema=None,
item_id=None,
extra=None,
default_node=None,
form_ns=None,
fill_author=True,
profile_key=C.PROF_KEY_NONE,
):
"""Bridge method to set item in node with schema
this method is a helper so dependant plugins can use it directly
when adding *Set methods
"""
client, service, node, schema, item_id, extra = self.prepareBridgeSet(service, node, schema, item_id, extra)
d = self.set(client, service, node, values, schema, item_id, extra,
deserialise=True,
form_ns=form_ns,
default_node=default_node,
fill_author=fill_author)
d.addCallback(lambda ret: ret or u'')
client, service, node, schema, item_id, extra = self.prepareBridgeSet(
service, node, schema, item_id, extra
)
d = self.set(
client,
service,
node,
values,
schema,
item_id,
extra,
deserialise=True,
form_ns=form_ns,
default_node=default_node,
fill_author=fill_author,
)
d.addCallback(lambda ret: ret or u"")
return d
@defer.inlineCallbacks
def set(self, client, service, node, values, schema, item_id, extra, deserialise, form_ns, default_node=None, fill_author=True):
def set(
self,
client,
service,
node,
values,
schema,
item_id,
extra,
deserialise,
form_ns,
default_node=None,
fill_author=True,
):
"""Set an item in a node with a schema
This method can be used directly by *Set methods added by dependant plugin
@ -463,44 +638,56 @@ class PubsubSchema(object):
node = default_node
now = utils.xmpp_date()
if not item_id:
values['created'] = now
elif extra.get(u'update', False):
values["created"] = now
elif extra.get(u"update", False):
if item_id is None:
raise exceptions.DataError(_(u'if extra["update"] is set, item_id must be set too'))
raise exceptions.DataError(
_(u'if extra["update"] is set, item_id must be set too')
)
try:
# we get previous item
items_data = yield self._p.getItems(client, service, node, item_ids=[item_id])
items_data = yield self._p.getItems(
client, service, node, item_ids=[item_id]
)
item_elt = items_data[0][0]
except Exception as e:
log.warning(_(u"Can't get previous item, update ignored: {reason}").format(
reason = e))
log.warning(
_(u"Can't get previous item, update ignored: {reason}").format(
reason=e
)
)
else:
# and parse it
form = data_form.findForm(item_elt, form_ns)
if form is None:
log.warning(_(u"Can't parse previous item, update ignored: data form not found").format(
reason = e))
log.warning(
_(
u"Can't parse previous item, update ignored: data form not found"
).format(reason=e)
)
else:
for name, field in form.fields.iteritems():
if name not in values:
values[name] = u'\n'.join(unicode(v) for v in field.values)
values[name] = u"\n".join(unicode(v) for v in field.values)
values['updated'] = now
values["updated"] = now
if fill_author:
if not values.get('author'):
if not values.get("author"):
identity = yield self._i.getIdentity(client, client.jid)
values['author'] = identity['nick']
if not values.get('author_jid'):
values['author_jid'] = client.jid.full()
item_id = yield self.sendDataFormItem(client, service, node, values, schema, item_id, extra, deserialise)
values["author"] = identity["nick"]
if not values.get("author_jid"):
values["author_jid"] = client.jid.full()
item_id = yield self.sendDataFormItem(
client, service, node, values, schema, item_id, extra, deserialise
)
defer.returnValue(item_id)
class SchemaHandler(XMPPHandler):
implements(iwokkel.IDisco)
def getDiscoInfo(self, requestor, service, nodeIdentifier=''):
def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
return [disco.DiscoFeature(NS_SCHEMA)]
def getDiscoItems(self, requestor, service, nodeIdentifier=''):
def getDiscoItems(self, requestor, service, nodeIdentifier=""):
return []

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.internet import defer
from sat.core import exceptions
@ -37,14 +38,13 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "ImportPlugin",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Generic import plugin, base for specialized importers""")
C.PI_DESCRIPTION: _(u"""Generic import plugin, base for specialized importers"""),
}
Importer = collections.namedtuple('Importer', ('callback', 'short_desc', 'long_desc'))
Importer = collections.namedtuple("Importer", ("callback", "short_desc", "long_desc"))
class ImportPlugin(object):
def __init__(self, host):
log.info(_("plugin Import initialization"))
self.host = host
@ -64,21 +64,51 @@ class ImportPlugin(object):
@param name(unicode): import handler name
"""
assert name == name.lower().strip()
log.info(_(u'initializing {name} import handler').format(name=name))
log.info(_(u"initializing {name} import handler").format(name=name))
import_handler.name = name
import_handler.register = partial(self.register, import_handler)
import_handler.unregister = partial(self.unregister, import_handler)
import_handler.importers = {}
def _import(name, location, options, pubsub_service, pubsub_node, profile):
return self._doImport(import_handler, name, location, options, pubsub_service, pubsub_node, profile)
return self._doImport(
import_handler,
name,
location,
options,
pubsub_service,
pubsub_node,
profile,
)
def _importList():
return self.listImporters(import_handler)
def _importDesc(name):
return self.getDescription(import_handler, name)
self.host.bridge.addMethod(name + "Import", ".plugin", in_sign='ssa{ss}sss', out_sign='s', method=_import, async=True)
self.host.bridge.addMethod(name + "ImportList", ".plugin", in_sign='', out_sign='a(ss)', method=_importList)
self.host.bridge.addMethod(name + "ImportDesc", ".plugin", in_sign='s', out_sign='(ss)', method=_importDesc)
self.host.bridge.addMethod(
name + "Import",
".plugin",
in_sign="ssa{ss}sss",
out_sign="s",
method=_import,
async=True,
)
self.host.bridge.addMethod(
name + "ImportList",
".plugin",
in_sign="",
out_sign="a(ss)",
method=_importList,
)
self.host.bridge.addMethod(
name + "ImportDesc",
".plugin",
in_sign="s",
out_sign="(ss)",
method=_importDesc,
)
def getProgress(self, import_handler, progress_id, profile):
client = self.host.getClient(profile)
@ -87,7 +117,10 @@ class ImportPlugin(object):
def listImporters(self, import_handler):
importers = import_handler.importers.keys()
importers.sort()
return [(name, import_handler.importers[name].short_desc) for name in import_handler.importers]
return [
(name, import_handler.importers[name].short_desc)
for name in import_handler.importers
]
def getDescription(self, import_handler, name):
"""Return import short and long descriptions
@ -98,13 +131,24 @@ class ImportPlugin(object):
try:
importer = import_handler.importers[name]
except KeyError:
raise exceptions.NotFound(u"{handler_name} importer not found [{name}]".format(
handler_name = import_handler.name,
name = name))
raise exceptions.NotFound(
u"{handler_name} importer not found [{name}]".format(
handler_name=import_handler.name, name=name
)
)
else:
return importer.short_desc, importer.long_desc
def _doImport(self, import_handler, name, location, options, pubsub_service='', pubsub_node='', profile=C.PROF_KEY_NONE):
def _doImport(
self,
import_handler,
name,
location,
options,
pubsub_service="",
pubsub_node="",
profile=C.PROF_KEY_NONE,
):
client = self.host.getClient(profile)
options = {key: unicode(value) for key, value in options.iteritems()}
for option in import_handler.BOOL_OPTIONS:
@ -116,12 +160,31 @@ class ImportPlugin(object):
try:
options[option] = json.loads(options[option])
except ValueError:
raise exceptions.DataError(_(u'invalid json option: {name}').format(name=option))
raise exceptions.DataError(
_(u"invalid json option: {name}").format(name=option)
)
pubsub_service = jid.JID(pubsub_service) if pubsub_service else None
return self.doImport(client, import_handler, unicode(name), unicode(location), options, pubsub_service, pubsub_node or None)
return self.doImport(
client,
import_handler,
unicode(name),
unicode(location),
options,
pubsub_service,
pubsub_node or None,
)
@defer.inlineCallbacks
def doImport(self, client, import_handler, name, location, options=None, pubsub_service=None, pubsub_node=None):
def doImport(
self,
client,
import_handler,
name,
location,
options=None,
pubsub_service=None,
pubsub_node=None,
):
"""Import data
@param import_handler(object): instance of the import handler
@ -142,7 +205,7 @@ class ImportPlugin(object):
for opt_name, opt_default in import_handler.OPT_DEFAULTS.iteritems():
# we want a filled options dict, with all empty or False values removed
try:
value =options[opt_name]
value = options[opt_name]
except KeyError:
if opt_default:
options[opt_name] = opt_default
@ -154,31 +217,61 @@ class ImportPlugin(object):
importer = import_handler.importers[name]
except KeyError:
raise exceptions.NotFound(u"Importer [{}] not found".format(name))
items_import_data, items_count = yield importer.callback(client, location, options)
items_import_data, items_count = yield importer.callback(
client, location, options
)
progress_id = unicode(uuid.uuid4())
try:
_import = client._import
except AttributeError:
_import = client._import = {}
progress_data = _import.setdefault(import_handler.name, {})
progress_data[progress_id] = {u'position': '0'}
progress_data[progress_id] = {u"position": "0"}
if items_count is not None:
progress_data[progress_id]['size'] = unicode(items_count)
metadata = {'name': u'{}: {}'.format(name, location),
'direction': 'out',
'type': import_handler.name.upper() + '_IMPORT'
}
self.host.registerProgressCb(progress_id, partial(self.getProgress, import_handler), metadata, profile=client.profile)
self.host.bridge.progressStarted(progress_id, metadata, client.profile)
session = { # session data, can be used by importers
u'root_service': pubsub_service,
u'root_node': pubsub_node
progress_data[progress_id]["size"] = unicode(items_count)
metadata = {
"name": u"{}: {}".format(name, location),
"direction": "out",
"type": import_handler.name.upper() + "_IMPORT",
}
self.recursiveImport(client, import_handler, items_import_data, progress_id, session, options, None, pubsub_service, pubsub_node)
self.host.registerProgressCb(
progress_id,
partial(self.getProgress, import_handler),
metadata,
profile=client.profile,
)
self.host.bridge.progressStarted(progress_id, metadata, client.profile)
session = { #  session data, can be used by importers
u"root_service": pubsub_service,
u"root_node": pubsub_node,
}
self.recursiveImport(
client,
import_handler,
items_import_data,
progress_id,
session,
options,
None,
pubsub_service,
pubsub_node,
)
defer.returnValue(progress_id)
@defer.inlineCallbacks
def recursiveImport(self, client, import_handler, items_import_data, progress_id, session, options, return_data=None, service=None, node=None, depth=0):
def recursiveImport(
self,
client,
import_handler,
items_import_data,
progress_id,
session,
options,
return_data=None,
service=None,
node=None,
depth=0,
):
"""Do the import recursively
@param import_handler(object): instance of the import handler
@ -196,33 +289,37 @@ class ImportPlugin(object):
if return_data is None:
return_data = {}
for idx, item_import_data in enumerate(items_import_data):
item_data = yield import_handler.importItem(client, item_import_data, session, options, return_data, service, node)
item_data = yield import_handler.importItem(
client, item_import_data, session, options, return_data, service, node
)
yield import_handler.itemFilters(client, item_data, session, options)
recurse_kwargs = yield import_handler.importSubItems(client, item_import_data, item_data, session, options)
recurse_kwargs = yield import_handler.importSubItems(
client, item_import_data, item_data, session, options
)
yield import_handler.publishItem(client, item_data, service, node, session)
if recurse_kwargs is not None:
recurse_kwargs['client'] = client
recurse_kwargs['import_handler'] = import_handler
recurse_kwargs['progress_id'] = progress_id
recurse_kwargs['session'] = session
recurse_kwargs.setdefault('options', options)
recurse_kwargs['return_data'] = return_data
recurse_kwargs['depth'] = depth + 1
recurse_kwargs["client"] = client
recurse_kwargs["import_handler"] = import_handler
recurse_kwargs["progress_id"] = progress_id
recurse_kwargs["session"] = session
recurse_kwargs.setdefault("options", options)
recurse_kwargs["return_data"] = return_data
recurse_kwargs["depth"] = depth + 1
log.debug(_(u"uploading subitems"))
yield self.recursiveImport(**recurse_kwargs)
if depth == 0:
client._import[import_handler.name][progress_id]['position'] = unicode(idx+1)
client._import[import_handler.name][progress_id]["position"] = unicode(
idx + 1
)
if depth == 0:
self.host.bridge.progressFinished(progress_id,
return_data,
client.profile)
self.host.bridge.progressFinished(progress_id, return_data, client.profile)
self.host.removeProgressCb(progress_id, client.profile)
del client._import[import_handler.name][progress_id]
def register(self, import_handler, name, callback, short_desc='', long_desc=''):
def register(self, import_handler, name, callback, short_desc="", long_desc=""):
"""Register an Importer method
@param name(unicode): unique importer name, should indicate the software it can import and always lowercase
@ -239,9 +336,11 @@ class ImportPlugin(object):
"""
name = name.lower()
if name in import_handler.importers:
raise exceptions.ConflictError(_(u"An {handler_name} importer with the name {name} already exist").format(
handler_name = import_handler.name,
name = name))
raise exceptions.ConflictError(
_(
u"An {handler_name} importer with the name {name} already exist"
).format(handler_name=import_handler.name, name=name)
)
import_handler.importers[name] = Importer(callback, short_desc, long_desc)
def unregister(self, import_handler, name):

View file

@ -19,6 +19,7 @@
from sat.core.i18n import _, D_
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from sat.tools import xml_tools
@ -31,7 +32,7 @@ from twisted.python.failure import Failure
from twisted.words.protocols.jabber import jid
from sat.tools import email as sat_email
# FIXME: this plugin code is old and need a cleaning
#  FIXME: this plugin code is old and need a cleaning
# TODO: account deletion/password change need testing
@ -41,10 +42,10 @@ PLUGIN_INFO = {
C.PI_TYPE: "MISC",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0077"],
C.PI_RECOMMENDATIONS: ['GROUPBLOG'],
C.PI_RECOMMENDATIONS: ["GROUPBLOG"],
C.PI_MAIN: "MiscAccount",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""SàT account creation""")
C.PI_DESCRIPTION: _(u"""SàT account creation"""),
}
CONFIG_SECTION = "plugin account"
@ -53,22 +54,24 @@ CONFIG_SECTION = "plugin account"
# all theses values (key=option name, value=default) can (and should) be overriden in sat.conf
# in section CONFIG_SECTION
default_conf = {"email_from": "NOREPLY@example.net",
"email_server": "localhost",
"email_sender_domain": "",
"email_port": 25,
"email_username": "",
"email_password": "",
"email_starttls": "false",
"email_auth": "false",
"email_admins_list": [],
"admin_email": "",
"new_account_server": "localhost",
"new_account_domain": "", # use xmpp_domain if not found
"reserved_list": ['libervia'] # profiles which can't be used
}
default_conf = {
"email_from": "NOREPLY@example.net",
"email_server": "localhost",
"email_sender_domain": "",
"email_port": 25,
"email_username": "",
"email_password": "",
"email_starttls": "false",
"email_auth": "false",
"email_admins_list": [],
"admin_email": "",
"new_account_server": "localhost",
"new_account_domain": "", #  use xmpp_domain if not found
"reserved_list": ["libervia"], # profiles which can't be used
}
WELCOME_MSG = D_(u"""Welcome to Libervia, the web interface of Salut à Toi.
WELCOME_MSG = D_(
u"""Welcome to Libervia, the web interface of Salut à Toi.
Your account on {domain} has been successfully created.
This is a demonstration version to show you the current status of the project.
@ -88,55 +91,101 @@ Any feedback welcome. Thank you!
Salut à Toi association
https://www.salut-a-toi.org
""")
"""
)
DEFAULT_DOMAIN = u"example.net"
class MiscAccount(object):
"""Account plugin: create a SàT + XMPP account, used by Libervia"""
# XXX: This plugin was initialy a Q&D one used for the demo.
# TODO: cleaning, separate email handling, more configuration/tests, fixes
def __init__(self, host):
log.info(_(u"Plugin Account initialization"))
self.host = host
host.bridge.addMethod("registerSatAccount", ".plugin", in_sign='sss', out_sign='', method=self._registerAccount, async=True)
host.bridge.addMethod("getNewAccountDomain", ".plugin", in_sign='', out_sign='s', method=self.getNewAccountDomain, async=False)
host.bridge.addMethod("getAccountDialogUI", ".plugin", in_sign='s', out_sign='s', method=self._getAccountDialogUI, async=False)
host.bridge.addMethod("asyncConnectWithXMPPCredentials", ".plugin", in_sign='ss', out_sign='b', method=self.asyncConnectWithXMPPCredentials, async=True)
host.bridge.addMethod(
"registerSatAccount",
".plugin",
in_sign="sss",
out_sign="",
method=self._registerAccount,
async=True,
)
host.bridge.addMethod(
"getNewAccountDomain",
".plugin",
in_sign="",
out_sign="s",
method=self.getNewAccountDomain,
async=False,
)
host.bridge.addMethod(
"getAccountDialogUI",
".plugin",
in_sign="s",
out_sign="s",
method=self._getAccountDialogUI,
async=False,
)
host.bridge.addMethod(
"asyncConnectWithXMPPCredentials",
".plugin",
in_sign="ss",
out_sign="b",
method=self.asyncConnectWithXMPPCredentials,
async=True,
)
self.fixEmailAdmins()
self._sessions = Sessions()
self.__account_cb_id = host.registerCallback(self._accountDialogCb, with_data=True)
self.__change_password_id = host.registerCallback(self.__changePasswordCb, with_data=True)
self.__account_cb_id = host.registerCallback(
self._accountDialogCb, with_data=True
)
self.__change_password_id = host.registerCallback(
self.__changePasswordCb, with_data=True
)
def deleteBlogCallback(posts, comments):
return lambda data, profile: self.__deleteBlogPostsCb(posts, comments, data, profile)
return lambda data, profile: self.__deleteBlogPostsCb(
posts, comments, data, profile
)
self.__delete_posts_id = host.registerCallback(deleteBlogCallback(True, False), with_data=True)
self.__delete_comments_id = host.registerCallback(deleteBlogCallback(False, True), with_data=True)
self.__delete_posts_comments_id = host.registerCallback(deleteBlogCallback(True, True), with_data=True)
self.__delete_account_id = host.registerCallback(self.__deleteAccountCb, with_data=True)
self.__delete_posts_id = host.registerCallback(
deleteBlogCallback(True, False), with_data=True
)
self.__delete_comments_id = host.registerCallback(
deleteBlogCallback(False, True), with_data=True
)
self.__delete_posts_comments_id = host.registerCallback(
deleteBlogCallback(True, True), with_data=True
)
self.__delete_account_id = host.registerCallback(
self.__deleteAccountCb, with_data=True
)
# FIXME: remove this after some time, when the deprecated parameter is really abandoned
def fixEmailAdmins(self):
"""Handle deprecated config option "admin_email" to fix the admin emails list"""
admin_email = self.getConfig('admin_email')
admin_email = self.getConfig("admin_email")
if not admin_email:
return
log.warning(u"admin_email parameter is deprecated, please use email_admins_list instead")
log.warning(
u"admin_email parameter is deprecated, please use email_admins_list instead"
)
param_name = "email_admins_list"
try:
section = ""
value = self.host.memory.getConfig(section, param_name, Exception)
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
section = CONFIG_SECTION
value = self.host.memory.getConfig(section, param_name, default_conf[param_name])
value = self.host.memory.getConfig(
section, param_name, default_conf[param_name]
)
value = set(value)
value.add(admin_email)
@ -193,7 +242,7 @@ class MiscAccount(object):
if not password or not profile:
raise exceptions.DataError
if profile.lower() in self.getConfig('reserved_list'):
if profile.lower() in self.getConfig("reserved_list"):
return defer.fail(Failure(exceptions.ConflictError))
d = self.host.memory.createProfile(profile, password)
@ -216,11 +265,15 @@ class MiscAccount(object):
else:
jid_s = profile + u"@" + self.getNewAccountDomain()
jid_ = jid.JID(jid_s)
d = self.host.plugins['XEP-0077'].registerNewAccount(jid_, password)
d = self.host.plugins["XEP-0077"].registerNewAccount(jid_, password)
def setParams(dummy):
self.host.memory.setParam("JabberID", jid_s, "Connection", profile_key=profile)
d = self.host.memory.setParam("Password", password, "Connection", profile_key=profile)
self.host.memory.setParam(
"JabberID", jid_s, "Connection", profile_key=profile
)
d = self.host.memory.setParam(
"Password", password, "Connection", profile_key=profile
)
return d
def removeProfile(failure):
@ -235,9 +288,11 @@ class MiscAccount(object):
def _sendEmailEb(self, failure_, email):
# TODO: return error code to user
log.error(_(u"Failed to send account creation confirmation to {email}: {msg}").format(
email = email,
msg = failure_))
log.error(
_(u"Failed to send account creation confirmation to {email}: {msg}").format(
email=email, msg=failure_
)
)
def sendEmails(self, email, profile):
# time to send the email
@ -245,43 +300,68 @@ class MiscAccount(object):
domain = self.getNewAccountDomain()
# email to the administrators
admins_emails = self.getConfig('email_admins_list')
admins_emails = self.getConfig("email_admins_list")
if not admins_emails:
log.warning(u"No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter")
log.warning(
u"No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter"
)
d_admin = defer.fail(exceptions.DataError("no admin email"))
else:
subject = _(u'New Libervia account created')
body = (u"""New account created: {profile} [{email}]""".format(
profile = profile,
subject = _(u"New Libervia account created")
body = u"""New account created: {profile} [{email}]""".format(
profile=profile,
# there is no email when an existing XMPP account is used
email = email or u"<no email>"))
email=email or u"<no email>",
)
d_admin = sat_email.sendEmail(self.host, admins_emails, subject, body)
admins_emails_txt = u', '.join([u'<' + addr + u'>' for addr in admins_emails])
d_admin.addCallbacks(lambda dummy: log.debug(u"Account creation notification sent to admin(s) {}".format(admins_emails_txt)),
lambda dummy: log.error(u"Failed to send account creation notification to admin {}".format(admins_emails_txt)))
admins_emails_txt = u", ".join([u"<" + addr + u">" for addr in admins_emails])
d_admin.addCallbacks(
lambda dummy: log.debug(
u"Account creation notification sent to admin(s) {}".format(
admins_emails_txt
)
),
lambda dummy: log.error(
u"Failed to send account creation notification to admin {}".format(
admins_emails_txt
)
),
)
if not email:
# TODO: if use register with an existing account, an XMPP message should be sent
return d_admin
jid_s = self.host.memory.getParamA(u"JabberID", u"Connection", profile_key=profile)
subject = _(u'Your Libervia account has been created')
body = (_(WELCOME_MSG).format(profile=profile, jid=jid_s, domain=domain))
jid_s = self.host.memory.getParamA(
u"JabberID", u"Connection", profile_key=profile
)
subject = _(u"Your Libervia account has been created")
body = _(WELCOME_MSG).format(profile=profile, jid=jid_s, domain=domain)
# XXX: this will not fail when the email address doesn't exist
# FIXME: check email reception to validate email given by the user
# FIXME: delete the profile if the email could not been sent?
d_user = sat_email.sendEmail(self.host, [email], subject, body)
d_user.addCallbacks(lambda dummy: log.debug(u"Account creation confirmation sent to <{}>".format(email)),
self._sendEmailEb)
d_user.addCallbacks(
lambda dummy: log.debug(
u"Account creation confirmation sent to <{}>".format(email)
),
self._sendEmailEb,
)
return defer.DeferredList([d_user, d_admin])
def getNewAccountDomain(self):
"""get the domain that will be set to new account"""
domain = self.getConfig('new_account_domain') or self.getConfig('xmpp_domain', None)
domain = self.getConfig("new_account_domain") or self.getConfig(
"xmpp_domain", None
)
if not domain:
log.warning(_(u'xmpp_domain needs to be set in sat.conf. Using "{default}" meanwhile').format(default=DEFAULT_DOMAIN))
log.warning(
_(
u'xmpp_domain needs to be set in sat.conf. Using "{default}" meanwhile'
).format(default=DEFAULT_DOMAIN)
)
return DEFAULT_DOMAIN
return domain
@ -291,10 +371,17 @@ class MiscAccount(object):
@param profile: %(doc_profile)s
@return: XML of the dialog
"""
form_ui = xml_tools.XMLUI("form", "tabs", title=D_("Manage your account"), submit_id=self.__account_cb_id)
form_ui = xml_tools.XMLUI(
"form",
"tabs",
title=D_("Manage your account"),
submit_id=self.__account_cb_id,
)
tab_container = form_ui.current_container
tab_container.addTab("update", D_("Change your password"), container=xml_tools.PairsContainer)
tab_container.addTab(
"update", D_("Change your password"), container=xml_tools.PairsContainer
)
form_ui.addLabel(D_("Current profile password"))
form_ui.addPassword("current_passwd", value="")
form_ui.addLabel(D_("New password"))
@ -328,7 +415,9 @@ class MiscAccount(object):
@param data
@param profile
"""
sat_cipher = yield self.host.memory.asyncGetParamA(C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile)
sat_cipher = yield self.host.memory.asyncGetParamA(
C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile
)
@defer.inlineCallbacks
def verify(attempt):
@ -340,7 +429,7 @@ class MiscAccount(object):
message = D_("The provided profile password doesn't match.")
error_ui = xml_tools.XMLUI("popup", title=D_("Attempt failure"))
error_ui.addText(message)
return {'xmlui': error_ui.toXml()}
return {"xmlui": error_ui.toXml()}
# check for account deletion
# FIXME: uncomment and fix these features
@ -370,9 +459,9 @@ class MiscAccount(object):
"""
# check for password modification
current_passwd = data[xml_tools.SAT_FORM_PREFIX + 'current_passwd']
new_passwd1 = data[xml_tools.SAT_FORM_PREFIX + 'new_passwd1']
new_passwd2 = data[xml_tools.SAT_FORM_PREFIX + 'new_passwd2']
current_passwd = data[xml_tools.SAT_FORM_PREFIX + "current_passwd"]
new_passwd1 = data[xml_tools.SAT_FORM_PREFIX + "new_passwd1"]
new_passwd2 = data[xml_tools.SAT_FORM_PREFIX + "new_passwd2"]
if new_passwd1 or new_passwd2:
verified = yield verify(current_passwd)
assert isinstance(verified, bool)
@ -381,7 +470,11 @@ class MiscAccount(object):
data = yield self.__changePassword(new_passwd1, profile=profile)
defer.returnValue(data)
else:
defer.returnValue(error_ui(D_("The values entered for the new password are not equal.")))
defer.returnValue(
error_ui(
D_("The values entered for the new password are not equal.")
)
)
defer.returnValue(error_ui())
defer.returnValue({})
@ -392,11 +485,22 @@ class MiscAccount(object):
@param password (str): the new password
@param profile (str): %(doc_profile)s
"""
session_id, dummy = self._sessions.newSession({'new_password': password}, profile=profile)
form_ui = xml_tools.XMLUI("form", title=D_("Change your password?"), submit_id=self.__change_password_id, session_id=session_id)
form_ui.addText(D_("Note for advanced users: this will actually change both your SàT profile password AND your XMPP account password."))
session_id, dummy = self._sessions.newSession(
{"new_password": password}, profile=profile
)
form_ui = xml_tools.XMLUI(
"form",
title=D_("Change your password?"),
submit_id=self.__change_password_id,
session_id=session_id,
)
form_ui.addText(
D_(
"Note for advanced users: this will actually change both your SàT profile password AND your XMPP account password."
)
)
form_ui.addText(D_("Continue with changing the password?"))
return {'xmlui': form_ui.toXml()}
return {"xmlui": form_ui.toXml()}
def __changePasswordCb(self, data, profile):
"""Actually change the user XMPP account and SàT profile password
@ -404,22 +508,33 @@ class MiscAccount(object):
@profile (str): %(doc_profile)s
"""
client = self.host.getClient(profile)
password = self._sessions.profileGet(data['session_id'], profile)['new_password']
del self._sessions[data['session_id']]
password = self._sessions.profileGet(data["session_id"], profile)["new_password"]
del self._sessions[data["session_id"]]
def passwordChanged(dummy):
d = self.host.memory.setParam(C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=profile)
d.addCallback(lambda dummy: self.host.memory.setParam("Password", password, "Connection", profile_key=profile))
d = self.host.memory.setParam(
C.PROFILE_PASS_PATH[1],
password,
C.PROFILE_PASS_PATH[0],
profile_key=profile,
)
d.addCallback(
lambda dummy: self.host.memory.setParam(
"Password", password, "Connection", profile_key=profile
)
)
confirm_ui = xml_tools.XMLUI("popup", title=D_("Confirmation"))
confirm_ui.addText(D_("Your password has been changed."))
return defer.succeed({'xmlui': confirm_ui.toXml()})
return defer.succeed({"xmlui": confirm_ui.toXml()})
def errback(failure):
error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
error_ui.addText(D_("Your password could not be changed: %s") % failure.getErrorMessage())
return defer.succeed({'xmlui': error_ui.toXml()})
error_ui.addText(
D_("Your password could not be changed: %s") % failure.getErrorMessage()
)
return defer.succeed({"xmlui": error_ui.toXml()})
d = self.host.plugins['XEP-0077'].changePassword(client, password)
d = self.host.plugins["XEP-0077"].changePassword(client, password)
d.addCallbacks(passwordChanged, errback)
return d
@ -427,12 +542,31 @@ class MiscAccount(object):
"""Ask for a confirmation before deleting the XMPP account and SàT profile
@param profile
"""
form_ui = xml_tools.XMLUI("form", title=D_("Delete your account?"), submit_id=self.__delete_account_id)
form_ui.addText(D_("If you confirm this dialog, you will be disconnected and then your XMPP account AND your SàT profile will both be DELETED."))
target = D_('contact list, messages history, blog posts and comments' if 'GROUPBLOG' in self.host.plugins else D_('contact list and messages history'))
form_ui.addText(D_("All your data stored on %(server)s, including your %(target)s will be erased.") % {'server': self.getNewAccountDomain(), 'target': target})
form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
return {'xmlui': form_ui.toXml()}
form_ui = xml_tools.XMLUI(
"form", title=D_("Delete your account?"), submit_id=self.__delete_account_id
)
form_ui.addText(
D_(
"If you confirm this dialog, you will be disconnected and then your XMPP account AND your SàT profile will both be DELETED."
)
)
target = D_(
"contact list, messages history, blog posts and comments"
if "GROUPBLOG" in self.host.plugins
else D_("contact list and messages history")
)
form_ui.addText(
D_(
"All your data stored on %(server)s, including your %(target)s will be erased."
)
% {"server": self.getNewAccountDomain(), "target": target}
)
form_ui.addText(
D_(
"There is no other confirmation dialog, this is the very last one! Are you sure?"
)
)
return {"xmlui": form_ui.toXml()}
def __deleteAccountCb(self, data, profile):
"""Actually delete the XMPP account and SàT profile
@ -441,18 +575,25 @@ class MiscAccount(object):
@param profile
"""
client = self.host.getClient(profile)
def userDeleted(dummy):
# FIXME: client should be disconnected at this point, so 2 next loop should be removed (to be confirmed)
for jid_ in client.roster._jids: # empty roster
client.presence.unsubscribe(jid_)
for jid_ in self.host.memory.getWaitingSub(profile): # delete waiting subscriptions
for jid_ in self.host.memory.getWaitingSub(
profile
): # delete waiting subscriptions
self.host.memory.delWaitingSub(jid_)
delete_profile = lambda: self.host.memory.asyncDeleteProfile(profile, force=True)
if 'GROUPBLOG' in self.host.plugins:
d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogsAndComments(profile_key=profile)
delete_profile = lambda: self.host.memory.asyncDeleteProfile(
profile, force=True
)
if "GROUPBLOG" in self.host.plugins:
d = self.host.plugins["GROUPBLOG"].deleteAllGroupBlogsAndComments(
profile_key=profile
)
d.addCallback(lambda dummy: delete_profile())
else:
delete_profile()
@ -461,10 +602,13 @@ class MiscAccount(object):
def errback(failure):
error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
error_ui.addText(D_("Your XMPP account could not be deleted: %s") % failure.getErrorMessage())
return defer.succeed({'xmlui': error_ui.toXml()})
error_ui.addText(
D_("Your XMPP account could not be deleted: %s")
% failure.getErrorMessage()
)
return defer.succeed({"xmlui": error_ui.toXml()})
d = self.host.plugins['XEP-0077'].unregister(client, jid.JID(client.jid.host))
d = self.host.plugins["XEP-0077"].unregister(client, jid.JID(client.jid.host))
d.addCallbacks(userDeleted, errback)
return d
@ -477,20 +621,60 @@ class MiscAccount(object):
"""
if posts:
if comments: # delete everything
form_ui = xml_tools.XMLUI("form", title=D_("Delete all your (micro-)blog posts and comments?"), submit_id=self.__delete_posts_comments_id)
form_ui.addText(D_("If you confirm this dialog, all the (micro-)blog data you submitted will be erased."))
form_ui.addText(D_("These are the public and private posts and comments you sent to any group."))
form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
form_ui = xml_tools.XMLUI(
"form",
title=D_("Delete all your (micro-)blog posts and comments?"),
submit_id=self.__delete_posts_comments_id,
)
form_ui.addText(
D_(
"If you confirm this dialog, all the (micro-)blog data you submitted will be erased."
)
)
form_ui.addText(
D_(
"These are the public and private posts and comments you sent to any group."
)
)
form_ui.addText(
D_(
"There is no other confirmation dialog, this is the very last one! Are you sure?"
)
)
else: # delete only the posts
form_ui = xml_tools.XMLUI("form", title=D_("Delete all your (micro-)blog posts?"), submit_id=self.__delete_posts_id)
form_ui.addText(D_("If you confirm this dialog, all the public and private posts you sent to any group will be erased."))
form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
form_ui = xml_tools.XMLUI(
"form",
title=D_("Delete all your (micro-)blog posts?"),
submit_id=self.__delete_posts_id,
)
form_ui.addText(
D_(
"If you confirm this dialog, all the public and private posts you sent to any group will be erased."
)
)
form_ui.addText(
D_(
"There is no other confirmation dialog, this is the very last one! Are you sure?"
)
)
elif comments: # delete only the comments
form_ui = xml_tools.XMLUI("form", title=D_("Delete all your (micro-)blog comments?"), submit_id=self.__delete_comments_id)
form_ui.addText(D_("If you confirm this dialog, all the public and private comments you made on other people's posts will be erased."))
form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
form_ui = xml_tools.XMLUI(
"form",
title=D_("Delete all your (micro-)blog comments?"),
submit_id=self.__delete_comments_id,
)
form_ui.addText(
D_(
"If you confirm this dialog, all the public and private comments you made on other people's posts will be erased."
)
)
form_ui.addText(
D_(
"There is no other confirmation dialog, this is the very last one! Are you sure?"
)
)
return {'xmlui': form_ui.toXml()}
return {"xmlui": form_ui.toXml()}
def __deleteBlogPostsCb(self, posts, comments, data, profile):
"""Actually delete the XMPP account and SàT profile
@ -500,26 +684,39 @@ class MiscAccount(object):
"""
if posts:
if comments:
target = D_('blog posts and comments')
d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogsAndComments(profile_key=profile)
target = D_("blog posts and comments")
d = self.host.plugins["GROUPBLOG"].deleteAllGroupBlogsAndComments(
profile_key=profile
)
else:
target = D_('blog posts')
d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogs(profile_key=profile)
target = D_("blog posts")
d = self.host.plugins["GROUPBLOG"].deleteAllGroupBlogs(
profile_key=profile
)
elif comments:
target = D_('comments')
d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogsComments(profile_key=profile)
target = D_("comments")
d = self.host.plugins["GROUPBLOG"].deleteAllGroupBlogsComments(
profile_key=profile
)
def deleted(result):
ui = xml_tools.XMLUI("popup", title=D_("Deletion confirmation"))
# TODO: change the message when delete/retract notifications are done with XEP-0060
ui.addText(D_("Your %(target)s have been deleted.") % {'target': target})
ui.addText(D_("Known issue of the demo version: you need to refresh the page to make the deleted posts actually disappear."))
return defer.succeed({'xmlui': ui.toXml()})
ui.addText(D_("Your %(target)s have been deleted.") % {"target": target})
ui.addText(
D_(
"Known issue of the demo version: you need to refresh the page to make the deleted posts actually disappear."
)
)
return defer.succeed({"xmlui": ui.toXml()})
def errback(failure):
error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
error_ui.addText(D_("Your %(target)s could not be deleted: %(message)s") % {'target': target, 'message': failure.getErrorMessage()})
return defer.succeed({'xmlui': error_ui.toXml()})
error_ui.addText(
D_("Your %(target)s could not be deleted: %(message)s")
% {"target": target, "message": failure.getErrorMessage()}
)
return defer.succeed({"xmlui": error_ui.toXml()})
d.addCallbacks(deleted, errback)
return d
@ -541,16 +738,21 @@ class MiscAccount(object):
raise exceptions.ConflictError
d = self.createProfile(password, jid_s, jid_s)
d.addCallback(lambda dummy: self.host.memory.getProfileName(jid_s)) # checks if the profile has been successfuly created
d.addCallback(
lambda dummy: self.host.memory.getProfileName(jid_s)
) # checks if the profile has been successfuly created
d.addCallback(self.host.connect, password, {}, 0)
def connected(result):
self.sendEmails(None, profile=jid_s)
return result
def removeProfile(failure): # profile has been successfully created but the XMPP credentials are wrong!
log.debug("Removing previously auto-created profile: %s" % failure.getErrorMessage())
def removeProfile(
failure
): # profile has been successfully created but the XMPP credentials are wrong!
log.debug(
"Removing previously auto-created profile: %s" % failure.getErrorMessage()
)
self.host.memory.asyncDeleteProfile(jid_s)
raise failure

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
import sys
@ -32,7 +33,9 @@ PLUGIN_INFO = {
C.PI_TYPE: C.PLUG_TYPE_MISC,
C.PI_MAIN: "AndroidPlugin",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: D_("""Manage Android platform specificities, like pause or notifications""")
C.PI_DESCRIPTION: D_(
"""Manage Android platform specificities, like pause or notifications"""
),
}
if sys.platform != "android":
@ -44,6 +47,7 @@ PARAM_VIBRATE_CATEGORY = "Notifications"
PARAM_VIBRATE_NAME = "vibrate"
PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
class AndroidPlugin(object):
params = """
@ -55,40 +59,41 @@ class AndroidPlugin(object):
</individual>
</params>
""".format(
category_name = PARAM_VIBRATE_CATEGORY,
category_label = D_(PARAM_VIBRATE_CATEGORY),
param_name = PARAM_VIBRATE_NAME,
param_label = PARAM_VIBRATE_LABEL,
)
category_name=PARAM_VIBRATE_CATEGORY,
category_label=D_(PARAM_VIBRATE_CATEGORY),
param_name=PARAM_VIBRATE_NAME,
param_label=PARAM_VIBRATE_LABEL,
)
def __init__(self, host):
log.info(_("plugin Android initialization"))
self.host = host
host.memory.updateParams(self.params)
self.cagou_status_fd = open('.cagou_status', 'rb')
self.cagou_status = mmap.mmap(self.cagou_status_fd.fileno(), 1, prot=mmap.PROT_READ)
self.cagou_status_fd = open(".cagou_status", "rb")
self.cagou_status = mmap.mmap(
self.cagou_status_fd.fileno(), 1, prot=mmap.PROT_READ
)
# we set a low priority because we want the notification to be sent after all plugins have done their job
host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=-1000)
@property
def cagou_active(self):
# 'R' status means Cagou is running in front
return self.cagou_status[0] == 'R'
#  'R' status means Cagou is running in front
return self.cagou_status[0] == "R"
def _notifyMessage(self, mess_data, client):
# send notification if there is a message and it is not a groupchat
if mess_data['message'] and mess_data['type'] != C.MESS_TYPE_GROUPCHAT:
message = mess_data['message'].itervalues().next()
if mess_data["message"] and mess_data["type"] != C.MESS_TYPE_GROUPCHAT:
message = mess_data["message"].itervalues().next()
try:
subject = mess_data['subject'].itervalues().next()
subject = mess_data["subject"].itervalues().next()
except StopIteration:
subject = u'Cagou new message'
subject = u"Cagou new message"
notification.notify(
title = subject,
message = message
)
if self.host.memory.getParamA(PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile):
notification.notify(title=subject, message=message)
if self.host.memory.getParamA(
PARAM_VIBRATE_NAME, PARAM_VIBRATE_CATEGORY, profile_key=client.profile
):
vibrator.vibrate()
return mess_data

View file

@ -19,6 +19,7 @@
from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core.constants import Const as C
import json
@ -31,17 +32,21 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "Debug",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Set of method to make development and debugging easier""")
C.PI_DESCRIPTION: _("""Set of method to make development and debugging easier"""),
}
class Debug(object):
def __init__(self, host):
log.info(_("Plugin Debug initialization"))
self.host = host
host.bridge.addMethod("debugFakeSignal", ".plugin", in_sign='sss', out_sign='', method=self._fakeSignal)
host.bridge.addMethod(
"debugFakeSignal",
".plugin",
in_sign="sss",
out_sign="",
method=self._fakeSignal,
)
def _fakeSignal(self, signal, arguments, profile_key):
"""send a signal from backend
@ -56,6 +61,3 @@ class Debug(object):
profile = self.host.memory.getProfileName(profile_key)
args.append(profile)
method(*args)

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.memory import params
from twisted.words.protocols.jabber import jid
@ -34,7 +35,7 @@ PLUGIN_INFO = {
C.PI_RECOMMENDATIONS: [],
C.PI_MAIN: "ExtraPEP",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Display messages from extra PEP services""")
C.PI_DESCRIPTION: _(u"""Display messages from extra PEP services"""),
}
@ -57,11 +58,11 @@ class ExtraPEP(object):
</individual>
</params>
""" % {
'category_name': PARAM_KEY,
'category_label': D_(PARAM_KEY),
'param_name': PARAM_NAME,
'param_label': D_(PARAM_LABEL),
'jids': u"\n".join({elt.toXml() for elt in params.createJidElts(PARAM_DEFAULT)})
"category_name": PARAM_KEY,
"category_label": D_(PARAM_KEY),
"param_name": PARAM_NAME,
"param_label": D_(PARAM_LABEL),
"jids": u"\n".join({elt.toXml() for elt in params.createJidElts(PARAM_DEFAULT)}),
}
def __init__(self, host):

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from sat.tools import xml_tools
@ -37,38 +38,67 @@ PLUGIN_INFO = {
C.PI_MODES: C.PLUG_MODE_BOTH,
C.PI_MAIN: "FilePlugin",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""File Tansfer Management:
This plugin manage the various ways of sending a file, and choose the best one.""")
C.PI_DESCRIPTION: _(
"""File Tansfer Management:
This plugin manage the various ways of sending a file, and choose the best one."""
),
}
SENDING = D_(u'Please select a file to send to {peer}')
SENDING_TITLE = D_(u'File sending')
CONFIRM = D_(u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?')
CONFIRM_TITLE = D_(u'Confirm file transfer')
CONFIRM_OVERWRITE = D_(u'File {} already exists, are you sure you want to overwrite ?')
CONFIRM_OVERWRITE_TITLE = D_(u'File exists')
SENDING = D_(u"Please select a file to send to {peer}")
SENDING_TITLE = D_(u"File sending")
CONFIRM = D_(
u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?'
)
CONFIRM_TITLE = D_(u"Confirm file transfer")
CONFIRM_OVERWRITE = D_(u"File {} already exists, are you sure you want to overwrite ?")
CONFIRM_OVERWRITE_TITLE = D_(u"File exists")
SECURITY_LIMIT = 30
PROGRESS_ID_KEY = 'progress_id'
PROGRESS_ID_KEY = "progress_id"
class FilePlugin(object):
File=stream.SatFile
File = stream.SatFile
def __init__(self, host):
log.info(_("plugin File initialization"))
self.host = host
host.bridge.addMethod("fileSend", ".plugin", in_sign='ssssa{ss}s', out_sign='a{ss}', method=self._fileSend, async=True)
host.bridge.addMethod(
"fileSend",
".plugin",
in_sign="ssssa{ss}s",
out_sign="a{ss}",
method=self._fileSend,
async=True,
)
self._file_callbacks = []
host.importMenu((D_("Action"), D_("send file")), self._fileSendMenu, security_limit=10, help_string=D_("Send a file"), type_=C.MENU_SINGLE)
host.importMenu(
(D_("Action"), D_("send file")),
self._fileSendMenu,
security_limit=10,
help_string=D_("Send a file"),
type_=C.MENU_SINGLE,
)
def _fileSend(self, peer_jid_s, filepath, name="", file_desc="", extra=None, profile=C.PROF_KEY_NONE):
def _fileSend(
self,
peer_jid_s,
filepath,
name="",
file_desc="",
extra=None,
profile=C.PROF_KEY_NONE,
):
client = self.host.getClient(profile)
return self.fileSend(client, jid.JID(peer_jid_s), filepath, name or None, file_desc or None, extra)
return self.fileSend(
client, jid.JID(peer_jid_s), filepath, name or None, file_desc or None, extra
)
@defer.inlineCallbacks
def fileSend(self, client, peer_jid, filepath, filename=None, file_desc=None, extra=None):
def fileSend(
self, client, peer_jid, filepath, filename=None, file_desc=None, extra=None
):
"""Send a file using best available method
@param peer_jid(jid.JID): jid of the destinee
@ -81,22 +111,34 @@ class FilePlugin(object):
if not os.path.isfile(filepath):
raise exceptions.DataError(u"The given path doesn't link to a file")
if not filename:
filename = os.path.basename(filepath) or '_'
filename = os.path.basename(filepath) or "_"
for namespace, callback, priority, method_name in self._file_callbacks:
has_feature = yield self.host.hasFeature(client, namespace, peer_jid)
if has_feature:
log.info(u"{name} method will be used to send the file".format(name=method_name))
progress_id = yield callback(client, peer_jid, filepath, filename, file_desc, extra)
defer.returnValue({'progress': progress_id})
log.info(
u"{name} method will be used to send the file".format(
name=method_name
)
)
progress_id = yield callback(
client, peer_jid, filepath, filename, file_desc, extra
)
defer.returnValue({"progress": progress_id})
msg = u"Can't find any method to send file to {jid}".format(jid=peer_jid.full())
log.warning(msg)
defer.returnValue({'xmlui': xml_tools.note(u"Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING).toXml()})
defer.returnValue(
{
"xmlui": xml_tools.note(
u"Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING
).toXml()
}
)
def _onFileChoosed(self, client, peer_jid, data):
cancelled = C.bool(data.get("cancelled", C.BOOL_FALSE))
if cancelled:
return
path=data['path']
path = data["path"]
return self.fileSend(client, peer_jid, path)
def _fileSendMenu(self, data, profile):
@ -105,20 +147,28 @@ class FilePlugin(object):
@param profile: %(doc_profile)s
"""
try:
jid_ = jid.JID(data['jid'])
jid_ = jid.JID(data["jid"])
except RuntimeError:
raise exceptions.DataError(_("Invalid JID"))
file_choosed_id = self.host.registerCallback(lambda data, profile: self._onFileChoosed(self.host.getClient(profile), jid_, data), with_data=True, one_shot=True)
file_choosed_id = self.host.registerCallback(
lambda data, profile: self._onFileChoosed(
self.host.getClient(profile), jid_, data
),
with_data=True,
one_shot=True,
)
xml_ui = xml_tools.XMLUI(
C.XMLUI_DIALOG,
dialog_opt = {
dialog_opt={
C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_FILE,
C.XMLUI_DATA_MESS: _(SENDING).format(peer=jid_.full())},
title = _(SENDING_TITLE),
submit_id = file_choosed_id)
C.XMLUI_DATA_MESS: _(SENDING).format(peer=jid_.full()),
},
title=_(SENDING_TITLE),
submit_id=file_choosed_id,
)
return {'xmlui': xml_ui.toXml()}
return {"xmlui": xml_ui.toXml()}
def register(self, namespace, callback, priority=0, method_name=None):
"""Register a fileSending method
@ -130,8 +180,12 @@ class FilePlugin(object):
"""
for data in self._file_callbacks:
if namespace == data[0]:
raise exceptions.ConflictError(u'A method with this namespace is already registered')
self._file_callbacks.append((namespace, callback, priority, method_name or namespace))
raise exceptions.ConflictError(
u"A method with this namespace is already registered"
)
self._file_callbacks.append(
(namespace, callback, priority, method_name or namespace)
)
self._file_callbacks.sort(key=lambda data: data[2], reverse=True)
def unregister(self, namespace):
@ -148,29 +202,31 @@ class FilePlugin(object):
"""create SatFile or FileStremaObject for the requested file and fill suitable data
"""
if stream_object:
assert 'stream_object' not in transfer_data
transfer_data['stream_object'] = stream.FileStreamObject(
assert "stream_object" not in transfer_data
transfer_data["stream_object"] = stream.FileStreamObject(
self.host,
client,
file_path,
mode='wb',
mode="wb",
uid=file_data[PROGRESS_ID_KEY],
size=file_data['size'],
data_cb = file_data.get('data_cb'),
)
size=file_data["size"],
data_cb=file_data.get("data_cb"),
)
else:
assert 'file_obj' not in transfer_data
transfer_data['file_obj'] = stream.SatFile(
assert "file_obj" not in transfer_data
transfer_data["file_obj"] = stream.SatFile(
self.host,
client,
file_path,
mode='wb',
mode="wb",
uid=file_data[PROGRESS_ID_KEY],
size=file_data['size'],
data_cb = file_data.get('data_cb'),
)
size=file_data["size"],
data_cb=file_data.get("data_cb"),
)
def _gotConfirmation(self, data, client, peer_jid, transfer_data, file_data, stream_object):
def _gotConfirmation(
self, data, client, peer_jid, transfer_data, file_data, stream_object
):
"""Called when the permission and dest path have been received
@param peer_jid(jid.JID): jid of the file sender
@ -181,17 +237,20 @@ class FilePlugin(object):
False if user wants to cancel
if file exists ask confirmation and call again self._getDestDir if needed
"""
if data.get('cancelled', False):
if data.get("cancelled", False):
return False
path = data['path']
file_data['file_path'] = file_path = os.path.join(path, file_data['name'])
log.debug(u'destination file path set to {}'.format(file_path))
path = data["path"]
file_data["file_path"] = file_path = os.path.join(path, file_data["name"])
log.debug(u"destination file path set to {}".format(file_path))
# we manage case where file already exists
if os.path.exists(file_path):
def check_overwrite(overwrite):
if overwrite:
self.openFileWrite(client, file_path, transfer_data, file_data, stream_object)
self.openFileWrite(
client, file_path, transfer_data, file_data, stream_object
)
return True
else:
return self.getDestDir(client, peer_jid, transfer_data, file_data)
@ -200,12 +259,14 @@ class FilePlugin(object):
self.host,
_(CONFIRM_OVERWRITE).format(file_path),
_(CONFIRM_OVERWRITE_TITLE),
action_extra={'meta_from_jid': peer_jid.full(),
'meta_type': C.META_TYPE_OVERWRITE,
'meta_progress_id': file_data[PROGRESS_ID_KEY]
},
action_extra={
"meta_from_jid": peer_jid.full(),
"meta_type": C.META_TYPE_OVERWRITE,
"meta_progress_id": file_data[PROGRESS_ID_KEY],
},
security_limit=SECURITY_LIMIT,
profile=client.profile)
profile=client.profile,
)
exists_d.addCallback(check_overwrite)
return exists_d
@ -239,24 +300,38 @@ class FilePlugin(object):
a stream.FileStreamObject will be used
return (defer.Deferred): True if transfer is accepted
"""
cont,ret_value = self.host.trigger.returnPoint("FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object)
cont, ret_value = self.host.trigger.returnPoint(
"FILE_getDestDir", client, peer_jid, transfer_data, file_data, stream_object
)
if not cont:
return ret_value
filename = file_data['name']
assert filename and not '/' in filename
filename = file_data["name"]
assert filename and not "/" in filename
assert PROGRESS_ID_KEY in file_data
# human readable size
file_data['size_human'] = u'{:.6n} Mio'.format(float(file_data['size'])/(1024**2))
d = xml_tools.deferDialog(self.host,
file_data["size_human"] = u"{:.6n} Mio".format(
float(file_data["size"]) / (1024 ** 2)
)
d = xml_tools.deferDialog(
self.host,
_(CONFIRM).format(peer=peer_jid.full(), **file_data),
_(CONFIRM_TITLE),
type_=C.XMLUI_DIALOG_FILE,
options={C.XMLUI_DATA_FILETYPE: C.XMLUI_DATA_FILETYPE_DIR},
action_extra={'meta_from_jid': peer_jid.full(),
'meta_type': C.META_TYPE_FILE,
'meta_progress_id': file_data[PROGRESS_ID_KEY]
},
action_extra={
"meta_from_jid": peer_jid.full(),
"meta_type": C.META_TYPE_FILE,
"meta_progress_id": file_data[PROGRESS_ID_KEY],
},
security_limit=SECURITY_LIMIT,
profile=client.profile)
d.addCallback(self._gotConfirmation, client, peer_jid, transfer_data, file_data, stream_object)
profile=client.profile,
)
d.addCallback(
self._gotConfirmation,
client,
peer_jid,
transfer_data,
file_data,
stream_object,
)
return d

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.internet import defer
from sat.core import exceptions
@ -32,10 +33,12 @@ try:
except ImportError:
from wokkel.subprotocols import XMPPHandler
NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
NS_GROUPBLOG = 'http://salut-a-toi.org/protocol/groupblog'
#NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features
NS_PUBSUB_EXP = NS_PUBSUB # XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
NS_PUBSUB = "http://jabber.org/protocol/pubsub"
NS_GROUPBLOG = "http://salut-a-toi.org/protocol/groupblog"
# NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features
NS_PUBSUB_EXP = (
NS_PUBSUB
) # XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
NS_PUBSUB_GROUPBLOG = NS_PUBSUB_EXP + "#groupblog"
NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config"
@ -48,7 +51,7 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0277"],
C.PI_MAIN: "GroupBlog",
C.PI_HANDLER: "yes",
C.PI_DESCRIPTION: _("""Implementation of microblogging fine permissions""")
C.PI_DESCRIPTION: _("""Implementation of microblogging fine permissions"""),
}
@ -74,7 +77,11 @@ class GroupBlog(object):
yield self.host.checkFeatures(client, (NS_PUBSUB_GROUPBLOG,))
except exceptions.FeatureNotFound:
client.server_groupblog_available = False
log.warning(_(u"Server is not able to manage item-access pubsub, we can't use group blog"))
log.warning(
_(
u"Server is not able to manage item-access pubsub, we can't use group blog"
)
)
else:
client.server_groupblog_available = True
log.info(_(u"Server can manage group blogs"))
@ -85,7 +92,7 @@ class GroupBlog(object):
except exceptions.ProfileNotSetError:
return {}
try:
return {'available': C.boolConst(client.server_groupblog_available)}
return {"available": C.boolConst(client.server_groupblog_available)}
except AttributeError:
if self.host.isConnected(profile):
log.debug("Profile is not connected, service is not checked yet")
@ -100,7 +107,11 @@ class GroupBlog(object):
return
access_model = config_form.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN)
if access_model == self._p.ACCESS_PUBLISHER_ROSTER:
data_format.iter2dict('group', config_form.fields[self._p.OPT_ROSTER_GROUPS_ALLOWED].values, microblog_data)
data_format.iter2dict(
"group",
config_form.fields[self._p.OPT_ROSTER_GROUPS_ALLOWED].values,
microblog_data,
)
def _data2entryTrigger(self, client, mb_data, entry_elt, item_elt):
"""Build fine access permission if needed
@ -108,14 +119,16 @@ class GroupBlog(object):
This trigger check if "group*" key are present,
and create a fine item config to restrict view to these groups
"""
groups = list(data_format.dict2iter('group', mb_data))
groups = list(data_format.dict2iter("group", mb_data))
if not groups:
return
if not client.server_groupblog_available:
raise exceptions.CancelError(u"GroupBlog is not available")
log.debug(u"This entry use group blog")
form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
access = data_form.Field(None, self._p.OPT_ACCESS_MODEL, value=self._p.ACCESS_PUBLISHER_ROSTER)
form = data_form.Form("submit", formNamespace=NS_PUBSUB_ITEM_CONFIG)
access = data_form.Field(
None, self._p.OPT_ACCESS_MODEL, value=self._p.ACCESS_PUBLISHER_ROSTER
)
allowed = data_form.Field(None, self._p.OPT_ROSTER_GROUPS_ALLOWED, values=groups)
form.addField(access)
form.addField(allowed)
@ -128,14 +141,16 @@ class GroupBlog(object):
"""
if "group" in mb_data:
options[self._p.OPT_ACCESS_MODEL] = self._p.ACCESS_PUBLISHER_ROSTER
options[self._p.OPT_ROSTER_GROUPS_ALLOWED] = list(data_format.dict2iter('group', mb_data))
options[self._p.OPT_ROSTER_GROUPS_ALLOWED] = list(
data_format.dict2iter("group", mb_data)
)
class GroupBlog_handler(XMPPHandler):
implements(iwokkel.IDisco)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
return [disco.DiscoFeature(NS_GROUPBLOG)]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
def getDiscoItems(self, requestor, target, nodeIdentifier=""):
return []

Some files were not shown because too many files have changed in this diff Show more