def translate(msg): ''' @param msg: Message to translate @summary: Translates a query into destination language using Microsoft Translate @attention: TODO ''' try: req = urllib2.Request('http://translate.google.com/#auto|en|%s.' % urllib.quote(msg)) req.add_header( 'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5' ) response = urllib2.urlopen(req) page = response.read() Log.write(page) response.close() soup = BeautifulSoup(page) trans = ''.join( soup.find('span', attrs={ 'id': 'result' }).findAll(text=True)) return ("%s -> %s" % (msg, trans)).encode('utf-8') except Exception: Log.error() return None
def arma_recover(self, channel, auto_op=True): ''' @param auto_op: If set to true ensures that the bot is +o'd first @summary: Recovers from the armageddon by unbanning all the people previously banned ''' if self._resetlist.has_key(channel) and len(self._resetlist[channel]): if self._bot.has_flag(channel, 'o'): self._recover = False for u in self._resetlist[channel]: Log.write('Unban %s' % u, 'D') self._bot.unban(channel, u, False) self._bot.deop(channel, self._bot.nick) self._bot.flag(channel, 'o', False) elif auto_op: self._recover = True self._bot.op(channel, self._bot.nick)
def log(self, channel, msg): if self.is_enabled(): now = datetime.utcnow() dirpath = self.parse_template(self._dir, channel, now) filename = os.path.join(dirpath , self.parse_template(self._format, channel, now)) #d = now - self._date if self._date.date() != now.date() or (self._file and filename != self._file.name): self._date = now self.close() if not os.path.exists(dirpath): os.makedirs(dirpath) self._file = open(filename, "a") Log.write('Opening Log: ' + self._file.name) self._buffer += datetime.strftime(now, "[%H:%M:%S] ") + msg + '\n' if len(self._buffer) > self._bufferlen: self._file.write(self._buffer) self._file.flush() self._buffer = ""
def translate(msg): ''' @param msg: Message to translate @summary: Translates a query into destination language using Microsoft Translate @attention: TODO ''' try: req = urllib2.Request('http://translate.google.com/#auto|en|%s.' % urllib.quote(msg)) req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5') response = urllib2.urlopen(req) page = response.read() Log.write(page) response.close() soup = BeautifulSoup(page) trans = ''.join(soup.find('span', attrs={'id': 'result'}).findAll(text=True)) return ("%s -> %s" % (msg, trans)).encode('utf-8') except Exception: Log.error() return None
def lynch_player(self, channel): ''' @summary: Lynches the top suspected player ''' m = 0 p = None for k,v in self.lynch_vote.items(): # Get top voted player if v > m: p = k m = v if p: v = self.users.pop(p) # Remove from game Log.write(self.users) if v: self.wolves -= 1 Thread(target=self.callback, args=('%s was lynched! Justice is served. %s is dead.' % (p, p),), name='callback').start() else: self.villagers -= 1 Thread(target=self.callback, args=('%s was lynched! Sinners all of you! The blood of innocent %s is on your hands.' % (p, p),), name='callback').start() else: Thread(target=self.callback, args=("Come forth people, else the beast shall devour you all!",), name='callback').start()
def armageddon(self, channel, stage=0, nicks=None): ''' @param build: True if called for first time, from stage 0. False otherwise. @summary: Does what it says. Armageddon. Kickbans all users except the ones in whitelist ''' self._state = stage + 1 if stage == 0: # Stage 1, Prepare usernames self._nicklist[channel] = nicks self._bot.request_memberlist(channel) elif stage == 1: # Stage 2, Prepare userhosts if self._armadict[channel].has_key(self._bot.nick): self._armadict[channel].pop(self._bot.nick) # Remove bot's nick from the list if len(self._armadict[channel]): self._bot.op(channel, self._bot.nick) else: self.reset() elif stage == 2: # Final Stage, kickban everyone except in whitelist self._resetlist[channel] = [] regx = None if len(self._whitelist): regx = re.compile(r'^%s' % '|'.join(self._whitelist)) # Set whitelist #regx = re.compile('|'.join(self._whitelist())) # Set whitelist for u in self._armadict[channel].values(): if regx is None or regx.match(u.host) is None: Log.write('armageddon-kickban : %s : %s %s' % (channel, u.nick, u.host)) self._resetlist[channel].append('*!*@' + u.host) self._bot.ban(channel, '*!*@' + u.host, False) self._bot.kick(channel, u.nick, 'ARMAGEDDON', False) else: Log.write('Saved %s %s' % (u.nick, u.host)) # Reset vars self._bot.deop(channel, self._bot.nick) self._bot.flag(channel, 'o', False) self.reset()