admin.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import secrets, string
  2. from common import userstatus, do_everything_to
  3. from commands.common import CommandHelpers as CH
  4. from commands.common import AdminHelpers as AH
  5. bold = "\x02"
  6. italic = "\x1D"
  7. underline = "\x1F"
  8. reverse = "\x16" # swap background and foreground colors ("reverse video")
  9. reset = "\x0F"
  10. blue = "\x0302"
  11. green = "\x0303"
  12. red = "\x0304"
  13. grey = "\x0314"
  14. def do_command(self, connection, event):
  15. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  16. if not command:
  17. return # Do nothing if there is no command.
  18. # Ignore channel commands from users that do not have at least voice in homechannel or operator status in target channel.
  19. if event.type == "pubmsg": # It's a channel message.
  20. 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.
  21. return
  22. if command == "cmd" or command == "cmds" or command == "commands":
  23. if cmdtype == "cmd":
  24. message = grey + "Admin: "
  25. if CH.ccc(self, "channelfunctions", {"homechan": "oper", "chan": "oper"}, event):
  26. message += CH.ccc(self, "channelfunctions", {"homechan": "oper", "chan": "oper"}, event)
  27. if CH.ccc(self, "join", {"homechan": "oper", "chan": None}, event):
  28. message += CH.ccc(self, "join", {"homechan": "oper", "chan": None}, event)
  29. if CH.ccc(self, "part", {"homechan": "oper", "chan": None}, event):
  30. message += CH.ccc(self, "part", {"homechan": "oper", "chan": None}, event)
  31. if CH.ccc(self, "quit", {"homechan": "admin", "chan": None}, event):
  32. message += CH.ccc(self, "quit", {"homechan": "admin", "chan": None}, event)
  33. if CH.ccc(self, "reconnect", {"homechan": "oper", "chan": None}, event):
  34. message += CH.ccc(self, "reconnect", {"homechan": "oper", "chan": None}, event)
  35. if CH.ccc(self, "recovernick", {"homechan": "oper", "chan": None}, event):
  36. message += CH.ccc(self, "recovernick", {"homechan": "oper", "chan": None}, event)
  37. if CH.ccc(self, "registernick", {"homechan": "owner", "chan": None}, event):
  38. message += CH.ccc(self, "registernick", {"homechan": "owner", "chan": None}, event)
  39. if CH.ccc(self, "msg", {"homechan": "oper", "chan": "oper"}, event):
  40. message += CH.ccc(self, "msg", {"homechan": "oper", "chan": "oper"}, event)
  41. if CH.ccc(self, "act", {"homechan": "oper", "chan": "oper"}, event):
  42. message += CH.ccc(self, "act", {"homechan": "oper", "chan": "oper"}, event)
  43. if message == grey + "Admin: ": # No commands to display.
  44. return
  45. connection.privmsg(replyto, message[:-2] + ".")
  46. elif command.split()[0] == "quit":
  47. if not userstatus.atleast_admin(self, event.source.nick, self.homechannel): #Insufficient rights.
  48. connection.privmsg(replyto, "Denied, you need to have admin (super operator) status or higher in " + red + self.homechannel + reset + ".")
  49. return
  50. if cmdtype == "help": # Display help text.
  51. if len(command.split()) is not 1:
  52. return
  53. connection.privmsg(replyto, "Disconnect and terminate " + connection.get_nickname() + ". Optionally with reason.")
  54. connection.privmsg(replyto, grey + "Usage: " + blue + "!quit " + reset + italic + "reason")
  55. elif cmdtype == "cmd":
  56. if len(command.split()) == 1:
  57. self.die(msg = "Killed by " + event.source.nick)
  58. else:
  59. self.die(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
  60. elif command.split()[0] == "reconnect":
  61. if not userstatus.atleast_oper(self, event.source.nick, self.homechannel):
  62. connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel + reset + ".")
  63. return
  64. if cmdtype == "help": # Display help text.
  65. if len(command.split()) is not 1:
  66. return
  67. connection.privmsg(replyto, "Reconnect " + connection.get_nickname() + ". Reason optional.")
  68. connection.privmsg(replyto, grey + "Usage: " + blue + "!reconnect " + reset + italic + "reason")
  69. elif cmdtype == "cmd":
  70. if len(command.split()) == 1:
  71. self.disconnect(msg = "Reconnect requested by " + event.source.nick)
  72. else:
  73. self.disconnect(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
  74. elif command.split()[0] == "recovernick":
  75. if not userstatus.atleast_voiced(self, event.source.nick, self.homechannel):
  76. connection.privmsg(replyto, "Denied, you need to have voiced status or higher in " + red + self.homechannel + reset + ".")
  77. return
  78. if cmdtype == "help": # Display help text.
  79. if len(command.split()) is not 1:
  80. return
  81. connection.privmsg(replyto, "Let " + connection.get_nickname() + " try to recover " + connection.nickname + " as nickname.")
  82. elif cmdtype == "cmd":
  83. if connection.get_nickname() == connection.nickname:
  84. connection.action(replyto, "is already named " + red + connection.nickname + reset + ".")
  85. return
  86. from common.networkservices import NickServ
  87. NickServ.recover_nick(connection, self.password)
  88. elif command.split()[0] == "join":
  89. if not userstatus.atleast_oper(self, event.source.nick, self.homechannel):
  90. connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel + reset + ".")
  91. return
  92. if cmdtype == "help": #Display help text.
  93. if len(command.split()) is not 1:
  94. return
  95. connection.privmsg(replyto, "Make " + connection.get_nickname() + " join a channel. Password optional.")
  96. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "join " + red + italic + "channel " + reset + italic + "password")
  97. elif cmdtype == "cmd":
  98. try:
  99. channel = command.split()[1]
  100. except IndexError:
  101. connection.privmsg(replyto, "Specify channel. For help type: " + blue + self.helpchar + "join")
  102. return
  103. try:
  104. key = command.split(maxsplit=2)[2]
  105. except IndexError:
  106. do_everything_to.join(self, connection, channel)
  107. return
  108. do_everything_to.join(self, connection, channel, key)
  109. elif command.split()[0] == "part":
  110. if cmdtype == "help": #Display help text.
  111. if len(command.split()) is not 1:
  112. return
  113. connection.privmsg(replyto, "Make " + connection.get_nickname() + " part a channel. Reason optional.")
  114. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "join " + red + italic + "channel " + reset + italic + "password")
  115. elif cmdtype == "cmd":
  116. homeadmin = False
  117. if userstatus.atleast_oper(self, event.source.nick, self.homechannel): # Is at least operator in home channel.
  118. homeadmin = True
  119. targetadmin = False
  120. if userstatus.atleast_oper(self, event.source.nick, event.target):
  121. targetadmin = True
  122. if len(command.split()) == 1: # No arguments.
  123. if event.target in self.channels: # It's a channel message.
  124. if not homeadmin and not targetadmin: # Insufficient rights:
  125. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + self.homechan + reset + " or " + red + event.target + reset + ".")
  126. return
  127. if event.target == self.homechannel:
  128. connection.action("shall not abandon it's home channel!")
  129. return
  130. connection.part(event.target, event.source.nick)
  131. else: # It's a PM.
  132. connection.privmsg(replyto, "Specify a channel to part. For help type " + blue + self.helpchar + "part" + reset + ".")
  133. elif len(command.split()) > 1: # Arguments
  134. if command.split()[1] not in self.channels: # First argument is not a channel the bot inhabits.
  135. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ". For help type " + blue + self.helpchar + "part" + reset + ".")
  136. return
  137. if not homeadmin and not userstatus.atleast_oper(self, event.source.nick, command.split()[1]): # Insufficient rights.
  138. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + self.homechan + reset + " or " + red + command.split()[1] + reset + ".")
  139. return
  140. if command.split()[1] == self.homechannel:
  141. connection.action("shall not abandon it's home channel!")
  142. return
  143. try:
  144. connection.part(command.split()[1], command.split(maxsplit=2)[2])
  145. except:
  146. connection.part(command.split()[1], event.source.nick)
  147. elif command.split()[0] == "msg" or command.split(maxsplit=1)[0] == "act":
  148. if cmdtype == "help": #Display help text.
  149. if len(command.split()) is not 1:
  150. return
  151. if command.split(maxsplit=1)[0] == "act":
  152. message = "Let " + connection.get_nickname() + " send an action to a channel."
  153. arguments = "action-message"
  154. else:
  155. message = "Let " + connection.get_nickname() + "send a message to a channel."
  156. arguments = "name message"
  157. connection.privmsg(replyto, message)
  158. connection.privmsg(replyto, grey + "Usage: " + blue + "!" + command.split(maxsplit=1)[0] + reset + italic + " target " + arguments)
  159. elif cmdtype == "cmd":
  160. # Parse user input.
  161. try:
  162. destination = trigger.split()[1]
  163. except IndexError: # User did not specify a destination.
  164. connection.privmsg(replyto, "Destination not specified. For help type: " + blue + self.helpchar + command.split(maxsplit=1)[0])
  165. return
  166. try:
  167. message = trigger.split(maxsplit=2)[2]
  168. except IndexError: # User did not specify a message.
  169. connection.privmsg(replyto, "Message not specified. For help type: " + blue + self.helpchar + command.split(maxsplit=1)[0])
  170. return
  171. # Send the message if user has owner status in the home channel.
  172. if self.channels[self.homechannel].is_owner(event.source.nick):
  173. if destination == connection.get_nickname():
  174. connection.privmsg(replyto, "To prevent dying in a loop I shall not message myself.")
  175. return
  176. if command.split(maxsplit=1)[0] == "act":
  177. connection.action(destination, message)
  178. else:
  179. connection.privmsg(destination, message)
  180. # Reply error when bot does not inhabit destination channel.
  181. elif destination not in self.channels:
  182. connection.action(replyto, "does not inhabit " + red + destination + reset + ".")
  183. # Send message if user has at least operator status the home channel or the channel the message is intended for.
  184. elif userstatus.atleast_oper(self, event.source.nick, self.homechannel) or userstatus.atleast_oper(self, event.source.nick, destination):
  185. if command.split(maxsplit=1)[0] == "act":
  186. connection.action(destination, message)
  187. else:
  188. connection.privmsg(destination, message)
  189. # Reply error if user is not operator of destination channel.
  190. else:
  191. connection.privmsg(replyto, "Denied, you need to be an operator of " + red + destination + reset +".")
  192. elif command.split()[0] == "channelfunctions":
  193. 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.
  194. if len(command.split()) is not 1:
  195. return
  196. connection.privmsg(replyto, "Display or toggle the status channel functions. Channel, function and value optional. Get a description of a functio via the help argument.")
  197. connection.privmsg(replyto, grey + "Usage: " + blue + "!channelfunctions " + red + italic + "channel " + reset + italic + "function value")
  198. connection.privmsg(replyto, grey + "Usage: " + blue + "!channelfunctions describe " + reset + italic + "function")
  199. elif cmdtype == "cmd":
  200. if len(command.split()) == 1: # No arguments.
  201. if event.target == connection.get_nickname(): # Command issued via PM.
  202. connection.privmsg(replyto, "Nothing to display, Specify a channel.")
  203. else: # Command issued as channel message.
  204. message = AH.get_channelfunctions(self, event.target)
  205. connection.privmsg(replyto, message)
  206. elif len(command.split()) == 2: # One argument.
  207. if command.split()[1] in self.channels: # Info requested on specific channel.
  208. message = AH.get_channelfunctions(self, command.split()[1])
  209. connection.privmsg(replyto, message)
  210. else: # First argument is not a channel the bot inhabits.
  211. if not command.split()[1].lower() == "help": # Not a help request.
  212. connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  213. else: # Help request.
  214. connection.privmsg(replyto, "Specify a channel function to get a description of. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  215. elif len(command.split()) == 3: # Two arguments.
  216. channel = event.target
  217. if event.target == connection.get_nickname(): # Command issued via PM.
  218. connection.privmsg(replyto, "One or three arguments required. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  219. else: # Command issued via channel.
  220. if not AH.is_channelfunction(command.split()[1]): # First argument is not a channelfunction.
  221. if not command.split()[1].lower() == "describe": # Not a help request.
  222. connection.privmsg(replyto, command.split()[1] + " is not a channel function. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  223. return
  224. if not AH.is_channelfunction(command.split()[2]): # Second argument not a channel function.
  225. connection.privmsg(replyto, command.split()[2] + " is not a channel function.")
  226. return
  227. connection.privmsg(replyto, AH.describe_channelfunction(command.split()[2]))
  228. return
  229. # Second argument unsupported.
  230. if not command.split()[2].lower() in ["on", "off"]:
  231. if command.split()[1].lower() == "aggressiveness":
  232. if not AH.is_aggressiveness(command.split()[2].lower()): # Is not an aggressiveness setting.
  233. connection.privmsg(replyto, command.split()[2] + " is not an aggressiveness setting. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  234. return
  235. else: # Channel function is not aggresiveness.
  236. connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  237. return
  238. 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.
  239. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
  240. return
  241. if command.split()[1].lower() == "autojoin" and event.target == self.homechannel: # Chaning autojoin of homechannel.
  242. connection.action(replyto, "will always join it's homechannel " + red + self.homechannel + reset + ", regardless of the autojoin function.")
  243. #self.db.run("UPDATE channels SET " + command.split()[1].lower() + "='" + command.split()[2].lower() + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
  244. self.db.run("UPDATE channels SET " + command.split()[1].lower() + "=%s WHERE name='" + event.target + "' AND network='" + self.network + "'", (command.split()[2].lower(), ))
  245. elif len(command.split()) == 4: # Three arguments.
  246. if not command.split()[1] in self.channels: # Bot does not inhabit channel to be altered.
  247. connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  248. return
  249. if not AH.is_channelfunction(command.split()[2]): # Function does not exist.
  250. connection.privmsg(replyto, command.split()[2] + " is not a valid channel function. For a list help type: " + blue + self.cmdchar + "channelfunctions" + red + italic + "channel")
  251. return
  252. if not command.split()[3].lower() in ["on", "off"] and not command.split()[2].lower() == "aggressiveness": # Third argument unsupported.
  253. connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  254. return
  255. 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.
  256. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
  257. return
  258. if command.split()[2].lower() == "autojoin" and command.split()[1] == self.homechannel: # Chaning autojoin of homechannel.
  259. connection.action(replyto, "will always join it's homechannel " + red + self.homechannel + reset + ", regardless of the autojoin function.")
  260. try:
  261. 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 + "'")
  262. except:
  263. connection.privmsg(replyto, "Error, database record not updated.")
  264. return
  265. else: # Too many arguments.
  266. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  267. elif command.split()[0] == "registernick":
  268. if not userstatus.atleast_owner(self, event.source.nick, self.homechannel): #Insufficient rights.
  269. connection.privmsg(replyto, "Denied, you need to be the owner of " + red + self.homechannel + reset + ".")
  270. return
  271. if cmdtype == "help": # Display help text.
  272. if len(command.split()) is not 1:
  273. return
  274. connection.privmsg(replyto, "Register with NickServ.")
  275. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + "registernick " + reset + italic + "email")
  276. elif cmdtype == "cmd":
  277. if len(command.split()) == 1:
  278. connection.privmsg(replyto, "Insufficient arguments. For help type " + blue + self.helpchar + "registernick" + reset + ".")
  279. elif len(command.split()) > 2:
  280. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "registernick" + reset + ".")
  281. elif not self.db.one("SELECT password FROM networks WHERE name='" + self.network + "'"):
  282. alphabet = string.ascii_letters + string.digits
  283. password = ''.join(secrets.choice(alphabet) for i in range(20)) # 20-character password.
  284. self.db.run("UPDATE networks SET password=%s WHERE name=%s", (password, self.network))
  285. connection.privmsg("NickServ", "REGISTER " + password + " " + trigger.split()[1])
  286. else:
  287. connection.action("NickServ", "REGISTER " + self.db.one("SELECT password FROM networks WHERE name='" + self.network + "'") + " " + trigger.split()[1])