summaryrefslogtreecommitdiff
path: root/src/mir_app/src/auth.cpp
blob: d6c76c1bfb9399c60ec307428bd436522d7df285 (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (C) 2012-19 Miranda NG team (https://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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"

/////////////////////////////////////////////////////////////////////////////////////////
// Auth Request dialog

class CAuthReqDlg : public CDlgBase
{
	MEVENT m_hDbEvent;
	MCONTACT m_hContact;
	const char *m_szProto;

	CCtrlCheck chkAdd;
	CCtrlButton btnDetails, btnLater, btnOk, btnCancel;

public:
	CAuthReqDlg(MEVENT hEvent) :
		CDlgBase(g_plugin, IDD_AUTHREQ),
		m_hDbEvent(hEvent),
		btnOk(this, IDOK),
		btnCancel(this, IDCANCEL),
		btnLater(this, IDC_DECIDELATER),
		btnDetails(this, IDC_DETAILS),
		chkAdd(this, IDC_ADDCHECK)
	{
		btnOk.OnClick = Callback(this, &CAuthReqDlg::onClick_OK);
		btnCancel.OnClick = Callback(this, &CAuthReqDlg::onClick_Cancel);
		btnLater.OnClick = Callback(this, &CAuthReqDlg::onClick_Later);
		btnDetails.OnClick = Callback(this, &CAuthReqDlg::onClick_Details);
	}

	bool OnInitDialog() override
	{
		Button_SetIcon_IcoLib(m_hwnd, IDC_DETAILS, SKINICON_OTHER_USERDETAILS, LPGEN("View user's details"));
		Button_SetIcon_IcoLib(m_hwnd, IDC_ADD, SKINICON_OTHER_ADDCONTACT, LPGEN("Add contact permanently to list"));

		int iBlobSize = db_event_getBlobSize(m_hDbEvent);
		if (iBlobSize == -1)
			return false;

		// blob is: uin(DWORD), hcontact(DWORD), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ), reason(ASCIIZ)
		DBEVENTINFO dbei = {};
		dbei.cbBlob = iBlobSize;
		dbei.pBlob = (PBYTE)alloca(dbei.cbBlob);
		if (db_event_get(m_hDbEvent, &dbei))
			return false;

		m_szProto = dbei.szModule;

		DWORD uin = *(PDWORD)dbei.pBlob;
		m_hContact = DbGetAuthEventContact(&dbei);
		char *nick = (char*)dbei.pBlob + sizeof(DWORD) * 2;
		char *first = nick + mir_strlen(nick) + 1;
		char *last = first + mir_strlen(first) + 1;
		char *email = last + mir_strlen(last) + 1;
		char *reason = email + mir_strlen(email) + 1;

		SendMessage(m_hwnd, WM_SETICON, ICON_SMALL, CallProtoService(dbei.szModule, PS_LOADICON, PLI_PROTOCOL | PLIF_SMALL, 0));
		SendMessage(m_hwnd, WM_SETICON, ICON_BIG, CallProtoService(dbei.szModule, PS_LOADICON, PLI_PROTOCOL | PLIF_LARGE, 0));

		ptrW lastT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(last) : mir_a2u(last));
		ptrW firstT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(first) : mir_a2u(first));
		ptrW nickT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(nick) : mir_a2u(nick));
		ptrW emailT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(email) : mir_a2u(email));
		ptrW reasonT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(reason) : mir_a2u(reason));

		wchar_t name[128] = L"";
		int off = 0;
		if (firstT[0] && lastT[0])
			off = mir_snwprintf(name, L"%s %s", (wchar_t*)firstT, (wchar_t*)lastT);
		else if (firstT[0])
			off = mir_snwprintf(name, L"%s", (wchar_t*)firstT);
		else if (lastT[0])
			off = mir_snwprintf(name, L"%s", (wchar_t*)lastT);
		if (mir_wstrlen(nickT)) {
			if (off)
				mir_snwprintf(name + off, _countof(name) - off, L" (%s)", (wchar_t*)nickT);
			else
				wcsncpy_s(name, nickT, _TRUNCATE);
		}
		if (!name[0])
			wcsncpy_s(name, TranslateT("<Unknown>"), _TRUNCATE);

		PROTOACCOUNT *acc = Proto_GetAccount(dbei.szModule);

		wchar_t hdr[256];
		if (uin && emailT[0])
			mir_snwprintf(hdr, TranslateT("%s requested authorization\n%u (%s) on %s"), name, uin, (wchar_t*)emailT, acc->tszAccountName);
		else if (uin)
			mir_snwprintf(hdr, TranslateT("%s requested authorization\n%u on %s"), name, uin, acc->tszAccountName);
		else
			mir_snwprintf(hdr, TranslateT("%s requested authorization\n%s on %s"), name, emailT[0] ? (wchar_t*)emailT : TranslateT("(Unknown)"), acc->tszAccountName);

		SetDlgItemText(m_hwnd, IDC_HEADERBAR, hdr);
		SetDlgItemText(m_hwnd, IDC_REASON, reasonT);

		if (m_hContact == INVALID_CONTACT_ID || !db_get_b(m_hContact, "CList", "NotOnList", 0))
			ShowWindow(GetDlgItem(m_hwnd, IDC_ADD), FALSE);

		SendDlgItemMessage(m_hwnd, IDC_DENYREASON, EM_LIMITTEXT, 255, 0);
		if (CallProtoService(dbei.szModule, PS_GETCAPS, PFLAGNUM_4, 0) & PF4_NOAUTHDENYREASON) {
			EnableWindow(GetDlgItem(m_hwnd, IDC_DENYREASON), FALSE);
			SetDlgItemText(m_hwnd, IDC_DENYREASON, TranslateT("Feature is not supported by protocol"));
		}

		if (!db_get_b(m_hContact, "CList", "NotOnList", 0)) {
			chkAdd.Disable();
			chkAdd.SetState(false);
		}
		else chkAdd.SetState(true);
		return true;
	}

	void OnDestroy() override
	{
		Button_FreeIcon_IcoLib(m_hwnd, IDC_ADD);
		Button_FreeIcon_IcoLib(m_hwnd, IDC_DETAILS);
		DestroyIcon((HICON)SendMessage(m_hwnd, WM_SETICON, ICON_BIG, 0));
		DestroyIcon((HICON)SendMessage(m_hwnd, WM_SETICON, ICON_SMALL, 0));
	}

	void onClick_OK(CCtrlButton*)
	{
		CallProtoService(m_szProto, PS_AUTHALLOW, m_hDbEvent, 0);

		if (chkAdd.GetState())
			Contact_AddByEvent(m_hDbEvent, m_hwnd);
	}

	void onClick_Cancel(CCtrlButton*)
	{
		if (IsWindowEnabled(GetDlgItem(m_hwnd, IDC_DENYREASON))) {
			wchar_t tszReason[256];
			GetDlgItemText(m_hwnd, IDC_DENYREASON, tszReason, _countof(tszReason));
			CallProtoService(m_szProto, PS_AUTHDENY, m_hDbEvent, (LPARAM)tszReason);
		}
		else CallProtoService(m_szProto, PS_AUTHDENY, m_hDbEvent, 0);
	}

	void onClick_Later(CCtrlButton*)
	{
		Close();
	}

	void onClick_Details(CCtrlButton*)
	{
		CallService(MS_USERINFO_SHOWDIALOG, m_hContact, 0);
	}
};

static INT_PTR ShowReqWindow(WPARAM, LPARAM lParam)
{
	(new CAuthReqDlg(((CLISTEVENT *)lParam)->hDbEvent))->Show();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

class CAddedDlg : public CDlgBase
{
	MEVENT m_hDbEvent;
	MCONTACT m_hContact;

	CCtrlButton btnDetails, btnAdd, btnOk;

public:
	CAddedDlg(MEVENT hEvent) :
		CDlgBase(g_plugin, IDD_ADDED),
		m_hDbEvent(hEvent),
		btnOk(this, IDOK),
		btnAdd(this, IDC_ADD),
		btnDetails(this, IDC_DETAILS)
	{
		btnOk.OnClick = Callback(this, &CAddedDlg::onClick_OK);
		btnAdd.OnClick = Callback(this, &CAddedDlg::onClick_Add);
		btnDetails.OnClick = Callback(this, &CAddedDlg::onClick_Details);
	}

	bool OnInitDialog() override
	{
		Button_SetIcon_IcoLib(m_hwnd, IDC_DETAILS, SKINICON_OTHER_USERDETAILS, LPGEN("View user's details"));
		Button_SetIcon_IcoLib(m_hwnd, IDC_ADD, SKINICON_OTHER_ADDCONTACT, LPGEN("Add contact permanently to list"));

		// blob is: uin(DWORD), hcontact(HANDLE), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ)
		DBEVENTINFO dbei = {};
		dbei.cbBlob = db_event_getBlobSize(m_hDbEvent);
		dbei.pBlob = (PBYTE)alloca(dbei.cbBlob);
		db_event_get(m_hDbEvent, &dbei);

		m_hContact = DbGetAuthEventContact(&dbei);

		DWORD uin = *(PDWORD)dbei.pBlob;
		char* nick = (char*)dbei.pBlob + sizeof(DWORD) * 2;
		char* first = nick + mir_strlen(nick) + 1;
		char* last = first + mir_strlen(first) + 1;
		char* email = last + mir_strlen(last) + 1;

		SendMessage(m_hwnd, WM_SETICON, ICON_SMALL, CallProtoService(dbei.szModule, PS_LOADICON, PLI_PROTOCOL | PLIF_SMALL, 0));
		SendMessage(m_hwnd, WM_SETICON, ICON_BIG, CallProtoService(dbei.szModule, PS_LOADICON, PLI_PROTOCOL | PLIF_LARGE, 0));

		PROTOACCOUNT* acc = Proto_GetAccount(dbei.szModule);

		ptrW lastT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(last) : mir_a2u(last));
		ptrW firstT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(first) : mir_a2u(first));
		ptrW nickT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(nick) : mir_a2u(nick));
		ptrW emailT(dbei.flags & DBEF_UTF ? mir_utf8decodeW(email) : mir_a2u(email));

		wchar_t name[128] = L"";
		int off = 0;
		if (firstT[0] && lastT[0])
			off = mir_snwprintf(name, L"%s %s", firstT, lastT);
		else if (firstT[0])
			off = mir_snwprintf(name, L"%s", firstT);
		else if (lastT[0])
			off = mir_snwprintf(name, L"%s", lastT);
		if (nickT[0]) {
			if (off)
				mir_snwprintf(name + off, _countof(name) - off, L" (%s)", nickT);
			else
				wcsncpy_s(name, nickT, _TRUNCATE);
		}
		if (!name[0])
			wcsncpy_s(name, TranslateT("<Unknown>"), _TRUNCATE);

		wchar_t hdr[256];
		if (uin && emailT[0])
			mir_snwprintf(hdr, TranslateT("%s added you to the contact list\n%u (%s) on %s"), name, uin, emailT, acc->tszAccountName);
		else if (uin)
			mir_snwprintf(hdr, TranslateT("%s added you to the contact list\n%u on %s"), name, uin, acc->tszAccountName);
		else
			mir_snwprintf(hdr, TranslateT("%s added you to the contact list\n%s on %s"), name, emailT[0] ? emailT : TranslateT("(Unknown)"), acc->tszAccountName);
		SetDlgItemText(m_hwnd, IDC_HEADERBAR, hdr);

		if (m_hContact == INVALID_CONTACT_ID || !db_get_b(m_hContact, "CList", "NotOnList", 0))
			ShowWindow(GetDlgItem(m_hwnd, IDC_ADD), FALSE);
		return true;
	}
			
	void OnDestroy() override
	{
		Button_FreeIcon_IcoLib(m_hwnd, IDC_ADD);
		Button_FreeIcon_IcoLib(m_hwnd, IDC_DETAILS);
		DestroyIcon((HICON)SendMessage(m_hwnd, WM_SETICON, ICON_BIG, 0));
		DestroyIcon((HICON)SendMessage(m_hwnd, WM_SETICON, ICON_SMALL, 0));
	}

	void onClick_OK(CCtrlButton*)
	{
		Contact_AddByEvent(m_hDbEvent, m_hwnd);
	}

	void onClick_Add(CCtrlButton*)
	{
		Contact_AddByEvent(m_hDbEvent, m_hwnd);

		if (m_hContact == INVALID_CONTACT_ID || !db_get_b(m_hContact, "CList", "NotOnList", 0))
			ShowWindow(GetDlgItem(m_hwnd, IDC_ADD), FALSE);
	}

	void onClick_Details(CCtrlButton*)
	{
		CallService(MS_USERINFO_SHOWDIALOG, m_hContact, 0);
	}
};

static INT_PTR ShowAddedWindow(WPARAM, LPARAM lParam)
{
	(new CAddedDlg(((CLISTEVENT *)lParam)->hDbEvent))->Show();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

#define MS_AUTH_SHOWADDED   "Auth/ShowAdded"
#define MS_AUTH_SHOWREQUEST "Auth/ShowRequest"

static int AuthEventAdded(WPARAM, LPARAM lParam)
{
	wchar_t szTooltip[256];
	MEVENT hDbEvent = (MEVENT)lParam;

	DBEVENTINFO dbei = {};
	db_event_get(lParam, &dbei);
	if (dbei.flags & (DBEF_SENT | DBEF_READ) || (dbei.eventType != EVENTTYPE_AUTHREQUEST && dbei.eventType != EVENTTYPE_ADDED))
		return 0;

	dbei.cbBlob = db_event_getBlobSize(hDbEvent);
	dbei.pBlob = (PBYTE)alloca(dbei.cbBlob);
	db_event_get(hDbEvent, &dbei);

	MCONTACT hContact = DbGetAuthEventContact(&dbei);

	CLISTEVENT cle = {};
	cle.hContact = hContact;
	cle.szTooltip.w = szTooltip;
	cle.flags = CLEF_UNICODE;
	cle.lParam = lParam;
	cle.hDbEvent = hDbEvent;

	ptrW szUid(Contact_GetInfo(CNF_UNIQUEID, hContact));

	if (dbei.eventType == EVENTTYPE_AUTHREQUEST) {
		Skin_PlaySound("AuthRequest");
		if (szUid)
			mir_snwprintf(szTooltip, TranslateT("%s requests authorization"), szUid);
		else
			mir_snwprintf(szTooltip, TranslateT("%u requests authorization"), *(PDWORD)dbei.pBlob);

		cle.hIcon = Skin_LoadIcon(SKINICON_AUTH_REQUEST);
		cle.pszService = MS_AUTH_SHOWREQUEST;
		g_clistApi.pfnAddEvent(&cle);
	}
	else if (dbei.eventType == EVENTTYPE_ADDED) {
		Skin_PlaySound("AddedEvent");
		if (szUid)
			mir_snwprintf(szTooltip, TranslateT("%s added you to their contact list"), szUid);
		else
			mir_snwprintf(szTooltip, TranslateT("%u added you to their contact list"), *(PDWORD)dbei.pBlob);

		cle.hIcon = Skin_LoadIcon(SKINICON_AUTH_ADD);
		cle.pszService = MS_AUTH_SHOWADDED;
		g_clistApi.pfnAddEvent(&cle);
	}
	return 0;
}

static void CALLBACK LaunchAuth()
{
	HookEvent(ME_DB_EVENT_ADDED, AuthEventAdded);
}

int LoadSendRecvAuthModule(void)
{
	CreateServiceFunction(MS_AUTH_SHOWREQUEST, ShowReqWindow);
	CreateServiceFunction(MS_AUTH_SHOWADDED, ShowAddedWindow);
	Miranda_WaitOnHandle(LaunchAuth);

	g_plugin.addSound("AuthRequest", LPGENW("Alerts"), LPGENW("Authorization request"));
	g_plugin.addSound("AddedEvent", LPGENW("Alerts"), LPGENW("Added event"));
	return 0;
}