comparison src/plugins/plugin_tmp_demo_directory.py @ 869:0d1a17375bf5

plugin demo directory: temporary plugin to integrate Salut's directory in SàT's parameters: this plugin has to be explicitly activate in sat.conf with the following parameters in category [plugin demo directory]: activate=yes ==> to activate service=salut.domain.tld ==> Salut service jid This plugin is dependant of Salut's implementation, and will not work with other services
author Goffi <goffi@goffi.org>
date Tue, 25 Feb 2014 23:03:17 +0100
parents
children 301b342c697a
comparison
equal deleted inserted replaced
868:967b94ef821e 869:0d1a17375bf5
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for account creation (experimental)
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _, D_
21 from logging import debug, info, warning, error
22 from twisted.internet import defer
23 from twisted.words.protocols.jabber.xmlstream import IQ
24 from twisted.words.protocols.jabber import jid
25 from wokkel import data_form
26
27 PLUGIN_INFO = {
28 "name": "Demo directory plugin",
29 "import_name": "DEMO-DIRECTORY",
30 "type": "TMP",
31 "protocols": [],
32 "dependencies": [],
33 "main": "DemoDirectory",
34 "handler": "no",
35 "description": _(u"""Plugin to add paramaters to subscribe to the demo directory""")
36 }
37
38 NS_COMMANDS = "http://jabber.org/protocol/commands"
39
40 CONFIG_SECTION = "plugin demo directory"
41 CONFIG_ACTIVATE = "activate"
42 CONFIG_SERVICE = "service"
43
44 PARAM_CATEGORY = ("directory", D_("Directory"))
45 PARAM_NAME_INFO = D_("To appear on the directory, check the box. To be removed from the directory, just uncheck it.")
46 PARAM_NAME_DESC = ("description", D_("Some words about you"))
47 PARAM_NAME_SUB = ("subscribe", D_("I want to appear in the public demo directory"))
48
49
50
51 class DemoDirectory(object):
52 """Account plugin: create a SàT + Prosody account, used by Libervia"""
53 # XXX: This plugin is a Q&D one used for the demo.
54 # TODO: A generic way to add menu/parameters for local services would be nice
55
56 params = """
57 <params>
58 <individual>
59 <category name="%(category_name)s" label="%(category_label)s">
60 <param label="%(info)s" type="text" security="1" />
61 <param name="%(desc)s" label="%(desc_label)s" type="string" security="1" />
62 <param name="%(sub)s" label="%(sub_label)s" value='false' type="bool" security="1" />
63 </category>
64 </individual>
65 </params>
66 """ % {
67 'category_name': PARAM_CATEGORY[0],
68 'category_label': _(PARAM_CATEGORY[1]),
69 'info': _(PARAM_NAME_INFO),
70 'desc': PARAM_NAME_DESC[0],
71 'desc_label': _(PARAM_NAME_DESC[1]),
72 'sub': PARAM_NAME_SUB[0],
73 'sub_label': _(PARAM_NAME_SUB[1]),
74 }
75
76 def __init__(self, host):
77 info(_(u"Plugin demo directory initialization"))
78 activate = host.memory.getConfig(CONFIG_SECTION, CONFIG_ACTIVATE) or 'false'
79 if not activate.lower() in ('true', 'yes', '1'):
80 info("not activated")
81 return
82 service_str = host.memory.getConfig(CONFIG_SECTION, CONFIG_SERVICE) or 'salut.libervia.org'
83 self.service = jid.JID(service_str)
84 self.host = host
85 host.memory.updateParams(self.params)
86 host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
87
88 @defer.inlineCallbacks
89 def manageSubscription(self, subscribe, profile):
90 """ Subscribe or unsubscribe according to subscribe value
91 This follow the implementation of the "Salut", this is not for general purpose
92 @param subscribe: True to subscribe, else unsubscribe
93 @param profile: %(doc_profile)s
94
95 """
96 client = self.host.getClient(profile)
97 if not subscribe:
98 info ("Unsubscribing [%s] from directory" % profile)
99 unsub_req = IQ(client.xmlstream, 'set')
100 unsub_req['from'] = client.jid.full()
101 unsub_req['to'] = self.service.userhost()
102 command_elt = unsub_req.addElement('command', NS_COMMANDS)
103 command_elt['node'] = 'unsubscribe'
104 command_elt['action'] = 'execute'
105 yield unsub_req.send(self.service.userhost())
106 else:
107 info ("Subscribing [%s] to directory" % profile)
108 sub_req = IQ(client.xmlstream, 'set')
109 sub_req['from'] = client.jid.full()
110 sub_req['to'] = self.service.userhost()
111 command_elt = sub_req.addElement('command', NS_COMMANDS)
112 command_elt['node'] = 'subscribe'
113 command_elt['action'] = 'execute'
114 sub_first_elt = yield sub_req.send(self.service.userhost())
115 sub_cmd = sub_first_elt.elements(NS_COMMANDS, 'command').next()
116 session_id = sub_cmd['sessionid']
117 sub_req = IQ(client.xmlstream, 'set')
118 sub_req['from'] = client.jid.full()
119 sub_req['to'] = self.service.userhost()
120 command_elt = sub_req.addElement('command', NS_COMMANDS)
121 command_elt['node'] = 'subscribe'
122 command_elt['action'] = 'execute'
123 command_elt['sessionid'] = session_id
124 description = self.host.memory.getParamA(PARAM_NAME_DESC[0], PARAM_CATEGORY[0], profile_key=client.profile)
125 ret_form = data_form.Form('submit')
126 ret_form.makeFields({'jid': client.jid.userhost(), 'description': description})
127 command_elt.addChild(ret_form.toElement())
128 yield sub_req.send(self.service.userhost())
129
130 def paramUpdateTrigger(self, name, value, category, type_, profile):
131 """
132 Reset all the existing chat state entity data associated with this profile after a parameter modification.
133 @param name: parameter name
134 @param value: "true" to activate the notifications, or any other value to delete it
135 @param category: parameter category
136 """
137 if (category, name) == (PARAM_CATEGORY[0], PARAM_NAME_SUB[0]):
138 self.manageSubscription(value.lower()=='true', profile)
139 return False
140 return True