aboutsummaryrefslogtreecommitdiff
path: root/src/sbsessions.c
blob: 0ce353cb0c48fdb0c7182e8aac83b7be8529f0dd (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
/*
 * sbsessions.c
 * 
 * SB session (=>IM window) management (but not the protocol nor UI)
 *
 * (c) 2002-2005 Thomas White <taw27@srcf.ucam.org>
 *  Part of TuxMessenger - GTK+-based MSN Messenger client
 *
 * This package 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; version 2 dated June, 1991. 
 *
 * This package 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 package; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA. 
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "debug.h"
#include "messagewindow.h"
#include "sbsessions.h"
#include "sbprotocol.h"
#include "msnprotocol.h"
#include "contactlist.h"
#include "msngenerics.h"
#include "options.h"
#include "msnp2p.h"
#include "error.h"
#include "routines.h"
#include "fonttrans.h"

SbSession *sessions = NULL;

static SbSession *sbsessions_lastsession() {

	SbSession *session = sessions;

	while ( session ) {
	
		assert(session != NULL);

		if ( session->next == NULL ) {
			return session;
		} else {
			session = session->next;
		}
		
	}
	
	return NULL;	/* If there were no sessions at all. */

}

static void sbsessions_inituser(SbUser *new_user) {

}

static int sbsessions_ready_callback(SbSession *session) {

	if ( !session->ready ) {
		debug_print("SS %8p: Not ready after 60 seconds - deleting.\n", session);
		sbsessions_destroy(session);
		return FALSE;
	}	
	session->ready_timeout = 0;
	
	return FALSE;	/* Don't repeat */

}


static SbSession *sbsessions_new(const char *username, SbSessionSource source) {

	SbSession *new_session;
	SbSession *last_session;
		
	new_session = malloc(sizeof(SbSession));
	new_session->users = malloc(sizeof(SbUser));
	assert(new_session->users != NULL);
	
	new_session->messagewindow = NULL;
		
	new_session->users->next = NULL;
	new_session->users->username = strdup(username);
	sbsessions_inituser(new_session->users);
		
	new_session->ready = FALSE;
	new_session->num_users = 1;
	new_session->rbufsize = 0;
	new_session->wbufsize = 0;
	new_session->rbuffer = NULL;
	new_session->wbuffer = NULL;
	new_session->roffset = 0;
	new_session->roffset = 0;
	new_session->am_typing = 0;
	new_session->am_typing_callback = 0;
	new_session->threeway_intent = NULL;
	new_session->multipackets = NULL;
	new_session->trid = 1;	/* Reset in sbprotocol_connect */
	new_session->cached = NULL;

	new_session->source = source;
	new_session->rcallback = 0;
	new_session->wcallback = 0;
	
	/* 60 seconds to get ready. */
	new_session->ready_timeout = gtk_timeout_add(60000, (GtkFunction)sbsessions_ready_callback, new_session);
	
	/* Link it into the list. */
	last_session = sbsessions_lastsession();
	if ( last_session != NULL ) {
		assert(last_session->next == NULL);
		last_session->next = new_session;
	} else {
		sessions = new_session;
	}
	new_session->next = NULL;

	return new_session;

}

SbSession *sbsessions_create_local(const char *username) {

	SbSession *session = sbsessions_new(username, SESSION_SOURCE_LOCAL);
	
	session->neg_trid = msnprotocol_initiatesb();
	debug_print("SS %8p: New local session for user %s.\n", session, username);
	
	return session;

}

/* Called when the user clicks on a name in the contact list. */
SbSession *sbsessions_create_remote(char *username, char *switchboardaddress, char *sessionid, char *authchallenge) {

	SbSession *session = sbsessions_new(username, SESSION_SOURCE_REMOTE);
	
	session->neg_trid = 0;
	sbprotocol_initiate_remote(session, switchboardaddress, sessionid, authchallenge);
	debug_print("SS %8p: New remote session for user %s.\n", session, username);

	return session;

}

/* Create a three-way session in "one go". */
SbSession *sbsessions_create_threeway(char *username1, char *username2) {

	SbSession *session = sbsessions_new(username1, SESSION_SOURCE_LOCAL);

	session->threeway_intent = strdup(username2);
	session->neg_trid = msnprotocol_initiatesb();
	debug_print("SS %8p: New local session for user %s.  Intending to invite %s too.\n", session, username1, username2);

	return session;

}

void sbsessions_plug(SbSession *session, MessageWindow *messagewindow) {
	session->messagewindow = messagewindow;
}

/* Unplug any SB sessions which are plugged into a given IM window */
void sbsessions_unplug(MessageWindow *messagewindow) {

	SbSession *session = sessions;
	
	while ( session != NULL ) {
		if ( session->messagewindow == messagewindow ) {
			session->messagewindow = NULL;
		}
		session = session->next;
	}

}

/* Find a headless session with this username. */
SbSession *sbsessions_find_headless(const char *username) {

	SbSession *session = sessions;
	
	while ( session != NULL ) {
		if ( (session->num_users == 1) && (strcmp(username, session->users->username) == 0) && (session->messagewindow == NULL) ) {
			return session;
		}
		session = session->next;
	}
	
	return NULL;	

}

/* Find a session with just this user in, and noone about to randomly join (hopefully...) */
SbSession *sbsessions_find_single_safe(const char *username) {

	SbSession *session = sessions;
	
	while ( session != NULL ) {
		if ( (session->num_users == 1) && (strcmp(username, session->users->username) == 0) ) {
			if ( session->threeway_intent == NULL ) {
				return session;
			}
		}
		session = session->next;
	}
	
	return NULL;	

}


/* SbUsers are unique to a particular SbSession.  Find the parent SbSession
	given the SbUser. Could just keep a note of the parent record in the
	SbUser record, but sbsessions_stoptyping needs to know if the SbUser
	is still attached to (any) SbSession. */
SbSession *sbsessions_find_user(SbUser *target_user) {

	SbSession *session;

	session = sessions;
	while ( session != NULL ) {	
		SbUser *user = session->users;
		while ( user != NULL ) {		
			if ( user == target_user ) {
				return session;
			}		
			user = user->next;		
		}		
		session = session->next;		
	}
	
	debug_print("SS %8p: Couldn't find user in all sessions.\n", NULL);
	return NULL;

}

/* Return a session's record given "TrID". */
SbSession *sbsessions_find_trid(unsigned int trid) {

	SbSession *session;

	session = sessions;
	while ( session != NULL ) {
	
		assert(session != NULL);
	
		if ( session->neg_trid == trid ) {
			return session;
		} else {
			session = session->next;
		}
		
	}
	
	debug_print("SS %8p: Failed to find session negotiated with TrId %i\n", NULL, trid);
	return NULL;

}

SbUser *sbsessions_find_username(SbSession *session, const char *username) {

	SbUser *user;
	
	assert(session != NULL);
	assert(session->users != NULL);
	
	user = session->users;
	while ( user != NULL ) {
	
		assert(user != NULL);
		
		/* Case-insensitive here.  Username may have different case depending
			on its source, since the servers seem to change usernames to
			lower case but clients might not in (e.g.) TypingUser controls. */
		if ( strcasecmp(user->username, username) == 0 ) {
			return user;
		} else {
			user = user->next;
		}
	
	}
	
	return NULL;

}

/* Locate a user on a given SB session from their DP SHA1D field. */
SbUser *sbsessions_find_dpsha1d(SbSession *session, char *sha1d) {

	SbUser *user;
	
	assert(session != NULL);
	assert(session->users != NULL);
	
	user = session->users;
	while ( user != NULL ) {
	
		assert(user != NULL);
		
		if ( strcmp(user->dpsha1d, sha1d) == 0 ) {
			return user;
		} else {
			user = user->next;
		}
	
	}
	
	return NULL;

}

void sbsessions_destroy(SbSession *session) {

	SbSession *prev_session;
	SbUser *user;
	CachedMsg *cached;
	
	/* Unplug the session and close the socket. */
	messagewindow_unplug(session);
	sbprotocol_close(session);
	
	/* Remove the session from the list. */
	prev_session = sessions;
	if ( prev_session != session ) {
		while ( prev_session != NULL ) {
			assert(prev_session != NULL);
			if ( prev_session->next == session ) {
				break;
			} else {
				prev_session = prev_session->next;
			}
		}
		assert(prev_session->next == session);
		/* Link it out of the list. */
		prev_session->next = session->next;
	} else {
		/* This session was the first on the list. */
		assert(sessions == session);	/* Can't fail... */
		sessions = session->next;	/* Which may be NULL if the list is now empty. */
	}	
	
	/* Drop all MSNP2P sessions in this SB session. */
	msnp2p_abortall(session);
	
	/* Drop any cached messages, and inform the user. */
	cached = session->cached;
	while ( cached != NULL ) {
	
		CachedMsg *next = cached->next;
		if ( !session->messagewindow ) {
			messagewindow_mitigate(session);
		}
		messagewindow_reportdropped(session->messagewindow, cached->message, cached->length);
		free(cached->message);
		free(cached);
		cached = next;
		messagewindow_unplug(session);	/* This session doesn't exist, remember? */
	
	}
	
	/* Remove all the users. */
	user = session->users;
	while ( user != NULL ) {
	
		SbUser *next_user = user->next;
		free(user->username);
		free(user);
		user = next_user;
		
	}
	
	/* Wind up the low-level business. */
	if ( session->rcallback != 0 ) {
		gdk_input_remove(session->rcallback);
	}
	session->rcallback = 0;
	if ( session->wcallback != 0 ) {
		gdk_input_remove(session->wcallback);
	}
	session->wcallback = 0;

	if ( session->ready_timeout != 0 ) {
		gtk_timeout_remove(session->ready_timeout);
	}
	session->ready_timeout = 0;
		
	if ( session->am_typing_callback != 0 ) {
		gtk_timeout_remove(session->am_typing_callback);
		session->am_typing = 0;
		session->am_typing_callback = 0;
	}
	
	if ( session->rbuffer != NULL ) {
		free(session->rbuffer);
	}
	if ( session->wbuffer != NULL ) {
		free(session->wbuffer);
	}
	
	free(session);

}

SbUser *sbsessions_lastuser(SbSession *session) {

	SbUser *user;

	user = session->users;
	while ( user != NULL ) {
	
		assert(user != NULL);
		
		if ( user->next == NULL ) {
			return user;
		} else {
			user = user->next;
		}
	
	}
	
	debug_print("SS %8p: Reached end of sbsessions_lastuser!  This doesn't happen!\n", session);
	return NULL;

}

static SbUser *sbsessions_addnewuser(SbSession *session, char *username) {

	SbUser *new_user;
	SbUser *previous_user;

	new_user = malloc(sizeof(SbUser));
	assert(new_user != NULL);
	new_user->next = NULL;
	new_user->username = strdup(username);
	sbsessions_inituser(new_user);

	previous_user = sbsessions_lastuser(session);
	assert(previous_user->next == NULL);
	previous_user->next = new_user;
	
	session->num_users++;
	
	return new_user;

}

/* Deal with any user joining a session (whether by JOI or IRO). */
void sbsessions_joined(SbSession *session, char *username, char *friendlyname) {

	SbUser *user;

	/* "friendlyname" is from the JOI message.  This user might not be in any of the
		contact lists.  Now would be a good time to check, and if not, create a
		temporary record for them. */
	if ( contactlist_friendlyname(username) == NULL ) {	
		debug_print("SS %8p: Creating temporary user record for '%s'/'%s'\n", session, username, friendlyname);
		contactlist_tldetails(CONTACT_SOURCE_RNG, username, friendlyname, ONLINE_NLN);
	}
	
	user = sbsessions_find_username(session, username);
	if ( user == NULL ) {
	
		/* User is new - probably got invited by someone else */
		debug_print("SS %8p: Couldn't find user %s in SB record - adding them.\n", session, username);
		
		user = sbsessions_addnewuser(session, username);
				
	}
	
	if ( session->messagewindow != NULL ) {
		messagewindow_joined(session->messagewindow, username);
	}

}

/* Deal with any user leaving a session. */
void sbsessions_left(SbSession *session, char *username) {

	SbUser *user;
	
	user = sbsessions_find_username(session, username);
	if ( user == NULL ) {
		debug_print("SS %8p: Couldn't find leaving user %s in SB record!\n", session, username);
		return;
	}
	
	if ( session->num_users == 1 ) {
		sbprotocol_leavesession(session);
	} else {

		/* "Normal" situation.  Remove user record, their DP and status bar. */

		SbUser *find_user;

		session->num_users--;
		if ( session->messagewindow != NULL ) {
			messagewindow_removeuser(session->messagewindow, username);
		}
		
		/* Find the preceding user in the session and link this user out. */
		find_user = session->users;
		if ( find_user == user ) {
			/* User was the first in the list. */
			session->users = user->next;
		} else {
		
			while ( find_user != NULL ) {
		
				assert(find_user != NULL);
			
				if ( find_user->next == user ) {
					find_user->next = user->next;
					break;
				} else {
					find_user = find_user->next;
				}
		
			}

		}
		free(user->username);
		free(user);

	}

}

static int sbsessions_amtyping_callback(SbSession *session) {

	session->am_typing = 0;
	session->am_typing_callback = 0;
	
	return FALSE;	/* Don't repeat */

}

void sbsessions_am_typing(SbSession *session) {

	if ( !session->ready ) {
		debug_print("SS %8p: Not ready - not sending TypingUser.\n", session);
		return;
	}

	/* Calculate timeout then send TypingUser control. */
	if ( session->am_typing == 0 ) {

		sbprotocol_sendtypingcontrol(session);
		session->am_typing = 1;
		session->am_typing_callback = gtk_timeout_add(5000, (GtkFunction)sbsessions_amtyping_callback, session);
	
	}

}

/* Check a session is ready for I/O. */
int sbsessions_sessionready(SbSession *session) {

	if ( session == NULL ) {
		return FALSE;
	}

	return session->ready;

}

/* Delete all sessions. */
void sbsessions_destroy_all() {

	SbSession *session;
	SbSession *next;

	debug_print("SB %8p: destroying all sessions.\n", NULL);

	session = sessions;
	while ( session != NULL ) {
		next = session->next;
		sbsessions_destroy(session);
		session = next;
	}

}