comparison sat/plugins/plugin_misc_watched.py @ 2562:26edcf3a30eb

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/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_misc_watched.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SàT plugin to be notified on some entities presence
5 # Copyright (C) 2009-2018 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 C.PI_NAME: "Watched",
30 C.PI_IMPORT_NAME: "WATCHED",
31 C.PI_TYPE: "Misc",
32 C.PI_PROTOCOLS: [],
33 C.PI_DEPENDENCIES: [],
34 C.PI_MAIN: "Watched",
35 C.PI_HANDLER: "no",
36 C.PI_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=profile)
80
81 return True