on_kick.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from common.networkservices import ChanServ
  2. from common import userstatus, do_everything_to
  3. from events.common import Aggressiveness, Lastact
  4. bold = "\x02"
  5. italic = "\x1D"
  6. underline = "\x1F"
  7. reverse = "\x16" # swap background and foreground colors ("reverse video")
  8. reset = "\x0F"
  9. blue = "\x0302"
  10. green = "\x0303"
  11. red = "\x0304"
  12. grey = "\x0314"
  13. def process_event(self, connection, event):
  14. kicker = event.source.nick
  15. channel = event.target
  16. kicked = event.arguments[0]
  17. reason = event.arguments[1]
  18. # Update last act.
  19. if reason:
  20. Lastact.update(self, kicker, "kick", channel=channel, lastact=kicked, auxiliary=reason)
  21. Lastact.update(self, kicked, "kicked", channel=channel, lastact=kicked, auxiliary=reason)
  22. else:
  23. Lastact.update(self, kicker, "kick", channel=channel, lastact=kicked)
  24. Lastact.update(self, kicked, "kicked", channel=channel, lastact=kicked)
  25. # Create user records if they don't exist.
  26. if not self.db.one("SELECT id FROM users WHERE name='" + kicker + "' AND network='" + self.network + "'"): # Kicker does not have a user record.
  27. self.db.run("INSERT INTO \"users\" (name, network) VALUES ('" + kicker + "', '" + self.network + "')") # Create user record.
  28. if not self.db.one("SELECT id FROM users WHERE name='" + kicked + "' AND network='" + self.network + "'"): # Kicked does not have a user record.
  29. self.db.run("INSERT INTO \"users\" (name, network) VALUES ('" + kicked + "', '" + self.network + "')") # Create user record.
  30. # Create kick records if they don't exist.
  31. if not self.db.one("SELECT id FROM kicks WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicker + "' AND user_network='" + self.network + "'"): # No records for kicker channel combination.
  32. self.db.run("INSERT INTO kicks (channel, channel_network, \"user\", user_network) VALUES ('" + channel + "', '" + self.network + "', '" + kicker + "', '" + self.network + "')")
  33. if not self.db.one("SELECT id FROM kicks WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicked + "' AND user_network='" + self.network + "'"): # No records for kicked channel combination.
  34. self.db.run("INSERT INTO kicks (channel, channel_network, \"user\", user_network) VALUES ('" + channel + "', '" + self.network + "', '" + kicked + "', '" + self.network + "')")
  35. # Save statistic.
  36. self.db.run("UPDATE kicks SET given = given + 1 WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicker + "' AND user_network='" + self.network + "'")
  37. self.db.run("UPDATE kicks SET received = received + 1 WHERE channel='" + channel + "'AND channel_network='" + self.network + "' AND \"user\"='" + kicked + "' AND user_network='" + self.network + "'")
  38. # Update protectees if needed.
  39. if channel == self.homechannel: # Kicked from home channel
  40. if event.source.nick in self.protectees: # Protectee kicked.
  41. del self.protectees[event.source.nick] # Remove old nick from list.
  42. # Do nothing more when user is not protected.
  43. if not userstatus.atleast_halfop(self, kicked, self.homechannel) and not kicked == connection.get_nickname():
  44. return
  45. # Report.
  46. if not channel == self.homechannel: # Not kicked from homechannel.
  47. if reason:
  48. connection.privmsg(self.homechannel, red + kicked + reset + " has been kicked from " + red + channel + reset + " by " + red + kicker + reset + ": " + green + reason)
  49. else:
  50. connection.privmsg(self.homechannel, red + kicked + reset + " has been kicked from " + red + channel + reset + " by " + red + kicker + reset + ".")
  51. # React.
  52. print("Reacting to kick")
  53. behaviour = self.db.one("SELECT aggressiveness FROM channels WHERE name='" + channel + "' AND network='" + self.network + "'")
  54. if behaviour == "passive": # Passive behaviour.
  55. if kicked == connection.get_nickname() and channel == self.homechannel: # Bot was kicked from it's home channel.
  56. ChanServ.unban(connection, channel, kicked)
  57. ChanServ.akick_del(connection, channel, kicked)
  58. connection.privmsg("ChanServ", "UNBAN " + channel)
  59. do_everything_to.join(self, connection, self.homechannel)
  60. elif behaviour == "defense_only": # Defensive behaviour.
  61. ChanServ.unban(connection, channel, kicked)
  62. ChanServ.akick_del(connection, channel, kicked)
  63. if kicked == connection.get_nickname(): # Bot was kicked.
  64. connection.privmsg("ChanServ", "UNBAN " + channel)
  65. do_everything_to.join(self, connection, channel)
  66. elif behaviour == "equal_retalliation": # Equal retalitory behaviour.
  67. ChanServ.akick_del(connection, channel, kicked)
  68. # Rejoin if bot was kicked.
  69. if kicked == connection.get_nickname():
  70. connection.privmsg("ChanServ", "UNBAN " + channel)
  71. do_everything_to.join(self, connection, self.homechannel)
  72. if event.source.nick == connection.get_nickname() or self.channels[self.homechannel].is_owner(kicker):
  73. return # Stop if offender is bot or owner.
  74. if not userstatus.atleast_halfop(self, kicked, self.homechannel) or not kicked == connection.get_nickname():
  75. return # Stop if offended is not atleast halfop and is not the bot itself.
  76. if userstatus.atleast_halfop(self, kicker, self.homechannel) and not self.channels[self.homechannel].is_owner(kicked):
  77. return # Stop if offender is at least halfop in the home channel and the offended is not owner.
  78. # Kick.
  79. do_everything_to.kick(connection, channel, kicker, Aggressiveness.retalliation_reason(self, connection, kicked, behaviour))
  80. # Battlebot behaviour.
  81. elif behaviour == "battlebot":
  82. ChanServ.akick_del(connection, channel, kicked)
  83. # Rejoin if bot was kicked.
  84. if kicked == connection.get_nickname():
  85. print("Rejoining " + channel)
  86. do_everything_to.join(self, connection, self.homechannel)
  87. if event.source.nick == connection.get_nickname() or self.channels[self.homechannel].is_owner(kicker):
  88. print("Stop if offender is bot or owner")
  89. return # Stop if offender is bot or owner.
  90. if not userstatus.atleast_halfop(self, kicked, self.homechannel) or not kicked == connection.get_nickname():
  91. print("Stop if offended is not atleast halfop and is not the bot itself")
  92. return # Stop if offended is not atleast halfop and is not the bot itself.
  93. if userstatus.atleast_halfop(self, kicker, self.homechannel) and not self.channels[self.homechannel].is_owner(kicked):
  94. print("Stop if offender is at least halfop in the home channel and the offended is not owner")
  95. return # Stop if offender is at least halfop in the home channel and the offended is not owner.
  96. print("Kickbanning")
  97. ChanServ.tempban(connection, channel, kicker, "1h", "Aggression channel function = equal_retalliation: " + kicked)
  98. connection.mode(channel, "+e " + kicked) # Excempt operator.
  99. ChanServ.akick_add(connection, channel, kicker) # Add kicker to ChanServs autokick.
  100. do_everything_to.bankick(connection, channel, kicker, event.source, "Aggression channel function = equal_retalliation: " + kicked)