]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/botuser.rb
New Auth Framework: dream about auto-notifying hashes, and use #replace() when loadin...
[user/henk/code/ruby/rbot.git] / lib / rbot / botuser.rb
1 #-- vim:sw=2:et\r
2 #++\r
3 # :title: User management\r
4 #\r
5 # rbot user management\r
6 # Author:: Giuseppe Bilotta (giuseppe.bilotta@gmail.com)\r
7 # Copyright:: Copyright (c) 2006 Giuseppe Bilotta\r
8 # License:: GPLv2\r
9 \r
10 require 'singleton'\r
11 require 'set'\r
12 \r
13 # This would be a good idea if it was failproof, but the truth\r
14 # is that other methods can indirectly modify the hash. *sigh*\r
15 #\r
16 # class AuthNotifyingHash < Hash\r
17 #   %w(clear default= delete delete_if replace invert\r
18 #      merge! update rehash reject! replace shift []= store).each { |m|\r
19 #     class_eval {\r
20 #       define_method(m) { |*a|\r
21 #         r = super(*a)\r
22 #         Irc::Auth.authmanager.set_changed\r
23 #         r\r
24 #       }\r
25 #     }\r
26 #   }\r
27 # end\r
28\r
29 \r
30 module Irc\r
31 \r
32 \r
33   # This module contains the actual Authentication stuff\r
34   #\r
35   module Auth\r
36 \r
37     BotConfig.register BotConfigStringValue.new( 'auth.password',\r
38       :default => 'rbotauth', :wizard => true,\r
39       :on_change => Proc.new {|bot, v| bot.auth.botowner.password = v},\r
40       :desc => _('Password for the bot owner'))\r
41     BotConfig.register BotConfigBooleanValue.new( 'auth.login_by_mask',\r
42       :default => 'true',\r
43       :desc => _('Set false to prevent new botusers from logging in without a password when the user netmask is known'))\r
44     BotConfig.register BotConfigBooleanValue.new( 'auth.autologin',\r
45       :default => 'true',\r
46       :desc => _('Set false to prevent new botusers from recognizing IRC users without a need to manually login'))\r
47     BotConfig.register BotConfigBooleanValue.new( 'auth.autouser',\r
48       :default => 'false',\r
49       :desc => _('Set true to allow new botusers to be created automatically'))\r
50     # BotConfig.register BotConfigIntegerValue.new( 'auth.default_level',\r
51     #   :default => 10, :wizard => true,\r
52     #   :desc => 'The default level for new/unknown users' )\r
53 \r
54     # Generate a random password of length _l_\r
55     #\r
56     def Auth.random_password(l=8)\r
57       pwd = ""\r
58       l.times do\r
59         pwd << (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr\r
60       end\r
61       return pwd\r
62     end\r
63 \r
64 \r
65     # An Irc::Auth::Command defines a command by its "path":\r
66     #\r
67     #   base::command::subcommand::subsubcommand::subsubsubcommand\r
68     #\r
69     class Command\r
70 \r
71       attr_reader :command, :path\r
72 \r
73       # A method that checks if a given _cmd_ is in a form that can be\r
74       # reduced into a canonical command path, and if so, returns it\r
75       #\r
76       def sanitize_command_path(cmd)\r
77         pre = cmd.to_s.downcase.gsub(/^\*?(?:::)?/,"").gsub(/::$/,"")\r
78         return pre if pre.empty?\r
79         return pre if pre =~ /^\S+(::\S+)*$/\r
80         raise TypeError, "#{cmd.inspect} is not a valid command"\r
81       end\r
82 \r
83       # Creates a new Command from a given string; you can then access\r
84       # the command as a symbol with the :command method and the whole\r
85       # path as :path\r
86       #\r
87       #   Command.new("core::auth::save").path => [:"*", :"core", :"core::auth", :"core::auth::save"]\r
88       #\r
89       #   Command.new("core::auth::save").command => :"core::auth::save"\r
90       #\r
91       def initialize(cmd)\r
92         cmdpath = sanitize_command_path(cmd).split('::')\r
93         seq = cmdpath.inject(["*"]) { |list, cmd|\r
94           list << (list.length > 1 ? list.last + "::" : "") + cmd\r
95         }\r
96         @path = seq.map { |k|\r
97           k.to_sym\r
98         }\r
99         @command = path.last\r
100         debug "Created command #{@command.inspect} with path #{@path.pretty_inspect}"\r
101       end\r
102 \r
103       # Returs self\r
104       def to_irc_auth_command\r
105         self\r
106       end\r
107 \r
108     end\r
109 \r
110   end\r
111 \r
112 end\r
113 \r
114 \r
115 class String\r
116 \r
117   # Returns an Irc::Auth::Comand from the receiver\r
118   def to_irc_auth_command\r
119     Irc::Auth::Command.new(self)\r
120   end\r
121 \r
122 end\r
123 \r
124 \r
125 class Symbol\r
126 \r
127   # Returns an Irc::Auth::Comand from the receiver\r
128   def to_irc_auth_command\r
129     Irc::Auth::Command.new(self)\r
130   end\r
131 \r
132 end\r
133 \r
134 \r
135 module Irc\r
136 \r
137 \r
138   module Auth\r
139 \r
140 \r
141     # This class describes a permission set\r
142     class PermissionSet\r
143 \r
144       attr_reader :perm\r
145       # Create a new (empty) PermissionSet\r
146       #\r
147       def initialize\r
148         @perm = {}\r
149       end\r
150 \r
151       # Inspection simply inspects the internal hash\r
152       def inspect\r
153         @perm.inspect\r
154       end\r
155 \r
156       # Sets the permission for command _cmd_ to _val_,\r
157       #\r
158       def set_permission(str, val)\r
159         cmd = str.to_irc_auth_command\r
160         case val\r
161         when true, false\r
162           @perm[cmd.command] = val\r
163         when nil\r
164           @perm.delete(cmd.command)\r
165         else\r
166           raise TypeError, "#{val.inspect} must be true or false" unless [true,false].include?(val)\r
167         end\r
168       end\r
169 \r
170       # Resets the permission for command _cmd_\r
171       #\r
172       def reset_permission(cmd)\r
173         set_permission(cmd, nil)\r
174       end\r
175 \r
176       # Tells if command _cmd_ is permitted. We do this by returning\r
177       # the value of the deepest Command#path that matches.\r
178       #\r
179       def permit?(str)\r
180         cmd = str.to_irc_auth_command\r
181         allow = nil\r
182         cmd.path.reverse.each { |k|\r
183           if @perm.has_key?(k)\r
184             allow = @perm[k]\r
185             break\r
186           end\r
187         }\r
188         return allow\r
189       end\r
190 \r
191     end\r
192 \r
193 \r
194     # This is the error that gets raised when an invalid password is met\r
195     #\r
196     class InvalidPassword < RuntimeError\r
197     end\r
198 \r
199 \r
200     # This is the basic class for bot users: they have a username, a\r
201     # password, a list of netmasks to match against, and a list of\r
202     # permissions. A BotUser can be marked as 'transient', usually meaning\r
203     # it's not intended for permanent storage. Transient BotUsers have lower\r
204     # priority than nontransient ones for autologin purposes.\r
205     #\r
206     # To initialize a BotUser, you pass a _username_ and an optional\r
207     # hash of options. Currently, only two options are recognized:\r
208     #\r
209     # transient:: true or false, determines if the BotUser is transient or\r
210     #             permanent (default is false, permanent BotUser).\r
211     #\r
212     #             Transient BotUsers are initialized by prepending an\r
213     #             asterisk (*) to the username, and appending a sanitized\r
214     #             version of the object_id. The username can be empty.\r
215     #             A random password is generated.\r
216     #\r
217     #             Permanent Botusers need the username as is, and no\r
218     #             password is generated.\r
219     #\r
220     # masks::     an array of Netmasks to initialize the NetmaskList. This\r
221     #             list is used as-is for permanent BotUsers.\r
222     #\r
223     #             Transient BotUsers will alter the list elements which are\r
224     #             Irc::User by globbing the nick and any initial nonletter\r
225     #             part of the ident.\r
226     #\r
227     #             The masks option is optional for permanent BotUsers, but\r
228     #             obligatory (non-empty) for transients.\r
229     #\r
230     class BotUser\r
231 \r
232       attr_reader :username\r
233       attr_reader :password\r
234       attr_reader :netmasks\r
235       attr_reader :perm\r
236       attr_reader :data\r
237       attr_writer :login_by_mask\r
238       attr_writer :autologin\r
239       attr_writer :transient\r
240 \r
241       # Checks if the BotUser is transient\r
242       def transient?\r
243         @transient\r
244       end\r
245 \r
246       # Checks if the BotUser is permanent (not transient)\r
247       def permanent?\r
248         !@permanent\r
249       end\r
250 \r
251       # Sets if the BotUser is permanent or not\r
252       def permanent=(bool)\r
253         @transient=!bool\r
254       end\r
255 \r
256       # Create a new BotUser with given username\r
257       def initialize(username, options={})\r
258         opts = {:transient => false}.merge(options)\r
259         @transient = opts[:transient]\r
260 \r
261         if @transient\r
262           @username = "*"\r
263           @username << BotUser.sanitize_username(username) if username and not username.to_s.empty?\r
264           @username << BotUser.sanitize_username(object_id)\r
265           reset_password\r
266           @login_by_mask=true\r
267           @autologin=true\r
268         else\r
269           @username = BotUser.sanitize_username(username)\r
270           @password = nil\r
271           reset_login_by_mask\r
272           reset_autologin\r
273         end\r
274 \r
275         @netmasks = NetmaskList.new\r
276         if opts.key?(:masks) and opts[:masks]\r
277           masks = opts[:masks]\r
278           masks = [masks] unless masks.respond_to?(:each)\r
279           masks.each { |m|\r
280             mask = m.to_irc_netmask\r
281             if @transient and User === m\r
282               mask.nick = "*"\r
283               mask.host = m.host.dup\r
284               mask.user = "*" + m.user.sub(/^\w?[^\w]+/,'')\r
285             end\r
286             add_netmask(mask) unless mask.to_s == "*"\r
287           }\r
288         end\r
289         raise "must provide a usable mask for transient BotUser #{@username}" if @transient and @netmasks.empty?\r
290 \r
291         @perm = {}\r
292 \r
293         # @data = AuthNotifyingHash.new\r
294         @data = {}\r
295       end\r
296 \r
297       # Inspection\r
298       def inspect\r
299         str = "<#{self.class}:#{'0x%08x' % self.object_id}"\r
300         str << " (transient)" if @transient\r
301         str << ":"\r
302         str << " @username=#{@username.inspect}"\r
303         str << " @netmasks=#{@netmasks.inspect}"\r
304         str << " @perm=#{@perm.inspect}"\r
305         str << " @login_by_mask=#{@login_by_mask}"\r
306         str << " @autologin=#{@autologin}"\r
307         if @data.empty?\r
308           str << " no data"\r
309         else\r
310           str << " data for #{@data.keys.join(', ')}"\r
311         end\r
312         str << ">"\r
313       end\r
314 \r
315       # In strings\r
316       def to_s\r
317         @username\r
318       end\r
319 \r
320       # Convert into a hash\r
321       def to_hash\r
322         {\r
323           :username => @username,\r
324           :password => @password,\r
325           :netmasks => @netmasks,\r
326           :perm => @perm,\r
327           :login_by_mask => @login_by_mask,\r
328           :autologin => @autologin,\r
329           :data => @data\r
330         }\r
331       end\r
332 \r
333       # Do we allow logging in without providing the password?\r
334       #\r
335       def login_by_mask?\r
336         @login_by_mask\r
337       end\r
338 \r
339       # Reset the login-by-mask option\r
340       #\r
341       def reset_login_by_mask\r
342         @login_by_mask = Auth.authmanager.bot.config['auth.login_by_mask'] unless defined?(@login_by_mask)\r
343       end\r
344 \r
345       # Reset the autologin option\r
346       #\r
347       def reset_autologin\r
348         @autologin = Auth.authmanager.bot.config['auth.autologin'] unless defined?(@autologin)\r
349       end\r
350 \r
351       # Do we allow automatic logging in?\r
352       #\r
353       def autologin?\r
354         @autologin\r
355       end\r
356 \r
357       # Restore from hash\r
358       def from_hash(h)\r
359         @username = h[:username] if h.has_key?(:username)\r
360         @password = h[:password] if h.has_key?(:password)\r
361         @netmasks = h[:netmasks] if h.has_key?(:netmasks)\r
362         @perm = h[:perm] if h.has_key?(:perm)\r
363         @login_by_mask = h[:login_by_mask] if h.has_key?(:login_by_mask)\r
364         @autologin = h[:autologin] if h.has_key?(:autologin)\r
365         @data.replace(h[:data]) if h.has_key?(:data)\r
366       end\r
367 \r
368       # This method sets the password if the proposed new password\r
369       # is valid\r
370       def password=(pwd=nil)\r
371         pass = pwd.to_s\r
372         if pass.empty?\r
373           reset_password\r
374         else\r
375           begin\r
376             raise InvalidPassword, "#{pass} contains invalid characters" if pass !~ /^[\x21-\x7e]+$/\r
377             raise InvalidPassword, "#{pass} too short" if pass.length < 4\r
378             @password = pass\r
379           rescue InvalidPassword => e\r
380             raise e\r
381           rescue => e\r
382             raise InvalidPassword, "Exception #{e.inspect} while checking #{pass.inspect} (#{pwd.inspect})"\r
383           end\r
384         end\r
385       end\r
386 \r
387       # Resets the password by creating a new onw\r
388       def reset_password\r
389         @password = Auth.random_password\r
390       end\r
391 \r
392       # Sets the permission for command _cmd_ to _val_ on channel _chan_\r
393       #\r
394       def set_permission(cmd, val, chan="*")\r
395         k = chan.to_s.to_sym\r
396         @perm[k] = PermissionSet.new unless @perm.has_key?(k)\r
397         @perm[k].set_permission(cmd, val)\r
398       end\r
399 \r
400       # Resets the permission for command _cmd_ on channel _chan_\r
401       #\r
402       def reset_permission(cmd, chan ="*")\r
403         set_permission(cmd, nil, chan)\r
404       end\r
405 \r
406       # Checks if BotUser is allowed to do something on channel _chan_,\r
407       # or on all channels if _chan_ is nil\r
408       #\r
409       def permit?(cmd, chan=nil)\r
410         if chan\r
411           k = chan.to_s.to_sym\r
412         else\r
413           k = :*\r
414         end\r
415         allow = nil\r
416         if @perm.has_key?(k)\r
417           allow = @perm[k].permit?(cmd)\r
418         end\r
419         return allow\r
420       end\r
421 \r
422       # Adds a Netmask\r
423       #\r
424       def add_netmask(mask)\r
425         @netmasks << mask.to_irc_netmask\r
426       end\r
427 \r
428       # Removes a Netmask\r
429       #\r
430       def delete_netmask(mask)\r
431         m = mask.to_irc_netmask\r
432         @netmasks.delete(m)\r
433       end\r
434 \r
435       # Removes all <code>Netmask</code>s\r
436       #\r
437       def reset_netmasks\r
438         @netmasks = NetmaskList.new\r
439       end\r
440 \r
441       # This method checks if BotUser has a Netmask that matches _user_\r
442       #\r
443       def knows?(usr)\r
444         user = usr.to_irc_user\r
445         known = false\r
446         @netmasks.each { |n|\r
447           if user.matches?(n)\r
448             known = true\r
449             break\r
450           end\r
451         }\r
452         return known\r
453       end\r
454 \r
455       # This method gets called when User _user_ wants to log in.\r
456       # It returns true or false depending on whether the password\r
457       # is right. If it is, the Netmask of the user is added to the\r
458       # list of acceptable Netmask unless it's already matched.\r
459       def login(user, password=nil)\r
460         if password == @password or (password.nil? and (@login_by_mask || @autologin) and knows?(user))\r
461           add_netmask(user) unless knows?(user)\r
462           debug "#{user} logged in as #{self.inspect}"\r
463           return true\r
464         else\r
465           return false\r
466         end\r
467       end\r
468 \r
469       # # This method gets called when User _user_ has logged out as this BotUser\r
470       # def logout(user)\r
471       #   delete_netmask(user) if knows?(user)\r
472       # end\r
473 \r
474       # This method sanitizes a username by chomping, downcasing\r
475       # and replacing any nonalphanumeric character with _\r
476       #\r
477       def BotUser.sanitize_username(name)\r
478         candidate = name.to_s.chomp.downcase.gsub(/[^a-z0-9]/,"_")\r
479         raise "sanitized botusername #{candidate} too short" if candidate.length < 3\r
480         return candidate\r
481       end\r
482 \r
483     end\r
484 \r
485     # This is the default BotUser: it's used for all users which haven't\r
486     # identified with the bot\r
487     #\r
488     class DefaultBotUserClass < BotUser\r
489 \r
490       private :add_netmask, :delete_netmask\r
491 \r
492       include Singleton\r
493 \r
494       # The default BotUser is named 'everyone'\r
495       #\r
496       def initialize\r
497         reset_login_by_mask\r
498         reset_autologin\r
499         super("everyone")\r
500         @default_perm = PermissionSet.new\r
501       end\r
502 \r
503       # This method returns without changing anything\r
504       #\r
505       def login_by_mask=(val)\r
506         debug "Tried to change the login-by-mask for default bot user, ignoring"\r
507         return @login_by_mask\r
508       end\r
509 \r
510       # The default botuser allows logins by mask\r
511       #\r
512       def reset_login_by_mask\r
513         @login_by_mask = true\r
514       end\r
515 \r
516       # This method returns without changing anything\r
517       #\r
518       def autologin=(val)\r
519         debug "Tried to change the autologin for default bot user, ignoring"\r
520         return\r
521       end\r
522 \r
523       # The default botuser doesn't allow autologin (meaningless)\r
524       #\r
525       def reset_autologin\r
526         @autologin = false\r
527       end\r
528 \r
529       # Sets the default permission for the default user (i.e. the ones\r
530       # set by the BotModule writers) on all channels\r
531       #\r
532       def set_default_permission(cmd, val)\r
533         @default_perm.set_permission(Command.new(cmd), val)\r
534         debug "Default permissions now: #{@default_perm.pretty_inspect}"\r
535       end\r
536 \r
537       # default knows everybody\r
538       #\r
539       def knows?(user)\r
540         return true if user.to_irc_user\r
541       end\r
542 \r
543       # We always allow logging in as the default user\r
544       def login(user, password)\r
545         return true\r
546       end\r
547 \r
548       # Resets the NetmaskList\r
549       def reset_netmasks\r
550         super\r
551         add_netmask("*!*@*")\r
552       end\r
553 \r
554       # DefaultBotUser will check the default_perm after checking\r
555       # the global ones\r
556       # or on all channels if _chan_ is nil\r
557       #\r
558       def permit?(cmd, chan=nil)\r
559         allow = super(cmd, chan)\r
560         if allow.nil? && chan.nil?\r
561           allow = @default_perm.permit?(cmd)\r
562         end\r
563         return allow\r
564       end\r
565 \r
566     end\r
567 \r
568     # Returns the only instance of DefaultBotUserClass\r
569     #\r
570     def Auth.defaultbotuser\r
571       return DefaultBotUserClass.instance\r
572     end\r
573 \r
574     # This is the BotOwner: he can do everything\r
575     #\r
576     class BotOwnerClass < BotUser\r
577 \r
578       include Singleton\r
579 \r
580       def initialize\r
581         @login_by_mask = false\r
582         @autologin = true\r
583         super("owner")\r
584       end\r
585 \r
586       def permit?(cmd, chan=nil)\r
587         return true\r
588       end\r
589 \r
590     end\r
591 \r
592     # Returns the only instance of BotOwnerClass\r
593     #\r
594     def Auth.botowner\r
595       return BotOwnerClass.instance\r
596     end\r
597 \r
598 \r
599     # This is the AuthManagerClass singleton, used to manage User/BotUser connections and\r
600     # everything\r
601     #\r
602     class AuthManagerClass\r
603 \r
604       include Singleton\r
605 \r
606       attr_reader :everyone\r
607       attr_reader :botowner\r
608       attr_reader :bot\r
609 \r
610       # The instance manages two <code>Hash</code>es: one that maps\r
611       # <code>Irc::User</code>s onto <code>BotUser</code>s, and the other that maps\r
612       # usernames onto <code>BotUser</code>\r
613       def initialize\r
614         @everyone = Auth::defaultbotuser\r
615         @botowner = Auth::botowner\r
616         bot_associate(nil)\r
617       end\r
618 \r
619       def bot_associate(bot)\r
620         raise "Cannot associate with a new bot! Save first" if defined?(@has_changes) && @has_changes\r
621 \r
622         reset_hashes\r
623 \r
624         # Associated bot\r
625         @bot = bot\r
626 \r
627         # This variable is set to true when there have been changes\r
628         # to the botusers list, so that we know when to save\r
629         @has_changes = false\r
630       end\r
631 \r
632       def set_changed\r
633         @has_changes = true\r
634       end\r
635 \r
636       def reset_changed\r
637         @has_changes = false\r
638       end\r
639 \r
640       def changed?\r
641         @has_changes\r
642       end\r
643 \r
644       # resets the hashes\r
645       def reset_hashes\r
646         @botusers = Hash.new\r
647         @allbotusers = Hash.new\r
648         [everyone, botowner].each { |x|\r
649           @allbotusers[x.username.to_sym] = x\r
650         }\r
651         @transients = Set.new\r
652       end\r
653 \r
654       def load_array(ary, forced)\r
655         unless ary\r
656           warning "Tried to load an empty array"\r
657           return\r
658         end\r
659         raise "Won't load with unsaved changes" if @has_changes and not forced\r
660         reset_hashes\r
661         ary.each { |x|\r
662           raise TypeError, "#{x} should be a Hash" unless x.kind_of?(Hash)\r
663           u = x[:username]\r
664           unless include?(u)\r
665             create_botuser(u)\r
666           end\r
667           get_botuser(u).from_hash(x)\r
668           get_botuser(u).transient = false\r
669         }\r
670         @has_changes=false\r
671       end\r
672 \r
673       def save_array\r
674         @allbotusers.values.map { |x|\r
675           x.transient? ? nil : x.to_hash\r
676         }.compact\r
677       end\r
678 \r
679       # checks if we know about a certain BotUser username\r
680       def include?(botusername)\r
681         @allbotusers.has_key?(botusername.to_sym)\r
682       end\r
683 \r
684       # Maps <code>Irc::User</code> to BotUser\r
685       def irc_to_botuser(ircuser)\r
686         logged = @botusers[ircuser.to_irc_user]\r
687         return logged if logged\r
688         return autologin(ircuser)\r
689       end\r
690 \r
691       # creates a new BotUser\r
692       def create_botuser(name, password=nil)\r
693         n = BotUser.sanitize_username(name)\r
694         k = n.to_sym\r
695         raise "botuser #{n} exists" if include?(k)\r
696         bu = BotUser.new(n)\r
697         bu.password = password\r
698         @allbotusers[k] = bu\r
699         return bu\r
700       end\r
701 \r
702       # returns the botuser with name _name_\r
703       def get_botuser(name)\r
704         @allbotusers.fetch(BotUser.sanitize_username(name).to_sym)\r
705       end\r
706 \r
707       # Logs Irc::User _user_ in to BotUser _botusername_ with password _pwd_\r
708       #\r
709       # raises an error if _botusername_ is not a known BotUser username\r
710       #\r
711       # It is possible to autologin by Netmask, on request\r
712       #\r
713       def login(user, botusername, pwd=nil)\r
714         ircuser = user.to_irc_user\r
715         n = BotUser.sanitize_username(botusername)\r
716         k = n.to_sym\r
717         raise "No such BotUser #{n}" unless include?(k)\r
718         if @botusers.has_key?(ircuser)\r
719           return true if @botusers[ircuser].username == n\r
720           # TODO\r
721           # @botusers[ircuser].logout(ircuser)\r
722         end\r
723         bu = @allbotusers[k]\r
724         if bu.login(ircuser, pwd)\r
725           @botusers[ircuser] = bu\r
726           return true\r
727         end\r
728         return false\r
729       end\r
730 \r
731       # Tries to auto-login Irc::User _user_ by looking at the known botusers that allow autologin\r
732       # and trying to login without a password\r
733       #\r
734       def autologin(user)\r
735         ircuser = user.to_irc_user\r
736         debug "Trying to autologin #{ircuser}"\r
737         return @botusers[ircuser] if @botusers.has_key?(ircuser)\r
738         @allbotusers.each { |n, bu|\r
739           debug "Checking with #{n}"\r
740           return bu if bu.autologin? and login(ircuser, n)\r
741         }\r
742         # Check with transient users\r
743         @transients.each { |bu|\r
744           return bu if bu.login(ircuser)\r
745         }\r
746         # Finally, create a transient if we're set to allow it\r
747         if @bot.config['auth.autouser']\r
748           bu = create_transient_botuser(ircuser)\r
749           return bu\r
750         end\r
751         return everyone\r
752       end\r
753 \r
754       # Creates a new transient BotUser associated with Irc::User _user_,\r
755       # automatically logging him in\r
756       #\r
757       def create_transient_botuser(user)\r
758         ircuser = user.to_irc_user\r
759         bu = BotUser.new(ircuser, :transient => true, :masks => ircuser)\r
760         bu.login(ircuser)\r
761         @transients << bu\r
762         return bu\r
763       end\r
764 \r
765       # Checks if User _user_ can do _cmd_ on _chan_.\r
766       #\r
767       # Permission are checked in this order, until a true or false\r
768       # is returned:\r
769       # * associated BotUser on _chan_\r
770       # * associated BotUser on all channels\r
771       # * everyone on _chan_\r
772       # * everyone on all channels\r
773       #\r
774       def permit?(user, cmdtxt, channel=nil)\r
775         if user.class <= BotUser\r
776           botuser = user\r
777         else\r
778           botuser = irc_to_botuser(user)\r
779         end\r
780         cmd = cmdtxt.to_irc_auth_command\r
781 \r
782         chan = channel\r
783         case chan\r
784         when User\r
785           chan = "?"\r
786         when Channel\r
787           chan = chan.name\r
788         end\r
789 \r
790         allow = nil\r
791 \r
792         allow = botuser.permit?(cmd, chan) if chan\r
793         return allow unless allow.nil?\r
794         allow = botuser.permit?(cmd)\r
795         return allow unless allow.nil?\r
796 \r
797         unless botuser == everyone\r
798           allow = everyone.permit?(cmd, chan) if chan\r
799           return allow unless allow.nil?\r
800           allow = everyone.permit?(cmd)\r
801           return allow unless allow.nil?\r
802         end\r
803 \r
804         raise "Could not check permission for user #{user.inspect} to run #{cmdtxt.inspect} on #{chan.inspect}"\r
805       end\r
806 \r
807       # Checks if command _cmd_ is allowed to User _user_ on _chan_, optionally\r
808       # telling if the user is authorized\r
809       #\r
810       def allow?(cmdtxt, user, chan=nil)\r
811         if permit?(user, cmdtxt, chan)\r
812           return true\r
813         else\r
814           # cmds = cmdtxt.split('::')\r
815           # @bot.say chan, "you don't have #{cmds.last} (#{cmds.first}) permissions here" if chan\r
816           @bot.say chan, _("%{user}, you don't have '%{command}' permissions here") %\r
817                         {:user=>user, :command=>cmdtxt} if chan\r
818           return false\r
819         end\r
820       end\r
821 \r
822     end\r
823 \r
824     # Returns the only instance of AuthManagerClass\r
825     #\r
826     def Auth.authmanager\r
827       return AuthManagerClass.instance\r
828     end\r
829 \r
830   end\r
831 \r
832   class User\r
833 \r
834     # A convenience method to automatically found the botuser\r
835     # associated with the receiver\r
836     #\r
837     def botuser\r
838       Irc::Auth.authmanager.irc_to_botuser(self)\r
839     end\r
840 \r
841     # The botuser is used to store data associated with the\r
842     # given Irc::User\r
843     #\r
844     def data\r
845       self.botuser.data\r
846     end\r
847   end\r
848 \r
849 end\r