]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/auth.rb
Ruby 1.9 can intern empty strings
[user/henk/code/ruby/rbot.git] / lib / rbot / core / auth.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: rbot auth management from IRC
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7
8 class AuthModule < CoreBotModule
9
10   def initialize
11     super
12
13     # The namespace migration causes each Irc::Auth::PermissionSet to be
14     # unrecoverable, and we have to rename their class name to
15     # Irc::Bot::Auth::PermissionSet
16     @registry.recovery = Proc.new { |val|
17       patched = val.sub("o:\035Irc::Auth::PermissionSet", "o:\042Irc::Bot::Auth::PermissionSet")
18       Marshal.restore(patched)
19     }
20
21     load_array(:default, true)
22     debug "initialized auth. Botusers: #{@bot.auth.save_array.pretty_inspect}"
23   end
24
25   def save
26     save_array
27   end
28
29   def save_array(key=:default)
30     if @bot.auth.changed?
31       @registry[key] = @bot.auth.save_array
32       @bot.auth.reset_changed
33       debug "saved botusers (#{key}): #{@registry[key].pretty_inspect}"
34     end
35   end
36
37   def load_array(key=:default, forced=false)
38     debug "loading botusers (#{key}): #{@registry[key].pretty_inspect}"
39     @bot.auth.load_array(@registry[key], forced) if @registry.has_key?(key)
40     if @bot.auth.botowner.password != @bot.config['auth.password']
41       error "Master password is out of sync!"
42       debug "  db password: #{@bot.auth.botowner.password}"
43       debug "conf password: #{@bot.config['auth.password']}"
44       error "Using conf password"
45       @bot.auth.botowner.password = @bot.config['auth.password']
46     end
47   end
48
49   # The permission parameters accept arguments with the following syntax:
50   #   cmd_path... [on #chan .... | in here | in private]
51   # This auxiliary method scans the array _ar_ to see if it matches
52   # the given syntax: it expects + or - signs in front of _cmd_path_
53   # elements when _setting_ = true
54   #
55   # It returns an array whose first element is the array of cmd_path,
56   # the second element is an array of locations and third an array of
57   # warnings occurred while parsing the strings
58   #
59   def parse_args(ar, setting)
60     cmds = []
61     locs = []
62     warns = []
63     doing_cmds = true
64     next_must_be_chan = false
65     want_more = false
66     last_idx = 0
67     ar.each_with_index { |x, i|
68       if doing_cmds # parse cmd_path
69         # check if the list is done
70         if x == "on" or x == "in"
71           doing_cmds = false
72           next_must_be_chan = true if x == "on"
73           next
74         end
75         if "+-".include?(x[0])
76           warns << ArgumentError.new(_("please do not use + or - in front of command %{command} when resetting") % {:command => x}) unless setting
77         else
78           warns << ArgumentError.new(_("+ or - expected in front of %{string}") % {:string => x}) if setting
79         end
80         cmds << x
81       else # parse locations
82         if x[-1].chr == ','
83           want_more = true
84         else
85           want_more = false
86         end
87         case next_must_be_chan
88         when false
89           locs << x.gsub(/^here$/,'_').gsub(/^private$/,'?')
90         else
91           warns << ArgumentError.new(_("'%{string}' doesn't look like a channel name") % {:string => x}) unless @bot.server.supports[:chantypes].include?(x[0])
92           locs << x
93         end
94         unless want_more
95           last_idx = i
96           break
97         end
98       end
99     }
100     warns << _("trailing comma") if want_more
101     warns << _("you probably forgot a comma") unless last_idx == ar.length - 1
102     return cmds, locs, warns
103   end
104
105   def auth_edit_perm(m, params)
106
107     setting = m.message.split[1] == "set"
108     splits = params[:args]
109
110     has_for = splits[-2] == "for"
111     return usage(m) unless has_for
112
113     begin
114       user = @bot.auth.get_botuser(splits[-1].sub(/^all$/,"everyone"))
115     rescue
116       return m.reply(_("couldn't find botuser %{name}") % {:name => splits[-1]})
117     end
118     return m.reply(_("you can't change permissions for %{username}") % {:username => user.username}) if user.owner?
119     splits.slice!(-2,2) if has_for
120
121     cmds, locs, warns = parse_args(splits, setting)
122     errs = warns.select { |w| w.kind_of?(Exception) }
123
124     unless errs.empty?
125       m.reply _("couldn't satisfy your request: %{errors}") % {:errors => errs.join(',')}
126       return
127     end
128
129     if locs.empty?
130       locs << "*"
131     end
132     begin
133       locs.each { |loc|
134         ch = loc
135         if m.private?
136           ch = "?" if loc == "_"
137         else
138           ch = m.target.to_s if loc == "_"
139         end
140         cmds.each { |setval|
141           if setting
142             val = setval[0].chr == '+'
143             cmd = setval[1..-1]
144             user.set_permission(cmd, val, ch)
145           else
146             cmd = setval
147             user.reset_permission(cmd, ch)
148           end
149         }
150       }
151     rescue => e
152       m.reply "something went wrong while trying to set the permissions"
153       raise
154     end
155     @bot.auth.set_changed
156     debug "user #{user} permissions changed"
157     m.okay
158   end
159
160   def auth_view_perm(m, params)
161     begin
162       if params[:user].nil?
163         user = get_botuser_for(m.source)
164         return m.reply(_("you are owner, you can do anything")) if user.owner?
165       else
166         user = @bot.auth.get_botuser(params[:user].sub(/^all$/,"everyone"))
167         return m.reply(_("owner can do anything")) if user.owner?
168       end
169     rescue
170       return m.reply(_("couldn't find botuser %{name}") % {:name => params[:user]})
171     end
172     perm = user.perm
173     str = []
174     perm.each { |k, val|
175       next if val.perm.empty?
176       case k
177       when :*
178         str << _("on any channel: ").dup
179       when :"?"
180         str << _("in private: ").dup
181       else
182         str << _("on #{k}: ").dup
183       end
184       sub = []
185       val.perm.each { |cmd, bool|
186         sub << (bool ? "+" : "-")
187         sub.last << cmd.to_s
188       }
189       str.last << sub.join(', ')
190     }
191     if str.empty?
192       m.reply _("no permissions set for %{user}") % {:user => user.username}
193     else
194       m.reply _("permissions for %{user}:: %{permissions}") %
195               { :user => user.username, :permissions => str.join('; ')}
196     end
197   end
198
199   def auth_search_perm(m, p)
200     pattern = Regexp.new(p[:pattern].to_s)
201     results = @bot.plugins.maps.select { |k, v| k.match(pattern) }
202     count = results.length
203     max = @bot.config['send.max_lines']
204     extra = (count > max ? _(". only %{max} will be shown") : "") % { :max => max }
205     m.reply _("%{count} commands found matching %{pattern}%{extra}") % {
206       :count => count, :pattern => pattern, :extra => extra
207     }
208     return if count == 0
209     results[0,max].each { |cmd, hash|
210       m.reply _("%{cmd}: %{perms}") % {
211         :cmd => cmd,
212         :perms => hash[:auth].join(", ")
213       }
214     }
215   end
216
217   def find_auth(pseudo)
218     k = pseudo.plugin.intern
219     cmds = @bot.plugins.commands
220     auth = nil
221     if cmds.has_key?(k)
222       cmds[k][:botmodule].handler.each do |tmpl|
223         options = tmpl.recognize(pseudo)
224         next if options.kind_of? MessageMapper::Failure
225         auth = tmpl.options[:full_auth_path]
226         break
227       end
228     end
229     return auth
230   end
231
232   def auth_allow_deny(m, p)
233     begin
234       botuser = @bot.auth.get_botuser(p[:user].sub(/^all$/,"everyone"))
235     rescue
236       return m.reply(_("couldn't find botuser %{name}") % {:name => p[:user]})
237     end
238
239     if p[:where].to_s.empty?
240       where = :*
241     else
242       where = m.parse_channel_list(p[:where].to_s).first # should only be one anyway
243     end
244
245     if p.has_key? :auth_path
246       auth_path = p[:auth_path]
247     else
248       # pseudo-message to find the template. The source is ignored, and the
249       # target is set according to where the template should be checked
250       # (public or private)
251       # This might still fail in the case of 'everywhere' for commands there are
252       # really only private
253       case where
254       when :"?"
255         pseudo_target = @bot.myself
256       when :*
257         pseudo_target = m.channel
258       else
259         pseudo_target = m.server.channel(where)
260       end
261
262       pseudo = PrivMessage.new(bot, m.server, m.source, pseudo_target, p[:stuff].to_s)
263
264       auth_path = find_auth(pseudo)
265     end
266     debug auth_path
267
268     if auth_path
269       allow = p[:allow]
270       if @bot.auth.permit?(botuser, auth_path, where)
271         return m.reply(_("%{user} can already do that") % {:user => botuser}) if allow
272       else
273         return m.reply(_("%{user} can't do that already") % {:user => botuser}) if !allow
274       end
275       cmd = PrivMessage.new(bot, m.server, m.source, m.target, "permissions set %{sign}%{path} %{where} for %{user}" % {
276         :path => auth_path,
277         :user => p[:user],
278         :sign => (allow ? '+' : '-'),
279         :where => p[:where].to_s
280       })
281       handle(cmd)
282     else
283       m.reply(_("sorry, %{cmd} doesn't look like a valid command. maybe you misspelled it, or you need to specify it should be in private?") % {
284         :cmd => p[:stuff].to_s
285       })
286     end
287   end
288
289   def auth_allow(m, p)
290     auth_allow_deny(m, p.merge(:allow => true))
291   end
292
293   def auth_deny(m, p)
294     auth_allow_deny(m, p.merge(:allow => false))
295   end
296
297   def get_botuser_for(user)
298     @bot.auth.irc_to_botuser(user)
299   end
300
301   def get_botusername_for(user)
302     get_botuser_for(user).username
303   end
304
305   def say_welcome(m)
306     m.reply _("welcome, %{user}") % {:user => get_botusername_for(m.source)}
307   end
308
309   def auth_auth(m, params)
310     params[:botuser] = 'owner'
311     auth_login(m,params)
312   end
313
314   def auth_login(m, params)
315     begin
316       case @bot.auth.login(m.source, params[:botuser], params[:password])
317       when true
318         say_welcome(m)
319         @bot.auth.set_changed
320       else
321         m.reply _("sorry, can't do")
322       end
323     rescue => e
324       m.reply _("couldn't login: %{exception}") % {:exception => e}
325       raise
326     end
327   end
328
329   def auth_autologin(m, params)
330     u = do_autologin(m.source)
331     if u.default?
332       m.reply _("I couldn't find anything to let you login automatically")
333     else
334       say_welcome(m)
335     end
336   end
337
338   def do_autologin(user)
339     @bot.auth.autologin(user)
340   end
341
342   def auth_whoami(m, params)
343     m.reply _("you are %{who}") % {
344       :who => get_botusername_for(m.source).gsub(
345                 /^everyone$/, _("no one that I know")).gsub(
346                 /^owner$/, _("my boss"))
347     }
348   end
349
350   def auth_whois(m, params)
351     return auth_whoami(m, params) if !m.public?
352     u = m.channel.users[params[:user]]
353
354     return m.reply("I don't see anyone named '#{params[:user]}' here") unless u
355
356     m.reply _("#{params[:user]} is %{who}") % {
357       :who => get_botusername_for(u).gsub(
358                 /^everyone$/, _("no one that I know")).gsub(
359                 /^owner$/, _("my boss"))
360     }
361   end
362
363   def help(cmd, topic="")
364     case cmd
365     when "login"
366       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")
367     when "whoami"
368       return _("whoami: names the botuser you're linked to")
369     when "who"
370       return _("who is <user>: names the botuser <user> is linked to")
371     when /^permission/
372       case topic
373       when "syntax"
374         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")
375       when "set", "reset", "[re]set", "(re)set"
376         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)")
377       when "view"
378         return _("permissions view [for <user>]: display the permissions for user <user>")
379       when "search"
380         return _("permissions search <pattern>: display the permissions associated with the commands matching <pattern>")
381       else
382         return _("permission topics: syntax, (re)set, view, search")
383       end
384     when "user"
385       case topic
386       when "show"
387         return _("user show <what> : shows info about the user; <what> can be any of autologin, login-by-mask, netmasks")
388       when /^(en|dis)able/
389         return _("user enable|disable <what> : turns on or off <what> (autologin, login-by-mask)")
390       when "set"
391         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")
392       when "add", "rm"
393         return _("user add|rm netmask <mask> : adds/removes netmask <mask> from the list of netmasks known to the botuser you're linked to")
394       when "reset"
395         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)")
396       when "tell"
397         return _("user tell <who> the password for <botuser> : contacts <who> in private to tell him/her the password for <botuser>")
398       when "create"
399         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 (_)")
400       when "list"
401         return _("user list : lists all the botusers")
402       when "destroy"
403         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}
404       when "export"
405         return _("user export [to <filename>]: exports user data to file <filename> (default: new-auth.users)")
406       when "import"
407         return _("user import [from <filename>]: import user data from file <filename> (default: new-auth.users)")
408       else
409         return _("user topics: show, enable|disable, add|rm netmask, set, reset, tell, create, list, destroy, import, export")
410       end
411     when "auth"
412       return _("auth <masterpassword>: log in as the bot owner; other commands: login, whoami, permissions syntax, permissions [re]set, permissions view, user, meet, hello, allow, deny")
413     when "meet"
414       return _("meet <nick> [as <user>]: creates a bot user for nick, calling it user (defaults to the nick itself)")
415     when "hello"
416       return _("hello: creates a bot user for the person issuing the command")
417     when "allow"
418       return [
419         _("allow <user> to do <sample command> [<where>]: gives botuser <user> the permissions to execute a command such as the provided sample command"),
420         _("(in private or in channel, according to the optional <where>)."),
421         _("<sample command> should be a full command, not just the command keyword --"),
422         _("correct: allow user to do addquote stuff --"),
423         _("wrong: allow user to do addquote.")
424       ].join(" ")
425     when "deny"
426       return [
427         _("deny <user> from doing <sample command> [<where>]: removes from botuser <user> the permissions to execute a command such as the provided sample command"),
428         _("(in private or in channel, according to the optional <where>)."),
429         _("<sample command> should be a full command, not just the command keyword --"),
430         _("correct: deny user from doing addquote stuff --"),
431         _("wrong: deny user from doing addquote.")
432       ].join(" ")
433     else
434       return _("auth commands: auth, login, whoami, who, permission[s], user, meet, hello, allow, deny")
435     end
436   end
437
438   def need_args(cmd)
439     _("sorry, I need more arguments to %{command}") % {:command => cmd}
440   end
441
442   def not_args(cmd, *stuff)
443     _("I can only %{command} these: %{arguments}") %
444       {:command => cmd, :arguments => stuff.join(', ')}
445   end
446
447   def set_prop(botuser, prop, val)
448     k = prop.to_s.gsub("-","_")
449     botuser.send( (k + "=").to_sym, val)
450     if prop == :password and botuser == @bot.auth.botowner
451       @bot.config.items[:'auth.password'].set_string(@bot.auth.botowner.password)
452     end
453   end
454
455   def reset_prop(botuser, prop)
456     k = prop.to_s.gsub("-","_")
457     botuser.send( ("reset_"+k).to_sym)
458   end
459
460   def ask_bool_prop(botuser, prop)
461     k = prop.to_s.gsub("-","_")
462     botuser.send( (k + "?").to_sym)
463   end
464
465   def auth_manage_user(m, params)
466     splits = params[:data]
467
468     cmd = splits.first
469     return auth_whoami(m, params) if cmd.nil?
470
471     botuser = get_botuser_for(m.source)
472     # By default, we do stuff on the botuser the irc user is bound to
473     butarget = botuser
474
475     has_for = splits[-2] == "for"
476     if has_for
477       butarget = @bot.auth.get_botuser(splits[-1]) rescue nil
478       return m.reply(_("no such bot user %{user}") % {:user => splits[-1]}) unless butarget
479       splits.slice!(-2,2)
480     end
481     return m.reply(_("you can't mess with %{user}") % {:user => butarget.username}) if butarget.owner? && botuser != butarget
482
483     bools = [:autologin, :"login-by-mask"]
484     can_set = [:password]
485     can_addrm = [:netmasks]
486     can_reset = bools + can_set + can_addrm
487     can_show = can_reset + ["perms"]
488
489     begin
490     case cmd.to_sym
491
492     when :show
493       return m.reply(_("you can't see the properties of %{user}") %
494              {:user => butarget.username}) if botuser != butarget &&
495                                                !botuser.permit?("auth::show::other")
496
497       case splits[1]
498       when nil, "all"
499         props = can_reset
500       when "password"
501         if botuser != butarget
502           return m.reply(_("no way I'm telling you the master password!")) if butarget == @bot.auth.botowner
503           return m.reply(_("you can't ask for someone else's password"))
504         end
505         return m.reply(_("c'mon, you can't be asking me seriously to tell you the password in public!")) if m.public?
506         return m.reply(_("the password for %{user} is %{password}") %
507           { :user => butarget.username, :password => butarget.password })
508       else
509         props = splits[1..-1]
510       end
511
512       str = []
513
514       props.each { |arg|
515         k = arg.to_sym
516         next if k == :password
517         case k
518         when *bools
519           if ask_bool_prop(butarget, k)
520             str << _("can %{action}") % {:action => k}
521           else
522             str << _("can not %{action}") % {:action => k}
523           end
524         when :netmasks
525           if butarget.netmasks.empty?
526             str << _("knows no netmasks")
527           else
528             str << _("knows %{netmasks}") % {:netmasks => butarget.netmasks.join(", ")}
529           end
530         end
531       }
532       return m.reply("#{butarget.username} #{str.join('; ')}")
533
534     when :enable, :disable
535       return m.reply(_("you can't change the default user")) if butarget.default? && !botuser.permit?("auth::edit::other::default")
536       return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if butarget != botuser && !botuser.permit?("auth::edit::other")
537
538       return m.reply(need_args(cmd)) unless splits[1]
539       things = []
540       skipped = []
541       splits[1..-1].each { |a|
542         arg = a.to_sym
543         if bools.include?(arg)
544           set_prop(butarget, arg, cmd.to_sym == :enable)
545           things << a
546         else
547           skipped << a
548         end
549       }
550
551       m.reply(_("I ignored %{things} because %{reason}") % {
552                 :things => skipped.join(', '),
553                 :reason => not_args(cmd, *bools)}) unless skipped.empty?
554       if things.empty?
555         m.reply _("I haven't changed anything")
556       else
557         @bot.auth.set_changed
558         return auth_manage_user(m, {:data => ["show"] + things + ["for", butarget.username] })
559       end
560
561     when :set
562       return m.reply(_("you can't change the default user")) if
563              butarget.default? && !botuser.permit?("auth::edit::default")
564       return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if
565              butarget != botuser && !botuser.permit?("auth::edit::other")
566
567       return m.reply(need_args(cmd)) unless splits[1]
568       arg = splits[1].to_sym
569       return m.reply(not_args(cmd, *can_set)) unless can_set.include?(arg)
570       argarg = splits[2]
571       return m.reply(need_args([cmd, splits[1]].join(" "))) unless argarg
572       if arg == :password && m.public?
573         return m.reply(_("is that a joke? setting the password in public?"))
574       end
575       set_prop(butarget, arg, argarg)
576       @bot.auth.set_changed
577       auth_manage_user(m, {:data => ["show", arg.to_s, "for", butarget.username] })
578
579     when :reset
580       return m.reply(_("you can't change the default user")) if
581              butarget.default? && !botuser.permit?("auth::edit::default")
582       return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if
583              butarget != botuser && !botuser.permit?("auth::edit::other")
584
585       return m.reply(need_args(cmd)) unless splits[1]
586       things = []
587       skipped = []
588       splits[1..-1].each { |a|
589         arg = a.to_sym
590         if can_reset.include?(arg)
591           reset_prop(butarget, arg)
592           things << a
593         else
594           skipped << a
595         end
596       }
597
598       m.reply(_("I ignored %{things} because %{reason}") %
599                 { :things => skipped.join(', '),
600                   :reason => not_args(cmd, *can_reset)}) unless skipped.empty?
601       if things.empty?
602         m.reply _("I haven't changed anything")
603       else
604         @bot.auth.set_changed
605         @bot.say(m.source, _("the password for %{user} is now %{password}") %
606           {:user => butarget.username, :password => butarget.password}) if
607           things.include?("password")
608         return auth_manage_user(m, {:data => (["show"] + things - ["password"]) + ["for", butarget.username]})
609       end
610
611     when :add, :rm, :remove, :del, :delete
612       return m.reply(_("you can't change the default user")) if
613              butarget.default? && !botuser.permit?("auth::edit::default")
614       return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if
615              butarget != botuser && !botuser.permit?("auth::edit::other")
616
617       arg = splits[1]
618       if arg.nil? or arg !~ /netmasks?/ or splits[2].nil?
619         return m.reply(_("I can only add/remove netmasks. See +help user add+ for more instructions"))
620       end
621
622       method = cmd.to_sym == :add ? :add_netmask : :delete_netmask
623
624       failed = []
625
626       splits[2..-1].each { |mask|
627         begin
628           butarget.send(method, mask.to_irc_netmask(:server => @bot.server))
629         rescue => e
630           debug "failed with #{e.message}"
631           debug e.backtrace.join("\n")
632           failed << mask
633         end
634       }
635       m.reply "I failed to #{cmd} #{failed.join(', ')}" unless failed.empty?
636       @bot.auth.set_changed
637       return auth_manage_user(m, {:data => ["show", "netmasks", "for", butarget.username] })
638
639     else
640       m.reply _("sorry, I don't know how to %{request}") % {:request => m.message}
641     end
642     rescue => e
643       m.reply _("couldn't %{cmd}: %{exception}") % {:cmd => cmd, :exception => e}
644     end
645   end
646
647   def auth_meet(m, params)
648     nick = params[:nick]
649     if !nick
650       # we are actually responding to a 'hello' command
651       unless m.botuser.transient?
652         m.reply @bot.lang.get('hello_X') % m.botuser, :nick => false
653         return
654       end
655       nick = m.sourcenick
656       irc_user = m.source
657     else
658       # m.channel is always an Irc::Channel because the command is either
659       # public-only 'meet' or private/public 'hello' which was handled by
660       # the !nick case, so this shouldn't fail
661       irc_user = m.channel.users[nick]
662       return m.reply("I don't see anyone named '#{nick}' here") unless irc_user
663     end
664     # BotUser name
665     buname = params[:user] || nick
666     begin
667       call_event(:botuser,:pre_perm, {:irc_user => irc_user, :bot_user => buname})
668       met = @bot.auth.make_permanent(irc_user, buname)
669       @bot.auth.set_changed
670       call_event(:botuser,:post_perm, {:irc_user => irc_user, :bot_user => buname})
671       m.reply @bot.lang.get('hello_X') % met, :nick => false
672       @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" % {
673         :buname => buname,
674         :pass => met.password
675       })
676     rescue RuntimeError
677       # or can this happen for other cases too?
678       # TODO autologin if forced
679       m.reply _("but I already know %{buname}" % {:buname => buname})
680     rescue => e
681       m.reply _("I had problems meeting %{nick}: %{e}" % { :nick => nick, :e => e })
682     end
683   end
684
685   def auth_tell_password(m, params)
686     user = params[:user]
687     begin
688       botuser = @bot.auth.get_botuser(params[:botuser])
689     rescue
690       return m.reply(_("couldn't find botuser %{user}") % {:user => params[:botuser]})
691     end
692     return m.reply(_("I'm not telling the master password to anyone, pal")) if botuser == @bot.auth.botowner
693     msg = _("the password for botuser %{user} is %{password}") %
694           {:user => botuser.username, :password => botuser.password}
695     @bot.say user, msg
696     @bot.say m.source, _("I told %{user} that %{message}") % {:user => user, :message => msg}
697   end
698
699   def auth_create_user(m, params)
700     name = params[:name]
701     password = params[:password]
702     return m.reply(_("are you nuts, creating a botuser with a publicly known password?")) if m.public? and not password.nil?
703     begin
704       bu = @bot.auth.create_botuser(name, password)
705       @bot.auth.set_changed
706     rescue => e
707       m.reply(_("failed to create %{user}: %{exception}") % {:user => name,  :exception => e})
708       debug e.inspect + "\n" + e.backtrace.join("\n")
709       return
710     end
711     m.reply(_("created botuser %{user}") % {:user => bu.username})
712   end
713
714   def auth_list_users(m, params)
715     # TODO name regexp to filter results
716     list = @bot.auth.save_array.inject([]) { |lst, x| ['everyone', 'owner'].include?(x[:username]) ? lst : lst << x[:username] }
717     if defined?(@destroy_q)
718       list.map! { |x|
719         @destroy_q.include?(x) ? x + _(" (queued for destruction)") : x
720       }
721     end
722     return m.reply(_("I have no botusers other than the default ones")) if list.empty?
723     return m.reply(n_("botuser: %{list}", "botusers: %{list}", list.length) %
724                    {:list => list.join(', ')})
725   end
726
727   def auth_destroy_user(m, params)
728     @destroy_q = [] unless defined?(@destroy_q)
729     buname = params[:name]
730     return m.reply(_("You can't destroy %{user}") % {:user => buname}) if
731            ["everyone", "owner"].include?(buname)
732     mod = params[:modifier].nil_or_empty? ? nil : params[:modifier].to_sym
733
734     buser_array = @bot.auth.save_array
735     buser_hash = buser_array.inject({}) { |h, u|
736       h[u[:username]] = u
737       h
738     }
739
740     return m.reply(_("no such botuser %{user}") % {:user=>buname}) unless
741            buser_hash.keys.include?(buname)
742
743     case mod
744     when :cancel
745       if @destroy_q.include?(buname)
746         @destroy_q.delete(buname)
747         m.reply(_("%{user} removed from the destruction queue") % {:user=>buname})
748       else
749         m.reply(_("%{user} was not queued for destruction") % {:user=>buname})
750       end
751       return
752     when nil
753       if @destroy_q.include?(buname)
754         return m.reply(_("%{user} already queued for destruction, use %{highlight}user confirm destroy %{user}%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold})
755       else
756         @destroy_q << buname
757         return m.reply(_("%{user} queued for destruction, use %{highlight}user confirm destroy %{user}%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold})
758       end
759     when :confirm
760       begin
761         return m.reply(_("%{user} is not queued for destruction yet") %
762                {:user=>buname}) unless @destroy_q.include?(buname)
763         buser_array.delete_if { |u|
764           u[:username] == buname
765         }
766         @destroy_q.delete(buname)
767         @bot.auth.load_array(buser_array, true)
768         @bot.auth.set_changed
769       rescue => e
770         return m.reply(_("failed: %{exception}") % {:exception => e})
771       end
772       return m.reply(_("botuser %{user} destroyed") % {:user => buname})
773     end
774   end
775
776   def auth_copy_ren_user(m, params)
777     source = Auth::BotUser.sanitize_username(params[:source])
778     dest = Auth::BotUser.sanitize_username(params[:dest])
779     return m.reply(_("please don't touch the default users")) unless
780       (["everyone", "owner"] & [source, dest]).empty?
781
782     buser_array = @bot.auth.save_array
783     buser_hash = buser_array.inject({}) { |h, u|
784       h[u[:username]] = u
785       h
786     }
787
788     return m.reply(_("no such botuser %{source}") % {:source=>source}) unless
789            buser_hash.keys.include?(source)
790     return m.reply(_("botuser %{dest} exists already") % {:dest=>dest}) if
791            buser_hash.keys.include?(dest)
792
793     copying = m.message.split[1] == "copy"
794     begin
795       if copying
796         h = {}
797         buser_hash[source].each { |k, val|
798           h[k] = val.dup
799         }
800       else
801         h = buser_hash[source]
802       end
803       h[:username] = dest
804       buser_array << h if copying
805
806       @bot.auth.load_array(buser_array, true)
807       @bot.auth.set_changed
808       call_event(:botuser, copying ? :copy : :rename, :source => source, :dest => dest)
809     rescue => e
810       return m.reply(_("failed: %{exception}") % {:exception=>e})
811     end
812     if copying
813       m.reply(_("botuser %{source} copied to %{dest}") %
814            {:source=>source, :dest=>dest})
815     else
816       m.reply(_("botuser %{source} renamed to %{dest}") %
817            {:source=>source, :dest=>dest})
818     end
819
820   end
821
822   def auth_export(m, params)
823
824     exportfile = @bot.path "new-auth.users"
825
826     what = params[:things]
827
828     has_to = what[-2] == "to"
829     if has_to
830       exportfile = @bot.path what[-1]
831       what.slice!(-2,2)
832     end
833
834     what.delete("all")
835
836     m.reply _("selecting data to export ...")
837
838     buser_array = @bot.auth.save_array
839     buser_hash = buser_array.inject({}) { |h, u|
840       h[u[:username]] = u
841       h
842     }
843
844     if what.empty?
845       we_want = buser_hash
846     else
847       we_want = buser_hash.delete_if { |key, val|
848         not what.include?(key)
849       }
850     end
851
852     m.reply _("preparing data for export ...")
853     begin
854       yaml_hash = {}
855       we_want.each { |k, val|
856         yaml_hash[k] = {}
857         val.each { |kk, v|
858           case kk
859           when :username
860             next
861           when :netmasks
862             yaml_hash[k][kk] = []
863             v.each { |nm|
864               yaml_hash[k][kk] << {
865                 :fullform => nm.fullform,
866                 :casemap => nm.casemap.to_s
867               }
868             }
869           else
870             yaml_hash[k][kk] = v
871           end
872         }
873       }
874     rescue => e
875       m.reply _("failed to prepare data: %{exception}") % {:exception=>e}
876       debug e.backtrace.dup.unshift(e.inspect).join("\n")
877       return
878     end
879
880     m.reply _("exporting to %{file} ...") % {:file=>exportfile}
881     begin
882       # m.reply yaml_hash.inspect
883       File.open(exportfile, "w") do |file|
884         file.puts YAML::dump(yaml_hash)
885       end
886     rescue => e
887       m.reply _("failed to export users: %{exception}") % {:exception=>e}
888       debug e.backtrace.dup.unshift(e.inspect).join("\n")
889       return
890     end
891     m.reply _("done")
892   end
893
894   def auth_import(m, params)
895
896     importfile = @bot.path "new-auth.users"
897
898     what = params[:things]
899
900     has_from = what[-2] == "from"
901     if has_from
902       importfile = @bot.path what[-1]
903       what.slice!(-2,2)
904     end
905
906     what.delete("all")
907
908     m.reply _("reading %{file} ...") % {:file=>importfile}
909     begin
910       yaml_hash = YAML::load_file(importfile)
911     rescue => e
912       m.reply _("failed to import from: %{exception}") % {:exception=>e}
913       debug e.backtrace.dup.unshift(e.inspect).join("\n")
914       return
915     end
916
917     # m.reply yaml_hash.inspect
918
919     m.reply _("selecting data to import ...")
920
921     if what.empty?
922       we_want = yaml_hash
923     else
924       we_want = yaml_hash.delete_if { |key, val|
925         not what.include?(key)
926       }
927     end
928
929     m.reply _("parsing data from import ...")
930
931     buser_hash = {}
932
933     begin
934       yaml_hash.each { |k, val|
935         buser_hash[k] = { :username => k }
936         val.each { |kk, v|
937           case kk
938           when :netmasks
939             buser_hash[k][kk] = []
940             v.each { |nm|
941               buser_hash[k][kk] << nm[:fullform].to_irc_netmask(:casemap => nm[:casemap].to_irc_casemap).to_irc_netmask(:server => @bot.server)
942             }
943           else
944             buser_hash[k][kk] = v
945           end
946         }
947       }
948     rescue => e
949       m.reply _("failed to parse data: %{exception}") % {:exception=>e}
950       debug e.backtrace.dup.unshift(e.inspect).join("\n")
951       return
952     end
953
954     # m.reply buser_hash.inspect
955
956     org_buser_array = @bot.auth.save_array
957     org_buser_hash = org_buser_array.inject({}) { |h, u|
958       h[u[:username]] = u
959       h
960     }
961
962     # TODO we may want to do a(n optional) key-by-key merge
963     #
964     org_buser_hash.merge!(buser_hash)
965     new_buser_array = org_buser_hash.values
966     @bot.auth.load_array(new_buser_array, true)
967     @bot.auth.set_changed
968
969     m.reply _("done")
970   end
971
972 end
973
974 auth = AuthModule.new
975
976 auth.map "user export *things",
977   :action => 'auth_export',
978   :defaults => { :things => ['all'] },
979   :auth_path => ':manage:fedex:'
980
981 auth.map "user import *things",
982  :action => 'auth_import',
983  :auth_path => ':manage:fedex:'
984
985 auth.map "user create :name :password",
986   :action => 'auth_create_user',
987   :defaults => {:password => nil},
988   :auth_path => ':manage:'
989
990 auth.map "user [:modifier] destroy :name",
991   :action => 'auth_destroy_user',
992   :requirements => { :modifier => /^(cancel|confirm)?$/ },
993   :defaults => { :modifier => '' },
994   :auth_path => ':manage::destroy!'
995
996 auth.map "user copy :source [to] :dest",
997   :action => 'auth_copy_ren_user',
998   :auth_path => ':manage:'
999
1000 auth.map "user rename :source [to] :dest",
1001   :action => 'auth_copy_ren_user',
1002   :auth_path => ':manage:'
1003
1004 auth.map "meet :nick [as :user]",
1005   :action => 'auth_meet',
1006   :auth_path => 'user::manage', :private => false
1007
1008 auth.map "hello",
1009   :action => 'auth_meet',
1010   :auth_path => 'user::manage::meet'
1011
1012 auth.default_auth("user::manage", false)
1013 auth.default_auth("user::manage::meet::hello", true)
1014
1015 auth.map "user tell :user the password for :botuser",
1016   :action => 'auth_tell_password',
1017   :auth_path => ':manage:'
1018
1019 auth.map "user list",
1020   :action => 'auth_list_users',
1021   :auth_path => '::'
1022
1023 auth.map "user *data",
1024   :action => 'auth_manage_user'
1025
1026 auth.map "allow :user to do *stuff [*where]",
1027   :action => 'auth_allow',
1028   :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
1029   :auth_path => ':edit::other:'
1030
1031 auth.map "deny :user from doing *stuff [*where]",
1032   :action => 'auth_deny',
1033   :requirements => {:where => /^(?:anywhere|everywhere|[io]n \S+)$/},
1034   :auth_path => ':edit::other:'
1035
1036 auth.default_auth("user", true)
1037 auth.default_auth("edit::other", false)
1038
1039 auth.map "whoami",
1040   :action => 'auth_whoami',
1041   :auth_path => '!*!'
1042
1043 auth.map "who is :user",
1044   :action => 'auth_whois',
1045   :auth_path => '!*!'
1046
1047 auth.map "auth :password",
1048   :action => 'auth_auth',
1049   :public => false,
1050   :auth_path => '!login!'
1051
1052 auth.map "login :botuser :password",
1053   :action => 'auth_login',
1054   :public => false,
1055   :defaults => { :password => nil },
1056   :auth_path => '!login!'
1057
1058 auth.map "login :botuser",
1059   :action => 'auth_login',
1060   :auth_path => '!login!'
1061
1062 auth.map "login",
1063   :action => 'auth_autologin',
1064   :auth_path => '!login!'
1065
1066 auth.map "permissions set *args",
1067   :action => 'auth_edit_perm',
1068   :auth_path => ':edit::set:'
1069
1070 auth.map "permissions reset *args",
1071   :action => 'auth_edit_perm',
1072   :auth_path => ':edit::set:'
1073
1074 auth.map "permissions view [for :user]",
1075   :action => 'auth_view_perm',
1076   :auth_path => '::'
1077
1078 auth.map "permissions search *pattern",
1079   :action => 'auth_search_perm',
1080   :auth_path => '::'
1081
1082 auth.default_auth('*', false)
1083