+ # set path of the plugin that will be loaded next (see BotModule#initialize)
+ @next_plugin_path = File.dirname fname
+
+ plugin_module.module_eval(plugin_string, fname)
+
+ @next_plugin_path = nil
+
+ return :loaded
+ rescue Exception => err
+ # rescue TimeoutError, StandardError, NameError, LoadError, SyntaxError => err
+ error report_error("#{desc}#{fname} load failed", err)
+ bt = err.backtrace.select { |line|
+ line.match(/^(\(eval\)|#{fname}):\d+/)
+ }
+ bt.map! { |el|
+ el.gsub(/^\(eval\)(:\d+)(:in `.*')?(:.*)?/) { |m|
+ "#{fname}#{$1}#{$3}"
+ }
+ }
+ msg = err.to_s.gsub(/^\(eval\)(:\d+)(:in `.*')?(:.*)?/) { |m|
+ "#{fname}#{$1}#{$3}"
+ }
+ msg.gsub!(fname, File.basename(fname))
+ begin
+ newerr = err.class.new(msg)
+ rescue ArgumentError => aerr_in_err
+ # Somebody should hang the ActiveSupport developers by their balls
+ # with barbed wire. Their MissingSourceFile extension to LoadError
+ # _expects_ a second argument, breaking the usual Exception interface
+ # (instead, the smart thing to do would have been to make the second
+ # parameter optional and run the code in the from_message method if
+ # it was missing).
+ # Anyway, we try to cope with this in the simplest possible way. On
+ # the upside, this new block can be extended to handle other similar
+ # idiotic approaches
+ if err.class.respond_to? :from_message
+ newerr = err.class.from_message(msg)
+ elsif ([:file, :line, :column, :offset, :problem, :context] & err.methods).length == 6
+ # Another ‘brillian’ overload, this time from Psych::SyntaxError
+ # In this case we'll just leave the message as-is
+ newerr = err.dup
+ else
+ raise aerr_in_err
+ end
+ rescue NoMethodError => nmerr_in_err
+ # Another braindead extension to StandardError, OAuth2::Error,
+ # doesn't get a string as message, but a response
+ if err.respond_to? :response
+ newerr = err.class.new(err.response)
+ else
+ raise nmerr_in_err
+ end
+ end
+ newerr.set_backtrace(bt)
+ return newerr
+ end
+ end
+
+ # add one or more directories to the list of directories to
+ # load core modules from
+ def add_core_module_dir(*dirlist)
+ @core_module_dirs += dirlist
+ debug "Core module loading paths: #{@core_module_dirs.join(', ')}"
+ end
+
+ # add one or more directories to the list of directories to
+ # load plugins from
+ def add_plugin_dir(*dirlist)
+ @plugin_dirs += dirlist
+ debug "Plugin loading paths: #{@plugin_dirs.join(', ')}"
+ end
+
+ def clear_botmodule_dirs
+ @core_module_dirs.clear
+ @plugin_dirs.clear
+ debug "Core module and plugin loading paths cleared"
+ end
+
+ def scan_botmodules(opts={})
+ type = opts[:type]
+ processed = Hash.new
+
+ case type
+ when :core
+ dirs = @core_module_dirs
+ when :plugins
+ dirs = @plugin_dirs
+
+ @bot.config['plugins.blacklist'].each { |p|
+ pn = p + ".rb"
+ processed[pn.intern] = :blacklisted
+ }
+
+ whitelist = @bot.config['plugins.whitelist'].map { |p|
+ p + ".rb"
+ }
+ end
+
+ dirs.each do |dir|
+ next unless FileTest.directory?(dir)
+ d = Dir.new(dir)
+ d.sort.each do |file|
+ next unless file =~ /\.rb$/
+ next if file =~ /^\./
+
+ case type
+ when :plugins
+ if !whitelist.empty? && !whitelist.include?(file)
+ @ignored << {:name => file, :dir => dir, :reason => :"not whitelisted" }
+ next
+ elsif processed.has_key?(file.intern)