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