]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/auth.rb
Mode-checking methohds for User too
[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("%{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   end\r
303 \r
304   def reset_prop(botuser, prop)\r
305     k = prop.to_s.gsub("-","_")\r
306     botuser.send( ("reset_"+k).to_sym)\r
307   end\r
308 \r
309   def ask_bool_prop(botuser, prop)\r
310     k = prop.to_s.gsub("-","_")\r
311     botuser.send( (k + "?").to_sym)\r
312   end\r
313 \r
314   def auth_manage_user(m, params)\r
315     splits = params[:data]\r
316 \r
317     cmd = splits.first\r
318     return auth_whoami(m, params) if cmd.nil?\r
319 \r
320     botuser = get_botuser_for(m.source)\r
321     # By default, we do stuff on the botuser the irc user is bound to\r
322     butarget = botuser\r
323 \r
324     has_for = splits[-2] == "for"\r
325     butarget = @bot.auth.get_botuser(splits[-1]) if has_for\r
326     return m.reply(_("you can't mess with %{user}") % {:user => butarget.username}) \\r
327            if butarget == @bot.auth.botowner && botuser != butarget\r
328     splits.slice!(-2,2) if has_for\r
329 \r
330     bools = [:autologin, :"login-by-mask"]\r
331     can_set = [:password]\r
332     can_addrm = [:netmasks]\r
333     can_reset = bools + can_set + can_addrm\r
334     can_show = can_reset + ["perms"]\r
335 \r
336     case cmd.to_sym\r
337 \r
338     when :show\r
339       return _("you can't see the properties of %{user}") %\r
340              {:user => butarget.username} if botuser != butarget &&\r
341                                                !botuser.permit?("auth::show::other")\r
342 \r
343       case splits[1]\r
344       when nil, "all"\r
345         props = can_reset\r
346       when "password"\r
347         if botuser != butarget\r
348           return m.reply(_("no way I'm telling you the master password!")) if butarget == @bot.auth.botowner\r
349           return m.reply(_("you can't ask for someone else's password"))\r
350         end\r
351         return m.reply(_("c'mon, you can't be asking me seriously to tell you the password in public!")) if m.public?\r
352         return m.reply(_("the password for %{user} is %{password}")) %\r
353           { :user => butarget.username, :password => butarget.password }\r
354       else\r
355         props = splits[1..-1]\r
356       end\r
357 \r
358       str = []\r
359 \r
360       props.each { |arg|\r
361         k = arg.to_sym\r
362         next if k == :password\r
363         case k\r
364         when *bools\r
365           if ask_bool_prop(butarget, k)\r
366             str << _("can %{action}") % {:action => k}\r
367           else\r
368             str << _("can not %{action}") % {:action => k}\r
369           end\r
370         when :netmasks\r
371           if butarget.netmasks.empty?\r
372             str << _("knows no netmasks")\r
373           else\r
374             str << _("knows %{netmasks}") % {:netmasks => butarget.netmasks.join(", ")}\r
375           end\r
376         end\r
377       }\r
378       return m.reply("#{butarget.username} #{str.join('; ')}")\r
379 \r
380     when :enable, :disable\r
381       return m.reply(_("you can't change the default user")) if butarget == @bot.auth.everyone && !botuser.permit?("auth::edit::other::default")\r
382       return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if butarget != botuser && !botuser.permit?("auth::edit::other")\r
383 \r
384       return m.reply(need_args(cmd)) unless splits[1]\r
385       things = []\r
386       skipped = []\r
387       splits[1..-1].each { |a|\r
388         arg = a.to_sym\r
389         if bools.include?(arg)\r
390           set_prop(butarget, arg, cmd.to_sym == :enable)\r
391           things << a\r
392         else\r
393           skipped << a\r
394         end\r
395       }\r
396 \r
397       m.reply(_("I ignored %{things} because %{reason}") % {\r
398                 :things => skipped.join(', '),\r
399                 :reason => not_args(cmd, *bools)}) unless skipped.empty?\r
400       if things.empty?\r
401         m.reply _("I haven't changed anything")\r
402       else\r
403         @bot.auth.set_changed\r
404         return auth_manage_user(m, {:data => ["show"] + things + ["for", butarget.username] })\r
405       end\r
406 \r
407     when :set\r
408       return m.reply(_("you can't change the default user")) if\r
409              butarget == @bot.auth.everyone && !botuser.permit?("auth::edit::default")\r
410       return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if\r
411              butarget != botuser && !botuser.permit?("auth::edit::other")\r
412 \r
413       return m.reply(need_args(cmd)) unless splits[1]\r
414       arg = splits[1].to_sym\r
415       return m.reply(not_args(cmd, *can_set)) unless can_set.include?(arg)\r
416       argarg = splits[2]\r
417       return m.reply(need_args([cmd, splits[1]].join(" "))) unless argarg\r
418       if arg == :password && m.public?\r
419         return m.reply(_("is that a joke? setting the password in public?"))\r
420       end\r
421       set_prop(butarget, arg, argarg)\r
422       @bot.auth.set_changed\r
423       auth_manage_user(m, {:data => ["show", arg, "for", butarget.username] })\r
424 \r
425     when :reset\r
426       return m.reply(_("you can't change the default user")) if\r
427              butarget == @bot.auth.everyone && !botuser.permit?("auth::edit::default")\r
428       return m.reply(_("you can't edit %{user}") % {:user=>butarget.username}) if\r
429              butarget != botuser && !botuser.permit?("auth::edit::other")\r
430 \r
431       return m.reply(need_args(cmd)) unless splits[1]\r
432       things = []\r
433       skipped = []\r
434       splits[1..-1].each { |a|\r
435         arg = a.to_sym\r
436         if can_reset.include?(arg)\r
437           reset_prop(butarget, arg)\r
438           things << a\r
439         else\r
440           skipped << a\r
441         end\r
442       }\r
443 \r
444       m.reply(_("I ignored %{things} because %{reason}") %\r
445                 { :things => skipped.join(', '),\r
446                   :reason => not_args(cmd, *can_reset)}) unless skipped.empty?\r
447       if things.empty?\r
448         m.reply _("I haven't changed anything")\r
449       else\r
450         @bot.auth.set_changed\r
451         @bot.say(m.source, _("the password for %{user} is now %{password}") %\r
452           {:user => butarget.username, :password => butarget.password}) if\r
453           things.include?("password")\r
454         return auth_manage_user(m, {:data => (["show"] + things - ["password"]) + ["for", butarget.username]})\r
455       end\r
456 \r
457     when :add, :rm, :remove, :del, :delete\r
458       return m.reply(_("you can't change the default user")) if\r
459              butarget == @bot.auth.everyone && !botuser.permit?("auth::edit::default")\r
460       return m.reply(_("you can't edit %{user}") % {:user => butarget.username}) if\r
461              butarget != botuser && !botuser.permit?("auth::edit::other")\r
462 \r
463       arg = splits[1]\r
464       if arg.nil? or arg !~ /netmasks?/ or splits[2].nil?\r
465         return m.reply(_("I can only add/remove netmasks. See +help user add+ for more instructions"))\r
466       end\r
467 \r
468       method = cmd.to_sym == :add ? :add_netmask : :delete_netmask\r
469 \r
470       failed = []\r
471 \r
472       splits[2..-1].each { |mask|\r
473         begin\r
474           butarget.send(method, mask.to_irc_netmask(:server => @bot.server))\r
475         rescue\r
476           failed << mask\r
477         end\r
478       }\r
479       m.reply "I failed to #{cmd} #{failed.join(', ')}" unless failed.empty?\r
480       @bot.auth.set_changed\r
481       return auth_manage_user(m, {:data => ["show", "netmasks", "for", butarget.username] })\r
482 \r
483     else\r
484       m.reply _("sorry, I don't know how to %{request}") % {:request => m.message}\r
485     end\r
486   end\r
487 \r
488   def auth_tell_password(m, params)\r
489     user = params[:user]\r
490     begin\r
491       botuser = @bot.auth.get_botuser(params[:botuser])\r
492     rescue\r
493       return m.reply(_("couldn't find botuser %{user}") % {:user => params[:botuser]})\r
494     end\r
495     m.reply(_("I'm not telling the master password to anyway, pal")) if botuser == @bot.auth.botowner\r
496     msg = _("the password for botuser %{user} is %{password}") %\r
497           {:user => botuser.username, :password => botuser.password}\r
498     @bot.say user, msg\r
499     @bot.say m.source, _("I told %{user} that %{message}") % {:user => user, :message => msg}\r
500   end\r
501 \r
502   def auth_create_user(m, params)\r
503     name = params[:name]\r
504     password = params[:password]\r
505     return m.reply(_("are you nuts, creating a botuser with a publicly known password?")) if m.public? and not password.nil?\r
506     begin\r
507       bu = @bot.auth.create_botuser(name, password)\r
508       @bot.auth.set_changed\r
509     rescue => e\r
510       m.reply(_("failed to create %{user}: %{exception}") % {:user => name,  :exception => e})\r
511       debug e.inspect + "\n" + e.backtrace.join("\n")\r
512       return\r
513     end\r
514     m.reply(_("created botuser %{user}") % {:user => bu.username})\r
515   end\r
516 \r
517   def auth_list_users(m, params)\r
518     # TODO name regexp to filter results\r
519     list = @bot.auth.save_array.inject([]) { |list, x| list << x[:username] } - ['everyone', 'owner']\r
520     if defined?(@destroy_q)\r
521       list.map! { |x|\r
522         @destroy_q.include?(x) ? x + _(" (queued for destruction)") : x\r
523       }\r
524     end\r
525     return m.reply(_("I have no botusers other than the default ones")) if list.empty?\r
526     return m.reply(n_("botuser: %{list}", "botusers: %{list}", list.length) %\r
527                    {:list => list.join(', ')})\r
528   end\r
529 \r
530   def auth_destroy_user(m, params)\r
531     @destroy_q = [] unless defined?(@destroy_q)\r
532     buname = params[:name]\r
533     return m.reply(_("You can't destroy %{user}") % {:user => buname}) if\r
534            ["everyone", "owner"].include?(buname)\r
535     cancel = m.message.split[1] == 'cancel'\r
536     password = params[:password]\r
537 \r
538     buser_array = @bot.auth.save_array\r
539     buser_hash = buser_array.inject({}) { |h, u|\r
540       h[u[:username]] = u\r
541       h\r
542     }\r
543 \r
544     return m.reply(_("no such botuser %{user}") % {:user=>buname}) unless\r
545            buser_hash.keys.include?(buname)\r
546 \r
547     if cancel\r
548       if @destroy_q.include?(buname)\r
549         @destroy_q.delete(buname)\r
550         m.reply(_("%{user} removed from the destruction queue") % {:user=>buname})\r
551       else\r
552         m.reply(_("%{user} was not queued for destruction") % {:user=>buname})\r
553       end\r
554       return\r
555     end\r
556 \r
557     if password.nil?\r
558       if @destroy_q.include?(buname)\r
559         return m.reply(_("%{user} already queued for destruction, use %{highlight}user destroy %{user} <password>%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold})\r
560       else\r
561         @destroy_q << buname\r
562         return m.reply(_("%{user} queued for destruction, use %{highlight}user destroy %{user} <password>%{highlight} to destroy it") % {:user=>buname, :highlight=>Bold})\r
563       end\r
564     else\r
565       begin\r
566         return m.reply(_("%{user} is not queued for destruction yet") %\r
567                {:user=>buname}) unless @destroy_q.include?(buname)\r
568         return m.reply(_("wrong password for %{user}") %\r
569                {:user=>buname}) unless buser_hash[buname][:password] == password\r
570         buser_array.delete_if { |u|\r
571           u[:username] == buname\r
572         }\r
573         @destroy_q.delete(buname)\r
574         @bot.auth.load_array(buser_array, true)\r
575         @bot.auth.set_changed\r
576       rescue => e\r
577         return m.reply(_("failed: %{exception}") % {:exception => e})\r
578       end\r
579       return m.reply(_("botuser %{user} destroyed") % {:user => buname})\r
580     end\r
581 \r
582   end\r
583 \r
584   def auth_copy_ren_user(m, params)\r
585     source = Auth::BotUser.sanitize_username(params[:source])\r
586     dest = Auth::BotUser.sanitize_username(params[:dest])\r
587     return m.reply(_("please don't touch the default users")) unless\r
588       (["everyone", "owner"] & [source, dest]).empty?\r
589 \r
590     buser_array = @bot.auth.save_array\r
591     buser_hash = buser_array.inject({}) { |h, u|\r
592       h[u[:username]] = u\r
593       h\r
594     }\r
595 \r
596     return m.reply(_("no such botuser %{source}") % {:source=>source}) unless\r
597            buser_hash.keys.include?(source)\r
598     return m.reply(_("botuser %{dest} exists already") % {:dest=>dest}) if\r
599            buser_hash.keys.include?(dest)\r
600 \r
601     copying = m.message.split[1] == "copy"\r
602     begin\r
603       if copying\r
604         h = {}\r
605         buser_hash[source].each { |k, val|\r
606           h[k] = val.dup\r
607         }\r
608       else\r
609         h = buser_hash[source]\r
610       end\r
611       h[:username] = dest\r
612       buser_array << h if copying\r
613 \r
614       @bot.auth.load_array(buser_array, true)\r
615       @bot.auth.set_changed\r
616     rescue => e\r
617       return m.reply(_("failed: %{exception}") % {:exception=>e})\r
618     end\r
619     return m.reply(_("botuser %{source} copied to %{dest}") %\r
620            {:source=>source, :dest=>dest}) if copying\r
621     return m.reply(_("botuser %{source} renamed to %{dest}") %\r
622            {:source=>source, :dest=>dest})\r
623 \r
624   end\r
625 \r
626   def auth_export(m, params)\r
627 \r
628     exportfile = "#{@bot.botclass}/new-auth.users"\r
629 \r
630     what = params[:things]\r
631 \r
632     has_to = what[-2] == "to"\r
633     if has_to\r
634       exportfile = "#{@bot.botclass}/#{what[-1]}"\r
635       what.slice!(-2,2)\r
636     end\r
637 \r
638     what.delete("all")\r
639 \r
640     m.reply _("selecting data to export ...")\r
641 \r
642     buser_array = @bot.auth.save_array\r
643     buser_hash = buser_array.inject({}) { |h, u|\r
644       h[u[:username]] = u\r
645       h\r
646     }\r
647 \r
648     if what.empty?\r
649       we_want = buser_hash\r
650     else\r
651       we_want = buser_hash.delete_if { |key, val|\r
652         not what.include?(key)\r
653       }\r
654     end\r
655 \r
656     m.reply _("preparing data for export ...")\r
657     begin\r
658       yaml_hash = {}\r
659       we_want.each { |k, val|\r
660         yaml_hash[k] = {}\r
661         val.each { |kk, v|\r
662           case kk\r
663           when :username\r
664             next\r
665           when :netmasks\r
666             yaml_hash[k][kk] = []\r
667             v.each { |nm|\r
668               yaml_hash[k][kk] << {\r
669                 :fullform => nm.fullform,\r
670                 :casemap => nm.casemap.to_s\r
671               }\r
672             }\r
673           else\r
674             yaml_hash[k][kk] = v\r
675           end\r
676         }\r
677       }\r
678     rescue => e\r
679       m.reply _("failed to prepare data: %{exception}") % {:exception=>e}\r
680       debug e.backtrace.dup.unshift(e.inspect).join("\n")\r
681       return\r
682     end\r
683 \r
684     m.reply _("exporting to %{file} ...") % {:file=>exportfile}\r
685     begin\r
686       # m.reply yaml_hash.inspect\r
687       File.open(exportfile, "w") do |file|\r
688         file.puts YAML::dump(yaml_hash)\r
689       end\r
690     rescue => e\r
691       m.reply _("failed to export users: %{exception}") % {:exception=>e}\r
692       debug e.backtrace.dup.unshift(e.inspect).join("\n")\r
693       return\r
694     end\r
695     m.reply _("done")\r
696   end\r
697 \r
698   def auth_import(m, params)\r
699 \r
700     importfile = "#{@bot.botclass}/new-auth.users"\r
701 \r
702     what = params[:things]\r
703 \r
704     has_from = what[-2] == "from"\r
705     if has_from\r
706       importfile = "#{@bot.botclass}/#{what[-1]}"\r
707       what.slice!(-2,2)\r
708     end\r
709 \r
710     what.delete("all")\r
711 \r
712     m.reply _("reading %{file} ...") % {:file=>importfile}\r
713     begin\r
714       yaml_hash = YAML::load_file(importfile)\r
715     rescue => e\r
716       m.reply _("failed to import from: %{exception}") % {:exception=>e}\r
717       debug e.backtrace.dup.unshift(e.inspect).join("\n")\r
718       return\r
719     end\r
720 \r
721     # m.reply yaml_hash.inspect\r
722 \r
723     m.reply _("selecting data to import ...")\r
724 \r
725     if what.empty?\r
726       we_want = yaml_hash\r
727     else\r
728       we_want = yaml_hash.delete_if { |key, val|\r
729         not what.include?(key)\r
730       }\r
731     end\r
732 \r
733     m.reply _("parsing data from import ...")\r
734 \r
735     buser_hash = {}\r
736 \r
737     begin\r
738       yaml_hash.each { |k, val|\r
739         buser_hash[k] = { :username => k }\r
740         val.each { |kk, v|\r
741           case kk\r
742           when :netmasks\r
743             buser_hash[k][kk] = []\r
744             v.each { |nm|\r
745               buser_hash[k][kk] << nm[:fullform].to_irc_netmask(:casemap => nm[:casemap].to_irc_casemap).to_irc_netmask(:server => @bot.server)\r
746             }\r
747           else\r
748             buser_hash[k][kk] = v\r
749           end\r
750         }\r
751       }\r
752     rescue => e\r
753       m.reply _("failed to parse data: %{exception}") % {:exception=>e}\r
754       debug e.backtrace.dup.unshift(e.inspect).join("\n")\r
755       return\r
756     end\r
757 \r
758     # m.reply buser_hash.inspect\r
759 \r
760     org_buser_array = @bot.auth.save_array\r
761     org_buser_hash = org_buser_array.inject({}) { |h, u|\r
762       h[u[:username]] = u\r
763       h\r
764     }\r
765 \r
766     # TODO we may want to do a(n optional) key-by-key merge\r
767     #\r
768     org_buser_hash.merge!(buser_hash)\r
769     new_buser_array = org_buser_hash.values\r
770     @bot.auth.load_array(new_buser_array, true)\r
771     @bot.auth.set_changed\r
772 \r
773     m.reply _("done")\r
774   end\r
775 \r
776 end\r
777 \r
778 auth = AuthModule.new\r
779 \r
780 auth.map "user export *things",\r
781   :action => 'auth_export',\r
782   :defaults => { :things => ['all'] },\r
783   :auth_path => ':manage:fedex:'\r
784 \r
785 auth.map "user import *things",\r
786  :action => 'auth_import',\r
787  :auth_path => ':manage:fedex:'\r
788 \r
789 auth.map "user create :name :password",\r
790   :action => 'auth_create_user',\r
791   :defaults => {:password => nil},\r
792   :auth_path => ':manage:'\r
793 \r
794 auth.map "user [cancel] destroy :name :password",\r
795   :action => 'auth_destroy_user',\r
796   :defaults => { :password => nil },\r
797   :auth_path => ':manage::destroy:'\r
798 \r
799 auth.map "user copy :source [to] :dest",\r
800   :action => 'auth_copy_ren_user',\r
801   :auth_path => ':manage:'\r
802 \r
803 auth.map "user rename :source [to] :dest",\r
804   :action => 'auth_copy_ren_user',\r
805   :auth_path => ':manage:'\r
806 \r
807 auth.default_auth("user::manage", false)\r
808 \r
809 auth.map "user tell :user the password for :botuser",\r
810   :action => 'auth_tell_password',\r
811   :auth_path => '::'\r
812 \r
813 auth.map "user list",\r
814   :action => 'auth_list_users',\r
815   :auth_path => '::'\r
816 \r
817 auth.map "user *data",\r
818   :action => 'auth_manage_user'\r
819 \r
820 auth.default_auth("user", true)\r
821 auth.default_auth("edit::other", false)\r
822 \r
823 auth.map "whoami",\r
824   :action => 'auth_whoami',\r
825   :auth_path => '!*!'\r
826 \r
827 auth.map "auth :password",\r
828   :action => 'auth_auth',\r
829   :public => false,\r
830   :auth_path => '!login!'\r
831 \r
832 auth.map "login :botuser :password",\r
833   :action => 'auth_login',\r
834   :public => false,\r
835   :defaults => { :password => nil },\r
836   :auth_path => '!login!'\r
837 \r
838 auth.map "login :botuser",\r
839   :action => 'auth_login',\r
840   :auth_path => '!login!'\r
841 \r
842 auth.map "login",\r
843   :action => 'auth_autologin',\r
844   :auth_path => '!login!'\r
845 \r
846 auth.map "permissions set *args",\r
847   :action => 'auth_edit_perm',\r
848   :auth_path => ':edit::set:'\r
849 \r
850 auth.map "permissions reset *args",\r
851   :action => 'auth_edit_perm',\r
852   :auth_path => ':edit::reset:'\r
853 \r
854 auth.map "permissions view [for :user]",\r
855   :action => 'auth_view_perm',\r
856   :auth_path => '::'\r
857 \r
858 auth.default_auth('*', false)\r
859 \r