admin.py 18 KB

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