summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/autorejoin.rb
blob: 751966563a6e25e5a329c7776418fe661713fe5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
#-- vim:sw=2:et
#++
#
# :title: Autorejoin

class AutoRejoinPlugin < Plugin
  Config.register Config::BooleanValue.new('rejoin.insult',
    :default => true,
    :desc => "Determines if the bot will insult whoever kicked it, after rejoin")
  Config.register Config::BooleanValue.new('rejoin.kick',
    :default => false,
    :desc => "Determines if the bot will kick whoever kicked it, after rejoin")
  Config.register Config::ArrayValue.new('rejoin.no_kick_list',
    :default => ["owner"],
    :desc => "List of botusers that can kick the bot without being kicked")


  def initialize
    super
    @should_kick = Hash.new
  end

  def help(plugin, topic="")
    "performs an automatic rejoin if the bot is kicked from a channel"
  end

  def kick(m)
    password = m.channel.mode[:k].value

    if m.address?
      if @bot.config['rejoin.kick'] and not @bot.config['rejoin.no_kick_list'].include? m.source.botuser.username
        @should_kick[m.channel.downcase] = m.sourcenick
      end
      r = rand(10)
      if r > 0
	@bot.timer.add_once(r) {
	  @bot.join m.channel, password
	  @bot.say(m.channel, @bot.lang.get("insult") % m.sourcenick) if @bot.config['rejoin.insult']
	}
      else
	@bot.join m.channel, password
	@bot.say(m.channel, @bot.lang.get("insult") % m.sourcenick) if @bot.config['rejoin.insult']
      end
    end
  end

  def modechange(m)
    # if we got opped on a channel we want to kick somebody from,
    # do the kicking

    # getting opped on a channel is a channel mode change, so bail out if this
    # is not a channel mode change
    return unless m.target.kind_of? Channel
    # bail out if we are not op, too
    return unless @bot.myself.is_op?(m.target)
    # bail out if there's nobody to kick
    to_kick = @should_kick.delete(m.target.downcase)
    return unless to_kick
    # kick the evil user that kicked us
    @bot.kick m.target, to_kick, _("for kicking me out earlier")
  end

end

plugin = AutoRejoinPlugin.new