/[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.12 - (show annotations) (download) (as text)
Wed Sep 15 07:14:06 1999 UTC (20 years ago) by poptix
Branch: MAIN
Changes since 1.11: +2 -2 lines
File MIME type: text/x-chdr
tinyosfix.patch
: Modified Files:
: 	Makefile doc/UPDATES1.3 src/main.h src/misc.c
: 	src/mod/irc.mod/cmdsirc.c

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23