Class: MOG::Contacts::Gmail

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

Constant Summary collapse

URL =
"https://mail.google.com/mail/"
LOGIN_URL =
"https://www.google.com/accounts/ServiceLoginAuth"
LOGIN_REFERER_URL =
"https://www.google.com/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=http%3A%2F%2Fmail.google.com%2Fmail%3Fui%3Dhtml%26zy%3Dl&ltmpl=yj_blanco&ltmplcache=2&hl=en"
CONTACT_LIST_URL =
"https://mail.google.com/mail/contacts/data/contacts?thumb=true&show=ALL&enums=true&psort=Name&max=10000&out=js&rf=&jsx=true"
PROTOCOL_ERROR =
"Gmail has changed its protocols, please upgrade this library first. If that does not work, dive into the code and submit a patch at http://github.com/cardmagic/contacts"

Instance Method Summary collapse

Instance Method Details

#real_connectObject



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
# File 'lib/contacts/gmail.rb', line 12

def real_connect
  postdata =  "ltmpl=yj_blanco"
  postdata += "&continue=%s" % CGI.escape(URL)
  postdata += "&ltmplcache=2"
  postdata += "&service=mail"
  postdata += "&rm=false"
  postdata += "&ltmpl=yj_blanco"
  postdata += "&hl=en"
  postdata += "&Email=%s" % CGI.escape()
  postdata += "&Passwd=%s" % CGI.escape(password)
  postdata += "&rmShown=1"
  postdata += "&null=Sign+in"
      
  time =      Time.now.to_i
  time_past = Time.now.to_i - 8 - rand(12)
  cookie =    "GMAIL_LOGIN=T#{time_past}/#{time_past}/#{time}"

  data, resp, cookies, forward, old_url = post(LOGIN_URL, postdata, cookie, LOGIN_REFERER_URL) + [LOGIN_URL]

  cookies = remove_cookie("GMAIL_LOGIN", cookies)

  if data.index("Username and password do not match")
    raise AuthenticationError, "Username and password do not match"
  elsif data.index("Required field must not be blank")
    raise AuthenticationError, "Login and password must not be blank"
  elsif data.index("errormsg_0_logincaptcha")
    raise AuthenticationError, "Captcha error"
  elsif data.index("Invalid request")
    raise ConnectionError, PROTOCOL_ERROR
  elsif cookies == ""
    raise ConnectionError, PROTOCOL_ERROR
  end

  cookies = remove_cookie("LSID", cookies)
  cookies = remove_cookie("GV", cookies)

  @cookies = cookies
end