forked from k5bot/k5bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.rb
executable file
·149 lines (125 loc) · 3.51 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
65
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env ruby
# encoding: utf-8
# This file is part of the K5 bot project.
# See files README.md and COPYING for copyright and licensing information.
#noinspection RubyGlobalVariableNamingConvention
$VERBOSE = true
$stdout.sync = true
require 'rubygems'
require 'bundler/setup'
require 'i18n'
require 'i18n/backend/fallbacks'
require 'yaml'
File.dirname(__FILE__).tap do |lib_dir|
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
end
require 'IRC/IRCPluginManager'
class IRCHashPluginManager < IRCPluginManager
def initialize(config_name)
super()
@config_name = config_name
@config = nil
end
def reload_config
config_map = YAML.load_file(@config_name)
@config = normalize_config(config_map)
end
def load_all_plugins
plugins = @plugins.keys
prev_size = plugins.size - 1
while plugins.size > prev_size
prev_size = plugins.size
@config.keys.each do |plugin|
next if @plugins.include?(plugin)
begin
load_plugin(plugin)
rescue Exception => e
log(:error, "Exception during loading #{plugin}: #{e}")
raise e
end
end
plugins = @plugins.keys
end
raise if plugins.size != @config.size
end
def load_plugin(name)
begin
reload_config
rescue Exception => e
log(:error, "Config loading error: #{e}\n\t#{e.backtrace.join("\n\t")}")
return false
end
super
end
# The config read from yaml is an array, containing either
# string plugin_name, or
# hash { plugin_name => sub_config }.
# This function converts it into hash containing
# plugin_name => sub_config, for all plugins.
def normalize_config(config)
to_load = {}
config.each do |p|
name, config = parse_config_entry(p)
to_load[name] = config
end
to_load
end
def parse_config_entry(p)
if p.is_a?(Hash)
name = p.keys.first
config = p[name]
else
name = p
config = nil
end
return name.to_sym, config
end
def find_config_entry(name)
name = name.to_sym
[name, @config[name]]
end
end
config = if ARGV.first && File.exist?(ARGV.first)
ARGV.shift
else
File.exist?('config.yaml') ? 'config.yaml' : nil
end
if config == nil
puts 'Configuration file not found.'
exit 1
end
plugin_manager = IRCHashPluginManager.new(config) # Add plugin manager
begin
config = plugin_manager.reload_config
# Setup I18n
i18n_config = config[:I18N] || {}
I18n.enforce_available_locales = true
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.fallbacks = I18n::Locale::Fallbacks.new(:'en-GB')
I18n.load_path = Dir[File.join(File.dirname(__FILE__), 'locales', '*.yml')]
I18n.default_locale = i18n_config[:locale] || :'en-GB'
I18n.backend.load_translations
puts 'Loading plugins...'
plugin_manager.load_all_plugins # Load plugins
if plugin_manager.plugins[:Console]
plugin_manager.plugins[:Console].serve
else
puts 'All plugins loaded. Press Ctrl-C to terminate program.'
sleep
end
ensure
plugins = plugin_manager.plugins.keys
prev_size = plugins.size + 1
while plugins.size < prev_size
prev_size = plugins.size
plugins .each do |plugin|
begin
plugin_manager.unload_plugin(plugin)
rescue Exception => e
puts "Exception during unloading #{plugin}: #{e}"
end
end
plugins = plugin_manager.plugins.keys
end
puts "Plugins unloaded. #{' Failed to unload: ' + plugins.join(', ') + '.' unless plugins.empty?}"
end