-
Notifications
You must be signed in to change notification settings - Fork 218
/
mailru.rb
68 lines (51 loc) · 1.55 KB
/
mailru.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
require 'csv'
class Contacts
class Mailru < Base
LOGIN_URL = "https://auth.mail.ru/cgi-bin/auth"
ADDRESS_BOOK_URL = "http://win.mail.ru/cgi-bin/abexport/addressbook.csv"
attr_accessor :cookies
def real_connect
username = login
postdata = "Login=%s&Domain=%s&Password=%s" % [
CGI.escape(username),
CGI.escape(domain_param(username)),
CGI.escape(password)
]
data, resp, self.cookies, forward = post(LOGIN_URL, postdata, "")
if data.index("fail=1")
raise AuthenticationError, "Username and password do not match"
elsif cookies == "" or data == ""
raise ConnectionError, PROTOCOL_ERROR
end
data, resp, cookies, forward = get(login_token_link(data), login_cookies.join(';'))
end
def contacts
postdata = "confirm=1&abtype=6"
data, resp, cookies, forward = post(ADDRESS_BOOK_URL, postdata, login_cookies.join(';'))
@contacts = []
CSV.parse(data) do |row|
@contacts << [row[0], row[4]] unless header_row?(row)
end
@contacts
end
def skip_gzip?
true
end
private
def login_token_link(data)
data.match(/url=(.+)\">/)[1]
end
def login_cookies
self.cookies.split(';').collect{|c| c if (c.include?('t=') or c.include?('Mpop='))}.compact.collect{|c| c.strip}
end
def header_row?(row)
row[0] == 'AB-Name'
end
def domain_param(login)
login.include?('@') ?
login.match(/.+@(.+)/)[1] :
'mail.ru'
end
end
TYPES[:mailru] = Mailru
end