def message(self, bot, channel, user, message, type): ''' Called when bot "hears" a message. ''' if not channel: return # Only interested in channel messages nick = irc.get_nick(user) # Collect messages pertaining to this user messages = [] ; files = [] for recp in self._list_recipients(): if fnmatch.fnmatch(nick, recp): # Read messages from file file = self._get_filename(recp) with open(file) as f: messages.extend(json.load(f)) files.append(file) # Format and send them msgs = [] for message in messages: msg_time = datetime.fromtimestamp(message['timestamp']) msg_time = msg_time.strftime("%Y-%m-%d %H:%M:%S") msg = "%s <%s> %s: %s" % (msg_time, message['from'], nick, message['message']) msgs.append(msg) irc.say(bot, channel, msgs) # Delete files for file in files: os.unlink(file) # Log delivery log_file = os.path.join(self.dir, "delivery.log") with open(log_file, 'a') as log: log.writelines(m + "\n" for m in msgs)
def command(self, bot, channel, user, cmd, args): """Handles the .greet command.""" # Remember the greeting nick = irc.get_nick(user) self.greets[nick] = str(args) if args else None self._save() # Serve a response return "Greeting %s for user '%s'" % ('set' if args else 'reset', nick)
def join(self, bot, channel, user): ''' Called when user joins a channel. ''' # Retrieve the nick nick = irc.get_nick(user) if nick == bot.nickname: return # Only interested in others joining # Check if we have greeting and serve it greet = self.greets.get(nick) if greet: irc.say(bot, channel, greet)
def _reply(self, to, response): """Adorn the response to user's command with a prefix containing the user's nick, in the typical IRC fashion. """ if isinstance(response, basestring): response = [response] if not response: return response response[0] = u": ".join((irc.get_nick(to), response[0])) return response
def command(self, bot, user, cmd, args): ''' Called when user issues a command. ''' if cmd != 'greet': return # Only interested in this command # Remember the greeting nick = irc.get_nick(user) self.greets[nick] = str(args) if args else None self._save() # Serve a response return "Greeting %s for user '%s'" % ('set' if args else 'reset', nick)
def seen_plugin(bot, event, **kwargs): ''' Main function. Plugin is implemented as a function because it eliminates some redundancy in recording user's activity. ''' if event in ('init', 'tick'): return if event == 'command': # .seen command user_arg = kwargs['args'].strip() if user_arg == irc.get_nick(kwargs['user']): return "You might wanna look in the mirror..." if user_arg == bot.nickname: return "Looking for me?" return handle_seen_command(user_arg) track_activity(event, **kwargs)
def record_user_activity(user, channel, text): ''' Records activity represented by given text. ''' activity = {} user_file = os.path.join(storage_dir, irc.get_nick(user)) if os.path.exists(user_file): with open(user_file, 'r') as f: try: activity = json.load(f) except ValueError: pass channel = channel or GLOBAL_CHANNEL activity[channel] = {'text': text, 'timestamp': time()} with open(user_file, 'w') as f: json.dump(activity, f)
def message(self, bot, channel, user, message, type): """Called when we hear a message being spoken.""" if not channel or channel == '*': return # if message comes from server if '!' not in user: return if irc.get_nick(user) == bot.nickname: return url_match = URL_RE.search(message) if url_match: result = self._resolve_url(url_match.group(0)) if result: type_, title = result irc.say(bot, channel, u"[%s] %s" % (type_, title))
def command(self, bot, user, cmd, args): ''' Called when user issues a command. ''' if cmd != 'msg': return nick = irc.get_nick(user) # Forbid sending messages to the bot itself if nick == bot.nickname: return "I'm here, y'know." # Get recipient and message from arguments try: recipient, message = re.split(r"\s+", args, 1) except ValueError: message = None if not message: return "Message shall not be empty." # Store it self._store_message(nick, recipient, message) return "I will notify %s should they appear." % recipient
def command(self, bot, channel, user, cmd, args): """Called when user issues the .msg command.""" nick = irc.get_nick(user) # Forbid sending messages to the bot itself if nick == bot.nickname: return "I'm here, y'know." # Get recipient and message from arguments try: recipient, message = args.split(None, 1) except ValueError: message = None if not message: return "Message shall not be empty." # Store it self._store_message(nick, recipient, message) return "I will notify %s should they appear." % recipient
def track_activity(event, **kwargs): ''' Tracks activity of some user, recording it for further retrieving with .seen command. ''' # retrieve user(s) for this activity events_to_user_refs = { 'kick': ('kicker', 'kickee'), 'nick': ('old', 'new'), } user_refs = events_to_user_refs.get(event, ('user',)) users = filter(None, map(kwargs.get, user_refs)) if users: # replace full user IDs with just their nicks for ref in user_refs: kwargs[ref] = irc.get_nick(kwargs[ref]) text = format_activity_text(event, **kwargs) channel = kwargs.get('channel') for user in users: record_user_activity(user, channel, text)