public.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. from common import userstatus
  2. from commands.common import CommandHelpers as CH
  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. # The actual commands:
  17. if command == "test":
  18. if cmdtype == "help": #Display help text.
  19. if len(command.split()) is not 1:
  20. return
  21. connection.privmsg(replyto, "Strictly for testing purposes only!")
  22. elif cmdtype == "cmd":
  23. connection.privmsg(replyto, event)
  24. elif command == "cmd" or command == "commands":
  25. if cmdtype == "help": #Display help text.
  26. if len(command.split()) is not 1:
  27. return
  28. connection.privmsg(replyto, "Displays a list of commands.")
  29. elif cmdtype == "cmd":
  30. connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help") + CH.ccc(self, "joins") + CH.ccc(self, "stopgreet")[:-2] + ".")
  31. elif command == "help":
  32. if cmdtype == "help": #Display help text.
  33. if len(command.split()) is not 1:
  34. return
  35. connection.privmsg(replyto, "Explains how to get help on any specific command and hints the user to the commandlist.")
  36. elif cmdtype == "cmd":
  37. connection.privmsg(replyto, "Replace the " + italic + "! " + reset + "prefix of any comand with " + italic + self.helpchar + " " + reset + "for help with a specific command. Request the command list with: " + blue + "!cmd")
  38. connection.privmsg(replyto, grey + "Example: " + reset + blue + self.helpchar + "help")
  39. elif command.split()[0] == "joins":
  40. if cmdtype == "help": #Display help text.
  41. if len(command.split()) is not 1:
  42. return
  43. connection.privmsg(replyto, "Display amount of joins of user and or channel. Channel and user optional.")
  44. connection.privmsg(replyto, grey + "Usage: " + blue + "!joins " + reset + italic + "channel user")
  45. elif cmdtype == "cmd":
  46. # Parse user input
  47. if len(command.split()) == 1: # Command contains only !joins.
  48. user = event.source.nick
  49. if connection.get_nickname() is not event.target: # Channel message.
  50. channel = event.target
  51. elif len(command.split()) == 2: # Command has one argument.
  52. if command.split()[1] in self.channels:
  53. channel = command.split()[1]
  54. else:
  55. user = command.split()[1]
  56. elif len(command.split()) == 3: # Command has two arguments.
  57. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  58. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  59. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  60. return
  61. else: # User reverted user and channel in command syntax.
  62. user = command.split()[1]
  63. channel = command.split()[2]
  64. else: # Bot does inhabit requested channel.
  65. user = command.split()[2]
  66. channel = command.split()[1]
  67. elif len(command.split()) < 5: # To many arguments
  68. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  69. return
  70. try:
  71. if user and channel:
  72. userjoins = str(self.db.all("SELECT joins FROM joins WHERE channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "'"))
  73. userchanneljoins = str(self.db.one("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "'"))
  74. channeljoins = str(sum(self.db.all("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
  75. connection.privmsg(replyto, red + user + reset + " has " + green + str(sum(list(map(int, userjoins[1:-1].split(", "))))) + reset + " joins. Of which " + green + str(userchanneljoins) + reset + " have been in " + red + channel + reset + ", that has " + green + str(channeljoins) + reset + " joins" + reset + " in total.")
  76. return
  77. except:
  78. pass
  79. try:
  80. if user:
  81. userjoins = str(self.db.all("SELECT joins FROM joins WHERE channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "' AND user_network='" + self.network + "'"))
  82. connection.privmsg(replyto, red + user + reset + " has " + green + str(sum(list(map(int, userjoins[1:-1].split(", "))))) + reset + " joins in channels I monitor.")
  83. return
  84. except:
  85. pass
  86. try:
  87. if channel:
  88. channeljoins = str(sum(self.db.all("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
  89. connection.privmsg(replyto, red + channel + reset + " has been joined " + green + channeljoins + reset + " times.")
  90. return
  91. except:
  92. pass
  93. connection.privmsg(replyto, "tBKwtWS Was wondering if this programming was sloppy. When you see this message, it was.")
  94. elif command.split()[0] == "stopgreet":
  95. if cmdtype == "help": #Display help text.
  96. if len(command.split()) is not 1:
  97. return
  98. connection.privmsg(replyto, "Stops the bot from greeting you or a specific user. Channel, user and option to resume optional.")
  99. connection.privmsg(replyto, grey + "Usage: " + blue + "!stopgreet " + red + reset + italic + "resume " + red + "channel user")
  100. elif cmdtype == "cmd":
  101. # Check for resume variation of command.
  102. resume = False
  103. try:
  104. if command.split()[1] == "resume":
  105. resume = True
  106. command = command.split(' ', 1)[1] # Remove the resume argument. Which makes the following logic easier.
  107. except:
  108. pass
  109. if len(command.split()) == 1: # No arguments.
  110. if event.target == connection.get_nickname(): # PM.
  111. connection.privmsg(replyto, "Specify at least a channel. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  112. return
  113. user = event.source.nick
  114. channel = event.target
  115. if len(command.split()) == 2: # One argument.
  116. if command.split()[1] not in self.channels: # Argument is not a channel the bot inhabits.
  117. if event.target == connection.get_nickname(): # PM.
  118. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  119. return
  120. # Channel message
  121. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, event.target): # Insufficient rights.
  122. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + event.target + reset + ".")
  123. return
  124. # Stopgreet for x user in current channel
  125. user = command.split()[1]
  126. channel = event.target
  127. else: # Bot inhabit channel.
  128. user = event.source.nick
  129. channel = command.split()[1]
  130. if len(command.split()) == 3: # Two arguments.
  131. if command.split()[1] not in self.channels: # Bot does not inhabit channel.
  132. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  133. return
  134. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, command.split()[1]): # Insufficient rights.
  135. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + command.split()[1] + reset + ".")
  136. return
  137. user = command.split()[2]
  138. channel = command.split()[1]
  139. if len(command.split()) > 3: # Too many arguments.
  140. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  141. return
  142. # Check for database record.
  143. result = self.db.one("SELECT id, stopgreet FROM joins WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
  144. if not result: # No record in database.
  145. connection.action(replyto, "has not yet had the pleasure of greeting " + red + user + reset + " in " + red + channel + reset + ".")
  146. return
  147. if resume:
  148. stopgreet = False
  149. message = "has already every intention to greet "
  150. else:
  151. stopgreet = True
  152. message = "has already stopped greeting "
  153. if result[1] == stopgreet:
  154. connection.action(replyto, message + red + user + reset + " in " + red + channel + reset + ".")
  155. return
  156. print(str(stopgreet) + str(user) + str(channel))
  157. #try:
  158. print("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
  159. self.db.run("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
  160. #except:
  161. #connection.privmsg(replyto, "Failed to update database.")