summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/nickrecover.rb
blob: f0277cdf512363cce81a88645792f7f313cd0c03 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#-- vim:sw=2:et
#++
#
# :title: Nick recovery
#
# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
#
# Copyright:: (C) 2008 Giuseppe Bilotta
#
# This plugin tries to automatically recover the bot's wanted nick
# in case it couldn't be achieved.

class NickRecoverPlugin < Plugin

  Config.register Config::IntegerValue.new('irc.nick_retry',
    :default => 60, :valiedate => Proc.new { |v| v >= 0 },
    :on_change => Proc.new do |bot, v|
      if v > 0
        bot.plugin['nickrecover'].start_recovery(v)
      else
        bot.plugin['nickrecover'].stop_recovery
      end
    end, :requires_restart => false,
    :desc => _("Time in seconds to wait between attempts to recover the nick. set to 0 to disable nick recovery."))

  def help(plugin,topic="")
    [
      _("the nickrecover plugin takes care of recovering the bot nick by trying to change nick until it succeeds."),
      _("the plugin waits irc.nick_retry seconds between attempts."),
      _("set irc.nick_retry to 0 to disable it.")
    ].join(' ')
  end

  def enabled?
    @bot.config['irc.nick_retry'] > 0
  end

  def poll_time
    @bot.config['irc.nick_retry']
  end

  def wanted_nick
    @bot.wanted_nick
  end

  def has_nick?
    @bot.nick.downcase == wanted_nick.downcase
  end

  def recover
    @bot.nickchg wanted_nick
  end

  def stop_recovery
    begin
      @bot.timer.remove(@recovery) if @recovery
    ensure
      @recovery = nil
    end
  end

  def start_recovery(time=self.poll_time)
    if @recovery
      begin
        @bot.timer.reschedule(@recovery, poll_time)
        return
      rescue
        @recovery=nil
      end
    end
    @recovery = @bot.timer.add(time) do
      has_nick? ? stop_recovery : recover
    end
  end

  def initialize
    super
    @recovery = nil
  end

  def connect
    if enabled?
      start_recovery unless has_nick?
    end
  end

  def nick(m)
    return unless m.address?
    # if recovery is enabled and the nick is not the wanted nick,
    # launch the recovery process. Stop it otherwise
    if enabled? and m.newnick.downcase != wanted_nick.downcase
      start_recovery
    else
      stop_recovery
    end
  end

  def cleanup
    stop_recovery
  end

end

plugin = NickRecoverPlugin.new