statistics.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. from datetime import datetime
  2. from commands.common import CommandHelpers as CH, StatisticsHelpers
  3. bold = "\x02"
  4. italic = "\x1D"
  5. underline = "\x1F"
  6. reverse = "\x16" # swap background and foreground colors ("reverse video")
  7. reset = "\x0F"
  8. blue = "\x0302"
  9. green = "\x0303"
  10. red = "\x0304"
  11. grey = "\x0314"
  12. def do_command(self, connection, event):
  13. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  14. if not command: # Do nothing if there is no command.
  15. return
  16. 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():
  17. return # Do noting if the games channel function is off and it's a channel message.
  18. if command == "cmd" or command == "cmds" or command == "commands":
  19. if cmdtype == "cmd":
  20. 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] + ".")
  21. 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":
  22. if cmdtype == "help": #Display help text.
  23. if len(command.split()) is not 1:
  24. return
  25. connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and channel. Channel and user optional.")
  26. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user")
  27. elif cmdtype == "cmd":
  28. # Parse user input
  29. user = event.source.nick
  30. channel = None
  31. channelonly= False
  32. if not connection.get_nickname() == event.target: # Channel message.
  33. channel = event.target
  34. if len(command.split()) == 1: # Command contains only !joins.
  35. user = event.source.nick
  36. elif len(command.split()) == 2: # Command has one argument.
  37. if command.split()[1] in self.channels:
  38. channel = command.split()[1]
  39. if connection.get_nickname() == event.target: # Private message.
  40. channelonly = True
  41. else:
  42. user = trigger.split()[1]
  43. elif len(command.split()) == 3: # Command has two arguments.
  44. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  45. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  46. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  47. return
  48. else: # User reverted user and channel in command syntax.
  49. user = trigger.split()[1]
  50. channel = command.split()[2]
  51. else: # Bot does inhabit requested channel.
  52. user = trigger.split()[2]
  53. channel = trigger.split()[1]
  54. elif len(command.split()) < 5: # To many arguments
  55. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  56. return
  57. if command.split()[0] == "joins":
  58. if channel: # User and channel.
  59. 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 + "'")))
  60. 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 + "'"))
  61. 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 + "'")))
  62. if userchannelstat == "None":
  63. userchannelstat = "0"
  64. if channelonly:
  65. connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  66. elif userstat == "0" and not channelonly: # No user joins on record.
  67. connection.action(replyto, "has no record of any joins by " + red + user + reset + " in " + red + channel + reset + ".")
  68. else: # User joins on record.
  69. 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.")
  70. else: # Only user.
  71. 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 + "'")))
  72. if userstat == "0": # No statistics on user.
  73. connection.action(replyto, "has no record of any joins by " + red + user + reset + ".")
  74. else: # Got statistics on user.
  75. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
  76. elif command.split()[0] == "kicks":
  77. if channel: # User and channel.
  78. try:
  79. 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 + "'")
  80. except:
  81. givenkicks = 0
  82. receivedkicks = 0
  83. if channelonly:
  84. connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  85. elif givenkicks == 0: # No kicks on record.
  86. connection.action(replyto, "has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".")
  87. else: # Kciks on record.
  88. channelkicks = self.db.all("SELECT given FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  89. 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.")
  90. else: # Only user.
  91. userstat = self.db.all("SELECT given, received FROM kicks WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  92. kicksgiven = 0
  93. kicksreceived = 0
  94. for record in userstat:
  95. kicksgiven += record[0]
  96. kicksreceived += record[1]
  97. if kicksgiven == 0 and kicksreceived == 0:
  98. connection.action(replyto, red + user + reset + " has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".")
  99. else:
  100. connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
  101. elif command.split()[0] == "messages" or command.split()[0] == "actions" or command.split()[0] == "notices":
  102. 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 + "'")
  103. if not userstat:
  104. if command.split()[0] == "messages":
  105. connection.action(replyto, "has no record of " + red + user + reset + " speaking.")
  106. return
  107. if command.split()[0] == "actions":
  108. connection.action(replyto, "has no record of " + red + user + reset + " acting.")
  109. return
  110. if command.split()[0] == "notices":
  111. connection.action(replyto, "has not noticed " + red + user + reset + ".")
  112. return
  113. messages, words, characters = StatisticsHelpers.add_message_stats(userstat)
  114. message = "Totals of " + red + user + " " + green + str(messages) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(words) + reset + " words, " + green + str(characters) + reset + " characters."
  115. if channel: # User and channel.
  116. 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 + "'")
  117. 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 + "'")
  118. if userchanstat:
  119. 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."
  120. if chanstat:
  121. messages, words, characters = StatisticsHelpers.add_message_stats(chanstat)
  122. message += " Total in " + red + channel + " " + green + str(messages) + reset + " " + blue + command.split()[0] + reset + ", " + green + str(words) + reset + " wrd, " + green + str(characters) + reset + " chr."
  123. connection.privmsg(replyto, message)
  124. elif command.split()[0] == "seen":
  125. if cmdtype == "help": #Display help text.
  126. if len(command.split()) is not 1:
  127. return
  128. connection.privmsg(replyto, "Report the last sighting of a user.")
  129. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "user")
  130. elif cmdtype == "cmd":
  131. if len(command.split()) == 1:
  132. connection.privmsg(replyto, "I asm seeing you right now. For help type " + blue + self.helpchar + reset + ".")
  133. elif len(command.split()) == 2:
  134. if not self.db.one("SELECT last_act_type FROM users WHERE LOWER(name)='" + command.split()[1] + "' AND network='" + self.network + "'"):
  135. connection.action(replyto, "has never seen " + red + command.split()[1] + reset + ".")
  136. elif command.split()[1] == event.source.nick.lower():
  137. connection.action(replyto, "holds up a mirror to " + event.source.nick + ".")
  138. else:
  139. 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 + "'")
  140. if command.split()[1] == connection.get_nickname().lower():
  141. action = "last action was "
  142. else:
  143. action = "last saw " + red + trigger.split()[1] + reset + " "
  144. if record[0] == "nick":
  145. action += "changing nickname to " + red + record[3]
  146. elif record[0] == "join":
  147. action += "joining " + red + record[2]
  148. elif record[0] == "kick":
  149. if record[4]:
  150. action += "kicking " + red + record[4] + reset + " for " + green + record[3]
  151. else:
  152. action += "kicking " + red + record[3]
  153. elif record[0] == "kicked":
  154. if record[4]:
  155. action += "being kicked by " + red + record[3] + reset + " for " + green + record[4]
  156. else:
  157. action += "being kicked by " + red + record[3]
  158. elif record[0] == "mode":
  159. action += "changing modes on " + red + record[3]
  160. elif record[0] == "part":
  161. action += "parting " + red + record[2]
  162. if record[3]:
  163. action += reset + " for " + record[3]
  164. elif record[0] == "quit":
  165. action += "disconnecting"
  166. if record[3]:
  167. action += " due to " + green + record[3]
  168. elif record[0] == "topic":
  169. action += "changing the topic of " + red + record[2] + reset + " to " + green + record[3]
  170. elif record[0] == "msg":
  171. action += "posting " + green + record[3] + reset + " to " + red + record[2]
  172. elif record[0] == "notice":
  173. action += "posting the notice " + green + record[3] + " to " + red + record[2]
  174. elif record[0] == "action":
  175. action += green + record[3] + reset + " in " + red + record[2]
  176. else:
  177. connection.privmsg(replyto, "Last stored action unsupported by command code.")
  178. return
  179. action += reset + ", "
  180. differential = datetime.now() - record[1]
  181. if differential.seconds < 5: # Less then 5 seconds.
  182. action += blue + "right now."
  183. elif differential.seconds < 20: # Less then 20 seconds.
  184. action += blue + "just now."
  185. elif differential.seconds < 60: # Less then a minute.
  186. action += green + str(differential.seconds) + blue + " seconds " + reset + "ago."
  187. elif differential.seconds / 60 == 1: # 1 minute.
  188. action += green + "1 " + blue + "minute " + reset + "ago."
  189. elif int(differential.seconds / 60) < 60: # Less then an hour.
  190. action += green + str(int(differential.seconds / 60)) + blue + " minutes " + reset + "ago."
  191. elif int(differential.seconds / 60) == 60: # 1 hour.
  192. action += green + "1 " + blue + "hour " + reset + "ago."
  193. elif int(differential.seconds / 3600) < 24 : # Less then a day.
  194. remaining_seconds = int(differential.seconds - int(differential.seconds / 3600) * 3600)
  195. action += green + str(int(differential.seconds / 3600)) + blue + " hours" + reset + " and " + str(int(remaining_seconds / 60)) + " minutes ago."
  196. elif int(differential.seconds / 3600) == 24 : # 1 day.
  197. action += green + "1 " + blue + "day " + reset + "ago."
  198. elif differential.days < 7: # Less then a week.
  199. remaining = differential - datetime.timedelta(days=differential.days)
  200. action += green + str(differential.days) + blue + " days " + reset + "and " + str(int(remaining.seconds / 3600)) + " hours ago."
  201. elif differential.days < 365: # Less then a year.
  202. action += green + str(differential.days) + blue + " days " + reset + "ago."
  203. elif differential.days < 365: # Less then 5 years.
  204. remaining_days = int(int(differential.days / 365) * 365)
  205. remaining = differential - datetime.timedelta(days=remaining_days)
  206. action += green + str(int(differential.days / 365)) + blue + " years " + reset + "and " + str(remaining.days) + " days."
  207. else: # More then 5 years.
  208. action += green + str(int(differential.days / 365)) + blue + " years ago."
  209. connection.action(replyto, action)
  210. if record[5]:
  211. if record[6]:
  212. connection.privmsg(replyto, red + trigger.split()[1] + reset + " is away " + green + record[6] + ".")
  213. else:
  214. connection.privmsg(replyto, red + trigger.split()[1] + reset + " is away.")
  215. else: # Too many arguments.
  216. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "seen" + reset + ".")