File: irc.c

package info (click to toggle)
ircii 20210328-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,644 kB
  • sloc: ansic: 42,273; makefile: 860; sh: 524; perl: 348
file content (1272 lines) | stat: -rw-r--r-- 28,641 bytes parent folder | download
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
1272
/*
 * ircII: a new irc client.  I like it.  I hope you will too!
 *
 * Written By Michael Sandrof
 * 
 * Copyright (c) 1990 Michael Sandrof.
 * Copyright (c) 1991, 1992 Troy Rollo.
 * Copyright (c) 1992-2021 Matthew R. Green.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#define IRCII_VERSION	"20210314"	/* YYYYMMDD */

#include "irc.h"
IRCII_RCSID("@(#)$eterna: irc.c,v 1.416 2021/03/14 18:22:31 mrg Exp $");

#include <sys/stat.h>
#include <pwd.h>

#ifdef DO_USER2
# include <setjmp.h>
#endif /* DO_USER2 */

#include "status.h"
#include "dcc.h"
#include "names.h"
#include "vars.h"
#include "input.h"
#include "alias.h"
#include "output.h"
#include "ircterm.h"
#include "exec.h"
#include "screen.h"
#include "log.h"
#include "server.h"
#include "hook.h"
#include "keys.h"
#include "ircaux.h"
#include "edit.h"
#include "window.h"
#include "history.h"
#include "exec.h"
#include "notify.h"
#include "mail.h"
#include "debug.h"
#include "newio.h"
#include "ctcp.h"
#include "parse.h"
#include "strsep.h"
#include "notice.h"
#include "icb.h"
#include "signals.h"

static	int	qflag;			/* set if we ignore .ircrc */
static	int	bflag;			/* set if we load .ircrc before connecting */
static	int	tflag = 1;		/* don't use termcap ti/te sequences */
static	time_t	current_idle_time;	/* last time the user hit a key */
static	time_t  start_time;		/* epoch time we started */

static	int	cntl_c_hit = 0,
		irc_port_local = IRC_PORT,	/* port of ircd */
		add_servers = 0,		/* -a flag */
		do_refresh_screen,
		no_full_screen = 0,
		cur_signal = -1,		/* head of below */
		occurred_signals[64],		/* got signal which may
						   require /ON action */
		client_default_icb = 0,		/* default to irc server
						   connections */
		fflag = USE_FLOW_CONTROL,	/* true: ^Q/^S used for flow
						   cntl */
		using_server_process = 0;	/* Don't want to start ircio
						   by default... */

static	u_char	*irc_path,			/* paths used by /load */
		*ircrc_file,			/* full path .ircrc file */
		*ircquick_file,			/* full path .ircquick file */
		*progname,			/* set from argv[0] */
		*send_umode,			/* sent umode */
		*args_str,			/* list of command line args */
		hostname[NAME_LEN + 1],		/* name of current host */
		realname[REALNAME_LEN + 1],	/* real name of user */
		username[NAME_LEN + 1],		/* usernameof user */
		*nickname,			/* users nickname */
		*source_host,			/* specify a specific source
						   host for multi-homed
						   machines */
		*irc_lib,			/* path to the ircII library */
		*my_path_dir,			/* path to users home dir */
		*log_file,			/* path to debug log file */
		*default_proxy,			/* default proxy to use */
		irc_version_str[] = IRCII_VERSION;

#ifdef DO_USER2
static	jmp_buf	outta_here;
#endif /* DO_USER2 */

static	void	cntl_c(int);
static	void	sig_user1(int);
static	void	sig_refresh_screen(int);
static	void	sig_on(int);
static	void	real_sig_user1(void);
static	int	do_sig_user1;
#ifdef	DO_USER2
static	void	sig_user2(int) ;
#endif /* DO_USER2 */
static	int	irc_do_a_screen(Screen *, fd_set *);
static	void	irc_io(void);
static	void	quit_response(u_char *, u_char *);
static	void	show_version(void);
static	u_char	*get_arg(u_char *, u_char *, int *);
static	int	parse_arg(u_char *, u_char **, int *, u_char **);
static	u_char	*parse_args(u_char **, int);
static	void	input_pause(void);

#ifdef DEBUG
#define DEBUG_USAGE1 \
"\
   -D <level>\tenable debugging at the specified level\n"
#define DEBUG_USAGE2 \
"\
   -o <file>\twrite debug output to file\n"
#else
#define DEBUG_USAGE1 ""
#define DEBUG_USAGE2 ""
#endif

static	char	switch_help[] =
"Usage: irc [switches] [nickname] [server list] \n\
  The [nickname] can be at most 9 characters long on some server\n\
  The [server list] is a whitespace separated list of server names\n\
  The [switches] may be any or all of the following:\n\
   -a\t\tadds default servers and command line servers to server list\n\
   -b\t\tload .ircrc before connecting to a server\n\
   -c <channel>\tjoins <channel> on startup\n\
   -d\t\truns IRCII in non full screen terminal mode\n"
   DEBUG_USAGE1 "\
   -f\t\tyour terminal uses flow control (^S/^Q), so IRCII shouldn't\n\
   -F\t\tyour terminal doesn't use flow control (default)\n\
   -h <host>\tsource host, for multihomed machines\n\
   -H <host>\toriginating address for dcc requests (to work behind NATs etc)\n\
   -icb\t\tuse ICB connections by default\n\
   -irc\t\tuse IRC connections by default\n\
   -I <file>\tloads <file> in place of your .ircquick\n\
   -l <file>\tloads <file> in place of your .ircrc\n"
   DEBUG_USAGE2 "\
   -p <port>\tdefault IRC server connection port (usually 6667)\n\
   -P <port>\tdefault ICB server connection port (usually 7326)\n\
   -q\t\tdoes not load .ircrc nor .ircquick\n\
   -r\t\treverse terminal colours (only if colours are activated)\n\
   -R <host>[:<port>]\tdefault to using HTTP proxy for connections\n\
   -s\t\tdon't use separate server processes (ircio)\n\
   -S\t\tuse separate server processes (ircio)\n\
   -t\t\tdo not use termcap ti and te sequences at startup\n\
   -T\t\tuse termcap ti and te sequences at startup (default)\n\
Usage: icb [same switches]  (default to -icb)\n";

/* irc_exit: cleans up and leaves */
void
irc_exit(void)
{
	do_hook(EXIT_LIST, "Exiting");
	close_server(-1, empty_string());
	logger(0);
	set_history_file(NULL);
	clean_up_processes();
	if (!term_basic())
	{
		cursor_to_input();	/* Needed so that ircII doesn't gobble
					 * the last line of the kill. */
		term_cr();
		if (term_clear_to_eol())
			term_space_erase(0);
		term_reset();
	}
	exit(0);
}

/*
 * quit_response: Used by irc_io when called from irc_quit to see if we got
 * the right response to our question.  If the response was affirmative, the
 * user gets booted from irc.  Otherwise, life goes on. 
 */
static	void
quit_response(u_char *dummy, u_char *ptr)
{
	size_t	len;

	if ((len = my_strlen(ptr)) != 0)
	{
		if (!my_strnicmp(ptr, UP("yes"), len))
		{
			send_to_server("QUIT");
			irc_exit();
		}
	}
}

/* irc_quit: prompts the user if they wish to exit, then does the right thing */
void
irc_quit(u_int key, u_char *ptr)
{
	static	int in_it = 0;

	if (in_it)
		return;
	in_it = 1;
	add_wait_prompt(UP("Do you really want to quit? "), quit_response,
		empty_string(), WAIT_PROMPT_LINE);
	in_it = 0;
}

void
on_signal_occurred(int signo)
{
	if (cur_signal > ARRAY_SIZE(occurred_signals))
		return;
	occurred_signals[++cur_signal] = signo;
}

/*
 * cntl_c: emergency exit.... if somehow everything else freezes up, hitting
 * ^C five times should kill the program. 
 */
static	void
cntl_c(int signo)
{
	on_signal_occurred(signo);

	if (cntl_c_hit++ >= 4)
		irc_exit();
}

static void
sig_on(int signo)
{
	on_signal_occurred(signo);
}

static void
sig_user1(int signo)
{
	do_sig_user1++;
	on_signal_occurred(signo);
}

static void
real_sig_user1(void)
{
	say("Got SIGUSR1, closing DCC connections and EXECed processes");
	close_all_dcc();
	close_all_exec();
}

#ifdef DO_USER2
static void
sig_user2(int signo)
{
	on_signal_occurred(signo);
	say("Got SIGUSR2, jumping to normal loop");	/* unsafe */
	longjmp(outta_here);
}
#endif 

static void
sig_refresh_screen(int signo)
{
	on_signal_occurred(signo);
	do_refresh_screen++;
}

/* shows the version of irc */
static	void
show_version(void)
{
	printf("ircII version %s\n\r", irc_version());
	exit (0);
}

/* get_arg: used by parse_args() to get an argument after a switch */
static	u_char	*
get_arg(u_char *arg, u_char *next, int *ac)
{
	(*ac)++;
	if (*arg)
		return (arg);
	else
	{
		if (next)
			return (next);
		fprintf(stderr, "irc: missing parameter\n");
		exit(1);
		return (0); /* cleans up a warning */
	}
}

/*
 * parse_arg: parse a single command line argument.  returns 1 when
 * a "--" sequence is detected.
 */
static	int
parse_arg(u_char *arg, u_char **argv, int *acp, u_char **channelp)
{
	while (*arg)
	{
		switch (*(arg++))
		{
		case 'a':
			add_servers = 1;
			break;
		case 'b':
			if (qflag)
			{
				fprintf(stderr, "Can not use -b with -q\n");
				exit(1);
			}
			bflag = 1;
			break;
		case 'c':
			malloc_strcpy(channelp, get_arg(arg, argv[*acp], acp));
			break;
		case 'd':
			no_full_screen = 1;
			break;
#ifdef DEBUG
		case 'D':
			setdlevel(get_arg(arg, argv[*acp], acp));
			break;
#endif /* DEBUG */
		case 'e':
			{
				u_char *proto = get_arg(arg, argv[*acp], acp);
				server_default_encryption(proto,
							  get_arg(arg,
							      argv[*acp], acp));
			}
			break;
		case 'f':
			fflag = 1;
			break;
		case 'F':
			fflag = 0;
			break;
		case 'h':
			malloc_strcpy(&source_host,
				      get_arg(arg, argv[*acp], acp));
			break;
		case 'H':
			set_dcchost(get_arg(arg, argv[*acp], acp));
			break;
		case 'i':
			if (arg[0] == 'r' && arg[1] == 'c')
				client_default_icb = 0;
			else
			if (arg[0] == 'c' && arg[1] == 'b')
				client_default_icb = 1;
			else
			{
				printf("irc: invalid -i option; use -irc "
				       "or -icb");
				exit(-1);
			}
			arg += 2;
			break;
		case 'I':
			malloc_strcpy(&ircquick_file,get_arg(arg, argv[*acp], acp));
			break;
		case 'l':
			malloc_strcpy(&ircrc_file, get_arg(arg, argv[*acp], acp));
			break;
#ifdef DEBUG
		case 'o':
			log_file = get_arg(arg, argv[*acp], acp);
			break;
#endif /* DEBUG */
		case 'p':
			irc_port_local = my_atoi(get_arg(arg, argv[*acp], acp));
			break;
		case 'P':
			set_icb_port(my_atoi(get_arg(arg, argv[*acp], acp)));
			break;
		case 'q':
			if (bflag)
			{
				fprintf(stderr, "Can not use -q with -b\n");
				exit(1);
			}
			qflag = 1;
			break;
		case 'r':
			{
				int oldfg = get_int_var(FOREGROUND_COLOUR_VAR);
				set_int_var(FOREGROUND_COLOUR_VAR,
					    get_int_var(BACKGROUND_COLOUR_VAR));
				set_int_var(BACKGROUND_COLOUR_VAR, oldfg);
			}
			break;
		case 'R':
			default_proxy = get_arg(arg, argv[*acp], acp);
			break;
		case 's':
			using_server_process = 0;
			break;
		case 'S':
			using_server_process = 1;
			break;
		case 't':
			tflag = 1;
			break;
		case 'T':
			tflag = 0;
			break;
		case 'v':
			show_version();
			break;
		case '-':
			return 1;
		default:
			fputs(switch_help, stderr);
			exit(1);
		}
	}

	return 0;
}


/*
 * parse_args: parse command line arguments for irc, and sets all initial
 * flags, etc. 
 */
static	u_char	*
parse_args(u_char **argv, int argc)
{
	u_char	*arg,
		*ptr;
	int	ac;
	u_char	*channel = NULL;
	struct	passwd	*entry;
	int	minus_minus = 0;

	if (my_strncmp(argv[0], "icb", 3) == 0 || strstr(CP(argv[0]), "/icb") != 0)
		client_default_icb = 1;
	*realname = '\0';
	ac = 1;
	malloc_strcpy(&args_str, argv[0]);
	malloc_strcat(&args_str, UP(" "));
	while (ac < argc && (arg = argv[ac++]) != NULL)
	{
		malloc_strcat(&args_str, arg);
		malloc_strcat(&args_str, UP(" "));
		if (*arg == '-')
		{
			minus_minus = parse_arg(arg+1, argv, &ac, &channel);
			if (minus_minus)
				break;
		}
		else
		{
			if (nickname && *nickname)
				build_server_list(arg);
			else
				malloc_strcpy(&nickname, arg);
		}
	}
	if (NULL != (ptr = my_getenv("IRCLIB")))
	{
		malloc_strcpy(&irc_lib, ptr);
		malloc_strcat(&irc_lib, UP("/"));
	}
	else
		malloc_strcpy(&irc_lib, UP(IRCLIB));

	/* -h overrides environment variable */
	if (NULL == source_host && NULL != (ptr = my_getenv("IRCHOST")))
		malloc_strcpy(&source_host, ptr);

	if (NULL == ircrc_file && NULL != (ptr = my_getenv("IRCRC")))
		malloc_strcpy(&ircrc_file, ptr);

	if (NULL == ircquick_file && NULL != (ptr = my_getenv("IRCQUICK")))
		malloc_strcpy(&ircquick_file, ptr);

	if ((nickname == 0 || *nickname == '\0') && NULL != (ptr = my_getenv("IRCNICK")))
		malloc_strcpy(&nickname, ptr);

	if ((ptr = my_getenv("USER")) != NULL)
		my_strmcpy(username, ptr, NAME_LEN);

	if (NULL != (ptr = my_getenv("IRCUMODE")))
		malloc_strcpy(&send_umode, ptr);

	if (NULL != (ptr = my_getenv("IRCNAME")))
		my_strmcpy(realname, ptr, REALNAME_LEN);

	if (NULL != (ptr = my_getenv("IRCPATH")))
		malloc_strcpy(&irc_path, ptr);
	else
	{
#ifdef IRCPATH
		malloc_strcpy(&irc_path, UP(IRCPATH));
#else
		malloc_strcpy(&irc_path, UP(".:~/.irc:"));
		malloc_strcat(&irc_path, irc_lib);
		malloc_strcat(&irc_path, UP("script"));
#endif /* IRCPATH */
	}

	/* now, open the log file */
	if (log_file)
		open_log_file(log_file);

	if (default_proxy)
		server_set_default_proxy(default_proxy);

	set_string_var(LOAD_PATH_VAR, irc_path);
	new_free(&irc_path);
	if (NULL != (ptr = my_getenv("IRCSERVER")))
		build_server_list(ptr);
	if (0 == number_of_servers() || add_servers)
	{
		if (read_server_file() || 0 == number_of_servers())
		{
			u_char *s = NULL;

			malloc_strcpy(&s, UP(DEFAULT_SERVER));
			build_server_list(s);
			new_free(&s);
		}
	}

	if (NULL != (entry = getpwuid(getuid())))
	{
		if ((*realname == '\0') && entry->pw_gecos && *(entry->pw_gecos))
		{
#ifdef GECOS_DELIMITER
			if ((ptr = my_index(entry->pw_gecos, GECOS_DELIMITER)) 
					!= NULL)
				*ptr = '\0';
#endif /* GECOS_DELIMITER */
			if ((ptr = my_index(entry->pw_gecos, '&')) == NULL)
				my_strmcpy(realname, entry->pw_gecos, REALNAME_LEN);
			else {
				size_t len = ptr - (u_char *) entry->pw_gecos;

				if (len < REALNAME_LEN && *(entry->pw_name)) {
					u_char *q = realname + len;

					my_strmcpy(realname, entry->pw_gecos, len);
					my_strmcat(realname, entry->pw_name, REALNAME_LEN);
					my_strmcat(realname, ptr + 1, REALNAME_LEN);
					if (islower(*q) && (q == realname || isspace(*(q - 1))))
						*q = toupper(*q);
				} else
					my_strmcpy(realname, entry->pw_gecos, REALNAME_LEN);
			}
		}
		if (entry->pw_name && *(entry->pw_name) && '\0' == *username)
			my_strmcpy(username, entry->pw_name, NAME_LEN);

		if (entry->pw_dir && *(entry->pw_dir))
			malloc_strcpy(&my_path_dir, UP(entry->pw_dir));
	}
	if (NULL != (ptr = my_getenv("HOME")))
		malloc_strcpy(&my_path_dir, ptr);
	else if (*my_path_dir == '\0')
		malloc_strcpy(&my_path_dir, UP("/"));

	if ('\0' == *realname)
		my_strmcpy(realname, "*Unknown*", REALNAME_LEN);

	if ('\0' == *username)
		my_strmcpy(username, "Unknown", NAME_LEN);

	if (NULL != (ptr = my_getenv("IRCUSER")))
		my_strmcpy(username, ptr, REALNAME_LEN);

	if (nickname == 0 || *nickname == '\0')
		malloc_strcpy(&nickname, username);
	if (NULL == ircrc_file)
	{
		ircrc_file = new_malloc(my_strlen(my_path_dir) +
			my_strlen(IRCRC_NAME) + 1);
		my_strcpy(ircrc_file, my_path_dir);
		my_strcat(ircrc_file, IRCRC_NAME);
	}
	if (NULL == ircquick_file)
	{
		ircquick_file = new_malloc(my_strlen(my_path_dir) +
			my_strlen(IRCQUICK_NAME) + 1);
		my_strcpy(ircquick_file, my_path_dir);
		my_strcat(ircquick_file, IRCQUICK_NAME);
	}

	return (channel);
}

/*
 * input_pause: prompt the user for a single keystroke before continuing.
 * Only call this before calling irc_io().
 */
static void
input_pause(void)
{
	const u_char *msg = UP("********  Press any key to continue  ********");
	char dummy;

	if (term_basic())
	{
		puts(CP(msg));
		fflush(stdout);
	}
	else
	{
		int cols;

		copy_window_size(NULL, &cols);
		term_move_cursor(0, cols);
		output_line(msg, 0);
		cursor_not_in_display();
	}
	(void)read(0, &dummy, 1);
}

/*
 * generally processes one screen's input.  this used to be part of irc_io()
 * but has been split out because that function was too large and too nested.
 * it returns 0 if IO processing should continue and 1 if it should stop.
 */
static	int
irc_do_a_screen(Screen *screen, fd_set *rd) 
{
	if (!screen_get_alive(screen))
		return 0;
	set_current_screen(screen);
	if (!is_main_screen(screen) &&
	    FD_ISSET(screen_get_wserv_fd(screen), rd))
		screen_wserv_message(screen);
	if (FD_ISSET(screen_get_fdin(screen), rd))
	{
		/* buffer much bigger than IRCD_BUFFER_SIZE */
		u_char	lbuf[BIG_BUFFER_SIZE + 1];

		/*
		 * This section of code handles all in put from the terminal(s)
		 * connected to ircII.  Perhaps the idle time *shouldn't* be
		 * reset unless its not a screen-fd that was closed..
		 */
		current_idle_time = time(0);
		if (term_basic())
		{
			int     old_timeout;

			old_timeout = dgets_timeout(1);
			switch (dgets(lbuf, INPUT_BUFFER_SIZE, screen_get_fdin(screen)))
			{
			case 0:
			case -1:
				say("IRCII exiting on EOF from stdin");
				irc_exit();
				break;
			default:
				(void) dgets_timeout(old_timeout);
				*(lbuf + my_strlen(lbuf) - 1) = '\0';
				if (get_int_var(INPUT_ALIASES_VAR))	
					parse_line(NULL, lbuf,
					    empty_string(), 1, 0, 0);
				else
					parse_line(NULL, lbuf,
					    NULL, 1, 0, 0);
				break;
			}
		}
		else
		{
			int server;
			u_char	loc_buffer[BIG_BUFFER_SIZE + 1];
			int	n, i;

			server = set_from_server(get_window_server(0));
			set_last_input_screen(screen);
			if ((n = read(screen_get_fdin(screen), loc_buffer,
					sizeof loc_buffer)) != 0)
				for (i = 0; i < n; i++)
					edit_char((u_int)loc_buffer[i]);
				/*
				 * if the current screen isn't the main  screen,
				 * then the socket to the current screen must have
				 * closed, so we call kill_screen() to handle 
				 * this - phone, jan 1993.
				 * but not when we arent running windows - Fizzy, may 1993
				 * if it is the main screen we got an EOF on, we exit..
				 * closed tty -> chew cpu -> bad .. -phone, july 1993.
				 */
#ifdef WINDOW_CREATE
			else
			{
				if (!is_main_screen(screen))
					kill_screen(screen);
				else
					irc_exit();
			}
#endif /* WINDOW_CREATE */
			cntl_c_hit = 0;
			set_from_server(server);
		}
	}
	return 0;
}

/*
 * irc_io: the main irc input/output loop.   Handles all io from keyboard,
 * server, exec'd processes, etc.  If a prompt is specified, it is displayed
 * in the input line and cannot be backspaced over, etc. The func is a
 * function which will take the place of the SEND_LINE function (which is
 * what happens when you hit return at the end of a line). This function must
 * decide if it's ok to exit based on anything you really want.
 */
void
irc_io(void)
{
	fd_set	rd,
		wd;
	struct	timeval cursor_timeout,
		clock_timeout,
		right_away,
		timer,
		*timeptr;
	int	hold_over;
	Screen	*screen,
		*old_current_screen;
	int	irc_io_loop = 1;
	int	i;

	/* time before cursor jumps from display area to input line */
	cursor_timeout.tv_usec = 0L;
	cursor_timeout.tv_sec = 1L;

	/* time delay for updating of internal clock */
	clock_timeout.tv_usec = 0L;
	clock_timeout.tv_sec = 30L;

	right_away.tv_usec = 0L;
	right_away.tv_sec = 0L;

	if (!term_basic())
		set_input_prompt(get_string_var(INPUT_PROMPT_VAR));

#ifdef DO_USER2
	if (setjmp(outta_here))
		yell("*** Got SIGUSR2, Aborting");
#endif /* DO_USER2 */

	timeptr = &clock_timeout;
	do
	{
		set_ctcp_was_crypted(0);
		FD_ZERO(&rd);
		FD_ZERO(&wd);
		set_process_bits(&rd);
		server_set_bits(&rd, &wd);
		for (screen = screen_first(); screen; screen = screen_get_next(screen))
			if (screen_get_alive(screen))
			{
				FD_SET(screen_get_fdin(screen), &rd);
				if (!is_main_screen(screen))
					FD_SET(screen_get_wserv_fd(screen), &rd);
			}
		set_dcc_bits(&rd, &wd);
		term_check_refresh();
		timer_timeout(&timer);
		if (timer.tv_sec <= timeptr->tv_sec)
			timeptr = &timer;
		if ((hold_over = unhold_windows()) != 0)
			timeptr = &right_away;
		Debug(DB_IRCIO, "irc_io: selecting with %lld:%ld timeout",
			(long long)timeptr->tv_sec, (long)timeptr->tv_usec);
		switch (new_select(&rd, &wd, timeptr))
		{
		case 0:
		case -1:
	/*
	 * yay for the QNX socket manager... drift here, drift there, oops,
	 * i fell down a hole..
	 */
#ifdef __QNX__
			if (errno == EBADF || errno == ESRCH)
				irc_io_loop = 0;
#endif /* __QNX__ */
			if (cntl_c_hit)
			{
				edit_char((u_int)'\003');
				cntl_c_hit = 0;
			}
			check_status_alarmed();
			if (do_refresh_screen)
			{
				refresh_screen(0, NULL);
				do_refresh_screen = 0;
			}
			if (do_sig_user1)
			{
				real_sig_user1();
				do_sig_user1 = 0;
			}
			for (i = 0; i <= cur_signal; i++)
				if (occurred_signals[i] != 0)
					do_hook(OS_SIGNAL_LIST, "%s",
					    get_signal(i, 0));
			cur_signal = -1;
			if (!hold_over)
				cursor_to_input();
			break;
		default:
			term_check_refresh();
			old_current_screen = get_current_screen();
			set_current_screen(get_last_input_screen());
			dcc_check(&rd, &wd);
			do_server(&rd, &wd);
			set_current_screen(old_current_screen);
			for (screen = screen_first(); screen;
			     screen = screen_get_next(screen))
				if (irc_do_a_screen(screen, &rd))
					irc_io_loop = 0;
			set_current_screen(old_current_screen);
			if (irc_io_loop)
				do_processes(&rd);
			break;
		}
		if (!irc_io_loop)
			break;
		execute_timer();
		check_process_limits();
		while (check_wait_status(-1) >= 0)
			;
		if (get_primary_server() == -1 && !never_connected())
			do_hook(DISCONNECT_LIST, "%s", nickname);
		timeptr = &clock_timeout;

		old_current_screen = get_current_screen();
		for (screen = screen_first(); screen; screen = screen_get_next(screen))
		{
			set_current_screen(screen);
			if (screen_get_alive(screen) && is_cursor_in_display())
				timeptr = &cursor_timeout;
		}
		set_current_screen(old_current_screen);

		if (update_clock(0, 0, UPDATE_TIME))
		{
			Debug(DB_IRCIO, "update_clock(0,0,0) returned true; updating clock");
			if (get_int_var(CLOCK_VAR) || check_mail_status())
			{
				status_update(1);
				cursor_to_input();
			}
			if (get_primary_server() != -1)
				do_notify();
		}
	}
	while (irc_io_loop);

	update_input(UPDATE_ALL);
	return;
}

int
main(int, char *[], char *[]);

int
main(int argc, char *argv[], char *envp[])
{
	u_char	*channel;

	progname = UP(argv[0]);
	srandom(time(NULL) ^ getpid());	/* something */

	start_time = time(NULL);
#ifdef	SOCKS
	SOCKSinit(argv[0]);
#endif /* SOCKS */
	channel = parse_args((u_char **) argv, argc);
	if (gethostname(CP(hostname), NAME_LEN))
	{
		fprintf(stderr, "irc: couldn't figure out the name of your machine!\n");
		exit(1);
	}
	term_set_fp(stdout);
	if (term_basic())
		new_window();
	else
	{
		init_screen();

#if defined(SIGCONT)
		(void) MY_SIGNAL(SIGCONT, (sigfunc *) term_cont, 0);
#endif

#if defined(SIGWINCH)
		(void) MY_SIGNAL(SIGWINCH, (sigfunc *) sig_refresh_screen, 0);
#endif

		(void) MY_SIGNAL(SIGSEGV, (sigfunc *) SIG_DFL, 0);
		(void) MY_SIGNAL(SIGBUS, (sigfunc *) SIG_DFL, 0);
		(void) MY_SIGNAL(SIGHUP, (sigfunc *) irc_exit, 0);
		(void) MY_SIGNAL(SIGTERM, (sigfunc *) irc_exit, 0);
		(void) MY_SIGNAL(SIGPIPE, (sigfunc *) sig_on, 0);
		(void) MY_SIGNAL(SIGINT, (sigfunc *) cntl_c, 0);
#ifdef SIGSTOP
		(void) MY_SIGNAL(SIGSTOP, (sigfunc *) sig_on, 0);
#endif /* SIGSTOP */

		(void) MY_SIGNAL(SIGUSR1, (sigfunc *) sig_user1, 0);
#ifdef DO_USER2
		(void) MY_SIGNAL(SIGUSR2, (sigfunc *) sig_user2, 0);
#else
		(void) MY_SIGNAL(SIGUSR2, (sigfunc *) sig_on, 0);
#endif /* DO_USER2 */

#ifdef SIGPWR
		(void) MY_SIGNAL(SIGPWR, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGCHLD
		(void) MY_SIGNAL(SIGCHLD, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGURG
		(void) MY_SIGNAL(SIGURG, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGTRAP
		(void) MY_SIGNAL(SIGTRAP, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGTTIN
		(void) MY_SIGNAL(SIGTTIN, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGTTOU
		(void) MY_SIGNAL(SIGTTOU, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGXCPU
		(void) MY_SIGNAL(SIGXCPU, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGXFSZ
		(void) MY_SIGNAL(SIGXFSZ, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGPOLL
		(void) MY_SIGNAL(SIGPOLL, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGINFO
		(void) MY_SIGNAL(SIGINFO, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGWAITING
		(void) MY_SIGNAL(SIGWAITING, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGLWP
		(void) MY_SIGNAL(SIGLWP, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGSYS
		(void) MY_SIGNAL(SIGSYS, (sigfunc *) sig_on, 0);
#endif
#ifdef SIGIO
		(void) MY_SIGNAL(SIGIO, (sigfunc *) sig_on, 0);
#endif
		/* More signals could probably be added, perhaps some
		   should be removed */
	}

	init_variables();

	if (!term_basic())
	{
		build_status(NULL);
		update_input(UPDATE_ALL);
	}

#ifdef MOTD_FILE
	{
		struct	stat	motd_stat,
				my_stat;
		u_char	*motd = NULL;
		int	des;

#ifdef MOTD_ABSFILE
		malloc_strcpy(&motd, UP(MOTD_FILE));
#else
		malloc_strcpy(&motd, irc_lib);
		malloc_strcat(&motd, UP(MOTD_FILE));
#endif
		
		if (stat(CP(motd), &motd_stat) == 0)
		{
			u_char	*s = NULL;

			malloc_strcpy(&s, my_path_dir);
			malloc_strcat(&s, UP("/.ircmotd"));
			if (stat(CP(s), &my_stat))
			{
				my_stat.st_atime = 0L;
				my_stat.st_mtime = 0L;
			}
			unlink(CP(s));
			if ((des = open(CP(s), O_CREAT, S_IREAD | S_IWRITE))
					!= -1)
				new_close(des);
			new_free(&s);
			if (motd_stat.st_mtime > my_stat.st_mtime)
			{
				put_file(motd);
				/*
				 * Thanks to Mark Dame <mdame@uceng.ec.edu>
				 * for this one
				 */
				input_pause();
				clear_window_by_refnum(0);
			}
		}
		new_free(&motd);
	}
#endif /* MOTD_FILE */

	if (bflag)
	{
		load_global();
		load_ircrc();
	}

	get_connected(0);
	if (channel)
	{
		if (server_get_version(get_primary_server()) == ServerICB)
			server_set_icbgroup(get_primary_server(), channel);
		else
		{
			u_char    *ptr;

			ptr = my_strsep(&channel, UP(","));
			if (is_channel(ptr))
				add_channel(ptr, 0, 0, CHAN_LIMBO, NULL);
			while ((ptr = my_strsep(&channel, UP(","))) != NULL)
				if (is_channel(ptr))
					add_channel(ptr, 0, 0, CHAN_LIMBO, NULL);
			new_free(&channel);
		}
	}
	current_idle_time = time(0);
	set_input(empty_string());
	irc_io();
	irc_exit();
	return 0;
}

/* 
 * set_irchost: This sets the source host for subsequent connections.
 */
void
set_irchost(u_char *irchost)
{
	if (!irchost || !*irchost)
		new_free(&source_host);
	else
		malloc_strcpy(&source_host, irchost);
}

u_char *
get_irchost(void)
{
	return source_host;
}

int
irc_port(void)
{
	return irc_port_local;
}

int
using_ircio(void)
{
	return using_server_process;
}

u_char *
program_name(void)
{
	return progname;
}

u_char *
irc_version(void)
{
	return irc_version_str;
}

time_t
idle_time(void)
{
	return current_idle_time;
}

int
client_default_is_icb(void)
{
	return client_default_icb;
}

int
use_flow_control(void)
{
	return fflag;
}

int
use_termcap_enterexit(void)
{
	return tflag;
}

int
use_background_mode(void)
{
	return bflag;
}

int
ignore_ircrc(void)
{
	return qflag;
}

u_char *
my_path(void)
{
	return my_path_dir;
}

u_char *
my_hostname(void)
{
	return hostname;
}

u_char *
my_realname(void)
{
	return realname;
}

u_char *
my_username(void)
{
	return username;
}

void
set_realname(u_char *value)
{
	if (value)
		strmcpy(realname, value, REALNAME_LEN);
	else
		*realname = '\0';
}

void
set_username(u_char *value)
{
	if (value)
		strmcpy(username, value, REALNAME_LEN);
	else
		*username = '\0';
}

u_char *
my_irc_lib(void)
{
	return irc_lib;
}

u_char *
irc_umode(void)
{
	return (send_umode && *send_umode) ? send_umode : hostname;
}

u_char *
irc_extra_args(void)
{
	return args_str;
}

int
term_basic(void)
{
	return no_full_screen;
}

u_char *
my_nickname(void)
{
	return nickname;
}

void
set_nickname(u_char *value)
{
	if (value)
		malloc_strcpy(&nickname, value);
	else
		new_free(&nickname);
}

u_char *
ircquick_file_path(void)
{
	return ircquick_file;
}

u_char *
ircrc_file_path(void)
{
	return ircrc_file;
}

void
set_ircrc_file_path(u_char *path)
{
	if (path)
		malloc_strcpy(&ircrc_file, path);
	else
		new_free(&ircrc_file);
}

u_char *
zero(void)
{
	static u_char z[] = { '0', '\0' };
	return z;
}

u_char *
one(void)
{
	static u_char o[] = { '1', '\0' };
	return o;
}

u_char *
empty_string(void)
{
	static u_char e[] = { '\0' };
	return e;
}