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