summaryrefslogtreecommitdiff
path: root/protocols/GmailNotifier/src/check.cpp
blob: 34a123390cda447e6bc25cebacbd35234dc6c7bf (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "stdafx.h"

#pragma comment(lib, "Wininet.lib")

static int ParsePage(char *page, resultLink *prst)
{
	char *str_head;
	char *str_tail;
	char name[64], title[64];
	int num = 0;
	wchar_t str[64];

	prst->next = nullptr;
	if (!(str_head = strstr(page, "<entry>")))
		return 0;

	while (str_head = strstr(str_head, "<title>")) {
		prst = prst->next = (resultLink *)malloc(sizeof(resultLink));
		str_head += 7;
		str_tail = strstr(str_head, "</title>");
		*str_tail = '\0';
		mir_strncpy(title, str_head, 41);
		if (mir_strlen(title) == 40)
			mir_strcat(title, "...");
		*str_tail = ' ';

		str_head = strstr(str_head, "<name>") + 6;
		str_tail = strstr(str_head, "</name>");
		*str_tail = '\0';
		mir_strncpy(name, str_head, 11);
		mir_strcat(name, ": ");
		*str_tail = ' ';

		mir_strcpy(prst->content, name);
		mir_strcat(prst->content, title);
		MultiByteToWideChar(CP_UTF8, 0, prst->content, -1, str, 64);
		WideCharToMultiByte(CP_ACP, 0, str, -1, prst->content, 64, nullptr, nullptr);
		num++;
	}
	prst->next = nullptr;
	return num;
}

void CheckMailInbox(Account *curAcc)
{
	if (curAcc->IsChecking)
		return;

	curAcc->IsChecking = true;

	ptrA szNick(db_get_sa(curAcc->hContact, "CList", "MyHandle", curAcc->name));

	char *tail = strstr(szNick, " [");
	if (tail) *tail = 0;

	db_set_s(curAcc->hContact, "CList", "MyHandle", CMStringA(FORMAT, "%s [%s]", szNick.get(), Translate("Checking...")));

	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=");
		tail = strchr(curAcc->name, '@');
		if (tail) *tail = 0;
		szBody.Append(curAcc->name);
		if (tail) *tail = '@';
		szBody.Append("&password=");
		szBody.Append(curAcc->pass);

		MHttpRequest nlhr(REQUEST_POST);
		nlhr.m_szUrl = szUrl.GetBuffer();
		nlhr.m_szParam = szBody;
		nlhr.AddHeader("Content-Type", "application/x-www-form-urlencoded");

		NLHR_PTR nlu(Netlib_HttpTransaction(hNetlibUser, &nlhr));
		if (nlu == nullptr || nlu->resultCode != 200) {
			mir_strcpy(curAcc->results.content, Translate("Can't send account data!"));

			curAcc->results_num = -1;
			mir_strcat(curAcc->results.content, "]");
			curAcc->IsChecking = false;
		}
	}

	// go!
	CMStringA loginPass(FORMAT, "%s:%s", curAcc->name, curAcc->pass);
	ptrA loginPassEncoded(mir_base64_encode(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");

	MHttpRequest nlhr(REQUEST_GET);
	nlhr.m_szUrl = szUrl;
	nlhr.AddHeader("Authorization", szAuth.GetBuffer());

	NLHR_PTR nlu(Netlib_HttpTransaction(hNetlibUser, &nlhr));
	if (nlu == nullptr) {
		mir_snprintf(curAcc->results.content, "%s [%s]", szNick.get(), Translate("Wrong name or password!"));
		curAcc->results_num = -1;
	}
	else {
		curAcc->results_num = ParsePage(nlu->body.GetBuffer(), &curAcc->results);
		mir_snprintf(curAcc->results.content, "%s [%d]", szNick.get(), curAcc->results_num);
	}

	curAcc->IsChecking = false;
}

void __cdecl Check_ThreadFunc(void *lpParam)
{
	if (lpParam) {
		CheckMailInbox((Account *)lpParam);
		NotifyUser((Account *)lpParam);
	}
	else {
		for (auto &it : g_accs) {
			if (Proto_GetBaseAccountName(it->hContact)) {
				CheckMailInbox(it);
				NotifyUser(it);
			}
		}
	}
}