def __init__(self, base, scheme=None): """Initialize bot""" self.base = base self.log = get_logger('madcow', unique=False, stream=sys.stdout) self.colorlib = ColorLib(scheme) self.cached_nick = None self.running = False self.ignore_list = [nick.lower() for nick in settings.IGNORE_NICKS] # set encoding set_encoding(settings.ENCODING) self.log.info('Preferred encoding set to %s', get_encoding()) # create admin instance self.admin = Admin(self) # load modules self.modules = Modules(self, 'modules', settings.MODULES, supercedes=settings.MODULE_SUPERCEDES) self.tasks = Modules(self, 'tasks', settings.TASKS, supercedes=settings.TASK_SUPERCEDES) self.usage_lines = self.modules.help + self.tasks.help self.usage_lines.append(u'help - this screen') self.usage_lines.append(u'version - get bot version') # signal handlers if signal: signal.signal(signal.SIGHUP, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) # initialize threads self.request_queue = queue.Queue() self.response_queue = queue.Queue() self.lock = threading.RLock() self.ignore_res = [] if settings.IGNORE_REGEX: for pattern in settings.IGNORE_REGEX: try: regex = re.compile(pattern, re.I) except Exception, error: self.log.warn('%r pattern did not compile', pattern) continue self.ignore_res.append(regex)
class Main(Module): _regex = u'^\s*roll\s+(\d*)d(\d+)\s*(?:([-+])(\d+))?\s*$' pattern = re.compile(_regex, re.I) allow_threading = False require_addressing = True help = u'roll <dice>d<sides>[+-<modifier>] - roll dice of the specified size' def __init__(self, madcow=None): if madcow is not None: self.colorlib = madcow.colorlib self.madcow = madcow else: self.colorlib = ColorLib(u'ansi') def colour(self, text, color): return self.colorlib.get_color(color, text=text) def report(self, rolls): rolls = str(rolls) if len(rolls) > 300: return rolls[:300] + "...]" return rolls def response(self, nick, args, kwargs): dice,sides,sign,mod = args if dice == "": dice = "1" if int(dice) > 10240: return u"Sorry, I don't have that many dice." rolls = [random.randint(1, int(sides)) for die in range(0, int(dice))] total = reduce(lambda x,y: x+y, rolls) if mod: if sign == "+": mod = int(mod) else: mod = -int(mod) return u'%s: \x02%d\x02 (%sd%s%s%d = %s)' % (nick, total+mod, dice, sides, sign, mod, self.report(rolls)) else: return u'%s: \x02%d\x02 (%sd%s = %s)' % (nick, total, dice, sides, self.report(rolls))
class Main(Module): pattern = re.compile(r'^\s*(election|ev)\s*$', re.I) help = u'ev - current election 2008 vote prediction' _baseurl = u'http://www.electoral-vote.com/' _score_re = re.compile(r'<td class="score">(.*?)</td>', re.DOTALL) _dem_re = re.compile(r'<span class="dem">(.*?)\s+(\d+)') _gop_re = re.compile(r'<span class="gop">(.*?)\s+(\d+)') _tie_re = re.compile(r'(Ties)\s+(\d+)') def init(self): if self.madcow is None: self.colorlib = ColorLib('ansi') else: self.colorlib = self.madcow.colorlib def colorize(self, color, key, val): if key == '_SEN!D': key = 'Democrats' elif key == '_SEN!R': key = 'Republicans' key = self.colorlib.get_color(color, text=key) return u'%s: %s' % (key, val) def response(self, nick, args, kwargs): page = geturl(self._baseurl) try: score = self._score_re.search(page).group(1) dem = self._dem_re.search(score).groups() gop = self._gop_re.search(score).groups() # XXX diebold patch :D #dem, gop = (dem[0], gop[1]), (gop[0], dem[1]) tie = None try: tie = self._tie_re.search(score).groups() except AttributeError: pass except AttributeError: raise Exception(u"couldn't parse page") output = [self.colorize(u'blue', *dem), self.colorize(u'red', *gop)] if tie: output.append(self.colorize(u'white', *tie)) return u'%s: Projected Senate Seats 2010: %s' % (nick, u', '.join(output))
class Main(Module): _regex = u'^\s*(replay|tell me what I missed)\.?\s*$' #_regex = u'^\s*replay\s*$' pattern = re.compile(_regex, re.I) allow_threading = False require_addressing = True help = u'replay - replay all conversation since you last left the channel' def __init__(self, madcow=None): if madcow is not None: self.colorlib = madcow.colorlib self.madcow = madcow else: self.colorlib = ColorLib(u'ansi') def colour(self, text, color): return self.colorlib.get_color(color, text=text) def response(self, nick, args, kwargs): return "I don't know how to do that...yet."
class Main(Module): pattern = re.compile(r'^\s*(election|ev)\s*$', re.I) help = u'ev - current election 2008 vote prediction' baseurl = u'http://www.electoral-vote.com/' def init(self): if self.madcow is None: self.colorlib = ColorLib('ansi') else: self.colorlib = self.madcow.colorlib def colorize(self, color, key, val): return u'%s: %s' % (key, val) def render(self, node): pass def response(self, nick, args, kwargs): soup = getsoup(self.baseurl) out = [] for box in soup.find('div', 'score-box').findAll('div', 'box'): score = [] for key in 'name', 'score': val = strip_html(box.find('span', key).renderContents()).replace(u'\xa0', u'').strip() if key == 'name': if val == u'Obama': color = 'blue' elif val == 'Romney': color = 'red' else: color = None if color: val = self.colorlib.get_color(color, text=val) if val: score.append(val) if score: out.append(u'%s: %s' % tuple(score)) return u'%s: %s' % (nick, u', '.join(out))
class Main(Module): _allow = u'-?(?:[0-9.]+j?|pi|e)' _regex = u'^\s*roll\s+(%s?)d(%s)\s*$' % (_allow, _allow) pattern = re.compile(_regex, re.I) allow_threading = False require_addressing = True help = u'roll [<numdice>d<sides>] - roll die of the specified size' def __init__(self, madcow=None): if madcow is not None: self.colorlib = madcow.colorlib else: self.colorlib = ColorLib(u'ansi') def roll(self, min, max): if isinstance((min * max), (float, complex)): return random.uniform(min, max) else: return random.randint(min, max) def normalize(self, val): try: val = val.lower() if val == u'pi': val = math.pi elif val == u'e': val = math.e elif val.endswith(u'j'): val = complex(val) elif u'.' in val: val = float(val) else: val = int(val) except: val = 1 return val def colorize(self, text, color): return self.colorlib.get_color(color, text=text) def response(self, nick, args, kwargs): num_dice = self.normalize(args[0]) sides = self.normalize(args[1]) if sides == 0 or num_dice == 0: return u'GOOD JOB, UNIVERSE %s' % self.colorize(u'EXPLODES', u'red') if sides == 1 and num_dice == 1: return u'CHEATING DETECTED, YOU %s' % self.colorize(u'DIE', u'red') min = num_dice max = num_dice * sides saving_throw = self.roll(min, max) save_versus = self.roll(min, max) try: if saving_throw >= save_versus: result = self.colorize(u'LIVES', u'green') else: result = self.colorize(u'DIES', u'red') except: result = self.colorize(u'IS TOO COMPLEX', u'yellow') return u'%s rolls %s, needs %s, %s %s' % (nick, saving_throw, save_versus, nick, result)
def __init__(self, madcow=None): if madcow is not None: self.colorlib = madcow.colorlib else: self.colorlib = ColorLib(u'ansi')
def init(self): if self.madcow is None: self.colorlib = ColorLib('ansi') else: self.colorlib = self.madcow.colorlib