Class: Contacts::Hotmail

Inherits:
Base
  • Object
show all
Defined in:
lib/contacts/hotmail.rb

Constant Summary collapse

URL =
"https://login.live.com/login.srf?id=2"
CONTACT_LIST_URL =
"https://mail.live.com/mail/GetContacts.aspx"
PROTOCOL_ERROR =
"Hotmail has changed its protocols, please upgrade this library first. If that does not work, report this error at http://rubyforge.org/forum/?group_id=2693"
PWDPAD =
"IfYouAreReadingThisYouHaveTooMuchFreeTime"

Instance Method Summary collapse

Methods inherited from Base

#connect, #connected?, #initialize, #login, #login_with_domain, #login_without_domain, #password

Constructor Details

This class inherits a constructor from Contacts::Base

Instance Method Details

#contacts(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/contacts/hotmail.rb', line 59

def contacts(options = {})
  if @contacts.nil? && connected?
    url = URI.parse(contact_list_url)
    data, resp, cookies, forward = get(get_contact_list_url, @cookies )
    data.force_encoding('gbk')
    data = data.encode('UTF-8')
    data.gsub!(";",",")
    data.gsub!("'","")
    if data =~ /^<html><head><title>Object\s+moved<\/title><\/head>/
      url = data.scan(/<a\s+href=[\"\'](.*)[\'\"]/).flatten.first
      data, resp, cookies, forward = get(url, @cookies )
      data.force_encoding('gbk')
      data = data.encode('UTF-8')
    end
    @contacts = CSV.parse(data, {:headers => true, :col_sep => ','}).map do |row|
      name = ""
      name = row["First Name"] if !row["First Name"].nil?
      name << " #{row["Last Name"]}" if !row["Last Name"].nil?
      email = row["E-mail Address"] || ""
      [name, email]
    end
    @contacts.delete_if{|x| x[1].blank?}
  else
    @contacts || []
  end
end

#real_connectObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contacts/hotmail.rb', line 11

def real_connect

  data, resp, cookies, forward = get(URL)
  old_url = URL
  until forward.nil?
    data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
  end

  postdata =  "PPSX=%s&PwdPad=%s&login=%s&passwd=%s&LoginOptions=2&PPFT=%s" % [
    CGI.escape(data.split("><").grep(/PPSX/).first[/=\S+$/][2..-3]),
    PWDPAD[0...(PWDPAD.length-@password.length)],
    CGI.escape(),
    CGI.escape(password),
    CGI.escape(data.split("><").grep(/PPFT/).first[/=\S+$/][2..-3])
  ]

  form_url = data.split("><").grep(/form/).first.split[5][8..-2]
  data, resp, cookies, forward = post(form_url, postdata, cookies)

  old_url = form_url
  until cookies =~ /; PPAuth=/ || forward.nil?
    data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
  end

  if data.index("The e-mail address or password is incorrect")
    raise AuthenticationError, "Username and password do not match"
  elsif data != ""
    raise AuthenticationError, "Required field must not be blank"
  elsif cookies == ""
    raise ConnectionError, PROTOCOL_ERROR
  end

  data, resp, cookies, forward = get("http://mail.live.com/mail", cookies)
  until forward.nil?
    data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
  end


  @domain = URI.parse(old_url).host
  @cookies = cookies
rescue AuthenticationError => m
  if @attempt == 1
    retry
  else
    raise m
  end
end