]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nickrecover.rb
iplookup plugin: support IPv6 too
[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 has_nick?
47     @bot.nick.downcase == wanted_nick.downcase
48   end
49
50   def recover
51     @bot.nickchg wanted_nick
52   end
53
54   def stop_recovery
55     @bot.timer.remove(@recovery) if @recovery
56   end
57
58   def start_recovery(time=self.poll_time)
59     if @recovery
60       @bot.timer.reschedule(@recovery, poll_time)
61     else
62       @recovery = @bot.timer.add(time) do
63         has_nick? ? stop_recovery : recover
64       end
65     end
66   end
67
68   def initialize
69     super
70     @recovery = nil
71   end
72
73   def connect
74     if enabled?
75       start_recovery unless has_nick?
76     end
77   end
78
79   def nick(m)
80     return unless m.address?
81     # if recovery is enabled and the nick is not the wanted nick,
82     # launch the recovery process. Stop it otherwise
83     if enabled? and m.newnick.downcase != wanted_nick.downcase
84       start_recovery
85     else
86       stop_recovery
87     end
88   end
89
90   def cleanup
91     stop_recovery
92   end
93
94 end
95
96 plugin = NickRecoverPlugin.new
97