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