admin.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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:
  15. return # Do nothing if there is no command.
  16. # Ignore channel commands from users that do not have at least voice in homechannel or operator status in target channel.
  17. if event.type == "pubmsg": # It's a channel message.
  18. if not userstatus.atleast_oper(self, event.source.nick, self.homechannel) and not userstatus.atleast_oper(self, event.source.nick, event.target): # Does not have at least voiced status in homechannel or operator status in target channel.
  19. return
  20. if command == "cmd" or command == "commands":
  21. if cmdtype == "cmd":
  22. message = grey + "Admin: "
  23. if CH.ccc(self, "channelfunctions", {"homechan": "oper", "chan": "oper"}, event):
  24. message += CH.ccc(self, "channelfunctions", {"homechan": "oper", "chan": "oper"}, event)
  25. if CH.ccc(self, "join", {"homechan": "oper", "chan": None}, event):
  26. message += CH.ccc(self, "join", {"homechan": "oper", "chan": None}, event)
  27. if CH.ccc(self, "part", {"homechan": "oper", "chan": None}, event):
  28. message += CH.ccc(self, "part", {"homechan": "oper", "chan": None}, event)
  29. if CH.ccc(self, "die", {"homechan": "admin", "chan": None}, event):
  30. message += CH.ccc(self, "die", {"homechan": "admin", "chan": None}, event)
  31. if CH.ccc(self, "reconnect", {"homechan": "oper", "chan": None}, event):
  32. message += CH.ccc(self, "reconnect", {"homechan": "oper", "chan": None}, event)
  33. if CH.ccc(self, "recovernick", {"homechan": "oper", "chan": None}, event):
  34. message += CH.ccc(self, "recovernick", {"homechan": "oper", "chan": None}, event)
  35. if CH.ccc(self, "msg", {"homechan": "oper", "chan": "oper"}, event):
  36. message += CH.ccc(self, "msg", {"homechan": "oper", "chan": "oper"}, event)
  37. if CH.ccc(self, "act", {"homechan": "oper", "chan": "oper"}, event):
  38. message += CH.ccc(self, "act", {"homechan": "oper", "chan": "oper"}, event)
  39. if message == grey + "Admin: ": # No commands to display.
  40. return
  41. connection.privmsg(replyto, message[:-2] + ".")
  42. elif command.split()[0] == "die":
  43. if not userstatus.atleast_admin(self, event.source.nick, self.homechannel):
  44. connection.privmsg(replyto, "Denied, you need to have admin (super operator) status or higher in " + red + self.homechannel + reset + ".")
  45. return
  46. if cmdtype == "help": # Display help text.
  47. if len(command.split()) is not 1:
  48. return
  49. connection.privmsg(replyto, "Kill " + connection.get_nickname() + ". Optionally with reason.")
  50. connection.privmsg(replyto, grey + "Usage: " + blue + "!die " + reset + italic + "reason")
  51. elif cmdtype == "cmd":
  52. if len(command.split()) == 1:
  53. self.die(msg = "Killed by " + event.source.nick)
  54. else:
  55. self.die(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
  56. elif command.split()[0] == "reconnect":
  57. if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick) and not self.channels[self.homechannel].is_oper(event.source.nick):
  58. connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel + reset + ".")
  59. return
  60. if cmdtype == "help": # Display help text.
  61. if len(command.split()) is not 1:
  62. return
  63. connection.privmsg(replyto, "Reconnect " + connection.get_nickname() + ". Reason optional.")
  64. connection.privmsg(replyto, grey + "Usage: " + blue + "!reconnect " + reset + italic + "reason")
  65. elif cmdtype == "cmd":
  66. if len(command.split()) == 1:
  67. self.disconnect(msg = "Reconnect requested by " + event.source.nick)
  68. else:
  69. self.disconnect(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
  70. elif command.split()[0] == "recovernick":
  71. if not userstatus.atleast_voiced(self, event.source.nick, self.homechannel):
  72. connection.privmsg(replyto, "Denied, you need to have voiced status or higher in " + red + self.homechannel + reset + ".")
  73. return
  74. if cmdtype == "help": # Display help text.
  75. if len(command.split()) is not 1:
  76. return
  77. connection.privmsg(replyto, "Let " + connection.get_nickname() + " try to recover " + connection.nickname + " as nickname.")
  78. elif cmdtype == "cmd":
  79. if connection.get_nickname() == connection.nickname:
  80. connection.action(replyto, "is already named " + red + connection.nickname + reset + ".")
  81. return
  82. from common.networkservices import NickServ
  83. NickServ.recover_nick(connection, self.password)
  84. elif command.split()[0] == "join":
  85. if not userstatus.atleast_oper(self, event.source.nick, self.homechannel):
  86. connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel + reset + ".")
  87. return
  88. if cmdtype == "help": #Display help text.
  89. if len(command.split()) is not 1:
  90. return
  91. connection.privmsg(replyto, "Make " + connection.get_nickname() + " join a channel. Password optional.")
  92. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "join " + red + italic + "channel " + reset + italic + "password")
  93. elif cmdtype == "cmd":
  94. try:
  95. channel = command.split()[1]
  96. except IndexError:
  97. connection.privmsg(replyto, "Specify channel. For help type: " + blue + self.helpchar + "join")
  98. return
  99. try:
  100. key = command.split(maxsplit=2)[2]
  101. except IndexError:
  102. connection.join(channel)
  103. return
  104. print(channel + " | " + key)
  105. connection.join(channel, key=key)
  106. elif command.split()[0] == "part":
  107. if cmdtype == "help": #Display help text.
  108. if len(command.split()) is not 1:
  109. return
  110. connection.privmsg(replyto, "Make " + connection.get_nickname() + " part a channel. Reason optional.")
  111. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "join " + red + italic + "channel " + reset + italic + "password")
  112. elif cmdtype == "cmd":
  113. if userstatus.atleast_oper(self, event.source.nick, self.homechannel): # Is at least operator in home channel.
  114. homeadmin = True
  115. if userstatus.atleast_oper(self, event.source.nick, event.target):
  116. targetadmin = True
  117. if len(command.split()) == 1: # No arguments.
  118. if event.target in self.channels: # It's a channel message.
  119. if not homeadmin and not targetadmin: # Insufficient rights:
  120. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + self.homechan + reset + " or " + red + event.target + reset + ".")
  121. return
  122. if event.target == self.homechannel:
  123. connection.action("shall not abandon it's home channel!")
  124. return
  125. connection.part(event.target, event.source.nick)
  126. else: # It's a PM.
  127. connection.privmsg(replyto, "Specify a channel to part. For help type " + blue + self.helpchar + "part" + reset + ".")
  128. elif len(command.split()) > 1: # Arguments
  129. if command.split()[1] not in self.channels: # First argument is not a channel the bot inhabits.
  130. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ". For help type " + blue + self.helpchar + "part" + reset + ".")
  131. return
  132. if not homeadmin and not userstatus.atleast_oper(self, event.source.nick, command.split()[1]): # Insufficient rights.
  133. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + self.homechan + reset + " or " + red + command.split()[1] + reset + ".")
  134. return
  135. if command.split()[1] == self.homechannel:
  136. connection.action("shall not abandon it's home channel!")
  137. return
  138. try:
  139. connection.part(command.split()[1], command.split(maxsplit=2)[2])
  140. except:
  141. connection.part(command.split()[1], event.source.nick)
  142. elif command.split()[0] == "msg" or command.split(maxsplit=1)[0] == "act":
  143. if cmdtype == "help": #Display help text.
  144. if len(command.split()) is not 1:
  145. return
  146. if command.split(maxsplit=1)[0] == "act":
  147. message = "Let " + connection.get_nickname() + " send an action to a channel."
  148. arguments = "action-message"
  149. else:
  150. message = "Let " + connection.get_nickname() + "send a message to a channel."
  151. arguments = "name message"
  152. connection.privmsg(replyto, message)
  153. connection.privmsg(replyto, grey + "Usage: " + blue + "!" + command.split(maxsplit=1)[0] + " " + reset + italic + arguments)
  154. elif cmdtype == "cmd":
  155. # Parse user input.
  156. try:
  157. destination = trigger.split()[1]
  158. except IndexError: # User did not specify a destination.
  159. connection.privmsg(replyto, "Destination not specified. For help type: " + blue + self.helpchar + command.split(maxsplit=1)[0])
  160. return
  161. try:
  162. message = trigger.split(maxsplit=2)[2]
  163. except IndexError: # User did not specify a message.
  164. connection.privmsg(replyto, "Message not specified. For help type: " + blue + self.helpchar + command.split(maxsplit=1)[0])
  165. return
  166. # Send the message if user has owner status in the home channel.
  167. if self.channels[self.homechannel].is_owner(event.source.nick):
  168. if destination == connection.get_nickname():
  169. connection.privmsg(replyto, "To prevent dying in a loop I shall not message myself.")
  170. return
  171. if command.split(maxsplit=1)[0] == "act":
  172. connection.action(destination, message)
  173. else:
  174. connection.privmsg(destination, message)
  175. # Reply error when bot does not inhabit destination channel.
  176. elif destination not in self.channels:
  177. connection.action(replyto, "does not inhabit " + red + destination + reset + ".")
  178. # Send message if user has at least operator status the home channel or the channel the message is intended for.
  179. elif userstatus.atleast_oper(self, event.source.nick, self.homechannel) or userstatus.atleast_oper(self, event.source.nick, destination):
  180. if command.split(maxsplit=1)[0] == "act":
  181. connection.action(destination, message)
  182. else:
  183. connection.privmsg(destination, message)
  184. # Reply error if user is not operator of destination channel.
  185. else:
  186. connection.privmsg(replyto, "Denied, you need to be an operator of " + red + destination + reset +".")
  187. elif command.split()[0] == "channelfunctions":
  188. if cmdtype == "help": #Display help text. # Help code block first, as it is impossible to predict for what channel a later command is going to be issued. Rights filtering after help test.
  189. if len(command.split()) is not 1:
  190. return
  191. connection.privmsg(replyto, "Display or toggle the status channel functions. Channel, function and value optional.")
  192. connection.privmsg(replyto, grey + "Usage: " + blue + "!channelfunctions " + red + italic + "channel " + reset + italic + "function value")
  193. elif cmdtype == "cmd":
  194. if len(command.split()) == 1: # No arguments.
  195. if event.target == connection.get_nickname(): # Command issued via PM.
  196. connection.privmsg(replyto, "Nothing to display, Specify a channel.")
  197. else: # Command issued as channel message.
  198. message = CH.get_channelfunctions(self, event.target)
  199. connection.privmsg(replyto, message)
  200. elif len(command.split()) == 2: # One argument.
  201. if command.split()[1] in self.channels: # Info requested on specific channel.
  202. message = CH.get_channelfunctions(self, command.split()[1])
  203. connection.privmsg(replyto, message)
  204. else: # First argument is not a channel the bot inhabits.
  205. connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  206. elif len(command.split()) == 3: # Two arguments.
  207. if event.target == connection.get_nickname(): # Command issued via PM.
  208. connection.privmsg(replyto, "One or three arguments required. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  209. else: # Command issued via channel.
  210. if not CH.is_channelfunction(self, command.split()[1]): # First argument is not a channelfunction.
  211. connection.privmsg(replyto, command.split()[1] + " is not a channel function. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  212. return
  213. if not command.split()[2].lower() in ["on", "off"] and not command.split()[1].lower() == "aggressiveness": # Second argument unsupported.
  214. connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  215. return
  216. if not userstatus.atleast_oper(self, event.source.nick, self.homechannel) and not userstatus.atleast_oper(self, event.source.nick, event.target): # Does not have operator status or higher in target or home channel.
  217. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
  218. return
  219. self.db.run("UPDATE channels SET " + command.split()[1].lower() + "='" + command.split()[2].lower() + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
  220. elif len(command.split()) == 4: # Three arguments.
  221. if not command.split()[1] in self.channels: # Bot does not inhabit channel to be altered.
  222. connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  223. return
  224. if not CH.is_channelfunction(self, command.split()[2]): # Function does not exist.
  225. connection.privmsg(replyto, command.split()[2] + " is not a valid channel function. For a list help type: " + blue + self.cmdchar + "channelfunctions" + red + italic + "channel")
  226. return
  227. if not command.split()[3].lower() in ["on", "off"] and not command.split()[2].lower() == "aggressiveness": # Third argument unsupported.
  228. connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  229. return
  230. if not userstatus.atleast_oper(self, event.source.nick, self.homechannel) and not userstatus.atleast_oper(self, event.source.nick, command.split()[1]): # Does not have operator status or higher in target or home channel.
  231. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
  232. return
  233. try:
  234. self.db.run("UPDATE channels SET " + command.split()[2].lower() + "='" + command.split()[3].lower() + "' WHERE LOWER(name)=LOWER('" + command.split()[1] + "') AND network='" + self.network + "'")
  235. except:
  236. connection.privmsg(replyto, "Error, database record not updated.")
  237. return
  238. else:
  239. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")