Class: Cinch::Plugins::TimeBan

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

Constant Summary collapse

TIMEBAN_KEY =

network, channel.name.downcase, nick.downcase

'timeban:%s:%s:%s'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TimeBan


18
19
20
21
# File 'lib/cinch/plugins/time_ban.rb', line 18

def initialize(*args)
  super
  @active_timebans = Hash.new {|hash, key| hash[key] = {} }
end

Instance Attribute Details

#active_timebansObject (readonly)

Returns the value of attribute active_timebans


11
12
13
# File 'lib/cinch/plugins/time_ban.rb', line 11

def active_timebans
  @active_timebans
end

Instance Method Details

#execute_listbans(m) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cinch/plugins/time_ban.rb', line 101

def execute_listbans(m)
  return m.user.notice("#{Format(:red,:bold,"Whoops!")} · You don't have the proper authorization for that.") unless check_user(m.channel, m.user)
  key = TIMEBAN_KEY % [@bot.irc.isupport['NETWORK'], m.channel.name.downcase, nil]
  bans = shared[:redis].smembers(key[0..-2]).each_with_object([]) {|timeban,memo| memo << shared[:redis].hgetall(timeban) }
  lines = bans.each_with_index.each_with_object([]) {|(timeban,i),memo|
    width = timeban.keys.max_by(&:length).length
    memo << timeban.each_with_object([]) {|(k,v),mem| mem << "%-#{width}s: %s" % [k,v] }
  }
  if lines.length > 0
    maxwidth = lines.flatten.max_by {|l| l.length }.length
    lines.each_with_index {|l,i|
      m.user.msg ("#%d " % i.succ).ljust(maxwidth, '-')
      m.user.msg l * $/
    }
    m.user.msg '* End of results.'
  else
    m.user.msg "#{Format(:red,:bold,"Whoops!")} · No timebans could be found for %s." % m.channel.name
  end
end

#execute_timeban(m, nick, range, reason) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/cinch/plugins/time_ban.rb', line 24

def execute_timeban(m, nick, range, reason)
  return m.user.notice('#{Format(:red,:bold,"Whoops!")} · I do not have the correct privileges. (not +%s)' % @bot.irc.isupport["PREFIX"].keys - ['v']) unless check_user(m.channel, @bot)
  return m.user.notice('#{Format(:red,:bold,"Whoops!")} · You do not have the correct privileges. (not +%s)' % @bot.irc.isupport["PREFIX"].keys - ['v']) unless check_user(m.channel, m.user)
  user = User(nick)
  return if user == @bot
  return m.user.notice('%s does not seem to exist on %s' % [nick, @bot.irc.network.name]) if user.unknown?
  key = TIMEBAN_KEY % [@bot.irc.isupport['NETWORK'], m.channel.name.downcase, user.nick.downcase]
  set = key[/(.+):\S+$/,1]

  now = Time.now
  
  bandata = {
    'nick' => user.nick,
    'host.mask' => user.mask('*!*@%h'),
    'ban.start' => now,
    'ban.end' => range2time(range, now),
    'ban.reason' => reason || 'fuck you!',
    'banned.by' => m.user.mask
  }
  
  # Only add timeban entries that are less than a year in length
  if range2h(range)[:years] < 1
    synchronize(:timeban_update) do
      shared[:redis].hmset key, *bandata
      shared[:redis].sadd set, key
      @active_timebans[m.channel.name.downcase][user.nick.downcase] = 
        Timer(bandata['ban.end'] - Time.now, shots: 1) { unban(m, key) }
    end
    @bot.handlers.dispatch :timeban, m, 
      "%s just set a timed ban in %s for %s (%s) with reason %s. (Lasts %s.)" % [
        m.user.nick, 
        m.channel.name, 
        bandata['nick'], 
        bandata['host.mask'], 
        bandata['ban.reason'], 
        ChronicDuration.output((bandata['ban.end'] - bandata['ban.start']).round, format: :long)
      ], m.target
  else
    @bot.handlers.dispatch :timeban, m, 
      "%s just set a permanent (un)timed ban in %s for %s (%s) with reason %s." % [
        m.user.nick, 
        m.channel.name, 
        bandata['nick'], 
        bandata['host.mask'], 
        bandata['ban.reason']
      ], m.target
  end

  kickreason = 'Banned for %s by %s (%s)' % [ 
    ChronicDuration.output((bandata['ban.end'] - bandata['ban.start']).round, format: :long), 
    m.user.nick, 
    bandata['ban.reason']
  ]

  m.channel.ban(bandata['host.mask'])
  m.reply "Setted ban on %s" % bandata['host.mask']
  m.channel.kick(user, kickreason)

  # @todo kick all other nicks with the same host
end

#execute_unban(m, nick) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cinch/plugins/time_ban.rb', line 86

def execute_unban(m, nick)
  return m.user.notice('#{Format(:red,:bold,"Whoops!")} · I do not have the correct privileges. (not +%s)' % @bot.irc.isupport["PREFIX"].keys - ['v']) unless check_user(m.channel, @bot)
  return m.user.notice('#{Format(:red,:bold,"Whoops!")} · You do not have the correct privileges. (not +%s)' % @bot.irc.isupport["PREFIX"].keys - ['v']) unless check_user(m.channel, m.user)

  key = TIMEBAN_KEY % [@bot.irc.isupport['NETWORK'], m.channel.name.downcase, nick.downcase]

  if shared[:redis].exists(key)
    bandata = shared[:redis].hgetall(key)
    unban(m, key)
  else
    m.user.notice("#{Format(:red,:bold,"Whoops!")} · No timed ban could be found for %s." % nick)
  end
end

#on_join(m) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cinch/plugins/time_ban.rb', line 122

def on_join(m)
  return unless m.user == @bot
  key = TIMEBAN_KEY % [@bot.irc.isupport['NETWORK'], m.channel.name.downcase, nil]
  if shared[:redis].exists(key[0..-2])
    sleep 4
    shared[:redis].smembers(key[0..-2]).each {|timeban|
      bandata = shared[:redis].hgetall(timeban)
      if Time.parse(bandata['ban.end']) > Time.now
        @active_timebans[m.channel.name.downcase][bandata['nick'].downcase] = 
        Timer(Time.parse(bandata['ban.end']) - Time.now, shots: 1) { unban(m, timeban) }
      else
        unban(m, timeban)
      end
    }
  end
end

#on_part(m) ⇒ Object


140
141
142
143
144
145
# File 'lib/cinch/plugins/time_ban.rb', line 140

def on_part(m)
  return unless m.user == @bot
  channel_name = m.channel.name.downcase
  @active_timebans[channel_name].each {|_, timer| timer.stop }
  @active_timebans.delete(channel_name)
end