]> kaliko git repositories - sid.git/blob - sid/log.py
doc: Document log plugin
[sid.git] / sid / log.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2020 kaliko <kaliko@azylum.org>
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 only.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from datetime import datetime
18 from os import fdopen
19 from tempfile import mkstemp
20 from time import time
21
22 from .plugin import Plugin, botcmd
23
24
25 class Log(Plugin):
26     """Logs presence.
27     """
28     throttle_ts = int(time())-30
29
30     def __init__(self, bot):
31         Plugin.__init__(self, bot)
32         self.presence = list()
33         bot.add_event_handler("muc::%s::presence" %
34                               self.bot.room, self.log_presence)
35         bot.add_event_handler("muc::%s::got_online" %
36                               self.bot.room, self.log_online)
37         bot.add_event_handler("muc::%s::got_offline" %
38                               self.bot.room, self.log_offline)
39
40     def _get_jid(self, pres):
41         nick = pres['muc']['nick']
42         jid = self.bot.plugin['xep_0045'].rooms[self.bot.room][nick]['jid']
43         if not jid:
44             self.log.debug('jid not found, is bot account moderating room?')
45             return nick
46         jid = jid.split('/')[0]  # strip ressource
47         return jid
48
49     def log_online(self, pres):
50         nick = self._get_jid(pres)
51         self.log.info('got online: %s', nick)
52         self.presence.append((datetime.now(), 'online', nick))
53
54     def log_offline(self, pres):
55         nick = self._get_jid(pres)
56         self.log.info('got offline: %s', nick)
57         self.presence.append((datetime.now(), 'offline', nick))
58
59     def log_presence(self, pres):
60         nick = self._get_jid(pres)
61         self.log.debug('%s: %s', pres['muc']['nick'], pres['type'])
62         self.presence.append((datetime.now(), pres['type'], nick))
63
64     def _format_log(self):
65         log = [f'{d:%Y-%m-%d %H:%M}: {s:12} {n}' for d, s, n in self.presence]
66         return '\n'.join(log)
67
68     @botcmd(hidden=True)
69     def write(self, message, args):
70         """
71         **command** (hidden) ``!write`` : Writes log to file (use mktemp and return file location as MUC message)"""
72         delay = int(time()) - Log.throttle_ts
73         if delay < 30:
74             self.log.debug('throttling file creation')
75             self.reply(message, f'wait {30-delay}s')
76             return
77         log = self._format_log()
78         mode = {'mode': 'w', 'encoding': 'utf-8'}
79         prefix = self.bot.room.split('@')[0] + '_'
80         tmp = mkstemp(prefix=prefix, text=True)
81         self.log.info('Writing to %s', tmp[1])
82         with fdopen(tmp[0], **mode) as fileo:
83             fileo.writelines(log)
84             Log.throttle_ts = int(time())
85         self.reply(message, tmp[1])
86
87     @botcmd(hidden=True)
88     def dump(self, message, args):
89         """**command** (hidden)  ``!dump``  : Dumps log as MUC message"""
90         self.reply(message, self._format_log())
91
92
93 # VIM MODLINE
94 # vim: ai ts=4 sw=4 sts=4 expandtab