summaryrefslogtreecommitdiff
path: root/protocols/Sametime/src/sametime_proto.cpp
blob: 6037abcc7da062a5eb1b946bb3255f5892cd0262 (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "StdAfx.h"
#include "sametime.h"

CSametimeProto::CSametimeProto(const char* pszProtoName, const TCHAR* tszUserName) :
	PROTO<CSametimeProto>(pszProtoName, tszUserName),
	is_idle(false), idle_status(false),
	first_online(true),
	idle_timerid(0),
	session(NULL),
	my_login_info(NULL),
	my_conference(NULL),
	service_places(NULL),
	service_conference(NULL),
	server_connection(0)
{
	// Register m_hNetlibUser user
	TCHAR name[128];
	mir_sntprintf(name, SIZEOF(name), TranslateT("%s connection"), m_tszUserName);
	NETLIBUSER nlu = { 0 };
	nlu.cbSize = sizeof(nlu);
	nlu.flags = NUF_TCHAR | NUF_OUTGOING | NUF_INCOMING | NUF_HTTPCONNS;
	nlu.szSettingsModule = m_szModuleName;
	nlu.ptszDescriptiveName = name;
	m_hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);

	RegisterGLibLogger();
	debugLog(_T("CSametimeProto::CSametimeProto() start  m_szModuleName=[%s], m_tszUserName=[%s]"), _A2T(m_szModuleName), m_tszUserName);

	SametimeInitIcons();
	InitCritSection();

	CreateProtoService(PS_GETNAME, &CSametimeProto::GetName);
	CreateProtoService(PS_GETSTATUS, &CSametimeProto::GetStatus);
	CreateProtoService(PS_LOADICON, &CSametimeProto::SametimeLoadIcon);

	HookProtoEvent(ME_SYSTEM_MODULESLOADED, &CSametimeProto::OnModulesLoaded);
	HookProtoEvent(ME_SYSTEM_PRESHUTDOWN, &CSametimeProto::OnPreShutdown);
	HookProtoEvent(ME_MSG_WINDOWEVENT, &CSametimeProto::OnWindowEvent);
	HookProtoEvent(ME_IDLE_CHANGED, &CSametimeProto::OnIdleChanged);
	HookProtoEvent(ME_DB_CONTACT_DELETED, &CSametimeProto::OnSametimeContactDeleted);
	HookProtoEvent(ME_OPT_INITIALISE, &CSametimeProto::OptInit);

	// Initialize temporary DB settings
	db_set_resident(m_szModuleName, "Status");
	db_set_resident(m_szModuleName, "IdleTS");

	RegisterPopups();
	InitAwayMsg();

	mir_snprintf(szProtoGroups, SIZEOF(szProtoGroups), "%s_GROUPS", m_szModuleName);

	m_iStatus = ID_STATUS_OFFLINE;
	previous_status = ID_STATUS_OFFLINE;
	SetAllOffline();

	DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &mainThread, THREAD_SET_CONTEXT, FALSE, 0);

	InitConferenceMenu();
	InitSessionMenu();

	LoadOptions();

	debugLog(_T("CSametimeProto::CSametimeProto() end"));
}

CSametimeProto::~CSametimeProto()
{
	debugLog(_T("CSametimeProto::~CSametimeProto() start"));

	DeinitSessionMenu();
	DeinitConferenceMenu();

	DeinitAwayMsg();
	UnregisterPopups();
	DeinitCritSection();

	debugLog(_T("CSametimeProto::~CSametimeProto() end"));

	UnRegisterGLibLogger();
	Netlib_CloseHandle(m_hNetlibUser);
}



MCONTACT CSametimeProto::AddToList(int flags, PROTOSEARCHRESULT* psr)
{
	MYPROTOSEARCHRESULT* sr = (MYPROTOSEARCHRESULT*)psr;
	debugLog(_T("CSametimeProto::AddToList()  flags=[%d]"), flags);
	return AddSearchedUser(sr, flags & PALF_TEMPORARY);
}

MCONTACT CSametimeProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent)
{
	debugLog(_T("CSametimeProto::AddToListByEvent()  flags=[%d]"), flags);
	return 0;
}

int CSametimeProto::Authorize(HANDLE hDbEvent)
{
	debugLog(_T("CSametimeProto::Authorize()"));
	return 1;
}

int CSametimeProto::AuthDeny(HANDLE hDbEvent, const PROTOCHAR* szReason)
{
	debugLog(_T("CSametimeProto::AuthDeny()"));
	return 1;
}

int CSametimeProto::AuthRecv(MCONTACT hContact, PROTORECVEVENT*)
{
	debugLog(_T("CSametimeProto::AuthRecv()"));
	return 1;
}

int CSametimeProto::AuthRequest(MCONTACT hContact, const PROTOCHAR* szMessage)
{
	debugLog(_T("CSametimeProto::AuthRequest()"));
	return 1;
}

HANDLE CSametimeProto::FileAllow(MCONTACT hContact, HANDLE hTransfer, const PROTOCHAR* szPath)
{
	debugLog(_T("CSametimeProto::FileAllow()  hContact=[%x], szPath=[%s]"), hContact, szPath);
	char* szPathA = mir_t2a(szPath);
	HANDLE res = AcceptFileTransfer(hContact, hTransfer, szPathA);
	mir_free(szPathA);
	return res;
}

int CSametimeProto::FileCancel(MCONTACT hContact, HANDLE hTransfer)
{
	debugLog(_T("CSametimeProto::FileCancel()  hContact=[%x]"), hContact);
	CancelFileTransfer(hTransfer);
	return 0;
}

int CSametimeProto::FileDeny(MCONTACT hContact, HANDLE hTransfer, const PROTOCHAR* szReason)
{
	debugLog(_T("CSametimeProto::FileDeny()  hContact=[%x], szReason=[%s]"), hContact, szReason);
	RejectFileTransfer(hTransfer);
	return 0;
}

int CSametimeProto::FileResume(HANDLE hTransfer, int* action, const PROTOCHAR** szFilename)
{
	debugLog(_T("CSametimeProto::FileResume() action=[%d]"), &action);
	return 0;
}


DWORD_PTR CSametimeProto::GetCaps(int type, MCONTACT hContact)
{
	int ret = 0;
	switch (type) {
	case PFLAGNUM_1:
		ret = PF1_IM | PF1_BASICSEARCH | PF1_EXTSEARCHUI | PF1_ADDSEARCHRES | PF1_MODEMSG | PF1_FILE | PF1_CHAT;
		break;
	case PFLAGNUM_2:
		ret = PF2_ONLINE | PF2_SHORTAWAY | PF2_HEAVYDND | PF2_LIGHTDND;
		break;
	case PFLAGNUM_3:
		ret = PF2_ONLINE | PF2_SHORTAWAY | PF2_HEAVYDND | PF2_LIGHTDND;
		break;
	case PFLAGNUM_4:
		ret = PF4_SUPPORTTYPING | PF4_IMSENDUTF;
		break;
	case PFLAG_UNIQUEIDTEXT:
		ret = (DWORD_PTR)Translate("ID");
		break;
	case PFLAG_MAXLENOFMESSAGE:
		ret = MAX_MESSAGE_SIZE;
		break;
	case PFLAG_UNIQUEIDSETTING:
		ret = (DWORD_PTR) "stid";
		break;
	}
	return ret;
}

int CSametimeProto::GetInfo(MCONTACT hContact, int infoType)
{
	// GetInfo - retrieves a contact info
	debugLog(_T("CSametimeProto::GetInfo()  hContact=[%x], infoType=[%d]"), hContact, infoType);

	if (getByte(hContact, "ChatRoom", 0))
		return 1;

	if (!session)
		return 1;

	///TODO unimplemented - getting contact info

	TFakeAckParams* tfap = (TFakeAckParams*)mir_alloc(sizeof(TFakeAckParams));
	tfap->proto = this;
	tfap->hContact = hContact;
	tfap->lParam = NULL;
	mir_forkthread(sttFakeAckInfoSuccessThread, (void*)tfap);

	return 0;
}

HANDLE CSametimeProto::SearchBasic(const PROTOCHAR* id)
{
	debugLog(_T("CSametimeProto::SearchBasic()  id:len=[%d]"), id == NULL ? -1 : _tcslen(id));
	char* id_utf8 = mir_utf8encodeT(id);
	int ret = SearchForUser(id_utf8, FALSE);
	mir_free(id_utf8);
	return (HANDLE)ret;
	///TODO - add timeout (like at GGPROTO::searchthread)
}

HANDLE CSametimeProto::SearchByEmail(const PROTOCHAR* email)
{
	return 0;
}

HANDLE CSametimeProto::SearchByName(const PROTOCHAR* nick, const PROTOCHAR* firstName, const PROTOCHAR* lastName)
{
	return 0;
}

HWND CSametimeProto::SearchAdvanced(HWND owner)
{
	TCHAR buf[512];
	int ret = 0;
	if (GetDlgItemText(owner, IDC_EDIT1, buf, 512)) {
		debugLog(_T("CSametimeProto::SearchAdvanced()  buf:len=[%d]"), buf == NULL ? -1 : _tcslen(buf));
		char* buf_utf8 = mir_utf8encodeT(buf);
		ret = SearchForUser(buf_utf8, TRUE);
		mir_free(buf_utf8);
	}
	return (HWND)ret;
}

HWND CSametimeProto::CreateExtendedSearchUI(HWND owner)
{
	debugLog(_T("CSametimeProto::CreateExtendedSearchUI() start"));
	return CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_USERSEARCH), owner, SearchDialogFunc, (LPARAM)this);
}


int CSametimeProto::RecvContacts(MCONTACT hContact, PROTORECVEVENT*)
{
	debugLog(_T("CSametimeProto::RecvContacts()"));
	return 1;
}

int CSametimeProto::RecvFile(MCONTACT hContact, PROTOFILEEVENT* pre)
{
	debugLog(_T("CSametimeProto::RecvFile()  hContact=[%x]"), hContact);

	db_unset(hContact, "CList", "Hidden");
	db_unset(hContact, "CList", "NotOnList");

	return Proto_RecvFile(hContact, pre);
}

int CSametimeProto::RecvMsg(MCONTACT hContact, PROTORECVEVENT* pre)
{
	debugLog(_T("CSametimeProto::RecvMsg() hContact=[%x]"), hContact);

	db_unset(hContact, "CList", "Hidden");
	db_unset(hContact, "CList", "NotOnList");

	return Proto_RecvMessage(hContact, pre);
}

int CSametimeProto::RecvUrl(MCONTACT hContact, PROTORECVEVENT*)
{
	debugLog(_T("CSametimeProto::RecvUrl()"));
	return 1;
}

int CSametimeProto::SendContacts(MCONTACT hContact, int flags, int nContacts, MCONTACT* hContactsList)
{
	debugLog(_T("CSametimeProto::SendContacts()    flags=[%d], nContacts=[%d]"), flags, nContacts);
	return 1;
}

HANDLE CSametimeProto::SendFile(MCONTACT hContact, const PROTOCHAR* szDescription, PROTOCHAR** ppszFiles)
{
	debugLog(_T("CSametimeProto::SendFile()  hContact=[%x]"), hContact);
	if (m_iStatus != ID_STATUS_OFFLINE) {
		if (hContact && ppszFiles && szDescription) {
			if (db_get_w(hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE) != ID_STATUS_OFFLINE) {
				return SendFilesToUser(hContact, ppszFiles, szDescription);
			}
		}
	}
	return 0; // failure
}

int CSametimeProto::SendMsg(MCONTACT hContact, int flags, const char* msg)
{
	debugLog(_T("CSametimeProto::SendMsg()  hContact=[%x], flags=[%d]"), hContact, flags);

	char *proto = GetContactProto(hContact);
	int ret;

	if (!proto || strcmp(proto, m_szModuleName) != 0 || db_get_w(hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE) {
		TFakeAckParams* tfap = (TFakeAckParams*)mir_alloc(sizeof(TFakeAckParams));
		tfap->proto = this;
		tfap->hContact = hContact;
		tfap->lParam = 0;
		mir_forkthread(sttFakeAckMessageFailedThread, (void*)tfap);
		return 0;
	}

	char *msg_utf8;
	if (flags & PREF_UNICODE)
		msg_utf8 = mir_utf8encodeW((wchar_t*)&msg[strlen(msg) + 1]);
	else if (flags & PREF_UTF)
		msg_utf8 = mir_strdup(msg);
	else
		msg_utf8 = mir_utf8encode(msg);

	if (!msg_utf8)
		return 0;

	ret = (int)SendMessageToUser(hContact, msg_utf8);
	mir_free(msg_utf8);

	TFakeAckParams *tfap = (TFakeAckParams*)mir_alloc(sizeof(TFakeAckParams));
	tfap->proto = this;
	tfap->hContact = hContact;
	tfap->lParam = (LPARAM)ret;
	mir_forkthread(sttFakeAckMessageSuccessThread, (void*)tfap);

	return ret;
}

int CSametimeProto::SendUrl(MCONTACT hContact, int flags, const char* url)
{
	debugLog(_T("CSametimeProto::SendUrl()"));
	return 1;
}

int CSametimeProto::SetApparentMode(MCONTACT hContact, int mode)
{
	debugLog(_T("CSametimeProto::SetApparentMode()    mode=[%d]"), mode);
	return 1;
}

int CSametimeProto::SetStatus(int iNewStatus)
{
	debugLog(_T("CSametimeProto::SetStatus()  m_iStatus=[%d], m_iDesiredStatus=[%d], iNewStatus=[%d]"), m_iStatus, m_iDesiredStatus, iNewStatus);
	m_iDesiredStatus = iNewStatus;
	if (iNewStatus != ID_STATUS_OFFLINE) {
		if (m_iStatus == ID_STATUS_OFFLINE)
			LogIn(iNewStatus, m_hNetlibUser);
		else
			SetSessionStatus(iNewStatus);
	}
	else if (m_iStatus != ID_STATUS_OFFLINE && iNewStatus == ID_STATUS_OFFLINE) {
		LogOut();
	}

	return 0;
}

HANDLE CSametimeProto::GetAwayMsg(MCONTACT hContact)
{
	debugLog(_T("CSametimeProto::GetInfo()  hContact=[%x], m_iStatus=[%d]"), hContact, m_iStatus);
	if (hContact && m_iStatus != ID_STATUS_OFFLINE) {
		TFakeAckParams* tfap;
		tfap = (TFakeAckParams*)malloc(sizeof(TFakeAckParams));
		tfap->proto = this;
		tfap->hContact = hContact;
		mir_forkthread(sttRecvAwayThread, (void*)tfap);
		return (HANDLE)1;
	}
	return NULL;
}

int CSametimeProto::RecvAwayMsg(MCONTACT hContact, int mode, PROTORECVEVENT* evt)
{
	debugLog(_T("CSametimeProto::RecvAwayMsg()  hContact=[%x], mode=[%d]"), hContact, mode);

	if (evt->flags & PREF_UTF) {
		TCHAR* pszMsg = mir_utf8decodeT(evt->szMessage);
		ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)evt->lParam, (LPARAM)pszMsg);
		mir_free(pszMsg);
	}
	else ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_SUCCESS, (HANDLE)evt->lParam, (LPARAM)(TCHAR*)_A2T(evt->szMessage));

	return 0;
}

int CSametimeProto::SetAwayMsg(int iStatus, const PROTOCHAR* msg)
{
	debugLog(_T("CSametimeProto::SetAwayMsg()  iStatus=[%d], msg:len=[%d]"), iStatus, msg == NULL ? -1 : _tcslen(msg));
	SetSessionAwayMessage(iStatus, msg);
	return 0;
}

int CSametimeProto::UserIsTyping(MCONTACT hContact, int type)
{
	debugLog(_T("CSametimeProto::UserIsTyping()  hContact=[%x], type=[%d]"), hContact, type);
	SendTyping(hContact, type == PROTOTYPE_SELFTYPING_ON);
	return 0;
}

int CSametimeProto::OnEvent(PROTOEVENTTYPE iEventType, WPARAM wParam, LPARAM lParam)
{
	switch (iEventType) {
	case EV_PROTO_ONOPTIONS:
		debugLog(_T("CSametimeProto::OnEvent() EV_PROTO_ONOPTIONS"));
		OptInit(wParam, lParam);
		break;
	}

	return TRUE;
}