Class: ConfigModule

Inherits:
CoreBotModule
  • Object
show all
Defined in:
lib/rbot/core/config.rb

Overview

– vim:sw=2:et ++

:title: rbot config management from IRC

Author

Giuseppe “Oblomov” Bilotta <[email protected]>

Instance Method Summary collapse

Instance Method Details

#bot_nick(m, param) ⇒ Object



186
187
188
189
# File 'lib/rbot/core/config.rb', line 186

def bot_nick(m, param)
  @bot.nickchg(param[:nick])
  @bot.wanted_nick = param[:nick]
end

#bot_rescan(m, param) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/rbot/core/config.rb', line 178

def bot_rescan(m, param)
  m.reply _("saving ...")
  @bot.save
  m.reply _("rescanning ...")
  @bot.rescan
  m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
end

#bot_save(m, param) ⇒ Object



173
174
175
176
# File 'lib/rbot/core/config.rb', line 173

def bot_save(m, param)
  @bot.save
  m.okay
end

#bot_status(m, param) ⇒ Object



191
192
193
# File 'lib/rbot/core/config.rb', line 191

def bot_status(m, param)
  m.reply @bot.status
end

#bot_version(m, param) ⇒ Object

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


202
203
204
# File 'lib/rbot/core/config.rb', line 202

def bot_version(m, param)
  m.reply version_string
end

#ctcp_listen(m) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/rbot/core/config.rb', line 206

def ctcp_listen(m)
  who = m.private? ? "me" : m.target
  case m.ctcp.intern
  when :VERSION
    m.ctcp_reply version_string
  when :SOURCE
    m.ctcp_reply Irc::Bot::SOURCE_URL
  end
end

#handle_add(m, params) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rbot/core/config.rb', line 123

def handle_add(m, params)
  key = params[:key].to_s.intern
  values = params[:value].to_s.split(/,\s+/)
  unless @bot.config.items.has_key?(key)
    m.reply _("no such config key %{key}") % {:key => key}
    return
  end
  unless @bot.config.items[key].kind_of?(Config::ArrayValue)
    m.reply _("config key %{key} is not an array") % {:key => key}
    return
  end
  return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
  values.each do |value|
    begin
      @bot.config.items[key].add(value)
    rescue ArgumentError => e
      m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
      next
    end
  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

#handle_desc(m, params) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rbot/core/config.rb', line 63

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}") % {:key => key}
  end
  m.reply "#{key}: #{@bot.config.items[key].desc}"
end

#handle_get(m, params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/rbot/core/config.rb', line 52

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}") % {: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

#handle_help(m, params) ⇒ Object



216
217
218
# File 'lib/rbot/core/config.rb', line 216

def handle_help(m, params)
  m.reply help(params[:topic])
end

#handle_list(m, params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbot/core/config.rb', line 30

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 %{module}") % {: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

#handle_rm(m, params) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rbot/core/config.rb', line 148

def handle_rm(m, params)
  key = params[:key].to_s.intern
  values = params[:value].to_s.split(/,\s+/)
  unless @bot.config.items.has_key?(key)
    m.reply _("no such config key %{key}") % {:key => key}
    return
  end
  unless @bot.config.items[key].kind_of?(Config::ArrayValue)
    m.reply _("config key %{key} is not an array") % {:key => key}
    return
  end
  return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
  values.each do |value|
    begin
      @bot.config.items[key].rm(value)
    rescue ArgumentError => e
      m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
      next
    end
  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

#handle_search(m, params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbot/core/config.rb', line 71

def handle_search(m, params)
  rx = Regexp.new(params[:rx].to_s, true)
  cfs = []
  @bot.config.items.each do |k, v|
    cfs << [Bold + k.to_s + Bold, v.desc] if k.to_s.match(rx) or (v.desc.match(rx) rescue false)
  end
  if cfs.empty?
    m.reply _("no config key found matching %{r}") % { :r => params[:rx].to_s}
  else
    m.reply _("possible keys: %{kl}") % { :kl => cfs.map { |c| c.first}.sort.join(', ') } if cfs.length > 1
    m.reply cfs.map { |c| c.join(': ') }.join("\n")
  end
end

#handle_set(m, params) ⇒ Object



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
# File 'lib/rbot/core/config.rb', line 97

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}") % {:key => key} unless params[:silent]
    return false
  end
  return false 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}: %{error}") % {:key => key, :error => e.message} unless params[:silent]
    return false
  end
  if @bot.config.items[key].requires_restart
    m.reply _("this config change will take effect on the next restart") unless params[:silent]
    return :restart
  elsif @bot.config.items[key].requires_rescan
    m.reply _("this config change will take effect on the next rescan") unless params[:silent]
    return :rescan
  else
    m.okay unless params[:silent]
    return true
  end
end

#handle_unset(m, params) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbot/core/config.rb', line 85

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}") % {: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

#help(plugin, topic = "") ⇒ Object



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
# File 'lib/rbot/core/config.rb', line 220

def help(plugin, topic="")
  case plugin
  when "config"
    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 <values> to <key> => add values <values> 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
  when "nick"
    _("nick <newnick> => change the bot nick to <newnick>, if possible")
  when "status"
    _("status => display some information on the bot's status")
  when "save"
    _("save => save current dynamic data and configuration")
  when "rescan"
    _("rescan => reload modules and static facts")
  when "version"
    _("version => describes software version")
  else
    _("config-related tasks: config, save, rescan, version, nick, status")
  end
end

#saveObject



26
27
28
# File 'lib/rbot/core/config.rb', line 26

def save
  @bot.config.save
end

#version_stringObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rbot/core/config.rb', line 10

def version_string
  if $version_timestamp.to_i > 0
    ago = _(" [%{secs} ago]") % {
      :secs => Utils.secs_to_string(Time.now.to_i - $version_timestamp.to_i)
    }
  else
    ago = ''
  end
  _("I'm a v. %{version}%{ago} rubybot%{copyright}%{url}") % {
    :version => $version,
    :ago => ago,
    :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}",
    :url => " - #{Irc::Bot::SOURCE_URL}"
  }
end