summaryrefslogtreecommitdiff
path: root/plugins/IMO2sProxy/src/imo2skype/w32skypeemu.c
blob: f9cc734cd13f04f751b634c6dbb3604c7fecc538 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdio.h>
#include "imo2sproxy.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define LockMutex(x) EnterCriticalSection (&x)
#define UnlockMutex(x) LeaveCriticalSection(&x)
#define InitMutex(x) InitializeCriticalSection(&x)
#define ExitMutex(x) DeleteCriticalSection(&x)
#define strcasecmp stricmp
#define strncasecmp stricmpn
#define mutex_t CRITICAL_SECTION
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "memlist.h"
#include "w32skypeemu.h"

#ifndef _WIN64
#if WINVER<0x0500
#define SetWindowLongPtr SetWindowLong
#define GetWindowLongPtr GetWindowLong
#endif
#ifndef LONG_PTR
#define LONG_PTR LONG
#endif
#ifndef GWLP_USERDATA
#define GWLP_USERDATA GWL_USERDATA
#endif
#endif

// Skype API defines
#define SKYPECONTROLAPI_ATTACH_SUCCESS 0
#define SKYPECONTROLAPI_ATTACH_PENDING_AUTHORIZATION 1
#define SKYPECONTROLAPI_ATTACH_REFUSED 2
#define SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE 3
#define SKYPECONTROLAPI_ATTACH_API_AVAILABLE 0x8001

// -----------------------------------------------------------------------------

typedef struct
{
	IMO2SPROXY vtbl; // Must be first!
	IMO2SPROXY_CFG *pCfg;
	W32SKYPEEMU_CFG *pMyCfg;
	HWND hWndDispatch;
	TYP_LIST *hClients;
	mutex_t loopmutex;
	DWORD dwThreadID;
} IMO2SPROXY_INST;

typedef struct 
{
	HWND hWnd;
	IMOSAPI *hInst;
	IMO2SPROXY_INST *hProxy;
	mutex_t sendmutex;
	int iConnectionStat;
} CONNINST;

static UINT m_ControlAPIAttach = 0;
static UINT m_ControlAPIDiscover = 0;

// -----------------------------------------------------------------------------

static void EventHandler(char *pszMsg, void *pUser);
static CONNINST *FindClient(IMO2SPROXY_INST *hProxy, HWND hWnd);
static void CleanConnections (TYP_LIST *hList);
static void FreeConnection (CONNINST *pInst);
static LONG APIENTRY WndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam);

static int Imo2sproxy_Open(IMO2SPROXY *hInst);
static void Imo2sproxy_Loop(IMO2SPROXY *hInst);
static void Imo2sproxy_Exit(IMO2SPROXY *hInst);

// -----------------------------------------------------------------------------
static void EventHandler(char *pszMsg, void *pUser)
{
	CONNINST *pInst = (CONNINST*)pUser;
	COPYDATASTRUCT cds;
	DWORD dwRes = 0;

	LockMutex(pInst->sendmutex);
	if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
	{
		fprintf (pInst->hProxy->pCfg->fpLog, "%08X> %s\n", pInst->hWnd, pszMsg);
		fflush (pInst->hProxy->pCfg->fpLog);
	}
	cds.dwData = 0;
	cds.cbData = strlen(pszMsg)+1;
	cds.lpData = pszMsg;
	SendMessageTimeout (pInst->hWnd, WM_COPYDATA, (WPARAM)pInst->hProxy->hWndDispatch, (LPARAM)&cds, SMTO_NORMAL, 1000, &dwRes);
	UnlockMutex(pInst->sendmutex);
}

// -----------------------------------------------------------------------------

static CONNINST *FindClient(IMO2SPROXY_INST *hProxy, HWND hWnd)
{
	int i, nCount;
	CONNINST *pConn;

	for (i=0, nCount=List_Count(hProxy->hClients); i<nCount; i++)
	{
		if ((pConn=(CONNINST*)List_ElementAt (hProxy->hClients, i))->hWnd == hWnd)
			return pConn;
	}
	return NULL;
}

// -----------------------------------------------------------------------------

static void CleanConnections (TYP_LIST *hList)
{
	unsigned int i;
	CONNINST *pInst;

	for (i=0; i<List_Count(hList); i++)
	{
		pInst = List_ElementAt (hList, i);
		if (!IsWindow (pInst->hWnd))
		{
			if (pInst->hInst) FreeConnection(pInst);
			free (List_RemoveElementAt(hList, i));
			i--;
		}
	}
}

// -----------------------------------------------------------------------------

static void FreeConnection (CONNINST *pInst)
{
	if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
	{
		fprintf (pInst->hProxy->pCfg->fpLog, "Closed connection %08X\n", pInst->hWnd);
		fflush (pInst->hProxy->pCfg->fpLog);
	}
	ExitMutex (pInst->sendmutex);
	if (pInst->hInst)
	{
		IMOSAPI *hInst = pInst->hInst;
		pInst->hInst = NULL;
		Imo2S_Exit(hInst);
	}
}

// -----------------------------------------------------------------------------

static LONG APIENTRY WndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam) 
{ 
    switch (message) 
    { 
		case WM_CREATE:
		{
			LPCREATESTRUCT lpCr = (LPCREATESTRUCT)lParam;

			SetWindowLongPtr (hWnd, GWLP_USERDATA, (LONG_PTR)lpCr->lpCreateParams);
			SetTimer (hWnd, 0, 60000, NULL);
			break;
		}
        case WM_COPYDATA: 
		{
			PCOPYDATASTRUCT pCopyData = (PCOPYDATASTRUCT)lParam;
			CONNINST *pInst;
			IMO2SPROXY_INST *hProxy = (IMO2SPROXY_INST*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

			if (pInst = FindClient (hProxy, (HWND)wParam))
			{
				if (pInst->hProxy->pMyCfg->bDelayLogin && pInst->iConnectionStat < 1)
				{
					char *pszError;

					if ((pInst->iConnectionStat = Imo2S_Login (pInst->hInst, hProxy->pCfg->pszUser, hProxy->pCfg->pszPass, &pszError)) != 1)
					{
						pInst->hProxy->pCfg->logerror (stderr, "Connection %08X: Cannot login with (%s/****): %s\n", 
							pInst->hWnd, hProxy->pCfg->pszUser, pszError);
						FreeConnection(pInst);
						free (List_Pop(hProxy->hClients));
						PostMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_REFUSED);
						return 0;
					}
				}
				LockMutex(pInst->sendmutex);
				if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
				{
					fprintf (pInst->hProxy->pCfg->fpLog, "%08X< [%s]\n", pInst->hWnd, pCopyData->lpData);
					fflush (pInst->hProxy->pCfg->fpLog);
				}
				Imo2S_Send (pInst->hInst, pCopyData->lpData);
				UnlockMutex(pInst->sendmutex);
			}
			return 1;
		}
		case WM_TIMER:
			// Housekeeping timer
			CleanConnections (((IMO2SPROXY_INST*)GetWindowLongPtr(hWnd, GWLP_USERDATA))->hClients);
			break;
		case WM_DESTROY:
			KillTimer (hWnd, 0);
			break;
		default:
			if (message == m_ControlAPIDiscover)
			{
				CONNINST *pInst;
				IMO2SPROXY_INST *hProxy = (IMO2SPROXY_INST*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
				char *pszError;
				
				if (!(pInst = FindClient (hProxy, (HWND)wParam)))
				{
					pInst = (CONNINST*)calloc (1, sizeof(CONNINST));
					if (!pInst) break;
					List_Push(hProxy->hClients, pInst);
					pInst->hProxy = hProxy;
					pInst->hWnd = (HWND)wParam;
					if (hProxy->pCfg->bVerbose && hProxy->pCfg->fpLog)
						fprintf (hProxy->pCfg->fpLog, "Imo2sproxy::SkypeControlAPIDiscover\n");

					if (!(pInst->hInst = Imo2S_Init(EventHandler, pInst, hProxy->pCfg->iFlags)))
					{
						hProxy->pCfg->logerror (stderr, "Connection %08X: Cannot start Imo2Skype instance.\n", pInst->hWnd);
						free (List_Pop(hProxy->hClients));
						PostMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_REFUSED);
						return 0;
					}

					// FIXME: We should enable logging dependent on a loglevel rather than just enabling it
					if (hProxy->pCfg->bVerbose)
						Imo2S_SetLog (pInst->hInst, hProxy->pCfg->fpLog);

					InitMutex(pInst->sendmutex);
					
					if (!pInst->hProxy->pMyCfg->bDelayLogin)
					{
						PostMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE);
						if ((pInst->iConnectionStat = Imo2S_Login (pInst->hInst, hProxy->pCfg->pszUser, hProxy->pCfg->pszPass, &pszError)) != 1)
						{
							pInst->hProxy->pCfg->logerror (stderr, "Connection %08X: Cannot login with (%s/****): %s\n", 
								pInst->hWnd, hProxy->pCfg->pszUser, pszError);
							FreeConnection(pInst);
							free (List_Pop(hProxy->hClients));
							PostMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_REFUSED);
							return 0;
						}
						PostMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_API_AVAILABLE);
					}
					else
					{
						SendMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_SUCCESS);
					}
					return 0;
				}
				else
					SendMessage ((HWND)wParam, m_ControlAPIAttach, (WPARAM)hWnd, SKYPECONTROLAPI_ATTACH_SUCCESS);
				return 0;
			}
			break;
	}
	return (DefWindowProc(hWnd, message, wParam, lParam)); 
}



// -----------------------------------------------------------------------------
// PUBLIC
// -----------------------------------------------------------------------------

void W32SkypeEmu_Defaults (W32SKYPEEMU_CFG *pMyCfg)
{
	memset (pMyCfg, 0, sizeof(W32SKYPEEMU_CFG));
	// Login on first command from Client, immediately send SKYPECONTROLAPI_ATTACH_SUCCESS
	// This is necessary for some broken plugins, i.e. current SVN build of Miranda
	// Skype plugin, which doesn't correctly poll again if SKYPECONTROLAPI_ATTACH_NOT_AVAILABLE
	// is sent.
	pMyCfg->bDelayLogin = TRUE;
}

// -----------------------------------------------------------------------------

IMO2SPROXY *W32SkypeEmu_Init (IMO2SPROXY_CFG *pCfg, W32SKYPEEMU_CFG *pMyCfg)
{
	IMO2SPROXY_INST *pstInst = calloc(sizeof(IMO2SPROXY_INST), 1);

	pstInst->vtbl.Open = Imo2sproxy_Open;
	pstInst->vtbl.Loop = Imo2sproxy_Loop;
	pstInst->vtbl.Exit = Imo2sproxy_Exit;
	pstInst->pCfg = pCfg;
	pstInst->pMyCfg = pMyCfg;
	InitMutex(pstInst->loopmutex);
	return (IMO2SPROXY*)pstInst;
}

// -----------------------------------------------------------------------------
// IMPLEMENTATION
// -----------------------------------------------------------------------------
static int Imo2sproxy_Open(IMO2SPROXY *hInst)
{
	IMO2SPROXY_INST *hProxy = (IMO2SPROXY_INST*)hInst;
	WNDCLASS WndClass ={0}; 

	// Start Skype connection 
	if (hProxy->pCfg->bVerbose && hProxy->pCfg->fpLog)
		fprintf (hProxy->pCfg->fpLog, "W32SkypeEmu:Open(Start)\n");
	if ((!m_ControlAPIAttach && !(m_ControlAPIAttach=RegisterWindowMessage("SkypeControlAPIAttach"))) || 
		(!m_ControlAPIDiscover && !(m_ControlAPIDiscover=RegisterWindowMessage("SkypeControlAPIDiscover")))) 
	{
		hProxy->pCfg->logerror (stderr, "Cannot register Window messages!");
		return -1;
	}
  
	// Create window class
	WndClass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; 
	WndClass.lpfnWndProc = (WNDPROC)WndProc; 
	WndClass.hInstance =  GetModuleHandle(NULL);
	WndClass.lpszClassName = "Imo2SProxyDispatchWindow"; 
	RegisterClass(&WndClass);

	if (!(hProxy->hWndDispatch=CreateWindowEx( WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,
		WndClass.lpszClassName, "", WS_BORDER|WS_SYSMENU|WS_MINIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, 128, 128, NULL, 0, (HINSTANCE)WndClass.hInstance, (LPVOID)hProxy)))
	{
		hProxy->pCfg->logerror (stderr, "Unable to create dispatch window!");
		UnregisterClass (WndClass.lpszClassName, WndClass.hInstance);
		return -1;
	}

	hProxy->hClients = List_Init(32);
	return 0;
}

// -----------------------------------------------------------------------------

static void Imo2sproxy_Loop(IMO2SPROXY *hInst)
{
	IMO2SPROXY_INST *hProxy = (IMO2SPROXY_INST*)hInst;
	MSG Message;

	if (hProxy->pCfg->bVerbose && hProxy->pCfg->fpLog)
		fprintf (hProxy->pCfg->fpLog, "W32SkypeEmu:Loop(Start)\n");

	// Pump da messages
	hProxy->dwThreadID = GetCurrentThreadId();
	LockMutex(hProxy->loopmutex);
	while (GetMessage(&Message, NULL, 0, 0)) 
    { 
        TranslateMessage(&Message); 
        DispatchMessage(&Message);
    }
	
	if (hProxy->pCfg->bVerbose && hProxy->pCfg->fpLog)
		fprintf (hProxy->pCfg->fpLog, "W32SkypeEmu:Loop(End)\n");
	UnlockMutex(hProxy->loopmutex);
}


// -----------------------------------------------------------------------------

static void Imo2sproxy_Exit(IMO2SPROXY *hInst)
{
	IMO2SPROXY_INST *hProxy = (IMO2SPROXY_INST*)hInst;
	CONNINST *pInst;

	if (hProxy->pCfg->bVerbose && hProxy->pCfg->fpLog)
		fprintf (hProxy->pCfg->fpLog, "W32SkypeEmu:Exit()\n");

	if (hProxy->hWndDispatch) DestroyWindow (hProxy->hWndDispatch);
	if (hProxy->dwThreadID) PostThreadMessage (hProxy->dwThreadID, WM_QUIT, 0, 0);
	LockMutex(hProxy->loopmutex);

	// Kill 'em all!
	if (hProxy->hClients)
	{
		while (pInst=List_Pop(hProxy->hClients))
		{
			FreeConnection(pInst);
			free (pInst);
		}
		List_Exit (hProxy->hClients);
	}

	UnregisterClass ("Imo2SProxyDispatchWindow", GetModuleHandle(NULL));

	UnlockMutex(hProxy->loopmutex);
	ExitMutex(hProxy->loopmutex);

	free (hProxy);
}