aboutsummaryrefslogtreecommitdiff
path: root/src/options.c
blob: 65dd08d62a0bea9cd1d6e7acab78c52db2f13868 (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
/*
 * options.c
 *
 * Low-level option handling
 *
 * (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 <string.h>
#include <glob.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

#include "debug.h"
#include "accountwindow.h"
#include "options.h"
#include "msngenerics.h"
#include "mainwindow.h"
#include "error.h"
#include "routines.h"
#include "listcache.h"

#define DEFAULT_HOSTNAME "messenger.hotmail.com"
#define DEFAULT_WGET "/usr/bin/wget"

static struct {

	char *username;
	char *password;
	unsigned int rememberlogindetails;
		
	char *hostname;
	unsigned int port;
	
	char *wget;
	
	char *gtc;			/* Special - comes from list cache. */
	char *blp;			/* Special - comes from list cache. */
	
	unsigned int hideoffline;	/* Hide offline contacts? */
	unsigned int hidecsm;		/* Hide local CSM? */
	
	GdkColor *localcolour_gdk;	/* Colour for local user's messages. */
	char *localcolour_string;	/* String version of the above. */
	unsigned int ofontoverride;		/* Override other contacts' chosen fonts/colours? */
	GdkColor *ocolour_gdk;		/* Colour to override contacts' messages with. */
	char *localfont;		/* Font for local user's messages. */
	char *ofont;			/* Font to use for contacts' messages (if overriding). */

	unsigned int ircstyle;		/* Use IRC style for new IM windows? */
	unsigned int timestamps;	/* Show timestamps for messages in new IM windows? */
	unsigned int showavatars;	/* Display avatars in new IM windows? */
	unsigned int showemoticons;	/* Show emoticons in new IM windows? */

} options = {

	NULL,	/* Username */
	NULL,	/* Password */
	0,	/* Remember login details */
	
	NULL,	/* Hostname */
	0,	/* Port */
	
	NULL,	/* Wget */
	
	NULL,	/* GTC */
	NULL,	/* BLP */
	
	FALSE,	/* Hide offline */
	FALSE,	/* Hide CSM */
	
	NULL,	/* localcolour_gdk */
	NULL,	/* localcolour_string */
	FALSE,	/* ofontoverride */
	NULL,	/* ocolour_gdk */
	NULL,	/* localfont */
	NULL,	/* ofont */
	
	0,	/* ircstyle */
	0,	/* timestamps */
	0,	/* showavatars */
	0	/* showemoticons */

};

static void options_setdefaults() {

	options.username = strdup("");
	options.password = strdup("");
	options.rememberlogindetails = 0;
	
	options.hostname = strdup(DEFAULT_HOSTNAME);
	options.port = DEFAULT_NS_PORT;
	
	options.wget = strdup(DEFAULT_WGET);
	
	options.gtc = strdup("BL");
	options.blp = strdup("A");
	
	options.hideoffline = FALSE;
	options.hidecsm = FALSE;
	options.ofontoverride = FALSE;

	options.localcolour_gdk = NULL;
	options.localcolour_string = NULL;
	options.ocolour_gdk = NULL;
	options.localfont = NULL;
	options.ofont = NULL;
	
	options.ircstyle = 0;
	options.timestamps = 0;
	options.showavatars = 0;
	options.showemoticons = 0;
	
}

static void options_createhomedir(char *filename) {

	char *av_name;
	char *hg_name;

	/* Create directory. */
	if ( mkdir(filename, S_IRUSR | S_IWUSR | S_IXUSR) != 0 ) {
		debug_print("OP: Couldn't create ~/.tuxmessenger directory.\n");
		exit(1);
	}
	
	/* Create "avatars" subdirectory. */
	av_name = malloc(strlen(filename) + 9);
	strcpy(av_name, filename);
	strcat(av_name, "/avatars");
	if ( mkdir(av_name, S_IRUSR | S_IWUSR | S_IXUSR) != 0 ) {
		debug_print("OP: Couldn't create ~/.tuxmessenger/avatars directory.\n");
		exit(1);
	}
	free(av_name);
	
	/* Link the "Hourglass", "Default" and "Own" avatars. */
	hg_name = malloc(strlen(filename) + 20);

	strcpy(hg_name, filename);
	strcat(hg_name, "/wait_avatar.png");
	if ( symlink(DATADIR"/tuxmessenger/hourglass.png", hg_name) != 0 ) {
		debug_print("OP: Couldn't create ~/.tuxmessenger/wait_avatar.png.\n");
		exit(1);
	}

	strcpy(hg_name, filename);
	strcat(hg_name, "/default_avatar.png");
	if ( symlink(DATADIR"/tuxmessenger/no_avatar.png", hg_name) != 0 ) {
		debug_print("OP: Couldn't create ~/.tuxmessenger/default_avatar.png.\n");
		exit(1);
	}

	strcpy(hg_name, filename);
	strcat(hg_name, "/avatar.png");
	if ( symlink(DATADIR"/tuxmessenger/no_avatar.png", hg_name) != 0 ) {
		debug_print("OP: Couldn't create ~/.tuxmessenger/avatar.png.\n");
		exit(1);
	}

	free(hg_name);

}

static void options_bailout() {

	options_setdefaults();
	accountwindow_open();

}

void options_setusername(const char *username) {

	unsigned int changed = FALSE;

	if ( (username != NULL) && (options.username != NULL) && (strcmp(options.username, "") != 0) && (strcmp(username, options.username) != 0) ) {
		changed = TRUE;		
	}

	if ( options.username != NULL ) {
		free(options.username);
	}
	options.username = strdup(username);
	
	if ( changed ) {
		listcache_setcsm("");
		listcache_invalidate();
	}
	
}

void options_setpassword(const char *password) {

	if ( options.password != NULL ) {
		free(options.password);
	}
	options.password = strdup(password);
	
}

void options_sethostname(const char *hostname) {

	if ( options.hostname != NULL ) {
		free(options.hostname);
	}
	if ( strlen(hostname) > 0 ) {
		options.hostname = strdup(hostname);
	} else {
		debug_print("OP: Using default server hostname instead of zero-length string.\n");
		options.hostname = strdup(DEFAULT_HOSTNAME);
	}
	
}

void options_setport(const int port) {

	options.port = port;
	if ( options.port == 0 ) {
		/* That's just plain silly. */
		debug_print("OP: Correcting port number: 0->%i.\n", DEFAULT_NS_PORT);
		options_setport(DEFAULT_NS_PORT);
	}
	if ( options.port > 65535 ) {
		/* D'oh */
		debug_print("OP: Port number too large: correcting to %i\n", DEFAULT_NS_PORT);
		options_setport(DEFAULT_NS_PORT);
	}
	
}

void options_setwget(const char *wget) {

	if ( options.wget != NULL ) {
		free(options.wget);
	}
	if ( strlen(wget) > 0 ) {
		options.wget = strdup(wget);
	} else {
		debug_print("OP: Using default wget instead of zero-length string.\n");
		options.wget = strdup(DEFAULT_WGET);
	}
	
}

void options_setrememberlogindetails(unsigned int remember) {
	options.rememberlogindetails = remember;
}

void options_sethideoffline(unsigned int hide) {
	options.hideoffline = hide;
}

void options_sethidecsm(unsigned int hide) {
	options.hidecsm = hide;
}

void options_setlocalcolour_gdk(const GdkColor *colour) {

	char *string;

	if ( options.localcolour_gdk != NULL ) {
		gdk_color_free(options.localcolour_gdk);
	}
	if ( options.localcolour_string != NULL ) {
		free(options.localcolour_string);
	}
	
	options.localcolour_gdk = gdk_color_copy(colour);
	
	/* Now work out the string version */
	string = malloc(7);
	/* Yukky BGR order instead of RGB */
	snprintf(string, 7, "%02hhx%02hhx%02hhx", colour->blue >> 8, colour->green >> 8, colour->red >> 8);
	options.localcolour_string = string;
	debug_print("OP: String value '%s'\n", string);

}

void options_setlocalcolour_string(const char *colour) {

	char *flipped;
	char *string;
	GdkColor gdkcolour;

	if ( options.localcolour_gdk != NULL ) {
		gdk_color_free(options.localcolour_gdk);
	}
	if ( options.localcolour_string != NULL ) {
		free(options.localcolour_string);
	}
	
	options.localcolour_string = strdup(colour);
	
	/* Now work out the GDK version - flip then parse */
	flipped = routines_flipcolour(colour);
	string = malloc(8);
	strcpy(string, "#");
	strncat(string, flipped, 7);
	string[7] = '\0';
	free(flipped);
	if ( gdk_color_parse(string, &gdkcolour) ) {
		options.localcolour_gdk = gdk_color_copy(&gdkcolour);
	} else {
	
		debug_print("OP: Whoops! Colour parsing failed (%s)...\n", string);
		if ( options.localcolour_gdk != NULL ) {
			gdk_color_free(options.localcolour_gdk);
		}
		if ( options.localcolour_string != NULL ) {
			free(options.localcolour_string);
		}
		options.localcolour_string = NULL;
		options.localcolour_gdk = NULL;
		
	}
	free(string);

}

void options_setocolour_gdk(const GdkColor *colour) {

	if ( options.ocolour_gdk != NULL ) {
		gdk_color_free(options.ocolour_gdk);
	}
	
	options.ocolour_gdk = gdk_color_copy(colour);
	
}

void options_setofontoverride(unsigned int override) {
	options.ofontoverride = override;
}

void options_setocolour_string(const char *colour) {

	char *string;
	GdkColor gdkcolour;

	if ( options.ocolour_gdk != NULL ) {
		gdk_color_free(options.localcolour_gdk);
	}

	string = malloc(8);
	strcpy(string, "#");
	strncat(string, colour, 7);
	string[7] = '\0';
	if ( gdk_color_parse(string, &gdkcolour) ) {
		options.ocolour_gdk = gdk_color_copy(&gdkcolour);
	} else {
		debug_print("OP: Whoops! Colour parsing failed (%s)...\n", string);
		options.localcolour_gdk = NULL;
	}
	free(string);

}

void options_setlocalfont(const char *font) {
	options.localfont = strdup(font);
}

void options_setofont(const char *font) {
	options.ofont = strdup(font);
}

void options_setircstyle(unsigned int ircstyle) {
	options.ircstyle = ircstyle;
}

void options_settimestamps(unsigned int timestamps) {
	options.timestamps = timestamps;
}

void options_setshowavatars(unsigned int showavatars) {
	options.showavatars = showavatars;
}

void options_setshowemoticons(unsigned int showemoticons) {
	options.showemoticons = showemoticons;
}

void options_load() {

	int glob_retval;
	glob_t glob_result;
	struct stat stat_buffer;
	struct stat *statbuf;
	char *dir_filename;
	char *rc_filename;
	
	glob_retval = glob("~", GLOB_TILDE, NULL, &glob_result);
	statbuf = &stat_buffer;
	if ( glob_retval != 0 ) {

		debug_print("OP: glob() for ~/.tuxmessenger failed: ");
		switch ( glob_retval ) {
			case GLOB_NOSPACE : debug_print("GLOB_NOSPACE\n"); break;
			case GLOB_ABORTED : debug_print("GLOB_ABORTED\n"); break;
			case GLOB_NOMATCH : debug_print("GLOB_NOMATCH\n"); break;
			default	: debug_print("Unknown!\n"); break;
		}
		debug_print("OP: Can't continue :(\n");
		exit(1);

	}
	
	dir_filename = malloc(strlen(glob_result.gl_pathv[0]) + 15);
	strcpy(dir_filename, glob_result.gl_pathv[0]);
	globfree(&glob_result);
	strcat(dir_filename, "/.tuxmessenger");
	
	if ( stat(dir_filename, statbuf) != -1 ) {
	
		if ( S_ISDIR(stat_buffer.st_mode) ) {
	
			debug_print("OP: Found '%s'.\n", dir_filename);
			/* All is good. */

		} else {
		
			debug_print("OP: Found '%s', but it isn't a directory!\n", dir_filename);
			debug_print("OP: Can't continue :(\n");
			exit(1);
			
		}
		
	} else {
	
		debug_print("OP: ~/.tuxmessenger directory not found: creating it.\n");
		options_createhomedir(dir_filename);
		
	}

	/* Right - after all of that, let's take a look at the central config file. */
	options_setdefaults();	/* Set defaults to begin with. */
	rc_filename = malloc(strlen(dir_filename) + 8);
	strcpy(rc_filename, dir_filename);
	strcat(rc_filename, "/config");
	if ( stat(rc_filename, statbuf) != -1 ) {
	
		if ( (S_ISREG(stat_buffer.st_mode)) || (S_ISLNK(stat_buffer.st_mode)) ) {
		
			/* Yay :D */
			FILE *fh;
			char *line;
			char *rval;
			int whoops = 0;

			fh = fopen(rc_filename, "r");
			if ( fh == NULL ) {			
				debug_print("OP: Error opening options file.\n");
				options_bailout();
				free(rc_filename);
				free(dir_filename);
				return;
			}
			
			line = malloc(1024);
			assert(line != NULL);
			rval = "hello"; /* Non-NULL */
	
			while ( rval ) {

				rval = fgets(line, 1023, fh);
				
				if ( ferror(fh) && !feof(fh) ) {
					whoops = 1;
					break;
				}
				if ( strlen(line) > 1 ) {
					if ( line[strlen(line)-1] == '\n' ) {
						line[strlen(line)-1] = '\0';	/* Cut off trailing newline. */
					}
				} else {
					line[0] = '\0';
				}
				
				if ( strncmp(line, "username ", 9) == 0 ) {
					debug_print("OP: Got username from config file.\n");
					options_setusername(line+9);
				}
				if ( strncmp(line, "password ", 9) == 0 ) {
					debug_print("OP: Got password from config file.\n");
					options_setpassword(line+9);
				}
				if ( strncmp(line, "hostname ", 9) == 0 ) {
					debug_print("OP: Got server hostname from config file.\n");
					options_sethostname(line+9);
				}
				if ( strncmp(line, "port ", 5) == 0 ) {
					debug_print("OP: Got server port number from config file.\n");
					options_setport(atoi(line+5));
				}
				if ( strncmp(line, "wget ", 5) == 0 ) {
					debug_print("OP: Got wget command from config file.\n");
					options_setwget(line+5);
				}
				if ( strncmp(line, "hide_offline", 12) == 0 ) {
					debug_print("OP: Got hide_offline from config file.\n");
					options_sethideoffline(TRUE);
				}
				if ( strncmp(line, "hide_localcsm", 12) == 0 ) {
					debug_print("OP: Got hide_localcsm from config file.\n");
					options_sethidecsm(TRUE);
				}
				if ( strncmp(line, "localcolour ", 12) == 0 ) {
					debug_print("OP: Got local colour from config file.\n");
					options_setlocalcolour_string(line+12);
				}
				if ( strncmp(line, "ofontoverride", 12) == 0 ) {
					debug_print("OP: Got ofontoverride from config file.\n");
					options_setofontoverride(TRUE);
				}
				if ( strncmp(line, "ocolour ", 8) == 0 ) {
					debug_print("OP: Got override colour from config file.\n");
					options_setocolour_string(line+8);
				}
				if ( strncmp(line, "localfont ", 10) == 0 ) {
					debug_print("OP: Got localfont from config file.\n");
					options_setlocalfont(line+10);
				}
				if ( strncmp(line, "ofont ", 6) == 0 ) {
					debug_print("OP: Got ofont from config file.\n");
					options_setofont(line+6);
				}
				if ( strncmp(line, "show_avatars", 12) == 0 ) {
					debug_print("OP: Got show_avatars from config file.\n");
					options_setshowavatars(TRUE);
				}
				if ( strncmp(line, "show_emoticons", 14) == 0 ) {
					debug_print("OP: Got show_emoticons from config file.\n");
					options_setshowemoticons(TRUE);
				}
				if ( strncmp(line, "timestamps", 10) == 0 ) {
					debug_print("OP: Got timestamps from config file.\n");
					options_settimestamps(TRUE);
				}
				if ( strncmp(line, "ircstyle", 8) == 0 ) {
					debug_print("OP: Got ircstyle from config file.\n");
					options_setircstyle(TRUE);
				}
				
				line[0] = '\0';	/* Prevent this line from being parsed twice. */
				
			}		
			
			free(line);			
			fclose(fh);
			
			if ( whoops ) {
				debug_print("OP: Error reading options file.\n");
				options_bailout();				
			}
		
		} else {
		
			/* FFS.  I'm not handling that. */
			debug_print("OP: Options file exists but isn't a file!\n");
			exit(1);
			
		}
		
	} else {
	
		/* Options file isn't there.  Set "sensible defaults" and open the config window... */
		debug_print("OP: Options file not found.\n");
		options_bailout();
	
	}
	
	options_setrememberlogindetails(1);
	/* If the username and password are blank, and the server is set to its default, we assume "Remember login details" was unticked last time. */
	if ( (strlen(options_username())==0) && (strlen(options_password())==0) && (options_port()==DEFAULT_NS_PORT) && (strcmp(options_hostname(), DEFAULT_HOSTNAME)==0)) {
		options_setrememberlogindetails(0);
	}

	free(dir_filename);
	free(rc_filename);
	
}

const char *options_username() {

	/* FIXME: Check length */
	/* Don't forget to change to lower case. */
	
	return options.username;

}

const char *options_password() {
	return options.password;
}

const char *options_hostname() {
	return options.hostname;
}

unsigned short int options_port() {
	return options.port;
}

const char *options_wget() {
	return options.wget;
}

const char *options_blp() {
	return options.blp;
}

const char *options_gtc() {
	return options.gtc;
}

unsigned int options_rememberlogindetails() {
	return options.rememberlogindetails;
}

unsigned int options_hideoffline() {
	return options.hideoffline;
}

unsigned int options_hidecsm() {
	return options.hidecsm;
}

const GdkColor *options_localcolour_gdk() {
	return options.localcolour_gdk;
}

const char *options_localcolour_string() {
	return options.localcolour_string;
}

unsigned int options_ofontoverride() {
	return options.ofontoverride;
}

const GdkColor *options_ocolour_gdk() {
	return options.ocolour_gdk;
}

unsigned int options_ircstyle() {
	return options.ircstyle;
}

unsigned int options_timestamps() {
	return options.timestamps;
}

unsigned int options_showavatars() {
	return options.showavatars;
}

unsigned int options_showemoticons() {
	return options.showemoticons;
}

static char *options_ocolour_string() {

	char *string;
	const GdkColor *colour = options_ocolour_gdk();
	
	if ( colour == NULL ) {
		return NULL;
	}
	
	string = malloc(7);
	/* RGB order here */
	snprintf(string, 7, "%02hhx%02hhx%02hhx", colour->red >> 8, colour->green >> 8, colour->blue >> 8);

	return string;

}

const char *options_localfont() {
	return options.localfont;
}

const char *options_ofont() {
	return options.ofont;
}

void options_save() {

	FILE *fh;
	int glob_retval;
	glob_t glob_result;
	struct stat stat_buffer;
	struct stat *statbuf;
	char *dir_filename;
	char *rc_filename;
	int whoops = 0;
	char *newline;
	
	const char *ousername;
	const char *opassword;
	const char *ohostname;
	int oport;
	
	glob_retval = glob("~", GLOB_TILDE, NULL, &glob_result);
	statbuf = &stat_buffer;
	if ( glob_retval != 0 ) {

		debug_print("OP: glob() for ~/.tuxmessenger failed: ");
		switch ( glob_retval ) {
			case GLOB_NOSPACE : debug_print("GLOB_NOSPACE\n"); break;
			case GLOB_ABORTED : debug_print("GLOB_ABORTED\n"); break;
			case GLOB_NOMATCH : debug_print("GLOB_NOMATCH\n"); break;
			default	: debug_print("Unknown!\n"); break;
		}
		debug_print("OP: Couldn't save options :(\n");
		return;

	}
	
	dir_filename = malloc(strlen(glob_result.gl_pathv[0]) + 15);
	strcpy(dir_filename, glob_result.gl_pathv[0]);
	globfree(&glob_result);
	strcat(dir_filename, "/.tuxmessenger");
	
	if ( stat(dir_filename, statbuf) != -1 ) {
	
		if ( S_ISDIR(stat_buffer.st_mode) ) {
	
			debug_print("OP: Found '%s'.\n", dir_filename);
			/* All is good. */

		} else {
		
			debug_print("OP: Found '%s', but it isn't a directory!\n", dir_filename);
			debug_print("OP: Couldn't save options :(\n");
			return;
			
		}
		
	} else {
	
		/* This shouldn't happen here unless someone's been a muppet. */
		debug_print("OP: ~/.tuxmessenger directory not found: creating it.\n");
		options_createhomedir(dir_filename);
		
	}

	rc_filename = malloc(strlen(dir_filename) + 8);
	strcpy(rc_filename, dir_filename);
	strcat(rc_filename, "/config");

	fh = fopen(rc_filename, "w");
	if ( chmod(rc_filename, S_IRUSR | S_IWUSR) != 0 ) {
		error_report("Couldn't set permissions on config file!");
	}
	if ( fh == NULL ) {			

		debug_print("OP: Error opening options file.\n");
		debug_print("OP: Couldn't save options :(\n");
		return;

	}

	newline = malloc(2);
	sprintf(newline, "\n");
	
	if ( options_rememberlogindetails() ) {
		ousername = options_username();
		opassword = options_password();
		ohostname = options_hostname();
		oport = options_port();
	} else {
		ousername = "";
		opassword = "";
		ohostname = DEFAULT_HOSTNAME;
		oport = DEFAULT_NS_PORT;
	}
	
	/* ----------------------- Username -------------------------- */
	if ( fputs("username ", fh) == EOF ) {
		whoops = 1;
	}
	if ( whoops == 0 ) {
		if ( fputs(ousername, fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(newline, fh) == EOF ) {
			whoops = 1;
		}
	}

	/* ----------------------- Password -------------------------- */
	if ( whoops == 0 ) {
		if ( fputs("password ", fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(opassword, fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(newline, fh) == EOF ) {
			whoops = 1;
		}
	}
	
	/* ----------------------- wget -------------------------- */
	if ( whoops == 0 ) {
		if ( fputs("wget ", fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(options_wget(), fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(newline, fh) == EOF ) {
			whoops = 1;
		}
	}
	
	/* ----------------------- Hostname -------------------------- */
	if ( whoops == 0 ) {
		if ( fputs("hostname ", fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(ohostname, fh) == EOF ) {
			whoops = 1;
		}
	}
	if ( whoops == 0 ) {
		if ( fputs(newline, fh) == EOF ) {
			whoops = 1;
		}
	}

	/* ----------------------- Port -------------------------- */
	if ( whoops == 0 ) {
	
		char *port;

		if ( fputs("port ", fh) == EOF ) {
			whoops = 1;
		}
		/* options_port() < 65536 because of data size - so always fits. */
		port = malloc(6);
		sprintf(port, "%i", oport);
		if ( whoops == 0 ) {
			if ( fputs(port, fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(newline, fh) == EOF ) {
				whoops = 1;
			}
		}
		free(port);
		
	}

	/* ----------------------- Show Offline Contacts ---------------------- */
	if ( whoops == 0 ) {
	
		if ( options_hideoffline() ) {
			if ( fputs("hide_offline\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	/* ----------------------- Show CSM ---------------------- */
	if ( whoops == 0 ) {
	
		if ( options_hidecsm() ) {
			if ( fputs("hide_localcsm\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	/* ----------------------- Local colour -------------------------- */
	if ( options_localcolour_string() != NULL ) {
		if ( whoops == 0 ) {
			if ( fputs("localcolour ", fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(options_localcolour_string(), fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(newline, fh) == EOF ) {
				whoops = 1;
			}
		}
	}

	/* ----------------------- Override Contacts' Fonts/Colours ---------------------- */
	if ( whoops == 0 ) {
	
		if ( options_ofontoverride() ) {
			if ( fputs("ofontoverride\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	/* ----------------------- Override colour -------------------------- */
	if ( options_ocolour_string() ) {
		if ( whoops == 0 ) {
			if ( fputs("ocolour ", fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			char *string = options_ocolour_string();
			if ( fputs(string, fh) == EOF ) {
				whoops = 1;
			}
			free(string);
		}
		if ( whoops == 0 ) {
			if ( fputs(newline, fh) == EOF ) {
				whoops = 1;
			}
		}
	}

	/* ----------------------- Local font -------------------------- */
	if ( options_localfont() ) {
		if ( whoops == 0 ) {
			if ( fputs("localfont ", fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(options_localfont(), fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(newline, fh) == EOF ) {
				whoops = 1;
			}
		}
	}

	/* ----------------------- Contacts' font -------------------------- */
	if ( options_ofont() ) {
		if ( whoops == 0 ) {
			if ( fputs("ofont ", fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(options_ofont(), fh) == EOF ) {
				whoops = 1;
			}
		}
		if ( whoops == 0 ) {
			if ( fputs(newline, fh) == EOF ) {
				whoops = 1;
			}
		}
	}

	/* ----------------------- IRC style ---------------------- */
	if ( whoops == 0 ) {
	
		if ( options_ircstyle() ) {
			if ( fputs("ircstyle\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	/* ----------------------- Show emoticons ---------------------- */
	if ( whoops == 0 ) {
	
		if ( options_showemoticons() ) {
			if ( fputs("show_emoticons\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	/* ----------------------- Show avatars --------------------- */
	if ( whoops == 0 ) {
	
		if ( options_showavatars() ) {
			if ( fputs("show_avatars\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	/* ----------------------- Timestamps ---------------------- */
	if ( whoops == 0 ) {
	
		if ( options_timestamps() ) {
			if ( fputs("timestamps\n", fh) == EOF ) {
				whoops = 1;
			}
		} /* Else write nothing. */
		
	}

	if ( whoops != 0 ) {
		debug_print("OP: Whoops! Config file write failed.\n");
	}

	fclose(fh);
	free(newline);

}