admin.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. from commands.common import CommandHelpers as CH
  2. bold = "\x02"
  3. italic = "\x1D"
  4. underline = "\x1F"
  5. reverse = "\x16" # swap background and foreground colors ("reverse video")
  6. reset = "\x0F"
  7. blue = "\x0302"
  8. green = "\x0303"
  9. red = "\x0304"
  10. grey = "\x0314"
  11. def do_command(self, connection, event):
  12. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  13. # Ignore channel commands from users that do not have at least voice in homechannel or operator status in target channel.
  14. if event.type == "pubmsg": # It's a channel message.
  15. 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.
  16. return
  17. if command == "cmd" or command == "commands":
  18. if cmdtype == "cmd":
  19. connection.privmsg(replyto, grey + "Admin: " + CH.ccc(self, "channelfunctions", {"homechan": "oper", "chan": "oper"}, event) + CH.ccc(self, "join", {"homechan": "oper", "chan": None}, event) + CH.ccc(self, "die", {"homechan": "admin", "chan": None}, event) + CH.ccc(self, "reconnect", {"homechan": "oper", "chan": None}, event) + CH.ccc(self, "recovernick", {"homechan": "oper", "chan": None}, event)+ CH.ccc(self, "msg", {"homechan": "oper", "chan": "oper"}, event) + CH.ccc(self, "act", {"homechan": "oper", "chan": "oper"}, event)[:-2] + ".")
  20. elif command.split()[0] == "channelfunctions":
  21. 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.
  22. if len(command.split()) is not 1:
  23. return
  24. connection.privmsg(replyto, "Display or toggle the status channel functions. Channel, function and value optional.")
  25. connection.privmsg(replyto, grey + "Usage: " + blue + "!channelfunctions " + red + italic + "channel " + reset + italic + "function value")
  26. elif cmdtype == "cmd":
  27. if len(command.split()) == 1: # No arguments.
  28. if event.target == connection.get_nickname(): # Command issued via PM.
  29. connection.privmsg(replyto, "Nothing to display, Specify a channel.")
  30. else: # Command issued as channel message.
  31. message = CH.get_channelfunctions(self, event.target)
  32. connection.privmsg(replyto, message)
  33. elif len(command.split()) == 2: # One argument.
  34. if command.split()[1] in self.channels: # Info requested on specific channel.
  35. message = CH.get_channelfunctions(self, command.split()[1])
  36. connection.privmsg(replyto, message)
  37. else: # First argument is not a channel the bot inhabits.
  38. connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  39. elif len(command.split()) == 3: # Two arguments.
  40. if event.target == connection.get_nickname(): # Command issued via PM.
  41. connection.privmsg(replyto, "One or three arguments required. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  42. else: # Command issued via channel.
  43. if not CH.is_channelfunction(self, command.split()[1]): # First argument is not a channelfunction.
  44. connection.privmsg(replyto, command.split()[1] + " is not a channel function. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  45. return
  46. if not command.split()[2].lower() in ["on", "off"]: # Second argument is not "on" or "off".
  47. connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  48. return
  49. 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.
  50. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
  51. return
  52. self.db.run("UPDATE channels SET " + command.split()[1] + "='" + command.split()[2].lower() + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
  53. elif len(command.split()) == 4: # Three arguments.
  54. if not command.split()[1] in self.channels: # Bot does not inhabit channel to be altered.
  55. connection.privmsg(replyto, command.split()[1] + " is not a channel I inhabit. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  56. return
  57. if not CH.is_channelfunction(self, command.split()[2]): # Function does not exist.
  58. connection.privmsg(replyto, command.split()[2] + " is not a valid channel function. For a list help type: " + blue + self.cmdchar + "channelfunctions" + red + italic + "channel")
  59. return
  60. if not command.split()[3] in ["on", "off"]: # Third argument is not "on" or "off".
  61. connection.privmsg(replyto, "The value of this channel function can only be \"on\" or \"off\". For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  62. return
  63. 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.
  64. connection.privmsg(replyto, "Denied. You need to have at least operator status in " + red + event.target + reset + " or " + red + self.homechannel + reset + ".")
  65. return
  66. try:
  67. self.db.run("UPDATE channels SET " + command.split()[2] + "='" + command.split()[3] + "' WHERE name='" + command.split()[1] + "' AND network='" + self.network + "'")
  68. except:
  69. connection.privmsg(replyto, "Error, database record not updated.")
  70. return
  71. else:
  72. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "channelfunctions" + reset + ".")
  73. elif command.split()[0] == "join":
  74. 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):
  75. connection.privmsg(replyto, "Denied, you need to have admin (super operator) status or higher in " + red + self.homechannel + reset + ".")
  76. return
  77. if cmdtype == "help": #Display help text.
  78. if len(command.split()) is not 1:
  79. return
  80. connection.privmsg(replyto, "Make " + connection.get_nickname() + " join a channel. Password optional.")
  81. connection.privmsg(replyto, grey + "Usage: " + blue + "!join " + red + italic + "channel " + reset + italic + "password")
  82. elif cmdtype == "cmd":
  83. try:
  84. channel = command.split()[1]
  85. except IndexError:
  86. connection.privmsg(replyto, "Specify channel. For help type: " + blue + self.helpchar + "join")
  87. return
  88. try:
  89. key = command.split(maxsplit=2)[2]
  90. except IndexError:
  91. connection.join(channel)
  92. return
  93. print(channel + " | " + key)
  94. connection.join(channel, key=key)
  95. elif command.split()[0] == "die":
  96. if not self.channels[self.homechannel].is_owner(event.source.nick) and not self.channels[self.homechannel].is_admin(event.source.nick):
  97. connection.privmsg(replyto, "Denied, you need to have admin (super operator) status or higher in " + red + self.homechannel + reset + ".")
  98. return
  99. if cmdtype == "help": # Display help text.
  100. if len(command.split()) is not 1:
  101. return
  102. connection.privmsg(replyto, "Kill " + connection.get_nickname() + ". Optionally with reason.")
  103. connection.privmsg(replyto, grey + "Usage: " + blue + "!die " + reset + italic + "reason")
  104. elif cmdtype == "cmd":
  105. if len(command.split()) == 1:
  106. self.die(msg = "Killed by " + event.source.nick)
  107. else:
  108. self.die(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
  109. elif command.split()[0] == "reconnect":
  110. 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):
  111. connection.privmsg(replyto, "Denied, you need to have operator status or higher in " + red + self.homechannel + reset + ".")
  112. return
  113. if cmdtype == "help": # Display help text.
  114. if len(command.split()) is not 1:
  115. return
  116. connection.privmsg(replyto, "Reconnect " + connection.get_nickname() + ". Reason optional.")
  117. connection.privmsg(replyto, grey + "Usage: " + blue + "!reconnect " + reset + italic + "reason")
  118. elif cmdtype == "cmd":
  119. if len(command.split()) == 1:
  120. self.disconnect(msg = "reconnect requested by " + event.source.nick)
  121. else:
  122. self.disconnect(msg = "[" + event.source.nick + "] " + command.split(maxsplit=1)[1])
  123. elif command.split()[0] == "recovernick":
  124. 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):
  125. connection.privmsg(replyto, "Denied, you need to have voiced status or higher in " + red + self.homechannel + reset + ".")
  126. return
  127. if cmdtype == "help": # Display help text.
  128. if len(command.split()) is not 1:
  129. return
  130. connection.privmsg(replyto, "Let " + connection.get_nickname() + " try to recover " + connection.nickname + " as nickname.")
  131. elif cmdtype == "cmd":
  132. if connection.get_nickname() == connection.nickname:
  133. connection.action(replyto, "is already named " + red + connection.nickname + reset + ".")
  134. return
  135. from common.networkservices import NickServ
  136. NickServ.recover_nick(connection, self.password)
  137. elif command.split()[0] == "msg" or command.split(maxsplit=1)[0] == "act":
  138. if cmdtype == "help": #Display help text.
  139. if len(command.split()) is not 1:
  140. return
  141. if command.split(maxsplit=1)[0] == "act":
  142. message = "Let " + connection.get_nickname() + " send an action to a channel."
  143. arguments = "action-message"
  144. else:
  145. message = "Let " + connection.get_nickname() + "send a message to a channel."
  146. arguments = "name message"
  147. connection.privmsg(replyto, message)
  148. connection.privmsg(replyto, grey + "Usage: " + blue + "!" + command.split(maxsplit=1)[0] + " " + reset + italic + arguments)
  149. elif cmdtype == "cmd":
  150. # Parse user input.
  151. try:
  152. destination = trigger.split()[1]
  153. except IndexError: # User did not specify a destination.
  154. connection.privmsg(replyto, "Destination not specified. For help type: " + blue + self.helpchar + command.split(maxsplit=1)[0])
  155. return
  156. try:
  157. message = trigger.split(maxsplit=2)[2]
  158. except IndexError: # User did not specify a message.
  159. connection.privmsg(replyto, "Message not specified. For help type: " + blue + self.helpchar + command.split(maxsplit=1)[0])
  160. return
  161. # Send the message if user has owner status in the home channel.
  162. if self.channels[self.homechannel].is_owner(event.source.nick):
  163. if destination == connection.get_nickname():
  164. connection.privmsg(replyto, "To prevent dying in a loop I shall not message myself.")
  165. return
  166. if command.split(maxsplit=1)[0] == "act":
  167. connection.action(destination, message)
  168. else:
  169. connection.privmsg(destination, message)
  170. # Reply error when bot does not inhabit destination channel.
  171. elif destination not in self.channels:
  172. connection.action(replyto, "does not inhabit " + red + destination + reset + ".")
  173. # Send message if user has at least operator status the home channel or the channel the message is intended for.
  174. 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):
  175. if command.split(maxsplit=1)[0] == "act":
  176. connection.action(destination, message)
  177. else:
  178. connection.privmsg(destination, message)
  179. # Reply error if user is not operator of destination channel.
  180. else:
  181. connection.privmsg(replyto, "Denied, you need to be an operator of " + red + destination + reset +".")