]> kaliko git repositories - sid.git/blob - sid/bts.py
Fixed tell "purge" command
[sid.git] / sid / bts.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010, 2011 Anaël Verrier <elghinn@free.fr>
4 # Copyright (C) 2015 kaliko <kaliko@azylum.org>
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, version 3 only.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 from re import compile as re_compile
20
21 import debianbts
22
23 from .plugin import Plugin, botcmd
24
25
26 class Bugs(Plugin):
27     """Gets bugs info from the BTS
28     """
29     re_bugs = re_compile(r'(?<=#)(\d{6,7})')
30
31     def __init__(self, bot):
32         Plugin.__init__(self, bot)
33         bot.add_event_handler("muc::%s::message" % 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 bugs(self, message, args):
67         """ intercepts bugs number in any message, looking for string like #629234
68         !bugs : nothing yet.
69         """
70         return None