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