Gmail new mail checker

Share your advanced PureBasic knowledge/code with the community.
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Gmail new mail checker

Post by epidemicz »

Hey all, I recently found a really cool chrome extension that sits on top of chrome and checks for new mail. I thought this was really cool so I thought I'd try to see how it works & see if I could do something similar in PB.

It appears that google doesn't really have any api that lets you directly access your mailbox. However, they do have an atom feed that has all of your new mail in it (https://mail.google.com/mail/feed/atom). So, this program uses google api to grab an authentication token and I kinda hacked it up to pass that to the atom feed to get the mail. I couldn't find any documentation on how to use the auth token for logging into the atom feed, so it was a lot of reading through headers to see what was going on. Many thanks to the people who made live headers for firefox :D.

So, here's my proof of concept, quick and dirty like.

Code: Select all

;epidemicz 3/26/2010
;quick and dirty (really dirty) proof of concept gmail new mail checker

;be sure to fill out user & pass
user$ = "user" 
pass$ = "pass"

email$ = user$ + "%40gmail%2Ecom"


;needed for content-length: header -- google api doc doesn't say you need this
;but if you don't send content-length header with the request it will give error
;len is basically the length of the last section of the first request$
len = 57 + Len(email$) + Len(pass$)

InitNetwork()

*Buffer = AllocateMemory(3000)

connid = OpenNetworkConnection("mail.google.com", 80)

;request to get a ClientLogin authentication token from google
;http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
request$ = "POST https://www.google.com/accounts/ClientLogin HTTP/1.0" + Chr(10)
request$ + "Content-type: application/x-www-form-urlencoded" + Chr(10) 
request$ + "Connection: keep-alive" + Chr(10)
request$ + "Content-length: " + Str(Len) + Chr(10) + Chr(10) 
request$ + "&Email=" + email$ + "&Passwd=" + pass$ + "&accountType=HOSTED_OR_GOOGLE&service=mail"

SendNetworkString(connid, request$)

Debug "Logging in.."


Delay(2000)

Repeat
  cEvent = NetworkClientEvent(connid)
  
  Select cEvent
    Case #PB_NetworkEvent_Data
      ReceiveNetworkData(connid, *Buffer, 3000)
      response$ = PeekS(*Buffer)
      
      FreeMemory(*Buffer)  
      *Buffer = AllocateMemory(3000) 
      
      For x = 1 To CountString(response$, Chr(10))
        curline$ = StringField(response$, x, Chr(10))
        
        Debug curline$
        
        If FindString(curline$, "Auth=", 0)
          ;for some reason the auth token must have GX= in front of it
          auth$ = "GX=" + Mid(curline$, 6)
          
          Debug "sending request for mail feed.."

          ;forming request for gmail atom feed
          request$ = "GET /mail/feed/atom/ HTTP/1.1" + Chr(10) 
          request$ + "Host: mail.google.com" + Chr(10)
          request$ + "Cookie: " + auth$ + Chr(10) + Chr(10)
          
          SendNetworkString(connid, request$)
          
          Delay(2000)          
        EndIf
      Next x
  EndSelect    
  
Until cEvent = 0
Maybe nothing special but I'm quite proud :D.
Image
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Gmail new mail checker

Post by ar-s »

Works good :) Thanks
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Gmail new mail checker

Post by rsts »

NICE :D

Thanks for sharing.

cheers
a.ross
User
User
Posts: 21
Joined: Tue Dec 08, 2009 12:50 am

Re: Gmail new mail checker

Post by a.ross »

I tried for ages yesterday to do something very similar (But I had last checked the forum before the 26th :roll: )
Anyway thought I'ld share what I came up with for logging on to youtube:

Code: Select all

ProgramName.s="Youtubeupload"  
Address.s="www.google.com"
Type.s="POST"
URL.s="/youtube/accounts/ClientLogin"
Http.s="HTTP/1.1"
  
iSession=InternetOpen_(@ProgramName,0,#Null,#Null,0)
iConnection = InternetConnect_(iSession,@Address,443,#Null,#Null,3,0,0)
iOpenRequest = HttpOpenRequest_(iConnection,@Type,@URL,@http,#Null,0,$800000|$1000|$2000,0)
Encoding.S  =   "Content-Type: application/x-www-form-urlencoded"+Chr(10)+Chr(13)
HttpAddRequestHeaders_(iOpenRequest,@Encoding,Len(Encoding),$80000000 | $20000000)
PostMessage.S="Email=<username>&Passwd=<Password>&service=youtube&source=Test"
If HttpSendRequest_(iOpenRequest,#Null,#Null,@PostMessage,Len(PostMessage))
  *buffer=AllocateMemory(1024)
  BYTES=0
  InternetReadFile_(iOpenRequest,*buffer,1024,@bytes)
Else
  ;ERROR LOGGING IN
EndIf
its a bit nasty but will work and it needs a tidyup, replace <username> either with your google account or youtube account and <password> with you password, returns the authentication token in to the memory buffer you can then use that to upload or download using the youtube account entered, I used the windows API for this so I could send data via HTTPS.

Woops Forgot to add:

Code: Select all

InternetCloseHandle_(iOpenRequest)
InternetCloseHandle_(iConnection)
InternetCloseHandle_(iSession)
Andrew
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Gmail new mail checker

Post by epidemicz »

a.ross wrote:I tried for ages yesterday to do something very similar (But I had last checked the forum before the 26th :roll: )
Anyway thought I'ld share what I came up with for logging on to youtube:

Code: Select all

ProgramName.s="Youtubeupload"  
Address.s="www.google.com"
Type.s="POST"
URL.s="/youtube/accounts/ClientLogin"
Http.s="HTTP/1.1"
  
iSession=InternetOpen_(@ProgramName,0,#Null,#Null,0)
iConnection = InternetConnect_(iSession,@Address,443,#Null,#Null,3,0,0)
iOpenRequest = HttpOpenRequest_(iConnection,@Type,@URL,@http,#Null,0,$800000|$1000|$2000,0)
Encoding.S  =   "Content-Type: application/x-www-form-urlencoded"+Chr(10)+Chr(13)
HttpAddRequestHeaders_(iOpenRequest,@Encoding,Len(Encoding),$80000000 | $20000000)
PostMessage.S="Email=<username>&Passwd=<Password>&service=youtube&source=Test"
If HttpSendRequest_(iOpenRequest,#Null,#Null,@PostMessage,Len(PostMessage))
  *buffer=AllocateMemory(1024)
  BYTES=0
  InternetReadFile_(iOpenRequest,*buffer,1024,@bytes)
Else
  ;ERROR LOGGING IN
EndIf
its a bit nasty but will work and it needs a tidyup, replace <username> either with your google account or youtube account and <password> with you password, returns the authentication token in to the memory buffer you can then use that to upload or download using the youtube account entered, I used the windows API for this so I could send data via HTTPS.

Andrew
very nice :D.

I've been experimenting with something else I'd like to add in when I do eventually expand on this. I would like to have a link in my program that will take you directly to your gmail inbox without previously being signed in. You can see this in google talk, and recently discovered that somehow digsby can do it as well. You would think I could use the auth tokens like we are getting here to do it, but this seems to be a completely different token that they want.
Image
a.ross
User
User
Posts: 21
Joined: Tue Dec 08, 2009 12:50 am

Re: Gmail new mail checker

Post by a.ross »

Hmmmm, i'm not sure but you might need the HTTPS login and also use the HTTPS for the transfer of the gmail data, I'm not sure though, but thats how the web browsers loged in, I also have heard of people using a RSS/Atom feed to get there gmail alerts to there programs (I think?).
Might be heading a little offtopic here.....

Andrew
Lost
User
User
Posts: 63
Joined: Fri Dec 19, 2008 12:24 am
Location: Tasmania

Re: Gmail new mail checker

Post by Lost »

epidemicz wrote:I've been experimenting with something else I'd like to add in when I do eventually expand on this. I would like to have a link in my program that will take you directly to your gmail inbox without previously being signed in. You can see this in google talk, and recently discovered that somehow digsby can do it as well. You would think I could use the auth tokens like we are getting here to do it, but this seems to be a completely different token that they want.
I'm looking into this as well. I'm trying to authenticate an account just like you and once authenticated load a google service in a WebGadget. Unfortunately it turns out it's a bit more complicated than I expected.

At the moment I'm just logging in through a WebGadget, but I don't like the standard Google login screen. I just want a small, custom login box.
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Gmail new mail checker

Post by effis »

your code is excellent
but how do you get IP, and the all message (not only the beggining) and how do you get the reads messages?
is it possible also to send gmail messages?
and is it possible to open my internet explorer directly in my gmail page, without logging in?

thanks again
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Gmail new mail checker

Post by epidemicz »

effis wrote:your code is excellent
but how do you get IP, and the all message (not only the beggining) and how do you get the reads messages?
is it possible also to send gmail messages?
and is it possible to open my internet explorer directly in my gmail page, without logging in?

thanks again

Thanks :)

I haven't messed with this project much. I got stuck on a similar issue. If you look at the google talk app, there's a link on the application that will open your inbox in a browser window without having to log in thru the browser. I haven't quite figured out how they do that yet. These type of things weren't documented when I last looked so I don't think it something that google really wants you to do easily. Good luck though
Image
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Gmail new mail checker

Post by effis »

google talk app,
where is this?
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Gmail new mail checker

Post by epidemicz »

effis wrote:
google talk app,
where is this?

http://www.google.com/talk/about.html
Image
effis
User
User
Posts: 42
Joined: Thu May 27, 2010 11:22 am

Re: Gmail new mail checker

Post by effis »

thanks but i'm not a genius like you
can you maybe (or someone else) help be to go a little further?


thanks you very much
what i need is essencielly to open the gmail page in my browser without need to login, and maybe also to send mails
Post Reply