summaryrefslogtreecommitdiff
path: root/protocols/GTalkExt/inbox.cpp
blob: 53a4aa2fcbe5d27c85cf3f593665572e19eec0dd (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//***************************************************************************************
//
//   Google Extension plugin for the Miranda IM's Jabber protocol
//   Copyright (c) 2011 bems@jabber.org, George Hazan (ghazan@jabber.ru)
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//***************************************************************************************

#include "stdafx.h"
#include "inbox.h"
#include "notifications.h"
#include "db.h"
#include "options.h"

static const LPTSTR COMMON_GMAIL_HOST1 = _T("gmail.com");
static const LPTSTR COMMON_GMAIL_HOST2 = _T("googlemail.com");

static const LPSTR AUTH_REQUEST_URL = "https://www.google.com/accounts/ClientAuth";
static const LPSTR AUTH_REQUEST_PARAMS = "Email=%s&Passwd=%s&"
	"accountType=HOSTED_OR_GOOGLE&"
	"skipvpage=true&"
	"PersistentCookie=false";

static const LPSTR ISSUE_TOKEN_REQUEST_URL = "https://www.google.com/accounts/IssueAuthToken";
static const LPSTR ISSUE_TOKEN_REQUEST_PARAMS = "SID=%s&LSID=%s&"
	"Session=true&"
	"skipvpage=true&"
	"service=gaia";

static const LPSTR TOKEN_AUTH_URL = "https://www.google.com/accounts/TokenAuth?"\
	"auth=%s&"
	"service=mail&"
	"continue=%s&"
	"source=googletalk";


static const NETLIBHTTPHEADER HEADER_URL_ENCODED = {"Content-Type", "application/x-www-form-urlencoded"};
static const int HTTP_OK = 200;

static const LPSTR SID_KEY_NAME = "SID=";
static const LPSTR LSID_KEY_NAME = "LSID=";

static const LPSTR LOGIN_PASS_SETTING_NAME = "LoginPassword";

static const LPTSTR INBOX_URL_FORMAT = _T("https://mail.google.com/%s%s/#inbox");

static const DWORD SIZE_OF_JABBER_OPTIONS = 243 * sizeof(DWORD);

// 3 lines from netlib.h
#define GetNetlibHandleType(h)  (h?*(int*)h:NLH_INVALID)
#define NLH_INVALID      0
#define NLH_USER         'USER'

char to_hex(char code) {
  static char hex[] = "0123456789abcdef";
  return hex[code & 15];
}

char *url_encode(char *str) {
  char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
  while (*pstr) {
    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == ',' || *pstr == '~')
      *pbuf++ = *pstr;
    else if (*pstr == ' ')
      *pbuf++ = '+';
    else
      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}

LPSTR HttpPost(HANDLE hUser, LPSTR reqUrl, LPSTR reqParams)
{
	NETLIBHTTPREQUEST nlhr = {0};
	nlhr.cbSize = sizeof(nlhr);
	nlhr.requestType = REQUEST_POST;
	nlhr.flags = NLHRF_GENERATEHOST | NLHRF_SMARTAUTHHEADER | NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMP | NLHRF_NODUMPHEADERS;
	nlhr.szUrl = reqUrl;
	nlhr.headers = (NETLIBHTTPHEADER*)&HEADER_URL_ENCODED;
	nlhr.headersCount = 1;
	nlhr.pData = reqParams;
	nlhr.dataLength = lstrlenA(reqParams);

	NETLIBHTTPREQUEST *pResp = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hUser, (LPARAM)&nlhr);
	if (!pResp) return NULL;
	__try {
		if (HTTP_OK == pResp->resultCode)
			return _strdup(pResp->pData);
		else
			return NULL;
	}
	__finally {
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)pResp);
	}
}

LPSTR MakeRequest(HANDLE hUser, LPSTR reqUrl, LPSTR reqParamsFormat, LPSTR p1, LPSTR p2)
{
	LPSTR encodedP1 = url_encode(p1);
	__try {
		LPSTR encodedP2 = url_encode(p2);
		__try {
			LPSTR reqParams = (LPSTR)malloc(lstrlenA(reqParamsFormat) + 1 + lstrlenA(encodedP1) + lstrlenA(encodedP2));
			__try {
				sprintf(reqParams, reqParamsFormat, encodedP1, encodedP2);
				return HttpPost(hUser, reqUrl, reqParams);
			}
			__finally {
				free(reqParams);
			}
		}
		__finally {
			free(encodedP2);
		}
	}
	__finally {
		free(encodedP1);
	}
}

LPSTR FindSid(LPSTR resp, LPSTR *LSID)
{
	LPSTR SID = strstr(resp, SID_KEY_NAME);
	*LSID = strstr(resp, LSID_KEY_NAME);
	if (!SID || !*LSID) return NULL;

	if (SID - 1 == *LSID) SID = strstr(SID + 1, SID_KEY_NAME);
	if (!SID) return NULL;

	SID += lstrlenA(SID_KEY_NAME);
	LPSTR term = strstr(SID, "\n");
	if (term) term[0] = 0;

	*LSID += lstrlenA(LSID_KEY_NAME);
	term = strstr(*LSID, "\n");
	if (term) term[0] = 0;

	return SID;
}

void DoOpenUrl(LPSTR tokenResp, LPSTR url)
{
	LPSTR encodedUrl = url_encode(url);
	__try {
		LPSTR encodedToken = url_encode(tokenResp);
		__try {
			LPSTR composedUrl = (LPSTR)malloc(lstrlenA(TOKEN_AUTH_URL) + 1 + lstrlenA(encodedToken) + lstrlenA(encodedUrl));
			__try {
				sprintf(composedUrl, TOKEN_AUTH_URL, encodedToken, encodedUrl);
				ShellExecuteA(0, NULL, composedUrl, NULL, NULL, SW_SHOW);
			}
			__finally {
				free(composedUrl);
			}
		}
		__finally {
			free(encodedToken);
		}
	}
	__finally {
		free(encodedUrl);
	}
}

BOOL AuthAndOpen(HANDLE hUser, LPSTR url, LPSTR mailbox, LPSTR pwd)
{
	LPSTR authResp = MakeRequest(hUser, AUTH_REQUEST_URL, AUTH_REQUEST_PARAMS, mailbox, pwd);
	if (!authResp) return FALSE;

	__try {
		LPSTR LSID;
		LPSTR SID = FindSid(authResp, &LSID);
		LPSTR tokenResp = MakeRequest(hUser, ISSUE_TOKEN_REQUEST_URL, ISSUE_TOKEN_REQUEST_PARAMS, SID, LSID);
		if (!tokenResp) return FALSE;

		__try {
			DoOpenUrl(tokenResp, url);
			return TRUE;
		}
		__finally {
			free(tokenResp);
		}
	}
	__finally {
		free(authResp);
	}
}

struct OPEN_URL_HEADER {
	LPSTR url;
	LPSTR mailbox;
	LPSTR pwd;
	LPCSTR acc;
};

HANDLE FindNetUserHandle(LPCSTR acc)
{
	IJabberInterface *ji = getJabberApi(acc);
	if (!ji) return NULL;

	PBYTE m_psProto = *(PBYTE*)((PBYTE)ji + sizeof(*ji));   // see CJabberInterface in jabber_proto.h

	PHANDLE pResult = (PHANDLE)(m_psProto +                                 // see CJabberProto in jabber_proto.h
		sizeof(PVOID) +                                                                         // skip vtable ptr
		sizeof(PVOID) +                                                                         // skip m_ThreadInfo
		SIZE_OF_JABBER_OPTIONS);                                                        // skip m_options

	for (int i=0; i < 100; i++) {
		__try {
			if (GetNetlibHandleType(*pResult) == NLH_USER)
				break;
		}
		__except (EXCEPTION_EXECUTE_HANDLER){
		}
		pResult++;
	}

	assert(GetNetlibHandleType(*pResult) == NLH_USER);
	return *pResult;
}

unsigned __stdcall OpenUrlThread(OPEN_URL_HEADER* data)
{
	__try {
		HANDLE hUser = FindNetUserHandle(data->acc);
		if (!hUser || !AuthAndOpen(hUser, data->url, data->mailbox, data->pwd))
			ShellExecuteA(0, NULL, data->url, NULL, NULL, SW_SHOW);
	}
	__finally {
		free(data);
	}
	return 0;
}

void __forceinline DecryptString(LPSTR str, int len)
{
	for (--len; len >= 0; len--)
	{
		const char c = str[len] ^ 0xc3;
		if (c) str[len] = c;
	}
}

int GetMailboxPwd(LPCSTR acc, LPCTSTR mailbox, LPSTR *pwd, int buffSize)
{
	char buff[256];

	DBCONTACTGETSETTING cgs;
	DBVARIANT dbv;
	cgs.szModule = acc;
	cgs.szSetting = LOGIN_PASS_SETTING_NAME;
	cgs.pValue = &dbv;
	dbv.type = DBVT_ASCIIZ;
	dbv.pszVal = &buff[0];
	dbv.cchVal = sizeof(buff);
	if (CallService(MS_DB_CONTACT_GETSETTINGSTATIC, 0, (LPARAM)&cgs))
		return 0;

	int result = dbv.cchVal;

	if (pwd) {
		if (buffSize < result + 1) result = buffSize - 1;
		memcpy(*pwd, &buff, result + 1);
		DecryptString(*pwd, result);
	}

	return result;
}

BOOL OpenUrlWithAuth(LPCSTR acc, LPCTSTR mailbox, LPCTSTR url)
{
	int pwdLen = GetMailboxPwd(acc, mailbox, NULL, 0);
	if (!pwdLen++) return FALSE;

	int urlLen = lstrlen(url) + 1;
	int mailboxLen = lstrlen(mailbox) + 1;

	OPEN_URL_HEADER *data = (OPEN_URL_HEADER*)malloc(sizeof(OPEN_URL_HEADER) + urlLen + mailboxLen + pwdLen);
	__try {
		data->url = (LPSTR)data + sizeof(OPEN_URL_HEADER);
		LPSTR ansi = WtoA(url);
		__try {
			memcpy(data->url, ansi, urlLen);
		}
		__finally {
			free(ansi);
		}

		data->mailbox = data->url + urlLen;
		ansi = WtoA(mailbox);
		__try {
			memcpy(data->mailbox, ansi, mailboxLen);
		}
		__finally {
			free(ansi);
		}

		data->pwd = data->mailbox + mailboxLen;
		if (!GetMailboxPwd(acc, mailbox, &data->pwd, pwdLen)) return FALSE;

		data->acc = acc;

		if (HANDLE h = mir_forkthreadex((pThreadFuncEx)OpenUrlThread, data, NULL)) {
			CloseHandle(h);
			data = NULL;
		}
		else return FALSE;
	}
	__finally {
		free(data);
	}

	return TRUE;
}

unsigned __stdcall ShellExecuteThread(PVOID param)
{
	__try {
		ShellExecute(0, NULL, (LPTSTR)param, NULL, NULL, SW_SHOW);
	}
	__finally {
		free(param);
	}
	return 0;
}

void StartShellExecuteThread(LPCTSTR url)
{
	LPTSTR urlCopy = _tcsdup(url);
	__try {
		if (HANDLE h = mir_forkthreadex(ShellExecuteThread, urlCopy, NULL)) {
			CloseHandle(h);
			urlCopy = NULL;
		}
	}
	__finally {
		free(urlCopy);
	}
}

void OpenUrl(LPCSTR acc, LPCTSTR mailbox, LPCTSTR url)
{
	extern DWORD itlsSettings;
	if (!ReadCheckbox(0, IDC_AUTHONMAILBOX, (DWORD)TlsGetValue(itlsSettings)) ||
		!OpenUrlWithAuth(acc, mailbox, url))
			StartShellExecuteThread(url);
}

LPTSTR CraftInboxUrl(LPTSTR jid)
{
	LPTSTR host = _tcsstr(jid, _T("@")) + 1;

	LPTSTR result = (LPTSTR)malloc((lstrlen(INBOX_URL_FORMAT) + 1 + lstrlen(jid)) * sizeof(TCHAR));
	__try {
		if (lstrcmpi(host, COMMON_GMAIL_HOST1) && lstrcmpi(host, COMMON_GMAIL_HOST2))
			wsprintf(result, INBOX_URL_FORMAT, _T("a/"), host);   // hosted
		else
			wsprintf(result, INBOX_URL_FORMAT, NULL, _T("mail")); // common
	}
	__except (
		free(result),
		EXCEPTION_CONTINUE_SEARCH
	) {}

	return result;
}

void OpenContactInbox(HANDLE hContact)
{
	LPSTR acc = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
	if (!acc) return;

	DBVARIANT dbv;
	if (!DBGetContactSettingTString(0, acc, "jid", &dbv))
		__try {
			LPTSTR url = CraftInboxUrl(dbv.ptszVal);
			__try {
				OpenUrl(acc, dbv.ptszVal, url);
			}
			__finally {
				free(url);
			}
	}
	__finally {
		DBFreeVariant(&dbv);
	}
}