from datetime import datetime from commands.common import CommandHelpers as CH, StatisticsHelpers bold = "\x02" italic = "\x1D" underline = "\x1F" reverse = "\x16" # swap background and foreground colors ("reverse video") reset = "\x0F" blue = "\x0302" green = "\x0303" red = "\x0304" grey = "\x0314" def do_command(self, connection, event): cmdtype, trigger, command, replyto = CH.disect_command(self, event) if not command: # Do nothing if there is no command. return if not self.db.one("SELECT statistics_commands FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname(): return # Do noting if the games channel function is off and it's a channel message. if command == "cmd" or command == "cmds" or command == "commands": if cmdtype == "cmd": connection.privmsg(replyto, grey + "Statistics commands: " + CH.ccc(self, "seen") + CH.ccc(self, "joins") + CH.ccc(self, "kicks") + CH.ccc(self, "messages") + CH.ccc(self, "actions") + CH.ccc(self, "notices")[:-2] + ".") elif command.split()[0] == "joins" or command.split()[0] == "kicks" or command.split()[0] == "messages" or command.split()[0] == "actions" or command.split()[0] == "notices": if cmdtype == "help": #Display help text. if len(command.split()) is not 1: return connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and channel. Channel and user optional.") connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user") elif cmdtype == "cmd": # Parse user input user = event.source.nick channel = None channelonly= False if not connection.get_nickname() == event.target: # Channel message. channel = event.target if len(command.split()) == 1: # Command contains only !joins. user = event.source.nick elif len(command.split()) == 2: # Command has one argument. if command.split()[1] in self.channels: channel = command.split()[1] if connection.get_nickname() == event.target: # Private message. channelonly = True else: user = trigger.split()[1] elif len(command.split()) == 3: # Command has two arguments. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".") return else: # User reverted user and channel in command syntax. user = trigger.split()[1] channel = command.split()[2] else: # Bot does inhabit requested channel. user = trigger.split()[2] channel = trigger.split()[1] elif len(command.split()) < 5: # To many arguments connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".") return if command.split()[0] == "joins": if channel: # User and channel. userstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'"))) userchannelstat = str(self.db.one("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")) channelstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'"))) if userchannelstat == "None": userchannelstat = "0" if channelonly: connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.") elif userstat == "0" and not channelonly: # No user joins on record. connection.action(replyto, "has no record of any joins by " + red + user + reset + " in " + red + channel + reset + ".") else: # User joins on record. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + ". Of which " + green + userchannelstat + reset + " have been in " + red + channel + reset + ", that has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.") else: # Only user. userstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'"))) if userstat == "0": # No statistics on user. connection.action(replyto, "has no record of any joins by " + red + user + reset + ".") else: # Got statistics on user. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.") elif command.split()[0] == "kicks": if channel: # User and channel. try: givenkicks, receivedkicks = self.db.one("SELECT given, received FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'") except: givenkicks = 0 receivedkicks = 0 if channelonly: connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.") elif givenkicks == 0: # No kicks on record. connection.action(replyto, "has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".") else: # Kciks on record. channelkicks = self.db.all("SELECT given FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'") connection.privmsg(replyto, red + user + reset + " has kicked " + green + str(givenkicks) + reset + " and been kicked " + green + str(receivedkicks) + reset + " times in " + red + channel + reset + ", where were " + green + str(sum(channelkicks)) + reset + " kicks in total.") else: # Only user. userstat = self.db.all("SELECT given, received FROM kicks WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'") kicksgiven = 0 kicksreceived = 0 for record in userstat: kicksgiven += record[0] kicksreceived += record[1] if kicksgiven == 0 and kicksreceived == 0: connection.action(replyto, red + user + reset + " has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".") else: connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks") elif command.split()[0] == "messages" or command.split()[0] == "actions" or command.split()[0] == "notices": userstat = self.db.all("SELECT " + command.split()[0] + ", " + command.split()[0] + "_words, " + command.split()[0] + "_characters FROM messages WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'") if not userstat: if command.split()[0] == "messages": connection.action(replyto, "has no record of " + red + user + reset + " speaking.") return if command.split()[0] == "actions": connection.action(replyto, "has no record of " + red + user + reset + " acting.") return if command.split()[0] == "notices": connection.action(replyto, "has not noticed " + red + user + reset + ".") return messages, words, characters = StatisticsHelpers.add_message_stats(userstat) message = "Totals of " + red + user + " " + green + str(messages) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(words) + reset + " words, " + green + str(characters) + reset + " characters." if channel: # User and channel. userchanstat = self.db.one("SELECT " + command.split()[0] + ", " + command.split()[0] + "_words, " + command.split()[0] + "_characters FROM messages WHERE lower(channel)=lower('" + channel + "') AND channel_network='" + self.network + "' AND lower(\"user\")=lower('" + user + "') AND user_network='" + self.network + "'") chanstat = self.db.all("SELECT " + command.split()[0] + ", " + command.split()[0] + "_words, " + command.split()[0] + "_characters FROM messages WHERE lower(channel)=lower('" + channel + "') AND channel_network='" + self.network + "'") if userchanstat: message += " " + red + user + reset + " in " + red + channel + " " + green + str(userchanstat[0]) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(userchanstat[1]) + reset + " words, " + green + str(userchanstat[2]) + reset + " chars." if chanstat: messages, words, characters = StatisticsHelpers.add_message_stats(chanstat) message += " Total in " + red + channel + " " + green + str(messages) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(words) + reset + " wrd, " + green + str(characters) + reset + " chr." connection.privmsg(replyto, message) elif command.split()[0] == "seen": if cmdtype == "help": #Display help text. if len(command.split()) is not 1: return connection.privmsg(replyto, "Report the last sighting of a user.") connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "user") elif cmdtype == "cmd": if len(command.split()) == 1: connection.privmsg(replyto, "I am seeing you right now. For help type " + blue + self.helpchar + reset + ".") elif len(command.split()) == 2: if not self.db.one("SELECT last_act_type FROM users WHERE LOWER(name)='" + command.split()[1] + "' AND network='" + self.network + "'"): connection.action(replyto, "has never seen " + red + trigger.split()[1] + reset + ".") elif command.split()[1] == event.source.nick.lower(): connection.action(replyto, "holds up a mirror to " + event.source.nick + ".") else: record = self.db.one("SELECT last_act_type, last_act_datetime, last_act_channel, last_act, last_act_auxiliary, away, away_reason FROM users WHERE LOWER(name)='" + command.split()[1] + "' AND network='" + self.network + "'") if command.split()[1] == connection.get_nickname().lower(): action = "last action was " else: action = "last saw " + red + trigger.split()[1] + reset + " " if record[0] == "nick": action += "changing nickname to " + red + record[3] elif record[0] == "join": action += "joining " + red + record[2] elif record[0] == "kick": if record[4]: action += "kicking " + red + record[4] + reset + " for " + green + record[3] else: action += "kicking " + red + record[3] elif record[0] == "kicked": if record[4]: action += "being kicked by " + red + record[3] + reset + " for " + green + record[4] else: action += "being kicked by " + red + record[3] elif record[0] == "mode": action += "changing modes on " + red + record[3] elif record[0] == "part": action += "parting " + red + record[2] if record[3]: action += reset + " for " + record[3] elif record[0] == "quit": action += "disconnecting" if record[3]: action += " due to " + green + record[3] elif record[0] == "topic": action += "changing the topic of " + red + record[2] + reset + " to " + green + record[3] elif record[0] == "msg": action += "posting " + green + record[3] + reset + " to " + red + record[2] elif record[0] == "notice": action += "posting the notice " + green + record[3] + " to " + red + record[2] elif record[0] == "action": action += green + record[3] + reset + " in " + red + record[2] else: connection.privmsg(replyto, "Last stored action unsupported by command code.") return action += reset + ", " differential = datetime.now() - record[1] if differential.seconds < 5: # Less then 5 seconds. action += blue + "right now." elif differential.seconds < 20: # Less then 20 seconds. action += blue + "just now." elif differential.seconds < 60: # Less then a minute. action += green + str(differential.seconds) + blue + " seconds " + reset + "ago." elif differential.seconds / 60 == 1: # 1 minute. action += green + "1 " + blue + "minute " + reset + "ago." elif int(differential.seconds / 60) < 60: # Less then an hour. action += green + str(int(differential.seconds / 60)) + blue + " minutes " + reset + "ago." elif int(differential.seconds / 60) == 60: # 1 hour. action += green + "1 " + blue + "hour " + reset + "ago." elif int(differential.seconds / 3600) < 24 : # Less then a day. remaining_seconds = int(differential.seconds - int(differential.seconds / 3600) * 3600) action += green + str(int(differential.seconds / 3600)) + blue + " hours" + reset + " and " + str(int(remaining_seconds / 60)) + " minutes ago." elif int(differential.seconds / 3600) == 24 : # 1 day. action += green + "1 " + blue + "day " + reset + "ago." elif differential.days < 7: # Less then a week. remaining = differential - datetime.timedelta(days=differential.days) action += green + str(differential.days) + blue + " days " + reset + "and " + str(int(remaining.seconds / 3600)) + " hours ago." elif differential.days < 365: # Less then a year. action += green + str(differential.days) + blue + " days " + reset + "ago." elif differential.days < 365: # Less then 5 years. remaining_days = int(int(differential.days / 365) * 365) remaining = differential - datetime.timedelta(days=remaining_days) action += green + str(int(differential.days / 365)) + blue + " years " + reset + "and " + str(remaining.days) + " days." else: # More then 5 years. action += green + str(int(differential.days / 365)) + blue + " years ago." connection.action(replyto, action) if record[5]: if record[6]: connection.privmsg(replyto, red + trigger.split()[1] + reset + " is away " + green + record[6] + ".") else: connection.privmsg(replyto, red + trigger.split()[1] + reset + " is away.") else: # Too many arguments. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "seen" + reset + ".")