statistics.py 22 KB

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