Change soft-fail to use the config, rather than env
[rbx.git] / tools / rubuildius / matzbot / lib / matzbot.rb
blobf26c11420d80cecbcc934be868854b76f7154ceb
1 $:.unshift File.dirname(__FILE__)
2 require 'session'
3 require 'client'
4 require 'commands'
6 module MatzBot
7   extend self
9   @loaded_plugins ||= {}
11   def reload!
12     load File.join(File.expand_path(File.dirname(__FILE__)), 'session.rb')
13     load File.join(File.expand_path(File.dirname(__FILE__)), 'client.rb')
14     reload_plugins!
15   end
17   def reload_plugins!
18     commands_file = File.join(File.expand_path(File.dirname(__FILE__)), 'commands.rb')
19     if [commands_file, plugin_files].flatten.any? { |f| plugin_changed? f }
20       Commands.send(:reload_filters!)
21       MatzBot.send(:remove_const, :Commands)
22       load commands_file
23       load_plugins
24     end
25   end
27   def load_plugins
28     plugin_files.each do |file|
29       puts "Loading #{file}..."
30       begin
31         touch_plugin(file)
32         load file 
33       rescue Object => e
34         puts "Unable to load #{file}, disabled."
35         p e
36       end
37     end
38   end
40   def plugin_files
41     [dot_matz_bot, 'plugins'].map do |directory|
42       next unless File.exists? directory
43       Dir[File.join(directory, '*.rb')]
44     end.flatten.compact
45   end
47   def dot_matz_bot
48     @dot_matz_bot ||= 
49       if PLATFORM =~ /win32/ 
50         dot_matz_bot = ENV['HOMEDRIVE'] + ENV['HOMEPATH']
51         dot_matz_bot = File.join(home, 'MatzBot')
52       else
53         dot_matz_bot = File.join(File.expand_path("~/matzbot/"), "configuration")
54       end
55   end
57   def plugin_changed?(file)
58     touch_plugin(file) if !@loaded_plugins[file] || @loaded_plugins[file] < File.mtime(file)
59   end
61   def touch_plugin(file)
62     @loaded_plugins[file] = File.mtime(file)
63   end
64 end