comparison src/plugins/plugin_misc_watched.py @ 1480:8d61160ee4b8

core, plugin watched: new plugin, show an alert when a watched entity goes online
author Goffi <goffi@goffi.org>
date Thu, 20 Aug 2015 18:43:56 +0200
parents
children e2ed8009e66e
comparison
equal deleted inserted replaced
1479:057f0714f27e 1480:8d61160ee4b8
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SàT plugin to be notified on some entities presence
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 log = getLogger(__name__)
24 from sat.core import exceptions
25 from sat.tools import xml_tools
26
27
28 PLUGIN_INFO = {
29 "name": "Watched",
30 "import_name": "WATCHED",
31 "type": "Misc",
32 "protocols": [],
33 "dependencies": [],
34 "main": "Watched",
35 "handler": "no",
36 "description": _("""Watch for entities presence, and send notification accordingly""")
37 }
38
39
40 CATEGORY = D_("Misc")
41 NAME = "Watched"
42 NOTIF = D_("Watched entity {entity} is connected")
43
44
45 class Watched(object):
46 params = """
47 <params>
48 <individual>
49 <category name="{category_name}" label="{category_label}">
50 <param name="{name}" label="{label}" type="jids_list" security="0" />
51 </category>
52 </individual>
53 </params>
54 """.format(category_name=CATEGORY,
55 category_label=_(CATEGORY),
56 name=NAME,
57 label=_(NAME),
58 )
59
60 def __init__(self, host):
61 log.info(_("Watched initialisation"))
62 self.host = host
63 host.memory.updateParams(self.params)
64 host.trigger.add("presenceReceived", self._presenceReceivedTrigger)
65
66 def _presenceReceivedTrigger(self, entity, show, priority, statuses, profile):
67 if show == C.PRESENCE_UNAVAILABLE:
68 return True
69
70 # we check that the previous presence was unavailable (no notification else)
71 try:
72 old_show = self.host.memory.getEntityDatum(entity, "presence", profile).show
73 except (KeyError, exceptions.UnknownEntityError):
74 old_show = C.PRESENCE_UNAVAILABLE
75
76 if old_show == C.PRESENCE_UNAVAILABLE:
77 watched = self.host.memory.getParamA(NAME, CATEGORY, profile_key=profile)
78 if entity in watched or entity.userhostJID() in watched:
79 self.host.actionNew({'xmlui': xml_tools.note(_(NOTIF).format(entity=entity.full())).toXml()}, profile)
80
81 return True