Class: Irc::Bot::Auth::ManagerClass

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb

Overview

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Singleton

#_dump

Constructor Details

- (ManagerClass) initialize

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



663
664
665
666
667
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 663

def initialize
  @everyone = Auth::defaultbotuser
  @botowner = Auth::botowner
  bot_associate(nil)
end

Instance Attribute Details

- (Object) bot (readonly)

Returns the value of attribute bot



658
659
660
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 658

def bot
  @bot
end

- (Object) botowner (readonly)

Returns the value of attribute botowner



657
658
659
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 657

def botowner
  @botowner
end

- (Object) everyone (readonly)

Returns the value of attribute everyone



656
657
658
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 656

def everyone
  @everyone
end

- (Object) maskdb (readonly)

Returns the value of attribute maskdb



655
656
657
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 655

def maskdb
  @maskdb
end

Instance Method Details

- (Boolean) allow?(cmdtxt, user, chan = nil)

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

Returns:

  • (Boolean)


908
909
910
911
912
913
914
915
916
917
918
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 908

def allow?(cmdtxt, user, chan=nil)
  if permit?(user, cmdtxt, chan)
    return true
  else
    # cmds = cmdtxt.split('::')
    # @bot.say chan, "you don't have #{cmds.last} (#{cmds.first}) permissions here" if chan
    @bot.say chan, _("%{user}, you don't have '%{command}' permissions here") %
                  {:user=>user, :command=>cmdtxt} if chan
    return false
  end
end

- (Object) autologin(user)

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



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 784

def autologin(user)
  ircuser = user.to_irc_user
  debug "Trying to autologin #{ircuser}"
  return @botusers[ircuser] if @botusers.has_key?(ircuser)
  bu = maskdb.find(ircuser)
  if bu
    debug "trying #{bu}"
    bu.(ircuser) or raise '...what?!'
    @botusers[ircuser] = bu
    return bu
  end
  # Finally, create a transient if we're set to allow it
  if @bot.config['auth.autouser']
    bu = create_transient_botuser(ircuser)
    @botusers[ircuser] = bu
    return bu
  end
  return everyone
end

- (Object) bot_associate(bot)



669
670
671
672
673
674
675
676
677
678
679
680
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 669

def bot_associate(bot)
  raise "Cannot associate with a new bot! Save first" if defined?(@has_changes) && @has_changes

  reset_hashes

  # Associated bot
  @bot = bot

  # This variable is set to true when there have been changes
  # to the botusers list, so that we know when to save
  @has_changes = false
end

- (Boolean) changed?

Returns:

  • (Boolean)


690
691
692
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 690

def changed?
  @has_changes
end

- (Object) create_botuser(name, password = nil)

creates a new BotUser



742
743
744
745
746
747
748
749
750
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 742

def create_botuser(name, password=nil)
  n = BotUser.sanitize_username(name)
  k = n.to_sym
  raise "botuser #{n} exists" if include?(k)
  bu = BotUser.new(n)
  bu.password = password
  @allbotusers[k] = bu
  return bu
end

- (Object) create_transient_botuser(user)

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)



809
810
811
812
813
814
815
816
817
818
819
820
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 809

def create_transient_botuser(user)
  ircuser = user.to_irc_user
  bu = everyone
  begin
    bu = BotUser.new(ircuser, :transient => true, :masks => ircuser)
    bu.(ircuser)
  rescue
    warning "failed to create transient for #{user}"
    error $!
  end
  return bu
end

- (Object) get_botuser(name)

returns the botuser with name name



753
754
755
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 753

def get_botuser(name)
  @allbotusers.fetch(BotUser.sanitize_username(name).to_sym)
end

- (Boolean) include?(botusername)

checks if we know about a certain BotUser username

Returns:

  • (Boolean)


730
731
732
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 730

def include?(botusername)
  @allbotusers.has_key?(botusername.to_sym)
end

- (Object) irc_to_botuser(ircuser)

Maps Irc::User to BotUser



735
736
737
738
739
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 735

def irc_to_botuser(ircuser)
  logged = @botusers[ircuser.to_irc_user]
  return logged if logged
  return autologin(ircuser)
end

- (Object) load_array(ary, forced)



704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 704

def load_array(ary, forced)
  unless ary
    warning "Tried to load an empty array"
    return
  end
  raise "Won't load with unsaved changes" if @has_changes and not forced
  reset_hashes
  ary.each { |x|
    raise TypeError, "#{x} should be a Hash" unless x.kind_of?(Hash)
    u = x[:username]
    unless include?(u)
      create_botuser(u)
    end
    get_botuser(u).from_hash(x)
    get_botuser(u).transient = false
  }
  @has_changes=false
end

- (Object) login(user, botusername, pwd = nil)

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



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 763

def (user, botusername, pwd=nil)
  ircuser = user.to_irc_user
  n = BotUser.sanitize_username(botusername)
  k = n.to_sym
  raise "No such BotUser #{n}" unless include?(k)
  if @botusers.has_key?(ircuser)
    return true if @botusers[ircuser].username == n
    # TODO
    # @botusers[ircuser].logout(ircuser)
  end
  bu = @allbotusers[k]
  if bu.(ircuser, pwd)
    @botusers[ircuser] = bu
    return true
  end
  return false
end

- (Object) logout_transients(m)

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



825
826
827
828
829
830
831
832
833
834
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 825

def logout_transients(m)
  debug "to check: #{@botusers.keys.join ' '}"
  @botusers.keys.each do |iu|
    debug "checking #{iu.fullform} against #{m.fullform}"
    bu = @botusers[iu]
    bu.transient? or next
    iu.matches?(m) or next
    @botusers.delete(iu).autologin = false
  end
end

- (Object) make_permanent(user, name)

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

Raises:

  • (TypeError)


840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 840

def make_permanent(user, name)
  buname = BotUser.sanitize_username(name)
  # TODO merge BotUser instead?
  raise "there's already a BotUser called #{name}" if include?(buname)

  tuser = nil
  case user
  when String, Irc::User
    tuser = irc_to_botuser(user)
  when BotUser
    tuser = user
  else
    raise TypeError, "sorry, don't know how to make #{user.class} into a permanent BotUser"
  end
  return nil unless tuser
  raise TypeError, "#{tuser} is not transient" unless tuser.transient?

  tuser.make_permanent(buname)
  @allbotusers[tuser.username.to_sym] = tuser

  return tuser
end

- (Boolean) permit?(user, cmdtxt, channel = nil)

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

Returns:

  • (Boolean)


872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 872

def permit?(user, cmdtxt, channel=nil)
  if user.class <= BotUser
    botuser = user
  else
    botuser = user.botuser
  end
  cmd = cmdtxt.to_irc_auth_command

  chan = channel
  case chan
  when User
    chan = "?"
  when Channel
    chan = chan.name
  end

  allow = nil

  allow = botuser.permit?(cmd, chan) if chan
  return allow unless allow.nil?
  allow = botuser.permit?(cmd)
  return allow unless allow.nil?

  unless botuser == everyone
    allow = everyone.permit?(cmd, chan) if chan
    return allow unless allow.nil?
    allow = everyone.permit?(cmd)
    return allow unless allow.nil?
  end

  raise "Could not check permission for user #{user.inspect} to run #{cmdtxt.inspect} on #{chan.inspect}"
end

- (Object) reset_changed



686
687
688
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 686

def reset_changed
  @has_changes = false
end

- (Object) reset_hashes

resets the hashes



695
696
697
698
699
700
701
702
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 695

def reset_hashes
  @botusers = Hash.new
  @maskdb = NetmaskDb.new
  @allbotusers = Hash.new
  [everyone, botowner].each do |x|
    @allbotusers[x.username.to_sym] = x
  end
end

- (Object) save_array



723
724
725
726
727
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 723

def save_array
  @allbotusers.values.map { |x|
    x.transient? ? nil : x.to_hash
  }.compact
end

- (Object) set_changed



682
683
684
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 682

def set_changed
  @has_changes = true
end