1
0

statistics.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. from datetime import datetime
  2. from common import font, queries
  3. from commands.common import CommandHelpers as CH
  4. def do_command(self, connection, event, user, channel):
  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 statistics channel function is off and it's a channel message.
  18. if not event.target == connection.get_nickname():
  19. if not queries.get_channel_setting_statistic_commands(self, channel.id):
  20. return
  21. # Command list.
  22. if command == 'cmd' or command == 'cmds' or command == 'commands':
  23. if cmdtype == 'cmd':
  24. connection.privmsg(replyto, '%sStatistics: %s' % (font.grey, CH.ccc(self, "stat")[:-2])) #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] + "."))
  25. elif one == 'stat' or one == 'stats' or one == 'statistic' or one == 'statistics':
  26. if len(command.split()) == 1: # Command without arguments.
  27. if cmdtype == 'help':
  28. connection.privmsg(replyto, 'Serves a link with statistics on a channel or username. Channel and user arguments are optional')
  29. connection.privmsg(replyto, 'Usage %s%s%s %s%schannel' % (font.blue, self.network.command_character, one, font.reset, font.italic))
  30. if channel: # Don't advertise our channel on other channels.
  31. connection.privmsg(replyto, '%sExample %s%s%s %s%s' % (font.grey, font.reset, event.target, one, self.network.home_channel))
  32. else:
  33. connection.privmsg(replyto, '%sExample %s%s%s %s%s' % (font.grey, font.reset, self.network.command_character, one, self.network.home_channel))
  34. else: # Actual command with no aruments.
  35. if not channel: # Command sent directly to bot
  36. connection.privmsg(replyto, '%sYour statistics:%s %suser/%s' % (font.grey, font.reset, self.webgui['base_url'], user.slug))
  37. else: # Command sent to channel
  38. connection.privmsg(replyto, '%sChannel statistics:%s %schannel/%s %sYour statistics:%s %suser/%s' % (font.grey, font.reset, self.webgui['base_url'], channel.slug, font.grey, font.reset, self.webgui['base_url'], user.slug))
  39. elif len(command.split()) == 2: # Command with one argument.
  40. if command.split()[1] in self.channels: # Argument matches a inhabited channel.
  41. if cmdtype == 'help':
  42. connection.privmsg(replyto, 'Send a link for statistics on channel: %s%s' % ( font.red, command.split()[1]))
  43. else: # Actual command with one argument
  44. channel_slug = queries.get_channel_slug(self, command.split()[1])
  45. connection.privmsg(replyto, '%sStatistics on channel %s%s%s:%s %schannel/%s' % (font.grey, font.red, command.split()[1], font.grey, font.reset, self.webgui['base_url'], channel_slug))
  46. else: # Argument does not match a channel.
  47. user_slug = queries.get_user_slug(self, command.split()[1])
  48. if user_slug:
  49. connection.privmsg(replyto, '%sStatistics on nickname %s%s%s:%s %suser/%s' % (font.grey, font.red, command.split()[1], font.grey, font.reset, self.webgui['base_url'], user_slug))
  50. else: # Argument does not match a channel or user.
  51. connection.privmsg(replyto, 'I have not had the pleasure of being aquinted with: %s%s' % (font.red, command.split()[1]))
  52. elif len(command.split()) >= 3: # Too many arguments.
  53. connection.privmsg('Too many arguments, for help type: %s%s%s' % (font.blue, self.network.command_character, one))
  54. # 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":
  55. # if cmdtype == "help": #Display help text.
  56. # if len(command.split()) != 1:
  57. # return
  58. # connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and channel. Channel and user optional.")
  59. # connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + font.reset + font.italic + "channel user")
  60. # elif cmdtype == "cmd":
  61. #
  62. # # Parse user input
  63. # user = event.source.nick
  64. # channel = None
  65. # channelonly= False
  66. # if not connection.get_nickname() == event.target: # Channel message.
  67. # channel = event.target
  68. # if len(command.split()) == 1: # Command contains only !joins.
  69. # user = event.source.nick
  70. # elif len(command.split()) == 2: # Command has one argument.
  71. # if command.split()[1] in self.channels:
  72. # channel = command.split()[1]
  73. # if connection.get_nickname() == event.target: # Private message.
  74. # channelonly = True
  75. # else:
  76. # user = trigger.split()[1]
  77. # elif len(command.split()) == 3: # Command has two arguments.
  78. # if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  79. # if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  80. # connection.action(replyto, "does not inhabit " + font.red + command.split()[1] + font.reset + ".")
  81. # return
  82. # else: # User reverted user and channel in command syntax.
  83. # user = trigger.split()[1]
  84. # channel = command.split()[2]
  85. # else: # Bot does inhabit requested channel.
  86. # user = trigger.split()[2]
  87. # channel = trigger.split()[1]
  88. # elif len(command.split()) < 5: # To many arguments
  89. # connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + font.reset + ".")
  90. # return
  91. #
  92. # if command.split()[0] == "joins":
  93. # if channel: # User and channel.
  94. # 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 + "'")))
  95. # 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 + "'"))
  96. # 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 + "'")))
  97. # if userchannelstat == "None":
  98. # userchannelstat = "0"
  99. # if channelonly:
  100. # connection.privmsg(replyto, font.red + channel + font.reset + " has " + green + channelstat + font.reset + " " + command.split()[0] + font.reset + " in total.")
  101. # elif userstat == "0" and not channelonly: # No user joins on record.
  102. # connection.action(replyto, "has no record of any joins by " + font.red + user + font.reset + " in " + font.red + channel + font.reset + ".")
  103. # else: # User joins on record.
  104. # 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.")
  105. # else: # Only user.
  106. # 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 + "'")))
  107. # if userstat == "0": # No statistics on user.
  108. # connection.action(replyto, "has no record of any joins by " + font.red + user + font.reset + ".")
  109. # else: # Got statistics on user.
  110. # connection.privmsg(replyto, font.red + user + font.reset + " has " + green + userstat + font.reset + " " + command.split()[0] + " in channels I monitor.")
  111. # elif command.split()[0] == "kicks":
  112. # if channel: # User and channel.
  113. # try:
  114. # 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 + "'")
  115. # except:
  116. # givenkicks = 0
  117. # receivedkicks = 0
  118. # if channelonly:
  119. # connection.privmsg(replyto, font.red + channel + font.reset + " has " + green + channelstat + font.reset + " " + command.split()[0] + font.reset + " in total.")
  120. # elif givenkicks == 0 and receivedkicks == 0: # No kicks on record.
  121. # connection.action(replyto, "has no record of any kicks for " + font.red + user + font.reset + " in " + font.red + channel + font.reset + ".")
  122. # else: # Kciks on record.
  123. # channelkicks = self.db.all("SELECT given FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  124. # 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.")
  125. # else: # Only user.
  126. # userstat = self.db.all("SELECT given, received FROM kicks WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  127. # kicksgiven = 0
  128. # kicksreceived = 0
  129. # for record in userstat:
  130. # kicksgiven += record[0]
  131. # kicksreceived += record[1]
  132. # if kicksgiven == 0 and kicksreceived == 0:
  133. # 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 + ".")
  134. # else:
  135. # connection.privmsg(replyto, font.red + user + font.reset + " has given " + green + str(kicksgiven) + font.reset + " and received " + green + str(kicksreceived) + font.reset + " kicks")
  136. # elif command.split()[0] == "messages" or command.split()[0] == "actions" or command.split()[0] == "notices":
  137. # 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 + "'")
  138. #
  139. # userrecord = False
  140. # for record in userstat:
  141. # if not record[0] == 0:
  142. # userrecord = True
  143. #
  144. # if not userrecord:
  145. # if command.split()[0] == "messages":
  146. # connection.action(replyto, "has no record of " + font.red + user + font.reset + " speaking.")
  147. # return
  148. # if command.split()[0] == "actions":
  149. # connection.action(replyto, "has no record of " + font.red + user + font.reset + " acting.")
  150. # return
  151. # if command.split()[0] == "notices":
  152. # connection.action(replyto, "has not noticed " + font.red + user + font.reset + ".")
  153. # return
  154. # messages, words, characters = StatisticsHelpers.add_message_stats(userstat)
  155. # 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."
  156. # if channel: # User and channel.
  157. # 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 + "'")
  158. # 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 + "'")
  159. # if not userchanstat[0] == 0:
  160. # 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."
  161. # channelrecord = False
  162. # for record in chanstat:
  163. # if not record[0] == 0:
  164. # channelrecord = True
  165. # if channelrecord:
  166. # messages, words, characters = StatisticsHelpers.add_message_stats(chanstat)
  167. # 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."
  168. # connection.privmsg(replyto, message)
  169. #
  170. # elif command.split()[0] == "seen":
  171. # if cmdtype == "help": #Display help text.
  172. # if len(command.split()) != 1:
  173. # return
  174. # connection.privmsg(replyto, "Report the last sighting of a user.")
  175. # connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + font.reset + font.italic + "user")
  176. # elif cmdtype == "cmd":
  177. #
  178. # if len(command.split()) == 1:
  179. # connection.privmsg(replyto, "I am seeing you right now. For help type " + blue + self.helpchar + font.reset + ".")
  180. # elif len(command.split()) == 2:
  181. # if not self.db.one("SELECT last_act_type FROM users WHERE LOWER(name)='" + command.split()[1] + "' AND network='" + self.network + "'"):
  182. # connection.action(replyto, "has never seen " + font.red + trigger.split()[1] + font.reset + ".")
  183. # elif command.split()[1] == event.source.nick.lower():
  184. # connection.action(replyto, "holds up a mirror to " + event.source.nick + ".")
  185. # elif "!" in command.split()[1] and "@" in command.split()[1]:
  186. # connection.privmsg(replyto, "The looks more like a hostname then a nickname to me.")
  187. # else:
  188. # 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 + "'")
  189. # if command.split()[1] == connection.get_nickname().lower():
  190. # action = "last action was "
  191. # else:
  192. # action = "last saw " + font.red + trigger.split()[1] + font.reset + " "
  193. # if record[0] == "nick":
  194. # action += "changing nickname to " + font.red + record[3]
  195. # elif record[0] == "join":
  196. # action += "joining " + font.red + record[2]
  197. # elif record[0] == "kick":
  198. # if record[4]:
  199. # action += "kicking " + font.red + record[4] + font.reset + " for " + green + record[3]
  200. # else:
  201. # action += "kicking " + font.red + record[3]
  202. # elif record[0] == "kicked":
  203. # if record[4]:
  204. # action += "being kicked by " + font.red + record[3] + font.reset + " for " + green + record[4]
  205. # else:
  206. # action += "being kicked by " + font.red + record[3]
  207. # elif record[0] == "mode":
  208. # action += "changing modes on " + font.red + record[3]
  209. # elif record[0] == "part":
  210. # action += "parting " + font.red + record[2]
  211. # if record[3]:
  212. # action += font.reset + " for " + record[3]
  213. # elif record[0] == "quit":
  214. # action += "disconnecting"
  215. # if record[3]:
  216. # action += " due to " + green + record[3]
  217. # elif record[0] == "topic":
  218. # action += "changing the topic of " + font.red + record[2] + font.reset + " to " + green + record[3]
  219. # elif record[0] == "msg":
  220. # action += "posting " + green + record[3] + font.reset + " to " + font.red + record[2]
  221. # elif record[0] == "notice":
  222. # action += "posting the notice " + green + record[3] + " to " + font.red + record[2]
  223. # elif record[0] == "action":
  224. # action += green + record[3] + font.reset + " in " + font.red + record[2]
  225. # else:
  226. # connection.privmsg(replyto, "Last stored action unsupported by command code.")
  227. # return
  228. # action += font.reset + ", "
  229. # differential = datetime.now() - record[1]
  230. # if differential.seconds < 5: # Less then 5 seconds.
  231. # action += blue + "right now."
  232. # elif differential.seconds < 20: # Less then 20 seconds.
  233. # action += blue + "just now."
  234. # elif differential.seconds < 60: # Less then a minute.
  235. # action += green + str(differential.seconds) + blue + " seconds " + font.reset + "ago."
  236. # elif differential.seconds / 60 == 1: # 1 minute.
  237. # action += green + "1 " + blue + "minute " + font.reset + "ago."
  238. # elif int(differential.seconds / 60) < 60: # Less then an hour.
  239. # action += green + str(int(differential.seconds / 60)) + blue + " minutes " + font.reset + "ago."
  240. # elif int(differential.seconds / 60) == 60: # 1 hour.
  241. # action += green + "1 " + blue + "hour " + font.reset + "ago."
  242. # elif int(differential.seconds / 3600) < 24 : # Less then a day.
  243. # remaining_seconds = int(differential.seconds - int(differential.seconds / 3600) * 3600)
  244. # action += green + str(int(differential.seconds / 3600)) + blue + " hours" + font.reset + " and " + str(int(remaining_seconds / 60)) + " minutes ago."
  245. # elif int(differential.seconds / 3600) == 24 : # 1 day.
  246. # action += green + "1 " + blue + "day " + font.reset + "ago."
  247. # elif differential.days < 7: # Less then a week.
  248. # remaining = differential - datetime.timedelta(days=differential.days)
  249. # action += green + str(differential.days) + blue + " days " + font.reset + "and " + str(int(remaining.seconds / 3600)) + " hours ago."
  250. # elif differential.days < 365: # Less then a year.
  251. # action += green + str(differential.days) + blue + " days " + font.reset + "ago."
  252. # elif differential.days < 365: # Less then 5 years.
  253. # remaining_days = int(int(differential.days / 365) * 365)
  254. # remaining = differential - datetime.timedelta(days=remaining_days)
  255. # action += green + str(int(differential.days / 365)) + blue + " years " + font.reset + "and " + str(remaining.days) + " days."
  256. # else: # More then 5 years.
  257. # action += green + str(int(differential.days / 365)) + blue + " years ago."
  258. # connection.action(replyto, action)
  259. # if record[5]:
  260. # if record[6]:
  261. # connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is away " + green + record[6] + ".")
  262. # else:
  263. # connection.privmsg(replyto, font.red + trigger.split()[1] + font.reset + " is away.")
  264. # else: # Too many arguments.
  265. # connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "seen" + font.reset + ".")