Once I was given the task to get user's contact list for some email services (including gmail.com, yahoo.com, aos.com). I first began studying the mail protocols IMAP and POP3, but unfortunately these protocols do not provide for a list of contacts. From an API that provide some of these mail services I refused, so as these API is not available for all languages, and also would have to spend time researching each of them. I decided that the use of sockets - this is the only option that will quickly get a list of contacts and make it about the same for most email services. The point is to emulate the browser. I will consider an algorithm for obtaining the list of contacts with gmail.com, but it is done in a similar way to other postal services.To get started you will need a http sniffer. I used HTTP Analyzer and unfortunately Internet Explorer best approached (Chrome does not snifitsya and Mozilla Firefox snifit not much). Contacts are divided into categories, so we'll get out of the categories of "Contacts" and "Other Contacts". For a list of contacts, you must send the following request: GET / mail /? auth =% 0 HTTP/1.1Host: mail.google.com Cookie: SID =% 1 GET HTTP/1.1 Host: mail.google.com Cookie: GX = 0% is therefore required to get value and auth cookie SID and GX. proceed. logon and save option for sidt accounts.youtube.com cookies and SID:
QString GmailCom :: postLogin ()
{
int searchResult = - 1 ;
QString buffer = "" ;
QString temp = "" ;
QString endString = "\ R \ n" ;
QString endhead = "\ R \ n \ R \ n" ;
QString startSidt = "sidt =" ;
QString startCookieSid = "SID =" ;
QString autologin = "X-Auto-Login:" ;
QSslSocket socket;
socket.connectToHostEncrypted ( "accounts.google.com" , 443 );
if (! socket.waitForEncrypted (MAX_WAIT))
return "Error 0" ;
socket.write (QString ( "POST / HTTP/1.1 ServiceLoginAuth \ R \ n" \
"Content-Type: application / x-www-form-urlencoded \ R \ n" \
"Host: accounts.google.com \ R \ n " \
"Content-Length: 741 \ R \ n" \
"Cookie:
while (socket.waitForReadyRead (MAX_WAIT))
{
temp = socket.readAll ();
buffer + = temp;
if (temp.indexOf (endhead)! = - 1 ) Break ;
}
socket.disconnectFromHost ();
searchResult = buffer.indexOf (autoLogin);
if (searchResult! = - 1 ) return "Error 1" ;
searchResult = buffer.indexOf (startCookieSid);
if (searchResult == - 1 ) return "Error 2" ;
buffer = buffer.mid (searchResult + startCookieSid.length ());
mCookieSid = buffer.mid ( 0 , buffer.indexOf (endString));
searchResult = buffer.indexOf (startSidt);
if (searchResult == - 1 ) return "Error 3" ;
buffer = buffer.mid (searchResult + startSidt.length ());
mAccountsYoutubeSidt = buffer.mid ( 0 , buffer.indexOf (endString));
return "Ok" ;
}
Then we get the value for sidt accounts.google.com:
QString GmailCom :: getAccountsGoogle ()
{
int searchResult = - 1 ;
QString buffer = "" ;
QString temp = "" ;
QString endString = "\ R \ n" ;
QString endhead = "\ R \ n \ R \ n" ;
QString startSidt = "sidt =" ;
QSslSocket socket;
socket.connectToHostEncrypted ( "accounts.youtube.com" , 443 );
if (! socket.waitForEncrypted (MAX_WAIT))
return "Error 4" ;
socket.write (QString ( "GET / Accounts / setsid? SSDC = 1 & sidt =% 0 HTTP/1.1 \ R \ n" \
"Host: accounts.youtube.com \ R \ n" \
"\ R \ n" ). arg (mAccountsYoutubeSidt). toUtf8 ());
while (socket.waitForReadyRead (MAX_WAIT))
{
temp = socket.readAll ();
buffer + = temp;
if (temp.indexOf (endhead)! = - 1 ) Break ;
}
socket.disconnectFromHost ();
searchResult = buffer.indexOf (startSidt);
if (searchResult == - 1 ) return "Error 5" ;
buffer = buffer.mid (searchResult + startSidt.length ());
mAccountsGoogleSidt = buffer.mid ( 0 , buffer.indexOf (endString));
return "Ok" ;
}
After that we get value for auth mail.google.com:
QString GmailCom :: getAccountsMail ()
{
int searchResult = - 1 ;
QString buffer = "" ;
QString temp = "" ;
QString endString = "\ R \ n" ;
QString endhead = "\ R \ n \ R \ n" ;
QString startAuth = "auth =" ;
QSslSocket socket;
socket.connectToHostEncrypted ( "accounts.youtube.com" , 443 );
if (! socket.waitForEncrypted (MAX_WAIT))
return "Error 6" ;
socket.write (QString ( "GET / Accounts / setsid? SSDC = 1 & sidt =% 0 HTTP/1.1 \ R \ n" \
"Host: accounts.google.md \ R \ n" \
"\ R \ n" ). arg (mAccountsGoogleSidt). toUtf8 ());
while (socket.waitForReadyRead (MAX_WAIT))
{
temp = socket.readAll ();
buffer + = temp;
if (temp.indexOf (endhead)! = - 1 ) Break ;
}
socket.disconnectFromHost ();
searchResult = buffer.indexOf (startAuth);
if (searchResult == - 1 ) return "Error 7" ;
buffer = buffer.mid (searchResult + startAuth.length ());
mMailGoogleAuth = buffer.mid ( 0 , buffer.indexOf (endString));
return "Ok" ;
}
And we obtain our desired cookie GX:
QString GmailCom :: getMailAuth ()
{
int searchResult = - 1 ;
QString buffer = "" ;
QString temp = "" ;
QString endString = "\ R \ n" ;
QString endhead = "\ R \ n \ R \ n" ;
QString startGx = "GX =" ;
QSslSocket socket;
socket.connectToHostEncrypted ( "mail.google.com" , 443 );
if (! socket.waitForEncrypted (MAX_WAIT))
return "Error 8" ;
socket.write (QString ( "GET / mail /? auth =% 0 HTTP/1.1 \ R \ n" \
"Host: mail.google.com \ R \ n" \
"Cookie: SID =% 1 \ r \ n " \
"\ R \ n" ). arg (mMailGoogleAuth). arg (mCookieSid). toUtf8 ());
while (socket.waitForReadyRead (MAX_WAIT))
{
temp = socket.readAll ();
buffer + = temp;
if (temp.indexOf (endhead)! = - 1 ) Break ;
}
socket.disconnectFromHost ();
searchResult = buffer.indexOf (startGx);
if (searchResult == - 1 ) return "Error 9" ;
buffer = buffer.mid (searchResult + startGx.length ());
mGxCookie = buffer.mid ( 0 , buffer.indexOf (endString));
return "Ok" ;
}
Now you can get a list of contacts:
QString GmailCom :: getMyContacts ()
{
int searchResult = - 1 ;
QStringList listContacts;
QString stringContacts;
QString buffer = "" ;
QString startContacts = "&&& START &&&" ;
QString endContacts = "&&& END &&&" ;
QString temp = "" ;
QString rn = "\ R \ n" ;
QSslSocket socket;
socket.connectToHostEncrypted ( "mail.google.com" , 443 );
if (! socket.waitForEncrypted (MAX_WAIT))
return "Error 10" ;
socket.write (QString ( "GET HTTP/1.1 \ R \ n " \
"Host: mail.google.com \ R \ n" \
"Cookie: GX = 0% \ R \ n" \
"\ R \ n" ). arg (mGxCookie). toUtf8 ());
while (socket.waitForReadyRead (MAX_WAIT))
{
temp = socket.readAll ();
buffer + = temp;
if (temp.indexOf (endContacts)! = - 1 ) Break ;
}
socket.disconnectFromHost ();
searchResult = buffer.indexOf (startContacts);
if (searchResult == - 1 ) return "Error 11" ;
buffer = buffer.mid (searchResult + startContacts.length ());
searchResult = buffer.indexOf (endContacts);
if (searchResult == - 1 ) return "Error 11" ;
buffer = buffer.mid ( 0 , searchResult);
while ( 1 )
{
int rn_1 buffer.indexOf = (rn, 0 );
int rn_2 buffer.indexOf = (rn, rn_1 rn.length + ());
if (rn_1 == - 1 | | rn_2 == - 1 )
Break ;
else
buffer.remove (rn_1, rn_2 - rn_1 + rn.length ());
}
listContacts buffer.split = ( "\ \ \" " );
listContacts.removeDuplicates ();
for ( int I = 0 ; I <listContacts.size (); I + +)
if (listContacts [I]. indexOf ( "@" )! = - 1 )
stringContacts listContacts + = [I] + "" ;
return stringContacts;
}
And the list of contacts from a group "Other contacts":
QString GmailCom :: getGroupContacts ()
{
int searchResult = - 1 ;
QStringList listContacts;
QString stringContacts;
QString buffer = "" ;
QString startContacts = "&&& START &&&" ;
QString endContacts = "&&& END &&&" ;
QString temp = "" ;
QString rn = "\ R \ n" ;
QSslSocket socket;
socket.connectToHostEncrypted ( "mail.google.com" , 443 );
if (! socket.waitForEncrypted (MAX_WAIT))
return "Error 12" ;
socket.write (QString ( "GET HTTP/1.1 \ R \ n " \
"Host: mail.google.com \ R \ n" \
"Cookie: GX = 0% \ R \ n" \
"\ R \ n"
). Arg (mGxCookie). ToUtf8 ());
while (socket.waitForReadyRead (MAX_WAIT))
{
temp = socket.readAll ();
buffer + = temp;
if (temp.indexOf (endContacts)! = - 1 ) Break ;
}
socket.disconnectFromHost ();
searchResult = buffer.indexOf (startContacts);
if (searchResult == - 1 ) return "Error 13" ;
buffer = buffer.mid (searchResult + startContacts.length ());
searchResult = buffer.indexOf (endContacts);
if (searchResult == - 1 ) return "Error 13" ;
buffer = buffer.mid ( 0 , searchResult);
while ( 1 )
{
int rn_1 buffer.indexOf = (rn, 0 );
int rn_2 buffer.indexOf = (rn, rn_1 rn.length + ());
if (rn_1 == - 1 | | rn_2 == - 1 )
Break ;
else
buffer.remove (rn_1, rn_2 - rn_1 + rn.length ());
}
listContacts buffer.split = ( "\ \ \" " );
listContacts.removeDuplicates ();
for ( int I = 0 ; I <listContacts.size (); I + +)
if (listContacts [I]. indexOf ( "@" )! = - 1 )
stringContacts listContacts + = [I] + "" ;
return stringContacts;
}
Still have a constructor that initializes the username and password when you create an object:
GmailCom :: GmailCom (QString login, QString password):
mLogin (login), mPassword (password) {}
Both the method of obtaining the contact list:
QString GmailCom :: getContacts ()
{
QString temp;
QString error;
if ((error = PostLogin ())! = "Ok" ) return error;
if ((error = getAccountsGoogle ())! = "Ok" ) return error;
if ((error = getAccountsMail ())! = "Ok" ) return error;
if ((error = getMailAuth ())! = "Ok" ) return error;
QString stringContacts = "" ;
temp = getMyContacts ();
if (temp.split ( '' ) [ 0 ] == "Error" ) return temp;
stringContacts + = temp;
temp = getGroupContacts ();
if (temp.split ( '' ) [ 0 ] == "Error" ) return temp;
stringContacts + = temp;
stringContacts.remove (stringContacts.length () - 1 , 1 );
return stringContacts;
}
Header is as follows:
# Ifndef GMAILCOM_H
# define GMAILCOM_H
# Include <QSslSocket>
<QNetworkProxy> # include
# include <QStringList>
# Define MAX_WAIT 3000
class gmailcom
{
Private :
QString mLogin;
QString mPassword;
QString mAccountsYoutubeSidt;
QString mAccountsGoogleSidt;
QString mMailGoogleAuth;
QString mCookieSid;
QString mGxCookie;
QString postLogin ();
QString getAccountsGoogle ();
QString getAccountsMail ();
QString getMailAuth ();
QString getMyContacts ();
QString getGroupContacts ();
Public :
GmailCom (QString login, QString password);
QString getContacts ();
};
# Endif
Using the class:
# Include <QCoreApplication>
# include "GmailCom.h"
int main ( int argc, char * argv [])
{
QCoreApplication a (argc, argv);
Gmailcom gmailcom ( "login" , "password" );
qDebug () << gmailCom.getContacts ();
return a.exec ();
}
Finally *. Pro file:
# -------------------------------------------------
#
# Project Created by QtCreator 2013-10-23T09: 40:42
#
# --------------------------------- ----------------
QT + = core
QT + = network
QT - = gui
TARGET = Mailer
CONFIG + = console
CONFIG - = app_bundle
TEMPLATE = app
SOURCES + = main.cpp \
GmailCom.cpp
HEADERS + = \
GmailCom.h
All communication with the server takes place over a secure connection. The code is fully working. If skopipastit alternately each block of code and put in the appropriate files - it will compile. Used compiler g+ +, development environment Qt creator, version 5.1
No comments:
Post a Comment