forked from bimbiero/contacts
/
hotmail.rb
95 lines (81 loc) · 3.11 KB
/
hotmail.rb
1
2
3
4
5
6
7
8
9
10
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
58
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
85
86
87
88
89
90
91
92
93
94
95
require 'csv'
require 'rubygems'
require 'nokogiri'
class Contacts
class Hotmail < Base
DETECTED_DOMAINS = [ /hotmail/i, /live/i, /msn/i, /chaishop/i ]
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"
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(login),
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
def contacts(options = {})
if @contacts.nil? && connected?
url = URI.parse(contact_list_url)
contact_list_url = get_contact_list_url
data, resp, cookies, forward = get(contact_list_url, @cookies )
data.force_encoding('UTF-8')
separator = data[7]
unless data.valid_encoding?
data = data.slice(2..-1).split("\u0000").join('')
separator = ','
end
@contacts = CSV.parse(data, {:headers => true, :col_sep => separator}).map do |row|
name = ""
name = row["First Name"] if !row["First Name"].nil?
name << " #{row["Last Name"]}" if !row["Last Name"].nil?
[name, row["E-mail Address"] || ""]
end
else
@contacts || []
end
end
private
TYPES[:hotmail] = Hotmail
# the contacts url is dynamic
# luckily it tells us where to find it
def get_contact_list_url(url=CONTACT_LIST_URL)
data = get(url, @cookies)[0]
html_doc = Nokogiri::HTML(data)
html_doc.xpath("//a")[0]["href"]
end
end
end