Module: Cinchize

Defined in:
lib/cinchize.rb

Defined Under Namespace

Classes: Cinchize

Constant Summary collapse

Options =
{
  :ontop => true,
  :system => false,
  :local_config => File.join(Dir.pwd, 'cinchize.yml'),
  :system_config => '/etc/cinchize.yml',
  :action => nil,
}

Class Method Summary collapse

Class Method Details

.config(options, network) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cinchize.rb', line 66

def self.config options, network
  config_file = options[:system] ? options[:system_config]: options[:local_config]
  unless File.exists? config_file
    config_file.gsub! /yml$/, "json"
  end
  
  raise ArgumentError.new "there's no config file located at: #{config_file.gsub(/json$/, 'yml')}" unless File.exists? config_file
  raise ArgumentError.new "needs a network" if network.nil? or network.empty?

  if File.extname(config_file) == ".yml"
    cfg = YAML.load_file config_file
  else 
    require 'json'
    puts "WARNING: the json config format has be deprecated in favour of YaML"
    cfg = JSON.parse open(config_file, 'r').read()
  end
  
  raise ArgumentError.new "there's no server config in the config file" unless cfg.has_key? "servers"
  raise ArgumentError.new "the config file doesn't contain a config for #{network}" unless cfg["servers"].has_key? network
  
  ntw = cfg["servers"][network]
  
  plugins = []
  plugin_options = {}
  
  ntw.delete("plugins").each do |plugin|
    begin
      raise LoadError.new "the module can't be null" if plugin["module"].nil?
      raise NameError.new "the class can't be null" if plugin["class"].nil?
      
      require plugin["module"]
    
      clazz = nil
      plugin["class"].split("::").inject(Object) { |m,n| clazz = m.const_get(n) }
      plugins << clazz 
    
      plugin_options[clazz] = plugin["options"] || {}
    rescue LoadError => e
      puts "error while loading the module: #{e}"
    rescue NameError => e
      puts "error while loading the class: #{e}"
    end
  end

  raise ArgumentError.new "no plugins loaded" if plugins.size == 0

  cfg["options"] ||= {}
  dir_mode = cfg["options"].key?("dir_mode") ? cfg["options"]["dir_mode"] : "normal"
      
  daemon_options = {
    :dir_mode => dir_mode.to_sym,
    :dir => cfg["options"]["dir"] || Dir.getwd,
    :log_output => cfg["options"]["log_output"] || false,
    :app_name => "cinchize_#{network}",
    :ontop => options[:ontop],
  }

  [daemon_options, ntw, plugins, plugin_options]
end

.runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cinchize.rb', line 22

def self.run
  options = Options.dup
  
  ARGV.options do |o|
    o.set_summary_indent '  '
    o.banner = "Usage: #{File.basename $0} [Options] network"
    
    o.on("-d", "--daemonize", "Daemonize") { 
      options[:ontop] = false
    }
    
    o.on("-s", "--system-config", "Use system config") { 
      options[:system] = true
    }
    
    o.on("--start", "Start the bot") {
      options[:action] = :start
    }

    o.on("--status", "Status of the bot") {
      options[:action] = :status
    }
    
    o.on("--stop", "Stop the bot") {
      options[:action] = :stop
    }
    
    o.on("--restart", "Restart the bot") {
      options[:action] = :restart
    }
    
    o.parse!
  end

  daemon = Cinchize.new *config(options, ARGV.first)
  daemon.send options[:action]
rescue NoMethodError => e
  puts "Error: no such method"
  exit 1
rescue ArgumentError => e
  puts "Error: #{e}"
  exit 1
end