annotate sat/plugins/plugin_misc_watched.py @ 3215:bfa1bde97f48

core (tools/utils): new `asDeferred` function: asDeferred is similar to defer.maybeCoroutine, and also handles coroutine.
author Goffi <goffi@goffi.org>
date Fri, 13 Mar 2020 17:46:27 +0100
parents 559a625a236b
children 6cf4bd6972c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2820
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
1480
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
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
5 # Copyright (C) 2009-2020 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)
2820
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
65 host.trigger.add("presence_received", self._presenceReceivedTrigger)
1480
8d61160ee4b8 core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff changeset
66
2820
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
67 def _presenceReceivedTrigger(self, client, entity, show, priority, statuses):
1480
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:
2820
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 old_show = self.host.memory.getEntityDatum(
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
74 entity, "presence", client.profile).show
1480
8d61160ee4b8 core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff changeset
75 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
76 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
77
8d61160ee4b8 core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if old_show == C.PRESENCE_UNAVAILABLE:
2820
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
79 watched = self.host.memory.getParamA(
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
80 NAME, CATEGORY, profile_key=client.profile)
1480
8d61160ee4b8 core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff changeset
81 if entity in watched or entity.userhostJID() in watched:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 self.host.actionNew(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 "xmlui": xml_tools.note(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 _(NOTIF).format(entity=entity.full())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 ).toXml()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 },
2820
7ab8684784c6 core: renamed presenceReceived to presence_reveiced + use "client" as first arguments instead of "profile"
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 profile=client.profile,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 )
1480
8d61160ee4b8 core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff changeset
90
8d61160ee4b8 core, plugin watched: new plugin, show an alert when a watched entity goes online
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return True