]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/botuser.rb
New Auth Framework, New Irc Framework: convenience methods for Irc::User to retrieve...
[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_reader :data\r
221       attr_writer :login_by_mask\r
222       attr_writer :autologin\r
223       attr_writer :transient\r
224 \r
225       # Checks if the BotUser is transient\r
226       def transient?\r
227         @transient\r
228       end\r
229 \r
230       # Checks if the BotUser is permanent (not transient)\r
231       def permanent?\r
232         !@permanent\r
233       end\r
234 \r
235       # Sets if the BotUser is permanent or not\r
236       def permanent=(bool)\r
237         @transient=!bool\r
238       end\r
239 \r
240       # Create a new BotUser with given username\r
241       def initialize(username, options={})\r
242         opts = {:transient => false}.merge(options)\r
243         @transient = opts[:transient]\r
244 \r
245         if @transient\r
246           @username = "*"\r
247           @username << BotUser.sanitize_username(username) if username and not username.to_s.empty?\r
248           @username << BotUser.sanitize_username(object_id)\r
249           reset_password\r
250           @login_by_mask=true\r
251           @autologin=true\r
252         else\r
253           @username = BotUser.sanitize_username(username)\r
254           @password = nil\r
255           reset_login_by_mask\r
256           reset_autologin\r
257         end\r
258 \r
259         @netmasks = NetmaskList.new\r
260         if opts.key?(:masks) and opts[:masks]\r
261           masks = opts[:masks]\r
262           masks = [masks] unless masks.respond_to?(:each)\r
263           masks.each { |m|\r
264             mask = m.to_irc_netmask\r
265             if @transient and User === m\r
266               mask.nick = "*"\r
267               mask.host = m.host.dup\r
268               mask.user = "*" + m.user.sub(/^\w?[^\w]+/,'')\r
269             end\r
270             add_netmask(mask) unless mask.to_s == "*"\r
271           }\r
272         end\r
273         raise "must provide a usable mask for transient BotUser #{@username}" if @transient and @netmasks.empty?\r
274 \r
275         @perm = {}\r
276 \r
277         @data = {}\r
278       end\r
279 \r
280       # Inspection\r
281       def inspect\r
282         str = "<#{self.class}:#{'0x%08x' % self.object_id}"\r
283         str << " (transient)" if @transient\r
284         str << ":"\r
285         str << " @username=#{@username.inspect}"\r
286         str << " @netmasks=#{@netmasks.inspect}"\r
287         str << " @perm=#{@perm.inspect}"\r
288         str << " @login_by_mask=#{@login_by_mask}"\r
289         str << " @autologin=#{@autologin}"\r
290         if @data.empty?\r
291           str << " no data"\r
292         else\r
293           str << " data for #{@data.keys.join(', ')}"\r
294         end\r
295         str << ">"\r
296       end\r
297 \r
298       # In strings\r
299       def to_s\r
300         @username\r
301       end\r
302 \r
303       # Convert into a hash\r
304       def to_hash\r
305         {\r
306           :username => @username,\r
307           :password => @password,\r
308           :netmasks => @netmasks,\r
309           :perm => @perm,\r
310           :login_by_mask => @login_by_mask,\r
311           :autologin => @autologin\r
312         }\r
313       end\r
314 \r
315       # Do we allow logging in without providing the password?\r
316       #\r
317       def login_by_mask?\r
318         @login_by_mask\r
319       end\r
320 \r
321       # Reset the login-by-mask option\r
322       #\r
323       def reset_login_by_mask\r
324         @login_by_mask = Auth.authmanager.bot.config['auth.login_by_mask'] unless defined?(@login_by_mask)\r
325       end\r
326 \r
327       # Reset the autologin option\r
328       #\r
329       def reset_autologin\r
330         @autologin = Auth.authmanager.bot.config['auth.autologin'] unless defined?(@autologin)\r
331       end\r
332 \r
333       # Do we allow automatic logging in?\r
334       #\r
335       def autologin?\r
336         @autologin\r
337       end\r
338 \r
339       # Restore from hash\r
340       def from_hash(h)\r
341         @username = h[:username] if h.has_key?(:username)\r
342         @password = h[:password] if h.has_key?(:password)\r
343         @netmasks = h[:netmasks] if h.has_key?(:netmasks)\r
344         @perm = h[:perm] if h.has_key?(:perm)\r
345         @login_by_mask = h[:login_by_mask] if h.has_key?(:login_by_mask)\r
346         @autologin = h[:autologin] if h.has_key?(:autologin)\r
347       end\r
348 \r
349       # This method sets the password if the proposed new password\r
350       # is valid\r
351       def password=(pwd=nil)\r
352         pass = pwd.to_s\r
353         if pass.empty?\r
354           reset_password\r
355         else\r
356           begin\r
357             raise InvalidPassword, "#{pass} contains invalid characters" if pass !~ /^[\x21-\x7e]+$/\r
358             raise InvalidPassword, "#{pass} too short" if pass.length < 4\r
359             @password = pass\r
360           rescue InvalidPassword => e\r
361             raise e\r
362           rescue => e\r
363             raise InvalidPassword, "Exception #{e.inspect} while checking #{pass.inspect} (#{pwd.inspect})"\r
364           end\r
365         end\r
366       end\r
367 \r
368       # Resets the password by creating a new onw\r
369       def reset_password\r
370         @password = Auth.random_password\r
371       end\r
372 \r
373       # Sets the permission for command _cmd_ to _val_ on channel _chan_\r
374       #\r
375       def set_permission(cmd, val, chan="*")\r
376         k = chan.to_s.to_sym\r
377         @perm[k] = PermissionSet.new unless @perm.has_key?(k)\r
378         @perm[k].set_permission(cmd, val)\r
379       end\r
380 \r
381       # Resets the permission for command _cmd_ on channel _chan_\r
382       #\r
383       def reset_permission(cmd, chan ="*")\r
384         set_permission(cmd, nil, chan)\r
385       end\r
386 \r
387       # Checks if BotUser is allowed to do something on channel _chan_,\r
388       # or on all channels if _chan_ is nil\r
389       #\r
390       def permit?(cmd, chan=nil)\r
391         if chan\r
392           k = chan.to_s.to_sym\r
393         else\r
394           k = :*\r
395         end\r
396         allow = nil\r
397         if @perm.has_key?(k)\r
398           allow = @perm[k].permit?(cmd)\r
399         end\r
400         return allow\r
401       end\r
402 \r
403       # Adds a Netmask\r
404       #\r
405       def add_netmask(mask)\r
406         @netmasks << mask.to_irc_netmask\r
407       end\r
408 \r
409       # Removes a Netmask\r
410       #\r
411       def delete_netmask(mask)\r
412         m = mask.to_irc_netmask\r
413         @netmasks.delete(m)\r
414       end\r
415 \r
416       # Removes all <code>Netmask</code>s\r
417       #\r
418       def reset_netmasks\r
419         @netmasks = NetmaskList.new\r
420       end\r
421 \r
422       # This method checks if BotUser has a Netmask that matches _user_\r
423       #\r
424       def knows?(usr)\r
425         user = usr.to_irc_user\r
426         known = false\r
427         @netmasks.each { |n|\r
428           if user.matches?(n)\r
429             known = true\r
430             break\r
431           end\r
432         }\r
433         return known\r
434       end\r
435 \r
436       # This method gets called when User _user_ wants to log in.\r
437       # It returns true or false depending on whether the password\r
438       # is right. If it is, the Netmask of the user is added to the\r
439       # list of acceptable Netmask unless it's already matched.\r
440       def login(user, password=nil)\r
441         if password == @password or (password.nil? and (@login_by_mask || @autologin) and knows?(user))\r
442           add_netmask(user) unless knows?(user)\r
443           debug "#{user} logged in as #{self.inspect}"\r
444           return true\r
445         else\r
446           return false\r
447         end\r
448       end\r
449 \r
450       # # This method gets called when User _user_ has logged out as this BotUser\r
451       # def logout(user)\r
452       #   delete_netmask(user) if knows?(user)\r
453       # end\r
454 \r
455       # This method sanitizes a username by chomping, downcasing\r
456       # and replacing any nonalphanumeric character with _\r
457       #\r
458       def BotUser.sanitize_username(name)\r
459         candidate = name.to_s.chomp.downcase.gsub(/[^a-z0-9]/,"_")\r
460         raise "sanitized botusername #{candidate} too short" if candidate.length < 3\r
461         return candidate\r
462       end\r
463 \r
464     end\r
465 \r
466     # This is the default BotUser: it's used for all users which haven't\r
467     # identified with the bot\r
468     #\r
469     class DefaultBotUserClass < BotUser\r
470 \r
471       private :add_netmask, :delete_netmask\r
472 \r
473       include Singleton\r
474 \r
475       # The default BotUser is named 'everyone'\r
476       #\r
477       def initialize\r
478         reset_login_by_mask\r
479         reset_autologin\r
480         super("everyone")\r
481         @default_perm = PermissionSet.new\r
482       end\r
483 \r
484       # This method returns without changing anything\r
485       #\r
486       def login_by_mask=(val)\r
487         debug "Tried to change the login-by-mask for default bot user, ignoring"\r
488         return @login_by_mask\r
489       end\r
490 \r
491       # The default botuser allows logins by mask\r
492       #\r
493       def reset_login_by_mask\r
494         @login_by_mask = true\r
495       end\r
496 \r
497       # This method returns without changing anything\r
498       #\r
499       def autologin=(val)\r
500         debug "Tried to change the autologin for default bot user, ignoring"\r
501         return\r
502       end\r
503 \r
504       # The default botuser doesn't allow autologin (meaningless)\r
505       #\r
506       def reset_autologin\r
507         @autologin = false\r
508       end\r
509 \r
510       # Sets the default permission for the default user (i.e. the ones\r
511       # set by the BotModule writers) on all channels\r
512       #\r
513       def set_default_permission(cmd, val)\r
514         @default_perm.set_permission(Command.new(cmd), val)\r
515         debug "Default permissions now: #{@default_perm.pretty_inspect}"\r
516       end\r
517 \r
518       # default knows everybody\r
519       #\r
520       def knows?(user)\r
521         return true if user.to_irc_user\r
522       end\r
523 \r
524       # We always allow logging in as the default user\r
525       def login(user, password)\r
526         return true\r
527       end\r
528 \r
529       # Resets the NetmaskList\r
530       def reset_netmasks\r
531         super\r
532         add_netmask("*!*@*")\r
533       end\r
534 \r
535       # DefaultBotUser will check the default_perm after checking\r
536       # the global ones\r
537       # or on all channels if _chan_ is nil\r
538       #\r
539       def permit?(cmd, chan=nil)\r
540         allow = super(cmd, chan)\r
541         if allow.nil? && chan.nil?\r
542           allow = @default_perm.permit?(cmd)\r
543         end\r
544         return allow\r
545       end\r
546 \r
547     end\r
548 \r
549     # Returns the only instance of DefaultBotUserClass\r
550     #\r
551     def Auth.defaultbotuser\r
552       return DefaultBotUserClass.instance\r
553     end\r
554 \r
555     # This is the BotOwner: he can do everything\r
556     #\r
557     class BotOwnerClass < BotUser\r
558 \r
559       include Singleton\r
560 \r
561       def initialize\r
562         @login_by_mask = false\r
563         @autologin = true\r
564         super("owner")\r
565       end\r
566 \r
567       def permit?(cmd, chan=nil)\r
568         return true\r
569       end\r
570 \r
571     end\r
572 \r
573     # Returns the only instance of BotOwnerClass\r
574     #\r
575     def Auth.botowner\r
576       return BotOwnerClass.instance\r
577     end\r
578 \r
579 \r
580     # This is the AuthManagerClass singleton, used to manage User/BotUser connections and\r
581     # everything\r
582     #\r
583     class AuthManagerClass\r
584 \r
585       include Singleton\r
586 \r
587       attr_reader :everyone\r
588       attr_reader :botowner\r
589       attr_reader :bot\r
590 \r
591       # The instance manages two <code>Hash</code>es: one that maps\r
592       # <code>Irc::User</code>s onto <code>BotUser</code>s, and the other that maps\r
593       # usernames onto <code>BotUser</code>\r
594       def initialize\r
595         @everyone = Auth::defaultbotuser\r
596         @botowner = Auth::botowner\r
597         bot_associate(nil)\r
598       end\r
599 \r
600       def bot_associate(bot)\r
601         raise "Cannot associate with a new bot! Save first" if defined?(@has_changes) && @has_changes\r
602 \r
603         reset_hashes\r
604 \r
605         # Associated bot\r
606         @bot = bot\r
607 \r
608         # This variable is set to true when there have been changes\r
609         # to the botusers list, so that we know when to save\r
610         @has_changes = false\r
611       end\r
612 \r
613       def set_changed\r
614         @has_changes = true\r
615       end\r
616 \r
617       def reset_changed\r
618         @has_changes = false\r
619       end\r
620 \r
621       def changed?\r
622         @has_changes\r
623       end\r
624 \r
625       # resets the hashes\r
626       def reset_hashes\r
627         @botusers = Hash.new\r
628         @allbotusers = Hash.new\r
629         [everyone, botowner].each { |x|\r
630           @allbotusers[x.username.to_sym] = x\r
631         }\r
632         @transients = Set.new\r
633       end\r
634 \r
635       def load_array(ary, forced)\r
636         unless ary\r
637           warning "Tried to load an empty array"\r
638           return\r
639         end\r
640         raise "Won't load with unsaved changes" if @has_changes and not forced\r
641         reset_hashes\r
642         ary.each { |x|\r
643           raise TypeError, "#{x} should be a Hash" unless x.kind_of?(Hash)\r
644           u = x[:username]\r
645           unless include?(u)\r
646             create_botuser(u)\r
647           end\r
648           get_botuser(u).from_hash(x)\r
649           get_botuser(u).transient = false\r
650         }\r
651         @has_changes=false\r
652       end\r
653 \r
654       def save_array\r
655         @allbotusers.values.map { |x|\r
656           x.transient? ? nil : x.to_hash\r
657         }.compact\r
658       end\r
659 \r
660       # checks if we know about a certain BotUser username\r
661       def include?(botusername)\r
662         @allbotusers.has_key?(botusername.to_sym)\r
663       end\r
664 \r
665       # Maps <code>Irc::User</code> to BotUser\r
666       def irc_to_botuser(ircuser)\r
667         logged = @botusers[ircuser.to_irc_user]\r
668         return logged if logged\r
669         return autologin(ircuser)\r
670       end\r
671 \r
672       # creates a new BotUser\r
673       def create_botuser(name, password=nil)\r
674         n = BotUser.sanitize_username(name)\r
675         k = n.to_sym\r
676         raise "botuser #{n} exists" if include?(k)\r
677         bu = BotUser.new(n)\r
678         bu.password = password\r
679         @allbotusers[k] = bu\r
680         return bu\r
681       end\r
682 \r
683       # returns the botuser with name _name_\r
684       def get_botuser(name)\r
685         @allbotusers.fetch(BotUser.sanitize_username(name).to_sym)\r
686       end\r
687 \r
688       # Logs Irc::User _user_ in to BotUser _botusername_ with password _pwd_\r
689       #\r
690       # raises an error if _botusername_ is not a known BotUser username\r
691       #\r
692       # It is possible to autologin by Netmask, on request\r
693       #\r
694       def login(user, botusername, pwd=nil)\r
695         ircuser = user.to_irc_user\r
696         n = BotUser.sanitize_username(botusername)\r
697         k = n.to_sym\r
698         raise "No such BotUser #{n}" unless include?(k)\r
699         if @botusers.has_key?(ircuser)\r
700           return true if @botusers[ircuser].username == n\r
701           # TODO\r
702           # @botusers[ircuser].logout(ircuser)\r
703         end\r
704         bu = @allbotusers[k]\r
705         if bu.login(ircuser, pwd)\r
706           @botusers[ircuser] = bu\r
707           return true\r
708         end\r
709         return false\r
710       end\r
711 \r
712       # Tries to auto-login Irc::User _user_ by looking at the known botusers that allow autologin\r
713       # and trying to login without a password\r
714       #\r
715       def autologin(user)\r
716         ircuser = user.to_irc_user\r
717         debug "Trying to autologin #{ircuser}"\r
718         return @botusers[ircuser] if @botusers.has_key?(ircuser)\r
719         @allbotusers.each { |n, bu|\r
720           debug "Checking with #{n}"\r
721           return bu if bu.autologin? and login(ircuser, n)\r
722         }\r
723         # Check with transient users\r
724         @transients.each { |bu|\r
725           return bu if bu.login(ircuser)\r
726         }\r
727         # Finally, create a transient if we're set to allow it\r
728         if @bot.config['auth.autouser']\r
729           bu = create_transient_botuser(ircuser)\r
730           return bu\r
731         end\r
732         return everyone\r
733       end\r
734 \r
735       # Creates a new transient BotUser associated with Irc::User _user_,\r
736       # automatically logging him in\r
737       #\r
738       def create_transient_botuser(user)\r
739         ircuser = user.to_irc_user\r
740         bu = BotUser.new(ircuser, :transient => true, :masks => ircuser)\r
741         bu.login(ircuser)\r
742         @transients << bu\r
743         return bu\r
744       end\r
745 \r
746       # Checks if User _user_ can do _cmd_ on _chan_.\r
747       #\r
748       # Permission are checked in this order, until a true or false\r
749       # is returned:\r
750       # * associated BotUser on _chan_\r
751       # * associated BotUser on all channels\r
752       # * everyone on _chan_\r
753       # * everyone on all channels\r
754       #\r
755       def permit?(user, cmdtxt, channel=nil)\r
756         if user.class <= BotUser\r
757           botuser = user\r
758         else\r
759           botuser = irc_to_botuser(user)\r
760         end\r
761         cmd = cmdtxt.to_irc_auth_command\r
762 \r
763         chan = channel\r
764         case chan\r
765         when User\r
766           chan = "?"\r
767         when Channel\r
768           chan = chan.name\r
769         end\r
770 \r
771         allow = nil\r
772 \r
773         allow = botuser.permit?(cmd, chan) if chan\r
774         return allow unless allow.nil?\r
775         allow = botuser.permit?(cmd)\r
776         return allow unless allow.nil?\r
777 \r
778         unless botuser == everyone\r
779           allow = everyone.permit?(cmd, chan) if chan\r
780           return allow unless allow.nil?\r
781           allow = everyone.permit?(cmd)\r
782           return allow unless allow.nil?\r
783         end\r
784 \r
785         raise "Could not check permission for user #{user.inspect} to run #{cmdtxt.inspect} on #{chan.inspect}"\r
786       end\r
787 \r
788       # Checks if command _cmd_ is allowed to User _user_ on _chan_, optionally\r
789       # telling if the user is authorized\r
790       #\r
791       def allow?(cmdtxt, user, chan=nil)\r
792         if permit?(user, cmdtxt, chan)\r
793           return true\r
794         else\r
795           # cmds = cmdtxt.split('::')\r
796           # @bot.say chan, "you don't have #{cmds.last} (#{cmds.first}) permissions here" if chan\r
797           @bot.say chan, _("%{user}, you don't have '%{command}' permissions here") %\r
798                         {:user=>user, :command=>cmdtxt} if chan\r
799           return false\r
800         end\r
801       end\r
802 \r
803     end\r
804 \r
805     # Returns the only instance of AuthManagerClass\r
806     #\r
807     def Auth.authmanager\r
808       return AuthManagerClass.instance\r
809     end\r
810 \r
811   end\r
812 \r
813   class User\r
814 \r
815     # A convenience method to automatically found the botuser\r
816     # associated with the receiver\r
817     #\r
818     def botuser\r
819       Irc::Auth.authmanager.irc_to_botuser(self)\r
820     end\r
821 \r
822     # The botuser is used to store data associated with the\r
823     # given Irc::User\r
824     #\r
825     def data\r
826       self.botuser.data\r
827     end\r
828   end\r
829 \r
830 end\r