summaryrefslogtreecommitdiff
path: root/plugins/GmailNotifier/src/check.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/GmailNotifier/src/check.cpp')
-rw-r--r--plugins/GmailNotifier/src/check.cpp258
1 files changed, 111 insertions, 147 deletions
diff --git a/plugins/GmailNotifier/src/check.cpp b/plugins/GmailNotifier/src/check.cpp
index e2d5af213a..9a74b69765 100644
--- a/plugins/GmailNotifier/src/check.cpp
+++ b/plugins/GmailNotifier/src/check.cpp
@@ -2,151 +2,7 @@
#pragma comment(lib, "Wininet.lib")
-
-void CheckMailInbox(Account *curAcc)
-{
- // internet connection handle
- // internet request handle
- HINTERNET hHTTPConnection = nullptr, hHTTPRequest = nullptr;
-
- DBVARIANT dbv;
- static char *contentType = "Content-Type: application/x-www-form-urlencoded";
- char requestBuffer[256] = "continue=https%3A%2F%2Fmail.google.com%2Fa%2F";
- char fileBuffer[_MAX_DOWN_BUFFER] = "";
- char *tail;
- char str[64];
- char temp[_MAX_DOWN_BUFFER] = "";
- unsigned long bufferLength;
-
- if (curAcc->IsChecking)
- return;
-
- curAcc->IsChecking = TRUE;
-
- if (!db_get_s(curAcc->hContact, "CList", "MyHandle", &dbv)) {
- mir_strcpy(curAcc->results.content, dbv.pszVal);
- db_free(&dbv);
- }
- else mir_strcpy(curAcc->results.content, curAcc->name);
-
- tail = strstr(curAcc->results.content, " [");
- if (tail) *tail = '\0';
- mir_strcat(curAcc->results.content, " [");
-
- mir_strcpy(str, curAcc->results.content);
- mir_strcat(str, Translate("Checking..."));
- mir_strcat(str, "]");
-
- db_set_s(curAcc->hContact, "CList", "MyHandle", str);
- // internet open handle
- HINTERNET hHTTPOpen = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0);
- if (!hHTTPOpen) {
- mir_strcat(curAcc->results.content, Translate("Can't open Internet!"));
- goto error_handle;
- }
-
- if (curAcc->hosted[0]) {
- hHTTPConnection = InternetConnectA(hHTTPOpen,
- "www.google.com",
- INTERNET_DEFAULT_HTTPS_PORT,
- nullptr,
- nullptr,
- INTERNET_SERVICE_HTTP,
- 0,
- 0);
-
- if (!hHTTPConnection) {
- mir_strcat(curAcc->results.content, Translate("Can't reach server!"));
- goto error_handle;
- }
- mir_strcpy(str, "/a/");
- mir_strcat(str, curAcc->hosted);
- mir_strcat(str, "/LoginAction");
- hHTTPRequest = HttpOpenRequestA(hHTTPConnection, "POST", str, HTTP_VERSIONA, nullptr, nullptr, INTERNET_FLAG_SECURE, 0);
- mir_strcat(requestBuffer, curAcc->hosted);
- mir_strcat(requestBuffer, "%2Ffeed%2Fatom&service=mail&userName=");
- mir_strcat(requestBuffer, curAcc->name);
- tail = strchr(requestBuffer, '@');
- *tail = '\0';
- mir_strcat(requestBuffer, "&password=");
- mir_strcat(requestBuffer, curAcc->pass);
- if (!HttpSendRequestA(hHTTPRequest, contentType, (int)mir_strlen(contentType) + 1, requestBuffer, (int)mir_strlen(requestBuffer) + 1)) {
- mir_strcpy(curAcc->results.content, Translate("Can't send account data!"));
- goto error_handle;
- }
-
- InternetCloseHandle(hHTTPConnection);
- InternetCloseHandle(hHTTPRequest);
- hHTTPRequest = nullptr;
- }
-
- hHTTPConnection = InternetConnectA(hHTTPOpen, "mail.google.com", INTERNET_DEFAULT_HTTPS_PORT, nullptr, nullptr, INTERNET_SERVICE_HTTP, 0, 0);
- if (!hHTTPConnection) {
- mir_strcat(curAcc->results.content, Translate("Can't reach server!"));
- goto error_handle;
- }
- if (curAcc->hosted[0]) {
- mir_strcpy(str, "/a/");
- mir_strcat(str, curAcc->hosted);
- mir_strcat(str, "/feed/atom");
- }
- else mir_strcpy(str, "/mail/feed/atom");
-
- hHTTPRequest = HttpOpenRequest(hHTTPConnection, L"GET", _A2T(str), nullptr, nullptr, nullptr, INTERNET_FLAG_SECURE | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD, 0);
- InternetSetOption(hHTTPRequest, INTERNET_OPTION_USERNAME, _A2T(curAcc->name), (int)mir_strlen(curAcc->name) + 1);
- InternetSetOption(hHTTPRequest, INTERNET_OPTION_PASSWORD, _A2T(curAcc->pass), (int)mir_strlen(curAcc->pass) + 1);
- if (!HttpSendRequest(hHTTPRequest, nullptr, 0, nullptr, 0)) {
- mir_strcat(curAcc->results.content, Translate("Can't get RSS feed!"));
- goto error_handle;
- }
- while (InternetReadFile(hHTTPRequest, temp, _MAX_DOWN_BUFFER, &bufferLength) && bufferLength > 0) {
- temp[bufferLength] = '\0';
- mir_strcat(fileBuffer, temp);
- }
-
- fileBuffer[_MAX_DOWN_BUFFER - 1] = '\0';
- curAcc->results_num = ParsePage(fileBuffer, &curAcc->results);
- if (curAcc->results_num == -1) {
- mir_strcat(curAcc->results.content, Translate("Wrong name or password!"));
- goto error_handle;
- }
- InternetCloseHandle(hHTTPOpen);
- InternetCloseHandle(hHTTPConnection);
- InternetCloseHandle(hHTTPRequest);
-
- mir_strcat(curAcc->results.content, _itoa(curAcc->results_num, str, 10));
- mir_strcat(curAcc->results.content, "]");
-
- curAcc->IsChecking = FALSE;
- return;
-
-error_handle:
- curAcc->results_num = -1;
- InternetCloseHandle(hHTTPOpen);
- InternetCloseHandle(hHTTPConnection);
- InternetCloseHandle(hHTTPRequest);
-
- mir_strcat(curAcc->results.content, "]");
-
- curAcc->IsChecking = FALSE;
-}
-
-void __cdecl Check_ThreadFunc(void *lpParam)
-{
- InternetSetCookieA("https://mail.google.com/mail/", "GX", "");
- if (lpParam) {
- CheckMailInbox((Account *)lpParam);
- NotifyUser((Account *)lpParam);
- }
- else {
- for (int i = 0; i < acc_num && GetContactProto(acc[i].hContact); i++) {
- CheckMailInbox(&acc[i]);
- NotifyUser(&acc[i]);
- }
- }
-}
-
-int ParsePage(char *page, resultLink *prst)
+static int ParsePage(char *page, resultLink *prst)
{
char *str_head;
char *str_tail;
@@ -155,8 +11,6 @@ int ParsePage(char *page, resultLink *prst)
wchar_t str[64];
prst->next = nullptr;
- if (strstr(page, "Unauthorized"))
- return -1;
if (!(str_head = strstr(page, "<entry>")))
return 0;
@@ -186,3 +40,113 @@ int ParsePage(char *page, resultLink *prst)
prst->next = nullptr;
return num;
}
+
+void CheckMailInbox(Account *curAcc)
+{
+ if (curAcc->IsChecking)
+ return;
+
+ curAcc->IsChecking = true;
+
+ mir_strcpy(curAcc->results.content, curAcc->name);
+ mir_strcat(curAcc->results.content, " [");
+
+ char str[64];
+ mir_snprintf(str, "%s%s]", curAcc->results.content, Translate("Checking..."));
+ db_set_s(curAcc->hContact, "CList", "MyHandle", str);
+
+ if (curAcc->hosted[0]) {
+ CMStringA szUrl(FORMAT, "https://www.google.com/a/%s/LoginAction", curAcc->hosted);
+ CMStringA szBody("continue=https%3A%2F%2Fmail.google.com%2Fa%2F");
+ szBody.Append(curAcc->hosted);
+ szBody.Append("%2Ffeed%2Fatom&service=mail&userName=");
+ char *tail = strchr(curAcc->name, '@');
+ if (tail) *tail = 0;
+ szBody.Append(curAcc->name);
+ if (tail) *tail = '@';
+ szBody.Append("&password=");
+ szBody.Append(curAcc->pass);
+
+ NETLIBHTTPHEADER headers[1] = {
+ { "Content-Type", "application/x-www-form-urlencoded" }
+ };
+
+ NETLIBHTTPREQUEST nlr = {};
+ nlr.cbSize = sizeof(nlr);
+ nlr.szUrl = szUrl.GetBuffer();
+ nlr.requestType = REQUEST_POST;
+ nlr.headersCount = _countof(headers);
+ nlr.headers = headers;
+ nlr.dataLength = szBody.GetLength();
+ nlr.pData = szBody.GetBuffer();
+
+ NETLIBHTTPREQUEST *nlu = Netlib_HttpTransaction(hNetlibUser, &nlr);
+ if (nlu == nullptr || nlu->resultCode != 200) {
+ mir_strcpy(curAcc->results.content, Translate("Can't send account data!"));
+
+ Netlib_FreeHttpRequest(nlu);
+ curAcc->results_num = -1;
+ mir_strcat(curAcc->results.content, "]");
+ curAcc->IsChecking = false;
+ return;
+ }
+
+ Netlib_FreeHttpRequest(nlu);
+ }
+
+ // go!
+ CMStringA loginPass(FORMAT, "%s:%s", curAcc->name, curAcc->pass);
+ ptrA loginPassEncoded(mir_base64_encode((BYTE*)loginPass.c_str(), loginPass.GetLength()));
+
+ CMStringA szUrl("https://mail.google.com"), szAuth(FORMAT, "Basic %s", loginPassEncoded.get());
+ if (curAcc->hosted[0])
+ szUrl.AppendFormat("/a/%s/feed/atom", curAcc->hosted);
+ else
+ szUrl.Append("/mail/feed/atom");
+
+ NETLIBHTTPHEADER headers[1] = {
+ { "Authorization", szAuth.GetBuffer() }
+ };
+
+ NETLIBHTTPREQUEST nlr = {};
+ nlr.cbSize = sizeof(nlr);
+ nlr.szUrl = szUrl.GetBuffer();
+ nlr.requestType = REQUEST_GET;
+
+ NETLIBHTTPREQUEST *nlu = Netlib_HttpTransaction(hNetlibUser, &nlr);
+ if (nlu == nullptr) {
+ if (nlr.resultCode == 401)
+ mir_strcat(curAcc->results.content, Translate("Wrong name or password!"));
+ else
+ mir_strcat(curAcc->results.content, Translate("Can't get RSS feed!"));
+ Netlib_FreeHttpRequest(nlu);
+
+ curAcc->results_num = -1;
+ mir_strcat(curAcc->results.content, "]");
+ curAcc->IsChecking = false;
+ return;
+ }
+
+ curAcc->results_num = ParsePage(nlu->pData, &curAcc->results);
+ mir_strcat(curAcc->results.content, _itoa(curAcc->results_num, str, 10));
+ mir_strcat(curAcc->results.content, "]");
+
+ curAcc->IsChecking = false;
+}
+
+void __cdecl Check_ThreadFunc(void *lpParam)
+{
+ if (lpParam) {
+ CheckMailInbox((Account *)lpParam);
+ NotifyUser((Account *)lpParam);
+ }
+ else {
+ for (int i = 0; i < g_accs.getCount(); i++) {
+ Account &acc = g_accs[i];
+ if (GetContactProto(acc.hContact)) {
+ CheckMailInbox(&acc);
+ NotifyUser(&acc);
+ }
+ }
+ }
+}