summaryrefslogtreecommitdiff
path: root/plugins/CmdLine/src/utils.cpp
blob: c4b742db8b7dd1b0f776260542cc04bcdff96533 (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
/*
CmdLine plugin for Miranda IM

Copyright © 2007 Cristian Libotean

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"

int Info(char *title, char *format, ...)
{
	char str[4096];
	va_list vararg;
	int tBytes;
	va_start(vararg, format);
	tBytes = mir_vsnprintf(str, sizeof(str), format, vararg);
	if (tBytes > 0) {
		str[tBytes] = 0;
	}
	va_end(vararg);
	return MessageBoxA(nullptr, str, title, MB_OK | MB_ICONINFORMATION);
}

int MyPUShowMessage(char *lpzText, BYTE kind)
{
	if (ServiceExists(MS_POPUP_SHOWMESSAGE))
		return PUShowMessage(lpzText, kind);

	char *title = (kind == SM_NOTIFY) ? Translate("Notify") : Translate("Warning");
	return MessageBoxA(nullptr, lpzText, title, MB_OK | (kind == SM_NOTIFY) ? MB_ICONINFORMATION : MB_ICONEXCLAMATION);
}

#define HEX_SIZE 8

char *BinToHex(int size, PBYTE data)
{
	char *szresult = nullptr;
	int maxSize = size * 2 + HEX_SIZE + 1;
	szresult = (char *) new char[maxSize];
	mir_snprintf(szresult, maxSize, "%0*X", HEX_SIZE, size);
	return bin2hex(data, size, szresult + HEX_SIZE);
}

void HexToBin(char *inData, ULONG &size, LPBYTE &outData)
{
	char buffer[32] = { 0 };
	strcpy(buffer, "0x");
	strncpy_s(buffer + 2, HEX_SIZE, inData, _TRUNCATE);
	sscanf(buffer, "%x", &size);
	outData = (unsigned char*)new char[size * 2];

	char *tmp = inData + HEX_SIZE;
	buffer[4] = '\0'; //mark the end of the string
	for (UINT i = 0; i < size; i++) {
		strncpy_s(buffer + 2, 3, &tmp[i * 2], _TRUNCATE);
		sscanf(buffer, "%hhx", &outData[i]);
	}
}

int GetStringFromDatabase(MCONTACT hContact, char *szModule, char *szSettingName, char *szError, char *szResult, size_t size)
{
	DBVARIANT dbv = { 0 };
	int res = 1;
	size_t len;
	dbv.type = DBVT_ASCIIZ;
	if (db_get(hContact, szModule, szSettingName, &dbv) == 0) {
		res = 0;
		size_t tmp = mir_strlen(dbv.pszVal);
		len = (tmp < size - 1) ? tmp : size - 1;
		strncpy(szResult, dbv.pszVal, len);
		szResult[len] = '\0';
		mir_free(dbv.pszVal);
	}
	else {
		res = 1;
		if (szError) {
			size_t tmp = mir_strlen(szError);
			len = (tmp < size - 1) ? tmp : size - 1;
			strncpy(szResult, szError, len);
			szResult[len] = '\0';
		}
		else {
			szResult[0] = '\0';
		}
	}
	return res;
}

int GetStringFromDatabase(MCONTACT hContact, char *szModule, char *szSettingName, WCHAR *szError, WCHAR *szResult, size_t count)
{
	DBVARIANT dbv = { 0 };
	int res = 1;
	size_t len;
	dbv.type = DBVT_WCHAR;
	if (db_get_ws(hContact, szModule, szSettingName, &dbv) == 0) {
		res = 0;
		if (dbv.type != DBVT_WCHAR) {
			MultiByteToWideChar(CP_ACP, 0, dbv.pszVal, -1, szResult, (int)count);
		}
		else {
			size_t tmp = mir_wstrlen(dbv.pwszVal);
			len = (tmp < count - 1) ? tmp : count - 1;
			wcsncpy(szResult, dbv.pwszVal, len);
			szResult[len] = L'\0';
		}
		mir_free(dbv.pwszVal);
	}
	else {
		res = 1;
		if (szError) {
			size_t tmp = mir_wstrlen(szError);
			len = (tmp < count - 1) ? tmp : count - 1;
			wcsncpy(szResult, szError, len);
			szResult[len] = L'\0';
		}
		else {
			szResult[0] = L'\0';
		}
	}
	return res;
}

int GetStringFromDatabase(char *szSettingName, char *szError, char *szResult, size_t size)
{
	return GetStringFromDatabase(0, MODULENAME, szSettingName, szError, szResult, size);
}

int GetStringFromDatabase(char *szSettingName, WCHAR *szError, WCHAR *szResult, size_t count)
{
	return GetStringFromDatabase(0, MODULENAME, szSettingName, szError, szResult, count);
}

#pragma warning (disable: 4312)
wchar_t* GetContactName(MCONTACT hContact, char *szProto)
{
	return Contact_GetInfo(CNF_DISPLAY, hContact, szProto);
}

void GetContactProto(MCONTACT hContact, char *szProto, size_t size)
{
	GetStringFromDatabase(hContact, "Protocol", "p", nullptr, szProto, size);
}

wchar_t* GetContactID(MCONTACT hContact, char *szProto)
{
	return Contact_GetInfo(CNF_UNIQUEID, hContact, szProto);
}

MCONTACT GetContactFromID(wchar_t *szID, char *szProto)
{
	wchar_t dispName[1024];
	char cProtocol[256];

	int found = 0;
	for (auto &hContact : Contacts()) {
		GetContactProto(hContact, cProtocol, sizeof(cProtocol));
		ptrW szHandle(GetContactID(hContact, cProtocol));

		wchar_t *tmp = Clist_GetContactDisplayName(hContact);
		wcsncpy_s(dispName, tmp, _TRUNCATE);

		if ((szHandle) && ((mir_wstrcmpi(szHandle, szID) == 0) || (mir_wstrcmpi(dispName, szID) == 0)) && ((szProto == nullptr) || (_stricmp(szProto, cProtocol) == 0)))
			found = 1;

		if (found)
			return hContact;
	}

	return 0;
}

#pragma warning (default: 4312)

void ScreenToClient(HWND hWnd, LPRECT rect)
{
	POINT pt;
	int cx = rect->right - rect->left;
	int cy = rect->bottom - rect->top;
	pt.x = rect->left;
	pt.y = rect->top;
	ScreenToClient(hWnd, &pt);
	rect->left = pt.x;
	rect->top = pt.y;
	rect->right = pt.x + cx;
	rect->bottom = pt.y + cy;
}

void AnchorMoveWindow(HWND window, const WINDOWPOS *parentPos, int anchors)
{
	RECT rParent;
	RECT rChild;

	if (parentPos->flags & SWP_NOSIZE)
		return;

	GetWindowRect(parentPos->hwnd, &rParent);
	rChild = AnchorCalcPos(window, &rParent, parentPos, anchors);
	MoveWindow(window, rChild.left, rChild.top, rChild.right - rChild.left, rChild.bottom - rChild.top, FALSE);
}

RECT AnchorCalcPos(HWND window, const RECT *rParent, const WINDOWPOS *parentPos, int anchors)
{
	RECT rChild;
	RECT rTmp;

	GetWindowRect(window, &rChild);
	ScreenToClient(parentPos->hwnd, &rChild);

	int cx = rParent->right - rParent->left;
	int cy = rParent->bottom - rParent->top;
	if ((cx == parentPos->cx) && (cy == parentPos->cy))
		return rChild;

	if (parentPos->flags & SWP_NOSIZE)
		return rChild;

	rTmp.left = parentPos->x - rParent->left;
	rTmp.right = (parentPos->x + parentPos->cx) - rParent->right;
	rTmp.bottom = (parentPos->y + parentPos->cy) - rParent->bottom;
	rTmp.top = parentPos->y - rParent->top;

	cx = (rTmp.left) ? -rTmp.left : rTmp.right;
	cy = (rTmp.top) ? -rTmp.top : rTmp.bottom;

	rChild.right += cx;
	rChild.bottom += cy;
	//expanded the window accordingly, now we need to enforce the anchors
	if ((anchors & ANCHOR_LEFT) && (!(anchors & ANCHOR_RIGHT)))
		rChild.right -= cx;

	if ((anchors & ANCHOR_TOP) && (!(anchors & ANCHOR_BOTTOM)))
		rChild.bottom -= cy;

	if ((anchors & ANCHOR_RIGHT) && (!(anchors & ANCHOR_LEFT)))
		rChild.left += cx;

	if ((anchors & ANCHOR_BOTTOM) && (!(anchors & ANCHOR_TOP)))
		rChild.top += cy;

	return rChild;
}