diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-03-04 18:24:57 +0100 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2008-03-04 18:24:57 +0100 |
commit | 6f9bfa43ac907700fcba394e0f6b9d987b1192fb (patch) | |
tree | 90058ef94a1cdf86b51b70bfdafd17b8f26ea33a /lib/rbot/core | |
parent | 02e8b672cdf3bf8c67f78a2a7fc028c4c6304a30 (diff) |
Unixify all line endings.
Some files had DOS-style line endings. Change all of them to Unix-style.
Diffstat (limited to 'lib/rbot/core')
-rw-r--r-- | lib/rbot/core/auth.rb | 1936 | ||||
-rw-r--r-- | lib/rbot/core/basics.rb | 376 | ||||
-rw-r--r-- | lib/rbot/core/config.rb | 650 |
3 files changed, 1481 insertions, 1481 deletions
diff --git a/lib/rbot/core/auth.rb b/lib/rbot/core/auth.rb index b0aa12c1..d6167535 100644 --- a/lib/rbot/core/auth.rb +++ b/lib/rbot/core/auth.rb @@ -1,968 +1,968 @@ -#-- vim:sw=2:et
-#++
-#
-# :title: rbot auth management from IRC
-#
-# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2006,2007 Giuseppe Bilotta
-# License:: GPL v2
-
-class AuthModule < CoreBotModule
-
- def initialize
- super
-
- # The namespace migration causes each Irc::Auth::PermissionSet to be
- # unrecoverable, and we have to rename their class name to
- # Irc::Bot::Auth::PermissionSet
- @registry.recovery = Proc.new { |val|
- patched = val.sub("o:\035Irc::Auth::PermissionSet", "o:\042Irc::Bot::Auth::PermissionSet")
- Marshal.restore(patched)
- }
-
- load_array(:default, true)
- debug "initialized auth. Botusers: #{@bot.auth.save_array.pretty_inspect}"
- end
-
- def save
- save_array
- end
-
- def save_array(key=:default)
- if @bot.auth.changed?
- @registry[key] = @bot.auth.save_array
- @bot.auth.reset_changed
- debug "saved botusers (#{key}): #{@registry[key].pretty_inspect}"
- end
- end
-
- def load_array(key=:default, forced=false)
- debug "loading botusers (#{key}): #{@registry[key].pretty_inspect}"
- @bot.auth.load_array(@registry[key], forced) if @registry.has_key?(key)
- end
-
- # The permission parameters accept arguments with the following syntax:
- # cmd_path... [on #chan .... | in here | in private]
- # This auxiliary method scans the array _ar_ to see if it matches
- # the given syntax: it expects + or - signs in front of _cmd_path_
- # elements when _setting_ = true
- #
- # It returns an array whose first element is the array of cmd_path,
- # the second element is an array of locations and third an array of
- # warnings occurred while parsing the strings
- #
- def parse_args(ar, setting)
- cmds = []
- locs = []
- warns = []
- doing_cmds = true
- next_must_be_chan = false
- want_more = false
- last_idx = 0
- ar.each_with_index { |x, i|
- if doing_cmds # parse cmd_path
- # check if the list is done
- if x == "on" or x == "in"
- doing_cmds = false
- next_must_be_chan = true if x == "on"
- next
- end
- if "+-".include?(x[0])
- warns << ArgumentError.new(_("please do not use + or - in front of command %{command} when resetting") % {:command => x}) unless setting
- else
- warns << ArgumentError.new(_("+ or - expected in front of %{string}") % {:string => x}) if setting
- end
- cmds << x
- else # parse locations
- if x[-1].chr == ','
- want_more = true
- else
- want_more = false
- end
- case next_must_be_chan
- when false
- locs << x.gsub(/^here$/,'_').gsub(/^private$/,'?')
- else
- warns << ArgumentError.new(_("'%{string}' doesn't look like a channel name") % {:string => x}) unless @bot.server.supports[:chantypes].include?(x[0])
- locs << x
- end
- unless want_more
- last_idx = i
- break
- end
- end
- }
- warns << _("trailing comma") if want_more
- warns << _("you probably forgot a comma") unless last_idx == ar.length - 1
- return cmds, locs, warns
- end
-
- def auth_edit_perm(m, params)
-
- setting = m.message.split[1] == "set"
- splits = params[:args]
-
- has_for = splits[-2] == "for"
- return usage(m) unless has_for
-
- begin
- user = @bot.auth.get_botuser(splits[-1].sub(/^all$/,"everyone"))
- rescue
- return m.reply(_("couldn't find botuser %{name}") % {:name => splits[-1]})
- end
- return m.reply(_("you can't change permissions for %{username}") % {:username => user.username}) if user.owner?
- splits.slice!(-2,2) if has_for
-
- cmds, locs, warns = parse_args(splits, setting)
- errs = warns.select { |w| w.kind_of?(Exception) }
-
- unless errs.empty?
- m.reply _("couldn't satisfy your request: %{errors}") % {:errors => errs.join(',')}
- return
- end
-
- if locs.empty?
- locs << "*"
- end
- begin
- locs.each { |loc|
- ch = loc
- if m.private?
- ch = "?" if loc == "_"
- else
- ch = m.target.to_s if loc == "_"
- end
- cmds.each { |setval|
- if setting
- val = setval[0].chr == '+'
- cmd = setval[1..-1]
- user.set_permission(cmd, val, ch)
- else
- cmd = setval
- user.reset_permission(cmd, ch)
- end
- }
- }
- rescue => e
- m.reply "something went wrong while trying to set the permissions"
- raise
- end
- @bot.auth.set_changed
- debug "user #{user} permissions changed"
- m.okay
- end
-
- def auth_view_perm(m, params)
- begin
- if params[:user].nil?
- user = get_botusername_for(m.source)
- return m.reply(_("you are owner, you can do anything")) if user.owner?
- else
- user = @bot.auth.get_botuser(params[:user].sub(/^all$/,"everyone"))
- return m.reply(_("owner can do anything")) if user.owner?
- end
- rescue
- return m.reply(_("couldn't find botuser %{name}") % {:name => params[:user]})
- end
- perm = user.perm
- str = []
- perm.each { |k, val|
- next if val.perm.empty?
- case k
- when :*
- str << _("on any channel: ")
- when :"?"
- str << _("in private: ")
- else
- str << _("on #{k}: ")
- end
- sub = []
- val.perm.each { |cmd, bool|
- sub << (bool ? "+" : "-")
- sub.last << cmd.to_s
- }
- str.last << sub.join(', ')
- }
- if str.empty?
- m.reply _("no permissions set for %{user}") % {:user => user.username}
- else
- m.reply _("permissions for %{user}:: %{permissions}") %
- { :user => user.username, :permissions => str.join('; ')}
- end
- end
-
- def auth_search_perm(m, p)
- pattern = Regexp.new(p[:pattern].to_s)
- results = @bot.plugins.maps.select { |k, v| k.match(pattern) }
- count = results.length
- max = @bot.config['send.max_lines']
- extra = (count > max ? _(". only %{max} will be shown") : "") % { :max => max }
- m.reply _("%{count} commands found matching %{pattern}%{extra}") % {
- :count => count, :pattern => pattern, :extra => extra
- }
- return if count == 0
- results[0,max].each { |cmd, hash|
- m.reply _("%{cmd}: %{perms}") % {
- :cmd => cmd,
- :perms => hash[:auth].join(", ")
- }
- }
- end
-
- def get_botuser_for(user)
- @bot.auth.irc_to_botuser(user)
- end
-
- def get_botusername_for(user)
- get_botuser_for(user).username
- end
-
- def welcome(user)
- _("welcome, %{user}") % {:user => get_botusername_for(user)}
- end
-
- def auth_auth(m, params)
- params[:botuser] = 'owner'
- auth_login(m,params)
- end
-
- def auth_login(m, params)
- begin
- case @bot.auth.login(m.source, params[:botuser], params[:password])
- when true
- m.reply welcome(m.source)
- @bot.auth.set_changed
- else
- m.reply _("sorry, can't do")
- end
- rescue => e
- m.reply _("couldn't login: %{exception}") % {:exception => e}
- raise
- end
- end
-
- def auth_autologin(m, params)
- u = do_autologin(m.source)
- if u.default?
- m.reply _("I couldn't find anything to let you login automatically")
- else
- m.reply welcome(m.source)
- end
- end
-
- def do_autologin(user)
- @bot.auth.autologin(user)
- end
-
- def auth_whoami(m, params)
- m.reply _("you are %{who}") % {
- :who => get_botusername_for(m.source).gsub(
- /^everyone$/, _("no one that I know")).gsub(
- /^owner$/, _("my boss"))
- }
- end
-
- def auth_whois(m, params)
- return auth_whoami(m, params) if !m.public?
- u = m.channel.users[params[:user]]
-
- return m.reply("I don't see anyone named '#{params[:user]}' here") unless u
-
- m.reply _("#{params[:user]} is %{who}") % {
- :who => get_botusername_for(u).gsub(
- /^everyone$/, _("no one that I know")).gsub(
- /^owner$/, _("my boss"))
- }
- end
-
- def help(cmd, topic="")
- case cmd
- when "login"
- return _("login [<botuser>] [<pass>]: logs in to the bot as botuser <botuser> with password <pass>. When using the full form, you must contact the bot in private. <pass> can be omitted if <botuser> allows login-by-mask and your netmask is among the known ones. if <botuser> is omitted too autologin will be attempted")
- when "whoami"
- return _("whoami: names the botuser you're linked to")
- when "who"
- return _("who is <user>: names the botuser <user> is linked to")
- when /^permission/
- case topic
- when "syntax"
- return _("a permission is specified as module::path::to::cmd; when you want to enable it, prefix it with +; when you want to disable it, prefix it with -; when using the +reset+ command, do not use any prefix")
- when "set", "reset", "[re]set", "(re)set"
- return _("permissions [re]set <permission> [in <channel>] for <user>: sets or resets the permissions for botuser <user> in channel <channel> (use ? to change the permissions for private addressing)")
- when "view"
- return _("permissions view [for <user>]: display the permissions for user <user>")
- when "searc"
- return _("permissions search <pattern>: display the permissions associated with the commands matching <pattern>")
- else
- return _("permission topics: syntax, (re)set, view, search")
- end
- when "user"
- case topic
- when "show"
- return _("user show <what> : shows info about the user; <what> can be any of autologin, login-by-mask, netmasks")
- when /^(en|dis)able/
- return _("user enable|disable <what> : turns on or off <what> (autologin, login-by-mask)")
- when "set"
- return _("user set password <blah> : sets the user password to <blah>; passwords can only contain upper and lowercase letters and numbers, and must be at least 4 characters long")
- when "add", "rm"
- return _("user add|rm netmask <mask> : adds/removes netmask <mask> from the list of netmasks known to the botuser you're linked to")
- when "reset"
- return _("user reset <what> : resets <what> to the default values. <what> can be +netmasks+ (the list will be emptied), +autologin+ or +login-by-mask+ (will be reset to the default value) or +password+ (a new one will be generated and you'll be told in private)")
- when "tell"
- return _("user tell <who> the password for <botuser> : contacts <who> in private to tell him/her the password for <botuser>")
- when "create"
- return _("user create <name> <password> : create botuser named <name> with password <password>. The password can be omitted, in which case a random one will be generated. The <name> should only contain alphanumeric characters and the underscore (_)")
- when "list"
- return _("user list : lists all the botusers")
- when "destroy"
- return _("user destroy <botuser> : destroys <botuser>. This function %{highlight}must%{highlight} be called in two steps. On the first call <botuser> is queued for destruction. On the second call, which must be in the form 'user confirm destroy <botuser>', the botuser will be destroyed. If you want to cancel the destruction, issue the command 'user cancel destroy <botuser>'") % {:highlight => Bold}
- else
- return _("user topics: show, enable|disable, add|rm netmask, set, reset, tell, create, list, destroy")
- end
- when "auth"
- return _("auth <masterpassword>: log in as the bot owner; other commands: login, whoami, permission syntax, permissions [re]set, permissions view, user, meet, hello")
- when "meet"
- return _("meet <nick> [as <user>]: creates a bot user for nick, calling it user (defaults to the nick itself)")
- when "hello"
- return _("hello: creates a bot user for the person issuing the command")
- else
- return _("auth commands: auth, login, whoami, who, permission[s], user, meet, hello")
- end
- end
-
- def need_args(cmd)
- _("sorry, I need more arguments to %{command}") % {:command => cmd}
- end
-
- def not_args(cmd, *stuff)
- _("I can only %{command} these: %{arguments}") %
- {:command => cmd, :arguments => stuff.join(', ')}
- end
-
- def set_prop(botuser, prop, val)
- k = prop.to_s.gsub("-","_")
- botuser.send( (k + "=").to_sym, val)
- if prop == :password and botuser == @bot.auth.botowner
- @bot.config.items[:'auth.password'].set_string(@bot.auth.botowner.password)
- end
- end
-
- def reset_prop(botuser, prop)
- k = prop.to_s.gsub("-","_")
- botuser.send( ("reset_"+k).to_sym)
- end
-
- def ask_bool_prop(botuser, prop)
- k = prop.to_s.gsub("-","_")
- botuser.send( (k + "?").to_sym)
- end
-
- def auth_manage_user(m, params)
- splits = params[:data]
-
- cmd = splits.first
- return auth_whoami(m, params) if cmd.nil?
-
- botuser = get_botuser_for(m.source)
- # By default, we do stuff on the botuser the irc user is bound to
- butarget = botuser
-
- has_for = splits[-2] == "for"
- if has_for
- butarget = @bot.auth.get_botuser(splits[-1]) rescue nil
- return m.reply(_("no such bot user %{user}") % {:user => splits[-1]}) unless butarget
- splits.slice!(-2,2)
- end
- return m.reply(_("you can't mess with %{user}") % {:user => butarget.username}) if butarget.owner? && botuser != butarget
-
- bools = [:autologin, :"login-by-mask"]
- can_set = [:password]
- can_addrm = [:netmasks]
- can_reset = bools + can_set + can_addrm
- can_show = can_reset + ["perms"]
-
- begin
- case cmd.to_sym
-
- when :show
- return m.reply(_("you can't see the properties of %{user}") %
- {:user => butarget.username}) if botuser != butarget &&
- !botuser.permit?("auth::show::other")
-
- case splits[1]
- when nil, "all"
- props = can_reset
- when "password"
- if botuser != butarget
- return m.reply(_("no way I'm telling you the master password!")) if butarget == @bot.auth.botowner
- return m.reply(_("you can't ask for someone else's password"))
- end
- return m.reply(_("c'mon, you can't be asking me seriously to tell you the password in public!")) if m.public?
- return m.reply(_("the password for %{user} is %{password}") %
- { :user => butarget.username, :password => butarget.password })
- else
- props = splits[1..-1]
- end
-
- str = []
-
- props.each { |arg|
- k = arg.to_sym
- next if k == :password
- case k
- when *bools
- if ask_bool_prop(butarget, k)
- str << _("can %{action}") % {:action => k}
- else
- str << _("can not %{action}") % {:action => k}
- end
- when :netmasks
- if butarget.netmasks.empty?
- str << _("knows no netmasks")
- else
- str << _("knows %{netmasks}") % {:netmasks => butarget.netmasks.join(", ")}
- end
- end
- }
- return m.reply("#{butarget.username} #{str.join('; ')}")
-
- when :enable, :disable
- return m.reply(_("you can't change the default user")) if butarget.default? && !botuser.permit?("auth::edit::other::default")
- return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if butarget != botuser && !botuser.permit?("auth::edit::other")
-
- return m.reply(need_args(cmd)) unless splits[1]
- things = []
- skipped = []
- splits[1..-1].each { |a|
- arg = a.to_sym
- if bools.include?(arg)
- set_prop(butarget, arg, cmd.to_sym == :enable)
- things << a
- else
- skipped << a
- end
- }
-
- m.reply(_("I ignored %{things} because %{reason}") % {
- :things => skipped.join(', '),
- :reason => not_args(cmd, *bools)}) unless skipped.empty?
- if things.empty?
- m.reply _("I haven't changed anything")
- else
- @bot.auth.set_changed
- return auth_manage_user(m, {:data => ["show"] + things + ["for", butarget.username] })
- end
-
- when :set
- return m.reply(_("you can't change the default user")) if
- butarget.default? && !botuser.permit?("auth::edit::default")
- return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if
- butarget != botuser && !botuser.permit?("auth::edit::other")
-
- return m.reply(need_args(cmd)) unless splits[1]
- arg = splits[1].to_sym
- return m.reply(not_args(cmd, *can_set)) unless can_set.include?(arg)
- argarg = splits[2]
- return m.reply(need_args([cmd, splits[1]].join(" "))) unless argarg
- if arg == :password && m.public?
- return m.reply(_("is that a joke? setting the password in public?"))
- end
- set_prop(butarget, arg, argarg)
- @bot.auth.set_changed
- auth_manage_user(m, {:data => ["show", arg.to_s, "for", butarget.username] })
-
- when :reset
- return m.reply(_("you can't change the default user")) if
- butarget.default? && !botuser.permit?("auth::edit::default")
- return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if
- butarget != botuser && !botuser.permit?("auth::edit::other")
-
- return m.reply(need_args(cmd)) unless splits[1]
- things = []
- skipped = []
- splits[1..-1].each { |a|
- arg = a.to_sym
- if can_reset.include?(arg)
- reset_prop(butarget, arg)
- things << a
- else
- skipped << a
- end
- }
-
- m.reply(_("I ignored %{things} because %{reason}") %
- { :things => skipped.join(', '),
- :reason => not_args(cmd, *can_reset)}) unless skipped.empty?
- if things.empty?
- m.reply _("I haven't changed anything")
- else
- @bot.auth.set_changed
- @bot.say(m.source, _("the password for %{user} is now %{password}") %
- {:user => butarget.username, :password => butarget.password}) if
- things.include?("password")
- return auth_manage_user(m, {:data => (["show"] + things - ["password"]) + ["for", butarget.username]})
- end
-
- when :add, :rm, :remove, :del, :delete
- return m.reply(_("you can't change the default user")) if
- butarget.default? && !botuser.permit?("auth::edit::default")
- return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if
- butarget != botuser && !botuser.permit?("auth::edit::other")
-
- arg = splits[1]
- if arg.nil? or arg !~ /netmasks?/ or splits[2].nil?
- return m.reply(_("I can only add/remove netmasks. See +help user add+ for more instructions"))
- end
-
- method = cmd.to_sym == :add ? :add_netmask : :delete_netmask
-
- failed = []
-
- splits[2..-1].each { |mask|
- begin
- butarget.send(method, mask.to_irc_netmask(:server => @bot.server))
- rescue => e
- debug "failed with #{e.message}"
- debug e.backtrace.join("\n")
- failed << mask
- end
- }
- m.reply "I failed to #{cmd} #{failed.join(', ')}" unless failed.empty?
- @bot.auth.set_changed
- return auth_manage_user(m, {:data => ["show", "netmasks", "for", butarget.username] })
-
- else
- m.reply _("sorry, I don't know how to %{request}") % {:request => m.message}
- end
- rescue => e
- m.reply _("couldn't %{cmd}: %{exception}") % {:cmd => cmd, :exception => e}
- end
- end
-
- def auth_meet(m, params)
- nick = params[:nick]
- if !nick
- # we are actually responding to a 'hello' command
- unless m.botuser.transient?
- m.reply @bot.lang.get('hello_X') % m.botuser
- return
- end
- nick = m.sourcenick
- irc_user = m.source
- else
- # m.channel is always an Irc::Channel because the command is either
- # public-only 'meet' or private/public 'hello' which was handled by
- # the !nick case, so this shouldn't fail
- irc_user = m.channel.users[nick]
- return m.reply("I don't see anyone named '#{nick}' here") unless irc_user
- end
- # BotUser name
- buname = params[:user] || nick
- begin
- call_event(:botuser,:pre_perm, {:irc_user => irc_user, :bot_user => buname})
- met = @bot.auth.make_permanent(irc_user, buname)
- @bot.auth.set_changed
- call_event(:botuser,:post_perm, {:irc_user => irc_user, :bot_user => buname})
- m.reply @bot.lang.get('hello_X') % met
- @bot.say nick, _("you are now registered as %{buname}. I created a random password for you : %{pass} and you can change it at any time by telling me 'user set password <password>' in private" % {
- :buname => buname,
- :pass => met.password
- })
- rescue RuntimeError
- # or can this happen for other cases too?
- # TODO autologin if forced
- m.reply _("but I already know %{buname}" % {:buname => buname})
- rescue => e
- m.reply _("I had problems meeting %{nick}: %{e}" % { :nick => nick, :e => e })
- end
- end
-
- def auth_tell_password(m, params)
- user = params[:user]
- begin
- botuser = @bot.auth.get_botuser(params[:botuser])
- rescue
- return m.reply(_("couldn't find botuser %{user}") % {:user => params[:botuser]})
- end
- m.reply(_("I'm not telling the master password to anyway, pal")) if botuser == @bot.auth.botowner
- msg = _("the password for botuser %{user} is %{password}") %
- {:user => botuser.username, :password => botuser.password}
- @bot.say user, msg
- @bot.say m.source, _("I told %{user} that %{message}") % {:user => user, :message => msg}
- end
-
- def auth_create_user(m, params)
- name = params[:name]
- password = params[:password]
- return m.reply(_("are you nuts, creating a botuser with a publicly known password?")) if m.public? and not password.nil?
- begin
- bu = @bot.auth.create_botuser(name, password)
- @bot.auth.set_changed
- rescue => e
- m.reply(_("failed to create %{user}: %{exception}") % {:user => name, :exception => e})
- debug e.inspect + "\n" + e.backtrace.join("\n")
- return
- end
- m.reply(_("created botuser %{user}") % {:user => bu.username})
- end
-
- def auth_list_users(m, params)
- # TODO name regexp to filter results
- list = @bot.auth.save_array.inject([]) { |list, x| ['everyone', 'owner'].include?(x[:username]) ? list : list << x[:username] }
- if defined?(@destroy_q)
- list.map! { |x|
- @destroy_q.include?(x) ? x + _(" (queued for destruction)") : x
- }
- end
- return m.reply(_("I have no botusers other than the default ones")) if list.empty?
- return m.reply(n_("botuser: %{list}", "botusers: %{list}", list.length) %
- {:list => list.join(', ')})
- end
-
- def auth_destroy_user(m, params)
- @destroy_q = [] unless defined?(@destroy_q)
- buname = params[:name]
- return m.reply(_("You can't destroy %{user}") % {:user => buname}) if
- ["everyone", "owner"].include?(buname)
- mod = params[:modifier].to_sym rescue nil
-
- buser_array = @bot.auth.save_array
- buser_hash = buser_array.inject({}) { |h, u|
- h[u[:username]] = u
- h
- }
-
- return m.reply(_("no such botuser %{user}") % {:user=>buname}) unless
- buser_hash.keys.include?(buname)
-
- case mod
- when :cancel
- if @destroy_q.include?(buname)
- @destroy_q.delete(buname)
- m.reply(_("%{user} removed from the destruction queue") % {:user=>buname})
- else
- m.reply(_("%{user} was not queued for destruction") % {:user=>buname})
- end
- return
- when nil
- if @destroy_q.include?(buname)
- return m.reply(_("%{user} already queued for destruction, use %{highlight}user confirm destroy %{user}%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold})
- else
- @destroy_q << buname
- return m.reply(_("%{user} queued for destruction, use %{highlight}user confirm destroy %{user}%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold})
- end
- when :confirm
- begin
- return m.reply(_("%{user} is not queued for destruction yet") %
- {:user=>buname}) unless @destroy_q.include?(buname)
- buser_array.delete_if { |u|
- u[:username] == buname
- }
- @destroy_q.delete(buname)
- @bot.auth.load_array(buser_array, true)
- @bot.auth.set_changed
- rescue => e
- return m.reply(_("failed: %{exception}") % {:exception => e})
- end
- return m.reply(_("botuser %{user} destroyed") % {:user => buname})
- end
- end
-
- def auth_copy_ren_user(m, params)
- source = Auth::BotUser.sanitize_username(params[:source])
- dest = Auth::BotUser.sanitize_username(params[:dest])
- return m.reply(_("please don't touch the default users")) unless
- (["everyone", "owner"] & [source, dest]).empty?
-
- buser_array = @bot.auth.save_array
- buser_hash = buser_array.inject({}) { |h, u|
- h[u[:username]] = u
- h
- }
-
- return m.reply(_("no such botuser %{source}") % {:source=>source}) unless
- buser_hash.keys.include?(source)
- return m.reply(_("botuser %{dest} exists already") % {:dest=>dest}) if
- buser_hash.keys.include?(dest)
-
- copying = m.message.split[1] == "copy"
- begin
- if copying
- h = {}
- buser_hash[source].each { |k, val|
- h[k] = val.dup
- }
- else
- h = buser_hash[source]
- end
- h[:username] = dest
- buser_array << h if copying
-
- @bot.auth.load_array(buser_array, true)
- @bot.auth.set_changed
- call_event(:botuser, copying ? :copy : :rename, :source => source, :dest => dest)
- rescue => e
- return m.reply(_("failed: %{exception}") % {:exception=>e})
- end
- if copying
- m.reply(_("botuser %{source} copied to %{dest}") %
- {:source=>source, :dest=>dest})
- else
- m.reply(_("botuser %{source} renamed to %{dest}") %
- {:source=>source, :dest=>dest})
- end
-
- end
-
- def auth_export(m, params)
-
- exportfile = "#{@bot.botclass}/new-auth.users"
-
- what = params[:things]
-
- has_to = what[-2] == "to"
- if has_to
- exportfile = "#{@bot.botclass}/#{what[-1]}"
- what.slice!(-2,2)
- end
-
- what.delete("all")
-
- m.reply _("selecting data to export ...")
-
- buser_array = @bot.auth.save_array
- buser_hash = buser_array.inject({}) { |h, u|
- h[u[:username]] = u
- h
- }
-
- if what.empty?
- we_want = buser_hash
- else
- we_want = buser_hash.delete_if { |key, val|
- not what.include?(key)
- }
- end
-
- m.reply _("preparing data for export ...")
- begin
- yaml_hash = {}
- we_want.each { |k, val|
- yaml_hash[k] = {}
- val.each { |kk, v|
- case kk
- when :username
- next
- when :netmasks
- yaml_hash[k][kk] = []
- v.each { |nm|
- yaml_hash[k][kk] << {
- :fullform => nm.fullform,
- :casemap => nm.casemap.to_s
- }
- }
- else
- yaml_hash[k][kk] = v
- end
- }
- }
- rescue => e
- m.reply _("failed to prepare data: %{exception}") % {:exception=>e}
- debug e.backtrace.dup.unshift(e.inspect).join("\n")
- return
- end
-
- m.reply _("exporting to %{file} ...") % {:file=>exportfile}
- begin
- # m.reply yaml_hash.inspect
- File.open(exportfile, "w") do |file|
- file.puts YAML::dump(yaml_hash)
- end
- rescue => e
- m.reply _("failed to export users: %{exception}") % {:exception=>e}
- debug e.backtrace.dup.unshift(e.inspect).join("\n")
- return
- end
- m.reply _("done")
- end
-
- def auth_import(m, params)
-
- importfile = "#{@bot.botclass}/new-auth.users"
-
- what = params[:things]
-
- has_from = what[-2] == "from"
- if has_from
- importfile = "#{@bot.botclass}/#{what[-1]}"
- what.slice!(-2,2)
- end
-
- what.delete("all")
-
- m.reply _("reading %{file} ...") % {:file=>importfile}
- begin
- yaml_hash = YAML::load_file(importfile)
- rescue => e
- m.reply _("failed to import from: %{exception}") % {:exception=>e}
- debug e.backtrace.dup.unshift(e.inspect).join("\n")
- return
- end
-
- # m.reply yaml_hash.inspect
-
- m.reply _("selecting data to import ...")
-
- if what.empty?
- we_want = yaml_hash
- else
- we_want = yaml_hash.delete_if { |key, val|
- not what.include?(key)
- }
- end
-
- m.reply _("parsing data from import ...")
-
- buser_hash = {}
-
- begin
- yaml_hash.each { |k, val|
- buser_hash[k] = { :username => k }
- val.each { |kk, v|
- case kk
- when :netmasks
- buser_hash[k][kk] = []
- v.each { |nm|
- buser_hash[k][kk] << nm[:fullform].to_irc_netmask(:casemap => nm[:casemap].to_irc_casemap).to_irc_netmask(:server => @bot.server)
- }
- else
- buser_hash[k][kk] = v
- end
- }
- }
- rescue => e
- m.reply _("failed to parse data: %{exception}") % {:exception=>e}
- debug e.backtrace.dup.unshift(e.inspect).join("\n")
- return
- end
-
- # m.reply buser_hash.inspect
-
- org_buser_array = @bot.auth.save_array
- org_buser_hash = org_buser_array.inject({}) { |h, u|
- h[u[:username]] = u
- h
- }
-
- # TODO we may want to do a(n optional) key-by-key merge
- #
- org_buser_hash.merge!(buser_hash)
- new_buser_array = org_buser_hash.values
- @bot.auth.load_array(new_buser_array, true)
- @bot.auth.set_changed
-
- m.reply _("done")
- end
-
-end
-
-auth = AuthModule.new
-
-auth.map "user export *things",
- :action => 'auth_export',
- :defaults => { :things => ['all'] },
- :auth_path => ':manage:fedex:'
-
-auth.map "user import *things",
- :action => 'auth_import',
- :auth_path => ':manage:fedex:'
-
-auth.map "user create :name :password",
- :action => 'auth_create_user',
- :defaults => {:password => nil},
- :auth_path => ':manage:'
-
-auth.map "user [:modifier] destroy :name",
- :action => 'auth_destroy_user',
- :requirements => { :modifier => /^(cancel|confirm)?$/ },
- :defaults => { :modifier => '' },
- :auth_path => ':manage::destroy!'
-
-auth.map "user copy :source [to] :dest",
- :action => 'auth_copy_ren_user',
- :auth_path => ':manage:'
-
-auth.map "user rename :source [to] :dest",
- :action => 'auth_copy_ren_user',
- :auth_path => ':manage:'
-
-auth.map "meet :nick [as :user]",
- :action => 'auth_meet',
- :auth_path => 'user::manage', :private => false
-
-auth.map "hello",
- :action => 'auth_meet',
- :auth_path => 'user::manage::meet'
-
-auth.default_auth("user::manage", false)
-auth.default_auth("user::manage::meet::hello", true)
-
-auth.map "user tell :user the password for :botuser",
- :action => 'auth_tell_password',
- :auth_path => ':manage:'
-
-auth.map "user list",
- :action => 'auth_list_users',
- :auth_path => '::'
-
-auth.map "user *data",
- :action => 'auth_manage_user'
-
-auth.default_auth("user", true)
-auth.default_auth("edit::other", false)
-
-auth.map "whoami",
- :action => 'auth_whoami',
- :auth_path => '!*!'
-
-auth.map "who is :user",
- :action => 'auth_whois',
- :auth_path => '!*!'
-
-auth.map "auth :password",
- :action => 'auth_auth',
- :public => false,
- :auth_path => '!login!'
-
-auth.map "login :botuser :password",
- :action => 'auth_login',
- :public => false,
- :defaults => { :password => nil },
- :auth_path => '!login!'
-
-auth.map "login :botuser",
- :action => 'auth_login',
- :auth_path => '!login!'
-
-auth.map "login",
- :action => 'auth_autologin',
- :auth_path => '!login!'
-
-auth.map "permissions set *args",
- :action => 'auth_edit_perm',
- :auth_path => ':edit::set:'
-
-auth.map "permissions reset *args",
- :action => 'auth_edit_perm',
- :auth_path => ':edit::set:'
-
-auth.map "permissions view [for :user]",
- :action => 'auth_view_perm',
- :auth_path => '::'
-
-auth.map "permissions search *pattern",
- :action => 'auth_search_perm',
- :auth_path => '::'
-
-auth.default_auth('*', false)
-
+#-- vim:sw=2:et +#++ +# +# :title: rbot auth management from IRC +# +# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com> +# Copyright:: (C) 2006,2007 Giuseppe Bilotta +# License:: GPL v2 + +class AuthModule < CoreBotModule + + def initialize + super + + # The namespace migration causes each Irc::Auth::PermissionSet to be + # unrecoverable, and we have to rename their class name to + # Irc::Bot::Auth::PermissionSet + @registry.recovery = Proc.new { |val| + patched = val.sub("o:\035Irc::Auth::PermissionSet", "o:\042Irc::Bot::Auth::PermissionSet") + Marshal.restore(patched) + } + + load_array(:default, true) + debug "initialized auth. Botusers: #{@bot.auth.save_array.pretty_inspect}" + end + + def save + save_array + end + + def save_array(key=:default) + if @bot.auth.changed? + @registry[key] = @bot.auth.save_array + @bot.auth.reset_changed + debug "saved botusers (#{key}): #{@registry[key].pretty_inspect}" + end + end + + def load_array(key=:default, forced=false) + debug "loading botusers (#{key}): #{@registry[key].pretty_inspect}" + @bot.auth.load_array(@registry[key], forced) if @registry.has_key?(key) + end + + # The permission parameters accept arguments with the following syntax: + # cmd_path... [on #chan .... | in here | in private] + # This auxiliary method scans the array _ar_ to see if it matches + # the given syntax: it expects + or - signs in front of _cmd_path_ + # elements when _setting_ = true + # + # It returns an array whose first element is the array of cmd_path, + # the second element is an array of locations and third an array of + # warnings occurred while parsing the strings + # + def parse_args(ar, setting) + cmds = [] + locs = [] + warns = [] + doing_cmds = true + next_must_be_chan = false + want_more = false + last_idx = 0 + ar.each_with_index { |x, i| + if doing_cmds # parse cmd_path + # check if the list is done + if x == "on" or x == "in" + doing_cmds = false + next_must_be_chan = true if x == "on" + next + end + if "+-".include?(x[0]) + warns << ArgumentError.new(_("please do not use + or - in front of command %{command} when resetting") % {:command => x}) unless setting + else + warns << ArgumentError.new(_("+ or - expected in front of %{string}") % {:string => x}) if setting + end + cmds << x + else # parse locations + if x[-1].chr == ',' + want_more = true + else + want_more = false + end + case next_must_be_chan + when false + locs << x.gsub(/^here$/,'_').gsub(/^private$/,'?') + else + warns << ArgumentError.new(_("'%{string}' doesn't look like a channel name") % {:string => x}) unless @bot.server.supports[:chantypes].include?(x[0]) + locs << x + end + unless want_more + last_idx = i + break + end + end + } + warns << _("trailing comma") if want_more + warns << _("you probably forgot a comma") unless last_idx == ar.length - 1 + return cmds, locs, warns + end + + def auth_edit_perm(m, params) + + setting = m.message.split[1] == "set" + splits = params[:args] + + has_for = splits[-2] == "for" + return usage(m) unless has_for + + begin + user = @bot.auth.get_botuser(splits[-1].sub(/^all$/,"everyone")) + rescue + return m.reply(_("couldn't find botuser %{name}") % {:name => splits[-1]}) + end + return m.reply(_("you can't change permissions for %{username}") % {:username => user.username}) if user.owner? + splits.slice!(-2,2) if has_for + + cmds, locs, warns = parse_args(splits, setting) + errs = warns.select { |w| w.kind_of?(Exception) } + + unless errs.empty? + m.reply _("couldn't satisfy your request: %{errors}") % {:errors => errs.join(',')} + return + end + + if locs.empty? + locs << "*" + end + begin + locs.each { |loc| + ch = loc + if m.private? + ch = "?" if loc == "_" + else + ch = m.target.to_s if loc == "_" + end + cmds.each { |setval| + if setting + val = setval[0].chr == '+' + cmd = setval[1..-1] + user.set_permission(cmd, val, ch) + else + cmd = setval + user.reset_permission(cmd, ch) + end + } + } + rescue => e + m.reply "something went wrong while trying to set the permissions" + raise + end + @bot.auth.set_changed + debug "user #{user} permissions changed" + m.okay + end + + def auth_view_perm(m, params) + begin + if params[:user].nil? + user = get_botusername_for(m.source) + return m.reply(_("you are owner, you can do anything")) if user.owner? + else + user = @bot.auth.get_botuser(params[:user].sub(/^all$/,"everyone")) + return m.reply(_("owner can do anything")) if user.owner? + end + rescue + return m.reply(_("couldn't find botuser %{name}") % {:name => params[:user]}) + end + perm = user.perm + str = [] + perm.each { |k, val| + next if val.perm.empty? + case k + when :* + str << _("on any channel: ") + when :"?" + str << _("in private: ") + else + str << _("on #{k}: ") + end + sub = [] + val.perm.each { |cmd, bool| + sub << (bool ? "+" : "-") + sub.last << cmd.to_s + } + str.last << sub.join(', ') + } + if str.empty? + m.reply _("no permissions set for %{user}") % {:user => user.username} + else + m.reply _("permissions for %{user}:: %{permissions}") % + { :user => user.username, :permissions => str.join('; ')} + end + end + + def auth_search_perm(m, p) + pattern = Regexp.new(p[:pattern].to_s) + results = @bot.plugins.maps.select { |k, v| k.match(pattern) } + count = results.length + max = @bot.config['send.max_lines'] + extra = (count > max ? _(". only %{max} will be shown") : "") % { :max => max } + m.reply _("%{count} commands found matching %{pattern}%{extra}") % { + :count => count, :pattern => pattern, :extra => extra + } + return if count == 0 + results[0,max].each { |cmd, hash| + m.reply _("%{cmd}: %{perms}") % { + :cmd => cmd, + :perms => hash[:auth].join(", ") + } + } + end + + def get_botuser_for(user) + @bot.auth.irc_to_botuser(user) + end + + def get_botusername_for(user) + get_botuser_for(user).username + end + + def welcome(user) + _("welcome, %{user}") % {:user => get_botusername_for(user)} + end + + def auth_auth(m, params) + params[:botuser] = 'owner' + auth_login(m,params) + end + + def auth_login(m, params) + begin + case @bot.auth.login(m.source, params[:botuser], params[:password]) + when true + m.reply welcome(m.source) + @bot.auth.set_changed + else + m.reply _("sorry, can't do") + end + rescue => e + m.reply _("couldn't login: %{exception}") % {:exception => e} + raise + end + end + + def auth_autologin(m, params) + u = do_autologin(m.source) + if u.default? + m.reply _("I couldn't find anything to let you login automatically") + else + m.reply welcome(m.source) + end + end + + def do_autologin(user) + @bot.auth.autologin(user) + end + + def auth_whoami(m, params) + m.reply _("you are %{who}") % { + :who => get_botusername_for(m.source).gsub( + /^everyone$/, _("no one that I know")).gsub( + /^owner$/, _("my boss")) + } + end + + def auth_whois(m, params) + return auth_whoami(m, params) if !m.public? + u = m.channel.users[params[:user]] + + return m.reply("I don't see anyone named '#{params[:user]}' here") unless u + + m.reply _("#{params[:user]} is %{who}") % { + :who => get_botusername_for(u).gsub( + /^everyone$/, _("no one that I know")).gsub( + /^owner$/, _("my boss")) + } + end + + def help(cmd, topic="") + case cmd + when "login" + return _("login [<botuser>] [<pass>]: logs in to the bot as botuser <botuser> with password <pass>. When using the full form, you must contact the bot in private. <pass> can be omitted if <botuser> allows login-by-mask and your netmask is among the known ones. if <botuser> is omitted too autologin will be attempted") + when "whoami" + return _("whoami: names the botuser you're linked to") + when "who" + return _("who is <user>: names the botuser <user> is linked to") + when /^permission/ + case topic + when "syntax" + return _("a permission is specified as module::path::to::cmd; when you want to enable it, prefix it with +; when you want to disable it, prefix it with -; when using the +reset+ command, do not use any prefix") + when "set", "reset", "[re]set", "(re)set" + return _("permissions [re]set <permission> [in <channel>] for <user>: sets or resets the permissions for botuser <user> in channel <channel> (use ? to change the permissions for private addressing)") + when "view" + return _("permissions view [for <user>]: display the permissions for user <user>") + when "searc" + return _("permissions search <pattern>: display the permissions associated with the commands matching <pattern>") + else + return _("permission topics: syntax, (re)set, view, search") + end + when "user" + case topic + when "show" + return _("user show <what> : shows info about the user; <what> can be any of autologin, login-by-mask, netmasks") + when /^(en|dis)able/ + return _("user enable|disable <what> : turns on or off <what> (autologin, login-by-mask)") + when "set" + return _("user set password <blah> : sets the user password to <blah>; passwords can only contain upper and lowercase letters and numbers, and must be at least 4 characters long") + when "add", "rm" + return _("user add|rm netmask <mask> : adds/removes netmask <mask> from the list of netmasks known to the botuser you're linked to") + when "reset" + return _("user reset <what> : resets <what> to the default values. <what> can be +netmasks+ (the list will be emptied), +autologin+ or +login-by-mask+ (will be reset to the default value) or +password+ (a new one will be generated and you'll be told in private)") + when "tell" + return _("user tell <who> the password for <botuser> : contacts <who> in private to tell him/her the password for <botuser>") + when "create" + return _("user create <name> <password> : create botuser named <name> with password <password>. The password can be omitted, in which case a random one will be generated. The <name> should only contain alphanumeric characters and the underscore (_)") + when "list" + return _("user list : lists all the botusers") + when "destroy" + return _("user destroy <botuser> : destroys <botuser>. This function %{highlight}must%{highlight} be called in two steps. On the first call <botuser> is queued for destruction. On the second call, which must be in the form 'user confirm destroy <botuser>', the botuser will be destroyed. If you want to cancel the destruction, issue the command 'user cancel destroy <botuser>'") % {:highlight => Bold} + else + return _("user topics: show, enable|disable, add|rm netmask, set, reset, tell, create, list, destroy") + end + when "auth" + return _("auth <masterpassword>: log in as the bot owner; other commands: login, whoami, permission syntax, permissions [re]set, permissions view, user, meet, hello") + when "meet" + return _("meet <nick> [as <user>]: creates a bot user for nick, calling it user (defaults to the nick itself)") + when "hello" + return _("hello: creates a bot user for the person issuing the command") + else + return _("auth commands: auth, login, whoami, who, permission[s], user, meet, hello") + end + end + + def need_args(cmd) + _("sorry, I need more arguments to %{command}") % {:command => cmd} + end + + def not_args(cmd, *stuff) + _("I can only %{command} these: %{arguments}") % + {:command => cmd, :arguments => stuff.join(', ')} + end + + def set_prop(botuser, prop, val) + k = prop.to_s.gsub("-","_") + botuser.send( (k + "=").to_sym, val) + if prop == :password and botuser == @bot.auth.botowner + @bot.config.items[:'auth.password'].set_string(@bot.auth.botowner.password) + end + end + + def reset_prop(botuser, prop) + k = prop.to_s.gsub("-","_") + botuser.send( ("reset_"+k).to_sym) + end + + def ask_bool_prop(botuser, prop) + k = prop.to_s.gsub("-","_") + botuser.send( (k + "?").to_sym) + end + + def auth_manage_user(m, params) + splits = params[:data] + + cmd = splits.first + return auth_whoami(m, params) if cmd.nil? + + botuser = get_botuser_for(m.source) + # By default, we do stuff on the botuser the irc user is bound to + butarget = botuser + + has_for = splits[-2] == "for" + if has_for + butarget = @bot.auth.get_botuser(splits[-1]) rescue nil + return m.reply(_("no such bot user %{user}") % {:user => splits[-1]}) unless butarget + splits.slice!(-2,2) + end + return m.reply(_("you can't mess with %{user}") % {:user => butarget.username}) if butarget.owner? && botuser != butarget + + bools = [:autologin, :"login-by-mask"] + can_set = [:password] + can_addrm = [:netmasks] + can_reset = bools + can_set + can_addrm + can_show = can_reset + ["perms"] + + begin + case cmd.to_sym + + when :show + return m.reply(_("you can't see the properties of %{user}") % + {:user => butarget.username}) if botuser != butarget && + !botuser.permit?("auth::show::other") + + case splits[1] + when nil, "all" + props = can_reset + when "password" + if botuser != butarget + return m.reply(_("no way I'm telling you the master password!")) if butarget == @bot.auth.botowner + return m.reply(_("you can't ask for someone else's password")) + end + return m.reply(_("c'mon, you can't be asking me seriously to tell you the password in public!")) if m.public? + return m.reply(_("the password for %{user} is %{password}") % + { :user => butarget.username, :password => butarget.password }) + else + props = splits[1..-1] + end + + str = [] + + props.each { |arg| + k = arg.to_sym + next if k == :password + case k + when *bools + if ask_bool_prop(butarget, k) + str << _("can %{action}") % {:action => k} + else + str << _("can not %{action}") % {:action => k} + end + when :netmasks + if butarget.netmasks.empty? + str << _("knows no netmasks") + else + str << _("knows %{netmasks}") % {:netmasks => butarget.netmasks.join(", ")} + end + end + } + return m.reply("#{butarget.username} #{str.join('; ')}") + + when :enable, :disable + return m.reply(_("you can't change the default user")) if butarget.default? && !botuser.permit?("auth::edit::other::default") + return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if butarget != botuser && !botuser.permit?("auth::edit::other") + + return m.reply(need_args(cmd)) unless splits[1] + things = [] + skipped = [] + splits[1..-1].each { |a| + arg = a.to_sym + if bools.include?(arg) + set_prop(butarget, arg, cmd.to_sym == :enable) + things << a + else + skipped << a + end + } + + m.reply(_("I ignored %{things} because %{reason}") % { + :things => skipped.join(', '), + :reason => not_args(cmd, *bools)}) unless skipped.empty? + if things.empty? + m.reply _("I haven't changed anything") + else + @bot.auth.set_changed + return auth_manage_user(m, {:data => ["show"] + things + ["for", butarget.username] }) + end + + when :set + return m.reply(_("you can't change the default user")) if + butarget.default? && !botuser.permit?("auth::edit::default") + return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if + butarget != botuser && !botuser.permit?("auth::edit::other") + + return m.reply(need_args(cmd)) unless splits[1] + arg = splits[1].to_sym + return m.reply(not_args(cmd, *can_set)) unless can_set.include?(arg) + argarg = splits[2] + return m.reply(need_args([cmd, splits[1]].join(" "))) unless argarg + if arg == :password && m.public? + return m.reply(_("is that a joke? setting the password in public?")) + end + set_prop(butarget, arg, argarg) + @bot.auth.set_changed + auth_manage_user(m, {:data => ["show", arg.to_s, "for", butarget.username] }) + + when :reset + return m.reply(_("you can't change the default user")) if + butarget.default? && !botuser.permit?("auth::edit::default") + return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if + butarget != botuser && !botuser.permit?("auth::edit::other") + + return m.reply(need_args(cmd)) unless splits[1] + things = [] + skipped = [] + splits[1..-1].each { |a| + arg = a.to_sym + if can_reset.include?(arg) + reset_prop(butarget, arg) + things << a + else + skipped << a + end + } + + m.reply(_("I ignored %{things} because %{reason}") % + { :things => skipped.join(', '), + :reason => not_args(cmd, *can_reset)}) unless skipped.empty? + if things.empty? + m.reply _("I haven't changed anything") + else + @bot.auth.set_changed + @bot.say(m.source, _("the password for %{user} is now %{password}") % + {:user => butarget.username, :password => butarget.password}) if + things.include?("password") + return auth_manage_user(m, {:data => (["show"] + things - ["password"]) + ["for", butarget.username]}) + end + + when :add, :rm, :remove, :del, :delete + return m.reply(_("you can't change the default user")) if + butarget.default? && !botuser.permit?("auth::edit::default") + return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if + butarget != botuser && !botuser.permit?("auth::edit::other") + + arg = splits[1] + if arg.nil? or arg !~ /netmasks?/ or splits[2].nil? + return m.reply(_("I can only add/remove netmasks. See +help user add+ for more instructions")) + end + + method = cmd.to_sym == :add ? :add_netmask : :delete_netmask + + failed = [] + + splits[2..-1].each { |mask| + begin + butarget.send(method, mask.to_irc_netmask(:server => @bot.server)) + rescue => e + debug "failed with #{e.message}" + debug e.backtrace.join("\n") + failed << mask + end + } + m.reply "I failed to #{cmd} #{failed.join(', ')}" unless failed.empty? + @bot.auth.set_changed + return auth_manage_user(m, {:data => ["show", "netmasks", "for", butarget.username] }) + + else + m.reply _("sorry, I don't know how to %{request}") % {:request => m.message} + end + rescue => e + m.reply _("couldn't %{cmd}: %{exception}") % {:cmd => cmd, :exception => e} + end + end + + def auth_meet(m, params) + nick = params[:nick] + if !nick + # we are actually responding to a 'hello' command + unless m.botuser.transient? + m.reply @bot.lang.get('hello_X') % m.botuser + return + end + nick = m.sourcenick + irc_user = m.source + else + # m.channel is always an Irc::Channel because the command is either + # public-only 'meet' or private/public 'hello' which was handled by + # the !nick case, so this shouldn't fail + irc_user = m.channel.users[nick] + return m.reply("I don't see anyone named '#{nick}' here") unless irc_user + end + # BotUser name + buname = params[:user] || nick + begin + call_event(:botuser,:pre_perm, {:irc_user => irc_user, :bot_user => buname}) + met = @bot.auth.make_permanent(irc_user, buname) + @bot.auth.set_changed + call_event(:botuser,:post_perm, {:irc_user => irc_user, :bot_user => buname}) + m.reply @bot.lang.get('hello_X') % met + @bot.say nick, _("you are now registered as %{buname}. I created a random password for you : %{pass} and you can change it at any time by telling me 'user set password <password>' in private" % { + :buname => buname, + :pass => met.password + }) + rescue RuntimeError + # or can this happen for other cases too? + # TODO autologin if forced + m.reply _("but I already know %{buname}" % {:buname => buname}) + rescue => e + m.reply _("I had problems meeting %{nick}: %{e}" % { :nick => nick, :e => e }) + end + end + + def auth_tell_password(m, params) + user = params[:user] + begin + botuser = @bot.auth.get_botuser(params[:botuser]) + rescue + return m.reply(_("couldn't find botuser %{user}") % {:user => params[:botuser]}) + end + m.reply(_("I'm not telling the master password to anyway, pal")) if botuser == @bot.auth.botowner + msg = _("the password for botuser %{user} is %{password}") % + {:user => botuser.username, :password => botuser.password} + @bot.say user, msg + @bot.say m.source, _("I told %{user} that %{message}") % {:user => user, :message => msg} + end + + def auth_create_user(m, params) + name = params[:name] + password = params[:password] + return m.reply(_("are you nuts, creating a botuser with a publicly known password?")) if m.public? and not password.nil? + begin + bu = @bot.auth.create_botuser(name, password) + @bot.auth.set_changed + rescue => e + m.reply(_("failed to create %{user}: %{exception}") % {:user => name, :exception => e}) + debug e.inspect + "\n" + e.backtrace.join("\n") + return + end + m.reply(_("created botuser %{user}") % {:user => bu.username}) + end + + def auth_list_users(m, params) + # TODO name regexp to filter results + list = @bot.auth.save_array.inject([]) { |list, x| ['everyone', 'owner'].include?(x[:username]) ? list : list << x[:username] } + if defined?(@destroy_q) + list.map! { |x| + @destroy_q.include?(x) ? x + _(" (queued for destruction)") : x + } + end + return m.reply(_("I have no botusers other than the default ones")) if list.empty? + return m.reply(n_("botuser: %{list}", "botusers: %{list}", list.length) % + {:list => list.join(', ')}) + end + + def auth_destroy_user(m, params) + @destroy_q = [] unless defined?(@destroy_q) + buname = params[:name] + return m.reply(_("You can't destroy %{user}") % {:user => buname}) if + ["everyone", "owner"].include?(buname) + mod = params[:modifier].to_sym rescue nil + + buser_array = @bot.auth.save_array + buser_hash = buser_array.inject({}) { |h, u| + h[u[:username]] = u + h + } + + return m.reply(_("no such botuser %{user}") % {:user=>buname}) unless + buser_hash.keys.include?(buname) + + case mod + when :cancel + if @destroy_q.include?(buname) + @destroy_q.delete(buname) + m.reply(_("%{user} removed from the destruction queue") % {:user=>buname}) + else + m.reply(_("%{user} was not queued for destruction") % {:user=>buname}) + end + return + when nil + if @destroy_q.include?(buname) + return m.reply(_("%{user} already queued for destruction, use %{highlight}user confirm destroy %{user}%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold}) + else + @destroy_q << buname + return m.reply(_("%{user} queued for destruction, use %{highlight}user confirm destroy %{user}%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold}) + end + when :confirm + begin + return m.reply(_("%{user} is not queued for destruction yet") % + {:user=>buname}) unless @destroy_q.include?(buname) + buser_array.delete_if { |u| + u[:username] == buname + } + @destroy_q.delete(buname) + @bot.auth.load_array(buser_array, true) + @bot.auth.set_changed + rescue => e + return m.reply(_("failed: %{exception}") % {:exception => e}) + end + return m.reply(_("botuser %{user} destroyed") % {:user => buname}) + end + end + + def auth_copy_ren_user(m, params) + source = Auth::BotUser.sanitize_username(params[:source]) + dest = Auth::BotUser.sanitize_username(params[:dest]) + return m.reply(_("please don't touch the default users")) unless + (["everyone", "owner"] & [source, dest]).empty? + + buser_array = @bot.auth.save_array + buser_hash = buser_array.inject({}) { |h, u| + h[u[:username]] = u + h + } + + return m.reply(_("no such botuser %{source}") % {:source=>source}) unless + buser_hash.keys.include?(source) + return m.reply(_("botuser %{dest} exists already") % {:dest=>dest}) if + buser_hash.keys.include?(dest) + + copying = m.message.split[1] == "copy" + begin + if copying + h = {} + buser_hash[source].each { |k, val| + h[k] = val.dup + } + else + h = buser_hash[source] + end + h[:username] = dest + buser_array << h if copying + + @bot.auth.load_array(buser_array, true) + @bot.auth.set_changed + call_event(:botuser, copying ? :copy : :rename, :source => source, :dest => dest) + rescue => e + return m.reply(_("failed: %{exception}") % {:exception=>e}) + end + if copying + m.reply(_("botuser %{source} copied to %{dest}") % + {:source=>source, :dest=>dest}) + else + m.reply(_("botuser %{source} renamed to %{dest}") % + {:source=>source, :dest=>dest}) + end + + end + + def auth_export(m, params) + + exportfile = "#{@bot.botclass}/new-auth.users" + + what = params[:things] + + has_to = what[-2] == "to" + if has_to + exportfile = "#{@bot.botclass}/#{what[-1]}" + what.slice!(-2,2) + end + + what.delete("all") + + m.reply _("selecting data to export ...") + + buser_array = @bot.auth.save_array + buser_hash = buser_array.inject({}) { |h, u| + h[u[:username]] = u + h + } + + if what.empty? + we_want = buser_hash + else + we_want = buser_hash.delete_if { |key, val| + not what.include?(key) + } + end + + m.reply _("preparing data for export ...") + begin + yaml_hash = {} + we_want.each { |k, val| + yaml_hash[k] = {} + val.each { |kk, v| + case kk + when :username + next + when :netmasks + yaml_hash[k][kk] = [] + v.each { |nm| + yaml_hash[k][kk] << { + :fullform => nm.fullform, + :casemap => nm.casemap.to_s + } + } + else + yaml_hash[k][kk] = v + end + } + } + rescue => e + m.reply _("failed to prepare data: %{exception}") % {:exception=>e} + debug e.backtrace.dup.unshift(e.inspect).join("\n") + return + end + + m.reply _("exporting to %{file} ...") % {:file=>exportfile} + begin + # m.reply yaml_hash.inspect + File.open(exportfile, "w") do |file| + file.puts YAML::dump(yaml_hash) + end + rescue => e + m.reply _("failed to export users: %{exception}") % {:exception=>e} + debug e.backtrace.dup.unshift(e.inspect).join("\n") + return + end + m.reply _("done") + end + + def auth_import(m, params) + + importfile = "#{@bot.botclass}/new-auth.users" + + what = params[:things] + + has_from = what[-2] == "from" + if has_from + importfile = "#{@bot.botclass}/#{what[-1]}" + what.slice!(-2,2) + end + + what.delete("all") + + m.reply _("reading %{file} ...") % {:file=>importfile} + begin + yaml_hash = YAML::load_file(importfile) + rescue => e + m.reply _("failed to import from: %{exception}") % {:exception=>e} + debug e.backtrace.dup.unshift(e.inspect).join("\n") + return + end + + # m.reply yaml_hash.inspect + + m.reply _("selecting data to import ...") + + if what.empty? + we_want = yaml_hash + else + we_want = yaml_hash.delete_if { |key, val| + not what.include?(key) + } + end + + m.reply _("parsing data from import ...") + + buser_hash = {} + + begin + yaml_hash.each { |k, val| + buser_hash[k] = { :username => k } + val.each { |kk, v| + case kk + when :netmasks + buser_hash[k][kk] = [] + v.each { |nm| + buser_hash[k][kk] << nm[:fullform].to_irc_netmask(:casemap => nm[:casemap].to_irc_casemap).to_irc_netmask(:server => @bot.server) + } + else + buser_hash[k][kk] = v + end + } + } + rescue => e + m.reply _("failed to parse data: %{exception}") % {:exception=>e} + debug e.backtrace.dup.unshift(e.inspect).join("\n") + return + end + + # m.reply buser_hash.inspect + + org_buser_array = @bot.auth.save_array + org_buser_hash = org_buser_array.inject({}) { |h, u| + h[u[:username]] = u + h + } + + # TODO we may want to do a(n optional) key-by-key merge + # + org_buser_hash.merge!(buser_hash) + new_buser_array = org_buser_hash.values + @bot.auth.load_array(new_buser_array, true) + @bot.auth.set_changed + + m.reply _("done") + end + +end + +auth = AuthModule.new + +auth.map "user export *things", + :action => 'auth_export', + :defaults => { :things => ['all'] }, + :auth_path => ':manage:fedex:' + +auth.map "user import *things", + :action => 'auth_import', + :auth_path => ':manage:fedex:' + +auth.map "user create :name :password", + :action => 'auth_create_user', + :defaults => {:password => nil}, + :auth_path => ':manage:' + +auth.map "user [:modifier] destroy :name", + :action => 'auth_destroy_user', + :requirements => { :modifier => /^(cancel|confirm)?$/ }, + :defaults => { :modifier => '' }, + :auth_path => ':manage::destroy!' + +auth.map "user copy :source [to] :dest", + :action => 'auth_copy_ren_user', + :auth_path => ':manage:' + +auth.map "user rename :source [to] :dest", + :action => 'auth_copy_ren_user', + :auth_path => ':manage:' + +auth.map "meet :nick [as :user]", + :action => 'auth_meet', + :auth_path => 'user::manage', :private => false + +auth.map "hello", + :action => 'auth_meet', + :auth_path => 'user::manage::meet' + +auth.default_auth("user::manage", false) +auth.default_auth("user::manage::meet::hello", true) + +auth.map "user tell :user the password for :botuser", + :action => 'auth_tell_password', + :auth_path => ':manage:' + +auth.map "user list", + :action => 'auth_list_users', + :auth_path => '::' + +auth.map "user *data", + :action => 'auth_manage_user' + +auth.default_auth("user", true) +auth.default_auth("edit::other", false) + +auth.map "whoami", + :action => 'auth_whoami', + :auth_path => '!*!' + +auth.map "who is :user", + :action => 'auth_whois', + :auth_path => '!*!' + +auth.map "auth :password", + :action => 'auth_auth', + :public => false, + :auth_path => '!login!' + +auth.map "login :botuser :password", + :action => 'auth_login', + :public => false, + :defaults => { :password => nil }, + :auth_path => '!login!' + +auth.map "login :botuser", + :action => 'auth_login', + :auth_path => '!login!' + +auth.map "login", + :action => 'auth_autologin', + :auth_path => '!login!' + +auth.map "permissions set *args", + :action => 'auth_edit_perm', + :auth_path => ':edit::set:' + +auth.map "permissions reset *args", + :action => 'auth_edit_perm', + :auth_path => ':edit::set:' + +auth.map "permissions view [for :user]", + :action => 'auth_view_perm', + :auth_path => '::' + +auth.map "permissions search *pattern", + :action => 'auth_search_perm', + :auth_path => '::' + +auth.default_auth('*', false) + diff --git a/lib/rbot/core/basics.rb b/lib/rbot/core/basics.rb index 4b5ab7d3..7a5d82c1 100644 --- a/lib/rbot/core/basics.rb +++ b/lib/rbot/core/basics.rb @@ -1,188 +1,188 @@ -#-- vim:sw=2:et
-#++
-#
-# :title: rbot basic management from IRC
-#
-# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2006,2007 Giuseppe Bilotta
-# License:: GPL v2
-
-class BasicsModule < CoreBotModule
-
- def ctcp_listen(m)
- who = m.private? ? "me" : m.target
- case m.ctcp.intern
- when :PING
- m.ctcp_reply m.message
- @bot.irclog "@ #{m.source} pinged #{who}"
- when :TIME
- m.ctcp_reply Time.now.to_s
- @bot.irclog "@ #{m.source} asked #{who} what time it is"
- end
- end
-
- def bot_join(m, param)
- if param[:pass]
- @bot.join param[:chan], param[:pass]
- else
- @bot.join param[:chan]
- end
- end
-
- def bot_part(m, param)
- if param[:chan]
- @bot.part param[:chan]
- else
- @bot.part m.target if m.public?
- end
- end
-
- def bot_quit(m, param)
- @bot.quit param[:msg].to_s
- end
-
- def bot_restart(m, param)
- @bot.restart param[:msg].to_s
- end
-
- def bot_hide(m, param)
- @bot.join 0
- end
-
- def bot_say(m, param)
- @bot.say param[:where], param[:what].to_s
- end
-
- def bot_action(m, param)
- @bot.action param[:where], param[:what].to_s
- end
-
- def bot_mode(m, param)
- @bot.mode param[:where], param[:what], param[:who].join(" ")
- end
-
- def bot_ping(m, param)
- m.reply "pong"
- end
-
- def bot_quiet(m, param)
- if param.has_key?(:where)
- @bot.set_quiet param[:where].sub(/^here$/, m.target.downcase)
- else
- @bot.set_quiet
- end
- # Make sense when the commmand is given in private or in a non-quieted
- # channel
- m.okay
- end
-
- def bot_talk(m, param)
- if param.has_key?(:where)
- @bot.reset_quiet param[:where].sub(/^here$/, m.target.downcase)
- else
- @bot.reset_quiet
- end
- # Make sense when the commmand is given in private or in a non-quieted
- # channel
- m.okay
- end
-
- def bot_help(m, param)
- m.reply @bot.help(param[:topic].join(" "))
- end
-
- #TODO move these to a "chatback" plugin
- # when (/^(botsnack|ciggie)$/i)
- # @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
- # @bot.say m.replyto, @lang.get("thanks") if(m.private?)
- # when (/^#{Regexp.escape(@bot.nick)}!*$/)
- # @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick
-
- # handle help requests for "core" topics
- def help(cmd, topic="")
- case cmd
- when "quit"
- _("quit [<message>] => quit IRC with message <message>")
- when "restart"
- _("restart => completely stop and restart the bot (including reconnect)")
- when "join"
- _("join <channel> [<key>] => join channel <channel> with secret key <key> if specified. #{@bot.myself} also responds to invites if you have the required access level")
- when "part"
- _("part <channel> => part channel <channel>")
- when "hide"
- _("hide => part all channels")
- when "nick"
- _("nick <nick> => attempt to change nick to <nick>")
- when "say"
- _("say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>")
- when "action"
- _("action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>")
- when "quiet"
- _("quiet [in here|<channel>] => with no arguments, stop speaking in all channels, if \"in here\", stop speaking in this channel, or stop speaking in <channel>")
- when "talk"
- _("talk [in here|<channel>] => with no arguments, resume speaking in all channels, if \"in here\", resume speaking in this channel, or resume speaking in <channel>")
- when "ping"
- _("ping => replies with a pong")
- when "mode"
- _("mode <channel> <mode> <nicks> => set channel modes for <nicks> on <channel> to <mode>")
- # when "botsnack"
- # return "botsnack => reward #{@bot.myself} for being good"
- # when "hello"
- # return "hello|hi|hey|yo [#{@bot.myself}] => greet the bot"
- else
- _("%{name}: quit, restart, join, part, hide, save, nick, say, action, topic, quiet, talk, ping, mode") % {:name=>name}
- #, botsnack, hello
- end
- end
-end
-
-basics = BasicsModule.new
-
-basics.map "quit *msg",
- :action => 'bot_quit',
- :defaults => { :msg => nil },
- :auth_path => 'quit'
-basics.map "restart *msg",
- :action => 'bot_restart',
- :defaults => { :msg => nil },
- :auth_path => 'quit'
-
-basics.map "quiet [in] [:where]",
- :action => 'bot_quiet',
- :auth_path => 'talk::set'
-basics.map "talk [in] [:where]",
- :action => 'bot_talk',
- :auth_path => 'talk::set'
-
-basics.map "say :where *what",
- :action => 'bot_say',
- :auth_path => 'talk::do'
-basics.map "action :where *what",
- :action => 'bot_action',
- :auth_path => 'talk::do'
-basics.map "mode :where :what *who",
- :action => 'bot_mode',
- :auth_path => 'talk::do'
-
-basics.map "join :chan :pass",
- :action => 'bot_join',
- :defaults => {:pass => nil},
- :auth_path => 'move'
-basics.map "part :chan",
- :action => 'bot_part',
- :defaults => {:chan => nil},
- :auth_path => 'move'
-basics.map "hide",
- :action => 'bot_hide',
- :auth_path => 'move'
-
-basics.map "ping",
- :action => 'bot_ping',
- :auth_path => '!ping!'
-basics.map "help *topic",
- :action => 'bot_help',
- :defaults => { :topic => [""] },
- :auth_path => '!help!'
-
-basics.default_auth('*', false)
-
+#-- vim:sw=2:et +#++ +# +# :title: rbot basic management from IRC +# +# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com> +# Copyright:: (C) 2006,2007 Giuseppe Bilotta +# License:: GPL v2 + +class BasicsModule < CoreBotModule + + def ctcp_listen(m) + who = m.private? ? "me" : m.target + case m.ctcp.intern + when :PING + m.ctcp_reply m.message + @bot.irclog "@ #{m.source} pinged #{who}" + when :TIME + m.ctcp_reply Time.now.to_s + @bot.irclog "@ #{m.source} asked #{who} what time it is" + end + end + + def bot_join(m, param) + if param[:pass] + @bot.join param[:chan], param[:pass] + else + @bot.join param[:chan] + end + end + + def bot_part(m, param) + if param[:chan] + @bot.part param[:chan] + else + @bot.part m.target if m.public? + end + end + + def bot_quit(m, param) + @bot.quit param[:msg].to_s + end + + def bot_restart(m, param) + @bot.restart param[:msg].to_s + end + + def bot_hide(m, param) + @bot.join 0 + end + + def bot_say(m, param) + @bot.say param[:where], param[:what].to_s + end + + def bot_action(m, param) + @bot.action param[:where], param[:what].to_s + end + + def bot_mode(m, param) + @bot.mode param[:where], param[:what], param[:who].join(" ") + end + + def bot_ping(m, param) + m.reply "pong" + end + + def bot_quiet(m, param) + if param.has_key?(:where) + @bot.set_quiet param[:where].sub(/^here$/, m.target.downcase) + else + @bot.set_quiet + end + # Make sense when the commmand is given in private or in a non-quieted + # channel + m.okay + end + + def bot_talk(m, param) + if param.has_key?(:where) + @bot.reset_quiet param[:where].sub(/^here$/, m.target.downcase) + else + @bot.reset_quiet + end + # Make sense when the commmand is given in private or in a non-quieted + # channel + m.okay + end + + def bot_help(m, param) + m.reply @bot.help(param[:topic].join(" ")) + end + + #TODO move these to a "chatback" plugin + # when (/^(botsnack|ciggie)$/i) + # @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?) + # @bot.say m.replyto, @lang.get("thanks") if(m.private?) + # when (/^#{Regexp.escape(@bot.nick)}!*$/) + # @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick + + # handle help requests for "core" topics + def help(cmd, topic="") + case cmd + when "quit" + _("quit [<message>] => quit IRC with message <message>") + when "restart" + _("restart => completely stop and restart the bot (including reconnect)") + when "join" + _("join <channel> [<key>] => join channel <channel> with secret key <key> if specified. #{@bot.myself} also responds to invites if you have the required access level") + when "part" + _("part <channel> => part channel <channel>") + when "hide" + _("hide => part all channels") + when "nick" + _("nick <nick> => attempt to change nick to <nick>") + when "say" + _("say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>") + when "action" + _("action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>") + when "quiet" + _("quiet [in here|<channel>] => with no arguments, stop speaking in all channels, if \"in here\", stop speaking in this channel, or stop speaking in <channel>") + when "talk" + _("talk [in here|<channel>] => with no arguments, resume speaking in all channels, if \"in here\", resume speaking in this channel, or resume speaking in <channel>") + when "ping" + _("ping => replies with a pong") + when "mode" + _("mode <channel> <mode> <nicks> => set channel modes for <nicks> on <channel> to <mode>") + # when "botsnack" + # return "botsnack => reward #{@bot.myself} for being good" + # when "hello" + # return "hello|hi|hey|yo [#{@bot.myself}] => greet the bot" + else + _("%{name}: quit, restart, join, part, hide, save, nick, say, action, topic, quiet, talk, ping, mode") % {:name=>name} + #, botsnack, hello + end + end +end + +basics = BasicsModule.new + +basics.map "quit *msg", + :action => 'bot_quit', + :defaults => { :msg => nil }, + :auth_path => 'quit' +basics.map "restart *msg", + :action => 'bot_restart', + :defaults => { :msg => nil }, + :auth_path => 'quit' + +basics.map "quiet [in] [:where]", + :action => 'bot_quiet', + :auth_path => 'talk::set' +basics.map "talk [in] [:where]", + :action => 'bot_talk', + :auth_path => 'talk::set' + +basics.map "say :where *what", + :action => 'bot_say', + :auth_path => 'talk::do' +basics.map "action :where *what", + :action => 'bot_action', + :auth_path => 'talk::do' +basics.map "mode :where :what *who", + :action => 'bot_mode', + :auth_path => 'talk::do' + +basics.map "join :chan :pass", + :action => 'bot_join', + :defaults => {:pass => nil}, + :auth_path => 'move' +basics.map "part :chan", + :action => 'bot_part', + :defaults => {:chan => nil}, + :auth_path => 'move' +basics.map "hide", + :action => 'bot_hide', + :auth_path => 'move' + +basics.map "ping", + :action => 'bot_ping', + :auth_path => '!ping!' +basics.map "help *topic", + :action => 'bot_help', + :defaults => { :topic => [""] }, + :auth_path => '!help!' + +basics.default_auth('*', false) + diff --git a/lib/rbot/core/config.rb b/lib/rbot/core/config.rb index ad9b7c74..1b14ebd8 100644 --- a/lib/rbot/core/config.rb +++ b/lib/rbot/core/config.rb @@ -1,325 +1,325 @@ -#-- vim:sw=2:et
-#++
-#
-# :title: rbot config management from IRC
-#
-# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2006,2007 Giuseppe Bilotta
-# License:: GPL v2
-
-class ConfigModule < CoreBotModule
-
- def version_string
- _("I'm a v. %{version} rubybot%{copyright}%{url}") % {
- :version => $version,
- :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}",
- :url => " - #{Irc::Bot::SOURCE_URL}"
- }
- end
-
- 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 %{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
-
- 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
-
- 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
-
- def handle_search(m, params)
- rx = Regexp.new(params[:rx].to_s, true)
- cfs = []
- @bot.config.items.each do |k, v|
- cfs << v 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.key}.join(', ') }
- m.reply cfs.map { |c| [c.key, c.desc].join(': ') }.join("\n")
- end
- 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}") % {: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}") % {: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
-
- 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}") % {: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)
- 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}
- 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}") % {: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)
- 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}
- 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. %{plugin_status}") % {:plugin_status => @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 version_string
- end
-
- def ctcp_listen(m)
- who = m.private? ? "me" : m.target
- case m.ctcp.intern
- when :VERSION
- m.ctcp_reply version_string
- @bot.irclog "@ #{m.source} asked #{who} about version info"
- when :SOURCE
- m.ctcp_reply Irc::Bot::SOURCE_URL
- @bot.irclog "@ #{m.source} asked #{who} about source info"
- end
- end
-
- def handle_help(m, params)
- m.reply help(params[:topic])
- end
-
- 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 <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
- 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
-
-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 Bot::Config 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::desc!'
-conf.map 'config search *rx',
- :action => 'handle_search',
- :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', true)
-conf.default_auth('show::get', false)
-# TODO these shouldn't be set here, we need a way to let the default
-# permission be specified together with the ConfigValue
-conf.default_auth('key', true)
-conf.default_auth('key::auth::password', false)
-
+#-- vim:sw=2:et +#++ +# +# :title: rbot config management from IRC +# +# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com> +# Copyright:: (C) 2006,2007 Giuseppe Bilotta +# License:: GPL v2 + +class ConfigModule < CoreBotModule + + def version_string + _("I'm a v. %{version} rubybot%{copyright}%{url}") % { + :version => $version, + :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}", + :url => " - #{Irc::Bot::SOURCE_URL}" + } + end + + 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 %{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 + + 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 + + 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 + + def handle_search(m, params) + rx = Regexp.new(params[:rx].to_s, true) + cfs = [] + @bot.config.items.each do |k, v| + cfs << v 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.key}.join(', ') } + m.reply cfs.map { |c| [c.key, c.desc].join(': ') }.join("\n") + end + 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}") % {: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}") % {: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 + + 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}") % {: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) + 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} + 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}") % {: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) + 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} + 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. %{plugin_status}") % {:plugin_status => @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 version_string + end + + def ctcp_listen(m) + who = m.private? ? "me" : m.target + case m.ctcp.intern + when :VERSION + m.ctcp_reply version_string + @bot.irclog "@ #{m.source} asked #{who} about version info" + when :SOURCE + m.ctcp_reply Irc::Bot::SOURCE_URL + @bot.irclog "@ #{m.source} asked #{who} about source info" + end + end + + def handle_help(m, params) + m.reply help(params[:topic]) + end + + 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 <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 + 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 + +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 Bot::Config 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::desc!' +conf.map 'config search *rx', + :action => 'handle_search', + :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', true) +conf.default_auth('show::get', false) +# TODO these shouldn't be set here, we need a way to let the default +# permission be specified together with the ConfigValue +conf.default_auth('key', true) +conf.default_auth('key::auth::password', false) + |