X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fircbot.rb;h=7a021fb58f94bfa012840fcc57b20c648dfe77df;hb=be5d914984e767ce1a718b84d0bad1c88d9f8ea3;hp=1c03d93cee02be93f28c224ab4cae9bc02438c61;hpb=b9fd5f1df8db0a7e8654add8e7e1afb9099a022a;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index 1c03d93c..7a021fb5 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -437,36 +437,25 @@ class Bot botclass.gsub!("\\","/") end end - botclass += "/.rbot" + botclass = File.join(botclass, ".rbot") end botclass = File.expand_path(botclass) @botclass = botclass.gsub(/\/$/, "") - if FileTest.directory? botclass - # compare the templates dir with the current botclass, and fill it in with - # any missing file. - # Sadly, FileUtils.cp_r doesn't have an :update option, so we have to do it - # manually - template = File.join Config::datadir, 'templates' - # note that we use the */** pattern because we don't want to match - # keywords.rbot, which gets deleted on load and would therefore be missing always - missing = Dir.chdir(template) { Dir.glob('*/**') } - Dir.chdir(botclass) { Dir.glob('*/**') } - missing.map do |f| - dest = File.join(botclass, f) - FileUtils.mkdir_p File.dirname dest - FileUtils.cp File.join(template, f), dest - end - else - log "no #{botclass} directory found, creating from templates.." - if FileTest.exist? botclass - error "file #{botclass} exists but isn't a directory" - exit 2 - end - FileUtils.cp_r Config::datadir+'/templates', botclass - end + repopulate_botclass_directory - Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry") - Dir.mkdir("#{botclass}/safe_save") unless File.exist?("#{botclass}/safe_save") + registry_dir = File.join(@botclass, 'registry') + Dir.mkdir(registry_dir) unless File.exist?(registry_dir) + unless FileTest.directory? registry_dir + error "registry storage location #{registry_dir} is not a directory" + exit 2 + end + save_dir = File.join(@botclass, 'safe_save') + Dir.mkdir(save_dir) unless File.exist?(save_dir) + unless FileTest.directory? save_dir + error "safe save location #{save_dir} is not a directory" + exit 2 + end # Time at which the last PING was sent @last_ping = nil @@ -490,7 +479,9 @@ class Bot @logfile = @config['log.file'] if @logfile.class!=String || @logfile.empty? - @logfile = "#{botclass}/#{File.basename(botclass).gsub(/^\.+/,'')}.log" + logfname = File.basename(botclass).gsub(/^\.+/,'') + logfname << ".log" + @logfile = File.join(botclass, logfname) debug "Using `#{@logfile}' as debug log" end @@ -549,7 +540,7 @@ class Bot end end - File.open($opts['pidfile'] || "#{@botclass}/rbot.pid", 'w') do |pf| + File.open($opts['pidfile'] || File.join(@botclass, 'rbot.pid'), 'w') do |pf| pf << "#{$$}\n" end @@ -579,7 +570,6 @@ class Bot @auth.everyone.set_default_permission("*", true) @auth.botowner.password= @config['auth.password'] - Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins") @plugins = Plugins::manager @plugins.bot_associate(self) setup_plugins_path() @@ -776,15 +766,54 @@ class Bot trap_sigs end + def repopulate_botclass_directory + template_dir = File.join Config::datadir, 'templates' + if FileTest.directory? @botclass + # compare the templates dir with the current botclass dir, filling up the + # latter with any missing file. Sadly, FileUtils.cp_r doesn't have an + # :update option, so we have to do it manually. + # Note that we use the */** pattern because we don't want to match + # keywords.rbot, which gets deleted on load and would therefore be missing + # always + missing = Dir.chdir(template_dir) { Dir.glob('*/**') } - Dir.chdir(@botclass) { Dir.glob('*/**') } + missing.map do |f| + dest = File.join(@botclass, f) + FileUtils.mkdir_p(File.dirname dest) + FileUtils.cp File.join(template_dir, f), dest + end + else + log "no #{@botclass} directory found, creating from templates..." + if FileTest.exist? @botclass + error "file #{@botclass} exists but isn't a directory" + exit 2 + end + FileUtils.cp_r template_dir, @botclass + end + end + + # Return a path under the current botclass by joining the mentioned + # components. The components are automatically converted to String + def path(*components) + File.join(@botclass, *(components.map {|c| c.to_s})) + end + def setup_plugins_path + plugdir_default = File.join(Config::datadir, 'plugins') + plugdir_local = File.join(@botclass, 'plugins') + Dir.mkdir(plugdir_local) unless File.exist?(plugdir_local) + @plugins.clear_botmodule_dirs - @plugins.add_botmodule_dir(Config::coredir + "/utils") - @plugins.add_botmodule_dir(Config::coredir) - @plugins.add_botmodule_dir("#{botclass}/plugins") + @plugins.add_core_module_dir(File.join(Config::coredir, 'utils')) + @plugins.add_core_module_dir(Config::coredir) + if FileTest.directory? plugdir_local + @plugins.add_plugin_dir(plugdir_local) + else + warning "local plugin location #{plugdir_local} is not a directory" + end @config['plugins.path'].each do |_| - path = _.sub(/^\(default\)/, Config::datadir + '/plugins') - @plugins.add_botmodule_dir(path) + path = _.sub(/^\(default\)/, plugdir_default) + @plugins.add_plugin_dir(path) end end @@ -943,7 +972,7 @@ class Bot rescue Errno::ETIMEDOUT, Errno::ECONNABORTED, TimeoutError, SocketError => e error "network exception: #{e.pretty_inspect}" quit_msg = e.to_s - rescue ServerError + rescue ServerError => e # received an ERROR from the server quit_msg = "server ERROR: " + e.message too_fast = e.message.index("reconnect too fast") @@ -973,8 +1002,22 @@ class Bot # Type can be PRIVMSG, NOTICE, etc, but those you should really use the # relevant say() or notice() methods. This one should be used for IRCd # extensions you want to use in modules. - def sendmsg(type, where, original_message, options={}) - opts = @default_send_options.merge(options) + def sendmsg(original_type, original_where, original_message, options={}) + + # filter message with sendmsg filters + ds = DataStream.new original_message.to_s.dup, + :type => original_type, :dest => original_where, + :options => @default_send_options.merge(options) + filters = filter_names(:sendmsg) + filters.each do |fname| + debug "filtering #{ds[:text]} with sendmsg filter #{fname}" + ds.merge! filter(self.global_filter_name(fname, :sendmsg), ds) + end + + opts = ds[:options] + type = ds[:type] + where = ds[:dest] + filtered = ds[:text] # For starters, set up appropriate queue channels and rings mchan = opts[:queue_channel] @@ -995,7 +1038,7 @@ class Bot end end - multi_line = original_message.to_s.gsub(/[\r\n]+/, "\n") + multi_line = filtered.gsub(/[\r\n]+/, "\n") # if target is a channel with nocolor modes, strip colours if where.kind_of?(Channel) and where.mode.any?(*config['server.nocolor_modes'])