Class Irc::Bot::Auth::ManagerClass
In: lib/rbot/botuser.rb
Parent: Object
BasicUserMessage JoinMessage NamesMessage WhoisMessage ModeChangeMessage KickMessage MotdMessage QuitMessage BanlistMessage UserMessage NoSuchTargetMessage TopicMessage NickMessage WelcomeMessage UnknownMessage InviteMessage PartMessage NetmaskList UserList ArrayOf ChannelList Netmask User\n[lib/rbot/botuser.rb\nlib/rbot/irc.rb] Channel Singleton RfcCasemap StrictRfcCasemap AsciiCasemap Casemap PrivMessage NoticeMessage TokyoCabinet::BDB CIBDB Btree CIBtree Socket MessageQueue QueueRing Client DBHash\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] DBTree\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] Server NetmaskDb Bot\n[lib/rbot/botuser.rb\nlib/rbot/config.rb\nlib/rbot/ircbot.rb\nlib/rbot/language.rb\nlib/rbot/message.rb\nlib/rbot/messagemapper.rb\nlib/rbot/plugins.rb\nlib/rbot/rbotconfig.rb\nlib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] lib/rbot/ircsocket.rb lib/rbot/rfc2812.rb lib/rbot/registry/tc.rb lib/rbot/irc.rb lib/rbot/maskdb.rb lib/rbot/message.rb lib/rbot/messagemapper.rb lib/rbot/botuser.rb lib/rbot/registry/tc.rb (null) BotConfig PKGConfig ServerOrCasemap Irc dot/m_35_0.png

This is the ManagerClass singleton, used to manage Irc::User/Irc::Bot::Auth::BotUser connections and everything

Methods

Included Modules

Singleton

Attributes

bot  [R] 
botowner  [R] 
everyone  [R] 
maskdb  [R] 

Public Class methods

The instance manages two Hashes: one that maps Irc::Users onto BotUsers, and the other that maps usernames onto BotUser

[Source]

     # File lib/rbot/botuser.rb, line 662
662:       def initialize
663:         @everyone = Auth::defaultbotuser
664:         @botowner = Auth::botowner
665:         bot_associate(nil)
666:       end

Public Instance methods

Checks if command cmd is allowed to User user on chan, optionally telling if the user is authorized

[Source]

     # File lib/rbot/botuser.rb, line 907
907:       def allow?(cmdtxt, user, chan=nil)
908:         if permit?(user, cmdtxt, chan)
909:           return true
910:         else
911:           # cmds = cmdtxt.split('::')
912:           # @bot.say chan, "you don't have #{cmds.last} (#{cmds.first}) permissions here" if chan
913:           @bot.say chan, _("%{user}, you don't have '%{command}' permissions here") %
914:                         {:user=>user, :command=>cmdtxt} if chan
915:           return false
916:         end
917:       end

Tries to auto-login Irc::User user by looking at the known botusers that allow autologin and trying to login without a password

[Source]

     # File lib/rbot/botuser.rb, line 783
783:       def autologin(user)
784:         ircuser = user.to_irc_user
785:         debug "Trying to autologin #{ircuser}"
786:         return @botusers[ircuser] if @botusers.has_key?(ircuser)
787:         bu = maskdb.find(ircuser)
788:         if bu
789:           debug "trying #{bu}"
790:           bu.login(ircuser) or raise '...what?!'
791:           @botusers[ircuser] = bu
792:           return bu
793:         end
794:         # Finally, create a transient if we're set to allow it
795:         if @bot.config['auth.autouser']
796:           bu = create_transient_botuser(ircuser)
797:           @botusers[ircuser] = bu
798:           return bu
799:         end
800:         return everyone
801:       end

[Source]

     # File lib/rbot/botuser.rb, line 668
668:       def bot_associate(bot)
669:         raise "Cannot associate with a new bot! Save first" if defined?(@has_changes) && @has_changes
670: 
671:         reset_hashes
672: 
673:         # Associated bot
674:         @bot = bot
675: 
676:         # This variable is set to true when there have been changes
677:         # to the botusers list, so that we know when to save
678:         @has_changes = false
679:       end

[Source]

     # File lib/rbot/botuser.rb, line 689
689:       def changed?
690:         @has_changes
691:       end

creates a new BotUser

[Source]

     # File lib/rbot/botuser.rb, line 741
741:       def create_botuser(name, password=nil)
742:         n = BotUser.sanitize_username(name)
743:         k = n.to_sym
744:         raise "botuser #{n} exists" if include?(k)
745:         bu = BotUser.new(n)
746:         bu.password = password
747:         @allbotusers[k] = bu
748:         return bu
749:       end

Creates a new transient BotUser associated with Irc::User user, automatically logging him in. Note that transient botuser creation can fail, typically if we don‘t have the complete user netmask (e.g. for messages coming in from a linkbot)

[Source]

     # File lib/rbot/botuser.rb, line 808
808:       def create_transient_botuser(user)
809:         ircuser = user.to_irc_user
810:         bu = everyone
811:         begin
812:           bu = BotUser.new(ircuser, :transient => true, :masks => ircuser)
813:           bu.login(ircuser)
814:         rescue
815:           warning "failed to create transient for #{user}"
816:           error $!
817:         end
818:         return bu
819:       end

returns the botuser with name name

[Source]

     # File lib/rbot/botuser.rb, line 752
752:       def get_botuser(name)
753:         @allbotusers.fetch(BotUser.sanitize_username(name).to_sym)
754:       end

checks if we know about a certain BotUser username

[Source]

     # File lib/rbot/botuser.rb, line 729
729:       def include?(botusername)
730:         @allbotusers.has_key?(botusername.to_sym)
731:       end

Maps Irc::User to BotUser

[Source]

     # File lib/rbot/botuser.rb, line 734
734:       def irc_to_botuser(ircuser)
735:         logged = @botusers[ircuser.to_irc_user]
736:         return logged if logged
737:         return autologin(ircuser)
738:       end

[Source]

     # File lib/rbot/botuser.rb, line 703
703:       def load_array(ary, forced)
704:         unless ary
705:           warning "Tried to load an empty array"
706:           return
707:         end
708:         raise "Won't load with unsaved changes" if @has_changes and not forced
709:         reset_hashes
710:         ary.each { |x|
711:           raise TypeError, "#{x} should be a Hash" unless x.kind_of?(Hash)
712:           u = x[:username]
713:           unless include?(u)
714:             create_botuser(u)
715:           end
716:           get_botuser(u).from_hash(x)
717:           get_botuser(u).transient = false
718:         }
719:         @has_changes=false
720:       end

Logs Irc::User user in to BotUser botusername with password pwd

raises an error if botusername is not a known BotUser username

It is possible to autologin by Netmask, on request

[Source]

     # File lib/rbot/botuser.rb, line 762
762:       def login(user, botusername, pwd=nil)
763:         ircuser = user.to_irc_user
764:         n = BotUser.sanitize_username(botusername)
765:         k = n.to_sym
766:         raise "No such BotUser #{n}" unless include?(k)
767:         if @botusers.has_key?(ircuser)
768:           return true if @botusers[ircuser].username == n
769:           # TODO
770:           # @botusers[ircuser].logout(ircuser)
771:         end
772:         bu = @allbotusers[k]
773:         if bu.login(ircuser, pwd)
774:           @botusers[ircuser] = bu
775:           return true
776:         end
777:         return false
778:       end

Logs out any Irc::User matching Irc::Netmask m and logged in to a transient BotUser

[Source]

     # File lib/rbot/botuser.rb, line 824
824:       def logout_transients(m)
825:         debug "to check: #{@botusers.keys.join ' '}"
826:         @botusers.keys.each do |iu|
827:           debug "checking #{iu.fullform} against #{m.fullform}"
828:           bu = @botusers[iu]
829:           bu.transient? or next
830:           iu.matches?(m) or next
831:           @botusers.delete(iu).autologin = false
832:         end
833:       end

Makes transient BotUser user into a permanent BotUser named name; if user is an Irc::User, act on the transient BotUser (if any) it‘s logged in as

[Source]

     # File lib/rbot/botuser.rb, line 839
839:       def make_permanent(user, name)
840:         buname = BotUser.sanitize_username(name)
841:         # TODO merge BotUser instead?
842:         raise "there's already a BotUser called #{name}" if include?(buname)
843: 
844:         tuser = nil
845:         case user
846:         when String, Irc::User
847:           tuser = irc_to_botuser(user)
848:         when BotUser
849:           tuser = user
850:         else
851:           raise TypeError, "sorry, don't know how to make #{user.class} into a permanent BotUser"
852:         end
853:         return nil unless tuser
854:         raise TypeError, "#{tuser} is not transient" unless tuser.transient?
855: 
856:         tuser.make_permanent(buname)
857:         @allbotusers[tuser.username.to_sym] = tuser
858: 
859:         return tuser
860:       end

Checks if User user can do cmd on chan.

Permission are checked in this order, until a true or false is returned:

  • associated BotUser on chan
  • associated BotUser on all channels
  • everyone on chan
  • everyone on all channels

[Source]

     # File lib/rbot/botuser.rb, line 871
871:       def permit?(user, cmdtxt, channel=nil)
872:         if user.class <= BotUser
873:           botuser = user
874:         else
875:           botuser = irc_to_botuser(user)
876:         end
877:         cmd = cmdtxt.to_irc_auth_command
878: 
879:         chan = channel
880:         case chan
881:         when User
882:           chan = "?"
883:         when Channel
884:           chan = chan.name
885:         end
886: 
887:         allow = nil
888: 
889:         allow = botuser.permit?(cmd, chan) if chan
890:         return allow unless allow.nil?
891:         allow = botuser.permit?(cmd)
892:         return allow unless allow.nil?
893: 
894:         unless botuser == everyone
895:           allow = everyone.permit?(cmd, chan) if chan
896:           return allow unless allow.nil?
897:           allow = everyone.permit?(cmd)
898:           return allow unless allow.nil?
899:         end
900: 
901:         raise "Could not check permission for user #{user.inspect} to run #{cmdtxt.inspect} on #{chan.inspect}"
902:       end

[Source]

     # File lib/rbot/botuser.rb, line 685
685:       def reset_changed
686:         @has_changes = false
687:       end

resets the hashes

[Source]

     # File lib/rbot/botuser.rb, line 694
694:       def reset_hashes
695:         @botusers = Hash.new
696:         @maskdb = NetmaskDb.new
697:         @allbotusers = Hash.new
698:         [everyone, botowner].each do |x|
699:           @allbotusers[x.username.to_sym] = x
700:         end
701:       end

[Source]

     # File lib/rbot/botuser.rb, line 722
722:       def save_array
723:         @allbotusers.values.map { |x|
724:           x.transient? ? nil : x.to_hash
725:         }.compact
726:       end

[Source]

     # File lib/rbot/botuser.rb, line 681
681:       def set_changed
682:         @has_changes = true
683:       end

[Validate]