admin.py 18 KB

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