]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nickrecover.rb
rss plugin: don't claim to be using old data when we don't
[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::BooleanValue.new('nickrecover.enabled',
16     :default => true, :requires_restart => false,
17     :desc => _("Should the bot try to recover its nick?"))
18
19   Config.register Config::IntegerValue.new('nickrecover.poll_time',
20     :default => 60, :valiedate => Proc.new { |v| v > 0 },
21     :on_change => Proc.new do |bot, v|
22       bot.plugin['nickrecover'].start_recovery(v)
23     end, :requires_restart => false,
24     :desc => _("Time in seconds to wait between attempts to recover the nick"))
25
26   def enabled?
27     @bot.config['nickrecover.enabled']
28   end
29
30   def poll_time
31     @bot.config['nickrecover.poll_time']
32   end
33
34   def wanted_nick
35     @bot.wanted_nick
36   end
37
38   def stop_recovery
39     @bot.timer.remove(@recovery) if @recovery
40   end
41
42   def start_recovery(time=self.poll_time)
43     if @recovery
44       @bot.timer.reschedule(@recovery, poll_time)
45     else
46       @recovery = @bot.timer.add(time) { @bot.nickchg wanted_nick }
47     end
48   end
49
50   def initialize
51     super
52     @recovery = nil
53     if enabled? and @bot.nick.downcase != wanted_nick
54       start_recovery
55     end
56   end
57
58   def nick(m)
59     return unless m.address?
60     # if recovery is enabled and the nick is not the wanted nick,
61     # launch the recovery process. Stop it otherwise
62     if enabled? and m.newnick.downcase != wanted_nick.downcase
63       start_recovery
64     else
65       stop_recovery
66     end
67   end
68
69 end
70
71 plugin = NickRecoverPlugin.new
72