]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Modularized core now functional. Still a lot to do and auth missing, but the bot...
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 2 Aug 2006 00:04:19 +0000 (00:04 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 2 Aug 2006 00:04:19 +0000 (00:04 +0000)
lib/rbot/botuser.rb
lib/rbot/core/core.rb
lib/rbot/ircbot.rb
lib/rbot/plugins.rb

index 67d7d842bcabde86d32d98a591197e6e96abc94f..98408a0d424b58a019dadf059d7f15e347da4be5 100644 (file)
@@ -258,10 +258,7 @@ module Irc
       def set_permission(cmd, val)\r
         raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val)\r
         Irc::error_if_not_command(cmd)\r
-        cmd.path.each { |k|\r
-          set_permission(k.to_s, true) unless @perm.has_key?(k)\r
-        }\r
-        @perm[path.last] = val\r
+        @perm[cmd.command] = val\r
       end\r
 \r
       # Tells if command _cmd_ is permitted. We do this by returning\r
@@ -308,7 +305,12 @@ module Irc
       def set_permission(cmd, val, chan="*")\r
         k = chan.to_s.to_sym\r
         @perm[k] = PermissionSet.new unless @perm.has_key?(k)\r
-        @perm[k].set_permission(cmd, val)\r
+        case cmd\r
+        when String\r
+          @perm[k].set_permission(Command.new(cmd), val)\r
+        else\r
+          @perm[k].set_permission(cmd, val)\r
+        end\r
       end\r
 \r
       # Checks if BotUser is allowed to do something on channel _chan_,\r
index c9210d5944c4855308b4d4e3e56ecdb2e7a70d6d..fcf5ac3a33f209b95357fd6972693efdc6acf17d 100644 (file)
@@ -4,8 +4,6 @@
 \r
 class Core < CoreBotModule\r
 \r
-  # TODO cleanup\r
-  # handle incoming IRC PRIVMSG +m+\r
   def listen(m)\r
     return unless m.class <= PrivMessage\r
     if(m.private? && m.message =~ /^\001PING\s+(.+)\001/)\r
@@ -13,105 +11,116 @@ class Core < CoreBotModule
       @bot.irclog "@ #{m.sourcenick} pinged me"\r
       return\r
     end\r
+  end\r
+\r
+  def bot_join(m, param)\r
+    if param[:pass]\r
+      @bot.join param[:chan], param[:pass]\r
+    else\r
+      @bot.join param[:chan]\r
+    end\r
+  end\r
+\r
+  def bot_part(m, param)\r
+    if param[:chan]\r
+      @bot.part param[:chan]\r
+    else\r
+      @bot.part m.target if m.public?\r
+    end\r
+  end\r
+\r
+  def bot_quit(m, param)\r
+    @bot.quit(param[:msg] ? param[:msg].join(" ") : nil)\r
+  end\r
+\r
+  def bot_restart(m, param)\r
+    @bot.restart(param[:msg] ? param[:msg].join(" ") : nil)\r
+  end\r
+\r
+  def bot_hide(m, param)\r
+    @bot.join 0\r
+  end\r
+\r
+  def bot_save(m, param)\r
+    @bot.save\r
+    m.okay\r
+  end\r
+\r
+  def bot_nick(m, param)\r
+    @bot.nickchg(param[:nick])\r
+  end\r
+\r
+  def bot_say(m, param)\r
+    @bot.say param[:where], param[:what].join(" ")\r
+  end\r
+\r
+  def bot_action(m, param)\r
+    @bot.action param[:where], param[:what].join(" ")\r
+  end\r
+\r
+  def bot_mode(m, param)\r
+    @bot.mode param[:where], param[:what], param[:who].join(" ")\r
+  end\r
+\r
+  def bot_ping(m, param)\r
+    m.reply "pong"\r
+  end\r
+\r
+  def bot_rescan(m, param)\r
+    m.reply "saving ..."\r
+    @bot.save\r
+    m.reply "rescanning ..."\r
+    @bot.rescan\r
+    m.reply "done. #{@plugins.status(true)}"\r
+  end\r
+\r
+  def bot_quiet(m, param)\r
+    if param.has_key?(:where)\r
+      @bot.set_quiet param[:where].sub(/^here$/, m.target)\r
+    else\r
+      @bot.set_quiet\r
+    end\r
+  end\r
 \r
-    if(m.address?)\r
-      case m.message\r
-      when (/^join\s+(\S+)\s+(\S+)$/i)\r
-        @bot.join $1, $2 if(@bot.auth.allow?("join", m.source, m.replyto))\r
-      when (/^join\s+(\S+)$/i)\r
-        @bot.join $1 if(@bot.auth.allow?("join", m.source, m.replyto))\r
-      when (/^part$/i)\r
-        @bot.part m.target if(m.public? && @bot.auth.allow?("join", m.source, m.replyto))\r
-      when (/^part\s+(\S+)$/i)\r
-        @bot.part $1 if(@bot.auth.allow?("join", m.source, m.replyto))\r
-      when (/^quit(?:\s+(.*))?$/i)\r
-        @bot.quit $1 if(@bot.auth.allow?("quit", m.source, m.replyto))\r
-      when (/^restart(?:\s+(.*))?$/i)\r
-        @bot.restart $1 if(@bot.auth.allow?("quit", m.source, m.replyto))\r
-      when (/^hide$/i)\r
-        @bot.join 0 if(@bot.auth.allow?("join", m.source, m.replyto))\r
-      when (/^save$/i)\r
-        if(@bot.auth.allow?("config", m.source, m.replyto))\r
-          @bot.save\r
-          m.okay\r
-        end\r
-      when (/^nick\s+(\S+)$/i)\r
-        @bot.nickchg($1) if(@bot.auth.allow?("nick", m.source, m.replyto))\r
-      when (/^say\s+(\S+)\s+(.*)$/i)\r
-        @bot.say $1, $2 if(@bot.auth.allow?("say", m.source, m.replyto))\r
-      when (/^action\s+(\S+)\s+(.*)$/i)\r
-        @bot.action $1, $2 if(@bot.auth.allow?("say", m.source, m.replyto))\r
-        # when (/^topic\s+(\S+)\s+(.*)$/i)\r
-        #   topic $1, $2 if(@bot.auth.allow?("topic", m.source, m.replyto))\r
-      when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i)\r
-        @bot.mode $1, $2, $3 if(@bot.auth.allow?("mode", m.source, m.replyto))\r
-      when (/^ping$/i)\r
-        @bot.say m.replyto, "pong"\r
-      when (/^rescan$/i)\r
-        if(@bot.auth.allow?("config", m.source, m.replyto))\r
-          m.reply "saving ..."\r
-          @bot.save\r
-          m.reply "rescanning ..."\r
-          @bot.rescan\r
-          m.reply "done. #{@plugins.status(true)}"\r
-        end\r
-      when (/^quiet$/i)\r
-        if(@bot.auth.allow?("talk", m.source, m.replyto))\r
-          m.okay\r
-          @bot.set_quiet\r
-        end\r
-      when (/^quiet in (\S+)$/i)\r
-        where = $1\r
-        if(@bot.auth.allow?("talk", m.source, m.replyto))\r
-          m.okay\r
-          where.gsub!(/^here$/, m.target) if m.public?\r
-          @bot.set_quiet(where)\r
-        end\r
-      when (/^talk$/i)\r
-        if(@bot.auth.allow?("talk", m.source, m.replyto))\r
-          @bot.reset_quiet\r
-          m.okay\r
-        end\r
-      when (/^talk in (\S+)$/i)\r
-        where = $1\r
-        if(@bot.auth.allow?("talk", m.source, m.replyto))\r
-          where.gsub!(/^here$/, m.target) if m.public?\r
-          @bot.reset_quiet(where)\r
-          m.okay\r
-        end\r
-      when (/^status\??$/i)\r
-        m.reply status if @bot.auth.allow?("status", m.source, m.replyto)\r
-      when (/^registry stats$/i)\r
-        if @bot.auth.allow?("config", m.source, m.replyto)\r
-          m.reply @registry.stat.inspect\r
-        end\r
-      when (/^(help\s+)?config(\s+|$)/)\r
-        @config.privmsg(m)\r
-      when (/^(version)|(introduce yourself)$/i)\r
-        @bot.say m.replyto, "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"\r
-      when (/^help(?:\s+(.*))?$/i)\r
-        @bot.say m.replyto, help($1)\r
-        #TODO move these to a "chatback" plugin\r
-      when (/^(botsnack|ciggie)$/i)\r
-        @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)\r
-        @bot.say m.replyto, @lang.get("thanks") if(m.private?)\r
-      when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)\r
-        @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)\r
-        @bot.say m.replyto, @lang.get("hello") if(m.private?)\r
-      end\r
+  def bot_talk(m, param)\r
+    if param.has_key?(:where)\r
+      @bot.reset_quiet param[:where].sub(/^here$/, m.target)\r
     else\r
-      # stuff to handle when not addressed\r
-      case m.message\r
-      when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@bot.nick)}$/i)\r
-        @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick\r
-      when (/^#{Regexp.escape(@bot.nick)}!*$/)\r
-        @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick\r
-      else\r
-        # @keywords.privmsg(m)\r
-      end\r
+      @bot.reset_quiet\r
     end\r
   end\r
 \r
+  def bot_status(m, param)\r
+    m.reply @bot.status\r
+  end\r
+\r
+  # TODO is this one of the methods that disappeared when the bot was moved\r
+  # from the single-file to the multi-file registry?\r
+  #\r
+  #  def bot_reg_stat(m, param)\r
+  #    m.reply @registry.stat.inspect\r
+  #  end\r
+\r
+  def bot_version(m, param)\r
+    m.reply  "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"\r
+  end\r
+\r
+  def bot_help(m, param)\r
+    m.reply @bot.help(param[:topic].join(" "))\r
+  end\r
+\r
+  #TODO move these to a "chatback" plugin\r
+  # when (/^(botsnack|ciggie)$/i)\r
+  #   @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)\r
+  #   @bot.say m.replyto, @lang.get("thanks") if(m.private?)\r
+  # when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)\r
+  #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)\r
+  #   @bot.say m.replyto, @lang.get("hello") if(m.private?)\r
+  # when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@bot.nick)}$/i)\r
+  #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick\r
+  # when (/^#{Regexp.escape(@bot.nick)}!*$/)\r
+  #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick\r
+\r
   # handle help requests for "core" topics\r
   def help(topic="")\r
     case topic\r
@@ -141,10 +150,10 @@ class Core < CoreBotModule
       return "talk [in here|<channel>] => with no arguments, resume speaking in all channels, if \"in here\", resume speaking in this channel, or resume speaking in <channel>"\r
     when "version"\r
       return "version => describes software version"\r
-    when "botsnack"\r
-      return "botsnack => reward #{myself} for being good"\r
-    when "hello"\r
-      return "hello|hi|hey|yo [#{myself}] => greet the bot"\r
+      #     when "botsnack"\r
+      #       return "botsnack => reward #{myself} for being good"\r
+      #     when "hello"\r
+      #       return "hello|hi|hey|yo [#{myself}] => greet the bot"\r
     else\r
       return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"\r
     end\r
@@ -153,3 +162,80 @@ end
 \r
 core = Core.new\r
 \r
+core.map "quit *msg",\r
+  :action => 'bot_quit',\r
+  :defaults => { :msg => nil },\r
+  :auth => 'core::quit::quit'\r
+core.map "restart *msg",\r
+  :action => 'bot_restart',\r
+  :defaults => { :msg => nil },\r
+  :auth => 'core::quit::restart'\r
+\r
+core.map "save",\r
+  :action => 'bot_save',\r
+  :auth => 'core::config::save'\r
+core.map "rescan",\r
+  :action => 'bot_rescan',\r
+  :auth => 'core::config::rescan'\r
+core.map "nick :nick",\r
+  :action => 'bot_nick',\r
+  :auth => 'core::config::nick'\r
+core.map "status",\r
+  :action => 'bot_status',\r
+  :auth => 'core::config::show::status'\r
+  # TODO see above\r
+  #\r
+  # core.map "registry stats",\r
+  #   :action => 'bot_reg_stat',\r
+  #   :auth => 'core::config::show::registry'\r
+core.map "version",\r
+  :action => 'bot_version',\r
+  :auth => 'core::config::show::version'\r
+\r
+core.map "quiet",\r
+  :action => 'bot_quiet',\r
+  :auth => 'core::talk::quiet'\r
+core.map "quiet in :chan",\r
+  :action => 'bot_quiet',\r
+  :auth => 'core::talk::quiet'\r
+core.map "talk",\r
+  :action => 'bot_talk',\r
+  :auth => 'core::talk::talk'\r
+core.map "quiet in :chan",\r
+  :action => 'bot_quiet',\r
+  :auth => 'core::talk::talk'\r
+\r
+core.map "join :chan :pass", \r
+  :action => 'bot_join',\r
+  :defaults => {:pass => nil},\r
+  :auth => 'core::movearound::join'\r
+core.map "part :chan",\r
+  :action => 'bot_part',\r
+  :defaults => {:chan => nil},\r
+  :auth => 'core::movearound::part'\r
+core.map "hide",\r
+  :action => 'bot_hide',\r
+  :auth => 'core::movearound::hide'\r
+\r
+core.map "say :where *what",\r
+  :action => 'bot_say',\r
+  :auth => 'core::talk::say'\r
+core.map "action :where *what",\r
+  :action => 'bot_action',\r
+  :auth => 'core::talk::act'\r
+core.map "mode :where :what *who",\r
+  :action => 'bot_mode',\r
+  :auth => 'core::talk::mode'\r
+\r
+core.map "ping",\r
+  :action => 'bot_ping'\r
+core.map "help *topic",\r
+  :action => 'bot_help',\r
+  :default => { :topic => [""] }\r
+\r
+# TODO the first line should probably go to the auth module?\r
+#\r
+core.default_auth('*', true)\r
+core.default_auth('core', false)\r
+core.default_auth('core::config::show', true)\r
+\r
index d567189b7c74c04e1a6b8a68983f5b6d4813d6bb..f1d9e127860d0d70d9b0e4ad26e98cafe7be10e9 100644 (file)
@@ -864,35 +864,23 @@ class IrcBot
       sendq "MODE #{channel} #{mode} #{target}", channel, 2
   end
 
-  # # m::     message asking for help
-  # # topic:: optional topic help is requested for
-  # # respond to online help requests
-  # def help(topic=nil)
-  #   topic = nil if topic == ""
-  #   case topic
-  #   when nil
-  #     helpstr = "help topics: core, auth, keywords"
-  #     helpstr += @plugins.helptopics
-  #     helpstr += " (help <topic> for more info)"
-  #   when /^core$/i
-  #     helpstr = corehelp
-  #   when /^core\s+(.+)$/i
-  #     helpstr = corehelp $1
-  #   when /^auth$/i
-  #     helpstr = @auth.help
-  #   when /^auth\s+(.+)$/i
-  #     helpstr = @auth.help $1
-  #   when /^keywords$/i
-  #     helpstr = @keywords.help
-  #   when /^keywords\s+(.+)$/i
-  #     helpstr = @keywords.help $1
-  #   else
-  #     unless(helpstr = @plugins.help(topic))
-  #       helpstr = "no help for topic #{topic}"
-  #     end
-  #   end
-  #   return helpstr
-  # end
+  # m::     message asking for help
+  # topic:: optional topic help is requested for
+  # respond to online help requests
+  def help(topic=nil)
+    topic = nil if topic == ""
+    case topic
+    when nil
+      helpstr = "help topics: "
+      helpstr += @plugins.helptopics
+      helpstr += " (help <topic> for more info)"
+    else
+      unless(helpstr = @plugins.help(topic))
+        helpstr = "no help for topic #{topic}"
+      end
+    end
+    return helpstr
+  end
 
   # returns a string describing the current status of the bot (uptime etc)
   def status
index bb4c744a6c238c9a821db33a44b3c4db8642ec4a..31fb11344c9861e8e5f46bb56fe78ceae177f5c0 100644 (file)
@@ -153,6 +153,14 @@ module Plugins
       end
     end
 
+    # Sets the default auth for command _cmd_ to _val_ on channel _chan_:
+    # usually _chan_ is either "*" for everywhere, public and private (in
+    # which case it can be omitted) or "?" for private communications
+    #
+    def default_auth(cmd, val, chan="*")
+      Auth::anonbotuser.set_permission(cmd, val)
+    end
+
     # return an identifier for this plugin, defaults to a list of the message
     # prefixes handled (used for error messages etc)
     def name
@@ -325,6 +333,8 @@ module Plugins
     # add one or more directories to the list of directories to
     # load botmodules from
     #
+    # TODO find a way to specify necessary plugins which _must_ be loaded
+    #
     def add_botmodule_dir(*dirlist)
       @dirs += dirlist
       debug "Botmodule loading path: #{@dirs.join(', ')}"
@@ -438,7 +448,7 @@ module Plugins
 
     # return list of help topics (plugin names)
     def helptopics
-      return " [#{status}]"
+      return status
     end
 
     def length
@@ -475,6 +485,7 @@ module Plugins
       when /^(\S+)\s*(.*)$/
         key = $1
         params = $2
+        # TODO should also check core_module and plugins
         [core_commands, plugin_commands].each { |pl|
           if(pl.has_key?(key))
             begin