File: tinyirc.c

package info (click to toggle)
tinyirc 1%3A1.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 292 kB
  • ctags: 156
  • sloc: ansic: 1,307; makefile: 74
file content (1082 lines) | stat: -rw-r--r-- 24,867 bytes parent folder | download | duplicates (6)
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
#undef AUTOJOIN	"JOIN :#linuxcon\n"
#define COMMANDCHAR	'/'
#define ASCIIHEXCHAR	'@'
#define HEXASCIICHAR	'#'
#define USE_ANSICOLOR
/* each line of hist adds 512 bytes to resident size */
#define HISTLEN		8
#ifdef AUTOJOIN
#define RELEASE		"TinyIRC 1.1 LinuxConv Edition"
#else
#define RELEASE		"TinyIRC 1.1"
#endif
/* most bytes to try to read from server at one time */
#define IB_SIZE		4096
/* TinyIRC 1.1
   Copyright (C) 1991-1996 Nathan I. Laredo

   This program is modifiable/redistributable under the terms
   of the GNU General Public Licence.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   Send your comments and all your spare pocket change to
   laredo@gnu.ai.mit.edu (Nathan Laredo) or to PSC1, BOX 709,
   Lackland AFB, TX, 78236-5128
 */
#include <stdio.h>
#ifndef POSIX
#include <sgtty.h>
#define	USE_OLD_TTY
#include <sys/ioctl.h>
#if !defined(sun) && !defined(sequent) && !defined(__hpux) && \
	!defined(_AIX_)
#include <strings.h>
#define strchr index
#else
#include <string.h>
#endif
#else
#include <string.h>
#include <termios.h>
#endif
#include <pwd.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <utmp.h>
#include <stdlib.h>
#include <time.h>
#include <term.h>
#include <arpa/inet.h>
struct dlist {
    char name[64];
    char mode[64];
    struct dlist *next;
};

#define ischan(x) (x && (*x == '#' || *x == '&' || *x == '+'))
#define OBJ obj->name
#define raise_sig(x) kill(getpid(), x)
struct dlist *obj = NULL, *olist = NULL, *newobj;
unsigned short IRCPORT = DEFAULTPORT;
int my_tcp, sok = 1, my_tty, hline, dumb = 0, CO, LI, column;
char *tmp, *linein, *CM, *CS, *CE, *SO, *SE, *DC, *ptr, *term, *fromhost,
*TOK[20], IRCNAME[32], IRCLOGIN[64], IRCGECOS[64], inputbuf[512], ib[IB_SIZE],
 serverdata[512], ch, bp[1024], lineout[512], *hist[HISTLEN], termcap[1024];
int cursd = 0, curli = 0, curx = 0, noinput = 0, reconnect = 1;
fd_set readfs;
struct timeval timeout;
struct tm *timenow;
static time_t idletimer, datenow, wasdate, tmptime;
struct passwd *userinfo;
#ifdef  CURSES
#include <curses.h>
#else
#ifdef	POSIX
struct termios _tty;
tcflag_t _res_oflg, _res_lflg;
#define raw() (_tty.c_lflag &= ~(ICANON | ECHO | ICRNL | ISIG), \
	_tty.c_oflag &= ~ONLCR, tcsetattr(my_tty, TCSANOW, &_tty))
#define savetty() ((void) tcgetattr(my_tty, &_tty), \
	_res_oflg = _tty.c_oflag, _res_lflg = _tty.c_lflag)
#define resetty() (_tty.c_oflag = _res_oflg, _tty.c_lflag = _res_lflg,\
	(void) tcsetattr(my_tty, TCSADRAIN, &_tty))
#else
struct sgttyb _tty;
int _res_flg;
#define raw() (_tty.sg_flags |= RAW, _tty.sg_flags &= ~(ECHO | CRMOD), \
	ioctl(my_tty, TIOCSETP, &_tty))
#define savetty() ((void) ioctl(my_tty, TIOCGETP, &_tty), \
	_res_flg = _tty.sg_flags)
#define resetty() (_tty.sg_flags = _res_flg, \
	(void) ioctl(my_tty, TIOCSETP, &_tty))
#endif
#endif
int putchar_x(c)
int c;
{
    return putchar(c);
}
#define	tputs_x(s) (tputs(s,0,putchar_x))
int my_stricmp(str1, str2)
char *str1, *str2;
{
    int cmp;
    if (str1 == NULL || str2 == NULL) return 0;
    while (*str1 != 0 && str2 != 0) {
	if (isalpha(*str1) && isalpha(*str2)) {
	    cmp = *str1 ^ *str2;
	    if ((cmp != 32) && (cmp != 0))
		return (*str1 - *str2);
	} else {
	    if (*str1 != *str2)
		return (*str1 - *str2);
	}
	str1++;
	str2++;
    }
    return (*str1 - *str2);
}
struct dlist *additem(item, p)
char *item;
struct dlist *p;
{
    newobj = (struct dlist *) malloc(sizeof(struct dlist));
    strcpy(newobj->name, item);
    newobj->mode[0] = '\0';
    newobj->next = p;
    return newobj;
}
struct dlist *finditem(item, p)
char *item;
struct dlist *p;
{
    while (p != NULL)
	if (my_stricmp(item, p->name) == 0)
	    break;
	else
	    p = p->next;
    return p;
}
struct dlist *delitem(item, p)
char *item;
struct dlist *p;
{
    struct dlist *prev = NULL, *start = p;
    while (p != NULL)
	if (my_stricmp(item, p->name) == 0) {
	    newobj = p->next;
	    if (obj == p)
		obj = NULL;
	    free(p);
	    if (prev == NULL)
		return newobj;
	    else {
		prev->next = newobj;
		return start;
	    }
	} else {
	    prev = p;
	    p = p->next;
	}
    return start;
}

char encoded[512];
void hexascii(s)
char *s;
{
    int ch, i, j, k, l;

    j = k = l = 0;
    for (i = 0; i < strlen(s) && j < 400; i++) {
	ch = toupper(s[i]);
	if (ch >= '0' && ch <= '9')
	    (l = (l << 4) | (ch - '0'), k++);
	else if (ch >= 'A' && ch <= 'F')
	    (l = (l << 4) | (ch - 'A' + 10), k++);
	if (k == 2)
	    (encoded[j++] = l, k = 0);
    }
    encoded[j] = '\0';
}
void asciihex(s)
char *s;
{
    int i;

    *encoded = '\0';
    for (i = 0; i < strlen(s); i++)
	sprintf(&encoded[strlen(encoded)], "%02x", s[i]);
}
int sendline()
{
    if (write(my_tcp, lineout, strlen(lineout)) < 1)
	return 0;
    return 1;
}
void updatestatus()
{
    int n;
    if (!dumb) {
	if (60 < (datenow = time(NULL)) - wasdate) {
	    wasdate = datenow;
	    timenow = localtime(&datenow);
	    tputs_x(tgoto(CM, 0, LI - 2));
	    tputs_x(SO);
	    if (obj != NULL)
		n = printf("%02d:%02d %s %s (%s) %s", timenow->tm_hour,
			   timenow->tm_min, IRCNAME, OBJ,
			   obj->mode, RELEASE);
	    else
		n = printf("%02d:%02d %s * * %s", timenow->tm_hour,
			   timenow->tm_min, IRCNAME, RELEASE);
	    for (; n < CO; n++)
		putchar(' ');
	    tputs_x(SE);
	}
    }
}
static int nop()
{
    return 1;
}
static int doerror()
{
    column = printf("*** ERROR:");
    return 2;
}
static int doinvite()
{
    printf("*** %s (%s) invites you to join %s.",
	   TOK[0], fromhost, TOK[3]);
    return 0;
}
static int dojoin()
{
    if (strcmp(TOK[0], IRCNAME) == 0) {
	obj = olist = additem(TOK[2], olist);
	sprintf(lineout, "MODE :%s\n", OBJ);
	sendline();
	printf("*** Now talking in %s", OBJ);
	wasdate = 0;
    } else
	printf("*** %s (%s) joined %s", TOK[0], fromhost, TOK[2]);
    return 0;
}
static int dokick()
{
    printf("*** %s was kicked from %s by %s (%s)",
	   TOK[3], TOK[2], TOK[0], TOK[4]);
    if (strcmp(TOK[3], IRCNAME) == 0) {
	olist = delitem(TOK[2], olist);
	if (obj == NULL)
	    obj = olist;
	if (obj != NULL)
	    printf("\n\r*** Now talking in %s", OBJ);
	wasdate = 0;
    }
    return 0;
}
static int dokill()
{
    printf("*** %s killed by %s: ", TOK[3], TOK[0]);
    if (strcmp(TOK[3], IRCNAME) == 0)
	reconnect = 0;		/* don't reconnect if killed */
    return 4;
}
static int domode()
{
    char *t = TOK[3], op = *TOK[3];
    printf("*** %s changed %s to:", TOK[0], TOK[2]);
    if ((newobj = finditem(TOK[2], olist)) != NULL) {
	while ((t = strpbrk(t, "-+psitnml")) != NULL) {
	    if (*t == '-' || *t == '+')
		op = *t;
	    else if (op == '-')
		for (tmp = strchr(newobj->mode, *t); *tmp != '\0'; tmp++)
		    *tmp = *(tmp + 1);
	    else
		strncat(newobj->mode, t, 1);
	    t++;
	}
	if (newobj == obj)
	    wasdate = 0;
    }
    return 3;
}
static int donick()
{
    if (strcmp(TOK[0], IRCNAME) == 0) {
	wasdate = 0;
	strcpy(IRCNAME, TOK[2]);
    }
    printf("*** %s is now known as %s", TOK[0], TOK[2]);
    return 0;
}
static int donotice()
{
    if (!ischan(TOK[2]))
	column = printf("-%s-", TOK[0]);
    else
	column = printf("-%s:%s-", TOK[0], TOK[2]);
    return 3;
}
static int dopart()
{
    printf("*** %s (%s) left %s", TOK[0], fromhost,
	   TOK[2]);
    if (strcmp(TOK[0], IRCNAME) == 0) {
	olist = delitem(TOK[2], olist);
	if (obj == NULL)
	    obj = olist;
	if (obj != NULL)
	    printf("\n\r*** Now talking in %s", OBJ);
	wasdate = 0;
    }
    return 0;
}
static int dopong()
{
    column = printf("*** Got PONG from %s:", TOK[0]);
    return 3;
}
static int doprivmsg()
{
#ifdef DO_CTCP
    if (strncmp(TOK[3], "\01PING", 5) == 0) {	/* lame ctcp ping hack */
	sprintf(lineout, "NOTICE %s :%s\n", TOK[0], TOK[3]);
	column = printf("*** CTCP PING from %s", TOK[0]);
	sendline();
	return 0;
    } else if (strncmp(TOK[3], "\01VERSION", 8) == 0) {		/* lame ctcp */
	sprintf(lineout, "NOTICE %s :\01VERSION " RELEASE " :*ix\01\n",
		TOK[0]);
	column = printf("*** CTCP VERSION from %s", TOK[0]);
	sendline();
	return 0;
    }
#endif
    if (!ischan(TOK[2])) {
#ifdef USE_ANSICOLOR
	printf("\E[1m");
#endif
	column = printf("*%s*", TOK[0]);
    } else if (obj != NULL && my_stricmp(OBJ, TOK[2]))
	column = printf("<%s:%s>", TOK[0], TOK[2]);
    else
	column = printf("<%s>", TOK[0]);
    return 3;
}
static int doquit()
{
    printf("*** %s (%s) Quit (%s)", TOK[0], fromhost, TOK[2]);
    return 0;
}
static int dosquit()
{
    return 1;
}
static int dotime()
{
    return 1;
}
static int dotopic()
{
    printf("*** %s set %s topic to \"%s\"", TOK[0], TOK[2],
	   TOK[3]);
    return 0;
}
int donumeric(num)
int num;
{
    switch (num) {
    case 352:
	column = printf("%-14s %-10s %-3s %s@%s :", TOK[3], TOK[7],
			TOK[8], TOK[4], TOK[5]);
	return 9;
    case 311:
	column = printf("*** %s is %s@%s", TOK[3], TOK[4], TOK[5]);
	return 6;
    case 324:
	if ((newobj = finditem(TOK[3], olist)) != NULL) {
	    strcpy(newobj->mode, TOK[4]);
	    wasdate = 0;
	}
	break;
    case 329:
	tmptime = atoi(TOK[4]);
	strcpy(lineout, ctime(&tmptime));
	tmp = strchr(lineout, '\n');
	if (tmp != NULL)
	    *tmp = '\0';
	column = printf("*** %s formed %s", TOK[3], lineout);
	return 0;
    case 333:
	tmptime = atoi(TOK[5]);
	strcpy(lineout, ctime(&tmptime));
	tmp = strchr(lineout, '\n');
	if (tmp != NULL)
	    *tmp = '\0';
	column = printf("*** %s topic set by %s on %s", TOK[3], TOK[4],
			lineout);
	return 0;
    case 317:
	tmptime = atoi(TOK[5]);
	strcpy(lineout, ctime(&tmptime));
	tmp = strchr(lineout, '\n');
	if (tmp != NULL)
	    *tmp = '\0';
	column = printf("*** %s idle %s second(s), on since %s",
			TOK[3], TOK[4], lineout);
	return 0;
    case 432:
    case 433:
	printf("*** You've chosen an invalid nick.  Choose again.");
	tmp = IRCNAME;
	if (!dumb) {
	    tputs_x(tgoto(CM, 0, LI - 1));
	    tputs_x(CE);
	    resetty();
	}
	printf("New Nick? ");
	while ((ch = getchar()) != '\n')
	    if (strlen(IRCNAME) < 9)
		*(tmp++) = ch;
	*tmp = '\0';
	if (!dumb) {
	    raw();
#ifdef CURSES
	    nonl();
	    noecho();
#endif
	    tputs_x(tgoto(CM, 0, LI - 1));
	    tputs_x(CE);
	}
	sprintf(lineout, "NICK :%s\n", IRCNAME);
	sendline();
#ifdef AUTOJOIN
	sprintf(lineout, AUTOJOIN);
	sendline();
#endif
	return wasdate = 0;
    default:
	break;
    }
    column = printf("%s", TOK[1]);
    return 3;
}
#define LISTSIZE 49
static char *clist[LISTSIZE] =
{"ADMIN", "AWAY", "CLOSE", "CONNECT", "DIE", "DNS", "ERROR", "HASH",
"HELP", "INFO", "INVITE", "ISON", "JOIN", "KICK", "KILL", "LINKS", "LIST",
 "LUSERS", "MODE", "MOTD", "MSG", "NAMES", "NICK", "NOTE", "NOTICE", "OPER",
 "PART", "PASS", "PING", "PONG", "PRIVMSG", "QUIT", "REHASH", "RESTART",
 "SERVER", "SQUIT", "STATS", "SUMMON", "TIME", "TOPIC", "TRACE", "USER",
 "USERHOST", "USERS", "VERSION", "WALLOPS", "WHO", "WHOIS", "WHOWAS"};
#define DO_JOIN 12
#define DO_MSG 20
#define DO_PART 26
#define DO_PRIVMSG 30
#define DO_QUIT 31
static int numargs[LISTSIZE] =
{
    15, 1, 15, 3, 15, 15, 15, 1, 15, 15, 15, 15, 15, 3, 2, 15, 15, 15,
    15, 15, 2, 1, 1, 2, 2, 15, 15, 1, 1, 1, 2, 1, 15, 15, 15, 2, 15,
    15, 15, 2, 15, 4, 15, 15, 15, 1, 15, 15, 15
};
static int (*docommand[LISTSIZE]) () =
{
    nop, nop, nop, nop, nop, nop, doerror, nop, nop, nop, doinvite,
	nop, dojoin, dokick, dokill, nop, nop, nop, domode, nop, nop, nop,
	donick, nop, donotice, nop, dopart, nop, nop, dopong, doprivmsg,
	doquit, nop, nop, nop, dosquit, nop, nop, dotime, dotopic, nop, nop,
	nop, nop, nop, nop, nop, nop, nop
};
int wordwrapout(p, count)
char *p;
int count;
{
    while (p != NULL) {
	if ((tmp = strchr(p, ' ')))
	    *(tmp++) = '\0';
	if (strlen(p) < CO - count)
	    count += printf(" %s", p);
	else
	    count = printf("\n\r   %s", p);
	p = tmp;
    }
    return count;
}
int parsedata()
{
    int i, found = 0;

    if (!dumb)
	tputs_x(tgoto(CM, 0, LI - 3));
    TOK[i = 0] = serverdata;
    TOK[i]++;
    while (TOK[i] != NULL && i < 15)
	if (*TOK[i] == ':')
	    break;
	else {
	    if ((tmp = strchr(TOK[i], ' '))) {
		TOK[++i] = &tmp[1];
		*tmp = '\0';
	    } else
		TOK[++i] = NULL;
	}
    if (TOK[i] != NULL && *TOK[i] == ':')
	TOK[i]++;
    TOK[++i] = NULL;
    if ((tmp = strchr(TOK[0], '!'))) {
	fromhost = &tmp[1];
	*tmp = '\0';
    } else
	fromhost = NULL;
    if (!dumb)
	putchar('\n');
    column = 0;
    if (serverdata[0] == 'P') {
	sprintf(lineout, "PONG :%s\n", TOK[1]);
	return sendline();
    }
    if ((i = atoi(TOK[1])))
	i = donumeric(i);
    else {
	for (i = 0; i < LISTSIZE && !found; i++)
	    found = (strcmp(clist[i], TOK[1]) == 0);
	if (found)
	    i = (*docommand[i - 1]) ();
	else
	    i = nop();
    }
    if (i) {
	if (*TOK[i] == ASCIIHEXCHAR && TOK[i + 1] == NULL) {
	    hexascii(&TOK[i][1]);
#ifdef USE_ANSICOLOR
	    printf("\E[1m");
#endif
	    wordwrapout(encoded);
#ifdef USE_ANSICOLOR
	    printf("\E[m");
#endif
	} else {
	    while (TOK[i])
		column = wordwrapout(TOK[i++], column);
#ifdef USE_ANSICOLOR
	    printf("\E[m");
#endif
	}
    }
    if (dumb)
	putchar('\n');
    if (strncmp(TOK[1], "Closing", 7) == 0)
	return (reconnect = 0);
    return 1;
}
int serverinput()
{
    int count, i;
    if ((count = read(my_tcp, ib, IB_SIZE)) < 1)
	return 0;
    for (i = 0; i < count; i++)
	if (ib[i] == '\n') {
	    serverdata[cursd] = '\0';
	    cursd = 0;
	    if (!parsedata())
		return 0;
	} else if (ib[i] != '\r')
	    serverdata[cursd++] = ib[i];
    return 1;
}
void parseinput()
{
    int i, j, outcol, c, found = 0;
    if (*linein == '\0')
	return;
    strcpy(inputbuf, linein);
    TOK[i = 0] = inputbuf;
    while (TOK[i] != NULL && i < 5)
	if ((tmp = strchr(TOK[i], ' '))) {
	    TOK[++i] = &tmp[1];
	    *tmp = '\0';
	} else
	    TOK[++i] = NULL;
    TOK[++i] = NULL;
    if (!dumb) {
	tputs_x(tgoto(CM, 0, LI - 3));
	putchar('\n');
    }
    if (*TOK[0] == COMMANDCHAR) {
	TOK[0]++;
	for (i = 0; i < strlen(TOK[0]) && isalpha(TOK[0][i]); i++)
	    TOK[0][i] = toupper(TOK[0][i]);
	for (i = 0; i < LISTSIZE && !found; i++)
	    found = (strncmp(clist[i], TOK[0], strlen(TOK[0])) == 0);
	i--;
#ifdef AUTOJOIN
	if (!found || i == DO_JOIN || i == DO_PART) {
#else
	if (!found) {
#endif
	    printf("*** Invalid command");
	    return;
	}
#ifndef AUTOJOIN
	if (i == DO_JOIN) {
	    if ((newobj = finditem(TOK[1], olist)) != NULL) {
		wasdate = 0;
		obj = newobj;
		printf("*** Now talking in %s", OBJ);
		return;
	    } else if (!ischan(TOK[1])) {
		obj = olist = additem(TOK[1], olist);
		printf("*** Now talking to %s", OBJ);
		wasdate = 0;
		return;
	    }
	}
	if (i == DO_PART && !ischan(TOK[1]))
	    if ((newobj = finditem(TOK[1], olist)) != NULL) {
		wasdate = 0;
		olist = delitem(TOK[1], olist);
		if (obj == NULL)
		    obj = olist;
		printf("*** No longer talking to %s", TOK[1]);
		if (obj != NULL)
		    printf(", now %s", OBJ);
		wasdate = 0;
		return;
	    }
#endif
	if (i == DO_MSG)
	    i = DO_PRIVMSG;
	if (i == DO_PRIVMSG && (TOK[1] == NULL || TOK[2] == NULL)) {
	    printf("*** Unable to parse message");
	    return;
	}
	strcpy(lineout, clist[i]);
	if (i == DO_QUIT)
	    reconnect = 0;
	if (i == DO_QUIT && TOK[1] == NULL)
	    strcat(lineout, " :" RELEASE);
	j = 0;
	if (i != DO_PRIVMSG || TOK[1] == NULL)
	    outcol = printf("= %s", lineout);
	else if (ischan(TOK[1]))
	    outcol = printf(">%s>", TOK[1]);
	else {
#ifdef USE_ANSICOLOR
	    printf("\E[1m");
#endif
	    outcol = printf("-> *%s*", TOK[1]);
	}
	while (TOK[++j]) {
	    c = strlen(lineout);
	    sprintf(&lineout[c], "%s%s", ((j == numargs[i] &&
			      TOK[j + 1] != NULL) ? " :" : " "), TOK[j]);
	    if (j > 1 || i != DO_PRIVMSG)
		outcol = wordwrapout(TOK[j], outcol);
#ifdef USE_ANSICOLOR
	    printf("\E[m");
#endif
	}
	strcat(lineout, "\n");
    } else {
	if (obj == NULL) {
	    printf("*** Nowhere to send");
	    return;
	}
	if (*TOK[0] == ASCIIHEXCHAR) {
	    asciihex(&linein[1]);
	    strcpy(&linein[1], encoded);
	} else if (*TOK[0] == HEXASCIICHAR) {
	    /* display decoded line */
	    hexascii(&linein[1]);
	    outcol = wordwrapout(encoded);
	    return;
	}
	sprintf(lineout, "PRIVMSG %s :%s\n", OBJ, linein);
	outcol = printf("> %s", TOK[j = 0]);
	while (TOK[++j])
	    outcol = wordwrapout(TOK[j], outcol);
    }
    sendline();
    idletimer = time(NULL);
}
void mypchar(c)
char c;
{
    if (c >= ' ')
	putchar(c);
    else {
	tputs_x(SO);
	putchar(c + 64);
	tputs_x(SE);
    }
}
void ppart()
{
    int i, x = (curx / CO) * CO;
    tputs_x(tgoto(CM, 0, LI - 1));
    for (i = x; i < x + CO && i < curli; i++)
	mypchar(linein[i]);
    tputs_x(CE);
    tputs_x(tgoto(CM, curx % CO, LI - 1));
}
void histupdate()
{
    linein = hist[hline];
    curx = curli = strlen(linein);
    ppart();
}

void userinput()
{
    int i, z;
    if (dumb) {
	fgets(linein, 500, stdin);
	tmp = strchr(linein, '\n');
	if (tmp != NULL)
	    *tmp = '\0';
	parseinput();
	putchar('\n');
    } else {
	read(my_tty, &ch, 1);
	if (ch == '\177')
	    ch = '\10';
	switch (ch) {
	case '\3':
	    raise_sig(SIGINT);
	    break;
	case '\0':
	    if (curx >= CO) {
		curx = 0;
		ppart();
	    } else
		tputs_x(tgoto(CM, curx = 0, LI - 1));
	    break;
	case '\4':
	case '\10':
	    if (curx) {
		if (ch == '\4' && curx < curli)
		    curx++;
		if (curli == curx)
		    linein[(--curx)] = '\0';
		else
		    for (i = (--curx); i < curli; i++)
			linein[i] = linein[i + 1];
		curli--;
		if (DC != NULL && curx % CO != CO - 1) {
		    tputs_x(tgoto(CM, curx % CO, LI - 1));
		    tputs_x(DC);
		} else
		    ppart();
	    }
	    break;
	case '\2':
	    if (curx > 0)
		curx--;
	    if (curx % CO == CO - 1)
		ppart();
	    else
		tputs_x(tgoto(CM, curx % CO, LI - 1));
	    break;
	case '\5':
	    curx = curli;
	case '\14':
	    ppart();
	    break;
	case '\6':
	    if (curx < curli)
		curx++;
	    tputs_x(tgoto(CM, curx % CO, LI - 1));
	    break;
	case '\16':
	    if ((++hline) >= HISTLEN)
		hline = 0;
	    histupdate();
	    break;
	case '\20':
	    if ((--hline) < 0)
		hline = HISTLEN - 1;
	    histupdate();
	    break;
	case '\r':
	case '\n':
	    if (!curli)
		return;
	    tputs_x(tgoto(CM, 0, LI - 1));
	    tputs_x(CE);
	    parseinput();
	    if ((++hline) >= HISTLEN)
		hline = 0;
	    curx = curli = 0;
	    linein = hist[hline];
	    break;
	case '\27':
	    if (obj == NULL)
		break;
	    obj = obj->next;
	    if (obj == NULL)
		obj = olist;
	    wasdate = 0;
	    break;
	case '\32':
	    raise_sig(SIGTSTP);
	    break;
	default:
	    if (curli < 499) {
		if (curli == curx) {
		    linein[++curli] = '\0';
		    linein[curx++] = ch;
		    mypchar(ch);
		    tputs_x(CE);
		} else {
		    for (i = (++curli); i >= curx; i--)
			linein[i + 1] = linein[i];
		    linein[curx] = ch;
		    for (i = (curx % CO); i < CO &&
			 (z = (curx / CO) * CO + i) < curli; i++)
			mypchar(linein[z]);
		    tputs_x(CE);
		    curx++;
		}
	    }
	    break;
	}
    }
}
void cleanup(sig)
int sig;
{
    if (!dumb) {
	resetty();
	tputs_x(tgoto(CS, -1, -1));
	tputs_x(tgoto(CM, 0, LI - 1));
	fflush(stdout);
    }
#ifndef __hpux
    psignal(sig, "tinyirc");
#endif
    if (sig != SIGTSTP)
	exit(128 + sig);
    raise_sig(SIGSTOP);
}
void stopin()
{
    signal(SIGTTIN, stopin);
    noinput = 1;
}

void redraw()
{
    signal(SIGCONT, redraw);
    signal(SIGTSTP, cleanup);
    if (!dumb) {
	if (noinput) {
	    raw();
#ifdef CURSES
	    nonl();
	    noecho();
#endif
	}
	wasdate = 0;
	tputs_x(tgoto(CS, LI - 3, 0));
	updatestatus();
	tputs_x(tgoto(CM, LI - 3, 0));
    }
    noinput = 0;
}
int makeconnect(hostname)
char *hostname;
{
    struct sockaddr_in sa;
    struct hostent *hp;
    int s, t;
    if ((hp = gethostbyname(hostname)) == NULL)
	return -1;
    for (t = 0, s = -1; s < 0 && hp->h_addr_list[t] != NULL; t++) {
	bzero(&sa, sizeof(sa));
	bcopy(hp->h_addr_list[t], (char *) &sa.sin_addr, hp->h_length);
	sa.sin_family = hp->h_addrtype;
	sa.sin_port = htons((unsigned short) IRCPORT);
	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
	if (s > 0) {
	    if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
		close(s);
		s = -1;
	    } else {
		fcntl(s, F_SETFL, O_NDELAY);
		my_tcp = s;
		sprintf(lineout, "USER %s * * :%s\n", IRCLOGIN, IRCGECOS);
		sendline();
		sprintf(lineout, "NICK :%s\n", IRCNAME);
		sendline();
#ifdef AUTOJOIN
		sprintf(lineout, AUTOJOIN);
		sendline();
#endif
		for (obj = olist; obj != NULL; obj = olist->next) {
		    sprintf(lineout, "JOIN %s\n", OBJ);
		    sendline();
		}		/* error checking will be done later */
	    }
	}
    }
    return s;
}
int main(argc, argv)
int argc;
char **argv;
{
    struct utmp ut, *utmp;
    char hostname[64];
    int i = 0;
    printf("%s Copyright (C) 1991-1996 Nathan Laredo\n\
This is free software with ABSOLUTELY NO WARRANTY.\n\
For details please see the file COPYING.\n", RELEASE);
    if (!(tmp = (char *) getenv("IRCSERVER")))
	strcpy(hostname, DEFAULTSERVER);
    else {
	while (*tmp && *tmp != ':')
	    hostname[i++] = *(tmp++);
	hostname[i] = '\0';
	if (*tmp == ':')
	    IRCPORT = (unsigned short) atoi(++tmp);
    }
    if (argc > 1) {
	for (i = 1; i < argc; i++)
	    if (argv[i][0] == '-') {
		if (argv[i][1] == 'd')
		    dumb = 1;
		else {
		    fprintf(stderr, "usage: %s %s\n", argv[0],
			    "[nick] [server] [port] [-dumb]");
		    exit(1);
		}
	    } else if (strchr(argv[i], '.'))
		strcpy(hostname, argv[i]);
	    else if (atoi(argv[i]) > 255)
		IRCPORT = atoi(argv[i]);
	    else
		strncpy(IRCNAME, argv[i], sizeof(IRCNAME));
    }
    if ((my_tty = open("/dev/tty", O_RDWR, 0)) == -1)
	my_tty = fileno(stdin);
    IRCGECOS[i = 63] = 0;
/*  if (!getpeername(my_tty, IRCGECOS, &i)) { // inetd
	strcpy(IRCNAME, IRCGECOS);
	strcpy(IRCLOGIN, "fromident");
	setenv("TERM", "vt102", 1);
    } else { */
	userinfo = getpwuid(getuid());
	tmp = (char *) getenv("IRCNICK");
	if (tmp == NULL)
	    strncpy(IRCNAME, userinfo->pw_name, sizeof(IRCNAME));
	else
	    strncpy(IRCNAME, tmp, sizeof(IRCNAME));
	strcpy(IRCLOGIN, userinfo->pw_name);
	setutent();
	strcpy(ut.ut_line, isatty(0) ? strrchr(ttyname(0), '/') + 1 : "");
	if ((utmp = getutline(&ut)) == NULL || !(utmp->ut_addr) ||
	    *((char *) utmp->ut_host) == ':' /* X connection */ )
	    tmp = userinfo->pw_gecos;
	else {
	    struct hostent *h;
	    struct in_addr a;
	    a.s_addr = utmp->ut_addr;
	    if (!(h = gethostbyaddr((char *) &a.s_addr,
				    sizeof(a.s_addr), AF_INET)))
		tmp = (char *) inet_ntoa(a);
	    else
		tmp = (char *) h->h_name;
	}
	strcpy(IRCGECOS, tmp);
	if ((tmp = strchr(IRCGECOS, ','))) *tmp = '\0';
	endutent();
/*  } */
    fprintf(stderr, "*** User is %s\n", IRCGECOS);
    printf("*** trying port %d of %s\n\n", IRCPORT, hostname);
    if (makeconnect(hostname) < 0) {
	fprintf(stderr, "*** %s refused connection, aborting\n", hostname);
	exit(0);
    }
    idletimer = time(NULL);
    ptr = termcap;
    if ((term = (char *) getenv("TERM")) == NULL) {
	fprintf(stderr, "tinyirc: TERM not set\n");
	exit(1);
    }
    if (tgetent(bp, term) < 1) {
	fprintf(stderr, "tinyirc: no termcap entry for %s\n", term);
	exit(1);
    }
    if ((CO = tgetnum("co") - 2) < 20)
	CO = 78;
    if ((LI = tgetnum("li")) == -1)
	LI = 24;
    if (!dumb) {
#define tgs(x) ((char *) tgetstr(x, &ptr))
	if ((CM = tgs("cm")) == NULL)
	    CM = tgs("CM");
	if ((SO = tgs("so")) == NULL)
	    SO = "";
	if ((SE = tgs("se")) == NULL)
	    SE = "";
	if (!CM || !(CS = tgs("cs")) ||
	    !(CE = tgs("ce"))) {
	    printf("tinyirc: sorry, no termcap cm,cs,ce: dumb mode set\n");
	    dumb = 1;
	}
	if (!dumb) {
	    DC = tgs("dc");
	    savetty();
	    raw();
#ifdef CURSES
	    nonl();
	    noecho();
#endif
	}
    }
    redraw();
    signal(SIGINT, cleanup);
    signal(SIGHUP, cleanup);
    signal(SIGTERM, cleanup);
    signal(SIGSEGV, cleanup);
    signal(SIGTTIN, stopin);
    for (i = 0; i < HISTLEN; i++)
	hist[i] = (char *) calloc(512, sizeof(char));
    linein = hist[hline = 0];
    while (sok) {
	FD_ZERO(&readfs);
	FD_SET(my_tcp, &readfs);
	if (!noinput)
	    FD_SET(my_tty, &readfs);
	if (!dumb) {
	    timeout.tv_sec = 61;
	    timeout.tv_usec = 0;
	}
	if (select(FD_SETSIZE, &readfs, NULL, NULL, (dumb ? NULL : &timeout))) {
	    if (FD_ISSET(my_tty, &readfs))
		userinput();
	    if (FD_ISSET(my_tcp, &readfs))
		sok = serverinput();
	    if (!wasdate)
		updatestatus();
	} else
	    updatestatus();
	if (!sok && reconnect) {
	    close(my_tcp);	/* dead socket */
	    printf("*** trying port %d of %s\n\n", IRCPORT, hostname);
	    if (makeconnect(hostname) < 0) {
		fprintf(stderr, "*** %s refused connection\n", hostname);
		exit(0);
	    }
	    sok++;
	}
	if (!dumb)
	    tputs_x(tgoto(CM, curx % CO, LI - 1));
	fflush(stdout);
    }
    if (!dumb) {
	tputs_x(tgoto(CS, -1, -1));
	tputs_x(tgoto(CM, 0, LI - 1));
#ifdef CURSES
	echo();
	nl();
#endif
	resetty();
    }
    exit(0);
}
/* EOF */