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