on_mode.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import fnmatch
  2. from irc.modes import parse_channel_modes
  3. from common.networkservices import ChanServ
  4. from common import do_everything_to, userstatus
  5. from events.common import Aggressiveness, Lastact
  6. bold = "\x02"
  7. italic = "\x1D"
  8. underline = "\x1F"
  9. reverse = "\x16" # swap background and foreground colors ("reverse video")
  10. reset = "\x0F"
  11. blue = "\x0302"
  12. green = "\x0303"
  13. red = "\x0304"
  14. grey = "\x0314"
  15. def process_event(self, connection, event):
  16. # Update protectees.
  17. if event.target == self.homechannel: # Home channel
  18. if any(mode in event.arguments[0][-1:] for mode in ("q", "a", "o", "h")): # Atleast halfop.
  19. connection.who(event.arguments[1]) # Get whorepy to update protectees.
  20. # Update last act.
  21. Lastact.update(self, event.source.nick, "mode", auxiliary=event.target)
  22. # React.
  23. modes = parse_channel_modes(" ".join(event.arguments))
  24. behaviour = self.db.one("SELECT aggressiveness FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
  25. for idx, mode in enumerate(modes):
  26. # Report.
  27. if not event.target == self.homechannel: # Not in home channel.
  28. for protectee in self.protectees:
  29. if mode[1] == "b" and fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]) and mode[0] == "+": # Protectee banned.
  30. connection.privmsg(self.homechannel, red + protectee + reset + " banned from " + red + event.target + reset + " by " + red + event.source.nick + reset + ": " + green + mode[2])
  31. if mode[1] == "e" and fnmatch.fnmatch(self.protectees[protectee]['ident'], event.arguments[idx + 1]) and mode[0] == "-": # Protectee's exception removed.
  32. connection.privmsg(self.homechannel, red + protectee + reset + " has had their exception removed from " + red + event.target + reset + " by " + red + event.source.nick + reset + ": " + green + event.arguments[idx + 1])
  33. # Track channel keys.
  34. if mode[1] == "k": # Channel key changed.
  35. if mode[0] == "+": # Key set.
  36. self.db.run("UPDATE channels SET key='" + mode[2] + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
  37. else: # Key removed.
  38. self.db.run("UPDATE channels SET key=NULL WHERE name='" + event.target + "' AND network='" + self.network + "'")
  39. # Unban if bot is banned from home channel
  40. if mode[1] == "b" and fnmatch.fnmatch(self.protectees[connection.get_nickname()]['ident'], mode[2]) and mode[0] == "+" and event.target == self.homechannel:
  41. connection.privmsg("ChanServ", "UNBAN")
  42. do_everything_to.unban(connection, event.target, connection.get_nickname(), mode[2])
  43. connection.mode(event.target, "-b " + mode[2])
  44. # Stop if offender is bot or bot owner.
  45. if event.source.nick == connection.get_nickname() or self.channels[self.homechannel].is_owner(event.source.nick):
  46. return
  47. for protectee in self.protectees:
  48. # Stop if offender is atleast halfop in the home channel and offended is not owner.
  49. if userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not self.channels[self.homechannel].is_owner(protectee):
  50. return
  51. if behaviour == "passive": # Passive behaviour.
  52. return
  53. elif behaviour == "defense_only": # Defensive only behaviour.
  54. if mode[1] == "b" and mode[0] == "+": # Ban.
  55. #for protectee in self.protectees:
  56. if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]): # Protectee.
  57. do_everything_to.unban(connection, event.target, protectee, mode[2])
  58. elif mode[1] == "e" and mode[0] == "-": # Removed exception.
  59. #for protectee in self.protectees:
  60. if fnmatch.fnmatch(self.protectees[protectee]['ident'], event.arguments[idx + 1]): # Protectee.
  61. connection.mode(event.target, "+e " + mode[2])
  62. elif behaviour == "equal_retalliation": # Equal retaliatory behaviour.
  63. if modes[1] == "b" and mode[0] == "+": # Ban.
  64. #for protectee in self.protectees:
  65. if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]): # Protectee.
  66. do_everything_to.unban(connection, event.target, protectee, mode[2])
  67. if protectee == connection.get_nickname(): # Bot banned.
  68. ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = equal_retalliation.")
  69. else:
  70. ChanServ.ban(connection, event.target, event.source.nick, "Aggression channel function = equal_retalliation: " + protectee + " is an operator of " + connection.get_nickname() + ".")
  71. connection.mode(event.target, "+b " + event.source)
  72. elif mode[1] == "e" and mode[0] == "-": # Removed exception.
  73. #for protectee in self.protectees:
  74. if fnmatch.fnmatch(self.protectees[protectee]['ident'], event.arguments[idx + 1]): # Protectee.
  75. connection.mode(event.target, "+e " + event.arguments[idx + 1])
  76. elif behaviour == "battlebot": # Battlebot behaviour.
  77. if mode[1] == "b" and mode[0] == "+": # Ban.
  78. #for protectee in self.protectees:
  79. if fnmatch.fnmatch(self.protectees[protectee]['ident'], mode[2]): # Protectee.
  80. do_everything_to.unban(connection, event.target, protectee, mode[2])
  81. do_everything_to.ban(connection, event.target, event.source.nick, event.source, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))
  82. connection.mode(event.target, "+e " + mode[2])
  83. ChanServ.akick_add(connection, event.target, event.source.nick)
  84. do_everything_to.kick(connection, event.target, event.source.nick, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))
  85. elif mode[1] == "e" and mode[0] == "-": # Removed exception.
  86. #for protectee in self.protectees:
  87. if fnmatch.fnmatch(self.protectees[protectee]['ident'], event.arguments[idx + 1]): # Protectee.
  88. do_everything_to.ban(connection, event.target, event.source.nick, event.source, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))
  89. connection.mode(event.target, "+e " + event.arguments[idx + 1])
  90. ChanServ.akick_add(connection, event.target, event.source.nick)
  91. do_everything_to.kick(connection, event.target, event.source.nick, Aggressiveness.retalliation_reason(self, connection, protectee, behaviour))