Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_watched.py @ 2676:da8f3ac86845
template: overriden get_template to always have full template name:
Environment.get_template has been overriden to have always full template name (i.e. template with (site/theme) prefix or absolute template).
This is needed to correctly handle cache, so "base/base.html" is converted to "(some_site/some_theme)base/base.html" and won't conflict with an other theme "base/base.html".
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 18 Sep 2018 20:59:34 +0200 |
parents | 56f94936df1e |
children | 003b8b4b56a7 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
1480
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SàT plugin to be notified on some entities presence |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
1480
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _, D_ |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
23 |
1480
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 log = getLogger(__name__) |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.core import exceptions |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from sat.tools import xml_tools |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 PLUGIN_INFO = { |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
30 C.PI_NAME: "Watched", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
31 C.PI_IMPORT_NAME: "WATCHED", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
32 C.PI_TYPE: "Misc", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
33 C.PI_PROTOCOLS: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
34 C.PI_DEPENDENCIES: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
35 C.PI_MAIN: "Watched", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
36 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
37 C.PI_DESCRIPTION: _( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
38 """Watch for entities presence, and send notification accordingly""" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 ), |
1480
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 } |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 CATEGORY = D_("Misc") |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 NAME = "Watched" |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 NOTIF = D_("Watched entity {entity} is connected") |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 class Watched(object): |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 params = """ |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 <params> |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 <individual> |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 <category name="{category_name}" label="{category_label}"> |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 <param name="{name}" label="{label}" type="jids_list" security="0" /> |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 </category> |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 </individual> |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 </params> |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 """.format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 category_name=CATEGORY, category_label=_(CATEGORY), name=NAME, label=_(NAME) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 ) |
1480
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def __init__(self, host): |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 log.info(_("Watched initialisation")) |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 self.host = host |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 host.memory.updateParams(self.params) |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 host.trigger.add("presenceReceived", self._presenceReceivedTrigger) |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def _presenceReceivedTrigger(self, entity, show, priority, statuses, profile): |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if show == C.PRESENCE_UNAVAILABLE: |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 return True |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 # we check that the previous presence was unavailable (no notification else) |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 try: |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 old_show = self.host.memory.getEntityDatum(entity, "presence", profile).show |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 except (KeyError, exceptions.UnknownEntityError): |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 old_show = C.PRESENCE_UNAVAILABLE |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 if old_show == C.PRESENCE_UNAVAILABLE: |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 watched = self.host.memory.getParamA(NAME, CATEGORY, profile_key=profile) |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 if entity in watched or entity.userhostJID() in watched: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 self.host.actionNew( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 "xmlui": xml_tools.note( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 _(NOTIF).format(entity=entity.full()) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
84 ).toXml() |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 }, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
86 profile=profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 ) |
1480
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
8d61160ee4b8
core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 return True |