From fecc93ba1523098b57526f1ffb0e9d73261d7151 Mon Sep 17 00:00:00 2001 From: Goffi Date: Tue, 25 Feb 2014 23:03:17 +0100 Subject: [PATCH] =?UTF-8?q?plugin=20demo=20directory:=20temporary=20plugin?= =?UTF-8?q?=20to=20integrate=20Salut's=20directory=20in=20S=C3=A0T's=20par?= =?UTF-8?q?ameters:=20this=20plugin=20has=20to=20be=20explicitly=20activat?= =?UTF-8?q?e=20in=20sat.conf=20with=20the=20following=20parameters=20in=20?= =?UTF-8?q?category=20[plugin=20demo=20directory]:=20activate=3Dyes=20=3D?= =?UTF-8?q?=3D>=20to=20activate=20service=3Dsalut.domain.tld=20=3D=3D>=20S?= =?UTF-8?q?alut=20service=20jid=20This=20plugin=20is=20dependant=20of=20Sa?= =?UTF-8?q?lut's=20implementation,=20and=20will=20not=20work=20with=20othe?= =?UTF-8?q?r=20services?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/plugin_tmp_demo_directory.py | 140 +++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 src/plugins/plugin_tmp_demo_directory.py diff --git a/src/plugins/plugin_tmp_demo_directory.py b/src/plugins/plugin_tmp_demo_directory.py new file mode 100755 index 00000000..c5838a62 --- /dev/null +++ b/src/plugins/plugin_tmp_demo_directory.py @@ -0,0 +1,140 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# SAT plugin for account creation (experimental) +# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 . + +from sat.core.i18n import _, D_ +from logging import debug, info, warning, error +from twisted.internet import defer +from twisted.words.protocols.jabber.xmlstream import IQ +from twisted.words.protocols.jabber import jid +from wokkel import data_form + +PLUGIN_INFO = { + "name": "Demo directory plugin", + "import_name": "DEMO-DIRECTORY", + "type": "TMP", + "protocols": [], + "dependencies": [], + "main": "DemoDirectory", + "handler": "no", + "description": _(u"""Plugin to add paramaters to subscribe to the demo directory""") +} + +NS_COMMANDS = "http://jabber.org/protocol/commands" + +CONFIG_SECTION = "plugin demo directory" +CONFIG_ACTIVATE = "activate" +CONFIG_SERVICE = "service" + +PARAM_CATEGORY = ("directory", D_("Directory")) +PARAM_NAME_INFO = D_("To appear on the directory, check the box. To be removed from the directory, just uncheck it.") +PARAM_NAME_DESC = ("description", D_("Some words about you")) +PARAM_NAME_SUB = ("subscribe", D_("I want to appear in the public demo directory")) + + + +class DemoDirectory(object): + """Account plugin: create a SàT + Prosody account, used by Libervia""" + # XXX: This plugin is a Q&D one used for the demo. + # TODO: A generic way to add menu/parameters for local services would be nice + + params = """ + + + + + + + + + + """ % { + 'category_name': PARAM_CATEGORY[0], + 'category_label': _(PARAM_CATEGORY[1]), + 'info': _(PARAM_NAME_INFO), + 'desc': PARAM_NAME_DESC[0], + 'desc_label': _(PARAM_NAME_DESC[1]), + 'sub': PARAM_NAME_SUB[0], + 'sub_label': _(PARAM_NAME_SUB[1]), + } + + def __init__(self, host): + info(_(u"Plugin demo directory initialization")) + activate = host.memory.getConfig(CONFIG_SECTION, CONFIG_ACTIVATE) or 'false' + if not activate.lower() in ('true', 'yes', '1'): + info("not activated") + return + service_str = host.memory.getConfig(CONFIG_SECTION, CONFIG_SERVICE) or 'salut.libervia.org' + self.service = jid.JID(service_str) + self.host = host + host.memory.updateParams(self.params) + host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger) + + @defer.inlineCallbacks + def manageSubscription(self, subscribe, profile): + """ Subscribe or unsubscribe according to subscribe value + This follow the implementation of the "Salut", this is not for general purpose + @param subscribe: True to subscribe, else unsubscribe + @param profile: %(doc_profile)s + + """ + client = self.host.getClient(profile) + if not subscribe: + info ("Unsubscribing [%s] from directory" % profile) + unsub_req = IQ(client.xmlstream, 'set') + unsub_req['from'] = client.jid.full() + unsub_req['to'] = self.service.userhost() + command_elt = unsub_req.addElement('command', NS_COMMANDS) + command_elt['node'] = 'unsubscribe' + command_elt['action'] = 'execute' + yield unsub_req.send(self.service.userhost()) + else: + info ("Subscribing [%s] to directory" % profile) + sub_req = IQ(client.xmlstream, 'set') + sub_req['from'] = client.jid.full() + sub_req['to'] = self.service.userhost() + command_elt = sub_req.addElement('command', NS_COMMANDS) + command_elt['node'] = 'subscribe' + command_elt['action'] = 'execute' + sub_first_elt = yield sub_req.send(self.service.userhost()) + sub_cmd = sub_first_elt.elements(NS_COMMANDS, 'command').next() + session_id = sub_cmd['sessionid'] + sub_req = IQ(client.xmlstream, 'set') + sub_req['from'] = client.jid.full() + sub_req['to'] = self.service.userhost() + command_elt = sub_req.addElement('command', NS_COMMANDS) + command_elt['node'] = 'subscribe' + command_elt['action'] = 'execute' + command_elt['sessionid'] = session_id + description = self.host.memory.getParamA(PARAM_NAME_DESC[0], PARAM_CATEGORY[0], profile_key=client.profile) + ret_form = data_form.Form('submit') + ret_form.makeFields({'jid': client.jid.userhost(), 'description': description}) + command_elt.addChild(ret_form.toElement()) + yield sub_req.send(self.service.userhost()) + + def paramUpdateTrigger(self, name, value, category, type_, profile): + """ + Reset all the existing chat state entity data associated with this profile after a parameter modification. + @param name: parameter name + @param value: "true" to activate the notifications, or any other value to delete it + @param category: parameter category + """ + if (category, name) == (PARAM_CATEGORY[0], PARAM_NAME_SUB[0]): + self.manageSubscription(value.lower()=='true', profile) + return False + return True