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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#-- vim:sw=2:et
#++
class ConfigModule < CoreBotModule
def save
@bot.config.save
end
def handle_list(m, params)
modules = []
if params[:module]
@bot.config.items.each_key do |key|
mod, name = key.to_s.split('.')
next unless mod == params[:module]
modules.push key unless modules.include?(name)
end
if modules.empty?
m.reply "no such module #{params[:module]}"
else
m.reply modules.join(", ")
end
else
@bot.config.items.each_key do |key|
name = key.to_s.split('.').first
modules.push name unless modules.include?(name)
end
m.reply "modules: " + modules.join(", ")
end
end
def handle_get(m, params)
key = params[:key].to_s.intern
unless @bot.config.items.has_key?(key)
m.reply "no such config key #{key}"
return
end
return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
value = @bot.config.items[key].to_s
m.reply "#{key}: #{value}"
end
def handle_desc(m, params)
key = params[:key].to_s.intern
unless @bot.config.items.has_key?(key)
m.reply "no such config key #{key}"
end
puts @bot.config.items[key].inspect
m.reply "#{key}: #{@bot.config.items[key].desc}"
end
def handle_unset(m, params)
key = params[:key].to_s.intern
unless @bot.config.items.has_key?(key)
m.reply "no such config key #{key}"
end
return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
@bot.config.items[key].unset
handle_get(m, params)
m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
end
def handle_set(m, params)
key = params[:key].to_s.intern
value = params[:value].join(" ")
unless @bot.config.items.has_key?(key)
m.reply "no such config key #{key}"
return
end
return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
begin
@bot.config.items[key].set_string(value)
rescue ArgumentError => e
m.reply "failed to set #{key}: #{e.message}"
return
end
if @bot.config.items[key].requires_restart
m.reply "this config change will take effect on the next restart"
elsif @bot.config.items[key].requires_rescan
m.reply "this config change will take effect on the next rescan"
else
m.okay
end
end
def handle_add(m, params)
key = params[:key].to_s.intern
value = params[:value]
unless @bot.config.items.has_key?(key)
m.reply "no such config key #{key}"
return
end
unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
m.reply "config key #{key} is not an array"
return
end
return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
begin
@bot.config.items[key].add(value)
rescue ArgumentError => e
m.reply "failed to add #{value} to #{key}: #{e.message}"
return
end
handle_get(m,{:key => key})
m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
end
def handle_rm(m, params)
key = params[:key].to_s.intern
value = params[:value]
unless @bot.config.items.has_key?(key)
m.reply "no such config key #{key}"
return
end
unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
m.reply "config key #{key} is not an array"
return
end
return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
begin
@bot.config.items[key].rm(value)
rescue ArgumentError => e
m.reply "failed to remove #{value} from #{key}: #{e.message}"
return
end
handle_get(m,{:key => key})
m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
end
def bot_save(m, param)
@bot.save
m.okay
end
def bot_rescan(m, param)
m.reply "saving ..."
@bot.save
m.reply "rescanning ..."
@bot.rescan
m.reply "done. #{@bot.plugins.status(true)}"
end
def bot_nick(m, param)
@bot.nickchg(param[:nick])
end
def bot_status(m, param)
m.reply @bot.status
end
# TODO is this one of the methods that disappeared when the bot was moved
# from the single-file to the multi-file registry?
#
# def bot_reg_stat(m, param)
# m.reply @registry.stat.inspect
# end
def bot_version(m, param)
m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
end
def handle_help(m, params)
m.reply help(params[:topic])
end
def help(plugin, topic="")
case topic
when "list"
"config list => list configuration modules, config list <module> => list configuration keys for module <module>"
when "get"
"config get <key> => get configuration value for key <key>"
when "unset"
"reset key <key> to the default"
when "set"
"config set <key> <value> => set configuration value for key <key> to <value>"
when "desc"
"config desc <key> => describe what key <key> configures"
when "add"
"config add <value> to <key> => add value <value> to key <key> if <key> is an array"
when "rm"
"config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"
else
"config module - bot configuration. usage: list, desc, get, set, unset, add, rm"
# else
# "no help for config #{topic}"
end
end
end
conf = ConfigModule.new
conf.map 'config list :module',
:action => 'handle_list',
:defaults => {:module => false},
:auth_path => 'show'
# TODO this one is presently a security risk, since the bot
# stores the master password in the config. Do we need auth levels
# on the BotConfig keys too?
conf.map 'config get :key',
:action => 'handle_get',
:auth_path => 'show'
conf.map 'config desc :key',
:action => 'handle_desc',
:auth_path => 'show'
conf.map 'config describe :key',
:action => 'handle_desc',
:auth_path => 'show'
conf.map "save",
:action => 'bot_save'
conf.map "rescan",
:action => 'bot_rescan'
conf.map "nick :nick",
:action => 'bot_nick'
conf.map "status",
:action => 'bot_status',
:auth_path => 'show::status'
# TODO see above
#
# conf.map "registry stats",
# :action => 'bot_reg_stat',
# :auth_path => '!config::status'
conf.map "version",
:action => 'bot_version',
:auth_path => 'show::status'
conf.map 'config set :key *value',
:action => 'handle_set',
:auth_path => 'edit'
conf.map 'config add :value to :key',
:action => 'handle_add',
:auth_path => 'edit'
conf.map 'config rm :value from :key',
:action => 'handle_rm',
:auth_path => 'edit'
conf.map 'config del :value from :key',
:action => 'handle_rm',
:auth_path => 'edit'
conf.map 'config delete :value from :key',
:action => 'handle_rm',
:auth_path => 'edit'
conf.map 'config unset :key',
:action => 'handle_unset',
:auth_path => 'edit'
conf.map 'config reset :key',
:action => 'handle_unset',
:auth_path => 'edit'
conf.map 'config help :topic',
:action => 'handle_help',
:defaults => {:topic => false},
:auth_path => '!help!'
conf.default_auth('*', false)
conf.default_auth('show::status', true)
# TODO these shouldn't be set here, we need a way to let the default
# permission be specified together with the BotConfigValue
conf.default_auth('key', true)
conf.default_auth('key::auth::password', false)
|