]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/nickserv.rb
nickserv password now tries to inform the nickserv of the password change; it can...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / nickserv.rb
index 1ef2baf7fdfff8cfcb0e7d596ffff97267557560..5c2e068f79d58b1725c50a4f6a4fa39e543e7d00 100644 (file)
@@ -1,7 +1,25 @@
-# automatically lookup nicks in @registry and identify when asked
+# Automatically lookup nicks in @registry and identify when asked
+# Takes over proper nick if required and nick is registered
+# TODO allow custom IDENTIFY and GHOST names
+# TODO instead of nickserv.wait it would be ideal if we could just
+# set up "don't send further commands until you receive this particular message"
 
 class NickServPlugin < Plugin
   
+  BotConfig.register BotConfigStringValue.new('nickserv.name',
+    :default => "nickserv", :requires_restart => false,
+    :desc => "Name of the nick server (all lowercase)")
+  BotConfig.register BotConfigStringValue.new('nickserv.ident_request',
+    :default => "IDENTIFY", :requires_restart => false,
+    :on_change => Proc.new { |bot, v| bot.plugins.delegate "set_ident_request", v },
+    :desc => "String to look for to see if the nick server is asking us to identify")
+  BotConfig.register BotConfigBooleanValue.new('nickserv.wants_nick',
+    :default => true, :requires_restart => false,
+    :desc => "Set to false if the nick server doesn't expect the nick as a parameter in the identify command")
+  BotConfig.register BotConfigIntegerValue.new('nickserv.wait',
+    :default => 30, :validate => Proc.new { |v| v > 0 }, :requires_restart => false,
+    :desc => "Seconds to wait after sending a message to nickserv, e.g. after ghosting")
+
   def help(plugin, topic="")
     case topic
     when ""
@@ -17,6 +35,19 @@ class NickServPlugin < Plugin
     end
   end
   
+  def genpasswd
+    # generate a random password
+    passwd = ""
+    8.times do
+      passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
+    end
+    return passwd
+  end
+
+  def set_ident_request(val)
+    @ident_request = Regexp.new(val)
+  end
+
   def initialize
     super
     # this plugin only wants to store strings!
@@ -28,72 +59,101 @@ class NickServPlugin < Plugin
         val
       end
     end
+    set_ident_request(@bot.config['nickserv.ident_request'])
   end
-  
-  def privmsg(m)
-    return unless m.params
-    
-    case m.params
-    when (/^password\s*(\S*)\s*(.*)$/)
-      nick = $1
-      passwd = $2
-      @registry[nick] = passwd
-      m.okay
-    when (/^register$/)
-      passwd = genpasswd
-      @bot.sendmsg "PRIVMSG", "NickServ", "REGISTER " + passwd
-      @registry[@bot.nick] = passwd
-      m.okay
-    when (/^register\s*(\S*)\s*(.*)$/)
-      passwd = $1
-      email = $2
-      @bot.sendmsg "PRIVMSG", "NickServ", "REGISTER " + passwd + " " + email
-      @registry[@bot.nick] = passwd
-      m.okay
-    when (/^register\s*(.*)\s*$/)
-      passwd = $1
-      @bot.sendmsg "PRIVMSG", "NickServ", "REGISTER " + passwd
-      @registry[@bot.nick] = passwd
-      m.okay
-    when (/^listnicks$/)
-      if @bot.auth.allow?("config", m.source, m.replyto)
-        if @registry.length > 0
-          @registry.each {|k,v|
-            @bot.say m.sourcenick, "#{k} => #{v}"
-          }
+
+  def password(m, params)
+    nick = params[:nick] || @bot.nick
+    passwd = params[:passwd]
+    if nick == @bot.nick
+      @bot.say @bot.config['nickserv.name'], "SET PASSWORD #{passwd}"
+    else
+      m.reply "I'm only changing this in my database, I won't inform #{@bot.config['nickserv.name']} of the change"
+    end
+    @registry[nick] = passwd
+    m.okay
+  end
+
+  def nick_register(m, params)
+    passwd = params[:passwd] ? params[:passwd] : genpasswd
+    message = "REGISTER #{passwd}"
+    message += " #{params[:email]}" if params[:email]
+    @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], message
+    @registry[@bot.nick] = passwd
+    m.okay
+  end
+
+  def listnicks(m, params)
+    if @registry.length > 0
+      @registry.each {|k,v|
+        @bot.say m.sourcenick, "#{k} => #{v}"
+      }
+    else
+      m.reply "none known"
+    end
+  end
+
+  def do_identify(nick=@bot.nick)
+    if @registry.has_key?(nick)
+      if @bot.config['nickserv.wants_nick']
+        @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "IDENTIFY #{nick} #{@registry[nick]}"
+      else
+        if nick == @bot.nick
+          @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "IDENTIFY #{@registry[nick]}"
         else
-          m.reply "none known"
+          # We cannot identify for different nicks if we can't use the nickname ...
+          return false
         end
       end
-    when (/^identify$/)
-      if @registry.has_key?(@bot.nick)
-        @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY " + @registry[@bot.nick]
-        m.okay
+      return true
+    end
+    return false
+  end
+
+  def identify(m, params)
+    if do_identify
+      m.okay
+    else
+      m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
+    end
+  end
+  
+  def connect
+    do_identify
+  end
+  
+  def nicktaken(nick)
+    if @registry.has_key?(nick)
+      @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "GHOST #{nick} #{@registry[nick]}"
+      if do_identify nick
+        sleep @bot.config['nickserv.wait']
+        @bot.nickchg nick
+        # We need to wait after changing nick, otherwise the server
+        # might refuse to execute further commangs, e.g. subsequent JOIN
+        # commands until the nick has changed.
+        sleep @bot.config['nickserv.wait']
       else
-        m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
+        debug "Failed to identify for nick #{nick}, cannot take over"
       end
     end
   end
-  
+
   def listen(m)
     return unless(m.kind_of? NoticeMessage)
 
-    if (m.sourcenick == "NickServ" && m.message =~ /This nickname is owned by someone else/)
-      puts "nickserv asked us to identify for nick #{@bot.nick}"
-      if @registry.has_key?(@bot.nick)
-        @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY " + @registry[@bot.nick]
-      end
+    if (m.sourcenick == @bot.config['nickserv.name'] && m.message =~ @ident_request)
+      debug "nickserv asked us to identify for nick #{@bot.nick}"
+      do_identify
     end
   end
 
-  def genpasswd
-    # generate a random password
-    passwd = ""
-    8.times do
-      passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
-    end
-    return passwd
-  end
 end
 plugin = NickServPlugin.new
-plugin.register("nickserv")
+plugin.map 'nickserv password [:nick] :passwd', :action => "password"
+plugin.map 'nickserv register :passwd :email', :action => 'nick_register',
+           :defaults => {:passwd => false, :email => false}
+plugin.map 'nickserv listnicks', :action => "listnicks"
+plugin.map 'nickserv identify', :action => "identify"
+
+plugin.default_auth('*', false)
+