summaryrefslogtreecommitdiff
path: root/plugins/GmailNotifier/src/check.cpp
blob: 5a5e2cc4fce639e5b8d7bd3a07b641fca34b0411 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#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;

	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;
	nlr.headers = headers;
	nlr.headersCount = _countof(headers);

	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);
			}
		}
	}
}