summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/IMO2sProxy2/src/imo2skype/miranda/skypepluginlink.c
blob: 0109bd6edee86cc071ab824e0584ab1770b04d6a (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
#include "imo2sproxy.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include "skypepluginlink.h"
#include "include/newpluginapi.h"
#include "include/m_system.h"

#define LockMutex(x) EnterCriticalSection (&x)
#define UnlockMutex(x) LeaveCriticalSection(&x)
#define InitMutex(x) InitializeCriticalSection(&x)
#define ExitMutex(x) DeleteCriticalSection(&x)
#define mutex_t CRITICAL_SECTION


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

struct tag_proxyInst;
typedef struct tag_proxyInst IMO2SPROXY_INST;

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

struct tag_proxyInst
{
	IMO2SPROXY vtbl; // Must be first!
	IMO2SPROXY_CFG *pCfg;
	SKYPEPLUGINLINK_CFG *pMyCfg;
	HANDLE hService;
	CONNINST stClient;	// Currently only 1 connection
};


static CONNINST *m_pConn = NULL;

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

static void EventHandler(char *pszMsg, void *pUser);
static int InitProxy(IMO2SPROXY_INST *hProxy);

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, "> %s\n", pszMsg);
		fflush (pInst->hProxy->pCfg->fpLog);
	}
	cds.dwData = 0;
	cds.cbData = strlen(pszMsg)+1;
	cds.lpData = pszMsg;
	CallService (SKYPE_PROTONAME PSS_SKYPEAPIMSG, 0, (LPARAM)&cds);
	//UnlockMutex (pInst->sendmutex);
}

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

INT_PTR CallIn(WPARAM wParam,LPARAM lParam)
{
	CONNINST *pInst = (CONNINST*)m_pConn;
	PCOPYDATASTRUCT pCopyData = (PCOPYDATASTRUCT)lParam;

	if (!pInst) return -1;

	if (pInst->hProxy->pMyCfg->bDelayLogin && pInst->iConnectionStat < 1)
	{
		LockMutex (pInst->rcvmutex);
		if (InitProxy(pInst->hProxy)<0)
		{
			UnlockMutex(pInst->rcvmutex);
			return -1;
		}
		UnlockMutex(pInst->rcvmutex);
	}

	if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
	{
		fprintf (pInst->hProxy->pCfg->fpLog, "< [%s]\n", pCopyData->lpData);
		fflush (pInst->hProxy->pCfg->fpLog);
	}
	Imo2S_Send (pInst->hInst, pCopyData->lpData);
	return 0;
}

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

static void FreeConnection (CONNINST *pInst)
{
	if (!pInst || !pInst->hProxy) return;
	if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
	{
		fprintf (pInst->hProxy->pCfg->fpLog, "SkypePluginLink: Closed connection\n");
		fflush (pInst->hProxy->pCfg->fpLog);
	}

	if (pInst->hProxy->hService)
	{
		DestroyServiceFunction(pInst->hProxy->hService);
		pInst->hProxy->hService = NULL;
		CallService (SKYPE_PROTONAME SKYPE_REGPROXY, 0, 0);
	}
	ExitMutex(pInst->sendmutex);
	ExitMutex(pInst->rcvmutex);
	if (pInst->hInst)
	{
		IMOSAPI *hInst = pInst->hInst;
		pInst->hInst = NULL;
		Imo2S_Exit(hInst);
	}
}

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

static int InitProxy(IMO2SPROXY_INST *hProxy)
{
	char *pszError;

	if (!hProxy->stClient.hInst)
	{
		if (!(hProxy->stClient.hInst = Imo2S_Init(EventHandler, &hProxy->stClient, hProxy->pCfg->iFlags)))
		{
			hProxy->pCfg->logerror (stderr, "SkypePluginLink: Cannot start Imo2Skype instance.\n");
			return -1;
		}
	}

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

	if (hProxy->stClient.iConnectionStat == 0 ||
		(hProxy->stClient.iConnectionStat = Imo2S_Login (hProxy->stClient.hInst, 
		hProxy->pCfg->pszUser, hProxy->pCfg->pszPass, &pszError)) != 1)
	{
		hProxy->pCfg->logerror (stderr, "SkypePluginLink: Cannot login with (%s/****): %s\n", 
			hProxy->pCfg->pszUser, pszError);
		return -1;
	}
	return 0;
}

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

void SkypePluginLink_Defaults (SKYPEPLUGINLINK_CFG *pMyCfg)
{
	memset (pMyCfg, 0, sizeof(SKYPEPLUGINLINK_CFG));
	pMyCfg->bDelayLogin = TRUE;
}

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

IMO2SPROXY *SkypePluginLink_Init (IMO2SPROXY_CFG *pCfg, SKYPEPLUGINLINK_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;
	return (IMO2SPROXY*)pstInst;
}

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

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

	// Only 1 connection instance, so we can init it right here
	InitMutex(hProxy->stClient.sendmutex);
	InitMutex(hProxy->stClient.rcvmutex);
	hProxy->stClient.hProxy = hProxy;
	m_pConn = &hProxy->stClient;

	// Register with Skype Plugin
	hProxy->hService = CreateServiceFunction("IMO2SPROXY" PSS_SKYPEAPIMSG, CallIn);
	CallService (SKYPE_PROTONAME SKYPE_REGPROXY, 0, (LPARAM)"IMO2SPROXY" PSS_SKYPEAPIMSG);
	hProxy->stClient.iConnectionStat = -1;

	if (!hProxy->pMyCfg->bDelayLogin)
	{
		if (InitProxy(hProxy)<0)
		{
			FreeConnection(&hProxy->stClient);
			return -1;
		}
	}
	return 0;
}

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

static void Imo2sproxy_Loop(IMO2SPROXY *hInst)
{
	// No loop needed for call-in
}


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

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

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

	FreeConnection (&hProxy->stClient);
	free (hProxy);
}