def recurse(bot, user, title, channel): #Kinda hacky method of preventing infinite loops (only the original url has a user attribute) #Mainly a problem with Twitter posts that have an image attached, since they essentially link to themselves global recursecount global norepeat if user is "": recursecount = recursecount + 1 if recursecount == 3: log.debug("Infinite (or just annoyingly long) recursion detected; aborting!") return else: recursecount = 0 urls = pyfiurl.grab(title.encode("UTF-8")) if urls: log.info(urls) user = "" for url in urls: if url in norepeat: break else: norepeat.append(url) msg = url handle_url(bot, user, channel, url, msg) else: norepeat = []
def privmsg(self, user, channel, msg): """This will get called when the bot receives a message. @param user: nick!user@host @param channel: Channel where the message originated from @param msg: The actual message """ channel = channel.lower() lnick = self.nickname.lower() if channel == lnick: # bot private commands (privcommand) if msg.startswith(self.factory.config['commandchar']): cmnd = msg[len(self.factory.config['commandchar']):] self._privcommand(user, self.factory.getNick(user), cmnd) else: # channel public commands (command) if msg.startswith(self.factory.config['commandchar']): cmnd = msg[len(self.factory.config['commandchar']):] self._command(user, channel, cmnd) # run URL handlers urls = pyfiurl.grab(msg) if urls: for url in urls: self._runhandler("url", user, channel, url, msg) # run privmsg handlers self._runhandler("privmsg", user, channel, msg)
def recurse(bot, user, title, channel): #Kinda hacky method of preventing infinite loops (only the original url has a user attribute) #Mainly a problem with Twitter posts that have an image attached, since they essentially link to themselves global recursecount global norepeat if user is "": recursecount = recursecount + 1 if recursecount == 3: bot.say( channel, "Infinite (or just annoyingly long) recursion detected; aborting!" ) return else: recursecount = 0 urls = pyfiurl.grab(title.encode("UTF-8")) if urls: user = "" for url in urls: if url in norepeat: break else: norepeat.append(url) msg = url handle_url(bot, user, channel, url, msg) else: norepeat = [] return None
def writeToLog(date, user ,channel, msg): with open(channel[1:] + ".log", "a") as logfile: urls = pyfiurl.grab(msg) if(urls): #add <a href=""> to urls for url in urls: parts = msg.partition(url) msg = "".join(cgi.escape(parts[0]) + "<a href='"+parts[1]+"' target='_blank'>" + cgi.escape(parts[1]) + "</a>" + cgi.escape(parts[2])) else: msg = cgi.escape(msg) logfile.write("<p><span class='timestamp'>[" + timestamp(date) + "]</span> " + "<span class='nick'>" + cgi.escape("<" + user + ">") + "</span> <span class='msg'>" + msg + "</span></p>")
def privmsg(self, user, channel, msg): """This will get called when the bot receives a message. @param user: nick!user@host @param channel: Channel where the message originated from @param msg: The actual message """ channel = channel.lower() is_command = False lmsg = msg.lower() lnick = self.nickname.lower() nickl = len(lnick) #self.log("<%s|%s> %s" % (self.getNick(user), channel, msg)) if channel == lnick: # Turn private queries into a format we can understand if not msg.startswith(self.cmdchar): msg = self.cmdchar + msg elif lmsg.startswith(lnick): msg = self.cmdchar + msg[nickl:].lstrip() elif lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.cmdchar + msg[nickl + 1:].lstrip() else: # Turn 'nick:' prefixes into self.cmdchar prefixes if lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.cmdchar + msg[len(self.nickname) + 1:].lstrip() reply = (channel == lnick) and user or channel #print 'cmdchar: %s' % self.cmdchar if msg.startswith(self.cmdchar) or self.cmdchar == '': cmnd = msg[len(self.cmdchar):] is_command = self._command(user, reply, cmnd) # run privmsg handlers self._runhandler("privmsg", user, reply, msg) # run URL handlers urls = pyfiurl.grab(msg) if urls: for url in urls: self._runhandler("url", user, reply, url, msg) # last but not least, log the message. if is_command == False: self.channel_log.append(ChannelLogItem(channel,user,msg,datetime.now()))
def privmsg(self, user, channel, msg): """This will get called when the bot receives a message. @param user: nick!user@host @param channel: Channel where the message originated from @param msg: The actual message """ if self.factory.isIgnored(user): return channel = channel.lower() lmsg = msg.lower() lnick = self.nickname.lower() nickl = len(lnick) #self.log("<%s|%s> %s" % (self.getNick(user), channel, msg)) if channel == lnick: # Turn private queries into a format we can understand if not msg.startswith(self.CMDCHAR): msg = self.CMDCHAR + msg elif lmsg.startswith(lnick): msg = self.CMDCHAR + msg[nickl:].lstrip() elif lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.CMDCHAR + msg[nickl + 1:].lstrip() else: # Turn 'nick:' prefixes into self.CMDCHAR prefixes if lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.CMDCHAR + msg[len(self.nickname) + 1:].lstrip() reply = (channel == lnick) and user or channel if msg.startswith(self.CMDCHAR): cmnd = msg[len(self.CMDCHAR):] self._command(user, reply, cmnd) # run privmsg handlers self._runhandler("privmsg", user, reply, msg) # run URL handlers urls = pyfiurl.grab(msg) if urls: for url in urls: self._runhandler("url", user, reply, url, msg)
def showTwit(bot, where, twquery): for q in twquery: # set localtime to Buenos Aires, change to yours bsas = pytz.timezone('America/Argentina/Buenos_Aires') if type(q).__name__ == 'Status': twuser = q.user.screen_name.encode('utf-8') else: twuser = q.from_user.encode('utf-8') bot.say(where, '@%s: %s (%s)' % ( twuser, q.text.encode('utf-8'), q.created_at.replace( tzinfo=pytz.utc).astimezone(bsas).strftime("%H:%M - %d.%m.%Y") )) urls = pyfiurl.grab(q.text.encode('utf-8')) if urls: for url in urls: bot._runhandler("url", twuser, where, url, ".")
def privmsg(self, user, channel, msg): """This will get called when the bot receives a message. @param user: nick!user@host @param channel: Channel where the message originated from @param msg: The actual message """ channel = channel.lower() lmsg = msg.lower() lnick = self.nickname.lower() nickl = len(lnick) if channel == lnick: # Turn private queries into a format we can understand if not msg.startswith(self.cmdchar): msg = self.cmdchar + msg elif lmsg.startswith(lnick): msg = self.cmdchar + msg[nickl:].lstrip() elif (lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation): msg = self.cmdchar + msg[nickl + 1:].lstrip() else: # Turn 'nick:' prefixes into self.cmdchar prefixes if (lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation): msg = self.cmdchar + msg[len(self.nickname) + 1:].lstrip() reply = (channel == lnick) and user or channel if msg.startswith(self.cmdchar): cmnd = msg[len(self.cmdchar):] self._command(user, reply, cmnd) # Run privmsg handlers self._runhandler("privmsg", user, reply, self.factory.to_unicode(msg)) # run URL handlers urls = pyfiurl.grab(msg) if urls: for url in urls: self._runhandler("url", user, reply, url, self.factory.to_unicode(msg))
def privmsg(self, user, channel, msg): """This will get called when the bot receives a message. @param user: nick!user@host @param channel: Channel where the message originated from @param msg: The actual message """ # Ignoring privmsgs from ignored users nick = self._getNick(user) for n in self.factory.config['networks'][self.network.alias]['ignored_nicks']: if n.lower() == nick.lower(): return channel = channel.lower() lmsg = msg.lower() lnick = self.nickname.lower() nickl = len(lnick) if channel == lnick: # Turn private queries into a format we can understand if not msg.startswith(self.CMDCHAR): msg = self.CMDCHAR + msg elif lmsg.startswith(lnick): msg = self.CMDCHAR + msg[nickl:].lstrip() elif lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.CMDCHAR + msg[nickl + 1:].lstrip() else: # Turn 'nick:' prefixes into self.CMDCHAR prefixes if lmsg.startswith(lnick) and len(lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.CMDCHAR + msg[len(self.nickname) + 1:].lstrip() reply = (channel == lnick) and user or channel if msg.startswith(self.CMDCHAR): cmnd = msg[len(self.CMDCHAR):] self._command(user, reply, cmnd) # Run privmsg handlers self._runhandler("privmsg", user, reply, self.factory.to_unicode(msg)) # run URL handlers urls = pyfiurl.grab(msg) if urls: for url in urls: self._runhandler("url", user, reply, url, msg) else: # Word stats functionality if not msg.startswith(self.CMDCHAR): stripped_msg = msg.strip() #TODO kiva tietää service goes here #kiva_tietaa service if self.kt == False: dt = datetime.now() if dt.hour == 3: luku = randint(1,10) if len(stripped_msg) > 70 and len(stripped_msg) <= 133 and luku <= 8: self._runhandler("kivatietaa", user, reply, stripped_msg) self.kt = True word_count = stripped_msg.split(" ") if channel in self.words: if nick in self.words[channel]: # add word count to existing entry self.words[channel][nick] += len(word_count) else: # new entry - begin counting words self.words[channel][nick] = len(word_count) else: self.words[channel] = {} self.words[channel][nick] = len(word_count)
def privmsg(self, user, channel, msg): """This will get called when the bot receives a message. @param user: nick!user@host @param channel: Channel where the message originated from @param msg: The actual message """ # Ignoring privmsgs from ignored users nick = self._getNick(user) for n in self.factory.config['networks'][ self.network.alias]['ignored_nicks']: if n.lower() == nick.lower(): return channel = channel.lower() lmsg = msg.lower() lnick = self.nickname.lower() nickl = len(lnick) if channel == lnick: # Turn private queries into a format we can understand if not msg.startswith(self.CMDCHAR): msg = self.CMDCHAR + msg elif lmsg.startswith(lnick): msg = self.CMDCHAR + msg[nickl:].lstrip() elif lmsg.startswith(lnick) and len( lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.CMDCHAR + msg[nickl + 1:].lstrip() else: # Turn 'nick:' prefixes into self.CMDCHAR prefixes if lmsg.startswith(lnick) and len( lmsg) > nickl and lmsg[nickl] in string.punctuation: msg = self.CMDCHAR + msg[len(self.nickname) + 1:].lstrip() reply = (channel == lnick) and user or channel if msg.startswith(self.CMDCHAR): cmnd = msg[len(self.CMDCHAR):] self._command(user, reply, cmnd) # Run privmsg handlers self._runhandler("privmsg", user, reply, self.factory.to_unicode(msg)) # run URL handlers urls = pyfiurl.grab(msg) if urls: for url in urls: self._runhandler("url", user, reply, url, msg) else: # Word stats functionality if not msg.startswith(self.CMDCHAR): stripped_msg = msg.strip() #TODO kiva tietää service goes here #kiva_tietaa service if self.kt == False: dt = datetime.now() if dt.hour == 3: luku = randint(1, 10) if len(stripped_msg) > 70 and len( stripped_msg) <= 133 and luku <= 8: self._runhandler("kivatietaa", user, reply, stripped_msg) self.kt = True word_count = stripped_msg.split(" ") if channel in self.words: if nick in self.words[channel]: # add word count to existing entry self.words[channel][nick] += len(word_count) else: # new entry - begin counting words self.words[channel][nick] = len(word_count) else: self.words[channel] = {} self.words[channel][nick] = len(word_count)
def handle_privmsg(bot, user, channel, msg): if not channel.lower() == bot.nickname.lower() and not msg.startswith("!") and not pyfiurl.grab(msg): f = open("logger.txt", "a") if ":" in msg: msg = msg[(msg.find(":")) + 1 :] f.write(msg.strip() + "\n") bot.log("%s" % msg.strip()) f.close()