aboutsummaryrefslogtreecommitdiff
path: root/src/sbprotocol.c
blob: 1d58ac75d1ff2705369e7838f1f8432c560224e5 (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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
/*
 * sbprotocol.c
 * 
 * SB protocol handling
 *
 * (c) 2002-2006 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 <assert.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>

#include "sbsessions.h"
#include "msnprotocol.h"
#include "error.h"
#include "debug.h"
#include "options.h"
#include "routines.h"
#include "messagewindow.h"
#include "mime.h"
#include "contactlist.h"
#include "main.h"
#include "msnp2p.h"
#include "msninvite.h"
#include "ink.h"
#include "sbprotocol.h"
#include "msngenerics.h"

/* Maximum allowed message size, and chunk size to split large messages into.
 *	MAX_MSG_SIZE needs to be sufficiently bigger than MULTIPACKET_SIZE to
 *	fit the headers in, otherwise you get an infinite loop. */
#define MULTIPACKET_SIZE 1024
#define MAX_MSG_SIZE 1280

/* Define this if you want to see what gets written to the socket. */
#define SBSEND_DEBUG 1
/* Define if you want to see SB write buffer operations. */
#undef SBWRITE_DEBUG

/* Some prototypes */
static void sbprotocol_sendmsg(SbSession *session, char *contenttype, char *extra_headers, char *body, ssize_t length);

/* Called when a socket gets disconnected.  Session record vanishes. */
static void sbprotocol_disconnected(SbSession *session) {
	debug_print("SB %8p: Disconnected - deleting.\n", session);
	sbsessions_destroy(session);
}

/* Write a chunk of the outgoing data queue to an SB socket. */
static void sbprotocol_writeable(SbSession *session) {

	ssize_t wlen;
	unsigned int new_wbufsize;
#ifdef SBSEND_DEBUG
	char *debug_string;
	unsigned int i;
#endif	/* SBSEND_DEBUG */

	wlen = write(session->socket, session->wbuffer, session->wbufsize);

#ifdef SBSEND_DEBUG
	debug_string = malloc(session->wbufsize+1);
	memcpy(debug_string, session->wbuffer, session->wbufsize);
	debug_string[wlen] = '\0';
	for ( i=0; i<session->wbufsize; i++ ) {
		if ( debug_string[i] == '\r' ) {
			debug_string[i] = 'r';
		}
		if ( debug_string[i] == '\n' ) {
			debug_string[i] = 'n';
		}
	}
	debug_print("SB %8p: Send:'%s'\n", session, debug_string);
	free(debug_string);
#endif	/* SBSEND_DEBUG */

	if ( wlen > 0 ) {
	
#ifdef SBWRITE_DEBUG
		debug_print("SB %8p: Wrote %i bytes to socket.\n", session, wlen);
#endif	/* SBWRITE_DEBUG */
	
		/* wlen holds the number of bytes written.  Sort the buffer out accordingly... */
		memmove(session->wbuffer, session->wbuffer + wlen, session->wbufsize - wlen);
		new_wbufsize = session->wbufsize - wlen;

		session->wbuffer = realloc(session->wbuffer, new_wbufsize);
		if ( new_wbufsize == 0 ) {
		
#ifdef SBWRITE_DEBUG
			debug_print("SB %8p: SB write buffer empty: destroying.\n", session);
#endif	/* SBWRITE_DEBUG */
			gdk_input_remove(session->wcallback);
			session->wcallback = 0;
			session->wbuffer = NULL;
		
		}
		session->wbufsize = new_wbufsize;
		session->woffset -= wlen;		

	} else {
	
		if ( wlen == -1 ) {
		
			/* Write error! :( */
			if ( errno != EAGAIN ) {	/* EAGAIN should never happen here */

				/* Something bad happened */
				int closeval;
				closeval = close(session->socket);
				if ( closeval != 0 ) {
					/* Grrr..  now it won't close - not much we can do. */
					debug_print("SB %8p: Couldn't close socket after write error.\n", session);
				}
				sbprotocol_disconnected(session);
				return;
				
			}
			
		}
		
	}

}

/* Queue data for sending to the server, adding a TrID - returns the TrID used for this transaction. */
static int sbprotocol_sendtr_internal(SbSession *session, const char *instr, const char *args, size_t arglen, int newline) {

	char *trid_string;
	size_t len;

	len = strlen(instr);
	trid_string = malloc(8);
	assert(session->trid < 999999);	/* Sanity check */
	sprintf(trid_string, "%i", session->trid);
	len += 1;	/* Space before the TrId */
	len += strlen(trid_string);
	if ( arglen > 0 ) {
		len += arglen;
		len += 1;	/* Space after the TrID */
	}
	if ( newline ) {
		len += 2;
	}
	
	if ( session->wbufsize == 0 ) {

		/* No buffer space currently exists.  Create it. */
#ifdef SBWRITE_DEBUG
		debug_print("SB %8p: Creating SB write buffer: %i bytes.\n", session, len);
#endif	/* SBWRITE_DEBUG */
		assert(session->wbuffer == NULL);
		session->wbuffer = malloc(len);
		assert(session->wbuffer != NULL);
		session->wbufsize = len;
		session->woffset = 0;
		
	}
	if ( (session->wbufsize - session->woffset) < len ) {

		/* Write buffer isn't big enough.  Make it bigger. */
#ifdef SBWRITE_DEBUG
		debug_print("SB %8p: Extending SB write buffer. Old size=%i (offset %i), Need %i, New size=%i\n", session, session->wbufsize, session->woffset, len, len+session->woffset);
#endif	/* SBWRITE_DEBUG */
		session->wbuffer = realloc(session->wbuffer, len + session->woffset);
		assert(session->wbuffer != NULL);
		session->wbufsize = len + session->woffset;
		assert(session->wbufsize < 1024*1024);	/* Stop the buffer from getting insane */
		
	}
	
	/* Do the write (to memory).  Deliberately verbose... */
	memcpy(session->wbuffer + session->woffset, instr, strlen(instr));
	session->woffset += strlen(instr);
	
	*(char *)(session->wbuffer + session->woffset) = ' ';
	session->woffset += 1;
	
	memcpy(session->wbuffer + session->woffset, trid_string, strlen(trid_string));
	session->woffset += strlen(trid_string);
	free(trid_string);
	
	if ( arglen > 0 ) {
	
		*(session->wbuffer + session->woffset) = ' ';
		session->woffset += 1;
		memcpy(session->wbuffer + session->woffset, args, arglen);
		session->woffset += arglen;
		
	}
	
	if ( newline ) {
	
		*(session->wbuffer + session->woffset) = '\r';
		session->woffset += 1;
		*(session->wbuffer + session->woffset) = '\n';
		session->woffset += 1;
				
	}
	/* Note the lack of a \0 terminator.  It's done using records of the buffer data lengths. */

	if ( session->wcallback == 0 ) {
		session->wcallback = gdk_input_add(session->socket, GDK_INPUT_WRITE, (GdkInputFunction)sbprotocol_writeable, session);
	}

	session->trid++;
	return session->trid-1;

}

/* Send a command to the switchboard, adding a TrID and a newline */
static int sbprotocol_sendtr(SbSession *session, const char *instr, const char *args) {
	return sbprotocol_sendtr_internal(session, instr, args, strlen(args), 1);
}

/* Send a command to the switchboard, adding a TrID but no newline. */
int sbprotocol_sendtr_nonewline(SbSession *session, const char *instr, const char *args, ssize_t length) {
	return sbprotocol_sendtr_internal(session, instr, args, length, 0);
}

void sbprotocol_leavesession(SbSession *session) {
	sbprotocol_sendtr(session, "OUT", "");
}

/* Split a large message up and throw it back at sbprotocol_sendmsg */
static int sbprotocol_sendmultipacket(SbSession *session, char *contenttype, char *extra_headers, char *body, ssize_t length) {

	char *messageid = routines_guid();
	int parts = (length / MULTIPACKET_SIZE) + 1;
	int i;
	char *new_extra_headers;
	char *packet = malloc(MULTIPACKET_SIZE);
	
	debug_print("SB %8p: Sending multipacket message (%i parts)\n", session, parts);
	if ( parts > 9999 ) {
		debug_print("SB %8p: Silly number of packets - aborting.\n", session);
		return 0;
	}
	
	/* Message-ID:     Chunks:     Chunk: */
	new_extra_headers = malloc(strlen(extra_headers) +12+40 +7+4 +6+4 +1);
	
	for ( i=0; i<parts; i++ ) {
	
		ssize_t size;
		
		strcpy(new_extra_headers, extra_headers);
		strcat(new_extra_headers, "\r\nMessage-ID: ");
		strcat(new_extra_headers, messageid);
			
		if ( i == 0 ) {
		
			/* First packet. Modify header to include number of chunks... */
			
			char *chunks;
			
			strcat(new_extra_headers, "\r\nChunks: ");
			chunks = malloc(5);
			sprintf(chunks, "%i", parts);
			strcat(new_extra_headers, chunks);
			free(chunks);
			
		} else {
		
			/* Not first packet: include chunk ID */
			
			char *chunk;
			
			strcat(new_extra_headers, "\r\nChunk: ");
			chunk = malloc(5);
			sprintf(chunk, "%i", i);
			strcat(new_extra_headers, chunk);
			free(chunk);
			
		}

		size = MULTIPACKET_SIZE;
		if ( i == parts-1 ) {
			/* Last message */
			size = length % MULTIPACKET_SIZE;
		}
		memcpy(packet, body+(MULTIPACKET_SIZE*i), size);
		
		debug_print("SB %8p: Sending packet %i of %i (bytes %i-%i of %i)\n", session, i+1, parts, MULTIPACKET_SIZE*i, size+(MULTIPACKET_SIZE*i), length);
		sbprotocol_sendmsg(session, contenttype, new_extra_headers, packet, size);
		
	}
	
	return 1;	
	
}

static void sbprotocol_flushcache(SbSession *session) {

	CachedMsg *cached = session->cached;
	CachedMsg *next;
	
	while ( cached != NULL ) {
	
		debug_print("Sending cached message %8p.\n", cached);
		sbprotocol_sendtr_nonewline(session, "MSG", cached->message, cached->length);
		free(cached->message);
		next = cached->next;
		free(cached);
		session->cached = next;
		cached = next;
		
	}

}

static void sbprotocol_sendmsg(SbSession *session, char *contenttype, char *extra_headers, char *body, ssize_t mlength) {

	char *serversend;
	char *length_text;
	size_t length;

	length = mlength;
	if ( extra_headers != NULL ) {
		length += (strlen(extra_headers) + 2);
	}
	length += (35 + strlen(contenttype) + 2);

	/* Check if this needs to be multipacketed or not... */
	if ( length > MAX_MSG_SIZE ) {
		sbprotocol_sendmultipacket(session, contenttype, extra_headers, body, mlength);
		return;
	}

	serversend = malloc(length+15);

	strcpy(serversend, "N ");
	length_text = malloc(16);
	sprintf(length_text, "%i", length);
	strncat(serversend, length_text, 15);
	
	strcat(serversend, "\r\nMIME-Version: 1.0\r\nContent-Type: ");
	strcat(serversend, contenttype);
	strcat(serversend, "\r\n");
	if ( extra_headers != NULL ) {
		strcat(serversend, extra_headers);
		strcat(serversend, "\r\n");
	}
	strcat(serversend, "\r\n");
	memcpy(serversend+strlen(serversend), body, mlength);
	
	if ( session->ready ) {
		sbprotocol_sendtr_nonewline(session, "MSG", serversend, length+strlen(length_text)+4);
	} else {

		CachedMsg *cached = malloc(sizeof(CachedMsg));

		cached->message = malloc(length+strlen(length_text)+5);
		memcpy(cached->message, serversend, length+strlen(length_text)+5);
		
		cached->message[length+strlen(length_text)+4] = '\0';	/* NULL-terminate! */
				
		cached->length = length+strlen(length_text)+4;
		
		if ( session->cached == NULL ) {
			session->cached = cached;
		} else {
			CachedMsg *find = session->cached;
			while ( find->next != NULL ) {
				find = find->next;
			}
			find->next = cached;
		}

		cached->next = NULL;

		debug_print("SB %8p: Cached message for sending later (%8p).\n", session, cached);
			
	}

	free(length_text);
	free(serversend);

}

void sbprotocol_sendcaps(SbSession *session) {

	char *caps_body;
	
	caps_body = malloc(64 + strlen(tuxmessenger_versionstring()));
	strcpy(caps_body, "Client-Name: ");
	strcat(caps_body, tuxmessenger_versionstring());
	strcat(caps_body, "\r\nChat-Logging: N");	/* Logging not implemented yet. */

	sbprotocol_sendmsg(session, "text/x-clientcaps", NULL, caps_body, strlen(caps_body));
	free(caps_body);

}

void sbprotocol_invite(SbSession *session, const char *username) {

	sbprotocol_sendtr(session, "CAL", username);

}

static void sbprotocol_parseline(SbSession *session, char *line) {

	char *token;

	debug_print("SB %8p: Recv '%s'\n", session, line);
	assert(session != NULL);
	
	token = routines_lindex(line, 0);
	
	if ( strcmp(token, "USR") == 0 ) {
	
		char *status;
		
		status = routines_lindex(line, 2);
		
		if ( strcmp(status, "OK") == 0 ) {
	
			if ( session->num_users != 1 ) {
				/* Not quite inconceivable, but would only happen in a *really* screwed-up case. */
				debug_print("SB %8p: %i users on session record during negotiation!\n", session, session->num_users);
			}
		
			assert(session->users != NULL);
			assert(session->users->username != NULL);
			debug_print("SB %8p: Inviting %s\n", session, session->users->username);
			sbprotocol_invite(session, session->users->username);
			if ( session->threeway_intent != NULL ) {
				debug_print("SB %8p: Also inviting %s\n", session, session->threeway_intent);
				sbprotocol_invite(session, session->threeway_intent);
			}

		} else {
		
			debug_print("SB %8p: Negotiation failed\n", session);
		
		}
		
		free(status);

	}
	
	if ( strcmp(token, "JOI") == 0 ) {
	
		char *username;
		char *friendlyname;
		
		session->ready = TRUE;
		sbprotocol_sendcaps(session);
		sbprotocol_flushcache(session);
	
		username = routines_lindex(line, 1);
		friendlyname = routines_lindex(line, 2);
		sbsessions_joined(session, username, friendlyname);
		free(username);
		free(friendlyname);
		
	}
	
	if ( strcmp(token, "IRO") == 0 ) {
	
		char *username;
		char *friendlyname;
		
		username = routines_lindex(line, 4);
		friendlyname = routines_lindex(line, 5);
		session->ready = TRUE;
		sbsessions_joined(session, username, friendlyname);
		free(username);
		free(friendlyname);
	
	}
	
	if ( strcmp(token, "MSG") == 0 ) {
	
		char *length;
		
		length = routines_lindex(line, 3);
		session->expect_length = atoi(length);
		free(length);
		
		session->msg_source = routines_lindex(line, 1);
		
		assert(session->expect_length > 0);
		session->conmode = SBCONMODE_MSG;
	
	}
	
	if ( strcmp(token, "BYE") == 0 ) {
	
		char *username;
		username = routines_lindex(line, 1);
		sbsessions_left(session, username);
		free(username);
	
	}
	
	if ( strcmp(token, "ANS") == 0 ) {
	
		char *reply;
		
		reply = routines_lindex(line, 2);
		if ( strcmp(reply, "OK") == 0 ) {
			session->ready = TRUE;	/* This is probably already the case. */
			sbprotocol_sendcaps(session);
		} else {
			debug_print("SB %8p: ANS reply not OK!\n", session);
		}
		free(reply);
	
	}
	
	if ( strcmp(token, "NAK") == 0 ) {
	
		/* Difficult to analyse this in detail... */
		if ( session->messagewindow != NULL ) {
			if ( !messagewindow_get_last_was_nak(session->messagewindow) ) {
				messagewindow_addtext_system(session->messagewindow, "Connection problems at the other end?");
				messagewindow_set_last_was_nak(session->messagewindow, TRUE);
			}
		}
		
	}

	if ( strcmp(token, "217") == 0 ) {
	
		/* Hmmm. */
		if ( !session->ready ) {
			debug_print("SB %8p: 217 before READY. Leaving...\n", session);
			sbprotocol_leavesession(session);
		}
		
	}

	free(token);

}

/* Deal with an incoming user IM */
static void sbprotocol_parseim(SbSession *session, const char *msg, size_t length) {

	char *says_text;
	const char *friendlyname_coded;
	char *friendlyname;
	SbUser *user;
	char *format;
	char *colour = NULL;
	char *flipcolour;
	char *hashcolour = NULL;
	char *padcolour;
	size_t i;
	char *font = NULL;
	
	if ( session->messagewindow == NULL ) {
		messagewindow_mitigate(session);
	}
	
	friendlyname_coded = contactlist_friendlyname(session->msg_source);
	friendlyname = routines_urldecode(friendlyname_coded);
	says_text = malloc(strlen(friendlyname) + 7);
	strcpy(says_text, friendlyname);
	free(friendlyname);
	strcat(says_text, " says:");
	
	if ( session->messagewindow->ofontoverride ) {
		hashcolour = strdup(session->messagewindow->ocolour_string);
		font = session->messagewindow->ofont;
	} else {
		format = mime_getfield(msg, "X-MMS-IM-Format");
		debug_print("SB %8p: Got format: '%s'\n", session, format);
		if ( strlen(format) > 0 ) {
		
			colour = strdup("000000");
			for ( i=0; i<strlen(format); i++ ) {
			
				if ( (format[i]=='C') && (format[i+1]=='O') && (format[i+2]=='=') ) {
				
					char c;
					size_t j = 0;
		
					free(colour);
					colour = malloc(7);
					c = format[i+3+j];
					while ( (j<7) && (c != ';') && (c != ' ') && (i+3+j < strlen(format)-1) ) {
						colour[j] = c;
						j++;
						c = format[i+3+j];
					}
					colour[j] = '\0';
					break;
					
				}
				
			}
			free(format);
			assert(colour != NULL);
			debug_print("SB %8p: Got colour '%s'\n", session, colour);
			
			if ( strlen(colour) < 6 ) {
			
				size_t len = strlen(colour);
				padcolour = strdup("000000");
				padcolour[6-len] = '\0';
				strcat(padcolour, colour);
				free(colour);
				colour = padcolour;
				debug_print("SB %8p: Padded: '%s'\n", session, colour);
				
			}
			
			flipcolour = routines_flipcolour(colour);
			hashcolour = malloc(8);
			strcpy(hashcolour, "#");
			strncat(hashcolour, flipcolour, 6);
			hashcolour[7] = '\0';
			free(flipcolour);
			free(colour);
			debug_print("SB %8p: Flipped and hashed: '%s'\n", session, hashcolour);
		}

	}
		
	messagewindow_addtext_system(session->messagewindow, says_text);
	free(says_text);
	messagewindow_addtext_user_nonewline(session->messagewindow, "\n", 1, NULL, NULL);
	messagewindow_addtext_user_nonewline(session->messagewindow, mime_getbody(msg), length-mime_headerlength(msg), hashcolour, font);
	free(hashcolour);
	
	user = sbsessions_find_username(session, session->msg_source);
	if ( user == NULL ) {
		debug_print("SB %8p: IM source user not found on switchboard!\n", session);
		return;
	}
	messagewindow_stoptypingbyusername(session->messagewindow, session->msg_source);

}

/* Handle an "MSMsgsControl" */
static void sbprotocol_parsemsgcontrol(SbSession *session, const char *msg) {

	char *typinguser;

	/* Check for a typing user.*/
	typinguser = mime_getfield(msg, "TypingUser");
	if ( strlen(typinguser) > 0 ) {

		if ( session->messagewindow == NULL ) {
			messagewindow_mitigate(session);
		}
		messagewindow_starttyping(session->messagewindow, typinguser);

	} else {
		debug_print("SB %8p: Unrecognised TypingUser control!\n", session);
	}
			
	free(typinguser);

}

static void sbprotocol_handleclientcaps(SbSession *session, const char *msg) {

	debug_print("SB %8p: clientcaps: '%s'\n", session, msg);

}

static void sbprotocol_handledatacast(SbSession *session, const char *username, const char *data) {

	int id;
	char *id_string = mime_getfield(data, "ID");
	
	if ( id_string == NULL ) {
		debug_print("SB %8p: Got datacast, couldn't find ID.\n", session);
		return;
	}
	
	id = atoi(id_string);
	free(id_string);

	debug_print("SB %8p: Got datacast: ID %i.\n", session, id);
	
	if ( id == 1 ) {
	
		const char *friendlyname;
		char *friendlyname_decoded;
		char *text;
		
		friendlyname = contactlist_friendlyname(username);
		friendlyname_decoded = routines_urldecode(friendlyname);
		
		text = malloc(strlen(friendlyname_decoded)+9);
		strcpy(text, friendlyname_decoded);
		strcat(text, " nudges.");
		free(friendlyname_decoded);
	
		if ( session->messagewindow == NULL ) {
			messagewindow_mitigate(session);
		}
		messagewindow_addtext_system(session->messagewindow, text);
		free(text);
		
	}

}


static void sbprotocol_handleink_isf(SbSession *session, const char *username, const char *ink) {

	debug_print("SB %8p: Got ISF: '%s'\n", session, ink);

}

static MultiPacketMsg *sbprotocol_findmultipacket(SbSession *session, const char *message_id) {

	MultiPacketMsg *msg = session->multipackets;
	
	while ( msg != NULL ) {
		if ( strcmp(msg->message_id, message_id) == 0 ) {
			return msg;
		}
		msg = msg->next;
	}
	
	return NULL;

}

/* Currently, this assumes the packets will arrive in the correct order. */
static int sbprotocol_checkmultipacket(SbSession *session, const char *msgdata, ssize_t msgdatalength) {

	char *chunks;
	char *chunk;

	chunks = mime_getfield(msgdata, "Chunks");
	chunk = mime_getfield(msgdata, "Chunk");
	if ( (strlen(chunks)>0) || (strlen(chunk)>0) ) {
	
		/* This is part of a multipacketed message. */
		MultiPacketMsg *msg;
		char *messageid;

		debug_print("SB %8p: Got a part of a multipacketed message.\n", session);
				
		messageid = mime_getfield(msgdata, "Message-ID");
		if ( strlen(messageid) == 0 ) {
			debug_print("SB %8p: No Message-ID!\n", session);
			free(messageid);
			free(chunks);
			free(chunk);
			return 0;
		}
		
		if ( (atoi(chunk) == 0) && (atoi(chunks) == 0) ) {
			debug_print("SB %8p: Couldn't find either a sensible 'Chunk' or 'Chunks' value!\n", session);
			free(messageid);
			free(chunks);
			free(chunk);
			return 0;
		}
	
		msg = sbprotocol_findmultipacket(session, messageid);
		if ( msg == NULL ) {
		
			/* No previous record of this series, so it had *better* contain the number of chunks to expect... */
			if ( atoi(chunks) == 0 ) {
				debug_print("SB %8p: Couldn't find the length of the multipacket series...\n", session);
				free(messageid);
				free(chunks);
				free(chunk);
				return 0;
			}

			msg = malloc(sizeof(MultiPacketMsg));
			msg->chunks_expected = atoi(chunks);
			msg->chunks_received = 1;
			msg->data = mime_removeheader(msgdata, msgdatalength, "Chunks");
			msg->datalength = msgdatalength - strlen(chunks) - strlen("Chunks: rn");
			msg->message_id = messageid;
			
			/* Link it into the list for this session. */
			msg->next = session->multipackets;
			msg->previous = NULL;
			if ( session->multipackets != NULL ) {
				session->multipackets->previous = msg;
			}
			session->multipackets = msg;
		
		} else {

			size_t body_size;
			const char *body;
			
			/* Known series, so this had *better* contain a chunk ID... */
			if ( atoi(chunk) == 0 ) {
				debug_print("SB %8p: Couldn't find the multipacket chunk ID...\n", session);
				return 0;
			}
			
			body = mime_getbody(msgdata);
			body_size = msgdatalength - mime_headerlength(msgdata);
			
			msg->data = realloc(msg->data, body_size+(msg->datalength));
			memcpy((msg->data)+msg->datalength, body, body_size);
			msg->datalength += body_size;
			
			msg->chunks_received++;
			if ( msg->chunks_received == msg->chunks_expected ) {
			
				debug_print("SB %8p: Got all parts of a multipacket message...\n", session);
				
				/* Extend buffer by one byte and fix \0 terminator. */
				msg->data = realloc(msg->data, (msg->datalength)+1);
				msg->data[msg->datalength] = '\0';
				
				sbprotocol_parsemsg(session, msg->data, msg->datalength);
				
				/* Remove from list */
				if ( msg->previous != NULL ) {
					msg->previous->next = msg->next;
				} else {
					session->multipackets = msg->next;
				}
				if ( msg->next != NULL ) {
					msg->next->previous = msg->previous;
				}

				free(msg->data);
				free(msg);
				
				return 1;	/* It's already been handled. */
				
			}

		}
	
	} else {
	
		free(chunks);
		free(chunk);
		return 0;
	
	}

	free(chunks);
	free(chunk);
	
	return 1;	/* Cause sbprotocol_parsemsg to ignore this one for now. */

}

/* Decide what to do with a received message.
	Also called by msnp2p.c to throw us a MIME message sent via MSNP2P (Yuk!). */
void sbprotocol_parsemsg(SbSession *session, const char *msg, ssize_t msglength) {

	char *content_type;
	
	debug_print("SB %8p: MSG (%i bytes): '%s'\n", session, msglength, msg);

	if ( sbprotocol_checkmultipacket(session, msg, msglength) ) {
		/* Soon, my pretties... */
		return;
	}

	content_type = mime_getfield(msg, "Content-Type");
	
	if ( strstr(content_type, "text/plain") != NULL ) {
		sbprotocol_parseim(session, msg, msglength);
	} else if ( strstr(content_type, "text/x-msmsgscontrol") != NULL ) {
		sbprotocol_parsemsgcontrol(session, msg);
	} else if ( strstr(content_type, "application/x-msnmsgrp2p") != NULL ) {
		msnp2p_parsemsg(session, session->msg_source, msg, session->expect_length);
	} else if ( strstr(content_type, "text/x-msmsgsinvite") != NULL ) {
		msninvite_parsemsg(session, msg);
	} else if ( strstr(content_type, "text/x-clientcaps") != NULL ) {
		sbprotocol_handleclientcaps(session, msg);
	} else if ( strstr(content_type, "application/x-ms-ink") != NULL ) {
		sbprotocol_handleink_isf(session, session->msg_source, mime_getbody(msg));
	} else if ( strstr(content_type, "text/x-msnmsgr-datacast") != NULL ) {
		sbprotocol_handledatacast(session, session->msg_source, mime_getbody(msg));
	} else {
		debug_print("SB %8p: No handler for this MSG (%s).\n", session, content_type);
	}
	
	free(content_type);
	free(session->msg_source);	/* Can safely be freed even if routines_lindex didn't find anything earlier. */

}

/* Internally called when data arrives from the SB */
static void sbprotocol_readable(SbSession *session) {

	unsigned int i = 0;	/* =0 keeps compiler happy */
	ssize_t rlen;
	int no_string = 0;

	assert(session->rbufsize - session->roffset > 0);
	rlen = read(session->socket, session->rbuffer + session->roffset, session->rbufsize - session->roffset);
	if ( rlen > 0 ) {
		session->roffset += rlen;
	}
	assert(session->roffset <= session->rbufsize);	/* This would indicate a buffer overrun */

	/* First, check this isn't a disconnection. rlen=0 is EOF, rlen=-1 is an error. */
	if ( (rlen == 0) || (rlen == -1) ) {

		int closeval;
		
		closeval = close(session->socket);
		
		if ( closeval != 0 ) {
			debug_print("SB %8p: Couldn't close socket after read error.\n", session);
		}
		
		sbprotocol_disconnected(session);		
		return;
	
	}
	
	while ( (!no_string) && (session->roffset > 0)  ) {
	
		int block_ready = 0;

		/* See if there's a full "block" in the buffer yet */
		if ( session->conmode == SBCONMODE_LINE ) {
		
			for ( i=0; i<session->roffset-1; i++ ) {	/* Means the last value looked at is roffset-2 */
				if ( (session->rbuffer[i] == '\r') && (session->rbuffer[i+1] == '\n') ) { 
					block_ready = 1;
					break;
				}
			}
			
		} else if ( session->conmode == SBCONMODE_MSG ) {
		
			if ( session->roffset >= session->expect_length ) {
				i = session->expect_length - 2;
				block_ready = 1;
			}
		
		} else {
			/* "Never happens". */
			debug_print("SB %8p: Can't determine end of block for SbConMode %i!\n", session, session->conmode);
		}
		
		if ( block_ready == 1 ) {
		
			char *block_buffer = NULL;
			unsigned int new_rbufsize;
			unsigned int endbit_length;
			SbConMode next_mode = session->conmode;
			
			if ( session->conmode == SBCONMODE_LINE ) {
				assert(session->rbuffer[i] == '\r');
				assert(session->rbuffer[i+1] == '\n');
			}
			
			
			if ( session->conmode == SBCONMODE_LINE ) {
			
				block_buffer = malloc(i+1);
				memcpy(block_buffer, session->rbuffer, i);
				block_buffer[i] = '\0';

				sbprotocol_parseline(session, block_buffer);
				
				/* Check to see if sbprotocol_parseline changed the mode.  If so, make sure
					it stays changed. */
				if ( session->conmode != SBCONMODE_LINE ) {
					next_mode = session->conmode;
				}
				
			} else if ( session->conmode == SBCONMODE_MSG ) {
			
				block_buffer = malloc(i+3);	/* Exactly the number of bytes we were told to expect, plus one for terminator. */
				memcpy(block_buffer, session->rbuffer, i+2);	/* Copy it in */
				block_buffer[i+2] = '\0';	/* Terminate */
				
				sbprotocol_parsemsg(session, block_buffer, i+2);
				next_mode = SBCONMODE_LINE;
				
			} else {
				debug_print("SB %8p: No handler for SbConMode %i !\n", session, session->conmode);
			}
			
			free(block_buffer);
			
			/* Now the block's been parsed, it should be forgotten about */
			if ( session->conmode == SBCONMODE_LINE ) {
				assert(session->rbuffer[i+1] == '\n');	/* Should still be the case.  Paranoid. */
			}
			endbit_length = i+2;
			memmove(session->rbuffer, session->rbuffer + endbit_length, session->rbufsize - endbit_length);
			session->roffset = session->roffset - endbit_length;	/* Subtract the number of bytes removed */
			new_rbufsize = session->rbufsize - endbit_length;
			if ( new_rbufsize == 0 ) {
				new_rbufsize = 32;
			}
			session->rbuffer = realloc(session->rbuffer, new_rbufsize);
			session->rbufsize = new_rbufsize;
			session->conmode = next_mode;
			
		} else {

			if ( session->roffset == session->rbufsize ) {
		
				/* More buffer space is needed */
				session->rbuffer = realloc(session->rbuffer, session->rbufsize + 32);
				session->rbufsize = session->rbufsize + 32;
				/* The new space gets used at the next read, shortly... */
		
			}
			no_string = 1;
			
		}

	}

}

/* Internally called when the socket is connected */
static void sbprotocol_ready(SbSession *session) {

	/* Remove the "writeable" callback, and add a "readable" callback */
	gdk_input_remove(session->wcallback);
	session->wcallback = 0;
	session->rcallback = gdk_input_add(session->socket, GDK_INPUT_READ, (GdkInputFunction)sbprotocol_readable, session);

	/* Begin the handshake */
	if ( session->source == SESSION_SOURCE_REMOTE ) {
	
		char *args;
		const char *username;
		
		username = options_username();
		args = malloc(strlen(username) + strlen(session->cki_key) + strlen(session->sessionid) +3);
		
		assert(username != NULL);
		assert(args != NULL);
		assert(session->cki_key != NULL);
		assert(session->sessionid != NULL);
		
		strcpy(args, username);
		strcat(args, " ");
		strcat(args, session->cki_key);
		strcat(args, " ");
		strcat(args, session->sessionid);
		free(session->cki_key);
		free(session->sessionid);
		
		sbprotocol_sendtr(session, "ANS", args);
		free(args);
		
	} else {
	
		char *args;
		const char *username;
		
		username = options_username();
		args = malloc(strlen(username) + strlen(session->cki_key) + 2);
		
		assert(username != NULL);
		assert(args != NULL);
		assert(session->cki_key != NULL);
		
		strcpy(args, username);
		strcat(args, " ");
		strcat(args, session->cki_key);
		free(session->cki_key);
		
		sbprotocol_sendtr(session, "USR", args);
		free(args);
		
	}

}

static void sbprotocol_connect(SbSession *session, const char *hostname, unsigned short int port) {

	struct sockaddr_in sa_desc;
	struct hostent *server;
	unsigned int sockopts;
	int conn_error;

	assert(hostname != NULL);
	if ( port == 0 ) {
		port = DEFAULT_SB_PORT;
		debug_print("SB %8p: Fixed obviously wrong port number to %i\n", session, port);
	}
	debug_print("SB %8p: Connecting to '%s' port %i\n", session, hostname, port);

	/* Create and configure the socket (but don't connect it yet) */
	session->socket = socket(PF_INET, SOCK_STREAM, 0);
	if ( session->socket == -1 ) {
		error_report("Couldn't create switchboard socket");
		return;
	}
	sockopts = fcntl(session->socket, F_GETFL);
	fcntl(session->socket, F_SETFL, sockopts | O_NONBLOCK);
	
	/* Resolve the server name */
	server = gethostbyname(hostname);
	if ( server == NULL ) {
		error_report("Couldn't resolve switchboard hostname.");
		return;
	}
	
	memset(&sa_desc, 0, sizeof(sa_desc));
	sa_desc.sin_family = AF_INET;
	memcpy(&sa_desc.sin_addr.s_addr, server->h_addr, server->h_length);
	sa_desc.sin_port = htons(port);
	
	session->rbuffer = malloc(256);
	assert(session->rbuffer != NULL);
	session->rbufsize = 256;
	session->roffset = 0;
	session->wbufsize = 0;
	session->wbuffer = NULL;
	session->woffset = 0;
		
	conn_error = connect(session->socket, (struct sockaddr *)&sa_desc, sizeof(sa_desc))<0;
	if ( (conn_error < 0) && (conn_error != EINPROGRESS) ) {
	
		debug_print("SB %8p: Couldn't connect to server", session);
		error_report("Couldn't connect to switchboard.");
		return;
		
	}
	
	/* Call back when the socket is connected */
	session->wcallback = gdk_input_add(session->socket, GDK_INPUT_WRITE, (GdkInputFunction)sbprotocol_ready, session);

	session->trid = 1;
	session->conmode = SBCONMODE_LINE;
	session->expect_length = 0;

}

/* Called from src/msnprotocol.c when the XFR SB information is ready. */
void sbprotocol_initiate_local(unsigned int trid, const char *hostname, unsigned int port, const char *cki_key) {

	SbSession *session;
	
	session = sbsessions_find_trid(trid);
	if ( session == NULL ) {
		debug_print("SB %8p: Couldn't find session! Aborting.\n", NULL);
		return;
	}
	
	assert(session->users != NULL);
	assert(session->users->username != NULL);
	debug_print("SB %8p: Matched to user %s\n", session, session->users->username);
	session->neg_trid = 0;	/* Stop this session from being re-matched. */
	
	session->cki_key = strdup(cki_key);
	sbprotocol_connect(session, hostname, port);

}

void sbprotocol_initiate_remote(SbSession *session, const char *switchboardaddress, const char *sessionid, const char *authchallenge) {

	char *hostname;
	unsigned int port;

	session->cki_key = strdup(authchallenge);
	session->sessionid = strdup(sessionid);
	
	hostname = routines_hostname(switchboardaddress);
	port = routines_port(switchboardaddress);
	
	sbprotocol_connect(session, hostname, port);
	
	free(hostname);

}

void sbprotocol_send(SbSession *session, char *textblock, int length) {

	if ( strlen(textblock) != 0 ) {
	
		char *user_header;
				
		session->am_typing = 0;
		if ( session->am_typing_callback != 0 ) {
			gtk_timeout_remove(session->am_typing_callback);
		}
		
		/* Send "User-Agent" and text style string with normal messages. */
		user_header = malloc(256 + strlen(tuxmessenger_versionstring()));
		strcpy(user_header, "User-Agent: ");
		strcat(user_header, tuxmessenger_versionstring());
		
		if ( session->messagewindow->localfont != NULL ) {
			strcat(user_header, "\r\nX-MMS-IM-Format: ");
			strcat(user_header, session->messagewindow->localfont);
			if ( session->messagewindow->localcolour_string != NULL ) {
				strcat(user_header, "; CO=");
				strcat(user_header, session->messagewindow->localcolour_string);
			}
		} else {
			if ( session->messagewindow->localcolour_string != NULL ) {
				strcat(user_header, "\r\nX-MMS-IM-Format: CO=");
				strcat(user_header, session->messagewindow->localcolour_string);
			}
		}				
		
		sbprotocol_sendmsg(session, "text/plain; charset=UTF-8", user_header, textblock, strlen(textblock));
		
		free(user_header);			
		return;
			
	} else {

		debug_print("SB %8p: Not sending empty message.\n", session);
		return;
	
	}

}

void sbprotocol_sendnudge(SbSession *session) {
	sbprotocol_sendmsg(session, "text/x-msnmsgr-datacast", NULL, "ID: 1\r\n\r\n", 9);
}

void sbprotocol_sendtypingcontrol(SbSession *session) {

	char *control_header;
	
	if ( session->ready != FALSE ) {
	
		control_header = malloc(strlen(options_username())+16);
		strcpy(control_header, "TypingUser: ");
		strcat(control_header, options_username());
		sbprotocol_sendmsg(session, "text/x-msmsgscontrol", control_header, "", 0);
		free(control_header);

	} else {
	
		debug_print("SB %8p: Not sending TypingUser control before session is ready.\n", session);
	
	}

}

void sbprotocol_close(SbSession *session) {
	close(session->socket);
}