core, setup: huge cleaning:

- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention
- move twisted directory to root
- removed all hacks from setup.py, and added missing dependencies, it is now clean
- use https URL for website in setup.py
- removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed
- renamed sat.sh to sat and fixed its installation
- added python_requires to specify Python version needed
- replaced glib2reactor which use deprecated code by gtk3reactor

sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
This commit is contained in:
Goffi 2018-04-02 19:44:50 +02:00
commit 80ef13612b
88 changed files with 42 additions and 269 deletions

7
sat/plugins/__init__.py Normal file
View file

@ -0,0 +1,7 @@
# FIXME: remove this when RSM and MAM are in wokkel
# 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

@ -0,0 +1,222 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for adding D-Bus to Ad-Hoc Commands
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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/")
import os.path
import uuid
import dbus
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
FD_NAME = "org.freedesktop.DBus"
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'
PLUGIN_INFO = {
C.PI_NAME: "Ad-Hoc Commands - D-Bus",
C.PI_IMPORT_NAME: "AD_HOC_DBUS",
C.PI_TYPE: "Misc",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0050"],
C.PI_MAIN: "AdHocDBus",
C.PI_HANDLER: "no",
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)
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']
def _DBusAsyncCall(self, proxy, method, *args, **kwargs):
""" Call a DBus method asynchronously and return a deferred
@param proxy: DBus object proxy, as returner by get_object
@param method: name of the method to call
@param args: will be transmitted to the method
@param kwargs: will be transmetted to the method, except for the following poped values:
- interface: name of the interface to use
@return: a deferred
"""
d = defer.Deferred()
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
def _DBusListNames(self):
return self._DBusAsyncCall(self.fd_object, "ListNames")
def _DBusIntrospect(self, proxy):
return self._DBusAsyncCall(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE)
def _acceptMethod(self, method):
""" Return True if we accept the method for a command
@param method: etree.Element
@return: True if the method is acceptable
"""
if method.xpath("arg[@direction='in']"): # we don't accept method with argument for the moment
return False
return True
@defer.inlineCallbacks
def _introspect(self, methods, bus_name, proxy):
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)
yield self._introspect(methods, bus_name, new_proxy)
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)
continue
log.debug("introspecting interface [%s]" % name)
for method in node.iterchildren('method'):
if self._acceptMethod(method):
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)
@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):
bus_names = yield self._DBusListNames()
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(("", []))
bus_names.sort()
for bus_name in bus_names:
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)
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)
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):
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.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'))
options = []
for path, iface, command in methods:
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)
form.addField(field)
payload = form.toElement()
note = None
elif len(actions) == 2:
# we should have the answer here
try:
x_elt = command_elt.elements(data_form.NS_X_DATA,'x').next()
answer_form = data_form.Form.fromElement(x_elt)
command = answer_form['command']
except (KeyError, StopIteration):
raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD)
if command not in names_map:
raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD)
path, iface, command = names_map[command]
proxy = self.session_bus.get_object(bus_name, path)
self._DBusAsyncCall(proxy, command, interface=iface)
# job done, we can end the session, except if we have FLAG_LOOP
if FLAG_LOOP in flags:
# 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'))
status = self.XEP_0050.STATUS.COMPLETED
payload = None
note = (self.XEP_0050.NOTE.INFO, _(u"Command sent"))
else:
raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.INTERNAL)
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)

274
sat/plugins/plugin_blog_import.py Executable file
View file

@ -0,0 +1,274 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT plugin for import external blogs
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from twisted.words.xish import domish
from sat.core import exceptions
from sat.tools import xml_tools
import os
import os.path
import tempfile
import urlparse
import shortuuid
PLUGIN_INFO = {
C.PI_NAME: "blog import",
C.PI_IMPORT_NAME: "BLOG_IMPORT",
C.PI_TYPE: (C.PLUG_TYPE_BLOG, C.PLUG_TYPE_IMPORT),
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.""")
}
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}
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')
def importItem(self, client, item_import_data, session, options, return_data, service, node):
"""importItem specialized for blog import
@param item_import_data(dict):
* mandatory keys:
'blog' (dict): microblog data of the blog post (cf. http://wiki.goffi.org/wiki/Bridge_API_-_Microblogging/en)
the importer MUST NOT create node or call XEP-0277 plugin itself
'comments*' key MUST NOT be used in this microblog_data, see bellow for comments
It is recommanded to use a unique id in the "id" key which is constant per blog item,
so if the import fail, a new import will overwrite the failed items and avoid duplicates.
'comments' (list[list[dict]],None): Dictionaries must have the same keys as main item (i.e. 'blog' and 'comments')
a list of list is used because XEP-0277 can handler several comments nodes,
but in most cases, there will we only one item it the first list (something like [[{comment1_data},{comment2_data}, ...]])
blog['allow_comments'] must be True if there is any comment, and False (or not present) if comments are not allowed.
If allow_comments is False and some comments are present, an exceptions.DataError will be raised
* optional keys:
'url' (unicode): former url of the post (only the path, without host part)
if present the association to the new path will be displayed to user, so it can make redirections if necessary
@param options(dict, None): Below are the generic options,
blog importer can have specific ones. All options have unicode values
generic options:
- OPT_HOST (unicode): original host
- OPT_UPLOAD_IMAGES (bool): upload images to XMPP server if True
see OPT_UPLOAD_IGNORE_HOST.
Default: True
- OPT_UPLOAD_IGNORE_HOST (unicode): don't upload images from this host
- OPT_IGNORE_TLS (bool): ignore TLS error for image upload.
Default: False
@param return_data(dict): will contain link between former posts and new items
"""
mb_data = item_import_data['blog']
try:
item_id = mb_data['id']
except KeyError:
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']
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))
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:
raise NotImplementedError(u"can't manage multiple comment links")
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
recurse_kwargs = {
'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")
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','')))
return self._m.send(client, mb_data, service, node)
@defer.inlineCallbacks
def itemFilters(self, client, mb_data, session, options):
"""Apply filters according to options
modify mb_data in place
@param posts_data(list[dict]): data as returned by importer callback
@param options(dict): dict as given in [blogImport]
"""
# FIXME: blog filters don't work on text content
# TODO: text => XHTML conversion should handler links with <a/>
# filters can then be used by converting text to XHTML
if not options:
return
# we want only XHTML content
for prefix in ('content',): # a tuple is use, if title need to be added in the future
try:
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))
# 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)]
try:
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)]
else:
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)
except domish.ParserError:
# we clean the xml and try again our luck
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, '', '', ''))
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']):
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
# we now replace the content with filtered one
mb_data['content_xhtml'] = top_elt.toXml()
@defer.inlineCallbacks
def imgFilters(self, client, img_elt, options, opt_host, tmp_dir):
"""Filters handling images
url without host are fixed (if possible)
according to options, images are uploaded to XMPP server
@param img_elt(domish.Element): <img/> element to handle
@param options(dict): filters options
@param opt_host(unicode): normalised host given in options
@param tmp_dir(str): path to temp directory
"""
try:
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()))
return
else:
url = urlparse.urljoin(opt_host, url)
filename = url.rsplit('/',1)[-1].strip()
if not filename:
raise KeyError
except (KeyError, IndexError):
log.warning(u"ignoring invalid img element: {}".format(img_elt.toXml()))
return
# we change the url for the normalized one
img_elt['src'] = url
if options.get(OPT_UPLOAD_IMAGES, False):
# upload is requested
try:
ignore_host = options[OPT_UPLOAD_IGNORE_HOST]
except KeyError:
pass
else:
# 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))
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)}
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)
download_url = yield download_d
except Exception as e:
log.warning(u"can't download image at {url}: {reason}".format(url=url, reason=e))
else:
img_elt['src'] = download_url
try:
os.unlink(tmp_file)
except OSError:
pass

View file

@ -0,0 +1,392 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT plugin to import dokuwiki blogs
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from twisted.internet import threads
from collections import OrderedDict
import calendar
import urllib
import urlparse
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"')
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")
PLUGIN_INFO = {
C.PI_NAME: "Dokuwiki import",
C.PI_IMPORT_NAME: "IMPORT_DOKUWIKI",
C.PI_TYPE: C.PLUG_TYPE_BLOG,
C.PI_DEPENDENCIES: ["BLOG_IMPORT"],
C.PI_MAIN: "DokuwikiImport",
C.PI_HANDLER: "no",
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.
To use it, you need an admin access to a running Dokuwiki website
(local or on the Internet). The importer retrieves the data using
the XMLRPC Dokuwiki API.
You can specify a namespace (that could be a namespace directory
or a single post) or leave it empty to use the root namespace "/"
and import all the posts.
You can specify a new media repository to modify the internal
media links and make them point to the URL of your choice, but
note that the upload is not done automatically: a temporary
directory will be created on your local drive and you will
need to upload it yourself to your repository via SSH or FTP.
Following options are recognized:
location: DokuWiki site URL
user: DokuWiki admin user
passwd: DokuWiki admin password
namespace: DokuWiki namespace to import (default: root namespace "/")
media_repo: URL to the new remote media repository (default: none)
limit: maximal number of posts to import (default: 100)
Example of usage (with jp frontend):
jp import dokuwiki -p dave --pwd xxxxxx --connect
http://127.0.1.1 -o user souliane -o passwd qwertz
-o namespace public:2015:10
-o media_repo http://media.diekulturvermittlung.at
This retrieves the 100 last blog posts from http://127.0.1.1 that
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):
"""
@param url (unicode): DokuWiki site URL
@param user (unicode): DokuWiki admin user
@param passwd (unicode): DokuWiki admin password
@param media_repo (unicode): New remote media repository
"""
DokuWiki.__init__(self, url, user, passwd)
self.url = url
self.media_repo = media_repo
self.temp_dir = tempfile.mkdtemp() if self.media_repo else None
self.limit = limit
self.posts_data = OrderedDict()
def getPostId(self, post):
"""Return a unique and constant post id
@param post(dict): parsed post data
@return (unicode): post unique item id
"""
return unicode(post['id'])
def getPostUpdated(self, post):
"""Return the update date.
@param post(dict): parsed post data
@return (unicode): update date
"""
return unicode(post['mtime'])
def getPostPublished(self, post):
"""Try to parse the date from the message ID, else use "mtime".
The date can be extracted if the message ID looks like one of:
- namespace:YYMMDD_short_title
- namespace:YYYYMMDD_short_title
@param post (dict): parsed post data
@return (unicode): publication date
"""
id_, default = unicode(post["id"]), unicode(post["mtime"])
try:
date = id_.split(":")[-1].split("_")[0]
except KeyError:
return default
try:
time_struct = time.strptime(date, "%y%m%d")
except ValueError:
try:
time_struct = time.strptime(date, "%Y%m%d")
except ValueError:
return default
return unicode(calendar.timegm(time_struct))
def processPost(self, post, profile_jid):
"""Process a single page.
@param post (dict): parsed post data
@param profile_jid
"""
# get main information
id_ = self.getPostId(post)
updated = self.getPostUpdated(post)
published = self.getPostPublished(post)
# manage links
backlinks = self.pages.backlinks(id_)
for link in self.pages.links(id_):
if link["type"] != "extern":
assert link["type"] == "local"
page = link["page"]
backlinks.append(page[1:] if page.startswith(":") else page)
self.pages.get(id_)
content_xhtml = self.processContent(self.pages.html(id_), backlinks, profile_jid)
# XXX: title is already in content_xhtml and difficult to remove, so leave it
# 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",
}
# 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':[[]]}
def process(self, client, namespace=DEFAULT_NAMESPACE):
"""Process a namespace or a single page.
@param namespace (unicode): DokuWiki namespace (or page) to import
"""
profile_jid = client.jid
log.info("Importing data from DokuWiki %s" % self.version)
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.')
return
if not pages_list: # namespace is actually a page?
names = namespace.split(":")
real_namespace = ":".join(names[0:-1])
pages_list = self.pages.list(real_namespace)
pages_list = [page for page in pages_list if page["id"] == namespace]
namespace = real_namespace
count = 0
for page in pages_list:
self.processPost(page, profile_jid)
count += 1
if count >= self.limit :
break
return (self.posts_data.itervalues(), len(self.posts_data))
def processContent(self, text, backlinks, profile_jid):
"""Do text substitutions and file copy.
@param text (unicode): message content
@param backlinks (list[unicode]): list of backlinks
"""
text = text.strip(u"\ufeff") # this is at the beginning of the file (BOM)
for backlink in backlinks:
src = '/doku.php?id=%s"' % backlink
tgt = '/blog/%s/%s" target="#"' % (profile_jid.user, backlink)
text = text.replace(src, tgt)
subs = {}
link_pattern = r"""<(img|a)[^>]* (src|href)="([^"]+)"[^>]*>"""
for tag in re.finditer(link_pattern, text):
type_, attr, link = tag.group(1), tag.group(2), tag.group(3)
assert (type_ == "img" and attr == "src") or (type_ == "a" and attr == "href")
if re.match(r"^\w*://", link): # absolute URL to link directly
continue
if self.media_repo:
self.moveMedia(link, subs)
elif link not in subs:
subs[link] = urlparse.urljoin(self.url, link)
for url, new_url in subs.iteritems():
text = text.replace(url, new_url)
return text
def moveMedia(self, link, subs):
"""Move a media from the DokuWiki host to the new repository.
This also updates the hyperlinks to internal media files.
@param link (unicode): media link
@param subs (dict): substitutions data
"""
url = urlparse.urljoin(self.url, link)
user_media = re.match(r"(/lib/exe/\w+.php\?)(.*)", link)
thumb_width = None
if user_media: # media that has been added by the user
params = urlparse.parse_qs(urlparse.urlparse(url).query)
try:
media = params["media"][0]
except KeyError:
log.warning("No media found in fetch URL: %s" % user_media.group(2))
return
if re.match(r"^\w*://", media): # external URL to link directly
subs[link] = media
return
try: # create thumbnail
thumb_width = params["w"][0]
except KeyError:
pass
filename = media.replace(":", "/")
# XXX: avoid "precondition failed" error (only keep the media parameter)
url = urlparse.urljoin(self.url, "/lib/exe/fetch.php?media=%s" % media)
elif link.startswith("/lib/plugins/"):
# other link added by a plugin or something else
filename = link[13:]
else: # fake alert... there's no media (or we don't handle it yet)
return
filepath = os.path.join(self.temp_dir, filename)
self.downloadMedia(url, filepath)
if thumb_width:
filename = os.path.join("thumbs", thumb_width, filename)
thumbnail = os.path.join(self.temp_dir, filename)
self.createThumbnail(filepath, thumbnail, thumb_width)
new_url = os.path.join(self.media_repo, filename)
subs[link] = new_url
def downloadMedia(self, source, dest):
"""Copy media to localhost.
@param source (unicode): source url
@param dest (unicode): target path
"""
dirname = os.path.dirname(dest)
if not os.path.exists(dest):
if not os.path.exists(dirname):
os.makedirs(dirname)
urllib.urlretrieve(source, dest)
log.debug("DokuWiki media file copied to %s" % dest)
def createThumbnail(self, source, dest, width):
"""Create a thumbnail.
@param source (unicode): source file path
@param dest (unicode): destination file path
@param width (unicode): thumbnail's width
"""
thumb_dir = os.path.dirname(dest)
if not os.path.exists(thumb_dir):
os.makedirs(thumb_dir)
try:
im = Image.open(source)
im.thumbnail((width, int(width) * im.size[0] / im.size[1]))
im.save(dest)
log.debug("DokuWiki media thumbnail created: %s" % dest)
except IOError:
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)
def DkImport(self, client, location, options=None):
"""Import from DokuWiki to PubSub
@param location (unicode): DokuWiki site URL
@param options (dict, None): DokuWiki import parameters
- user (unicode): DokuWiki admin user
- passwd (unicode): DokuWiki admin password
- namespace (unicode): DokuWiki namespace to import
- media_repo (unicode): New remote media repository
"""
options[self._blog_import.OPT_HOST] = location
try:
user = options["user"]
except KeyError:
raise exceptions.DataError('parameter "user" is required')
try:
passwd = options["passwd"]
except KeyError:
raise exceptions.DataError('parameter "passwd" is required')
opt_upload_images = options.get(self._blog_import.OPT_UPLOAD_IMAGES, None)
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}")
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.")
else:
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"]
except KeyError:
namespace = DEFAULT_NAMESPACE
try:
limit = options["limit"]
except KeyError:
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)
d = threads.deferToThread(dk_importer.process, client, namespace)
return d

View file

@ -0,0 +1,251 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT plugin for import external blogs
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from twisted.internet import threads
from collections import OrderedDict
import itertools
import time
import cgi
import os.path
PLUGIN_INFO = {
C.PI_NAME: "Dotclear import",
C.PI_IMPORT_NAME: "IMPORT_DOTCLEAR",
C.PI_TYPE: C.PLUG_TYPE_BLOG,
C.PI_DEPENDENCIES: ["BLOG_IMPORT"],
C.PI_MAIN: "DotclearImport",
C.PI_HANDLER: "no",
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.
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.
Export only one blog if you have many, i.e. select "Download database of current blog"
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'\\',
}
class DotclearParser(object):
# XXX: we have to parse all file to build data
# this can be ressource intensive on huge blogs
def __init__(self):
self.posts_data = OrderedDict()
self.tags = {}
def getPostId(self, post):
"""Return a unique and constant post id
@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'])
def getCommentId(self, comment):
"""Return a unique and constant comment id
@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'])
def getTime(self, data, key):
"""Parse time as given by dotclear, with timezone handling
@param data(dict): dotclear data (post or comment)
@param key(unicode): key to get (e.g. "post_creadt")
@return (float): Unix time
"""
return time.mktime(time.strptime(data[key], "%Y-%m-%d %H:%M:%S"))
def readFields(self, fields_data):
buf = []
idx = 0
while True:
if fields_data[idx] != '"':
raise exceptions.ParsingError
while True:
idx += 1
try:
char = fields_data[idx]
except IndexError:
raise exceptions.ParsingError("Data was expected")
if char == '"':
# we have reached the end of this field,
# we try to parse a new one
yield u''.join(buf)
buf = []
idx += 1
try:
separator = fields_data[idx]
except IndexError:
return
if separator != u',':
raise exceptions.ParsingError("Field separator was expeceted")
idx += 1
break # we have a new field
elif char == u'\\':
idx += 1
try:
char = ESCAPE_MAP[fields_data[idx]]
except IndexError:
raise exceptions.ParsingError("Escaped char was expected")
except KeyError:
char = fields_data[idx]
log.warning(u"Unknown key to escape: {}".format(char))
buf.append(char)
def parseFields(self, headers, data):
return dict(itertools.izip(headers, self.readFields(data)))
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'])}
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'])
def metaFinishedHandler(self):
for post_id, tags in self.tags.iteritems():
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']:
# 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'))
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': [[]]})
def parse(self, db_path):
with open(db_path) as f:
signature = f.readline().decode('utf-8')
try:
version = signature.split('|')[1]
except IndexError:
version = None
log.debug(u"Dotclear version: {}".format(version))
data_type = None
data_headers = None
index = None
while True:
buf = f.readline().decode('utf-8')
if not buf:
break
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(',')
# we need to remove the ']' from the last header
last_header = data_headers[-1]
data_headers[-1] = last_header[:last_header.rfind(']')]
except IndexError:
log.warning(u"Can't read data)")
else:
if data_type is None:
continue
buf = buf.strip()
if not buf and data_type in KNOWN_DATA_TYPES:
try:
finished_handler = getattr(self, '{}FinishedHandler'.format(data_type))
except AttributeError:
pass
else:
finished_handler()
log.debug(u"{} data finished".format(data_type))
data_type = None
continue
assert data_type
try:
fields_handler = getattr(self, '{}Handler'.format(data_type))
except AttributeError:
pass
else:
fields_handler(data_headers, buf, index)
index += 1
return (self.posts_data.itervalues(), len(self.posts_data))
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)
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")
dc_parser = DotclearParser()
d = threads.deferToThread(dc_parser.parse, location)
return d

View file

@ -0,0 +1,382 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for parrot mode (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from sat.tools import stream
from twisted.internet import defer
from twisted.words.protocols.jabber import error
from wokkel import pubsub
from wokkel import generic
from functools import partial
import os
import os.path
import mimetypes
PLUGIN_INFO = {
C.PI_NAME: "File sharing component",
C.PI_IMPORT_NAME: "file_sharing",
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_RECOMMENDATIONS: [],
C.PI_MAIN: "FileSharing",
C.PI_HANDLER: C.BOOL_TRUE,
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/'
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']
host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
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)
self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
def getHandler(self, client):
return Comments_handler(self)
def profileConnected(self, client):
path = client.file_tmp_dir = os.path.join(
self.host.memory.getConfig('', 'local_dir'),
C.FILES_TMP_DIR,
regex.pathEscape(client.profile))
if not os.path.exists(path):
os.makedirs(path)
@defer.inlineCallbacks
def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path):
"""post file reception tasks
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']
extra = {}
if file_data[u'hash_algo'] == HASH_ALGO:
log.debug(_(u"Reusing already generated hash"))
file_hash = file_data[u'hash_hasher'].hexdigest()
else:
hasher = self._h.getHasher(HASH_ALGO)
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))
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))
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'):
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)
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})
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):
"""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 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'])
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)
return False, defer.succeed(True)
@defer.inlineCallbacks
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']
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'))
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')))
defer.returnValue(False)
if not found_files:
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_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']
# 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(
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):
if not client.is_component:
return True, None
else:
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')
except KeyError:
return
comment_elt = file_elt.addElement((NS_COMMENTS, 'comments'), content=comments_url)
try:
count = len(extra_args[u'extra'][u'comments'])
except KeyError:
count = 0
comment_elt['count'] = unicode(count)
return True
def _getFileComments(self, file_elt, file_data):
try:
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']
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'])
return True
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())
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'}
def _getFileId(self, nodeIdentifier):
if not nodeIdentifier.startswith(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')
return file_id
@defer.inlineCallbacks
def getFileData(self, requestor, nodeIdentifier):
file_id = self._getFileId(nodeIdentifier)
try:
files = yield self.host.memory.getFiles(self.parent, requestor, file_id)
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')
if not files:
raise error.StanzaError('item-not-found')
if len(files) > 1:
raise error.InternalError('there should be only one file')
defer.returnValue(files[0])
def commentsUpdate(self, extra, new_comments, peer_jid):
"""update comments (replace or insert new_comments)
@param extra(dict): extra data to update
@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}
updated = []
# we now check every current comment, to see if one id in new ones
# exist, in which case we must update
for idx, comment in enumerate(current_comments):
comment_id = comment[0]
if comment_id in new_comments_by_id:
# a new comment has an existing id, update is requested
if peer_jid and comment[1] != peer_jid:
# requestor has not the right to modify the comment
raise exceptions.PermissionError
# we replace old_comment with updated one
new_comment = new_comments_by_id[comment_id]
current_comments[idx] = new_comment
updated.append(new_comment)
# we now remove every updated comments, to only keep
# the ones to insert
for comment in updated:
new_comments.remove(comment)
current_comments.extend(new_comments)
def commentsDelete(self, extra, comments):
try:
comments_dict = extra['comments']
except KeyError:
return
for comment in comments:
try:
comments_dict.remove(comment)
except ValueError:
continue
def _getFrom(self, item_elt):
"""retrieve published of an item
@param item_elt(domish.element): <item> element
@return (unicode): full jid as string
"""
iq_elt = item_elt
while iq_elt.parent != None:
iq_elt = iq_elt.parent
return iq_elt['from']
@defer.inlineCallbacks
def publish(self, requestor, service, nodeIdentifier, items):
# 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']:
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)
except exceptions.PermissionError:
raise error.StanzaError('not-authorized')
@defer.inlineCallbacks
def items(self, requestor, service, nodeIdentifier, maxItems,
itemIdentifiers):
file_data = yield self.getFileData(requestor, nodeIdentifier)
comments = file_data['extra'].get('comments', [])
if 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']
try:
comments = file_data['extra']['comments']
except KeyError:
raise error.StanzaError('item-not-found')
to_remove = []
for comment in comments:
comment_id = comment[0]
if comment_id in itemIdentifiers:
to_remove.append(comment)
itemIdentifiers.remove(comment_id)
if not itemIdentifiers:
break
if itemIdentifiers:
# not all items have been to_remove, we can't continue
raise error.StanzaError('item-not-found')
if requestor.userhostJID() != file_data['owner']:
if not all([c[1] == requestor.userhost() for c in to_remove]):
raise error.StanzaError('not-authorized')
remove_cb = partial(self.commentsDelete, comments=to_remove)
yield self.host.memory.fileUpdate(file_id, 'extra', remove_cb)

View file

@ -0,0 +1,156 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin to export commands (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from sat.tools import trigger
from sat.tools.utils import clean_ustr
PLUGIN_INFO = {
C.PI_NAME: "Command export plugin",
C.PI_IMPORT_NAME: "EXP-COMMANS-EXPORT",
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "CommandExport",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Implementation of command export""")
}
class ExportCommandProtocol(protocol.ProcessProtocol):
""" Try to register an account with prosody """
def __init__(self, parent, client, target, options):
self.parent = parent
self.target = target
self.options = options
self.client = client
def _clean(self, data):
if not data:
log.error ("data should not be empty !")
return u""
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)
def errReceived(self, data):
self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True)
def processEnded(self, reason):
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'))
def boolOption(self, key):
""" Get boolean value from options
@param key: name of the option
@return: True if key exists and set to "true" (case insensitive),
False in all other cases """
value = self.options.get(key, "")
return value.lower() == "true"
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
def __init__(self, host):
log.info(_("Plugin command export initialization"))
self.host = host
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)
def removeProcess(self, entity, process):
""" Called when the process is finished
@param entity: jid.JID attached to the process
@param process: process to remove"""
try:
processes_set = self.spawned[(entity, process.client.profile)]
processes_set.discard(process)
if not processes_set:
del(self.spawned[(entity, process.client.profile)])
except ValueError:
pass
def MessageReceivedTrigger(self, client, message_elt, post_treat):
""" Check if source is linked and repeat message, else do nothing """
from_jid = jid.JID(message_elt["from"])
spawned_key = (from_jid.userhostJID(), client.profile)
if spawned_key in self.spawned:
try:
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'
processes_set = self.spawned[spawned_key]
_continue = False
exclusive = False
for process in processes_set:
process.write(mess_data)
_continue &= process.boolOption("continue")
exclusive |= process.boolOption("exclusive")
if exclusive:
raise trigger.SkipOtherTriggers
return _continue
return True
def _exportCommand(self, command, args, targets, options, profile_key):
""" Export a commands to authorised targets
@param command: full path of the command to execute
@param args: list of arguments, with command name as first one
@param targets: list of allowed entities
@param options: export options, a dict which can have the following keys ("true" to set booleans):
- exclusive: if set, skip all other triggers
- loop: if set, restart the command once terminated #TODO
- pty: if set, launch in a pseudo terminal
- continue: continue normal MessageReceived handling
"""
client = self.host.getClient(profile_key)
for target in targets:
try:
_jid = jid.JID(target)
if not _jid.user or not _jid.host:
raise jid.InvalidFormat
_jid = _jid.userhostJID()
except (RuntimeError, jid.InvalidFormat, AttributeError):
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'))

419
sat/plugins/plugin_exp_events.py Executable file
View file

@ -0,0 +1,419 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin to detect language (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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 uri_parse
from twisted.internet import defer
from twisted.words.protocols.jabber import jid, error
from twisted.words.xish import domish
from wokkel import pubsub
PLUGIN_INFO = {
C.PI_NAME: "Event plugin",
C.PI_IMPORT_NAME: "EVENTS",
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0060"],
C.PI_RECOMMENDATIONS: ["INVITATIONS", "XEP-0277"],
C.PI_MAIN: "Events",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Experimental implementation of XMPP events management""")
}
NS_EVENT = 'org.salut-a-toi.event:0'
class Events(object):
"""Q&D module to handle event attendance answer, experimentation only"""
def __init__(self, host):
log.info(_(u"Event plugin initialization"))
self.host = host
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("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='ssssassssssss', out_sign='',
method=self._invite,
async=True)
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)
return self.eventGet(client, service, node, id_)
@defer.inlineCallbacks
def eventGet(self, client, service, node, id_=NS_EVENT):
"""Retrieve event data
@param service(unicode, None): PubSub service
@param node(unicode): PubSub node of the event
@param id_(unicode): id_ with even data
@return (tuple[int, dict[unicode, unicode]): event data:
- timestamp of the event
- event metadata where key can be:
location: location of the event
image: URL of a picture to use to represent event
background-image: URL of a picture to use in background
"""
if not id_:
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'))
except IndexError:
raise exceptions.NotFound(_(u"No event with this id has been found"))
try:
timestamp = utils.date_parse(next(event_elt.elements(NS_EVENT, "date")))
except StopIteration:
timestamp = -1
data = {}
for key in (u'name',):
try:
data[key] = event_elt[key]
except KeyError:
continue
for elt_name in (u'description',):
try:
elt = next(event_elt.elements(NS_EVENT, elt_name))
except StopIteration:
continue
else:
data[elt_name] = unicode(elt)
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']
except StopIteration:
continue
except KeyError:
log.warning(_(u'no src found for image'))
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_parse.parseXMPPUri(uri)
if uri_data[u'type'] != u'pubsub':
raise ValueError
except StopIteration:
log.warning(_(u"no {uri_type} element found!").format(uri_type=uri_type))
except KeyError:
log.warning(_(u"incomplete {uri_type} element").format(uri_type=uri_type))
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']
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()))
continue
data[key] = unicode(meta_elt)
defer.returnValue((timestamp, data))
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 if node else NS_EVENT
client = self.host.getClient(profile_key)
return self.eventCreate(client, timestamp, data, service, node, id_ or NS_EVENT)
@defer.inlineCallbacks
def eventCreate(self, client, timestamp, data, service, node=None, item_id=NS_EVENT):
"""Create or replace an event
@param service(jid.JID, None): PubSub service
@param node(unicode, None): PubSub node of the event
None will create instant node.
@param item_id(unicode): ID of the item to create.
@param timestamp(timestamp, None)
@param data(dict[unicode, unicode]): data to update
dict will be cleared, do a copy if data are still needed
key can be:
- name: name of the event
- description: details
- image: main picture of the event
- background-image: image to use as background
@return (unicode): created node
"""
if not item_id:
raise ValueError(_(u"item_id must be set"))
if not service:
service = client.jid.userhostJID()
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)
for key in (u'name',):
if key in data:
event_elt[key] = data.pop(key)
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'):
if key in data:
elt = event_elt.addElement((NS_EVENT, 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
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})
uri_service = service
else:
uri = data.pop(key)
uri_data = uri_parse.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']
elt = event_elt.addElement((NS_EVENT, uri_type))
elt['uri'] = uri_parse.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
item_elt = pubsub.Item(id=item_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':
log.debug(_(u"requested node already exists"))
yield self._p.publish(client, service, node, items=[item_elt])
defer.returnValue(node)
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)
@defer.inlineCallbacks
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
creating or replacing it. Params are the same as for [eventCreate].
"""
event_timestamp, event_metadata = yield self.eventGet(client, service, node, id_)
new_timestamp = event_timestamp if timestamp_update is None else timestamp_update
new_data = event_metadata
if data_update:
for k, v in data_update.iteritems():
new_data[k] = v
yield self.eventCreate(client, new_timestamp, new_data, service, node, id_)
def _eventInviteeGet(self, service, node, profile_key):
service = jid.JID(service) if service else None
node = node if node else NS_EVENT
client = self.host.getClient(profile_key)
return self.eventInviteeGet(client, service, node)
@defer.inlineCallbacks
def eventInviteeGet(self, client, service, node):
"""Retrieve attendance from event node
@param service(unicode, None): PubSub service
@param node(unicode): PubSub node of the event
@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()])
try:
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'):
try:
data[key] = event_elt[key]
except KeyError:
continue
defer.returnValue(data)
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)
return self.eventInviteeSet(client, service, node, event_data)
def eventInviteeSet(self, client, service, node, data):
"""Set or update attendance data in event node
@param service(unicode, None): PubSub service
@param node(unicode): PubSub node of the event
@param data(dict[unicode, unicode]): data to update
key can be:
attend: one of "yes", "no", "maybe"
guests: an int
"""
event_elt = domish.Element((NS_EVENT, 'invitee'))
for key in (u'attend', u'guests'):
try:
event_elt[key] = data.pop(key)
except KeyError:
pass
item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt)
return self._p.publish(client, service, node, items=[item_elt])
def _eventInviteesList(self, service, node, profile_key):
service = jid.JID(service) if service else None
node = node if node else NS_EVENT
client = self.host.getClient(profile_key)
return self.eventInviteesList(client, service, node)
@defer.inlineCallbacks
def eventInviteesList(self, client, service, node):
"""Retrieve attendance from event node
@param service(unicode, None): PubSub service
@param node(unicode): PubSub node of the event
@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)
invitees = {}
for item in items:
try:
event_elt = next(item.elements(NS_EVENT, u'invitee'))
except IndexError:
# 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
)))
data = {}
for key in (u'attend', u'guests'):
try:
data[key] = event_elt[key]
except KeyError:
continue
invitees[item['id']] = data
defer.returnValue(invitees)
def _invite(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"):
value = locals()[key]
kwargs[key] = unicode(value)
return self.invite(client,
jid.JID(service) if service else None,
node,
id_ or NS_EVENT,
**kwargs)
@defer.inlineCallbacks
def invite(self, client, service, node, id_=NS_EVENT, **kwargs):
"""High level method to create an email invitation to an event
@param service(unicode, None): PubSub service
@param node(unicode): PubSub node of the event
@param id_(unicode): id_ with even data
"""
if self._i is None:
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'))
event_service = (service or client.jid.userhostJID())
event_uri = uri_parse.buildXMPPUri('pubsub',
path=event_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'))
yield self._p.setNodeAffiliations(client, event_service, node, {invitee_jid: u'member'})
log.debug(_(u'affiliation set on event node'))
dummy, event_data = yield self.eventGet(client, service, node, 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'})
# FIXME: what follow is crazy, we have no good way to handle comments affiliations for blog
blog_items, dummy = yield self._b.mbGet(client, blog_service, blog_node, None)
for item in blog_items:
comments_service = jid.JID(item['comments_service'])
comments_node = item['comments_node']
yield self._p.setNodeAffiliations(client, comments_service, comments_node, {invitee_jid: u'publisher'})
log.debug(_(u'affiliation set on blog and comments nodes'))

View file

@ -0,0 +1,281 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for managing pipes (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from twisted.words.xish import domish
from twisted.words.protocols.jabber import jid
from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet import endpoints
from twisted.internet import reactor
from twisted.internet import error
from twisted.internet import interfaces
from zope import interface
import errno
NS_STREAM = 'http://salut-a-toi.org/protocol/stream'
SECURITY_LIMIT=30
START_PORT = 8888
PLUGIN_INFO = {
C.PI_NAME: "Jingle Stream Plugin",
C.PI_IMPORT_NAME: "STREAM",
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0166"],
C.PI_MAIN: "JingleStream",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Jingle Stream plugin""")
}
CONFIRM = D_(u"{peer} wants to send you a stream, do you accept ?")
CONFIRM_TITLE = D_(u"Stream Request")
class StreamProtocol(protocol.Protocol):
def __init__(self):
self.pause = False
def setPause(self, paused):
# in Python 2.x, Twisted classes are old style
# so we can use property and setter
if paused:
if not self.pause:
self.transport.pauseProducing()
self.pause = True
else:
if self.pause:
self.transport.resumeProducing()
self.pause = False
def disconnect(self):
self.transport.loseConnection()
def connectionMade(self):
if self.factory.client_conn is not None:
self.transport.loseConnection()
self.factory.setClientConn(self)
def dataReceived(self, data):
self.factory.writeToConsumer(data)
def sendData(self, data):
self.transport.write(data)
def connectionLost(self, reason):
if self.factory.client_conn != self:
# only the first connected client_conn is relevant
return
if reason.type == error.ConnectionDone:
self.factory.streamFinished()
else:
self.factory.streamFailed(reason)
@interface.implementer(stream.IStreamProducer)
@interface.implementer(interfaces.IPushProducer)
@interface.implementer(interfaces.IConsumer)
class StreamFactory(protocol.Factory):
protocol = StreamProtocol
consumer = None
producer = None
deferred = None
def __init__(self):
self.client_conn = None
def setClientConn(self, stream_protocol):
# in Python 2.x, Twisted classes are old style
# so we can use property and setter
assert self.client_conn is None
self.client_conn = stream_protocol
if self.consumer is None:
self.client_conn.setPause(True)
def startStream(self, consumer):
if self.consumer is not None:
raise exceptions.InternalError(_(u"stream can't be used with multiple consumers"))
assert self.deferred is None
self.consumer = consumer
consumer.registerProducer(self, True)
self.deferred = defer.Deferred()
if self.client_conn is not None:
self.client_conn.setPause(False)
return self.deferred
def streamFinished(self):
self.client_conn = None
if self.consumer:
self.consumer.unregisterProducer()
self.port_listening.stopListening()
self.deferred.callback(None)
def streamFailed(self, failure_):
self.client_conn = None
if self.consumer:
self.consumer.unregisterProducer()
self.port_listening.stopListening()
self.deferred.errback(failure_)
elif self.producer:
self.producer.stopProducing()
def stopStream(self):
if self.client_conn is not None:
self.client_conn.disconnect()
def registerProducer(self, producer, streaming):
self.producer = producer
def pauseProducing(self):
self.client_conn.setPause(True)
def resumeProducing(self):
self.client_conn.setPause(False)
def stopProducing(self):
if self.client_conn:
self.client_conn.disconnect()
def write(self, data):
try:
self.client_conn.sendData(data)
except AttributeError:
log.warning(_(u"No client connected, can't send data"))
def writeToConsumer(self, data):
self.consumer.write(data)
class JingleStream(object):
"""This non standard jingle application send byte stream"""
def __init__(self, host):
log.info(_("Plugin Stream initialization"))
self.host = host
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)
# jingle callbacks
def _streamOut(self, to_jid_s, profile_key):
client = self.host.getClient(profile_key)
return self.streamOut(client, jid.JID(to_jid_s))
@defer.inlineCallbacks
def streamOut(self, client, to_jid):
"""send a stream
@param peer_jid(jid.JID): recipient
@return: an unique id to identify the transfer
"""
port = START_PORT
factory = StreamFactory()
while True:
endpoint = endpoints.TCP4ServerEndpoint(reactor, port)
try:
port_listening = yield endpoint.listen(factory)
except error.CannotListenError as e:
if e.socketError.errno == errno.EADDRINUSE:
port += 1
else:
raise e
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},
}])
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'))
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):
log.warning(u"Bad sender, assuming initiator")
content_data['senders'] = self._j.ROLE_INITIATOR
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",
},
security_limit=SECURITY_LIMIT,
profile=client.profile)
if not C.bool(confirm_data['answer']):
defer.returnValue(False)
try:
port = int(confirm_data['port'])
except (ValueError, KeyError):
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()
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']
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()
args = [client, session, content_name, content_data]
finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
else:
log.warning(u"FIXME: unmanaged action {}".format(action))
return desc_elt
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()
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()

View file

@ -0,0 +1,91 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin to detect language (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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")')
identifier = LanguageIdentifier.from_modelstring(model, norm_probs=False)
PLUGIN_INFO = {
C.PI_NAME: "Language detection plugin",
C.PI_IMPORT_NAME: "EXP-LANG-DETECT",
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "LangDetect",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Detect and set message language when unknown""")
}
CATEGORY = D_(u"Misc")
NAME = u"lang_detect"
LABEL = D_(u"language detection")
PARAMS = """
<params>
<individual>
<category name="{category_name}">
<param name="{name}" label="{label}" type="bool" value="true" />
</category>
</individual>
</params>
""".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
host.memory.updateParams(PARAMS)
host.trigger.add("MessageReceived", self.MessageReceivedTrigger)
host.trigger.add("sendMessage", self.MessageSendTrigger)
def addLanguage(self, mess_data):
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}
return mess_data
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)
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)
if lang_detect:
self.addLanguage(data)
return True

176
sat/plugins/plugin_exp_parrot.py Executable file
View file

@ -0,0 +1,176 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for parrot mode (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
PLUGIN_INFO = {
C.PI_NAME: "Parrot Plugin",
C.PI_IMPORT_NAME: "EXP-PARROT",
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0045"],
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)""")
}
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
# /unparrot command in text commands plugin.
# FIXME: potentially unsecure, specially with e2e encryption
def __init__(self, host):
log.info(_("Plugin Parrot initialization"))
self.host = host
host.trigger.add("MessageReceived", self.MessageReceivedTrigger, 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):
# """ Deactivate other triggers if recipient is in parrot links """
# try:
# _links = client.parrot_links
# except AttributeError:
# return True
#
# if mess_data['to'].userhostJID() in _links.values():
# log.debug("Parrot link detected, skipping other triggers")
# raise trigger.SkipOtherTriggers
def MessageReceivedTrigger(self, client, message_elt, post_treat):
""" Check if source is linked and repeat message, else do nothing """
# TODO: many things are not repeated (subject, thread, etc)
profile = client.profile
client = self.host.getClient(profile)
from_jid = message_elt["from"]
try:
_links = client.parrot_links
except AttributeError:
return True
if not from_jid.userhostJID() in _links:
return True
message = {}
for e in message_elt.elements(C.NS_CLIENT, 'body'):
body = unicode(e)
lang = e.getAttribute('lang') or ''
try:
entity_type = self.host.memory.getEntityData(from_jid, ['type'], profile)["type"]
except (UnknownEntityError, KeyError):
entity_type = "contact"
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
return True
else:
src_txt = from_jid.user
message[lang] = u"[{}] {}".format(src_txt, body)
linked = _links[from_jid.userhostJID()]
client.sendMessage(jid.JID(unicode(linked)), message, None, "auto", no_trigger=True)
return True
def addParrot(self, client, source_jid, dest_jid):
"""Add a parrot link from one entity to another one
@param source_jid: entity from who messages will be repeated
@param dest_jid: entity where the messages will be repeated
"""
try:
_links = client.parrot_links
except AttributeError:
_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)))
def removeParrot(self, client, source_jid):
"""Remove parrot link
@param source_jid: this entity will no more be repeated
"""
try:
del client.parrot_links[source_jid.userhostJID()]
except (AttributeError, KeyError):
pass
def cmd_parrot(self, client, mess_data):
"""activate Parrot mode between 2 entities, in both directions."""
log.debug("Catched parrot command")
txt_cmd = self.host.plugins[C.TEXT_CMDS]
try:
link_left_jid = jid.JID(mess_data["unparsed"].strip())
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)
return False
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)
return False
def cmd_unparrot(self, client, mess_data):
"""remove Parrot mode between 2 entities, in both directions."""
log.debug("Catched unparrot command")
txt_cmd = self.host.plugins[C.TEXT_CMDS]
try:
link_left_jid = jid.JID(mess_data["unparsed"].strip())
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)
return False
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)
return False

View file

@ -0,0 +1,249 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for Pubsub Hooks
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core import exceptions
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'
PLUGIN_INFO = {
C.PI_NAME: "PubSub Hook",
C.PI_IMPORT_NAME: NS_PUBSUB_HOOK,
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
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""")
}
# python module
HOOK_TYPE_PYTHON = u'python'
# python file path
HOOK_TYPE_PYTHON_FILE = u'python_file'
# python code directly
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
)
@defer.inlineCallbacks
def profileConnected(self, client):
hooks = client._hooks = persistent.PersistentBinaryDict(NS_PUBSUB_HOOK, client.profile)
client._hooks_temporary = {}
yield hooks.load()
for node in hooks:
self._installNodeManager(client, node)
def profileDisconnected(self, client):
for node in client._hooks:
self._removeNodeManager(client, node)
def _installNodeManager(self, client, node):
if node in self.node_hooks:
log.debug(_(u"node manager already set for {node}").format(node=node))
self.node_hooks[node] += 1
else:
# first hook on this node
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))
def _removeNodeManager(self, client, node):
try:
self.node_hooks[node] -= 1
except KeyError:
log.error(_(u"trying to remove a {node} without hook").format(node=node))
else:
if self.node_hooks[node] == 0:
del self.node_hooks[node]
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))
if hook_type != HOOK_TYPE_PYTHON_FILE:
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
}
if persistent:
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.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))
def _itemsReceived(self, client, itemsEvent):
node = itemsEvent.nodeIdentifier
for hooks in (client._hooks, client._hooks_temporary):
if node not in hooks:
continue
hooks_list = hooks[node]
for hook_data in hooks_list[:]:
if hook_data['service'] != itemsEvent.sender.userhostJID():
continue
try:
callback = hook_data['callback']
except KeyError:
# first time we get this hook, we create the callback
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']
else:
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))
hooks_list.remove(hook_data)
continue
for item in itemsEvent.items:
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))
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)
def addHook(self, client, service, node, hook_type, hook_arg, persistent):
r"""Add a hook which will be triggered on a pubsub notification
@param service(jid.JID): service of the node
@param node(unicode): Pubsub node
@param hook_type(unicode): type of the hook, one of:
- HOOK_TYPE_PYTHON: a python module (must be in path)
module must have a "hook" method which will be called
- HOOK_TYPE_PYTHON_FILE: a python file
file must have a "hook" method which will be called
- HOOK_TYPE_PYTHON_CODE: direct python code
/!\ Python hooks will be executed in SàT context,
with host, client and item as arguments, it means that:
- they can do whatever they wants, so don't run untrusted hooks
- they MUST NOT BLOCK, they are run in Twisted async environment and blocking would block whole SàT process
- item are domish.Element
@param hook_arg(unicode): argument of the hook, depending on the hook_type
can be a module path, file path, python code
"""
assert service is not None
return self.installHook(client, service, node, hook_type, hook_arg, persistent)
def _removeHook(self, service, node, hook_type, hook_arg, profile):
client = self.host.getClient(profile)
service = jid.JID(service) if service else client.jid.userhostJID()
return self.removeHook(client, service, node, hook_type or None, hook_arg or None)
def removeHook(self, client, service, node, hook_type=None, hook_arg=None):
"""Remove a persistent or temporaty root
@param service(jid.JID): service of the node
@param node(unicode): Pubsub node
@param hook_type(unicode, None): same as for [addHook]
match all if None
@param hook_arg(unicode, None): same as for [addHook]
match all if None
@return(int): number of hooks removed
"""
removed = 0
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']):
continue
hooks[node].remove(hook_data)
removed += 1
if not hooks[node]:
# no more hooks, we can remove the node
del hooks[node]
self._removeNodeManager(client, node)
else:
if hooks == client._hooks:
hooks.force(node)
return removed
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'])
return hooks_list
def listHooks(self, client):
"""return list of registered hooks"""
hooks_list = []
for hooks in (client._hooks, client._hooks_temporary):
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
})
return hooks_list

View file

@ -0,0 +1,493 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for Pubsub Schemas
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _
from sat.core import exceptions
from sat.core.constants import Const as C
from sat.tools import xml_tools
from sat.tools import utils
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
from wokkel import generic
from zope.interface import implements
from collections import Iterable
import copy
import itertools
NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0'
PLUGIN_INFO = {
C.PI_NAME: "PubSub Schema",
C.PI_IMPORT_NAME: "PUBSUB_SCHEMA",
C.PI_TYPE: "EXP",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"],
C.PI_MAIN: "PubsubSchema",
C.PI_HANDLER: "yes",
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)
def getHandler(self, client):
return SchemaHandler()
def _getSchemaBridgeCb(self, schema_elt):
if schema_elt is None:
return u''
return schema_elt.toXml()
def _getSchema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
d = self.getSchema(client, service, nodeIdentifier)
d.addCallback(self._getSchemaBridgeCb)
return d
def _getSchemaCb(self, iq_elt):
try:
schema_elt = next(iq_elt.elements(NS_SCHEMA, 'schema'))
except StopIteration:
raise exceptions.DataError('missing <schema> element')
try:
x_elt = next(schema_elt.elements((data_form.NS_X_DATA, 'x')))
except StopIteration:
# there is not schema on this node
return None
return x_elt
def getSchema(self, client, service, nodeIdentifier):
"""retrieve PubSub node schema
@param service(jid.JID, None): jid of PubSub service
None to use our PEP
@param nodeIdentifier(unicode): node to get schema from
@return (domish.Element, None): schema (<x> element)
None if not schema has been set on this node
"""
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
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):
"""get data form from node's schema
@param service(None, jid.JID): PubSub service
@param nodeIdentifier(unicode): node
@param schema(domish.Element, data_form.Form, None): node schema
if domish.Element, will be converted to data form
if data_form.Form it will be returned without modification
if None, it will be retrieved from node (imply one additional XMPP request)
@param form_type(unicode): type of the form
@param copy_form(bool): if True and if schema is already a data_form.Form, will deep copy it before returning
needed when the form is reused and it will be modified (e.g. in sendDataFormItem)
@return(data_form.Form): data form
the form should not be modified if copy_form is not set
"""
if schema is None:
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"))
elif isinstance(schema, data_form.Form):
if copy_form:
schema = copy.deepcopy(schema)
defer.returnValue(schema)
try:
form = data_form.Form.fromElement(schema)
except data_form.Error as 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, '')
return xmlui
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"))
nodeIdentifier = default_node
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
d = self.getUISchema(client, service, nodeIdentifier)
d.addCallback(lambda xmlui: xmlui.toXml())
return d
def getUISchema(self, client, service, nodeIdentifier):
d = self.getSchema(client, service, nodeIdentifier)
d.addCallback(self.schema2XMLUI)
return d
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'))
return self.setSchema(client, service, nodeIdentifier, schema)
def setSchema(self, client, service, nodeIdentifier, schema):
"""set or replace PubSub node schema
@param schema(domish.Element, None): schema to set
None if schema need to be removed
"""
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
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):
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'))
if schema:
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.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):
"""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
if None, it will be retrieved from node
@param default_node(unicode): node to use if nodeIdentifier is None or empty
@param form_ns (unicode, None): namespace of the form
None to accept everything, even if form has no namespace
@param filters(dict, None): same as for xml_tools.dataFormResult2XMLUI
other parameters as the same as for [getItems]
@return (list[unicode]): XMLUI of the forms
if an item is invalid (not corresponding to form_ns or not a data_form)
it will be skipped
@raise ValueError: one argument is invalid
"""
if not nodeIdentifier:
if not default_node:
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)
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')):
form = data_form.Form.fromElement(x_elt)
if form_ns and form.formNamespace != form_ns:
continue
xmlui = xml_tools.dataFormResult2XMLUI(
form,
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,
)
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):
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
if schema:
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'')
return d
@defer.inlineCallbacks
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
if not iterable, will be put in a list
@param schema(domish.Element, data_form.Form, None): data schema
None to retrieve data schema from node (need to do a additional XMPP call)
Schema is needed to construct data form to publish
@param deserialise(bool): if True, data are list of unicode and must be deserialized according to expected type
This is done in this method and not directly in _sendDataFormItem because we need to know the data type
which is in the form, not availablable in _sendDataFormItem
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')
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))
continue
if isinstance(values_list, basestring) or not isinstance(values_list, Iterable):
values_list = [values_list]
if deserialise:
if field.fieldType == 'boolean':
values_list = [C.bool(v) for v in values_list]
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]))
elif 'jid' in field.fieldType:
values_list = [jid.JID(v) for v in values_list]
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]
if not values_list:
# if values don't map to allowed values, we use default ones
values_list = field.values
field.values = values_list
yield self._p.sendItem(client, service, nodeIdentifier, form.toElement(), item_id, extra)
## filters ##
# filters useful for data form to XMLUI conversion #
def valueOrPublisherFilter(self, form_xmlui, widget_type, args, kwargs):
"""Replace missing value by publisher's user part"""
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)
except (KeyError, RuntimeError):
pass
else:
args[0] = publisher.user.capitalize()
return widget_type, args, kwargs
def textbox2ListFilter(self, form_xmlui, widget_type, args, kwargs):
"""Split lines of a textbox in a list
main use case is using a textbox for labels
"""
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')}
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]:
return widget_type, args, kwargs
# we convert XMPP date to timestamp
try:
args[0] = unicode(utils.date_parse(args[0]))
except Exception as e:
log.warning(_(u"Can't parse date field: {msg}").format(msg=e))
return widget_type, args, kwargs
## Helper methods ##
def prepareBridgeGet(self, service, node, max_items, sub_id, extra_dict, profile_key):
"""Parse arguments received from bridge *Get methods and return higher level data
@return (tuple): (client, service, node, max_items, extra, sub_id) usable for internal methods
"""
client = self.host.getClient(profile_key)
service = jid.JID(service) if service else None
if not node:
node = None
max_items = None if max_items == C.NO_LIMIT else max_items
if not sub_id:
sub_id = None
extra = self._p.parseExtra(extra_dict)
return client, service, node, max_items, extra, sub_id
def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra_dict=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
when adding *Get methods
"""
client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(service, node, max_items, sub_id, extra_dict, profile_key)
d = self.getDataFormItems(client, service, node or None,
max_items=max_items,
item_ids=item_ids,
sub_id=sub_id,
rsm_request=extra.rsm_request,
extra=extra.extra,
default_node=default_node,
form_ns=form_ns,
filters=filters)
d.addCallback(self._p.serItemsData)
return d
def prepareBridgeSet(self, service, node, schema, item_id, extra, profile_key):
"""Parse arguments received from bridge *Set methods and return higher level data
@return (tuple): (client, service, node, schema, item_id, extra) usable for internal methods
"""
client = self.host.getClient(profile_key)
service = None if not service else jid.JID(service)
if schema:
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'])
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):
"""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'')
return d
@defer.inlineCallbacks
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
@param values(dict[key(unicode), [iterable[object]|object]]): values of the items
if value is not iterable, it will be put in a list
'created' and 'updated' will be forced to current time:
- 'created' is set if item_id is None, i.e. if it's a new ticket
- 'updated' is set everytime
@param extra(dict, None): same as for [XEP-0060.sendItem] with additional keys:
- update(bool): if True, get previous item data to merge with current one
if True, item_id must be None
@param form_ns (unicode, None): namespace of the form
needed when an update is done
@param default_node(unicode, None): value to use if node is not set
other arguments are same as for [self._s.sendDataFormItem]
@return (unicode): id of the created item
"""
if not node:
if default_node is None:
raise ValueError(_(u"default_node must be set if node is not set"))
node = default_node
now = utils.xmpp_date()
if not item_id:
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'))
try:
# we get previous item
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))
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))
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['updated'] = now
if fill_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)
defer.returnValue(item_id)
class SchemaHandler(XMPPHandler):
implements(iwokkel.IDisco)
def getDiscoInfo(self, requestor, service, nodeIdentifier=''):
return [disco.DiscoFeature(NS_SCHEMA)]
def getDiscoItems(self, requestor, service, nodeIdentifier=''):
return []

248
sat/plugins/plugin_import.py Executable file
View file

@ -0,0 +1,248 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT plugin for generic data import handling
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from twisted.words.protocols.jabber import jid
from functools import partial
import collections
import uuid
import json
PLUGIN_INFO = {
C.PI_NAME: "import",
C.PI_IMPORT_NAME: "IMPORT",
C.PI_TYPE: C.PLUG_TYPE_IMPORT,
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "ImportPlugin",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Generic import plugin, base for specialized importers""")
}
Importer = collections.namedtuple('Importer', ('callback', 'short_desc', 'long_desc'))
class ImportPlugin(object):
def __init__(self, host):
log.info(_("plugin Import initialization"))
self.host = host
def initialize(self, import_handler, name):
"""Initialize a specialized import handler
@param import_handler(object): specialized import handler instance
must have the following methods:
- importItem: import a single main item (i.e. prepare data for publishing)
- importSubitems: import sub items (i.e. items linked to main item, e.g. comments).
Must return a dict with kwargs for recursiveImport if items are to be imported recursively.
At least "items_import_data", "service" and "node" keys must be provided.
if None is returned, no recursion will be done to import subitems, but import can still be done directly by the method.
- publishItem: actualy publish an item
- itemFilters: modify item according to options
@param name(unicode): import handler name
"""
assert name == name.lower().strip()
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)
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)
def getProgress(self, import_handler, progress_id, profile):
client = self.host.getClient(profile)
return client._import[import_handler.name][progress_id]
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]
def getDescription(self, import_handler, name):
"""Return import short and long descriptions
@param name(unicode): importer name
@return (tuple[unicode,unicode]): short and long description
"""
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))
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):
client = self.host.getClient(profile)
options = {key: unicode(value) for key, value in options.iteritems()}
for option in import_handler.BOOL_OPTIONS:
try:
options[option] = C.bool(options[option])
except KeyError:
pass
for option in import_handler.JSON_OPTIONS:
try:
options[option] = json.loads(options[option])
except ValueError:
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)
@defer.inlineCallbacks
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
@param name(unicode): name of the importer
@param location(unicode): location of the data to import
can be an url, a file path, or anything which make sense
check importer description for more details
@param options(dict, None): extra options.
@param pubsub_service(jid.JID, None): jid of the PubSub service where data must be imported
None to use profile's server
@param pubsub_node(unicode, None): PubSub node to use
None to use importer's default node
@return (unicode): progress id
"""
if options is None:
options = {}
else:
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]
except KeyError:
if opt_default:
options[opt_name] = opt_default
else:
if not value:
del options[opt_name]
try:
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)
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'}
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
}
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):
"""Do the import recursively
@param import_handler(object): instance of the import handler
@param items_import_data(iterable): iterable of data as specified in [register]
@param progress_id(unicode): id of progression
@param session(dict): data for this import session
can be used by importer so store any useful data
"root_service" and "root_node" are set to the main pubsub service and node of the import
@param options(dict): import options
@param return_data(dict): data to return on progressFinished
@param service(jid.JID, None): PubSub service to use
@param node(unicode, None): PubSub node to use
@param depth(int): level of recursion
"""
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)
yield import_handler.itemFilters(client, 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
log.debug(_(u"uploading subitems"))
yield self.recursiveImport(**recurse_kwargs)
if depth == 0:
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.removeProgressCb(progress_id, client.profile)
del client._import[import_handler.name][progress_id]
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
@param callback(callable): method to call:
the signature must be (client, location, options) (cf. [doImport])
the importer must return a tuple with (items_import_data, items_count)
items_import_data(iterable[dict]) data specific to specialized importer
cf. importItem docstring of specialized importer for details
items_count (int, None) indicate the total number of items (without subitems)
useful to display a progress indicator when the iterator is a generator
use None if you can't guess the total number of items
@param short_desc(unicode): one line description of the importer
@param long_desc(unicode): long description of the importer, its options, etc.
"""
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))
import_handler.importers[name] = Importer(callback, short_desc, long_desc)
def unregister(self, import_handler, name):
del import_handler.importers[name]

View file

@ -0,0 +1,197 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT plugin for import external blogs
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _, D_
from sat.core.constants import Const as C
from sat.core import exceptions
from twisted.internet import reactor, defer, protocol
from twisted.python.failure import Failure
from twisted.python.procutils import which
import re
from sat.core.log import getLogger
log = getLogger(__name__)
PLUGIN_INFO = {
C.PI_NAME: "Mercurial Merge Request handler",
C.PI_IMPORT_NAME: "MERGE_REQUEST_MERCURIAL",
C.PI_TYPE: C.PLUG_TYPE_MISC,
C.PI_DEPENDENCIES: ["MERGE_REQUESTS"],
C.PI_MAIN: "MercurialHandler",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Merge request handler for Mercurial""")
}
SHORT_DESC = D_(u"handle Mercurial repository")
class MercurialProtocol(protocol.ProcessProtocol):
"""handle hg commands"""
hg = None
def __init__(self, deferred, stdin=None):
"""
@param deferred(defer.Deferred): will be called when command is completed
@param stdin(str, None): if not None, will be push to standard input
"""
self._stdin = stdin
self._deferred = deferred
self.data = []
def connectionMade(self):
if self._stdin is not None:
self.transport.write(self._stdin)
self.transport.closeStdin()
def outReceived(self, data):
self.data.append(data)
def errReceived(self, data):
self.data.append(data)
def processEnded(self, reason):
data = u''.join([d.decode('utf-8') for d in self.data])
if (reason.value.exitCode == 0):
log.debug(_('Mercurial command succeed'))
self._deferred.callback(data)
else:
msg = _(u"Can't complete Mercurial command (error code: {code}): {message}").format(
code = reason.value.exitCode,
message = data)
log.warning(msg)
self._deferred.errback(Failure(RuntimeError(msg)))
@classmethod
def run(cls, path, command, *args, **kwargs):
"""Create a new MercurialRegisterProtocol and execute the given mercurialctl command.
@param path(unicode): path to the repository
@param command(unicode): command to run
@param *args(unicode): command arguments
@param **kwargs: used because Python2 doesn't handle normal kw args after *args
can only be:
- stdin(unicode, None): data to push to standard input
@return ((D)):
"""
stdin = kwargs.pop('stdin', None)
if kwargs:
raise exceptions.InternalError(u'only stdin is allowed as keyword argument')
if stdin is not None:
stdin = stdin.encode('utf-8')
d = defer.Deferred()
mercurial_prot = MercurialProtocol(d, stdin=stdin)
cmd_args = [cls.hg, command.encode('utf-8')]
cmd_args.extend([a.encode('utf-8') for a in args])
reactor.spawnProcess(mercurial_prot,
cls.hg,
cmd_args,
path=path.encode('utf-8'))
return d
class MercurialHandler(object):
data_types = (u'mercurial_changeset',)
def __init__(self, host):
log.info(_(u"Mercurial merge request handler initialization"))
try:
MercurialProtocol.hg = which('hg')[0]
except IndexError:
raise exceptions.NotFound(_(u"Mercurial executable (hg) not found, can't use Mercurial handler"))
self.host = host
self._m = host.plugins['MERGE_REQUESTS']
self._m.register('mercurial', self, self.data_types, SHORT_DESC)
def check(self, repository):
d = MercurialProtocol.run(repository, 'identify')
d.addCallback(lambda dummy: True)
d.addErrback(lambda dummy: False)
return d
def export(self, repository):
return MercurialProtocol.run(repository, 'export', '-g', '-r', 'outgoing()', '--encoding=utf-8')
def import_(self, repository, data, data_type, item_id, service, node, extra):
parsed_data = self.parse(data)
try:
parsed_name = parsed_data[0][u'commit_msg'].split(u'\n')[0]
parsed_name = re.sub(ur'[^\w -.]', u'', parsed_name, flags=re.UNICODE)[:40]
except Exception:
parsed_name = u''
name = u'mr_{item_id}_{parsed_name}'.format(item_id=item_id, parsed_name=parsed_name)
return MercurialProtocol.run(repository, 'qimport', '-g', '--name', name, '--encoding=utf-8', '-', stdin=data)
def parse(self, data, data_type=None):
lines = data.splitlines()
total_lines = len(lines)
patches = []
while lines:
patch = {}
commit_msg = []
diff = []
state = 'init'
if lines[0] != '# HG changeset patch':
raise exceptions.DataError(_(u'invalid changeset signature'))
# line index of this patch in the whole data
patch_idx = total_lines - len(lines)
del lines[0]
for idx, line in enumerate(lines):
if state == 'init':
if line.startswith(u'# '):
if line.startswith(u'# User '):
elems = line[7:].split()
if not elems:
continue
last = elems[-1]
if last.startswith(u'<') and last.endswith(u'>') and u'@' in last:
patch[self._m.META_EMAIL] = elems.pop()[1:-1]
patch[self._m.META_AUTHOR] = u' '.join(elems)
elif line.startswith(u'# Date '):
time_data = line[7:].split()
if len(time_data) != 2:
log.warning(_(u'unexpected time data: {data}').format(data=line[7:]))
continue
patch[self._m.META_TIMESTAMP] = int(time_data[0]) + int(time_data[1])
elif line.startswith(u'# Node ID '):
patch[self._m.META_HASH] = line[10:]
elif line.startswith(u'# Parent '):
patch[self._m.META_PARENT_HASH] = line[10:]
else:
state = 'commit_msg'
if state == 'commit_msg':
if line.startswith(u'diff --git a/'):
state = 'diff'
patch[self._m.META_DIFF_IDX] = patch_idx + idx + 1
else:
commit_msg.append(line)
if state == 'diff':
if line.startswith(u'# ') or idx == len(lines)-1:
# a new patch is starting or we have reached end of patches
patch[self._m.META_COMMIT_MSG] = u'\n'.join(commit_msg)
patch[self._m.META_DIFF] = u'\n'.join(diff)
patches.append(patch)
if idx == len(lines)-1:
del lines[:]
else:
del lines[:idx]
break
else:
diff.append(line)
return patches

View file

@ -0,0 +1,561 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for account creation (experimental)
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from sat.memory.memory import Sessions
from sat.memory.crypto import PasswordHasher
from sat.core.constants import Const as C
import ConfigParser
from twisted.internet import defer
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
# TODO: account deletion/password change need testing
PLUGIN_INFO = {
C.PI_NAME: "Account Plugin",
C.PI_IMPORT_NAME: "MISC-ACCOUNT",
C.PI_TYPE: "MISC",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: ["XEP-0077"],
C.PI_RECOMMENDATIONS: ['GROUPBLOG'],
C.PI_MAIN: "MiscAccount",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""SàT account creation""")
}
CONFIG_SECTION = "plugin account"
# You need do adapt the following consts to your server
# 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
}
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.
It is still under development, please keep it in mind!
Here is your connection information:
Login on {domain}: {profile}
Jabber ID (JID): {jid}
Your password has been chosen by yourself during registration.
In the beginning, you have nobody to talk to. To find some contacts, you may use the users' directory:
- make yourself visible in "Service / Directory subscription".
- search for people with "Contacts" / Search directory".
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)
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)
def deleteBlogCallback(posts, comments):
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)
# 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')
if not admin_email:
return
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 = set(value)
value.add(admin_email)
self.host.memory.config.set(section, param_name, ",".join(value))
def getConfig(self, name, section=CONFIG_SECTION):
if name.startswith("email_"):
# XXX: email_ parameters were first in [plugin account] section
# but as it make more sense to have them in common with other plugins,
# they can now be in [DEFAULT] section
try:
value = self.host.memory.getConfig(None, name, Exception)
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
pass
else:
return value
if section == CONFIG_SECTION:
default = default_conf[name]
else:
default = None
return self.host.memory.getConfig(section, name, default)
def _registerAccount(self, email, password, profile):
return self.registerAccount(email, password, None, profile)
def registerAccount(self, email, password, jid_s, profile):
"""Register a new profile, its associated XMPP account, send the confirmation emails.
@param email (unicode): where to send to confirmation email to
@param password (unicode): password chosen by the user
while be used for profile *and* XMPP account
@param jid_s (unicode): JID to re-use or to register:
- non empty value: bind this JID to the new sat profile
- None or "": register a new JID on the local XMPP server
@param profile
@return Deferred
"""
d = self.createProfile(password, jid_s, profile)
d.addCallback(lambda dummy: self.sendEmails(email, profile))
return d
def createProfile(self, password, jid_s, profile):
"""Register a new profile and its associated XMPP account.
@param password (unicode): password chosen by the user
while be used for profile *and* XMPP account
@param jid_s (unicode): JID to re-use or to register:
- non empty value: bind this JID to the new sat profile
- None or "": register a new JID on the local XMPP server
@param profile
@return Deferred
"""
if not password or not profile:
raise exceptions.DataError
if profile.lower() in self.getConfig('reserved_list'):
return defer.fail(Failure(exceptions.ConflictError))
d = self.host.memory.createProfile(profile, password)
d.addCallback(lambda dummy: self.profileCreated(password, jid_s, profile))
return d
def profileCreated(self, password, jid_s, profile):
"""Create the XMPP account and set the profile connection parameters.
@param password (unicode): password chosen by the user
@param jid_s (unicode): JID to re-use or to register:
- non empty value: bind this JID to the new sat profile
- None or empty: register a new JID on the local XMPP server
@param profile
@return: Deferred
"""
if jid_s:
d = defer.succeed(None)
jid_ = jid.JID(jid_s)
else:
jid_s = profile + u"@" + self.getNewAccountDomain()
jid_ = jid.JID(jid_s)
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)
return d
def removeProfile(failure):
self.host.memory.asyncDeleteProfile(profile)
return failure
d.addCallback(lambda dummy: self.host.memory.startSession(password, profile))
d.addCallback(setParams)
d.addCallback(lambda dummy: self.host.memory.stopSession(profile))
d.addErrback(removeProfile)
return d
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_))
def sendEmails(self, email, profile):
# time to send the email
domain = self.getNewAccountDomain()
# email to the administrators
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")
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,
# there is no email when an existing XMPP account is used
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)))
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))
# 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)
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)
if not 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
def _getAccountDialogUI(self, profile):
"""Get the main dialog to manage your account
@param menu_data
@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)
tab_container = form_ui.current_container
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"))
form_ui.addPassword("new_passwd1", value="")
form_ui.addLabel(D_("New password (again)"))
form_ui.addPassword("new_passwd2", value="")
# FIXME: uncomment and fix these features
"""
if 'GROUPBLOG' in self.host.plugins:
tab_container.addTab("delete_posts", D_("Delete your posts"), container=xml_tools.PairsContainer)
form_ui.addLabel(D_("Current profile password"))
form_ui.addPassword("delete_posts_passwd", value="")
form_ui.addLabel(D_("Delete all your posts and their comments"))
form_ui.addBool("delete_posts_checkbox", "false")
form_ui.addLabel(D_("Delete all your comments on other's posts"))
form_ui.addBool("delete_comments_checkbox", "false")
tab_container.addTab("delete", D_("Delete your account"), container=xml_tools.PairsContainer)
form_ui.addLabel(D_("Current profile password"))
form_ui.addPassword("delete_passwd", value="")
form_ui.addLabel(D_("Delete your account"))
form_ui.addBool("delete_checkbox", "false")
"""
return form_ui.toXml()
@defer.inlineCallbacks
def _accountDialogCb(self, data, profile):
"""Called when the user submits the main account dialog
@param data
@param 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):
auth = yield PasswordHasher.verify(attempt, sat_cipher)
defer.returnValue(auth)
def error_ui(message=None):
if not message:
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()}
# check for account deletion
# FIXME: uncomment and fix these features
"""
delete_passwd = data[xml_tools.SAT_FORM_PREFIX + 'delete_passwd']
delete_checkbox = data[xml_tools.SAT_FORM_PREFIX + 'delete_checkbox']
if delete_checkbox == 'true':
verified = yield verify(delete_passwd)
assert isinstance(verified, bool)
if verified:
defer.returnValue(self.__deleteAccount(profile))
defer.returnValue(error_ui())
# check for blog posts deletion
if 'GROUPBLOG' in self.host.plugins:
delete_posts_passwd = data[xml_tools.SAT_FORM_PREFIX + 'delete_posts_passwd']
delete_posts_checkbox = data[xml_tools.SAT_FORM_PREFIX + 'delete_posts_checkbox']
delete_comments_checkbox = data[xml_tools.SAT_FORM_PREFIX + 'delete_comments_checkbox']
posts = delete_posts_checkbox == 'true'
comments = delete_comments_checkbox == 'true'
if posts or comments:
verified = yield verify(delete_posts_passwd)
assert isinstance(verified, bool)
if verified:
defer.returnValue(self.__deleteBlogPosts(posts, comments, profile))
defer.returnValue(error_ui())
"""
# 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']
if new_passwd1 or new_passwd2:
verified = yield verify(current_passwd)
assert isinstance(verified, bool)
if verified:
if new_passwd1 == new_passwd2:
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())
defer.returnValue({})
def __changePassword(self, password, profile):
"""Ask for a confirmation before changing the XMPP account and SàT profile passwords.
@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."))
form_ui.addText(D_("Continue with changing the password?"))
return {'xmlui': form_ui.toXml()}
def __changePasswordCb(self, data, profile):
"""Actually change the user XMPP account and SàT profile password
@param data (dict)
@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']]
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))
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()})
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()})
d = self.host.plugins['XEP-0077'].changePassword(client, password)
d.addCallbacks(passwordChanged, errback)
return d
def __deleteAccount(self, profile):
"""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()}
def __deleteAccountCb(self, data, profile):
"""Actually delete the XMPP account and SàT profile
@param data
@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
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)
d.addCallback(lambda dummy: delete_profile())
else:
delete_profile()
return defer.succeed({})
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()})
d = self.host.plugins['XEP-0077'].unregister(client, jid.JID(client.jid.host))
d.addCallbacks(userDeleted, errback)
return d
def __deleteBlogPosts(self, posts, comments, profile):
"""Ask for a confirmation before deleting the blog posts
@param posts: delete all posts of the user (and their comments)
@param comments: delete all the comments of the user on other's posts
@param data
@param profile
"""
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?"))
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?"))
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?"))
return {'xmlui': form_ui.toXml()}
def __deleteBlogPostsCb(self, posts, comments, data, profile):
"""Actually delete the XMPP account and SàT profile
@param posts: delete all posts of the user (and their comments)
@param comments: delete all the comments of the user on other's posts
@param profile
"""
if posts:
if comments:
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)
elif comments:
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()})
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()})
d.addCallbacks(deleted, errback)
return d
def asyncConnectWithXMPPCredentials(self, jid_s, password):
"""Create and connect a new SàT profile using the given XMPP credentials.
Re-use given JID and XMPP password for the profile name and profile password.
@param jid_s (unicode): JID
@param password (unicode): XMPP password
@return Deferred(bool)
@raise exceptions.PasswordError, exceptions.ConflictError
"""
try: # be sure that the profile doesn't exist yet
self.host.memory.getProfileName(jid_s)
except exceptions.ProfileUnknownError:
pass
else:
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(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())
self.host.memory.asyncDeleteProfile(jid_s)
raise failure
# FIXME: we don't catch the case where the JID host is not an XMPP server, and the user
# has to wait until the DBUS timeout ; as a consequence, emails are sent to the admins
# and the profile is not deleted. When the host exists, removeProfile is well called.
d.addCallbacks(connected, removeProfile)
return d

View file

@ -0,0 +1,100 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for file tansfer
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
import mmap
PLUGIN_INFO = {
C.PI_NAME: "Android ",
C.PI_IMPORT_NAME: "android",
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""")
}
if sys.platform != "android":
raise exceptions.CancelError(u"this module is not needed on this platform")
from plyer import notification, vibrator
PARAM_VIBRATE_CATEGORY = "Notifications"
PARAM_VIBRATE_NAME = "vibrate"
PARAM_VIBRATE_LABEL = D_(u"Vibrate on notifications")
class AndroidPlugin(object):
params = """
<params>
<individual>
<category name="{category_name}" label="{category_label}">
<param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" />
</category>
</individual>
</params>
""".format(
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)
# 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'
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()
try:
subject = mess_data['subject'].itervalues().next()
except StopIteration:
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):
vibrator.vibrate()
return mess_data
def messageReceivedTrigger(self, client, message_elt, post_treat):
if not self.cagou_active:
# we only send notification is the frontend is not displayed
post_treat.addCallback(self._notifyMessage, client)
return True

View file

@ -0,0 +1,61 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT plugin for managing raw XML log
# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core.constants import Const as C
import json
PLUGIN_INFO = {
C.PI_NAME: "Debug Plugin",
C.PI_IMPORT_NAME: "DEBUG",
C.PI_TYPE: "Misc",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "Debug",
C.PI_HANDLER: "no",
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)
def _fakeSignal(self, signal, arguments, profile_key):
"""send a signal from backend
@param signal(str): name of the signal
@param arguments(unicode): json encoded list of arguments
@parm profile_key(unicode): profile_key to use or C.PROF_KEY_NONE if profile is not needed
"""
args = json.loads(arguments)
method = getattr(self.host.bridge, signal)
if profile_key != C.PROF_KEY_NONE:
profile = self.host.memory.getProfileName(profile_key)
args.append(profile)
method(*args)

View file

@ -0,0 +1,73 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for displaying messages from extra PEP services
# Copyright (C) 2015 Adrien Cossa (souliane@mailoo.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
PLUGIN_INFO = {
C.PI_NAME: "Extra PEP",
C.PI_IMPORT_NAME: "EXTRA-PEP",
C.PI_TYPE: "MISC",
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: [],
C.PI_RECOMMENDATIONS: [],
C.PI_MAIN: "ExtraPEP",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(u"""Display messages from extra PEP services""")
}
PARAM_KEY = u"Misc"
PARAM_NAME = u"blogs"
PARAM_LABEL = u"Blog authors following list"
PARAM_DEFAULT = (jid.JID("salut-a-toi@libervia.org"),)
class ExtraPEP(object):
params = """
<params>
<individual>
<category name="%(category_name)s" label="%(category_label)s">
<param name="%(param_name)s" label="%(param_label)s" type="jids_list" security="0">
%(jids)s
</param>
</category>
</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)})
}
def __init__(self, host):
log.info(_(u"Plugin Extra PEP initialization"))
self.host = host
host.memory.updateParams(self.params)
def getFollowedEntities(self, profile_key):
return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile_key)

View file

@ -0,0 +1,262 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SAT plugin for file tansfer
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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
from sat.tools import stream
from twisted.internet import defer
from twisted.words.protocols.jabber import jid
import os
import os.path
PLUGIN_INFO = {
C.PI_NAME: "File Tansfer",
C.PI_IMPORT_NAME: "FILE",
C.PI_TYPE: C.PLUG_TYPE_MISC,
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.""")
}
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'
class FilePlugin(object):
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)
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)
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)
@defer.inlineCallbacks
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
@param filepath(str): absolute path to the file
@param filename(unicode, None): name to use, or None to find it from filepath
@param file_desc(unicode, None): description of the file
@param profile: %(doc_profile)s
@return (dict): action dictionary, with progress id in case of success, else xmlui message
"""
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 '_'
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})
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()})
def _onFileChoosed(self, client, peer_jid, data):
cancelled = C.bool(data.get("cancelled", C.BOOL_FALSE))
if cancelled:
return
path=data['path']
return self.fileSend(client, peer_jid, path)
def _fileSendMenu(self, data, profile):
""" XMLUI activated by menu: return file sending UI
@param profile: %(doc_profile)s
"""
try:
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)
xml_ui = xml_tools.XMLUI(
C.XMLUI_DIALOG,
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)
return {'xmlui': xml_ui.toXml()}
def register(self, namespace, callback, priority=0, method_name=None):
"""Register a fileSending method
@param namespace(unicode): XEP namespace
@param callback(callable): method to call (must have the same signature as [fileSend])
@param priority(int): pririoty of this method, the higher available will be used
@param method_name(unicode): short name for the method, namespace will be used if None
"""
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))
self._file_callbacks.sort(key=lambda data: data[2], reverse=True)
def unregister(self, namespace):
for idx, data in enumerate(self._file_callbacks):
if data[0] == namespace:
del [idx]
return
raise exceptions.NotFound(u"The namespace to unregister doesn't exist")
# Dialogs with user
# the overwrite check is done here
def openFileWrite(self, client, file_path, transfer_data, file_data, stream_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(
self.host,
client,
file_path,
mode='wb',
uid=file_data[PROGRESS_ID_KEY],
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(
self.host,
client,
file_path,
mode='wb',
uid=file_data[PROGRESS_ID_KEY],
size=file_data['size'],
data_cb = file_data.get('data_cb'),
)
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
@param transfer_data(dict): same as for [self.getDestDir]
@param file_data(dict): same as for [self.getDestDir]
@param stream_object(bool): same as for [self.getDestDir]
return (bool): True if copy is wanted and OK
False if user wants to cancel
if file exists ask confirmation and call again self._getDestDir if needed
"""
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))
# 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)
return True
else:
return self.getDestDir(client, peer_jid, transfer_data, file_data)
exists_d = xml_tools.deferConfirm(
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]
},
security_limit=SECURITY_LIMIT,
profile=client.profile)
exists_d.addCallback(check_overwrite)
return exists_d
self.openFileWrite(client, file_path, transfer_data, file_data, stream_object)
return True
def getDestDir(self, client, peer_jid, transfer_data, file_data, stream_object=False):
"""Request confirmation and destination dir to user
Overwrite confirmation is managed.
if transfer is confirmed, 'file_obj' is added to transfer_data
@param peer_jid(jid.JID): jid of the file sender
@param filename(unicode): name of the file
@param transfer_data(dict): data of the transfer session,
it will be only used to store the file_obj.
"file_obj" (or "stream_object") key *MUST NOT* exist before using getDestDir
@param file_data(dict): information about the file to be transfered
It MUST contain the following keys:
- peer_jid (jid.JID): other peer jid
- name (unicode): name of the file to trasnsfer
the name must not be empty or contain a "/" character
- size (int): size of the file
- desc (unicode): description of the file
- progress_id (unicode): id to use for progression
It *MUST NOT* contain the "peer" key
It may contain:
- data_cb (callable): method called on each data read/write
"file_path" will be added to this dict once destination selected
"size_human" will also be added with human readable file size
@param stream_object(bool): if True, a stream_object will be used instead of file_obj
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)
if not cont:
return ret_value
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,
_(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]
},
security_limit=SECURITY_LIMIT,
profile=client.profile)
d.addCallback(self._gotConfirmation, client, peer_jid, transfer_data, file_data, stream_object)
return d

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