hotmail.rb
6.89 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class Contacts
class Hotmail < Base
URL = "http://login.live.com/login.srf?id=2"
OLD_CONTACT_LIST_URL = "http://%s/cgi-bin/addresses"
NEW_CONTACT_LIST_URL = "http://%s/mail/GetContacts.aspx"
NEWEST_CONTACT_LIST_URL = "http://%s/mail/options.aspx?subsection=26"
COMPOSE_URL = "http://%s/cgi-bin/compose?"
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"
MAX_HTTP_THREADS = 8
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)
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
old_url = form_url
until forward.nil?
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
end
=begin
if data =~ %r{action="(.*?)"}
forward = $1
puts forward
napexp = CGI.escape(data.to_s[/id="NAPExp" value="(.*?)"/][19...-1])
nap = CGI.escape(data.to_s[/id="NAP" value="(.*?)"/][16...-1])
anon = CGI.escape(data.to_s[/id="ANON" value="(.*?)"/][17...-1])
anonexp = CGI.escape(data.to_s[/id="ANONExp" value="(.*?)"/][20...-1])
t = CGI.escape(data.to_s[/id="t" value="(.*?)"/][14...-1])
postdata = "NAPExp=%s&NAP=%s&ANON=%s&ANONExp=%s&t=%s" % [ napexp, nap, anon, anonexp, t ]
puts postdata
data, resp, cookies, forward, old_url = post(forward, postdata, cookies, old_url) + [forward]
end
until forward.nil?
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
end
=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
# click on 'Contiune' if presented with the Hotmail Listened page
# look for the Submit button with a "TakeMeToInbox" name (this should work for other languages)
if (not old_url.grep(/MessageAtLogin.aspx/).first.nil?)
viewState = data.split(/>\s*?</).grep(/__VIEWSTATE/).first[/value=\".+?\"/][7..-2]
eventValidation = data.split(/>\s*?</).grep(/__EVENTVALIDATION/).first[/value=\".+?\"/][7..-2]
continueValue = data.split(/>\s*?</).grep(/TakeMeToInbox/).first[/value=\".+?\"/][7..-2]
# post back to the same url
postdata = "%s=%s&%s=%s&%s=%s" % [
"__VIEWSTATE", CGI.escape(viewState),
"__EVENTVALIDATION", CGI.escape(eventValidation),
CGI.escape("TakeMeToInbox"), CGI.escape(continueValue)
]
data, resp, cookies, forward = post( old_url, postdata, cookies, old_url )
until forward.nil?
data, resp, cookies, forward, old_url = get(forward, cookies, old_url) + [forward]
end
end
@domain = URI.parse(old_url).host
@cookies = cookies
rescue AuthenticationError => m
if @attempt == 1
retry
else
raise m
end
end
def contacts(options = {})
return @contacts if @contacts
if connected?
url = URI.parse(contact_list_url)
data, resp, cookies, forward = get( contact_list_url, @cookies )
if resp.code_type != Net::HTTPOK
raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR)
end
# we have to click on the Export Contacts button to get the csv:
# Search the content for __VIEWSTATE or __EVENTVALIDATION
viewState = data.split(/>\s*?</).grep(/__VIEWSTATE/).first[/value=\".+?\"/][7..-2]
eventValidation = data.split(/>\s*?</).grep(/__EVENTVALIDATION/).first[/value=\".+?\"/][7..-2]
exportValue = data.split(/>\s*?</).grep(/ctl02\$ExportButton/).first[/value=\".+?\"/][7..-2]
mt = cookies.split("; ").grep(/mt=/).first[3..-1]
# post back to the same url
postdata = "%s=%s&%s=%s&%s=%s&%s=%s" % [
"__VIEWSTATE", CGI.escape(viewState),
"__EVENTVALIDATION", CGI.escape(eventValidation),
CGI.escape("ctl02$ExportButton"), CGI.escape(exportValue),
"mt", CGI.escape( mt )
]
url = URI.parse(contact_list_url)
http = open_http(url)
resp, data = http.post("#{url.path}?#{url.query}", postdata,
"User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0",
"Accept-Encoding" => "gzip",
"Cookie" => cookies,
"Referer" => contact_list_url,
"Content-Type" => 'application/x-www-form-urlencoded'
)
data = uncompress(resp, data)
parse(data, options)
end
end
private
def contact_list_url
NEWEST_CONTACT_LIST_URL % @domain
end
def follow_email(data, id, contacts_slot)
compose_url = COMPOSE_URL % @domain
postdata = "HrsTest=&to=#{id}&mailto=1&ref=addresses"
postdata += "&curmbox=00000000-0000-0000-0000-000000000001"
a = data.split(/>\s*<input\s+/i).grep(/\s+name="a"/i)
return nil if a.empty?
a = a[0].match(/\s+value="([a-f0-9]+)"/i) or return nil
postdata += "&a=#{a[1]}"
data, resp, @cookies, forward = post(compose_url, postdata, @cookies)
e = data.split(/>\s*<input\s+/i).grep(/\s+name="to"/i)
return nil if e.empty?
e = e[0].match(/\s+value="([^"]+)"/i) or return nil
@contacts[contacts_slot][1] = e[1] if e[1].match(/@/)
end
def parse(data, options={})
data = data.split("\r\n")
data = CSV.parse(data.join("\r\n").gsub('"', '').gsub(';', ','), ';')
col_names = data.shift
@contacts = data.delete_if{|person|person[0].nil?}.map do |person|
person = person[0].split(",")
next unless (idx = person.index('SMTP'))
[[person[1], person[2], person[3]].delete_if{|i|i.empty?}.join(" "), person[idx - 1]] unless person[idx - 1].nil?
end.compact
end
end
TYPES[:hotmail] = Hotmail
end