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