/[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.17 - (show annotations) (download) (as text)
Sun Oct 17 20:27:29 1999 UTC (20 years ago) by guppy
Branch: MAIN
Changes since 1.16: +3 -1 lines
File MIME type: text/x-chdr
lots of patches

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23