]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nickrecover.rb
iplookup plugin: don't block
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / nickrecover.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Nick recovery
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 #
8 # Copyright:: (C) 2008 Giuseppe Bilotta
9 #
10 # This plugin tries to automatically recover the bot's wanted nick
11 # in case it couldn't be achieved.
12
13 class NickRecoverPlugin < Plugin
14   
15   Config.register Config::IntegerValue.new('irc.nick_retry',
16     :default => 60, :valiedate => Proc.new { |v| v >= 0 },
17     :on_change => Proc.new do |bot, v|
18       if v > 0
19         bot.plugin['nickrecover'].start_recovery(v)
20       else
21         bot.plugin['nickrecover'].stop_recovery
22       end
23     end, :requires_restart => false,
24     :desc => _("Time in seconds to wait between attempts to recover the nick. set to 0 to disable nick recovery."))
25
26   def help(plugin,topic="")
27     [
28       _("the nickrecover plugin takes care of recovering the bot nick by trying to change nick until it succeeds."),
29       _("the plugin waits irc.nick_retry seconds between attempts."),
30       _("set irc.nick_retry to 0 to disable it.")
31     ].join(' ')
32   end
33
34   def enabled?
35     @bot.config['irc.nick_retry'] > 0
36   end
37
38   def poll_time
39     @bot.config['irc.nick_retry']
40   end
41
42   def wanted_nick
43     @bot.wanted_nick
44   end
45
46   def stop_recovery
47     @bot.timer.remove(@recovery) if @recovery
48   end
49
50   def start_recovery(time=self.poll_time)
51     if @recovery
52       @bot.timer.reschedule(@recovery, poll_time)
53     else
54       @recovery = @bot.timer.add(time) { @bot.nickchg wanted_nick }
55     end
56   end
57
58   def initialize
59     super
60     @recovery = nil
61     if enabled? and @bot.nick.downcase != wanted_nick
62       start_recovery
63     end
64   end
65
66   def nick(m)
67     return unless m.address?
68     # if recovery is enabled and the nick is not the wanted nick,
69     # launch the recovery process. Stop it otherwise
70     if enabled? and m.newnick.downcase != wanted_nick.downcase
71       start_recovery
72     else
73       stop_recovery
74     end
75   end
76
77 end
78
79 plugin = NickRecoverPlugin.new
80