From: Giuseppe Bilotta Date: Wed, 2 Aug 2006 00:04:19 +0000 (+0000) Subject: Modularized core now functional. Still a lot to do and auth missing, but the bot... X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=7bef95455cfbec9cf77db283a11e177f4c981b6a;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git Modularized core now functional. Still a lot to do and auth missing, but the bot is almost usable now --- diff --git a/lib/rbot/botuser.rb b/lib/rbot/botuser.rb index 67d7d842..98408a0d 100644 --- a/lib/rbot/botuser.rb +++ b/lib/rbot/botuser.rb @@ -258,10 +258,7 @@ module Irc def set_permission(cmd, val) raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val) Irc::error_if_not_command(cmd) - cmd.path.each { |k| - set_permission(k.to_s, true) unless @perm.has_key?(k) - } - @perm[path.last] = val + @perm[cmd.command] = val end # Tells if command _cmd_ is permitted. We do this by returning @@ -308,7 +305,12 @@ module Irc def set_permission(cmd, val, chan="*") k = chan.to_s.to_sym @perm[k] = PermissionSet.new unless @perm.has_key?(k) - @perm[k].set_permission(cmd, val) + case cmd + when String + @perm[k].set_permission(Command.new(cmd), val) + else + @perm[k].set_permission(cmd, val) + end end # Checks if BotUser is allowed to do something on channel _chan_, diff --git a/lib/rbot/core/core.rb b/lib/rbot/core/core.rb index c9210d59..fcf5ac3a 100644 --- a/lib/rbot/core/core.rb +++ b/lib/rbot/core/core.rb @@ -4,8 +4,6 @@ class Core < CoreBotModule - # TODO cleanup - # handle incoming IRC PRIVMSG +m+ def listen(m) return unless m.class <= PrivMessage if(m.private? && m.message =~ /^\001PING\s+(.+)\001/) @@ -13,105 +11,116 @@ class Core < CoreBotModule @bot.irclog "@ #{m.sourcenick} pinged me" return end + end + + def bot_join(m, param) + if param[:pass] + @bot.join param[:chan], param[:pass] + else + @bot.join param[:chan] + end + end + + def bot_part(m, param) + if param[:chan] + @bot.part param[:chan] + else + @bot.part m.target if m.public? + end + end + + def bot_quit(m, param) + @bot.quit(param[:msg] ? param[:msg].join(" ") : nil) + end + + def bot_restart(m, param) + @bot.restart(param[:msg] ? param[:msg].join(" ") : nil) + end + + def bot_hide(m, param) + @bot.join 0 + end + + def bot_save(m, param) + @bot.save + m.okay + end + + def bot_nick(m, param) + @bot.nickchg(param[:nick]) + end + + def bot_say(m, param) + @bot.say param[:where], param[:what].join(" ") + end + + def bot_action(m, param) + @bot.action param[:where], param[:what].join(" ") + end + + def bot_mode(m, param) + @bot.mode param[:where], param[:what], param[:who].join(" ") + end + + def bot_ping(m, param) + m.reply "pong" + end + + def bot_rescan(m, param) + m.reply "saving ..." + @bot.save + m.reply "rescanning ..." + @bot.rescan + m.reply "done. #{@plugins.status(true)}" + end + + def bot_quiet(m, param) + if param.has_key?(:where) + @bot.set_quiet param[:where].sub(/^here$/, m.target) + else + @bot.set_quiet + end + end - if(m.address?) - case m.message - when (/^join\s+(\S+)\s+(\S+)$/i) - @bot.join $1, $2 if(@bot.auth.allow?("join", m.source, m.replyto)) - when (/^join\s+(\S+)$/i) - @bot.join $1 if(@bot.auth.allow?("join", m.source, m.replyto)) - when (/^part$/i) - @bot.part m.target if(m.public? && @bot.auth.allow?("join", m.source, m.replyto)) - when (/^part\s+(\S+)$/i) - @bot.part $1 if(@bot.auth.allow?("join", m.source, m.replyto)) - when (/^quit(?:\s+(.*))?$/i) - @bot.quit $1 if(@bot.auth.allow?("quit", m.source, m.replyto)) - when (/^restart(?:\s+(.*))?$/i) - @bot.restart $1 if(@bot.auth.allow?("quit", m.source, m.replyto)) - when (/^hide$/i) - @bot.join 0 if(@bot.auth.allow?("join", m.source, m.replyto)) - when (/^save$/i) - if(@bot.auth.allow?("config", m.source, m.replyto)) - @bot.save - m.okay - end - when (/^nick\s+(\S+)$/i) - @bot.nickchg($1) if(@bot.auth.allow?("nick", m.source, m.replyto)) - when (/^say\s+(\S+)\s+(.*)$/i) - @bot.say $1, $2 if(@bot.auth.allow?("say", m.source, m.replyto)) - when (/^action\s+(\S+)\s+(.*)$/i) - @bot.action $1, $2 if(@bot.auth.allow?("say", m.source, m.replyto)) - # when (/^topic\s+(\S+)\s+(.*)$/i) - # topic $1, $2 if(@bot.auth.allow?("topic", m.source, m.replyto)) - when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i) - @bot.mode $1, $2, $3 if(@bot.auth.allow?("mode", m.source, m.replyto)) - when (/^ping$/i) - @bot.say m.replyto, "pong" - when (/^rescan$/i) - if(@bot.auth.allow?("config", m.source, m.replyto)) - m.reply "saving ..." - @bot.save - m.reply "rescanning ..." - @bot.rescan - m.reply "done. #{@plugins.status(true)}" - end - when (/^quiet$/i) - if(@bot.auth.allow?("talk", m.source, m.replyto)) - m.okay - @bot.set_quiet - end - when (/^quiet in (\S+)$/i) - where = $1 - if(@bot.auth.allow?("talk", m.source, m.replyto)) - m.okay - where.gsub!(/^here$/, m.target) if m.public? - @bot.set_quiet(where) - end - when (/^talk$/i) - if(@bot.auth.allow?("talk", m.source, m.replyto)) - @bot.reset_quiet - m.okay - end - when (/^talk in (\S+)$/i) - where = $1 - if(@bot.auth.allow?("talk", m.source, m.replyto)) - where.gsub!(/^here$/, m.target) if m.public? - @bot.reset_quiet(where) - m.okay - end - when (/^status\??$/i) - m.reply status if @bot.auth.allow?("status", m.source, m.replyto) - when (/^registry stats$/i) - if @bot.auth.allow?("config", m.source, m.replyto) - m.reply @registry.stat.inspect - end - when (/^(help\s+)?config(\s+|$)/) - @config.privmsg(m) - when (/^(version)|(introduce yourself)$/i) - @bot.say m.replyto, "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/" - when (/^help(?:\s+(.*))?$/i) - @bot.say m.replyto, help($1) - #TODO move these to a "chatback" plugin - when (/^(botsnack|ciggie)$/i) - @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?) - @bot.say m.replyto, @lang.get("thanks") if(m.private?) - when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i) - @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?) - @bot.say m.replyto, @lang.get("hello") if(m.private?) - end + def bot_talk(m, param) + if param.has_key?(:where) + @bot.reset_quiet param[:where].sub(/^here$/, m.target) else - # stuff to handle when not addressed - case m.message - when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@bot.nick)}$/i) - @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick - when (/^#{Regexp.escape(@bot.nick)}!*$/) - @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick - else - # @keywords.privmsg(m) - end + @bot.reset_quiet end end + def bot_status(m, param) + m.reply @bot.status + end + + # TODO is this one of the methods that disappeared when the bot was moved + # from the single-file to the multi-file registry? + # + # def bot_reg_stat(m, param) + # m.reply @registry.stat.inspect + # end + + def bot_version(m, param) + m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/" + end + + def bot_help(m, param) + m.reply @bot.help(param[:topic].join(" ")) + end + + #TODO move these to a "chatback" plugin + # when (/^(botsnack|ciggie)$/i) + # @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?) + # @bot.say m.replyto, @lang.get("thanks") if(m.private?) + # when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i) + # @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?) + # @bot.say m.replyto, @lang.get("hello") if(m.private?) + # when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@bot.nick)}$/i) + # @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick + # when (/^#{Regexp.escape(@bot.nick)}!*$/) + # @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick + # handle help requests for "core" topics def help(topic="") case topic @@ -141,10 +150,10 @@ class Core < CoreBotModule return "talk [in here|] => with no arguments, resume speaking in all channels, if \"in here\", resume speaking in this channel, or resume speaking in " when "version" return "version => describes software version" - when "botsnack" - return "botsnack => reward #{myself} for being good" - when "hello" - return "hello|hi|hey|yo [#{myself}] => greet the bot" + # when "botsnack" + # return "botsnack => reward #{myself} for being good" + # when "hello" + # return "hello|hi|hey|yo [#{myself}] => greet the bot" else return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello" end @@ -153,3 +162,80 @@ end core = Core.new +core.map "quit *msg", + :action => 'bot_quit', + :defaults => { :msg => nil }, + :auth => 'core::quit::quit' +core.map "restart *msg", + :action => 'bot_restart', + :defaults => { :msg => nil }, + :auth => 'core::quit::restart' + +core.map "save", + :action => 'bot_save', + :auth => 'core::config::save' +core.map "rescan", + :action => 'bot_rescan', + :auth => 'core::config::rescan' +core.map "nick :nick", + :action => 'bot_nick', + :auth => 'core::config::nick' +core.map "status", + :action => 'bot_status', + :auth => 'core::config::show::status' + # TODO see above + # + # core.map "registry stats", + # :action => 'bot_reg_stat', + # :auth => 'core::config::show::registry' +core.map "version", + :action => 'bot_version', + :auth => 'core::config::show::version' + +core.map "quiet", + :action => 'bot_quiet', + :auth => 'core::talk::quiet' +core.map "quiet in :chan", + :action => 'bot_quiet', + :auth => 'core::talk::quiet' +core.map "talk", + :action => 'bot_talk', + :auth => 'core::talk::talk' +core.map "quiet in :chan", + :action => 'bot_quiet', + :auth => 'core::talk::talk' + +core.map "join :chan :pass", + :action => 'bot_join', + :defaults => {:pass => nil}, + :auth => 'core::movearound::join' +core.map "part :chan", + :action => 'bot_part', + :defaults => {:chan => nil}, + :auth => 'core::movearound::part' +core.map "hide", + :action => 'bot_hide', + :auth => 'core::movearound::hide' + +core.map "say :where *what", + :action => 'bot_say', + :auth => 'core::talk::say' +core.map "action :where *what", + :action => 'bot_action', + :auth => 'core::talk::act' +core.map "mode :where :what *who", + :action => 'bot_mode', + :auth => 'core::talk::mode' + +core.map "ping", + :action => 'bot_ping' +core.map "help *topic", + :action => 'bot_help', + :default => { :topic => [""] } + +# TODO the first line should probably go to the auth module? +# +core.default_auth('*', true) +core.default_auth('core', false) +core.default_auth('core::config::show', true) + diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index d567189b..f1d9e127 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -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 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 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 diff --git a/lib/rbot/plugins.rb b/lib/rbot/plugins.rb index bb4c744a..31fb1134 100644 --- a/lib/rbot/plugins.rb +++ b/lib/rbot/plugins.rb @@ -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