Module | FacebookLogin |
In: |
facebook_bot/login.rb
|
Handles all login-related functionality to Facebook. This is a staple. Can‘t do much on facebook without logging in!
Tests whether we have logged in successfully or not via cookies. And love.
if !fb.logged_in? raise 'you dunce!' end
# File facebook_bot/login.rb, line 74 74: def logged_in? 75: @opts[:headers]['Cookie'] =~ /login/i 76: end
Login to facebook. Doesn‘t take any arguments and is generally not supposed to be called directly. When you create a new Facebook object, it will automatically try_try_again to login. It reads the username and password straight from @opts. Probably is pretty insecure, since it uses SSL to send your request. Will update session cookie upon successful login, to be used throughout the rest of the functions. You‘ll want to call this function a lot, it will only try to login if
fb.login
# File facebook_bot/login.rb, line 12 12: def login 13: connect # to reset any weird-o connections we might have done in the past. 14: if @opts[:cookie] 15: @opts[:headers]['Cookie'] = @opts[:cookie] 16: req = @http.get2('/', @opts[:headers]) 17: if req.code.to_i == 302 18: puts "Successfully used cookie to login!" 19: @default_host = URI.parse(req.response['location']).host 20: else 21: puts "Failed to login with cookie!" 22: log req 23: end 24: else 25: #only login if we haven't in the last minute 26: if @last_login.nil? || @last_login+60 < Time.now.to_i 27: #logout 28: connect 'www.facebook.com' #reconnect to basic address. 29: @opts[:headers]['Cookie'] = 'test_cookie=1' #clear cookies to logout 30: # get the login challenge 31: doc = hpricot_get_url '/' 32: challenge = doc.at("input[@name='challenge']").attributes['value'] 33: 34: #login ! 35: loginhttp = Net::HTTP.new('login.facebook.com', 443) 36: loginhttp.use_ssl = true 37: loginhttp.verify_mode = OpenSSL::SSL::VERIFY_NONE # disables pesky warnings 38: 39: login = loginhttp.post2('/login.php', "md5pass=&noerror=0&email=#{@opts[:email]}&pass=#{@opts[:password]}&doquicklogin=Login&challenge=#{challenge}", @opts[:headers]) 40: 41: #302 redirect == successful login! 42: if login.code.to_i == 302 43: @default_host = URI.parse(login.response['location']).host 44: connect @default_host 45: puts "Successfully logged in!" 46: @last_login = Time.now.to_i 47: update_session login.response['set-cookie'] 48: else 49: puts "Failed to login!" 50: log login 51: end 52: end 53: end 54: end
Update the login session with new cookies.
# File facebook_bot/login.rb, line 57 57: def update_session cookie 58: if !cookie.nil? 59: #sanatize cookie 60: cookie.gsub!(/path=\/; /,'') 61: cookie.gsub!(/httponly, /,'') 62: cookie.gsub!(/domain=\.facebook.com; /,'') 63: cookie.gsub!(/expires=[^;]+;/,'') 64: cookie.gsub!(/path=\//,'') 65: cookie.gsub!(/\s+/,' ') 66: @opts[:headers]['Cookie'] = cookie 67: end 68: end