statistics.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from commands.common import CommandHelpers as CH, StatisticsHelpers
  2. bold = "\x02"
  3. italic = "\x1D"
  4. underline = "\x1F"
  5. reverse = "\x16" # swap background and foreground colors ("reverse video")
  6. reset = "\x0F"
  7. blue = "\x0302"
  8. green = "\x0303"
  9. red = "\x0304"
  10. grey = "\x0314"
  11. def do_command(self, connection, event):
  12. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  13. if not command: # Do nothing if there is no command.
  14. return
  15. 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():
  16. return # Do noting if the games channel function is off and it's a channel message.
  17. if command == "cmd" or command == "commands":
  18. if cmdtype == "cmd":
  19. 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] + ".")
  20. 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":
  21. if cmdtype == "help": #Display help text.
  22. if len(command.split()) is not 1:
  23. return
  24. connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and or channel. Channel and user optional.")
  25. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user")
  26. elif cmdtype == "cmd":
  27. # Parse user input
  28. user = event.source.nick
  29. channel = None
  30. channelonly= False
  31. if not connection.get_nickname() == event.target: # Channel message.
  32. channel = event.target
  33. if len(command.split()) == 1: # Command contains only !joins.
  34. user = event.source.nick
  35. elif len(command.split()) == 2: # Command has one argument.
  36. if command.split()[1] in self.channels:
  37. channel = command.split()[1]
  38. if connection.get_nickname() == event.target: # Private message.
  39. channelonly = True
  40. else:
  41. user = command.split()[1]
  42. elif len(command.split()) == 3: # Command has two arguments.
  43. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  44. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  45. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  46. return
  47. else: # User reverted user and channel in command syntax.
  48. user = command.split()[1]
  49. channel = command.split()[2]
  50. else: # Bot does inhabit requested channel.
  51. user = command.split()[2]
  52. channel = command.split()[1]
  53. elif len(command.split()) < 5: # To many arguments
  54. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  55. return
  56. if command.split()[0] == "joins":
  57. if channel: # User and channel.
  58. 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 + "'")))
  59. 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 + "'"))
  60. 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 + "'")))
  61. if userchannelstat == "None":
  62. userchannelstat = "0"
  63. if channelonly:
  64. connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  65. elif userstat == "0" and not channelonly: # No user joins on record.
  66. connection.action(replyto, "has no record of any joins by " + red + user + reset + " in " + red + channel + reset + ".")
  67. else: # User joins on record.
  68. 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.")
  69. else: # Only user.
  70. 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 + "'")))
  71. if userstat == "0": # No statistics on user.
  72. connection.action(replyto, "has no record of any joins by " + red + user + reset + ".")
  73. else: # Got statistics on user.
  74. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
  75. elif command.split()[0] == "kicks":
  76. if channel: # User and channel.
  77. try:
  78. 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 + "'")
  79. except:
  80. givenkicks = 0
  81. receivedkicks = 0
  82. if channelonly:
  83. connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  84. elif givenkicks == 0: # No kicks on record.
  85. connection.action(replyto, "has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".")
  86. else: # Kciks on record.
  87. channelkicks = self.db.all("SELECT given FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  88. 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.")
  89. else: # Only user.
  90. userstat = self.db.all("SELECT given, received FROM kicks WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  91. kicksgiven = 0
  92. kicksreceived = 0
  93. for record in userstat:
  94. kicksgiven += record[0]
  95. kicksreceived += record[1]
  96. if kicksgiven == 0 and kicksreceived == 0:
  97. connection.action(replyto, red + user + reset + " has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".")
  98. else:
  99. connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
  100. elif command.split()[0] == "messages" or command.split()[0] == "actions" or command.split()[0] == "notices":
  101. 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 + "'")
  102. if not userstat:
  103. if command.split()[0] == "messages":
  104. connection.action(replyto, "has no record of " + red + user + reset + " speaking.")
  105. return
  106. if command.split()[0] == "actions":
  107. connection.action(replyto, "has no record of " + red + user + reset + " acting.")
  108. return
  109. if command.split()[0] == "notices":
  110. connection.action(replyto, "has not noticed " + red + user + reset + ".")
  111. return
  112. messages, words, characters = StatisticsHelpers.add_message_stats(userstat)
  113. message = "Totals of " + red + user + " " + green + str(messages) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(words) + reset + " words, " + green + str(characters) + reset + " characters."
  114. if channel: # User and channel.
  115. 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 + "'")
  116. 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 + "'")
  117. if userchanstat:
  118. 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."
  119. if chanstat:
  120. messages, words, characters = StatisticsHelpers.add_message_stats(chanstat)
  121. message += " Total in " + red + channel + " " + green + str(messages) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(words) + reset + " wrd, " + green + str(characters) + reset + " chr."
  122. connection.privmsg(replyto, message)