def rm_command(command, server): """Execute the )rm command.""" user = command.event.name if len(command.args) > 0: # Delete all messages if "all" in command.args: server.persistent_data.set("messages.%s.inbox" % user, []) return # Delete all listed messages if server.persistent_data.get("messages.%s.inbox" % user): messages = server.persistent_data.get("messages.%s.inbox" % user) for arg in command.args: if not arg == "": try: server.persistent_data.remove( "messages.%s.inbox" % user, messages[int(arg)]) except ValueError: number_error_event = irc.Irc_event( "PRIVMSG", user, "\"%s\" is no valid number." % arg) server.send_event(number_error_event) except IndexError: index_error_event = irc.Irc_event( "PRIVMSG", user, "%s is no valid index." % arg) server.send_event(index_error_event)
def roll_command(command, server): """Execute the )roll command.""" channel = command.event.args[ 0] if not command.event.args[0] == server.nick else command.event.name # Roll a number within the given range if len(command.args) > 0: for arg in command.args: if not arg == "": try: number = random.randint(0, int(arg)) roll_event = irc.Irc_event("PRIVMSG", channel, "You rolled %d." % number) server.send_event(roll_event) except ValueError: error_event = irc.Irc_event("PRIVMSG", channel, "%s is no valid number." % arg) server.send_event(error_event) # Roll a number from 0 trough 1 else: number = random.random() roll_event = irc.Irc_event("PRIVMSG", channel, "You rolled %f." % number) server.send_event(roll_event)
def autoremove_command(command, server): """Execute the )autoremove command.""" user = command.event.name # Set the autoremove option if len(command.args) > 0: if command.args[0] == "on": server.persistent_data.set("messages.%s.autoremove" % user, True) elif command.args[0] == "off": server.persistent_data.set("messages.%s.autoremove" % user, False) else: invalid_option_event = irc.Irc_event( "PRIVMSG", user, "\"%s\" is not valid option." % command.args[0]) server.send_event(invalid_option_event) # Display the autoremove option else: if server.persistent_data.get("messages.%s.autoremove" % user): display_event = irc.Irc_event("PRIVMSG", user, "You set autoremove to \"on\".") server.send_event(display_event) else: display_event = irc.Irc_event("PRIVMSG", user, "You set autoremove to \"off\".") server.send_event(display_event)
def gh_add_cmd(command, server): """Execute the )gh_add command.""" channel = command.event.args[0] if len(command.args) == 1: user = command.args[0] # Query for the first time _, etag, _, _, last_id = query_data(user, None, "0") # Already set etag and last_id, so the channel isn't spammend with 30 # updates on the next query server.persistent_data.set("gh.%s.targets.%s" % (channel, user), { "etag": etag, "last_id": last_id }) server.send_event( irc.Irc_event("PRIVMSG", channel, "Started watching %s" % user)) else: server.send_event( irc.Irc_event( "PRIVMSG", channel, "Usage: )gh_add <user> to start watching a user on github"))
def cm_command(command, server): """Execute the )cm command.""" user = command.event.name # There are messages if server.persistent_data.get("messages.%s.inbox" % user): messages = server.persistent_data.get("messages.%s.inbox" % user) if len(messages) > 0: messages_header_event = irc.Irc_event("PRIVMSG", user, "Your current inbox:") server.send_event(messages_header_event) index = 0 for message in messages: message_event = irc.Irc_event("PRIVMSG", user, "%d. %s" % (index, message)) server.send_event(message_event) index += 1 # Prevent flooding protection to kick in if len(messages) > 3: time.sleep(0.5) if server.persistent_data.get("messages.%s.autoremove" % user): server.persistent_data.remove("messages.%s.inbox" % user, message) server.persistent_data.set("messages.%s.unseen" % user, False) return # Catch both cases for no messages no_messages_event = irc.Irc_event("PRIVMSG", user, "You don't have any messages.") server.send_event(no_messages_event)
def status_command(command, server): """Execute the )status command.""" channel = command.event.args[0] if not command.event.args[ 0] == server.nick else command.event.name # Display afk status for each name if len(command.args) > 0: for name in command.args: if not name == "": mark_type = None if server.shared_data.get("afk.%s.bool" % name): mark_type = "afk" if server.shared_data.get("afk.%s.probably" % name): mark_type = "proposed as afk by %s" % server.shared_data.get( "afk.%s.proposer" % name) if mark_type == None: status_event = irc.Irc_event( "PRIVMSG", channel, "%s is not afk" % name) server.send_event(status_event) else: if server.shared_data.get("afk.%s.message" % name): message = server.shared_data.get( "afk.%s.message" % name) status_event = irc.Irc_event( "PRIVMSG", channel, "%s is %s ( %s )" % (name, mark_type, message)) server.send_event(status_event) else: status_event = irc.Irc_event( "PRIVMSG", channel, "%s is %s" % (name, mark_type)) server.send_event(status_event)
def on_privmsg(event, server): """Detect urls in messages and reply with their titles""" channel = event.args[0] message = event.args[1] # Find the URLs for url_match in re.finditer("(http(?:s)?://.*?)( |\\?|$)", message): url = url_match.group(1) # Check wether the URL should be highlighted if valid_content(url): try: html = urllib.urlopen(url).read() # Find the title tag title_match = re.search("(?s)<title *>(.+?)</title *>", html) if title_match: # Reply with the URL's title title = title_match.group(1).strip() title = HTMLParser.HTMLParser().unescape(title) title = title.replace("\n", " ") reply = irc.Irc_event("PRIVMSG", channel, "[%s] - %s" % (title, url)) server.send_event(reply) # The opening of URLs might fail except IOError: reply = irc.Irc_event("PRIVMSG", channel, "Failed to open url: %s" % url) server.send_event(reply)
def afkPropose_command(command, server): """Execute the )afkPropose command.""" channel = command.event.args[0] if not command.event.args[ 0] == server.nick else command.event.name if len(command.args) < 1: return # Ignore. proposer = command.event.name target = command.args[0] if server.shared_data.get("afk.%s.bool" % target): reject_event = irc.Irc_event( "PRIVMSG", channel, "%s is already afk." % target) server.send_event(reject_event) return # Set afk message if len(command.args) > 1: message = " ".join(command.args[1:]) server.shared_data.set("afk.%s.message" % target, message) # Mark user as possibly afk server.shared_data.set("afk.%s.probably" % target, True) server.shared_data.set("afk.%s.proposer" % target, proposer) # Send announcing message announce_event = irc.Irc_event( "PRIVMSG", channel, "%s is now probably afk." % target) server.send_event(announce_event)
def query_thread(server, channel): """A thread regularily querying github for the messages to send to a channel.""" delay = 60 while True: targets = server.persistent_data.get("gh.%s.targets" % channel) for user, user_data in targets.iteritems(): if user_data: etag = user_data["etag"] last_id = user_data["last_id"] # Query github data, n_etag, n_delay, changed, id = query_data( user, etag, last_id) # Update the data with the results delay = n_delay server.persistent_data.set( "gh.%s.targets.%s" % (channel, user), { "etag": n_etag, "last_id": id }) # Send messages, if changed if changed: for message in generate_messages(data): server.send_event( irc.Irc_event("PRIVMSG", channel, message)) sleep(delay)
def gh_del_cmd(command, server): """Execute the )gh_del command.""" channel = command.event.args[0] if len(command.args) == 1: user = command.args[0] server.persistent_data.set("gh.%s.targets.%s" % (channel, user), None) server.send_event( irc.Irc_event("PRIVMSG", channel, "Stopped watching %s" % user)) else: server.send_event( irc.Irc_event( "PRIVMSG", channel, "Usage: )gh_del <user> to stop watching a user on github"))
def matelight_command(command, server): """Execute the )matelight command""" channel = command.event.args[0] if host: message = " ".join(command.args) s = socket.socket() s.connect((host, port)) s.send(message) reply = s.recv(1024) s.close() server.send_event(irc.Irc_event("PRIVMSG", channel, reply)) else: server.send_event(irc.Irc_event("PRIVMSG", channel, host_not_set))
def tokens_command(command, server): """Execute the )tokens command.""" channel = command.event.args[0] if not command.event.args[ 0] == server.nick else command.event.name if shared_data.get("greeter.tokens"): tokens = shared_data.get("greeter.tokens") tokens_list = ", ".join(tokens.keys()) tokens_event = irc.Irc_event( "PRIVMSG", channel, "Available tokens: %s" % tokens_list) server.send_event(tokens_event) else: no_tokens_event = irc.Irc_event( "PRIVMSG", channel, "No tokens available.") server.send_event(no_tokens_event)
def on_join(event, server): """Handle JOIN events.""" # There are unseen messages if server.persistent_data.get("messages.%s.unseen" % event.name): # Notify user unseen_messages_event = irc.Irc_event("PRIVMSG", event.name, "You have unseen messages.") server.send_event(unseen_messages_event)
def on_join(event, server): """Handle JOIN events.""" channel = event.args[0] # Don't greet the bot if not server.nick == event.name: greet_event = irc.Irc_event( "PRIVMSG", channel, _generate_greeting(event, server)) server.send_event(greet_event)
def on_privmsg(event, server): """Handle PRIVMSG events.""" is_marked = server.shared_data.get( "afk.%s.bool" % event.name) or server.shared_data.get("afk.%s.probably" % event.name) if is_marked: undo_afk(server, event.name) # Notify that user is back back_event = irc.Irc_event( "PRIVMSG", event.args[0], "%s is back." % event.name) server.send_event(back_event)
def welcomemsg_command(command, server): """Execute the )welcomemsg command.""" user = command.event.name # Set welcome message if len(command.args) > 0: server.persistent_data.set( "greeter.%s.welcomemsgs" % user, " ".join(command.args)) # Output current welcome message else: if server.persistent_data.get("greeter.%s.welcomemsgs" % user): message = "Your welcome message is: \"%s\"" % server.persistent_data.get( "greeter.%s.welcomemsgs" % user) output_event = irc.Irc_event("PRIVMSG", user, message) server.send_event(output_event) else: output_event = irc.Irc_event( "PRIVMSG", user, "No welcome message set.") server.send_event(output_event)
def help_command(command, server): """Execute the )help command.""" channel = command.event.args[ 0] if not command.event.args[0] == server.nick else command.event.name help_data = shared_data.get("help") if help_data: # Display all available topics if len(command.args) == 0: if len(help_data.keys()) > 0: help_message = "Available topics: %s" % ", ".join( help_data.keys()) help_event = irc.Irc_event("PRIVMSG", channel, help_message) server.send_event(help_event) # Display help for the topics else: for topic in command.args: if topic in help_data: topic_event = irc.Irc_event( "PRIVMSG", channel, "%s: %s" % (topic, help_data[topic])) server.send_event(topic_event)
def afk_command(command, server): """Execute the )afk command.""" channel = command.event.args[0] if not command.event.args[ 0] == server.nick else command.event.name user = command.event.name # Set afk message if len(command.args) > 0: message = " ".join(command.args) server.shared_data.set("afk.%s.message" % user, message) # Mark user as afk server.shared_data.set("afk.%s.bool" % user, True) # Send announcing message announce_event = irc.Irc_event("PRIVMSG", channel, "%s is now afk." % user) server.send_event(announce_event)
def msg_command(command, server): """Execute the )msg command.""" # This command needs a receipent and a message -> args > 1 if len(command.args) > 1: message = "Message from %s: %s" % (command.event.name, " ".join( command.args[1:])) receipent = command.args[0] # Receipent is online if server.user_data.get_user(receipent): # Send it directly to receipent message_event = irc.Irc_event("PRIVMSG", receipent, message) server.send_event(message_event) else: # Create list if it doesn't already exist if not server.persistent_data.get("messages.%s.inbox" % receipent): server.persistent_data.set("messages.%s.inbox" % receipent, []) # Put message in inbox server.persistent_data.append("messages.%s.inbox" % receipent, message) server.persistent_data.set("messages.%s.unseen" % receipent, True)