Class: Cinch::Plugins::AntiSpam

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/anti_spam.rb

Defined Under Namespace

Classes: Abuser

Constant Summary collapse

PERIOD_MULTIPLIER =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AntiSpam


40
41
42
43
# File 'lib/cinch/plugins/anti_spam.rb', line 40

def initialize(*args)
  super
  @command_abusers = {}
end

Instance Attribute Details

#command_abusersObject (readonly)

Returns the value of attribute command_abusers


7
8
9
# File 'lib/cinch/plugins/anti_spam.rb', line 7

def command_abusers
  @command_abusers
end

Instance Method Details

#antispam_status(m) ⇒ Object


90
91
92
93
94
# File 'lib/cinch/plugins/anti_spam.rb', line 90

def antispam_status(m)
  m.reply "Abusers will be kicked after %d commands within %d seconds, and will be banned after %d kicks.
A 'grace period' of %d minutes is given, after which the abuser's record is deleted, unless they abuse again.
There are currently %d records on %s." % [config[:command_threshold], config[:seconds_threshold], config[:kick_threshold_for_ban], (config[:seconds_threshold] * PERIOD_MULTIPLIER) / 60, command_abusers.size, @bot.irc.network.name]
end

#listen_to_spam(m) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cinch/plugins/anti_spam.rb', line 46

def listen_to_spam(m)
  #return if check_user(m.channel, m.user)
  return unless m.message.match(/^[[email protected]%\-.\]?]+\w+/)

  if @command_abusers[m.user] # record exists
    record = @command_abusers[m.user]

    record.reset_current! unless record.current

    # The timer will run once after seconds_threshold * PERIOD_MULTIPLIER runs. That is,
    # if seconds_threshold is set to 30, then timer code will run in an hour.
    record.current.timer = Timer(config[:seconds_threshold] * PERIOD_MULTIPLIER, shots: 1) do
      @command_abusers.delete(m.user)
    end

    record.increment_current!
    
    if (record.current.last_offence - record.current.first_offence) <= config[:seconds_threshold] && record.current.count == config[:command_threshold]
      if record.kick_count < config[:kick_threshold_for_ban]
        m.channel.kick(m.user, "You have spammed commands #{record.current.count} times in #{(record.current.last_offence - record.current.first_offence).round(2)} seconds.")
        record.delete_current_and_increment_kick_count!
      else
        m.channel.ban(m.user)
        m.channel.kick(m.user, "You have spammed commands #{record.current.count} times in #{(record.current.last_offence - record.current.first_offence).round(2)} seconds and been kicked #{record.kick_count} times.")
        @command_abusers.delete(m.user) # Erase record after kickban
      end
    elsif (record.current.last_offence - record.current.first_offence) > config[:seconds_threshold]
      # Reset if user ran a command after the threshold
      record.reset_current!
    end
  else # creating a new record
    record = Abuser.new
    #record.current.increment_count!
    record.current.timer = Timer(config[:seconds_threshold] * PERIOD_MULTIPLIER, shots: 1) do
      # Delete abuse record after timer has elapsed.
      @command_abusers.delete(m.user)
    end
    @command_abusers[m.user] = record
  end
  
  #@bot.handlers.dispatch :antispam, m, [d,@command_abusers[m.user]], m.target
end