]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/nickrecover.rb
rbot-remote: allow override of function
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / nickrecover.rb
index 24b41a0453539fb807ccfcf73330e4848337576f..f0277cdf512363cce81a88645792f7f313cd0c03 100644 (file)
 # in case it couldn't be achieved.
 
 class NickRecoverPlugin < Plugin
-  
-  Config.register Config::BooleanValue.new('nickrecover.enabled',
-    :default => true, :requires_restart => false,
-    :desc => _("Should the bot try to recover its nick?"))
 
-  Config.register Config::IntegerValue.new('nickrecover.poll_time',
-    :default => 60, :valiedate => Proc.new { |v| v > 0 },
+  Config.register Config::IntegerValue.new('irc.nick_retry',
+    :default => 60, :valiedate => Proc.new { |v| v >= 0 },
     :on_change => Proc.new do |bot, v|
-      bot.plugin['nickrecover'].start_recovery(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"))
+    :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['nickrecover.enabled']
+    @bot.config['irc.nick_retry'] > 0
   end
 
   def poll_time
-    @bot.config['nickrecover.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
-    @bot.timer.remove(@recovery) if @recovery
+    begin
+      @bot.timer.remove(@recovery) if @recovery
+    ensure
+      @recovery = nil
+    end
   end
 
   def start_recovery(time=self.poll_time)
     if @recovery
-      @bot.timer.reschedule(@recovery, poll_time)
-    else
-      @recovery = @bot.timer.add(time) { @bot.nickchg wanted_nick }
+      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
-    if enabled? and @bot.nick.downcase != wanted_nick
-      start_recovery
+  end
+
+  def connect
+    if enabled?
+      start_recovery unless has_nick?
     end
   end
 
@@ -66,6 +95,10 @@ class NickRecoverPlugin < Plugin
     end
   end
 
+  def cleanup
+    stop_recovery
+  end
+
 end
 
 plugin = NickRecoverPlugin.new