statistics.py 19 KB

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