summaryrefslogtreecommitdiff
path: root/protocols/Sametime/src/conference.cpp
blob: dcba8b5dd6aa34617981753b569250f8d434610f (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "StdAfx.h"
#include "sametime.h"

void CloseMyConference(CSametimeProto* proto)
{
	mwConference_destroy(proto->my_conference, 0, Translate("I'm outa here."));
	proto->my_conference = nullptr;
}

CSametimeProto* getProtoFromMwConference(mwConference* conf)
{
	mwServiceConference* servConference = mwConference_getServiceConference(conf);
	mwService* service = mwServiceConference_getService(servConference);
	mwSession* session = mwService_getSession(service);
	return (CSametimeProto*)mwSession_getProperty(session, "PROTO_STRUCT_PTR");
}

/** triggered when we receive a conference invitation. Call
  mwConference_accept to accept the invitation and join the
  conference, or mwConference_close to reject the invitation.

  @param conf     the newly created conference
  @param inviter  the identity of the user who sent the invitation
  @param invite   the invitation text
*/
void mwServiceConf_on_invited(mwConference* conf, mwLoginInfo* inviter, const char* invite)
{
	GList *members, *mem;
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_on_invited() start");

	members = mem = mwConference_getMembers(conf);
	for (;mem;mem=mem->next) {
		if (proto->my_login_info && strcmp(proto->my_login_info->login_id, ((mwLoginInfo*)mem->data)->login_id) == 0) {
			proto->debugLogW(L"mwServiceConf_on_invited() already present");
			mwConference_reject(conf, 0, T2Utf(TranslateT("Invitation rejected - already present.")));
			return;
		}
	}
	g_list_free(members);

	wchar_t ws_username[128];
	MultiByteToWideChar(CP_UTF8, 0, (const char*)inviter->user_name, -1, ws_username, 128);

	wchar_t ws_invite[512];
	MultiByteToWideChar(CP_UTF8, 0, (const char*)invite, -1, ws_invite, 128);

	if (MessageBoxW(nullptr, ws_invite, ws_username, MB_OKCANCEL) == IDOK) {
		proto->debugLogW(L"mwServiceConf_on_invited() mwConference_accept");
		mwConference_accept(conf);
	}
	else {
		proto->debugLogW(L"mwServiceConf_on_invited() mwConference_reject");
		char* temp = mir_utf8encodeW(TranslateT("Your invitation has been rejected."));
		mwConference_reject(conf, 0, temp);
		mir_free(temp);
	}
}

void CSametimeProto::ClearInviteQueue()
{	
	debugLogW(L"CSametimeProto::ClearInviteQueue() start");
	if (!my_conference)
		return;

	mwIdBlock idb;
	idb.community = nullptr;

	while(invite_queue.size()) {
		idb.user = (char *)invite_queue.front().c_str();

		MCONTACT hContact = FindContactByUserId(idb.user);
		if (!hContact) {
			mwSametimeList* user_list = mwSametimeList_new();
			mwSametimeGroup* stgroup = mwSametimeGroup_new(user_list, mwSametimeGroup_NORMAL, T2Utf(TranslateT("None")));
			mwSametimeUser* stuser = mwSametimeUser_new(stgroup, mwSametimeUser_NORMAL, &idb);

			hContact = AddContact(stuser, (options.add_contacts ? false : true));
			mwSametimeList_free(user_list);
		}

		bool found = false;
		GList *members, *mem;
		members = mem = mwConference_getMembers(my_conference);
		for (;mem;mem=mem->next) {
			if (my_login_info && strcmp(idb.user, ((mwLoginInfo *)mem->data)->user_id) == 0) {
				found = true;
				break;
			}
		}
		g_list_free(members);

		if (!found)
			mwConference_invite(my_conference, &idb, T2Utf(TranslateT("Please join this meeting.")));

		invite_queue.pop();
	}
}

/** triggered when we enter the conference. Provides the initial
  conference membership list as a GList of mwLoginInfo structures

  @param conf     the conference just joined
  @param members  mwLoginInfo list of existing conference members
*/
void mwServiceConf_conf_opened(mwConference* conf, GList* members)
{
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_conf_opened() start");

	ptrW tszConfId(mir_utf8decodeW(mwConference_getName(conf)));
	ptrW tszConfTitle(mir_utf8decodeW(mwConference_getTitle(conf)));

	// create new chat session
	SESSION_INFO *si = Chat_NewSession(GCW_CHATROOM, proto->m_szModuleName, tszConfId, tszConfTitle);
	if (!si)
		return;

	// add a group
	Chat_AddGroup(si, TranslateT("Normal"));

	// add users
	GCEVENT gce = { proto->m_szModuleName, 0, GC_EVENT_JOIN };
	gce.pszID.w = tszConfId;

	GList *user = members;
	for (;user; user=user->next) {
		proto->debugLogW(L"mwServiceConf_conf_opened() add user");

		ptrW tszUserName(mir_utf8decodeW(((mwLoginInfo*)user->data)->user_name));
		ptrW tszUserId(mir_utf8decodeW(((mwLoginInfo*)user->data)->login_id));
		gce.dwFlags = GCEF_ADDTOLOG;
		gce.pszNick.w = tszUserName;
		gce.pszUID.w = tszUserId;
		gce.bIsMe = (strcmp(((mwLoginInfo*)user->data)->login_id, proto->my_login_info->login_id) == 0);
		Chat_Event( &gce);
	}

	// finalize setup (show window)
	Chat_Control(proto->m_szModuleName, tszConfId, SESSION_INITDONE);
	Chat_Control(proto->m_szModuleName, tszConfId, SESSION_ONLINE);

	if (conf == proto->my_conference)
		proto->ClearInviteQueue();
}

/** triggered when a conference is closed. This is typically when
  we've left it */
void mwServiceConf_conf_closed(mwConference* conf, guint32 reason)
{
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_conf_closed() start");

	ptrW tszConfId(mir_utf8decodeW(mwConference_getName(conf)));
	Chat_Control(proto->m_szModuleName, tszConfId, SESSION_OFFLINE);
	Chat_Terminate(proto->m_szModuleName, tszConfId);
}

/** triggered when someone joins the conference */
void mwServiceConf_on_peer_joined(mwConference* conf, mwLoginInfo *user)
{
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_on_peer_joined() start");

	MCONTACT hContact = proto->FindContactByUserId(user->user_id);
	if (!hContact) {
		mwIdBlock idb;
		idb.user = ((mwLoginInfo *)user)->user_id;
		idb.community = nullptr;

		mwSametimeList* user_list = mwSametimeList_new();
		mwSametimeGroup* stgroup = mwSametimeGroup_new(user_list, mwSametimeGroup_NORMAL, T2Utf(TranslateT("None")));
		mwSametimeUser* stuser = mwSametimeUser_new(stgroup, mwSametimeUser_NORMAL, &idb);

		hContact = proto->AddContact(stuser, (proto->options.add_contacts ? false : true));

		mwSametimeList_free(user_list);
	}

	ptrW tszConfId(mir_utf8decodeW(mwConference_getName(conf)));
	ptrW tszUserName(mir_utf8decodeW(user->user_name));
	ptrW tszUserId(mir_utf8decodeW(user->login_id));

	// add user
	GCEVENT gce = { proto->m_szModuleName, 0, GC_EVENT_JOIN };
	gce.pszID.w = tszConfId;
	gce.dwFlags = GCEF_ADDTOLOG;
	gce.pszNick.w = tszUserName;
	gce.pszUID.w = tszUserId;
	gce.pszStatus.w = L"Normal";
	gce.time = (DWORD)time(0);

	Chat_Event( &gce);

	mir_free(tszUserName);
	mir_free(tszUserId);
	mir_free(tszConfId);
}

/** triggered when someone leaves the conference */
void mwServiceConf_on_peer_parted(mwConference* conf, mwLoginInfo* user)
{
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_on_peer_parted() start");

	ptrW tszConfId(mir_utf8decodeW(mwConference_getName(conf)));
	ptrW tszUserName(mir_utf8decodeW(user->user_name));
	ptrW tszUserId(mir_utf8decodeW(user->login_id));

	// remove user
	GCEVENT gce = { proto->m_szModuleName, 0, GC_EVENT_PART };
	gce.pszID.w = tszConfId;
	gce.dwFlags = GCEF_ADDTOLOG;
	gce.pszNick.w = tszUserName;
	gce.pszUID.w = tszUserId;
	gce.pszStatus.w = L"Normal";
	gce.time = (DWORD)time(0);
	Chat_Event(&gce);
}

/** triggered when someone says something */
void mwServiceConf_on_text(mwConference* conf, mwLoginInfo* user, const char* what)
{
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_on_text() start");

	ptrW textT(mir_utf8decodeW(what));
	ptrW tszConfId(mir_utf8decodeW(mwConference_getName(conf)));
	ptrW tszUserId(mir_utf8decodeW(user->login_id));
	ptrW tszUserName(mir_utf8decodeW(user->user_name));

	GCEVENT gce = { proto->m_szModuleName, 0, GC_EVENT_MESSAGE };
	gce.pszID.w = tszConfId;
	gce.dwFlags = GCEF_ADDTOLOG;
	gce.pszText.w = textT;
	gce.pszNick.w = tszUserName;
	gce.pszUID.w = tszUserId;
	gce.time = (DWORD)time(0);
	Chat_Event(&gce);
}

/** typing notification */
void mwServiceConf_on_typing(mwConference* conf, mwLoginInfo* who, gboolean typing)
{
	CSametimeProto* proto = getProtoFromMwConference(conf);
	proto->debugLogW(L"mwServiceConf_on_typing() start");
	///TODO unimplemented
}

/** optional. called from mwService_free */
void mwServiceConf_clear(mwServiceConference* srvc)
{
}

mwConferenceHandler mwConference_handler = {
	mwServiceConf_on_invited,
	mwServiceConf_conf_opened,
	mwServiceConf_conf_closed,
	mwServiceConf_on_peer_joined,
	mwServiceConf_on_peer_parted,
	mwServiceConf_on_text,
	mwServiceConf_on_typing,
	mwServiceConf_clear
};

void CSametimeProto::TerminateConference(char* name)
{
	debugLogW(L"CSametimeProto::TerminateConference() start");
		
	GList *conferences, *conf;
	conferences = conf = mwServiceConference_getConferences(service_conference);
	for (;conf;conf = conf->next)
		if (strcmp(name, mwConference_getName((mwConference*)conf->data)) == 0)
			Chat_Terminate(m_szModuleName, ptrW(mir_utf8decodeW(name)));

	g_list_free(conferences);
}


int CSametimeProto::GcEventHook(WPARAM wParam, LPARAM lParam) {

	GCHOOK* gch = (GCHOOK*)lParam;

	if (strcmp(gch->pszModule, m_szModuleName) != 0) return 0;

	GList *conferences = mwServiceConference_getConferences(service_conference);
	for (GList *conf = conferences;conf;conf = conf->next) {
		wchar_t* tszConfId = mir_utf8decodeW(mwConference_getName((mwConference*)conf->data));
		if (mir_wstrcmp(gch->ptszID, tszConfId) == 0) {
			switch(gch->iType) {
			case GC_USER_MESSAGE:
				{
					debugLogW(L"CSametimeProto::GcEventHook() GC_USER_MESSAGE");
					mwConference_sendText((mwConference*)conf->data, T2Utf(gch->ptszText));
				}
				break;
			case GC_SESSION_TERMINATE:
				{
					if (my_conference == conf->data){
						debugLogW(L"CSametimeProto::GcEventHook() GC_SESSION_TERMINATE CloseMyConference");
						CloseMyConference(this);
					} else {
						debugLogW(L"CSametimeProto::GcEventHook() GC_SESSION_TERMINATE mwConference_destroy");
						mwConference_destroy((mwConference*)conf->data, 0, T2Utf(TranslateT("I'm outa here.")));
					}
				}
				break;
			}
			mir_free(tszConfId);
			break;
		}
		mir_free(tszConfId);
	}

	g_list_free(conferences);

	return 0;
}

int CSametimeProto::ChatDeleted(MCONTACT hContact) {
	
	if (db_get_b(hContact, m_szModuleName, "ChatRoom", 0) == 0)
		return 0;

	debugLogW(L"CSametimeProto::ChatDeleted() hContact=[%x]", hContact);
	DBVARIANT dbv;
	if (!db_get_s(hContact, m_szModuleName, "ChatRoomID", &dbv)) {
		TerminateConference(dbv.pszVal);
		db_free(&dbv);
	}

	return 0;
}


INT_PTR CSametimeProto::onMenuLeaveChat(WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)wParam;
	debugLogW(L"CSametimeProto::onMenuLeaveChat() hContact=[%x]", hContact);
	ChatDeleted(hContact);
	return 0;
}


INT_PTR CSametimeProto::onMenuCreateChat(WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)wParam;
	debugLogW(L"CSametimeProto::onMenuCreateChat() hContact=[%x]", hContact);
	mwAwareIdBlock id_block;
	mwIdBlock idb;
	if (my_login_info && GetAwareIdFromContact(hContact, &id_block)) {
		wchar_t title[512];
		wchar_t* ts = mir_utf8decodeW(my_login_info->user_name);
		mir_snwprintf(title, TranslateT("%s's conference"), ts);
		mir_free(ts);

		idb.user = id_block.user;
		idb.community = id_block.community;

		invite_queue.push(idb.user);

		if (!my_conference) {
			debugLogW(L"CSametimeProto::onMenuCreateChat() mwConference_open");
			my_conference = mwConference_new(service_conference, T2Utf(title));
			mwConference_open(my_conference);
		}
		else {
			debugLogW(L"CSametimeProto::onMenuCreateChat() ClearInviteQueue");
			ClearInviteQueue();
		}

		free(id_block.user);
	}

	return 0;
}

int CSametimeProto::PrebuildContactMenu(WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)wParam;
	debugLogW(L"CSametimeProto::PrebuildContactMenu() hContact=[%x]", hContact);

	Menu_ShowItem(hLeaveChatMenuItem, db_get_b(hContact, m_szModuleName, "ChatRoom", 0) == 1);

	// if user is already in our meeting, 
	bool not_present = true;
	DBVARIANT dbv;
	if (my_conference && !db_get_utf(hContact, m_szModuleName, "stid", &dbv)) {
		char* user_id = dbv.pszVal;

		GList *members, *mem;
		members = mem = mwConference_getMembers(my_conference);
		for (;mem;mem=mem->next) {
			if (my_login_info && strcmp(user_id, ((mwLoginInfo *)mem->data)->user_id) == 0) {
				not_present = false;
				break;
			}
		}
		g_list_free(members);

		db_free(&dbv);
	}

	Menu_ShowItem(hCreateChatMenuItem, db_get_b(hContact, m_szModuleName, "ChatRoom", 0) == 0 && not_present);
	return 0;
}

void CSametimeProto::InitConference()
{
	debugLogW(L"CSametimeProto::InitConference()");

	my_login_info = mwSession_getLoginInfo(session);

	service_conference = mwServiceConference_new(session, &mwConference_handler);
	mwSession_addService(session, (struct mwService*)service_conference);

	HookProtoEvent(ME_GC_EVENT, &CSametimeProto::GcEventHook);
}

void CSametimeProto::DeinitConference()
{
	GList *conferences, *conf;
	debugLogW(L"CSametimeProto::DeinitConference()");

	if (service_conference){
		conferences = conf = mwServiceConference_getConferences(service_conference);
		for (;conf;conf = conf->next) {
			if (my_conference == conf->data)
				CloseMyConference(this);
			else
				mwConference_destroy((mwConference*)conf->data, 0, T2Utf(TranslateT("I'm outa here.")));
		}
		g_list_free(conferences);
	}

	my_login_info = nullptr;

	mwSession_removeService(session, mwService_CONFERENCE);
	if (service_conference){
		mwService_free((mwService*)service_conference);
		service_conference = nullptr;
	}
}

void CSametimeProto::InitConferenceMenu()
{
	debugLogW(L"CSametimeProto::InitConferenceMenu()");

	CreateProtoService(MS_SAMETIME_MENULEAVECHAT, &CSametimeProto::onMenuLeaveChat);
	CreateProtoService(MS_SAMETIME_MENUCREATECHAT, &CSametimeProto::onMenuCreateChat);

	CMenuItem mi(&g_plugin);
	mi.flags = CMIF_UNICODE | CMIF_NOTOFFLINE;
	
	SET_UID(mi, 0x98cf8a8c, 0x75ba, 0x46f2, 0xa3, 0x35, 0x65, 0x46, 0x4a, 0x38, 0x20, 0x7d);
	mi.name.w = LPGENW("Leave conference");
	mi.pszService = MS_SAMETIME_MENULEAVECHAT;
	mi.hIcolibItem = g_plugin.getIconHandle(IDI_ICON_LEAVE);
	hLeaveChatMenuItem = Menu_AddContactMenuItem(&mi, m_szModuleName);

	SET_UID(mi, 0x45501e10, 0x2914, 0x4daa, 0xb4, 0xcf, 0x83, 0x8a, 0x6a, 0x14, 0xd, 0x7);
	mi.name.w = LPGENW("Start conference");
	mi.pszService = MS_SAMETIME_MENUCREATECHAT;
	mi.hIcolibItem = g_plugin.getIconHandle(IDI_ICON_INVITE);
	hCreateChatMenuItem = Menu_AddContactMenuItem(&mi, m_szModuleName);

	HookProtoEvent(ME_CLIST_PREBUILDCONTACTMENU, &CSametimeProto::PrebuildContactMenu);
}