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

template<typename T>
T Var_To(VARIANTARG &pVar, char strType = 'W')
{
	T retVal = NULL;
	switch (pVar.vt)
	{
	case VT_BSTR:
		if (strType == 'U')
			retVal = (T)mir_utf8encodeW(pVar.bstrVal);
		else if (strType == 'A')
			retVal = (T)mir_u2a(pVar.bstrVal);
		else
			retVal = (T)pVar.bstrVal;
		break;
	case VT_INT:
	case VT_I1:
	case VT_I2:
	case VT_I4:
	case VT_I8:
		retVal = (T)pVar.intVal;
		break;
	}
	return retVal;
}

namespace External
{
	HRESULT mir_CallService(DISPPARAMS *pDispParams, VARIANT *pVarResult)
	{
		if (pDispParams == nullptr || pDispParams->cArgs < 3)
			return E_INVALIDARG;

		wchar_t wType = 'W', lType = 'W';
		if (pDispParams->cArgs >= 5) lType = pDispParams->rgvarg[4].bstrVal[0];
		if (pDispParams->cArgs >= 4) wType = pDispParams->rgvarg[3].bstrVal[0];

		BSTR szName = pDispParams->rgvarg[2].bstrVal;
		WPARAM wParam = Var_To<WPARAM>(pDispParams->rgvarg[1], wType);
		LPARAM lParam = Var_To<LPARAM>(pDispParams->rgvarg[0], lType);

		INT_PTR res = CallService(_T2A((wchar_t*)szName), wParam, lParam);

		if (wType == 'A' || wType == 'U') mir_free((void*)wParam);
		if (lType == 'A' || lType == 'U') mir_free((void*)lParam);

		if (pVarResult != nullptr)
		{
			pVarResult->vt = VT_UINT;
			pVarResult->uintVal = (UINT)res;
		}
		return S_OK;
	}

	HRESULT mir_CallContactService(DISPPARAMS *pDispParams, VARIANT *pVarResult)
	{
		if (pDispParams == nullptr || pDispParams->cArgs < 4)
			return E_INVALIDARG;

		wchar_t wType = 'W', lType = 'W';
		if (pDispParams->cArgs >= 6) lType = pDispParams->rgvarg[5].bstrVal[0];
		if (pDispParams->cArgs >= 5) wType = pDispParams->rgvarg[4].bstrVal[0];

		MCONTACT hContact = pDispParams->rgvarg[3].intVal;
		BSTR szName = pDispParams->rgvarg[2].bstrVal;
		WPARAM wParam = Var_To<WPARAM>(pDispParams->rgvarg[1], wType);
		LPARAM lParam = Var_To<LPARAM>(pDispParams->rgvarg[0], lType);

		INT_PTR res = ProtoChainSend(hContact, _T2A((wchar_t*)szName), wParam, lParam);

		if (wType == 'A' || wType == 'U') mir_free((void*)wParam);
		if (lType == 'A' || lType == 'U') mir_free((void*)lParam);

		if (pVarResult != nullptr)
		{
			pVarResult->vt = VT_UINT;
			pVarResult->uintVal = (UINT)res;
		}
		return S_OK;
	}

	HRESULT IEView_GetCurrentContact(IEView *self, DISPPARAMS*, VARIANT *pVarResult)
	{
		if (pVarResult != nullptr)
		{
			pVarResult->vt = VT_UINT;
			pVarResult->uintVal = self->Get_CurrentContact();
		}
		return S_OK;
	}

	HRESULT db_get(DISPPARAMS *pDispParams, VARIANT *pVarResult)
	{
		if (pDispParams->cArgs < 3)
			return E_INVALIDARG;

		MCONTACT hContact = pDispParams->rgvarg[2].intVal;
		BSTR szModule = pDispParams->rgvarg[1].bstrVal;
		BSTR szSetting = pDispParams->rgvarg[0].bstrVal;

		DBVARIANT dbv = { 0 };

		if (db_get(hContact, _T2A((wchar_t*)szModule), _T2A((wchar_t*)szSetting), &dbv)) 
			return S_OK;

		if (pVarResult != nullptr)
		{
			switch (dbv.type)
			{
			case DBVT_BYTE:
				//pVarResult->bVal = dbv.bVal != 0;
				//pVarResult->vt = VT_BOOL;
				pVarResult->vt = VT_UI1;
				pVarResult->bVal = dbv.bVal;
				break;
			case DBVT_WCHAR:
				pVarResult->vt = VT_BSTR;
				pVarResult->bstrVal = ::SysAllocString(dbv.pwszVal);
				break;
			case DBVT_UTF8:
				pVarResult->vt = VT_BSTR;
				pVarResult->bstrVal = ::SysAllocString(ptrW(mir_utf8decodeW(dbv.pszVal)));
				break;
			case DBVT_ASCIIZ:
				pVarResult->vt = VT_BSTR;
				pVarResult->bstrVal = ::SysAllocString(_A2T(dbv.pszVal));
				break;
			case DBVT_DWORD:
				pVarResult->vt = VT_INT;
				pVarResult->intVal = dbv.dVal;
				break;
			case DBVT_WORD:
				pVarResult->vt = VT_I2;
				pVarResult->iVal = dbv.wVal;
				break;
			}
		}
		db_free(&dbv);
		return S_OK;
	}

	HRESULT db_set(DISPPARAMS *pDispParams, VARIANT *pVarResult)
	{
		if (pDispParams == nullptr || pDispParams->cArgs < 4)
			return E_INVALIDARG;

		MCONTACT hContact = pDispParams->rgvarg[3].intVal;
		BSTR szModule = pDispParams->rgvarg[2].bstrVal;
		BSTR szSetting = pDispParams->rgvarg[1].bstrVal;

		DBVARIANT dbv = { 0 };

		VARIANT& pVal = pDispParams->rgvarg[0];

		switch (pVal.vt)
		{
		case VT_BSTR:
			dbv.type = DBVT_WCHAR;
			dbv.pwszVal = mir_wstrdup(pVal.bstrVal);
			break;
		case VT_INT:
		case VT_I1:
		case VT_I2:
		case VT_I4:
		case VT_I8:
			dbv.type = DBVT_DWORD;
			dbv.dVal = pVal.intVal;
			break;
		case VT_BOOL:
			dbv.type = DBVT_BYTE;
			dbv.bVal = pVal.boolVal;
			break;
		default: 
			return E_INVALIDARG;
		}

		INT_PTR res = ::db_set(hContact, _T2A((wchar_t*)szModule), _T2A((wchar_t*)szSetting), &dbv);

		if (pVarResult != nullptr)
		{
			pVarResult->vt = VT_INT;
			pVarResult->intVal = (int)res;
		}
		return S_OK;
	}

	HRESULT win32_ShellExecute(DISPPARAMS *pDispParams, VARIANT *pVarResult)
	{
		if (pDispParams == nullptr || pDispParams->cArgs < 5)
			return E_INVALIDARG;

		HINSTANCE res = ShellExecuteW(nullptr, pDispParams->rgvarg[4].bstrVal, pDispParams->rgvarg[3].bstrVal, pDispParams->rgvarg[2].bstrVal, pDispParams->rgvarg[1].bstrVal, pDispParams->rgvarg[0].intVal);

		if (pVarResult != nullptr)
		{
			pVarResult->vt = VT_HANDLE;
			pVarResult->ullVal = (ULONGLONG)res;
		}

		return S_OK;
	}

	HRESULT win32_CopyToClipboard(DISPPARAMS *pDispParams, VARIANT*)
	{
		if (pDispParams == nullptr || pDispParams->cArgs < 1)
			return E_INVALIDARG;

		BSTR data = pDispParams->rgvarg[0].bstrVal;
		if (OpenClipboard(nullptr)) 
		{
			EmptyClipboard();
			size_t size = sizeof(wchar_t)* (mir_wstrlen(data) + 1);
			HGLOBAL hClipboardData = GlobalAlloc(0, size);
			if (hClipboardData) 
			{
				wchar_t *pchData = (wchar_t*)GlobalLock(hClipboardData);
				if (pchData) 
				{
					memcpy(pchData, (wchar_t*)data, size);
					GlobalUnlock(hClipboardData);
					SetClipboardData(CF_UNICODETEXT, hClipboardData);
				}
			}
			CloseClipboard();
		}
		return S_OK;
	}
}