]> kaliko git repositories - sid.git/blob - sid/archive.py
Add Debian archive plugin
[sid.git] / sid / archive.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
18 from re import compile as re_compile
19
20 from .plugin import Plugin, botcmd
21 from .lib import get_pkg
22
23
24 class Archive(Plugin):
25     """Fetch package info from the archive
26     """
27     stable_codename = 'buster'
28     re_pkg = re_compile(r'(?P<package>[0-9a-z.+-]+)$')
29
30     def __init__(self, bot):
31         Plugin.__init__(self, bot)
32         bot.add_event_handler("muc::%s::message" %
33                               self.bot.room, self.muc_message)
34
35     def muc_message(self, msg):
36         """Handler method dealing with MUC incoming messages"""
37         # Does not reply to myself
38         if msg['mucnick'] == self.bot.nick:
39             return
40         if '#' not in msg['body']:
41             return
42         bugs = list()
43         for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())):
44             self.log.debug('got bug id: %s', bug_id)
45             query = debianbts.get_status(bug_id)
46             if len(query) == 1:
47                 bug = query[0]
48                 url = debianbts.BTS_URL + bug_id
49                 bugs.append({'id': bug_id,
50                              'package': bug.package,
51                              'summary': bug.subject,
52                              'url': url})
53             else:
54                 self.log.warning('Wrong bug number "%s"?', bug_id)
55                 bugs.append({'id': bug_id})
56         for bug in bugs:
57             if len(bug) == 1:
58                 message = 'Invalid bug id: {id}'.format(**bug)
59                 self.reply(msg, message)
60             else:
61                 message = {'mhtml': '<a href="%(url)s">#%(id)s</a>: %(package)s “ %(summary)s ”' % bug,
62                            'mbody': '#%(id)s: %(package)s “ %(summary)s ” %(url)s' % bug}
63                 self.reply(msg, message)
64
65     @botcmd
66     def archive(self, rcv, args):
67         """Fetch pkg info from the archive:
68
69         !archive pkg-name : Returns package versions (by suite)
70         """
71         if not args:
72             return
73         if len(args) > 1:
74             self.log.info('more than one packages provided')
75         pkg = Archive.re_pkg.match(args[0])
76         if not pkg:
77             msg = 'Wrong package name format re: "{}"'.format(
78                 Archive.re_pkg.pattern)
79             self.reply(rcv, msg)
80             return
81         pkg_name = pkg.groupdict('package').get('package')
82         info = get_pkg(pkg_name)
83         if not info:
84             self.reply(rcv, 'pakage not found')
85             return
86         messages = []
87         suites_av = set(info.keys() & {'stable', 'testing', 'unstable',
88                                        f'{Archive.stable_codename}-backports'})
89         for suite in sorted(suites_av):
90             component = '/'.join({v['component'] for v in info[suite].values()})
91             versions = '/'.join(info[suite].keys())
92             messages.append(f'{suite:16}: {versions} {component}')
93         msg = '\n'.join(messages)
94         self.reply(rcv, msg)