/[cvs]/eggdrop1.4/src/misc.c
ViewVC logotype

Contents of /eggdrop1.4/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Tue Aug 24 07:16:50 1999 UTC (20 years, 1 month ago) by guppy
Branch: MAIN
Changes since 1.7: +2 -0 lines
File MIME type: text/x-chdr
blah

1 /*
2 * misc.c -- handles:
3 * split() maskhost() copyfile() movefile() fixfrom()
4 * dumplots() daysago() days() daysdur()
5 * logging things
6 * queueing output for the bot (msg and help)
7 * resync buffers for sharebots
8 * help system
9 * motd display and %var substitution
10 *
11 * dprintf'ized, 12dec1995
12 */
13 /*
14 * This file is part of the eggdrop source code
15 * copyright (c) 1997 Robey Pointer
16 * and is distributed according to the GNU general public license.
17 * For full details, read the top of 'main.c' or the file called
18 * COPYING that was distributed with this code.
19 */
20
21 #include "main.h"
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include "chan.h"
26 #ifdef HAVE_UNAME
27 #include <sys/utsname.h>
28 #endif
29
30 extern int dcc_total;
31 extern struct dcc_t *dcc;
32 extern char helpdir[];
33 extern char version[];
34 extern char origbotname[];
35 extern char botname[];
36 extern char admin[];
37 extern int backgrd;
38 extern int con_chan;
39 extern int term_z;
40 extern int use_stderr;
41 extern char motdfile[];
42 extern char ver[];
43 extern int keep_all_logs;
44 extern int quick_logs;
45 extern char botnetnick[];
46 extern struct chanset_t *chanset;
47 extern time_t now;
48 extern char bannerfile[];
49
50 int shtime = 1; /* whether or not to display the time
51 * with console output */
52 log_t *logs = 0; /* logfiles */
53 int max_logs = 5; /* current maximum log files */
54 int max_logsize = 0; /* maximum logfile size, 0 for no limit */
55 int conmask = LOG_MODES | LOG_CMDS | LOG_MISC; /* console mask */
56 int debug_output = 0; /* disply output to server to LOG_SERVEROUT */
57
58 struct help_list {
59 struct help_list *next;
60 char *name;
61 int type;
62 };
63
64 static struct help_ref {
65 char *name;
66 struct help_list *first;
67 struct help_ref *next;
68 } *help_list = NULL;
69
70 /* expected memory usage */
71 int expmem_misc()
72 {
73 struct help_ref *current;
74 struct help_list *item;
75 int tot = 0;
76
77 for (current = help_list; current; current = current->next) {
78 tot += sizeof(struct help_ref) + strlen(current->name) + 1;
79
80 for (item = current->first; item; item = item->next)
81 tot += sizeof(struct help_list) + strlen(item->name) + 1;
82 }
83 return tot + (max_logs * sizeof(log_t));
84 }
85
86 void init_misc()
87 {
88 static int last = 0;
89
90 if (max_logs < 1)
91 max_logs = 1;
92 if (logs)
93 logs = nrealloc(logs, max_logs * sizeof(log_t));
94 else
95 logs = nmalloc(max_logs * sizeof(log_t));
96 for (; last < max_logs; last++) {
97 logs[last].filename = logs[last].chname = NULL;
98 logs[last].mask = 0;
99 logs[last].f = NULL;
100 /* Added by cybah */
101 logs[last].szLast[0] = 0;
102 logs[last].Repeats = 0;
103 }
104 }
105
106 /***** MISC FUNCTIONS *****/
107
108 /* low-level stuff for other modules */
109 static int is_file(char *s)
110 {
111 struct stat ss;
112 int i = stat(s, &ss);
113
114 if (i < 0)
115 return 0;
116 if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
117 return 1;
118 return 0;
119 }
120
121 /* unixware has no strcasecmp() without linking in a hefty library */
122 #if !HAVE_STRCASECMP
123 #define upcase(c) (((c)>='a' && (c)<='z') ? (c)-'a'+'A' : (c))
124
125 int strcasecmp(char *s1, char *s2)
126 {
127 while ((*s1) && (*s2) && (upcase(*s1) == upcase(*s2))) {
128 s1++;
129 s2++;
130 }
131 return upcase(*s1) - upcase(*s2);
132 }
133 #endif
134
135 int my_strcpy(char *a, char *b)
136 {
137 char *c = b;
138
139 while (*b)
140 *a++ = *b++;
141 *a = *b;
142 return b - c;
143 }
144
145 /* split first word off of rest and put it in first */
146 void splitc(char *first, char *rest, char divider)
147 {
148 char *p;
149
150 p = strchr(rest, divider);
151 if (p == NULL) {
152 if ((first != rest) && (first != NULL))
153 first[0] = 0;
154 return;
155 }
156 *p = 0;
157 if (first != NULL)
158 strcpy(first, rest);
159 if (first != rest)
160 strcpy(rest, p + 1);
161 }
162
163 char *splitnick(char **blah)
164 {
165 char *p = strchr(*blah, '!'), *q = *blah;
166
167 if (p) {
168 *p = 0;
169 *blah = p + 1;
170 return q;
171 }
172 return "";
173 }
174
175 char *newsplit(char **rest)
176 {
177 register char *o, *r;
178
179 if (!rest)
180 return *rest = "";
181 o = *rest;
182 while (*o == ' ')
183 o++;
184 r = o;
185 while (*o && (*o != ' '))
186 o++;
187 if (*o)
188 *o++ = 0;
189 while (*o && (*o == ' '))
190 o++;
191 *rest = o;
192 return r;
193 }
194
195 /* convert "abc!user@a.b.host" into "*!user@*.b.host"
196 * or "abc!user@1.2.3.4" into "*!user@1.2.3.*" */
197 void maskhost(char *s, char *nw)
198 {
199 char *p, *q, *e, *f;
200 int i;
201
202 *nw++ = '*';
203 *nw++ = '!';
204 p = (q = strchr(s, '!')) ? q + 1 : s;
205 /* strip of any nick, if a username is found, use last 8 chars */
206 if ((q = strchr(p, '@'))) {
207 if ((q - p) > 9) {
208 nw[0] = '*';
209 p = q - 7;
210 i = 1;
211 } else
212 i = 0;
213 while (*p != '@') {
214 if ((*p == '~') || (*p == '+') || (*p == '-'))
215 nw[i] = '?';
216 else
217 nw[i] = *p;
218 p++;
219 i++;
220 }
221 nw[i++] = '@';
222 q++;
223 } else {
224 nw[0] = '*';
225 nw[1] = '@';
226 i = 2;
227 q = s;
228 }
229 nw += i;
230 /* now q points to the hostname, i point to where to put the mask */
231 if (!(p = strchr(q, '.')) || !(e = strchr(p + 1, '.')))
232 /* TLD or 2 part host */
233 strcpy(nw, q);
234 else {
235 for (f = e; *f; f++);
236 f--;
237 if ((*f >= '0') && (*f <= '9')) { /* numeric IP address */
238 while (*f != '.')
239 f--;
240 strncpy(nw, q, f - q);
241 nw += (f - q);
242 strcpy(nw, ".*");
243 } else { /* normal host >= 3 parts */
244 /* ok, people whined at me...how about this? ..
245 * a.b.c -> *.b.c
246 * a.b.c.d -> *.b.c.d if tld is a country (2 chars)
247 * OR *.c.d if tld is com/edu/etc (3 chars)
248 * a.b.c.d.e -> *.c.d.e etc
249 */
250 char *x = strchr(e + 1, '.');
251
252 if (!x)
253 x = p;
254 else if (strchr(x + 1, '.'))
255 x = e;
256 else if (strlen(x) == 3)
257 x = p;
258 else
259 x = e;
260 sprintf(nw, "*%s", x);
261 }
262 }
263 }
264
265 /* copy a file from one place to another (possibly erasing old copy) */
266 /* returns 0 if OK, 1 if can't open original file, 2 if can't open new */
267 /* file, 3 if original file isn't normal, 4 if ran out of disk space */
268 int copyfile(char *oldpath, char *newpath)
269 {
270 int fi, fo, x;
271 char buf[512];
272 struct stat st;
273
274 fi = open(oldpath, O_RDONLY, 0);
275 if (fi < 0)
276 return 1;
277 fstat(fi, &st);
278 if (!(st.st_mode & S_IFREG))
279 return 3;
280 fo = creat(newpath, (int) (st.st_mode & 0777));
281 if (fo < 0) {
282 close(fi);
283 return 2;
284 }
285 for (x = 1; x > 0;) {
286 x = read(fi, buf, 512);
287 if (x > 0) {
288 if (write(fo, buf, x) < x) { /* couldn't write */
289 close(fo);
290 close(fi);
291 unlink(newpath);
292 return 4;
293 }
294 }
295 }
296 close(fo);
297 close(fi);
298 return 0;
299 }
300
301 int movefile(char *oldpath, char *newpath)
302 {
303 int x = copyfile(oldpath, newpath);
304
305 if (x == 0)
306 unlink(oldpath);
307 return x;
308 }
309
310 /* dump a potentially super-long string of text */
311 /* assume prefix 20 chars or less */
312 void dumplots(int idx, char *prefix, char *data)
313 {
314 char *p = data, *q, *n, c;
315
316 if (!(*data)) {
317 dprintf(idx, "%s\n", prefix);
318 return;
319 }
320 while (strlen(p) > 480) {
321 q = p + 480;
322 /* search for embedded linefeed first */
323 n = strchr(p, '\n');
324 if ((n != NULL) && (n < q)) {
325 /* great! dump that first line then start over */
326 *n = 0;
327 dprintf(idx, "%s%s\n", prefix, p);
328 *n = '\n';
329 p = n + 1;
330 } else {
331 /* search backwards for the last space */
332 while ((*q != ' ') && (q != p))
333 q--;
334 if (q == p)
335 q = p + 480;
336 /* ^ 1 char will get squashed cos there was no space -- too bad */
337 c = *q;
338 *q = 0;
339 dprintf(idx, "%s%s\n", prefix, p);
340 *q = c;
341 p = q + 1;
342 }
343 }
344 /* last trailing bit: split by linefeeds if possible */
345 n = strchr(p, '\n');
346 while (n != NULL) {
347 *n = 0;
348 dprintf(idx, "%s%s\n", prefix, p);
349 *n = '\n';
350 p = n + 1;
351 n = strchr(p, '\n');
352 }
353 if (*p)
354 dprintf(idx, "%s%s\n", prefix, p); /* last trailing bit */
355 }
356
357 /* convert an interval (in seconds) to one of:
358 * "19 days ago", "1 day ago", "18:12" */
359 void daysago(time_t now, time_t then, char *out)
360 {
361 char s[81];
362
363 if (now - then > 86400) {
364 int days = (now - then) / 86400;
365
366 sprintf(out, "%d day%s ago", days, (days == 1) ? "" : "s");
367 return;
368 }
369 strcpy(s, ctime(&then));
370 s[16] = 0;
371 strcpy(out, &s[11]);
372 }
373
374 /* convert an interval (in seconds) to one of:
375 * "in 19 days", "in 1 day", "at 18:12" */
376 void days(time_t now, time_t then, char *out)
377 {
378 char s[81];
379
380 if (now - then > 86400) {
381 int days = (now - then) / 86400;
382
383 sprintf(out, "in %d day%s", days, (days == 1) ? "" : "s");
384 return;
385 }
386 strcpy(out, "at ");
387 strcpy(s, ctime(&now));
388 s[16] = 0;
389 strcpy(&out[3], &s[11]);
390 }
391
392 /* convert an interval (in seconds) to one of:
393 * "for 19 days", "for 1 day", "for 09:10" */
394 void daysdur(time_t now, time_t then, char *out)
395 {
396 char s[81];
397 int hrs, mins;
398
399 if (now - then > 86400) {
400 int days = (now - then) / 86400;
401
402 sprintf(out, "for %d day%s", days, (days == 1) ? "" : "s");
403 return;
404 }
405 strcpy(out, "for ");
406 now -= then;
407 hrs = (int) (now / 3600);
408 mins = (int) ((now - (hrs * 3600)) / 60);
409 sprintf(s, "%02d:%02d", hrs, mins);
410 strcat(out, s);
411 }
412
413 /***** LOGGING *****/
414
415 /* log something */
416 /* putlog(level,channel_name,format,...); */
417 void putlog VARARGS_DEF(int, arg1)
418 {
419 int i, type;
420 char *format, *chname, s[MAX_LOG_LINE + 1], s1[256], *out;
421 time_t tt;
422 char ct[81];
423 struct tm *T = localtime(&now);
424
425 va_list va;
426 type = VARARGS_START(int, arg1, va);
427 chname = va_arg(va, char *);
428 format = va_arg(va, char *);
429
430 /* format log entry at offset 8, then i can prepend the timestamp */
431 out = &s[8];
432 #ifdef HAVE_VSNPRINTF
433 /* no need to check if out should be null-terminated here,
434 * just do it! <cybah> */
435 vsnprintf(out, MAX_LOG_LINE - 8, format, va);
436 out[MAX_LOG_LINE - 8] = 0;
437 #else
438 vsprintf(out, format, va);
439 #endif
440 tt = now;
441 if (keep_all_logs) {
442 strcpy(ct, ctime(&tt));
443 ct[10] = 0;
444 strcpy(ct, &ct[8]);
445 ct[7] = 0;
446 strcpy(&ct[2], &ct[4]);
447 ct[24] = 0;
448 strcpy(&ct[5], &ct[22]);
449 if (ct[0] == ' ')
450 ct[0] = '0';
451 }
452 if ((out[0]) && (shtime)) {
453 strcpy(s1, ctime(&tt));
454 strcpy(s1, &s1[11]);
455 s1[5] = 0;
456 out = s;
457 s[0] = '[';
458 strncpy(&s[1], s1, 5);
459 s[6] = ']';
460 s[7] = ' ';
461 }
462 strcat(out, "\n");
463 if (!use_stderr) {
464 for (i = 0; i < max_logs; i++) {
465 if ((logs[i].filename != NULL) && (logs[i].mask & type) &&
466 ((chname[0] == '*') || (logs[i].chname[0] == '*') ||
467 (!rfc_casecmp(chname, logs[i].chname)))) {
468 if (logs[i].f == NULL) {
469 /* open this logfile */
470 if (keep_all_logs) {
471 sprintf(s1, "%s.%s", logs[i].filename, ct);
472 logs[i].f = fopen(s1, "a+");
473 } else
474 logs[i].f = fopen(logs[i].filename, "a+");
475 }
476 if (logs[i].f != NULL) {
477 /* Check if this is the same as the last line added to
478 * the log. <cybah> */
479 if (!strcasecmp(out + 8, logs[i].szLast)) {
480 /* It is a repeat, so increment Repeats */
481 logs[i].Repeats++;
482 } else {
483 /* Not a repeat, check if there were any repeat
484 * lines previously... */
485 if (logs[i].Repeats > 0) {
486 /* Yep.. so display 'last message repeated x times'
487 * then reset repeats. We want the current time here,
488 * so put that in the file first. */
489 if (T) {
490 fprintf(logs[i].f, "[%2.2d:%2.2d] ", T->tm_hour, T->tm_min);
491 fprintf(logs[i].f, MISC_LOGREPEAT, logs[i].Repeats);
492 } else {
493 fprintf(logs[i].f, "[??:??] ");
494 fprintf(logs[i].f, MISC_LOGREPEAT, logs[i].Repeats);
495 }
496 logs[i].Repeats = 0;
497 /* no need to reset logs[i].szLast here
498 * because we update it later on... */
499 }
500 fputs(out, logs[i].f);
501 strncpy(logs[i].szLast, out + 8, MAX_LOG_LINE);
502 logs[i].szLast[MAX_LOG_LINE] = 0;
503 }
504 }
505 }
506 }
507 }
508 if ((!backgrd) && (!con_chan) && (!term_z))
509 printf("%s", out);
510 for (i = 0; i < dcc_total; i++)
511 if ((dcc[i].type == &DCC_CHAT) && (dcc[i].u.chat->con_flags & type)) {
512 if ((chname[0] == '*') || (dcc[i].u.chat->con_chan[0] == '*') ||
513 (!rfc_casecmp(chname, dcc[i].u.chat->con_chan)))
514 dprintf(i, "%s", out);
515 }
516 if ((type & LOG_MISC) && use_stderr) {
517 if (shtime)
518 out += 8;
519 dprintf(DP_STDERR, "%s", s);
520 }
521 va_end(va);
522 }
523
524 void check_logsize()
525 {
526 struct stat ss;
527 int i;
528
529 /* int x=1; */
530 char buf[1024]; /* should be plenty */
531
532 context;
533 if ((keep_all_logs == 0) && (max_logsize != 0)) {
534 for (i = 0; i < max_logs; i++) {
535 if (logs[i].filename) {
536 if (stat(logs[i].filename, &ss) != 0) {
537 break;
538 }
539 if ((ss.st_size >> 10) > max_logsize) {
540 context;
541 if (logs[i].f) {
542 /* write to the log before closing it huh.. */
543 putlog(LOG_MISC, "*", MISC_CLOGS, logs[i].filename, ss.st_size);
544 fflush(logs[i].f);
545 fclose(logs[i].f);
546 logs[i].f = NULL;
547 context;
548 }
549 context;
550
551 simple_sprintf(buf, "%s.yesterday", logs[i].filename);
552 buf[1023] = 0;
553 unlink(buf);
554 /* x++;
555 * This is an alternate method i was considering, i want to leave
556 * this in here and commented.. in case someone wants it like this
557 * it really depends on feedback from the users. - poptix
558 * feel free to ask me, if you have questions on this..
559 *
560 * while (x > 0) {
561 * x++;
562 * * only YOU can prevent buffer overflows! *
563 * simple_sprintf(buf,"%s.%d",logs[i].filename,x);
564 * buf[1023] = 0;
565 * if (stat(buf,&ss) == -1) {
566 * * file doesnt exist, lets use it *
567 */
568 #ifdef RENAME
569 rename(logs[i].filename, buf);
570 #else
571 movefile(logs[i].filename, buf);
572 #endif
573 /* x=0;
574 * }
575 * } */
576 }
577 }
578 }
579 }
580 context;
581 }
582
583 /* flush the logfiles to disk */
584 void flushlogs()
585 {
586 int i;
587 struct tm *T = localtime(&now);
588
589 context;
590 /* Now also checks to see if there's a repeat message and
591 * displays the 'last message repeated...' stuff too <cybah> */
592 for (i = 0; i < max_logs; i++) {
593 if (logs[i].f != NULL) {
594 if ((logs[i].Repeats > 0) && quick_logs) {
595 /* Repeat.. if quicklogs used then display 'last message
596 * repeated x times' and reset Repeats. */
597 if (T) {
598 fprintf(logs[i].f, "[%2.2d:%2.2d] ", T->tm_hour, T->tm_min);
599 fprintf(logs[i].f, MISC_LOGREPEAT, logs[i].Repeats);
600 } else {
601 fprintf(logs[i].f, "[??:??] ");
602 fprintf(logs[i].f, MISC_LOGREPEAT, logs[i].Repeats);
603 }
604 /* Reset repeats */
605 logs[i].Repeats = 0;
606 }
607 fflush(logs[i].f);
608 }
609 }
610 context;
611 }
612
613 /********** STRING SUBSTITUTION **********/
614
615 static int cols = 0;
616 static int colsofar = 0;
617 static int blind = 0;
618 static int subwidth = 70;
619 static char *colstr = NULL;
620
621 /* add string to colstr */
622 static void subst_addcol(char *s, char *newcol)
623 {
624 char *p, *q;
625 int i, colwidth;
626
627 if ((newcol[0]) && (newcol[0] != '\377'))
628 colsofar++;
629 colstr = nrealloc(colstr, strlen(colstr) + strlen(newcol) +
630 (colstr[0] ? 2 : 1));
631 if ((newcol[0]) && (newcol[0] != '\377')) {
632 if (colstr[0])
633 strcat(colstr, "\377");
634 strcat(colstr, newcol);
635 }
636 if ((colsofar == cols) || ((newcol[0] == '\377') && (colstr[0]))) {
637 colsofar = 0;
638 strcpy(s, " ");
639 colwidth = (subwidth - 5) / cols;
640 q = colstr;
641 p = strchr(colstr, '\377');
642 while (p != NULL) {
643 *p = 0;
644 strcat(s, q);
645 for (i = strlen(q); i < colwidth; i++)
646 strcat(s, " ");
647 q = p + 1;
648 p = strchr(q, '\377');
649 }
650 strcat(s, q);
651 nfree(colstr);
652 colstr = (char *) nmalloc(1);
653 colstr[0] = 0;
654 }
655 }
656
657 /* substitute %x codes in help files
658 * %B = bot nickname
659 * %V = version
660 * %C = list of channels i monitor
661 * %E = eggdrop banner
662 * %A = admin line
663 * %T = current time ("14:15")
664 * %N = user's nickname
665 * %U = display system name if possible
666 * %{+xy} require flags to read this section
667 * %{-} turn of required flag matching only
668 * %{center} center this line
669 * %{cols=N} start of columnated section (indented)
670 * %{help=TOPIC} start a section for a particular command
671 * %{end} end of section
672 */
673 #define HELP_BUF_LEN 256
674 #define HELP_BOLD 1
675 #define HELP_REV 2
676 #define HELP_UNDER 4
677 #define HELP_FLASH 8
678
679 void help_subst(char *s, char *nick, struct flag_record *flags,
680 int isdcc, char *topic)
681 {
682 char xx[HELP_BUF_LEN + 1], sub[161], *current, *q, chr, *writeidx,
683 *readidx, *towrite;
684 struct chanset_t *chan;
685 int i, j, center = 0;
686 static int help_flags;
687
688 #ifdef HAVE_UNAME
689 struct utsname uname_info;
690
691 #endif
692
693 if (s == NULL) {
694 /* used to reset substitutions */
695 blind = 0;
696 cols = 0;
697 subwidth = 70;
698 if (colstr != NULL) {
699 nfree(colstr);
700 colstr = NULL;
701 }
702 help_flags = isdcc;
703 return;
704 }
705 strncpy(xx, s, HELP_BUF_LEN);
706 xx[HELP_BUF_LEN] = 0;
707 readidx = xx;
708 writeidx = s;
709 current = strchr(readidx, '%');
710 while (current) {
711 /* are we about to copy a chuck to the end of the buffer?
712 * if so return */
713 if ((writeidx + (current - readidx)) >= (s + HELP_BUF_LEN)) {
714 strncpy(writeidx, readidx, (s + HELP_BUF_LEN) - writeidx);
715 s[HELP_BUF_LEN] = 0;
716 return;
717 }
718 chr = *(current + 1);
719 *current = 0;
720 if (!blind)
721 writeidx += my_strcpy(writeidx, readidx);
722 towrite = NULL;
723 switch (chr) {
724 case 'b':
725 if (glob_hilite(*flags)) {
726 if (help_flags & HELP_IRC) {
727 towrite = "\002";
728 } else if (help_flags & HELP_BOLD) {
729 help_flags &= ~HELP_BOLD;
730 towrite = "\033[0m";
731 } else {
732 help_flags |= HELP_BOLD;
733 towrite = "\033[1m";
734 }
735 }
736 break;
737 case 'v':
738 if (glob_hilite(*flags)) {
739 if (help_flags & HELP_IRC) {
740 towrite = "\026";
741 } else if (help_flags & HELP_REV) {
742 help_flags &= ~HELP_REV;
743 towrite = "\033[0m";
744 } else {
745 help_flags |= HELP_REV;
746 towrite = "\033[7m";
747 }
748 }
749 break;
750 case '_':
751 if (glob_hilite(*flags)) {
752 if (help_flags & HELP_IRC) {
753 towrite = "\037";
754 } else if (help_flags & HELP_UNDER) {
755 help_flags &= ~HELP_UNDER;
756 towrite = "\033[0m";
757 } else {
758 help_flags |= HELP_UNDER;
759 towrite = "\033[4m";
760 }
761 }
762 break;
763 case 'f':
764 if (glob_hilite(*flags)) {
765 if (help_flags & HELP_FLASH) {
766 if (help_flags & HELP_IRC) {
767 towrite = "\002\037";
768 } else {
769 towrite = "\033[0m";
770 }
771 help_flags &= ~HELP_FLASH;
772 } else {
773 help_flags |= HELP_FLASH;
774 if (help_flags & HELP_IRC) {
775 towrite = "\037\002";
776 } else {
777 towrite = "\033[5m";
778 }
779 }
780 }
781 break;
782 case 'U':
783 #ifdef HAVE_UNAME
784 if (!uname(&uname_info)) {
785 simple_sprintf(sub, "%s %s", uname_info.sysname,
786 uname_info.release);
787 towrite = sub;
788 } else
789 #endif
790 towrite = "*UNKNOWN*";
791 break;
792 case 'B':
793 towrite = (isdcc ? botnetnick : botname);
794 break;
795 case 'V':
796 towrite = ver;
797 break;
798 case 'E':
799 towrite = version;
800 break;
801 case 'A':
802 towrite = admin;
803 break;
804 case 'T':
805 strcpy(sub, ctime(&now));
806 sub[16] = 0;
807 towrite = sub + 11;
808 break;
809 case 'N':
810 towrite = strchr(nick, ':');
811 if (towrite)
812 towrite++;
813 else
814 towrite = nick;
815 break;
816 case 'C':
817 if (!blind)
818 for (chan = chanset; chan; chan = chan->next) {
819 if ((strlen(chan->name) + writeidx + 2) >=
820 (s + HELP_BUF_LEN)) {
821 strncpy(writeidx, chan->name, (s + HELP_BUF_LEN) - writeidx);
822 s[HELP_BUF_LEN] = 0;
823 return;
824 }
825 writeidx += my_strcpy(writeidx, chan->name);
826 if (chan->next) {
827 *writeidx++ = ',';
828 *writeidx++ = ' ';
829 }
830 }
831 break;
832 case '{':
833 q = current;
834 current++;
835 while ((*current != '}') && (*current))
836 current++;
837 if (*current) {
838 *current = 0;
839 current--;
840 q += 2;
841 /* now q is the string and p is where the rest of the fcn expects */
842 if (!strncmp(q, "help=", 5)) {
843 if (topic && strcasecmp(q + 5, topic))
844 blind |= 2;
845 else
846 blind &= ~2;
847 } else if (!(blind & 2)) {
848 if (q[0] == '+') {
849 struct flag_record fr =
850 {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
851
852 break_down_flags(q + 1, &fr, NULL);
853 if (!flagrec_ok(&fr, flags))
854 blind |= 1;
855 else
856 blind &= ~1;
857 } else if (q[0] == '-') {
858 blind &= ~1;
859 } else if (!strcasecmp(q, "end")) {
860 blind &= ~1;
861 subwidth = 70;
862 if (cols) {
863 subst_addcol(sub, "\377");
864 nfree(colstr);
865 colstr = NULL;
866 cols = 0;
867 towrite = sub;
868 }
869 } else if (!strcasecmp(q, "center"))
870 center = 1;
871 else if (!strncmp(q, "cols=", 5)) {
872 char *r;
873
874 cols = atoi(q + 5);
875 colsofar = 0;
876 colstr = (char *) nmalloc(1);
877 colstr[0] = 0;
878 r = strchr(q + 5, '/');
879 if (r != NULL)
880 subwidth = atoi(r + 1);
881 }
882 }
883 } else
884 current = q; /* no } so ignore */
885 break;
886 default:
887 if (!blind) {
888 *writeidx++ = chr;
889 if (writeidx >= (s + HELP_BUF_LEN)) {
890 *writeidx = 0;
891 return;
892 }
893 }
894 }
895 if (towrite && !blind) {
896 if ((writeidx + strlen(towrite)) >= (s + HELP_BUF_LEN)) {
897 strncpy(writeidx, towrite, (s + HELP_BUF_LEN) - writeidx);
898 s[HELP_BUF_LEN] = 0;
899 return;
900 }
901 writeidx += my_strcpy(writeidx, towrite);
902 }
903 if (chr) {
904 readidx = current + 2;
905 current = strchr(readidx, '%');
906 } else {
907 readidx = current + 1;
908 current = NULL;
909 }
910 }
911 if (!blind) {
912 i = strlen(readidx);
913 if (i && ((writeidx + i) >= (s + HELP_BUF_LEN))) {
914 strncpy(writeidx, readidx, (s + HELP_BUF_LEN) - writeidx);
915 s[HELP_BUF_LEN] = 0;
916 return;
917 }
918 strcpy(writeidx, readidx);
919 } else
920 *writeidx = 0;
921 if (center) {
922 strcpy(xx, s);
923 i = 35 - (strlen(xx) / 2);
924 if (i > 0) {
925 s[0] = 0;
926 for (j = 0; j < i; j++)
927 s[j] = ' ';
928 strcpy(s + i, xx);
929 }
930 }
931 if (cols) {
932 strcpy(xx, s);
933 s[0] = 0;
934 subst_addcol(s, xx);
935 }
936 }
937
938 static void scan_help_file(struct help_ref *current, char *filename, int type)
939 {
940 FILE *f;
941 char s[HELP_BUF_LEN + 1], *p, *q;
942 struct help_list *list;
943
944 if (is_file(filename) && (f = fopen(filename, "r"))) {
945 while (!feof(f)) {
946 fgets(s, HELP_BUF_LEN, f);
947 if (!feof(f)) {
948 p = s;
949 while ((q = strstr(p, "%{help="))) {
950 q += 7;
951 if ((p = strchr(q, '}'))) {
952 *p = 0;
953 list = nmalloc(sizeof(struct help_list));
954
955 list->name = nmalloc(p - q + 1);
956 strcpy(list->name, q);
957 list->next = current->first;
958 list->type = type;
959 current->first = list;
960 p++;
961 } else
962 p = "";
963 }
964 }
965 }
966 fclose(f);
967 }
968 }
969
970 void add_help_reference(char *file)
971 {
972 char s[1024];
973 struct help_ref *current;
974
975 for (current = help_list; current; current = current->next)
976 if (!strcmp(current->name, file))
977 return; /* already exists, can't re-add :P */
978 current = nmalloc(sizeof(struct help_ref));
979
980 current->name = nmalloc(strlen(file) + 1);
981 strcpy(current->name, file);
982 current->next = help_list;
983 current->first = NULL;
984 help_list = current;
985 simple_sprintf(s, "%smsg/%s", helpdir, file);
986 scan_help_file(current, s, 0);
987 simple_sprintf(s, "%s%s", helpdir, file);
988 scan_help_file(current, s, 1);
989 simple_sprintf(s, "%sset/%s", helpdir, file);
990 scan_help_file(current, s, 2);
991 };
992
993 void rem_help_reference(char *file)
994 {
995 struct help_ref *current, *last = NULL;
996 struct help_list *item;
997
998 for (current = help_list; current; last = current, current = current->next)
999 if (!strcmp(current->name, file)) {
1000 while ((item = current->first)) {
1001 current->first = item->next;
1002 nfree(item->name);
1003 nfree(item);
1004 }
1005 nfree(current->name);
1006 if (last)
1007 last->next = current->next;
1008 else
1009 help_list = current->next;
1010 nfree(current);
1011 return;
1012 }
1013 }
1014
1015 void reload_help_data(void)
1016 {
1017 struct help_ref *current = help_list, *next;
1018 struct help_list *item;
1019
1020 help_list = NULL;
1021 while (current) {
1022 while ((item = current->first)) {
1023 current->first = item->next;
1024 nfree(item->name);
1025 nfree(item);
1026 }
1027 add_help_reference(current->name);
1028 nfree(current->name);
1029 next = current->next;
1030 nfree(current);
1031 current = next;
1032 }
1033 }
1034
1035 void debug_help(int idx)
1036 {
1037 struct help_ref *current;
1038 struct help_list *item;
1039
1040 for (current = help_list; current; current = current->next) {
1041 dprintf(idx, "HELP FILE(S): %s\n", current->name);
1042 for (item = current->first; item; item = item->next) {
1043 dprintf(idx, " %s (%s)\n", item->name, (item->type == 0) ? "msg/" :
1044 (item->type == 1) ? "" : "set/");
1045 }
1046 }
1047 }
1048
1049 FILE *resolve_help(int dcc, char *file)
1050 {
1051 char s[1024], *p;
1052 FILE *f;
1053 struct help_ref *current;
1054 struct help_list *item;
1055
1056 /* somewhere here goes the eventual substituation */
1057 if (!(dcc & HELP_TEXT))
1058 for (current = help_list; current; current = current->next)
1059 for (item = current->first; item; item = item->next)
1060 if (!strcmp(item->name, file)) {
1061 if (!item->type && !dcc) {
1062 simple_sprintf(s, "%smsg/%s", helpdir, current->name);
1063 if ((f = fopen(s, "r")))
1064 return f;
1065 } else if (dcc && item->type) {
1066 if (item->type == 1)
1067 simple_sprintf(s, "%s%s", helpdir, current->name);
1068 else
1069 simple_sprintf(s, "%sset/%s", helpdir, current->name);
1070 if ((f = fopen(s, "r")))
1071 return f;
1072 }
1073 }
1074 for (p = s + simple_sprintf(s, "%s%s", helpdir, dcc ? "" : "msg/");
1075 *file && (p < s + 1023); file++, p++) {
1076 switch (*file) {
1077 case ' ':
1078 case '.':
1079 *p = '/';
1080 break;
1081 case '-':
1082 *p = '-';
1083 break;
1084 case '+':
1085 *p = 'P';
1086 break;
1087 default:
1088 *p = *file;
1089 }
1090 }
1091 *p = 0;
1092 if (!is_file(s)) {
1093 strcat(s, "/");
1094 strcat(s, file);
1095 if (!is_file(s))
1096 return NULL;
1097 }
1098 return fopen(s, "r");
1099 }
1100
1101 void showhelp(char *who, char *file, struct flag_record *flags, int fl)
1102 {
1103 int lines = 0;
1104 char s[HELP_BUF_LEN + 1];
1105 FILE *f = resolve_help(fl, file);
1106
1107 if (f) {
1108 help_subst(NULL, NULL, 0, HELP_IRC, NULL); /* clear flags */
1109 while (!feof(f)) {
1110 fgets(s, HELP_BUF_LEN, f);
1111 if (!feof(f)) {
1112 if (s[strlen(s) - 1] == '\n')
1113 s[strlen(s) - 1] = 0;
1114 if (!s[0])
1115 strcpy(s, " ");
1116 help_subst(s, who, flags, 0, file);
1117 if ((s[0]) && (strlen(s) > 1)) {
1118 dprintf(DP_HELP, "NOTICE %s :%s\n", who, s);
1119 lines++;
1120 }
1121 }
1122 }
1123 fclose(f);
1124 }
1125 if (!lines && !(fl & HELP_TEXT))
1126 dprintf(DP_HELP, "NOTICE %s :%s\n", who, IRC_NOHELP2);
1127 }
1128
1129 static int display_tellhelp(int idx, char *file, FILE *f, struct flag_record *flags)
1130 {
1131 char s[HELP_BUF_LEN + 1];
1132 int lines = 0;
1133
1134 if (f) {
1135 help_subst(NULL, NULL, 0,
1136 (dcc[idx].status & STAT_TELNET) ? 0 : HELP_IRC, NULL);
1137 while (!feof(f)) {
1138 fgets(s, HELP_BUF_LEN, f);
1139 if (!feof(f)) {
1140 if (s[strlen(s) - 1] == '\n')
1141 s[strlen(s) - 1] = 0;
1142 if (!s[0])
1143 strcpy(s, " ");
1144 help_subst(s, dcc[idx].nick, flags, 1, file);
1145 if (s[0]) {
1146 dprintf(idx, "%s\n", s);
1147 lines++;
1148 }
1149 }
1150 }
1151 fclose(f);
1152 }
1153 return lines;
1154 }
1155
1156 void tellhelp(int idx, char *file, struct flag_record *flags, int fl)
1157 {
1158 int lines = 0;
1159 FILE *f = resolve_help(HELP_DCC | fl, file);
1160
1161 if (f)
1162 lines = display_tellhelp(idx, file, f, flags);
1163 if (!lines && !(fl & HELP_TEXT))
1164 dprintf(idx, "%s\n", IRC_NOHELP2);
1165 }
1166
1167 void tellwildhelp(int idx, char *match, struct flag_record *flags)
1168 {
1169 struct help_ref *current;
1170 struct help_list *item;
1171 FILE *f;
1172 char s[1024];
1173
1174 for (current = help_list; current; current = current->next)
1175 for (item = current->first; item; item = item->next)
1176 if (wild_match(match, item->name) && item->type) {
1177 if (item->type == 1)
1178 simple_sprintf(s, "%s%s", helpdir, current->name);
1179 else
1180 simple_sprintf(s, "%sset/%s", helpdir, current->name);
1181 if ((f = fopen(s, "r")))
1182 display_tellhelp(idx, item->name, f, flags);
1183 }
1184 if (!s[0])
1185 dprintf(idx, "%s\n", IRC_NOHELP2);
1186 }
1187
1188 /* substitute vars in a lang text to dcc chatter */
1189 void sub_lang(int idx, char *text)
1190 {
1191 char s[1024];
1192 struct flag_record fr =
1193 {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
1194
1195 get_user_flagrec(dcc[idx].user, &fr, dcc[idx].u.chat->con_chan);
1196 help_subst(NULL, NULL, 0,
1197 (dcc[idx].status & STAT_TELNET) ? 0 : HELP_IRC, NULL);
1198 strncpy(s, text, 1024);
1199 if (s[strlen(s) - 1] == '\n')
1200 s[strlen(s) - 1] = 0;
1201 if (!s[0])
1202 strcpy(s, " ");
1203 help_subst(s, dcc[idx].nick, &fr, 1, botnetnick);
1204 if (s[0])
1205 dprintf(idx, "%s\n", s);
1206 }
1207
1208 /* show motd to dcc chatter */
1209 void show_motd(int idx)
1210 {
1211 FILE *vv;
1212 char s[1024];
1213 struct flag_record fr =
1214 {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
1215
1216 get_user_flagrec(dcc[idx].user, &fr, dcc[idx].u.chat->con_chan);
1217 vv = fopen(motdfile, "r");
1218 if (vv != NULL) {
1219 if (!is_file(motdfile)) {
1220 fclose(vv);
1221 dprintf(idx, "### MOTD %s\n", IRC_NOTNORMFILE);
1222 return;
1223 }
1224 dprintf(idx, "\n");
1225 help_subst(NULL, NULL, 0,
1226 (dcc[idx].status & STAT_TELNET) ? 0 : HELP_IRC, NULL);
1227 while (!feof(vv)) {
1228 fgets(s, 120, vv);
1229 if (!feof(vv)) {
1230 if (s[strlen(s) - 1] == '\n')
1231 s[strlen(s) - 1] = 0;
1232 if (!s[0])
1233 strcpy(s, " ");
1234 help_subst(s, dcc[idx].nick, &fr, 1, botnetnick);
1235 if (s[0])
1236 dprintf(idx, "%s\n", s);
1237 }
1238 }
1239 fclose(vv);
1240 dprintf(idx, "\n");
1241 }
1242 }
1243
1244 /* remove :'s from ignores and bans */
1245 void remove_gunk(char *par)
1246 {
1247 char *q, *p;
1248 char WBUF[strlen(par) + 1];
1249
1250 for (p = par, q = WBUF; *p; p++, q++) {
1251 if (*p == ':')
1252 q--;
1253 else
1254 *q = *p;
1255 }
1256 *q = *p;
1257 strcpy(par, WBUF);
1258 }
1259
1260 /* This will return a pointer to the first character after the @ in the
1261 * string given it. Possibly it's time to think about a regexp library
1262 * for eggdrop... */
1263 char *extracthostname(char *hostmask)
1264 {
1265 char *ptr = strrchr(hostmask, '@');
1266
1267 if (ptr) {
1268 ptr = ptr + 1;
1269 return ptr;
1270 }
1271 return "";
1272 }
1273
1274 /* show banner to telnet user, simialer to show_motd() - [seC] */
1275 void show_banner(int idx) {
1276 FILE *vv;
1277 char s[1024];
1278 struct flag_record fr = {FR_GLOBAL|FR_CHAN,0,0,0,0,0};
1279
1280 get_user_flagrec(dcc[idx].user,&fr,dcc[idx].u.chat->con_chan);
1281 vv = fopen(bannerfile, "r");
1282 if (!vv || !is_file(bannerfile))
1283 return;
1284 while(!feof(vv)) {
1285 fgets(s, 120, vv);
1286 if (!feof(vv)) {
1287 if (!s[0])
1288 strcpy(s, " \n");
1289 help_subst(s, dcc[idx].nick, &fr, 1, botnetnick);
1290 dprintf(idx, "%s", s);
1291 }
1292 }
1293 }
1294

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23