summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/password.cpp
blob: b3184d18c0e0803370e953330ae7ccd594a880f3 (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
#include "stdafx.h"

struct PasswordData
{
	MCONTACT hContact;
	int attempt;
};

bool CheckPassword(MCONTACT hContact, char *password)
{
	mir_md5_state_t ms;
	BYTE digest[16];
	mir_md5_init(&ms);
	mir_md5_append(&ms, (const unsigned char *)password, lstrlenA(password));
	mir_md5_finish(&ms, digest);

	DBVARIANT dbv;
	db_get(hContact, MODULENAME, "password", &dbv);
	if (dbv.type != DBVT_BLOB) {
		db_free(&dbv);
		return true;
	}
	else {
		if (dbv.cpbVal != 16) {
			db_free(&dbv);
			return false;
		}
		for (int i = 0; i < 16; i++) {
			if (dbv.pbVal[i] != digest[i]) {
				db_free(&dbv);
				return false;
			}
		}

		db_free(&dbv);
		return true;
	}
}

void SetPassword(MCONTACT hContact, char *password)
{
	if (!password || !*password) {
		db_unset(hContact, MODULENAME, "password");
		return;
	}

	mir_md5_state_t ms;
	BYTE digest[16];
	mir_md5_init(&ms);
	mir_md5_append(&ms, (const unsigned char *)password, lstrlenA(password));
	mir_md5_finish(&ms, digest);

	db_set_blob(hContact, MODULENAME, "password", (PBYTE)digest, 16);
}

INT_PTR CALLBACK PasswordDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PasswordData *data = (PasswordData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
	switch (msg) {
	case WM_INITDIALOG:
		data = new PasswordData;
		data->attempt = 0;
		data->hContact = (MCONTACT)lParam;
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data);

		RECT rc, rcMe;
		SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
		GetWindowRect(hwnd, &rcMe);
		SetWindowPos(hwnd, hwnd,
			rc.left + ((rc.right - rc.left) - (rcMe.right - rcMe.left)) / 2,
			rc.top + ((rc.bottom - rc.top) - (rcMe.bottom - rcMe.top)) / 2,
			0, 0, SWP_NOSIZE | SWP_NOZORDER);

		SetFocus(GetDlgItem(hwnd, IDC_PASSWORD));
		return TRUE;

	case WM_COMMAND:
		{
			switch (LOWORD(wParam)) {
			case IDOK:
				data->attempt++;
				{
					int length = GetWindowTextLengthA(GetDlgItem(hwnd, IDC_PASSWORD));
					char *input = new char[length + 1];
					GetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD), input, length + 1);
					if (CheckPassword(data->hContact, input)) {
						MessageBeep(MB_OK);
						EndDialog(hwnd, 1);
					}
					else {
						if (data->attempt >= 3) {
							MessageBoxA(hwnd, "Sorry, but you've not managed to type\r\nthe correct password in 3 attempts.", "Newstory", MB_ICONHAND | MB_OK);
							EndDialog(hwnd, 0);
						}
						else {
							MessageBeep(MB_ICONHAND);
							SetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD), "");
						}
					}
					delete[] input;
				}
				return TRUE;

			case IDCANCEL:
				EndDialog(hwnd, 0);
				return TRUE;
			}
			break;
		}

	case WM_CLOSE:
		DestroyWindow(hwnd);
		return TRUE;

	case WM_DESTROY:
		delete data;
		data = 0;
		SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
	}
	return FALSE;
}

bool AskPassword(MCONTACT hContact)
{
	DBVARIANT dbv;
	db_get(hContact, MODULENAME, "password", &dbv);
	if (dbv.type != DBVT_BLOB) {
		db_free(&dbv);
		return true;
	}
	db_free(&dbv);

	if (DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_PASSWORD), 0, PasswordDlgProc, (LPARAM)hContact))
		return true;
	return false;
}


INT_PTR CALLBACK ChangePasswordDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)GetWindowLongPtr(hwnd, GWLP_USERDATA);
	switch (msg) {
	case WM_INITDIALOG:
		{
			hContact = (MCONTACT)lParam;
			SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)hContact);

			// RECT rc, rcMe;
			// SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
			// GetWindowRect(hwnd, &rcMe);
			// SetWindowPos(hwnd, hwnd,
			// rc.left+((rc.right-rc.left) - (rcMe.right-rcMe.left))/2, 
			// rc.top+((rc.bottom-rc.top) - (rcMe.bottom-rcMe.top))/2, 
			// 0, 0, SWP_NOSIZE|SWP_NOZORDER);

			SetFocus(GetDlgItem(hwnd, IDC_PASSWORD));
			return TRUE;
		}

	case WM_COMMAND:
		{
			switch (LOWORD(wParam)) {
			case IDOK:
				{
					int length = GetWindowTextLengthA(GetDlgItem(hwnd, IDC_PASSWORD));
					char *input = new char[length + 1];
					GetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD), input, length + 1);

					length = GetWindowTextLengthA(GetDlgItem(hwnd, IDC_PASSWORD2));
					char *input2 = new char[length + 1];
					GetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD2), input2, length + 1);

					length = GetWindowTextLengthA(GetDlgItem(hwnd, IDC_PASSWORD3));
					char *input3 = new char[length + 1];
					GetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD3), input3, length + 1);

					if (lstrcmpA(input2, input3) || !CheckPassword(hContact, input)) {
						MessageBeep(MB_ICONHAND);
						SetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD), "");
						SetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD2), "");
						SetWindowTextA(GetDlgItem(hwnd, IDC_PASSWORD3), "");
					}
					else {
						SetPassword(hContact, input2);
						EndDialog(hwnd, 0);
					}

					delete[] input;
					delete[] input2;
					delete[] input3;
				}
				return TRUE;

			case IDCANCEL:
				EndDialog(hwnd, 0);
				return TRUE;
			}
			break;
		}

	case WM_CLOSE:
		EndDialog(hwnd, 0);
		DestroyWindow(hwnd);
		return TRUE;

	case WM_DESTROY:
		SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
	}
	return FALSE;
}

void ChangePassword(HWND hwnd, MCONTACT hContact)
{
	DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_CHANGEPASSWORD), hwnd, ChangePasswordDlgProc, (LPARAM)hContact);
}