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