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 == "commands": if cmdtype == "cmd": connection.privmsg(replyto, grey + "Statistics commands: " + 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 or 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 = command.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 = command.split()[1] channel = command.split()[2] else: # Bot does inhabit requested channel. user = command.split()[2] channel = command.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)