/
misc.c
1582 lines (1447 loc) · 39.5 KB
/
misc.c
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
/*
* misc.c -- handles:
* split() maskhost() dumplots() daysago() days() daysdur()
* logging things
* queueing output for the bot (msg and help)
* resync buffers for sharebots
* help system
* motd display and %var substitution
*/
/*
* Copyright (C) 1997 Robey Pointer
* Copyright (C) 1999 - 2018 Eggheads Development Team
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "main.h"
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "chan.h"
#include "tandem.h"
#include "modules.h"
#ifdef HAVE_UNAME
# include <sys/utsname.h>
#endif
#include "stat.h"
extern struct dcc_t *dcc;
extern struct chanset_t *chanset;
extern char helpdir[], version[], origbotname[], botname[], admin[], network[],
motdfile[], ver[], botnetnick[], bannerfile[], textdir[];
extern int backgrd, con_chan, term_z, use_stderr, dcc_total, keep_all_logs,
quick_logs, strict_host;
extern time_t now;
extern Tcl_Interp *interp;
char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix */
char log_ts[33] = "[%H:%M:%S]"; /* Timestamp format for logfile entries */
int shtime = 1; /* Display the time with console output */
log_t *logs = 0; /* Logfiles */
int max_logs = 5; /* Max log files, mismatch config on purpose */
int max_logsize = 0; /* Maximum logfile size, 0 for no limit */
int raw_log = 0; /* Display output to server to LOG_SERVEROUT */
int conmask = LOG_MODES | LOG_CMDS | LOG_MISC; /* Console mask */
struct help_list_t {
struct help_list_t *next;
char *name;
int type;
};
static struct help_ref {
char *name;
struct help_list_t *first;
struct help_ref *next;
} *help_list = NULL;
/* Expected memory usage
*/
int expmem_misc()
{
struct help_ref *current;
struct help_list_t *item;
int tot = 0;
for (current = help_list; current; current = current->next) {
tot += sizeof(struct help_ref) + strlen(current->name) + 1;
for (item = current->first; item; item = item->next)
tot += sizeof(struct help_list_t) + strlen(item->name) + 1;
}
return tot + (max_logs * sizeof(log_t));
}
void init_misc()
{
static int last = 0;
if (max_logs < 1)
max_logs = 1;
if (logs)
logs = nrealloc(logs, max_logs * sizeof(log_t));
else
logs = nmalloc(max_logs * sizeof(log_t));
for (; last < max_logs; last++) {
logs[last].filename = logs[last].chname = NULL;
logs[last].mask = 0;
logs[last].f = NULL;
/* Added by cybah */
logs[last].szlast[0] = 0;
logs[last].repeats = 0;
/* Added by rtc */
logs[last].flags = 0;
}
}
/*
* Misc functions
*/
/* low-level stuff for other modules
*/
int is_file(const char *s)
{
struct stat ss;
int i = stat(s, &ss);
if (i < 0)
return 0;
if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
return 1;
return 0;
}
/* This implementation wont overrun dst - 'max' is the max bytes that dst
* can be, including the null terminator. So if 'dst' is a 128 byte buffer,
* pass 128 as 'max'. The function will _always_ null-terminate 'dst'.
*
* Returns: The number of characters appended to 'dst'.
*
* Usage example:
*
* char buf[128];
* size_t bufsize = sizeof(buf);
*
* buf[0] = 0, bufsize--;
*
* while (blah && bufsize) {
* bufsize -= egg_strcatn(buf, <some-long-string>, sizeof(buf));
* }
*
* <Cybah>
*/
int egg_strcatn(char *dst, const char *src, size_t max)
{
size_t tmpmax = 0;
/* find end of 'dst' */
while (*dst && max > 0) {
dst++;
max--;
}
/* Store 'max', so we can use it to workout how many characters were
* written later on.
*/
tmpmax = max;
/* copy up to, but not including the null terminator */
while (*src && max > 1) {
*dst++ = *src++;
max--;
}
/* null-terminate the buffer */
*dst = 0;
/* Don't include the terminating null in our count, as it will cumulate
* in loops - causing a headache for the caller.
*/
return tmpmax - max;
}
int my_strcpy(char *a, char *b)
{
char *c = b;
while (*b)
*a++ = *b++;
*a = *b;
return b - c;
}
/* Split first word off of rest and put it in first
*/
void splitc(char *first, char *rest, char divider)
{
char *p = strchr(rest, divider);
if (p == NULL) {
if (first != rest && first)
first[0] = 0;
return;
}
*p = 0;
if (first != NULL)
strcpy(first, rest);
if (first != rest)
/* In most circumstances, strcpy with src and dst being the same buffer
* can produce undefined results. We're safe here, as the src is
* guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
*/
strcpy(rest, p + 1);
}
/* As above, but lets you specify the 'max' number of bytes (EXCLUDING the
* terminating null).
*
* Example of use:
*
* char buf[HANDLEN + 1];
*
* splitcn(buf, input, "@", HANDLEN);
*
* <Cybah>
*/
void splitcn(char *first, char *rest, char divider, size_t max)
{
char *p = strchr(rest, divider);
if (p == NULL) {
if (first != rest && first)
first[0] = 0;
return;
}
*p = 0;
if (first != NULL)
strlcpy(first, rest, max);
if (first != rest)
/* In most circumstances, strcpy with src and dst being the same buffer
* can produce undefined results. We're safe here, as the src is
* guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
*/
strcpy(rest, p + 1);
}
char *splitnick(char **blah)
{
char *p = strchr(*blah, '!'), *q = *blah;
if (p) {
*p = 0;
*blah = p + 1;
return q;
}
return "";
}
void remove_crlf(char **line)
{
char *p = *line;
while (*p) {
if (*p == '\r' || *p == '\n')
{
*p = 0;
break;
}
p++;
}
}
char *newsplit(char **rest)
{
char *o, *r;
if (!rest)
return "";
o = *rest;
while (*o == ' ')
o++;
r = o;
while (*o && (*o != ' '))
o++;
if (*o)
*o++ = 0;
*rest = o;
return r;
}
/* maskhost(), modified to support custom mask types, as defined
* by mIRC.
* Does not require a proper hostmask in 's'. Accepts any strings,
* including empty ones and attempts to provide meaningful results.
*
* Strings containing no '@' character will be parsed as if the
* whole string is a host.
* Strings containing no '!' character will be interpreted as if
* there is no nick.
* '!' as a nick/user separator must precede any '@' characters.
* Otherwise it will be considered a part of the host.
* Supported types are listed in tcl-commands.doc in the maskhost
* command section. Type 3 resembles the older maskhost() most closely.
*
* Specific examples (with type=3):
*
* "nick!user@is.the.lamest.bg" -> *!*user@*.the.lamest.bg (ccTLD)
* "nick!user@is.the.lamest.com" -> *!*user@*.lamest.com (gTLD)
* "lamest.example" -> *!*@lamest.example
* "whatever@lamest.example" -> *!*whatever@lamest.example
* "com.example@user!nick" -> *!*com.example@user!nick
* "!" -> *!*@!
* "@" -> *!*@*
* "" -> *!*@*
* "abc!user@2001:db8:618:5c0:263:15:dead:babe"
* -> *!*user@2001:db8:618:5c0:263:15:dead:*
* "abc!user@0:0:0:0:0:ffff:1.2.3.4"
* -> *!*user@0:0:0:0:0:ffff:1.2.3.*
*/
void maskaddr(const char *s, char *nw, int type)
{
int d = type % 5, num = 1;
char *p, *u = 0, *h = 0, *ss;
/* Look for user and host.. */
ss = (char *)s;
u = strchr(s, '!');
if (u)
h = strchr(u, '@');
if (!h){
h = strchr(s, '@');
u = 0;
}
/* Print nick if required and available */
if (!u || (type % 10) < 5)
*nw++ = '*';
else {
strncpy(nw, s, u - s);
nw += u - s;
}
*nw++ = '!';
/* Write user if required and available */
u = (u ? u + 1 : ss);
if (!h || (d == 2) || (d == 4))
*nw++ = '*';
else {
if (d) {
*nw++ = '*';
if (strchr("~+-^=", *u))
u++; /* trim leading crap */
}
strncpy(nw, u, h - u);
nw += h - u;
}
*nw++ = '@';
if (type >= 30) {
strcpy(nw, "*");
return;
}
/* The rest is for the host */
h = (h ? h + 1 : ss);
for (p = h; *p; p++) /* hostname? */
if ((*p > '9' || *p < '0') && *p != '.') {
num = 0;
break;
}
p = strrchr(h, ':'); /* IPv6? */
/* Mask out after the last colon/dot */
if (p && d > 2) {
if ((u = strrchr(p, '.')))
p = u;
strncpy(nw, h, ++p - h);
nw += p - h;
strcpy(nw, "*");
} else if (!p && !num && type >= 10) {
/* we have a hostname and type
requires us to replace numbers */
num = 0;
for (p = h; *p; p++) {
if (*p < '0' || *p > '9') {
*nw++ = *p;
num = 0;
} else {
if (type < 20)
*nw++ = '?';
else if (!num) {
*nw++ = '*';
num = 1; /* place only one '*'
per numeric sequence */
}
}
}
*nw = 0;
} else if (d > 2 && (p = strrchr(h, '.'))) {
if (num) { /* IPv4 */
strncpy(nw, h, p - h);
nw += p - h;
strcpy(nw, ".*");
return;
}
for (u = h, d = 0; (u = strchr(++u, '.')); d++);
if (d < 2) { /* types < 2 don't mask the host */
strcpy(nw, h);
return;
}
u = strchr(h, '.');
if (d > 3 || (d == 3 && strlen(p) > 3))
u = strchr(++u, '.'); /* ccTLD or not? Look above. */
sprintf(nw, "*%s", u);
} else if (!*h)
/* take care if the mask is empty or contains only '@' */
strcpy(nw, "*");
else
strcpy(nw, h);
}
/* Dump a potentially super-long string of text.
*/
void dumplots(int idx, const char *prefix, const char *data)
{
const char *p = data, *q, *n;
const int max_data_len = 500 - strlen(prefix);
if (!*data) {
dprintf(idx, "%s\n", prefix);
return;
}
while (strlen(p) > max_data_len) {
q = p + max_data_len;
/* Search for embedded linefeed first */
n = strchr(p, '\n');
if (n && n < q) {
/* Great! dump that first line then start over */
dprintf(idx, "%s%.*s\n", prefix, n - p, p);
p = n + 1;
} else {
/* Search backwards for the last space */
while (*q != ' ' && q != p)
q--;
if (q == p)
q = p + max_data_len;
dprintf(idx, "%s%.*s\n", prefix, q - p, p);
p = q;
if (*q == ' ')
p++;
}
}
/* Last trailing bit: split by linefeeds if possible */
n = strchr(p, '\n');
while (n) {
dprintf(idx, "%s%.*s\n", prefix, n - p, p);
p = n + 1;
n = strchr(p, '\n');
}
if (*p)
dprintf(idx, "%s%s\n", prefix, p); /* Last trailing bit */
}
/* Convert an interval (in seconds) to one of:
* "19 days ago", "1 day ago", "18:12"
*/
void daysago(time_t now, time_t then, char *out)
{
if (now - then > 86400) {
int days = (now - then) / 86400;
sprintf(out, "%d day%s ago", days, (days == 1) ? "" : "s");
return;
}
egg_strftime(out, 6, "%H:%M", localtime(&then));
}
/* Convert an interval (in seconds) to one of:
* "in 19 days", "in 1 day", "at 18:12"
*/
void days(time_t now, time_t then, char *out)
{
if (now - then > 86400) {
int days = (now - then) / 86400;
sprintf(out, "in %d day%s", days, (days == 1) ? "" : "s");
return;
}
egg_strftime(out, 9, "at %H:%M", localtime(&now));
}
/* Convert an interval (in seconds) to one of:
* "for 19 days", "for 1 day", "for 09:10"
*/
void daysdur(time_t now, time_t then, char *out)
{
char s[81];
int hrs, mins;
if (now - then > 86400) {
int days = (now - then) / 86400;
sprintf(out, "for %d day%s", days, (days == 1) ? "" : "s");
return;
}
strcpy(out, "for ");
now -= then;
hrs = (int) (now / 3600);
mins = (int) ((now - (hrs * 3600)) / 60);
sprintf(s, "%02d:%02d", hrs, mins);
strcat(out, s);
}
/*
* Logging functions
*/
/* Log something
* putlog(level,channel_name,format,...);
*/
void putlog EGG_VARARGS_DEF(int, arg1)
{
static int inhere = 0;
int i, type, tsl = 0;
char *format, *chname, s[LOGLINELEN], s1[256], *out, ct[81], *s2, stamp[34];
va_list va;
time_t now2 = time(NULL);
struct tm *t = localtime(&now2);
type = EGG_VARARGS_START(int, arg1, va);
chname = va_arg(va, char *);
format = va_arg(va, char *);
/* Create the timestamp */
if (shtime) {
egg_strftime(stamp, sizeof(stamp) - 2, log_ts, t);
strcat(stamp, " ");
tsl = strlen(stamp);
}
else
*stamp = '\0';
/* Format log entry at offset 'tsl,' then i can prepend the timestamp */
out = s + tsl;
/* No need to check if out should be null-terminated here,
* just do it! <cybah>
*/
egg_vsnprintf(out, LOGLINEMAX - tsl, format, va);
out[LOGLINEMAX - tsl] = 0;
if (keep_all_logs) {
if (!logfile_suffix[0])
egg_strftime(ct, 12, ".%d%b%Y", t);
else {
egg_strftime(ct, 80, logfile_suffix, t);
ct[80] = 0;
s2 = ct;
/* replace spaces by underscores */
while (s2[0]) {
if (s2[0] == ' ')
s2[0] = '_';
s2++;
}
}
}
/* Make sure the bind list is initialized and we're not looping here */
if (!inhere && H_log) {
inhere = 1;
check_tcl_log(type, chname, out);
inhere = 0;
}
/* Place the timestamp in the string to be printed */
if (out[0] && shtime) {
memcpy(s, stamp, tsl);
out = s;
}
strcat(out, "\n");
if (!use_stderr) {
for (i = 0; i < max_logs; i++) {
if ((logs[i].filename != NULL) && (logs[i].mask & type) &&
((chname[0] == '*') || (logs[i].chname[0] == '*') ||
(!rfc_casecmp(chname, logs[i].chname)))) {
if (logs[i].f == NULL) {
/* Open this logfile */
if (keep_all_logs) {
egg_snprintf(s1, 256, "%s%s", logs[i].filename, ct);
logs[i].f = fopen(s1, "a");
} else
logs[i].f = fopen(logs[i].filename, "a");
}
if (logs[i].f != NULL) {
/* Check if this is the same as the last line added to
* the log. <cybah>
*/
if (!egg_strcasecmp(out + tsl, logs[i].szlast))
/* It is a repeat, so increment repeats */
logs[i].repeats++;
else {
/* Not a repeat, check if there were any repeat
* lines previously...
*/
if (logs[i].repeats > 0) {
/* Yep.. so display 'last message repeated x times'
* then reset repeats. We want the current time here,
* so put that in the file first.
*/
fprintf(logs[i].f, "%s", stamp);
fprintf(logs[i].f, MISC_LOGREPEAT, logs[i].repeats);
logs[i].repeats = 0;
/* No need to reset logs[i].szlast here
* because we update it later on...
*/
}
fputs(out, logs[i].f);
strlcpy(logs[i].szlast, out + tsl, LOGLINEMAX);
}
}
}
}
}
for (i = 0; i < dcc_total; i++) {
if ((dcc[i].type == &DCC_CHAT) && (dcc[i].u.chat->con_flags & type)) {
if ((chname[0] == '*') || (dcc[i].u.chat->con_chan[0] == '*') ||
!rfc_casecmp(chname, dcc[i].u.chat->con_chan)) {
dprintf(i, "%s", out);
}
}
}
if (!backgrd && !con_chan && term_z < 0)
dprintf(DP_STDOUT, "%s", out);
else if ((type & LOG_MISC) && use_stderr) {
if (shtime)
out += tsl;
dprintf(DP_STDERR, "%s", out);
}
va_end(va);
}
/* Called as soon as the logfile suffix changes. All logs are closed
* and the new suffix is stored in `logfile_suffix'.
*/
void logsuffix_change(char *s)
{
int i;
char *s2 = logfile_suffix;
/* If the suffix didn't really change, ignore. It's probably a rehash. */
if (!s || (s && s2 && !strcmp(s, s2)))
return;
debug0("Logfile suffix changed. Closing all open logs.");
strlcpy(logfile_suffix, s, sizeof logfile_suffix);
while (s2[0]) {
if (s2[0] == ' ')
s2[0] = '_';
s2++;
}
for (i = 0; i < max_logs; i++) {
if (logs[i].f) {
fflush(logs[i].f);
fclose(logs[i].f);
logs[i].f = NULL;
}
}
}
void check_logsize()
{
struct stat ss;
int i;
/* int x=1; */
char buf[1024]; /* Should be plenty */
if (!keep_all_logs && max_logsize > 0) {
for (i = 0; i < max_logs; i++) {
if (logs[i].filename) {
if (stat(logs[i].filename, &ss) != 0) {
break;
}
if ((ss.st_size >> 10) > max_logsize) {
if (logs[i].f) {
/* write to the log before closing it huh.. */
putlog(LOG_MISC, "*", MISC_CLOGS, logs[i].filename, ss.st_size);
fflush(logs[i].f);
fclose(logs[i].f);
logs[i].f = NULL;
}
egg_snprintf(buf, sizeof buf, "%s.yesterday", logs[i].filename);
buf[1023] = 0;
unlink(buf);
movefile(logs[i].filename, buf);
}
}
}
}
}
/* Flush the logfiles to disk
*/
void flushlogs()
{
int i;
/* Logs may not be initialised yet. */
if (!logs)
return;
/* Now also checks to see if there's a repeat message and
* displays the 'last message repeated...' stuff too <cybah>
*/
for (i = 0; i < max_logs; i++) {
if (logs[i].f != NULL) {
if ((logs[i].repeats > 0) && quick_logs) {
/* Repeat.. if quicklogs used then display 'last message
* repeated x times' and reset repeats.
*/
char stamp[33];
egg_strftime(stamp, sizeof(stamp) - 1, log_ts, localtime(&now));
fprintf(logs[i].f, "%s ", stamp);
fprintf(logs[i].f, MISC_LOGREPEAT, logs[i].repeats);
/* Reset repeats */
logs[i].repeats = 0;
}
fflush(logs[i].f);
}
}
}
/*
* String substitution functions
*/
static int cols = 0;
static int colsofar = 0;
static int blind = 0;
static int subwidth = 70;
static char *colstr = NULL;
/* Add string to colstr
*/
static void subst_addcol(char *s, char *newcol)
{
char *p, *q;
int i, colwidth;
if ((newcol[0]) && (newcol[0] != '\377'))
colsofar++;
colstr = nrealloc(colstr, strlen(colstr) + strlen(newcol) +
(colstr[0] ? 2 : 1));
if ((newcol[0]) && (newcol[0] != '\377')) {
if (colstr[0])
strcat(colstr, "\377");
strcat(colstr, newcol);
}
if ((colsofar == cols) || ((newcol[0] == '\377') && (colstr[0]))) {
colsofar = 0;
strcpy(s, " ");
colwidth = (subwidth - 5) / cols;
q = colstr;
p = strchr(colstr, '\377');
while (p != NULL) {
*p = 0;
strcat(s, q);
for (i = strlen(q); i < colwidth; i++)
strcat(s, " ");
q = p + 1;
p = strchr(q, '\377');
}
strcat(s, q);
nfree(colstr);
colstr = nmalloc(1);
colstr[0] = 0;
}
}
/* Substitute %x codes in help files
*
* %B = bot nickname
* %V = version
* %C = list of channels i monitor
* %E = eggdrop banner
* %A = admin line
* %n = network name
* %T = current time ("14:15")
* %N = user's nickname
* %U = display system name if possible
* %{+xy} require flags to read this section
* %{-} turn of required flag matching only
* %{center} center this line
* %{cols=N} start of columnated section (indented)
* %{help=TOPIC} start a section for a particular command
* %{end} end of section
*/
#define HELP_BUF_LEN 256
#define HELP_BOLD 1
#define HELP_REV 2
#define HELP_UNDER 4
#define HELP_FLASH 8
void help_subst(char *s, char *nick, struct flag_record *flags,
int isdcc, char *topic)
{
char xx[HELP_BUF_LEN + 1], sub[161], *current, *q, chr, *writeidx,
*readidx, *towrite;
struct chanset_t *chan;
int i, j, center = 0;
static int help_flags;
#ifdef HAVE_UNAME
struct utsname uname_info;
#endif
if (s == NULL) {
/* Used to reset substitutions */
blind = 0;
cols = 0;
subwidth = 70;
if (colstr != NULL) {
nfree(colstr);
colstr = NULL;
}
help_flags = isdcc;
return;
}
strlcpy(xx, s, sizeof xx);
readidx = xx;
writeidx = s;
current = strchr(readidx, '%');
while (current) {
/* Are we about to copy a chuck to the end of the buffer?
* if so return
*/
if ((writeidx + (current - readidx)) >= (s + HELP_BUF_LEN)) {
strncpy(writeidx, readidx, (s + HELP_BUF_LEN) - writeidx);
s[HELP_BUF_LEN] = 0;
return;
}
chr = *(current + 1);
*current = 0;
if (!blind)
writeidx += my_strcpy(writeidx, readidx);
towrite = NULL;
switch (chr) {
case 'b':
if (glob_hilite(*flags)) {
if (help_flags & HELP_IRC) {
towrite = "\002";
} else if (help_flags & HELP_BOLD) {
help_flags &= ~HELP_BOLD;
towrite = "\033[0m";
} else {
help_flags |= HELP_BOLD;
towrite = "\033[1m";
}
}
break;
case 'v':
if (glob_hilite(*flags)) {
if (help_flags & HELP_IRC) {
towrite = "\026";
} else if (help_flags & HELP_REV) {
help_flags &= ~HELP_REV;
towrite = "\033[0m";
} else {
help_flags |= HELP_REV;
towrite = "\033[7m";
}
}
break;
case '_':
if (glob_hilite(*flags)) {
if (help_flags & HELP_IRC) {
towrite = "\037";
} else if (help_flags & HELP_UNDER) {
help_flags &= ~HELP_UNDER;
towrite = "\033[0m";
} else {
help_flags |= HELP_UNDER;
towrite = "\033[4m";
}
}
break;
case 'f':
if (glob_hilite(*flags)) {
if (help_flags & HELP_FLASH) {
if (help_flags & HELP_IRC)
towrite = "\002\037";
else
towrite = "\033[0m";
help_flags &= ~HELP_FLASH;
} else {
help_flags |= HELP_FLASH;
if (help_flags & HELP_IRC)
towrite = "\037\002";
else
towrite = "\033[5m";
}
}
break;
case 'U':
#ifdef HAVE_UNAME
if (uname(&uname_info) >= 0) {
egg_snprintf(sub, sizeof sub, "%s %s", uname_info.sysname,
uname_info.release);
towrite = sub;
} else
#endif
towrite = "*UNKNOWN*";
break;
case 'B':
towrite = (isdcc ? botnetnick : botname);
break;
case 'V':
towrite = ver;
break;
case 'E':
towrite = version;
break;
case 'A':
towrite = admin;
break;
case 'n':
towrite = network;
break;
case 'T':
egg_strftime(sub, 6, "%H:%M", localtime(&now));
towrite = sub;
break;
case 'N':
towrite = strchr(nick, ':');
if (towrite)
towrite++;
else
towrite = nick;
break;
case 'C':
if (!blind)
for (chan = chanset; chan; chan = chan->next) {
if ((strlen(chan->dname) + writeidx + 2) >= (s + HELP_BUF_LEN)) {
strncpy(writeidx, chan->dname, (s + HELP_BUF_LEN) - writeidx);
s[HELP_BUF_LEN] = 0;
return;
}
writeidx += my_strcpy(writeidx, chan->dname);
if (chan->next) {
*writeidx++ = ',';
*writeidx++ = ' ';
}
}
break;
case '{':
q = current;
current++;
while ((*current != '}') && (*current))
current++;
if (*current) {
*current = 0;
current--;
q += 2;
/* Now q is the string and p is where the rest of the fcn expects */
if (!strncmp(q, "help=", 5)) {
if (topic && egg_strcasecmp(q + 5, topic))
blind |= 2;
else
blind &= ~2;
} else if (!(blind & 2)) {
if (q[0] == '+') {
struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
break_down_flags(q + 1, &fr, NULL);
/* We used to check flagrec_ok(), but we can use flagrec_eq()
* instead because lower flags are automatically added now.
*/
if (!flagrec_eq(&fr, flags))
blind |= 1;
else
blind &= ~1;
} else if (q[0] == '-')
blind &= ~1;
else if (!egg_strcasecmp(q, "end")) {
blind &= ~1;
subwidth = 70;
if (cols) {
sub[0] = 0;
subst_addcol(sub, "\377");
nfree(colstr);
colstr = NULL;
cols = 0;
towrite = sub;
}
} else if (!egg_strcasecmp(q, "center"))
center = 1;
else if (!strncmp(q, "cols=", 5)) {