comparison libervia/backend/plugins/plugin_misc_watched.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_misc_watched.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SàT plugin to be notified on some entities presence
5 # Copyright (C) 2009-2021 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 libervia.backend.core.i18n import _, D_
21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
23
24 log = getLogger(__name__)
25 from libervia.backend.core import exceptions
26 from libervia.backend.tools import xml_tools
27
28
29 PLUGIN_INFO = {
30 C.PI_NAME: "Watched",
31 C.PI_IMPORT_NAME: "WATCHED",
32 C.PI_TYPE: "Misc",
33 C.PI_PROTOCOLS: [],
34 C.PI_DEPENDENCIES: [],
35 C.PI_MAIN: "Watched",
36 C.PI_HANDLER: "no",
37 C.PI_DESCRIPTION: _(
38 """Watch for entities presence, and send notification accordingly"""
39 ),
40 }
41
42
43 CATEGORY = D_("Misc")
44 NAME = "Watched"
45 NOTIF = D_("Watched entity {entity} is connected")
46
47
48 class Watched(object):
49 params = """
50 <params>
51 <individual>
52 <category name="{category_name}" label="{category_label}">
53 <param name="{name}" label="{label}" type="jids_list" security="0" />
54 </category>
55 </individual>
56 </params>
57 """.format(
58 category_name=CATEGORY, category_label=_(CATEGORY), name=NAME, label=_(NAME)
59 )
60
61 def __init__(self, host):
62 log.info(_("Watched initialisation"))
63 self.host = host
64 host.memory.update_params(self.params)
65 host.trigger.add("presence_received", self._presence_received_trigger)
66
67 def _presence_received_trigger(self, client, entity, show, priority, statuses):
68 if show == C.PRESENCE_UNAVAILABLE:
69 return True
70
71 # we check that the previous presence was unavailable (no notification else)
72 try:
73 old_show = self.host.memory.get_entity_datum(
74 client, entity, "presence").show
75 except (KeyError, exceptions.UnknownEntityError):
76 old_show = C.PRESENCE_UNAVAILABLE
77
78 if old_show == C.PRESENCE_UNAVAILABLE:
79 watched = self.host.memory.param_get_a(
80 NAME, CATEGORY, profile_key=client.profile)
81 if entity in watched or entity.userhostJID() in watched:
82 self.host.action_new(
83 {
84 "xmlui": xml_tools.note(
85 _(NOTIF).format(entity=entity.full())
86 ).toXml()
87 },
88 profile=client.profile,
89 )
90
91 return True