on_kick.py 7.4 KB

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