summaryrefslogtreecommitdiff
path: root/libpurple/protocols/mxit/multimx.c
blob: 8e0de5d7d4bd6d012db4c59f8a06abd494ffb9ef (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 *					MXit Protocol libPurple Plugin
 *
 *						-- MultiMx GroupChat --
 *
 *				Andrew Victor	<libpurple@mxit.com>
 *
 *			(C) Copyright 2009	MXit Lifestyle (Pty) Ltd.
 *				<http://www.mxitlifestyle.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "internal.h"
#include "purple.h"
#include "prpl.h"

#include "protocol.h"
#include "mxit.h"
#include "multimx.h"
#include "markup.h"


#if 0
static void multimx_dump(struct multimx* multimx)
{
	purple_debug_info(MXIT_PLUGIN_ID, "MultiMX:\n");
	purple_debug_info(MXIT_PLUGIN_ID, "  Chat ID: %i\n", multimx->chatid);
	purple_debug_info(MXIT_PLUGIN_ID, "  Username: %s\n", multimx->roomid);
	purple_debug_info(MXIT_PLUGIN_ID, "  Alias: %s\n", multimx->roomname);
	purple_debug_info(MXIT_PLUGIN_ID, "  State: %i\n", multimx->state);
}
#endif


/*------------------------------------------------------------------------
 * Find a MultiMx session based on libpurple chatID.
 *
 *  @param session		The MXit session object
 *  @param id			The libpurple group-chat ID
 *  @return				The MultiMX room object (or NULL if not found)
 */
static struct multimx* find_room_by_id(struct MXitSession* session, int id)
{
	GList* x = session->rooms;

	while (x != NULL) {
		struct multimx* multimx = (struct multimx *) x->data;

		if (multimx->chatid == id)
			return multimx;

		x = g_list_next(x);
	}

	return NULL;
}


/*------------------------------------------------------------------------
 * Find a MultiMx session based on Alias
 *
 *  @param session		The MXit session object
 *  @param roomname		The UI room-name
 *  @return				The MultiMX room object (or NULL if not found)
 */
static struct multimx* find_room_by_alias(struct MXitSession* session, const char* roomname)
{
	GList* x = session->rooms;

	while (x != NULL) {
		struct multimx* multimx = (struct multimx *) x->data;

		if (!strcmp(multimx->roomname, roomname))
			return multimx;

		x = g_list_next(x);
	}

	return NULL;
}


/*------------------------------------------------------------------------
 * Find a MultiMx session based on Username (MXit RoomId)
 *
 *  @param session		The MXit session object
 *  @param username		The MXit RoomID (MultiMX contact username)
 *  @return				The MultiMX room object (or NULL if not found)
 */
static struct multimx* find_room_by_username(struct MXitSession* session, const char* username)
{
	GList* x = session->rooms;

	while (x != NULL) {
		struct multimx* multimx = (struct multimx *) x->data;

		if (!strcmp(multimx->roomid, username))
			return multimx;

		x = g_list_next(x);
	}

	return NULL;
}


/*------------------------------------------------------------------------
 * Create a GroupChat room, and add to list of rooms.
 *
 *  @param session		The MXit session object
 *  @param roomid		The MXit RoomID (MultiMX contact username)
 *  @param roomname		The UI room-name
 *  @param state		The initial state of the room (see multimx.h)
 *  @return				The MultiMX room object
 */
static struct multimx* room_create(struct MXitSession* session, const char* roomid, const char* roomname, short state)
{
	struct multimx* multimx = NULL;
	static int groupchatID = 1;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat create - roomid='%s' roomname='%s'\n", roomid, roomname);

	/* Create a new GroupChat */
	multimx = g_new0(struct multimx, 1);

	/* Initialize groupchat */
	g_strlcpy(multimx->roomid, roomid, sizeof(multimx->roomid));
	g_strlcpy(multimx->roomname, roomname, sizeof(multimx->roomname));
	multimx->chatid = groupchatID++;
	multimx->state = state;

	/* determine our nickname (from profile) */
	if (session->profile && (session->profile->nickname[0] != '\0'))
		multimx->nickname = g_strdup(session->profile->nickname);

	/* Add to GroupChat list */
	session->rooms = g_list_append(session->rooms, multimx);

	return multimx;
}


/*------------------------------------------------------------------------
 * Free the Groupchat room.
 *
 *  @param session		The MXit session object
 *  @param multimx		The MultiMX room object to deallocate
 */
static void room_remove(struct MXitSession* session, struct multimx* multimx)
{
	/* Remove from GroupChat list */
	session->rooms = g_list_remove(session->rooms, multimx);

	/* free nickname */
	if (multimx->nickname)
		g_free(multimx->nickname);

	/* Deallocate it */
	g_free (multimx);
	multimx = NULL;
}


/*------------------------------------------------------------------------
 * Another user has join the GroupChat, add them to the member-list.
 *
 *  @param convo		The Conversation object
 *  @param nickname		The nickname of the user who joined the room
 */
static void member_added(PurpleConversation* convo, const char* nickname)
{
	purple_debug_info(MXIT_PLUGIN_ID, "member_added: '%s'\n", nickname);

	purple_conv_chat_add_user(PURPLE_CONV_CHAT(convo), nickname, NULL, PURPLE_CBFLAGS_NONE, TRUE);
}


/*------------------------------------------------------------------------
 * Another user has left the GroupChat, remove them from the member-list.
 *
 *  @param convo		The Conversation object
 *  @param nickname		The nickname of the user who left the room
 */
static void member_removed(PurpleConversation* convo, const char* nickname)
{
	purple_debug_info(MXIT_PLUGIN_ID, "member_removed: '%s'\n", nickname);

	purple_conv_chat_remove_user(PURPLE_CONV_CHAT(convo), nickname, NULL);
}


/*------------------------------------------------------------------------
 * A user was kicked from the GroupChat, remove them from the member-list.
 *
 *  @param convo		The Conversation object
 *  @param nickname		The nickname of the user who was kicked
 */
static void member_kicked(PurpleConversation* convo, const char* nickname)
{
	purple_debug_info(MXIT_PLUGIN_ID, "member_kicked: '%s'\n", nickname);

	purple_conv_chat_remove_user(PURPLE_CONV_CHAT(convo), nickname, _("was kicked"));
}


/*------------------------------------------------------------------------
 * You were kicked from the GroupChat.
 *
 *  @param convo		The Conversation object
 *  @param session		The MXit session object
 *  @param multimx		The MultiMX room object
 */
static void you_kicked(PurpleConversation* convo, struct MXitSession* session, struct multimx* multimx)
{
	purple_debug_info(MXIT_PLUGIN_ID, "you_kicked\n");

	purple_conv_chat_write(PURPLE_CONV_CHAT(convo), "MXit", _("You have been kicked from this MultiMX."), PURPLE_MESSAGE_SYSTEM, time(NULL));
	purple_conv_chat_clear_users(PURPLE_CONV_CHAT(convo));
	serv_got_chat_left(session->con, multimx->chatid);
}


/*------------------------------------------------------------------------
 * Update the full GroupChat member list.
 *
 *  @param convo		The Conversation object
 *  @param data			The nicknames of the users in the room (separated by \n)
 */
static void member_update(PurpleConversation* convo, char* data)
{
	gchar** userlist;
	int i = 0;

	purple_debug_info(MXIT_PLUGIN_ID, "member_update: '%s'\n", data);

	/* Clear list */
	purple_conv_chat_clear_users(PURPLE_CONV_CHAT(convo));

	/* Add each member */
	data = g_strstrip(data);				/* string leading & trailing whitespace */
	userlist = g_strsplit(data, "\n", 0);	/* tokenize string */
	while (userlist[i] != NULL) {
		purple_debug_info(MXIT_PLUGIN_ID, "member_update - adding: '%s'\n", userlist[i]);
		purple_conv_chat_add_user(PURPLE_CONV_CHAT(convo), userlist[i], NULL, PURPLE_CBFLAGS_NONE, FALSE);
		i++;
	}
	g_strfreev(userlist);
}


/* -------------------------------------------------------------------------------------------------
 * Calls from MXit Protocol layer
 * ------------------------------------------------------------------------------------------------- */

/*------------------------------------------------------------------------
 * Received a Subscription Request to a MultiMX room.
 *
 *  @param session		The MXit session object
 *  @param contact		The invited MultiMX room's contact information
 *  @param creator		The nickname of the room's creator / invitor
 */
void multimx_invite(struct MXitSession* session, struct contact* contact, const char* creator)
{
	GHashTable *components;
	struct multimx* multimx = NULL;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat invite to '%s' by '%s'\n", contact->alias, creator);

	/* Create a new room */
	multimx = room_create(session, contact->username, contact->alias, STATE_INVITED);

	components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
	g_hash_table_insert(components, g_strdup("room"), g_strdup(contact->alias));

	/* Call libpurple - will trigger either 'mxit_chat_join' or 'mxit_chat_reject' */
	serv_got_chat_invite(session->con, contact->alias, creator, NULL, components);
}


/*------------------------------------------------------------------------
 * MultiMX room has been added to the roster.
 *
 *  @param session		The MXit session object
 *  @param contact		The MultiMX room's contact information
 */
void multimx_created(struct MXitSession* session, struct contact* contact)
{
	PurpleConnection *gc = session->con;
	struct multimx* multimx = NULL;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat '%s' created as '%s'\n", contact->alias, contact->username);

	/* Find matching MultiMX group */
	multimx = find_room_by_username(session, contact->username);
	if (multimx == NULL) {
		multimx = room_create(session, contact->username, contact->alias, TRUE);
		}
	else if (multimx->state == STATE_INVITED) {
		/* After successfully accepting an invitation */
		multimx->state = STATE_JOINED;
	}

	/* Call libpurple - will trigger 'mxit_chat_join' */
	serv_got_joined_chat(gc, multimx->chatid, multimx->roomname);

	/* Send ".list" command to GroupChat server to retrieve current member-list */
	mxit_send_message(session, multimx->roomid, ".list", FALSE, FALSE);
}


/*------------------------------------------------------------------------
 * Is this username a MultiMX contact?
 *
 *  @param session		The MXit session object
 *  @param username		The username of the contact
 *  @return				TRUE if this contacts matches the RoomID of a MultiMX room.
 */
gboolean is_multimx_contact(struct MXitSession* session, const char* username)
{
	/* Check for username in list of open rooms */
	return (find_room_by_username(session, username) != NULL);
}


/*------------------------------------------------------------------------
 * Received a message from a MultiMX room.
 *
 */
void multimx_message_received(struct RXMsgData* mx, char* msg, int msglen, short msgtype, int msgflags)
{
	struct multimx* multimx = NULL;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat message received: %s\n", msg);

	/* Find matching multimx group */
	multimx = find_room_by_username(mx->session, mx->from);
	if (multimx == NULL) {
		purple_debug_error(MXIT_PLUGIN_ID, "Groupchat '%s' not found\n", mx->from);
		return;
	}

	/* Determine if system message or a message from a contact */
	if (msg[0] == '<') {
		/* Message contains embedded nickname - must be from contact */
		unsigned int i;

		for (i = 1; i < strlen(msg); i++) {		/* search for end of nickname */
			if (msg[i] == '>') {
				msg[i] = '\0';
				g_free(mx->from);
				mx->from = g_strdup(&msg[1]);
				msg = &msg[i+2];		/* skip '>' and newline */
				break;
			}
		}

		/* now do markup processing on the message */
		mx->chatid = multimx->chatid;
		mxit_parse_markup(mx, msg, strlen(msg), msgtype, msgflags);
	}
	else {
		/* Must be a service message */
		char* ofs;

		PurpleConversation* convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, multimx->roomname, mx->session->acc);
		if (convo == NULL) {
			purple_debug_error(MXIT_PLUGIN_ID, "Conversation '%s' not found\n", multimx->roomname);
			return;
		}

		/* Determine if somebody has joined or left - update member-list */
		if ((ofs = strstr(msg, " has joined")) != NULL) {
			/* Somebody has joined */
			*ofs = '\0';
			member_added(convo, msg);
			mx->processed = TRUE;
		}
		else if ((ofs = strstr(msg, " has left")) != NULL) {
			/* Somebody has left */
			*ofs = '\0';
			member_removed(convo, msg);
			mx->processed = TRUE;
		}
		else if ((ofs = strstr(msg, " has been kicked")) != NULL) {
			/* Somebody has been kicked */
			*ofs = '\0';
			member_kicked(convo, msg);
			mx->processed = TRUE;
		}
		else if (strcmp(msg, "You have been kicked.") == 0) {
			/* You have been kicked */
			you_kicked(convo, mx->session, multimx);
			mx->processed = TRUE;
		}
		else if (g_str_has_prefix(msg, "The following users are in this MultiMx:") == TRUE) {
			member_update(convo, msg + strlen("The following users are in this MultiMx:") + 1);
			mx->processed = TRUE;
		}
		else {
			/* Display server message in chat window */
			serv_got_chat_in(mx->session->con, multimx->chatid, "MXit", PURPLE_MESSAGE_SYSTEM, msg, mx->timestamp);
			mx->processed = TRUE;
		}
	}
}



/* -------------------------------------------------------------------------------------------------
 * Callbacks from libpurple
 * ------------------------------------------------------------------------------------------------- */

/*------------------------------------------------------------------------
 * User has selected "Add Chat" from the main menu.
 *
 *  @param gc			The connection object
 *  @return				A list of chat configuration values
 */
GList* mxit_chat_info(PurpleConnection *gc)
{
	GList *m = NULL;
	struct proto_chat_entry *pce;

	/* Configuration option: Room Name */
	pce = g_new0(struct proto_chat_entry, 1);
	pce->label = _( "_Room Name:" );
	pce->identifier = "room";
	pce->required = TRUE;
	m = g_list_append(m, pce);

	return m;
}


/*------------------------------------------------------------------------
 * User has joined a chatroom, either because they are creating it or they
 * accepted an invite.
 *
 *  @param gc			The connection object
 *  @param components	The list of chat configuration values
 */
void mxit_chat_join(PurpleConnection *gc, GHashTable *components)
{
	struct MXitSession* session = (struct MXitSession*) gc->proto_data;
	const char* roomname = NULL;
	struct multimx* multimx = NULL;

	purple_debug_info(MXIT_PLUGIN_ID, "mxit_chat_join\n");

	/* Determine if groupchat already exists */
	roomname = g_hash_table_lookup(components, "room");
	multimx = find_room_by_alias(session, roomname);

	if (multimx != NULL) {
		/* The room information already exists */

		if (multimx->state == STATE_INVITED) {
			/* Invite is pending */
			purple_debug_info(MXIT_PLUGIN_ID, "Groupchat %i accept sent\n", multimx->chatid);

			/* Send Subscription Accept to MXit */
			mxit_send_allow_sub(session, multimx->roomid, multimx->roomname);
		}
		else {
			/* Join existing room */
			purple_debug_info(MXIT_PLUGIN_ID, "Groupchat %i rejoined\n", multimx->chatid);

			serv_got_joined_chat(gc, multimx->chatid, multimx->roomname);
		}
	}
	else {
		/* Send Groupchat Create to MXit */
		mxit_send_groupchat_create(session, roomname, 0, NULL);
	}
}


/*------------------------------------------------------------------------
 * User has rejected an invite to join a MultiMX room.
 *
 *  @param gc			The connection object
 *  @param components	The list of chat configuration values
 */
void mxit_chat_reject(PurpleConnection *gc, GHashTable* components)
{
	struct MXitSession* session = (struct MXitSession*) gc->proto_data;
	const char* roomname = NULL;
	struct multimx* multimx = NULL;

	purple_debug_info(MXIT_PLUGIN_ID, "mxit_chat_reject\n");

	roomname = g_hash_table_lookup(components, "room");
	multimx = find_room_by_alias(session, roomname);
	if (multimx == NULL) {
		purple_debug_error(MXIT_PLUGIN_ID, "Groupchat '%s' not found\n", roomname);
		return;
	}

	/* Send Subscription Reject to MXit */
	mxit_send_deny_sub(session, multimx->roomid);

	/* Remove from our list of rooms */
	room_remove(session, multimx);
}


/*------------------------------------------------------------------------
 * Return name of chatroom (on mouse hover)
 *
 *  @param components	The list of chat configuration values.
 *  @return				The name of the chat room
 */
char* mxit_chat_name(GHashTable *components)
{
	return g_strdup(g_hash_table_lookup(components, "room"));
}


/*------------------------------------------------------------------------
 * User has selected to invite somebody to a chatroom.
 *
 *  @param gc			The connection object
 *  @param id			The chat room ID
 *  @param msg			The invitation message entered by the user
 *  @param name			The username of the person to invite
 */
void mxit_chat_invite(PurpleConnection *gc, int id, const char *msg, const char *username)
{
	struct MXitSession* session = (struct MXitSession*) gc->proto_data;
	struct multimx* multimx = NULL;
	PurpleBuddy* buddy;
	PurpleConversation *convo;
	char* tmp;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat invite to '%s'\n", username);

	/* Find matching MultiMX group */
	multimx = find_room_by_id(session, id);
	if (multimx == NULL) {
		purple_debug_error(MXIT_PLUGIN_ID, "Could not find groupchat %i\n", id);
		return;
	}

	/* Send invite to MXit */
	mxit_send_groupchat_invite(session, multimx->roomid, 1, &username);

	/* Find the buddy information for this contact (reference: "libpurple/blist.h") */
	buddy = purple_find_buddy(session->acc, username);
	if (!buddy) {
		purple_debug_warning(MXIT_PLUGIN_ID, "mxit_chat_invite: unable to find the buddy '%s'\n", username);
		return;
	}

	convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, multimx->roomname, session->acc);
	if (convo == NULL) {
		purple_debug_error(MXIT_PLUGIN_ID, "Conversation '%s' not found\n", multimx->roomname);
		return;
	}

	/* Display system message in chat window */
	tmp = g_strdup_printf("%s: %s", _("You have invited"), purple_buddy_get_alias(buddy));
	purple_conv_chat_write(PURPLE_CONV_CHAT(convo), "MXit", tmp, PURPLE_MESSAGE_SYSTEM, time(NULL));
	g_free(tmp);
}


/*------------------------------------------------------------------------
 * User as closed the chat window, and the chatroom is not marked as persistent.
 *
 *  @param gc			The connection object
 *  @param id			The chat room ID
 */
void mxit_chat_leave(PurpleConnection *gc, int id)
{
	struct MXitSession* session = (struct MXitSession*) gc->proto_data;
	struct multimx* multimx = NULL;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat %i leave\n", id);

	/* Find matching multimx group */
	multimx = find_room_by_id(session, id);
	if (multimx == NULL) {
		purple_debug_error(MXIT_PLUGIN_ID, "Could not find groupchat %i\n", id);
		return;
	}

	/* Send Remove Groupchat to MXit */
	mxit_send_remove(session, multimx->roomid);

	/* Remove from our list of rooms */
	room_remove(session, multimx);
}


/*------------------------------------------------------------------------
 * User has entered a message in a chatroom window, send it to the MXit server.
 *
 *  @param gc			The connection object
 *  @param id			The chat room ID
 *  @param message		The sent message data
 *  @param flags		The message flags
 *  @return				Indicates success / failure
 */
int mxit_chat_send(PurpleConnection *gc, int id, const char *message, PurpleMessageFlags flags)
{
	struct MXitSession* session = (struct MXitSession*) gc->proto_data;
	struct multimx* multimx = NULL;
	const char* nickname;

	purple_debug_info(MXIT_PLUGIN_ID, "Groupchat %i message send: '%s'\n", id, message);

	/* Find matching MultiMX group */
	multimx = find_room_by_id(session, id);
	if (multimx == NULL) {
		purple_debug_error(MXIT_PLUGIN_ID, "Could not find groupchat %i\n", id);
		return -1;
	}

	/* Send packet to MXit */
	mxit_send_message(session, multimx->roomid, message, TRUE, FALSE);

	/* Determine our nickname to display */
	if (multimx->nickname)
		nickname = multimx->nickname;
	else
		nickname = purple_account_get_alias(purple_connection_get_account(gc));		/* local alias */

	/* Display message in chat window */
	serv_got_chat_in(gc, id, nickname, flags, message, time(NULL));

	return 0;
}