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