summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/utils.cpp
blob: c131dda0f88767f086c7aea0cb1ba5a8e276d886 (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
#include "stdafx.h"

wchar_t* utils::removeA(wchar_t *str)
{
	if (str == NULL)
		return NULL;

	wchar_t *p = wcschr(str, '@');
	if (p) *p = 0;
	return str;
}

void utils::copyText(HWND hwnd, const wchar_t *text)
{
	if (!hwnd || !text) return;

	if (!OpenClipboard(hwnd))
		return;

	EmptyClipboard();
	HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t)*(mir_wstrlen(text) + 1));
	mir_wstrcpy((wchar_t*)GlobalLock(hMem), text);
	GlobalUnlock(hMem);
	SetClipboardData(CF_UNICODETEXT, hMem);
	CloseClipboard();
}

std::string getLastErrorMsg()
{
	LPVOID lpMsgBuf;
	DWORD dw = WSAGetLastError(); // retrieve the system error message for the last-error code

	FormatMessageA(
		FORMAT_MESSAGE_ALLOCATE_BUFFER |
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		dw,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPSTR)&lpMsgBuf,
		0, NULL);

	std::string ret((LPSTR)lpMsgBuf);
	LocalFree(lpMsgBuf);
	return ret;
}

void utils::setStatusMessage(MCONTACT hContact, const wchar_t *ptszMessage)
{
	if (ptszMessage != NULL) {
		StatusTextData st = { 0 };
		st.cbSize = sizeof(st);
		st.hIcon = Skin_LoadIcon(SKINICON_EVENT_MESSAGE);
		wcsncpy_s(st.tszText, ptszMessage, _TRUNCATE);
		CallService(MS_MSG_SETSTATUSTEXT, hContact, (LPARAM)&st);
	}
	else CallService(MS_MSG_SETSTATUSTEXT, hContact, NULL);
}

BYTE* utils::md5string(const BYTE *data, int size, BYTE *digest)
{
	mir_md5_state_t md5_state;
	mir_md5_init(&md5_state);
	mir_md5_append(&md5_state, data, size);
	mir_md5_finish(&md5_state, digest);
	return digest;
}

/////////////////////////////////////////////////////////////////////////////////////////
// external stubs for WhatsAPI++

std::string base64_encode(void* pData, size_t len)
{
	return (char*)ptrA(mir_base64_encode((BYTE*)pData, (unsigned)len));
}

void md5_string(const std::string &data, BYTE digest[16])
{
	utils::md5string(data, digest);
}

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		if (item.length() > 0) {
			elems.push_back(item);
		}
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}