Class | Irc::Bot::Auth::ManagerClass |
In: |
lib/rbot/botuser.rb
|
Parent: | Object |
This is the ManagerClass singleton, used to manage Irc::User/Irc::Bot::Auth::BotUser connections and everything
bot | [R] | |
botowner | [R] | |
everyone | [R] | |
maskdb | [R] |
Checks if command cmd is allowed to User user on chan, optionally telling if the user is authorized
# 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
# 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
# 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
# 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)
# 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
# File lib/rbot/botuser.rb, line 752 752: def get_botuser(name) 753: @allbotusers.fetch(BotUser.sanitize_username(name).to_sym) 754: end
# 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
# 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
# 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
# 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:
# 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
resets the hashes
# 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
# 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