#!/usr/bin/env python # -*- coding: utf-8 from zope.interface import implements from core.plugins.interface import IActionHandler from core.plugins.manager import plugin_manager class Echo(object): implements(IActionHandler) name = 'echo' def accepts_action(self, action): return action in ['privmsg', ] def handle_action(self, protocol, action, user, message): nick = user.split("!", 1)[0] protocol.say(protocol.channel, "%s: %s" % (nick, message)) echo = Echo() plugin_manager.register(echo)
from core.plugins.manager import plugin_manager class TitleFetcher(object): implements(IActionHandler) rx_url = re.compile(r'\b(http://\S+|www\.\S+)\b') rx_title = re.compile(r'<title>(.*?)</title>') name = 'plugin.title_fetcher' def accepts_action(self, action): return action == 'privmsg' def handle_action(self, protocol, action, user, message): "write a message to the file" for url in self.rx_url.findall(message): getPage(url).addCallback(self.send_page_title, protocol) def send_page_title(self, page, protocol): rx = self.rx_title.finditer(page) try: title = rx.next().groups() protocol.say(protocol.channel, 'title: "%s"' % title[0]) except StopIteration: pass plugin_manager.register(TitleFetcher())
class MessageLogger(object): implements(IActionHandler, IInitialize, IFinalize) def __init__(self, path): self.log_path = path self.name = "message_logger" def initialize(self): self.file = open(self.log_path, "a") def finalize(self): self.file.close() def accepts_action(self, action): return action in ['privmsg', 'ping', 'join'] def handle_action(self, protocol, action, user, message): "write a message to the file" timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time())) nick = user.split("!", 1)[0] self.file.write('(#%s) %s %s: %s\n' % \ (protocol.channel, timestamp, nick, message)) self.file.flush() logger = MessageLogger("/tmp/message_logger.log") plugin_manager.register(logger)
if self.last_match == match: return # dont talk too often if self.last_message is not None: diff = datetime.datetime.now() - self.last_message if (diff.seconds < SPEAK_INTERVAL): print "too early to reply" return print "match: %s" % match urls = self.image_dictionary[match] # pick a url from the list at random url = choice(urls) ret_msg = "%s -> %s" % (match, url) print "[%s] message %s from %s triggered %s" % ( datetime.datetime.now().strftime('%H:%M'), orig_message, user, ret_msg ) protocol.say(protocol.channel, ret_msg) self.last_message = datetime.datetime.now() self.last_match = match reaction = Reaction('reaction', 'data/reactions.txt') plugin_manager.register(reaction)
for rx, rx_str, reason in self._nasty_rx: if rx_str.startswith(rx_filter): response = 'rx: %s reason: %s' % (rx_str, reason) protocol.say(protocol.channel, response) elif message.startswith(CMD_UNBAN) and user in self.admins: # find matching filter and remove them rx_filter = message[len(CMD_UNBAN):] for i, (rx, rx_str, reason) in enumerate(self._nasty_rx): if rx_str == rx_filter: self._nasty_rx.pop(i) protocol.say(protocol.channel, 'unban: %s' % rx_str) else: # check if message contains banned word and kick user if does rx, reason = self.find_nasty_word(message) if rx: nick = user.split('!', 1)[0] protocol.kick(protocol.channel, nick, reason) def find_nasty_word(self, message): for rx, rx_str, reason in self._nasty_rx: if rx.search(message): return (rx, reason) return (None, None) admins = set([ '[email protected]', ]) nasty_filter = NastyRegexpFilter('myfilter', admins) plugin_manager.register(nasty_filter)
feed_list = loaded['feeds_list'] for feed in feed_list: if feed in self.feeds: self.feeds[feed]['last_update'] = feed_list[feed]['last_update'] else: self.feeds[feed] = feed_list[feed] def dump(self): return pickle.dumps({ 'feeds_list': self.feeds, 'feeds_battery': self.feeds_battery }) def periodic_handler(self, protocols): self.fetch() for i in self.feeds_battery[:self.max_messages]: feed = self.feeds_battery.pop(0) for protocol in protocols.filter(channels=['test_bot', ]): protocol.say(protocol.channel, feed.encode('utf-8')) feed_reader = FeedReader("pyrss", 60 * 15) # set feeds that object should fetch feed_reader.set_feeds([ ('http://code.activestate.com/feeds/langs/python/', 'ActiveState Code: %(title)s'), ]) plugin_manager.register(feed_reader)
def check_messages(self, protocol, user): nick = user.split("!", 1)[0] if nick in self.mem: protocol.say(protocol.channel, " # ".join((MSG_FORMAT % m for m in self.mem[nick]))) del self.mem[nick] def save_message(self, user, message): if not message.startswith(MSG_COMMAND): return message = message[len(MSG_COMMAND):].strip() send_to, message = message.split(' ', 1) author = user.split("!", 1)[0] if send_to == author: # do not save self sended messages return if not send_to in self.mem: self.mem[send_to] = [] self.mem[send_to].append( { "author": author, "date": datetime.datetime.now(), "to": send_to, "message": message, }) reminder = Reminder(name='reminder') plugin_manager.register(reminder)