admin.py 19 KB

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