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