~p1tr-dev/p1tr/main

« back to all changes in this revision

Viewing changes to plugins/launchpad.py

  • Committer: Siegfried-Angel Gevatter Pujals
  • Date: 2010-12-24 16:58:33 UTC
  • Revision ID: rainct@ubuntu.com-20101224165833-s1s69nagws5bmeom
Launchpad: Add protection against loops.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""
19
19
 
20
20
import re
 
21
import time
21
22
import urllib2
22
23
 
23
24
from lib.plugin import Plugin
48
49
            self._listen = False
49
50
        else:
50
51
            self._listen = True
 
52
        
 
53
        # Loop protection
 
54
        # Format: {'channel_name': [(timestamp, bugid), (timestamp, bugid)]}
 
55
        self._lastbugs = {}
51
56
    
52
57
    def lp(self, msg):
53
58
        
107
112
            return _('Launchpad bug #%d in %s: "%s" (%s, %s).') % \
108
113
                (bugnumber, data.task, data.title, data.status, data.importance)
109
114
    
 
115
    def _is_duplicated(self, channel, bugnumber):
 
116
        if not channel in self._lastbugs:
 
117
            self._lastbugs[channel] = {}
 
118
        lim = time.time() - 120 # only repeat bug information every two minutes
 
119
                                # the main reason for this is to avoid madness
 
120
                                # when two bots are in the channel
 
121
        if bugnumber not in self._lastbugs[channel] or \
 
122
        self._lastbugs[channel][bugnumber] < lim:
 
123
            self._lastbugs[channel][bugnumber] = time.time()
 
124
            return False
 
125
        return True
 
126
    
110
127
    def _bug_info(self, bot, string, channel):
111
128
        """Parses a line looking for bug numbers, and sends information about
112
129
        them."""
113
130
        for bugnumber in set(self.bugrule.findall(string)):
 
131
            direct_request = string.lower().startswith(bot.nickname.lower()) \
 
132
                and string[len(bot.nickname):][0] in (' ', ',', ':')
 
133
            if self._is_duplicated(channel, bugnumber) and not direct_request:
 
134
                continue
114
135
            bug = self._get_bug_info(int(bugnumber), bot, channel)
115
136
            if bug:
116
137
                bot.msg(channel, bug + ' https://launchpad.net/bugs/%d' % \
122
143
        for bugnumber in set(self.bugurlrule.findall(data)):
123
144
            if not 'bugs' in data:
124
145
                continue
 
146
            if self._is_duplicated(channel, bugnumber):
 
147
                continue
125
148
            bug = self._get_bug_info(int(bugnumber), bot, channel)
126
149
            if bug:
127
150
                bot.msg(channel, bug)