]> jfr.im git - irc/evilnet/x3.git/blob - src/tools.c
Lots of style changes to user replies. added numeric access back
[irc/evilnet/x3.git] / src / tools.c
1 /* tools.c - miscellaneous utility functions
2 * Copyright 2000-2004 srvx Development Team
3 *
4 * This file is part of x3.
5 *
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 #include "helpfile.h"
22 #include "log.h"
23 #include "nickserv.h"
24 #include "recdb.h"
25
26 #ifdef HAVE_NETDB_H
27 #include <netdb.h>
28 #endif
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
32 #ifdef HAVE_ARPA_INET_H
33 #include <arpa/inet.h>
34 #endif
35
36 #define NUMNICKLOG 6
37 #define NUMNICKBASE (1 << NUMNICKLOG)
38 #define NUMNICKMASK (NUMNICKBASE - 1)
39
40 /* Yes, P10's encoding here is almost-but-not-quite MIME Base64. Yay
41 * for gratuitous incompatibilities. */
42 static const char convert2y[256] = {
43 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
44 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
45 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
46 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
47 };
48
49 static const unsigned char convert2n[256] = {
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0,
54 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
55 15,16,17,18,19,20,21,22,23,24,25,62, 0,63, 0, 0,
56 0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
57 41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0
58 };
59
60 unsigned long int
61 base64toint(const char* s, int count)
62 {
63 unsigned int i = 0;
64 while (*s && count) {
65 i = (i << NUMNICKLOG) + convert2n[(unsigned char)*s++];
66 count--;
67 }
68 return i;
69 }
70
71 const char* inttobase64(char* buf, unsigned int v, unsigned int count)
72 {
73 buf[count] = '\0';
74 while (count > 0) {
75 buf[--count] = convert2y[(unsigned char)(v & NUMNICKMASK)];
76 v >>= NUMNICKLOG;
77 }
78 return buf;
79 }
80
81 static char irc_tolower[256];
82 #undef tolower
83 #define tolower(X) irc_tolower[(unsigned char)(X)]
84
85 int
86 irccasecmp(const char *stra, const char *strb) {
87 while (*stra && (tolower(*stra) == tolower(*strb)))
88 stra++, strb++;
89 return tolower(*stra) - tolower(*strb);
90 }
91
92 int
93 ircncasecmp(const char *stra, const char *strb, unsigned int len) {
94 len--;
95 while (*stra && (tolower(*stra) == tolower(*strb)) && len)
96 stra++, strb++, len--;
97 return tolower(*stra) - tolower(*strb);
98 }
99
100 const char *
101 irccasestr(const char *haystack, const char *needle) {
102 unsigned int hay_len = strlen(haystack), needle_len = strlen(needle), pos;
103 if (hay_len < needle_len)
104 return NULL;
105 for (pos=0; pos<hay_len+1-needle_len; ++pos) {
106 if ((tolower(haystack[pos]) == tolower(*needle))
107 && !ircncasecmp(haystack+pos, needle, needle_len))
108 return haystack+pos;
109 }
110 return NULL;
111 }
112
113 int
114 split_line(char *line, int irc_colon, int argv_size, char *argv[])
115 {
116 int argc = 0;
117 int n;
118 while (*line && (argc < argv_size)) {
119 while (*line == ' ')
120 *line++ = 0;
121 if (*line == ':' && irc_colon && argc > 0) {
122 /* the rest is a single parameter */
123 argv[argc++] = line + 1;
124 break;
125 }
126 if (!*line)
127 break;
128 argv[argc++] = line;
129 if (argc >= argv_size)
130 break;
131 while (*line != ' ' && *line)
132 line++;
133 }
134 #ifdef NDEBUG
135 n = 0;
136 #else
137 for (n=argc; n<argv_size; n++)
138 argv[n] = (char*)0xFEEDBEEF;
139 #endif
140 return argc;
141 }
142
143 /* This is ircu's mmatch() function, from match.c. */
144 int mmatch(const char *old_mask, const char *new_mask)
145 {
146 register const char *m = old_mask;
147 register const char *n = new_mask;
148 const char *ma = m;
149 const char *na = n;
150 int wild = 0;
151 int mq = 0, nq = 0;
152
153 while (1)
154 {
155 if (*m == '*')
156 {
157 while (*m == '*')
158 m++;
159 wild = 1;
160 ma = m;
161 na = n;
162 }
163
164 if (!*m)
165 {
166 if (!*n)
167 return 0;
168 for (m--; (m > old_mask) && (*m == '?'); m--)
169 ;
170 if ((*m == '*') && (m > old_mask) && (m[-1] != '\\'))
171 return 0;
172 if (!wild)
173 return 1;
174 m = ma;
175
176 /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
177 if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?')))
178 ++na;
179
180 n = ++na;
181 }
182 else if (!*n)
183 {
184 while (*m == '*')
185 m++;
186 return (*m != 0);
187 }
188 if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
189 {
190 m++;
191 mq = 1;
192 }
193 else
194 mq = 0;
195
196 /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
197 if ((*n == '\\') && ((n[1] == '*') || (n[1] == '?')))
198 {
199 n++;
200 nq = 1;
201 }
202 else
203 nq = 0;
204
205 /*
206 * This `if' has been changed compared to match() to do the following:
207 * Match when:
208 * old (m) new (n) boolean expression
209 * * any (*m == '*' && !mq) ||
210 * ? any except '*' (*m == '?' && !mq && (*n != '*' || nq)) ||
211 * any except * or ? same as m (!((*m == '*' || *m == '?') && !mq) &&
212 * toLower(*m) == toLower(*n) &&
213 * !((mq && !nq) || (!mq && nq)))
214 *
215 * Here `any' also includes \* and \? !
216 *
217 * After reworking the boolean expressions, we get:
218 * (Optimized to use boolean shortcircuits, with most frequently occuring
219 * cases upfront (which took 2 hours!)).
220 */
221 if ((*m == '*' && !mq) ||
222 ((!mq || nq) && tolower(*m) == tolower(*n)) ||
223 (*m == '?' && !mq && (*n != '*' || nq)))
224 {
225 if (*m)
226 m++;
227 if (*n)
228 n++;
229 }
230 else
231 {
232 if (!wild)
233 return 1;
234 m = ma;
235
236 /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
237 if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?')))
238 ++na;
239
240 n = ++na;
241 }
242 }
243 }
244
245 int
246 match_ircglob(const char *text, const char *glob)
247 {
248 unsigned int star_p, q_cnt;
249 while (1) {
250 switch (*glob) {
251 case 0:
252 return !*text;
253 case '\\':
254 glob++;
255 /* intentionally not tolower(...) so people can force
256 * capitalization, or we can overload \ in the future */
257 if (*text++ != *glob++)
258 return 0;
259 break;
260 case '*':
261 case '?':
262 star_p = q_cnt = 0;
263 do {
264 if (*glob == '*')
265 star_p = 1;
266 else if (*glob == '?')
267 q_cnt++;
268 else
269 break;
270 glob++;
271 } while (1);
272 while (q_cnt) {
273 if (!*text++)
274 return 0;
275 q_cnt--;
276 }
277 if (star_p) {
278 /* if this is the last glob character, it will match any text */
279 if (!*glob)
280 return 1;
281 /* Thanks to the loop above, we know that the next
282 * character is a normal character. So just look for
283 * the right character.
284 */
285 for (; *text; text++) {
286 if ((tolower(*text) == tolower(*glob))
287 && match_ircglob(text+1, glob+1)) {
288 return 1;
289 }
290 }
291 return 0;
292 }
293 /* if !star_p, fall through to normal character case,
294 * first checking to see if ?s carried us to the end */
295 if (!*glob && !*text)
296 return 1;
297 default:
298 if (!*text)
299 return 0;
300 while (*text && *glob && *glob != '*' && *glob != '?' && *glob != '\\') {
301 if (tolower(*text++) != tolower(*glob++))
302 return 0;
303 }
304 }
305 }
306 }
307
308 extern const char *hidden_host_suffix;
309
310 int user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick)
311 {
312 /* A new glob function, the old one had many false positives.
313 * look at IsSetHost(user) etc and include_nick, build a nick!user@host string
314 * from user and save it in a variable. Compare with match_ircglob() and
315 * return the results. Match any of the possible user@host combos (ugh) -Rubin
316 */
317 char *matchstr_user;
318 char *matchstr_host;
319 char *matchstr_full = NULL;
320
321 if(IsSetHost(user)) /* S: line sethosts */
322 {
323 /* Grab host and user from sethost instead of real host */
324 char *buff;
325 buff = alloca(strlen(user->sethost) + 1);
326 strcpy(buff, user->sethost);
327 matchstr_user = mysep(&buff, "@");
328 matchstr_host = mysep(&buff, "@");
329
330 matchstr_full = alloca(strlen(user->nick) + strlen(matchstr_user) + strlen(matchstr_host) + 4);
331 if(include_nick)
332 sprintf(matchstr_full, "%s!%s@%s", user->nick, matchstr_user, matchstr_host);
333 else
334 sprintf(matchstr_full, "%s@%s", matchstr_user, matchstr_host);
335 if(match_ircglob(matchstr_full, orig_glob))
336 return(1);
337 }
338 else if(IsFakeHost(user)) /* Fakehost */
339 {
340 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(user->fakehost) + 4);
341 if(include_nick)
342 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, user->fakehost);
343 else
344 sprintf(matchstr_full, "%s@%s", user->ident, user->fakehost);
345 if(match_ircglob(matchstr_full, orig_glob))
346 return(1);
347 }
348 else if(hidden_host_suffix && user->handle_info) /* name.users.network.org host */
349 {
350 matchstr_host = alloca(strlen(user->handle_info->handle) + strlen(hidden_host_suffix) + 2);
351 sprintf(matchstr_host, "%s.%s", user->handle_info->handle, hidden_host_suffix);
352 matchstr_user = user->ident;
353
354 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(matchstr_host) + 4);
355 if(include_nick)
356 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, matchstr_host);
357 else
358 sprintf(matchstr_full, "%s@%s", user->ident, matchstr_host);
359 if(match_ircglob(matchstr_full, orig_glob))
360 return(1);
361 }
362
363 /* Check normal hostname */
364 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(user->hostname) + 4);
365 if(include_nick)
366 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, user->hostname);
367 else
368 sprintf(matchstr_full, "%s@%s", user->ident, user->hostname);
369 if(match_ircglob(matchstr_full, orig_glob))
370 return(1);
371
372 /* Check IP hostname (could skip this if same as above?)*/
373 matchstr_host = inet_ntoa(user->ip);
374 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(matchstr_host) + 4);
375 if(include_nick)
376 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, matchstr_host);
377 else
378 sprintf(matchstr_full, "%s@%s", user->ident, matchstr_host);
379 if(match_ircglob(matchstr_full, orig_glob))
380 return(1);
381
382 return(0); /* Didnt match anything */
383 }
384
385 int
386 user_matches_glob_broken(struct userNode *user, const char *orig_glob, int include_nick)
387 {
388 char *glob, *marker;
389 char *setident = NULL, *sethostname = NULL; // sethost - reed/apples
390
391 /* Make a writable copy of the glob */
392 glob = alloca(strlen(orig_glob)+1);
393 strcpy(glob, orig_glob);
394 /* Check the nick, if it's present */
395 if (include_nick) {
396 if (!(marker = strchr(glob, '!'))) {
397 log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include a '!'", user->nick, orig_glob, include_nick);
398 return 0;
399 }
400 *marker = 0;
401 if (!match_ircglob(user->nick, glob)) return 0;
402 glob = marker + 1;
403 }
404 /* Check the ident */
405 if (!(marker = strchr(glob, '@'))) {
406 log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include an '@'", user->nick, orig_glob, include_nick);
407 return 0;
408 }
409 *marker = 0;
410
411 // sethost - reed/apples
412 if (IsSetHost(user)) {
413 setident = alloca(strcspn(user->sethost, "@")+2);
414 safestrncpy(setident, user->sethost, strcspn(user->sethost, "@")+1);
415 sethostname = strchr(user->sethost, '@') + 1;
416 }
417
418 if (!match_ircglob(user->ident, glob) && (IsSetHost(user) && !match_ircglob(setident, glob)))
419 return 0;
420 glob = marker + 1;
421 /* If it might be an IP glob, test that. */
422 if (!glob[strspn(glob, "0123456789./*?")]
423 && match_ircglob(inet_ntoa(user->ip), glob))
424 return 1;
425 /* Check for a fakehost match. */
426 if (IsFakeHost(user) && match_ircglob(user->fakehost, glob))
427 return 1;
428 if (IsSetHost(user) && match_ircglob(sethostname, glob))
429 return 1;
430 /* Check for an account match. */
431 if (hidden_host_suffix && user->handle_info) {
432 char hidden_host[HOSTLEN+1];
433 snprintf(hidden_host, sizeof(hidden_host), "%s.%s", user->handle_info->handle, hidden_host_suffix);
434 if (match_ircglob(hidden_host, glob))
435 return 1;
436 }
437 /* None of the above; could only be a hostname match. */
438 return match_ircglob(user->hostname, glob);
439 }
440
441 int
442 is_ircmask(const char *text)
443 {
444 while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text)))
445 text++;
446 if (*text++ != '!')
447 return 0;
448 while (*text && *text != '@' && !isspace((char)*text))
449 text++;
450 if (*text++ != '@')
451 return 0;
452 while (*text && !isspace((char)*text))
453 text++;
454 return !*text;
455 }
456
457 int
458 is_gline(const char *text)
459 {
460 if (*text == '@')
461 return 0;
462 text += strcspn(text, "@!% \t\r\n");
463 if (*text++ != '@')
464 return 0;
465 if (!*text)
466 return 0;
467 while (*text && (isalnum((char)*text) || strchr(".-?*:", *text)))
468 text++;
469 return !*text;
470 }
471
472 int
473 split_ircmask(char *text, char **nick, char **ident, char **host)
474 {
475 char *start;
476
477 start = text;
478 while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text))
479 text++;
480 if (*text != '!' || ((text - start) > NICKLEN))
481 return 0;
482 *text = 0;
483 if (nick)
484 *nick = start;
485
486 start = ++text;
487 while (*text && *text != '@' && !isspace((char)*text))
488 text++;
489 if (*text != '@' || ((text - start) > USERLEN))
490 return 0;
491 *text = 0;
492 if (ident)
493 *ident = start;
494
495 start = ++text;
496 while (*text && (isalnum((char)*text) || strchr(".-?*:", *text)))
497 text++;
498 if (host)
499 *host = start;
500 return !*text && ((text - start) <= HOSTLEN) && nick && ident && host;
501 }
502
503 char *
504 sanitize_ircmask(char *input)
505 {
506 unsigned int length, flag;
507 char *mask, *start, *output;
508
509 /* Sanitize everything in place; input *must* be a valid
510 hostmask. */
511 output = input;
512 flag = 0;
513
514 /* The nick is truncated at the end. */
515 length = 0;
516 mask = input;
517 while(*input++ != '!')
518 {
519 length++;
520 }
521 if(length > NICKLEN)
522 {
523 mask += NICKLEN;
524 *mask++ = '!';
525
526 /* This flag is used to indicate following parts should
527 be shifted. */
528 flag = 1;
529 }
530 else
531 {
532 mask = input;
533 }
534
535 /* The ident and host must be truncated at the beginning and
536 replaced with a '*' to be compatible with ircu. */
537 length = 0;
538 start = input;
539 while(*input++ != '@')
540 {
541 length++;
542 }
543 if(length > USERLEN || flag)
544 {
545 if(length > USERLEN)
546 {
547 start = input - USERLEN;
548 *mask++ = '*';
549 }
550 while(*start != '@')
551 {
552 *mask++ = *start++;
553 }
554 *mask++ = '@';
555
556 flag = 1;
557 }
558 else
559 {
560 mask = input;
561 }
562
563 length = 0;
564 start = input;
565 while(*input++)
566 {
567 length++;
568 }
569 if(length > HOSTLEN || flag)
570 {
571 if(length > HOSTLEN)
572 {
573 start = input - HOSTLEN;
574 *mask++ = '*';
575 }
576 while(*start)
577 {
578 *mask++ = *start++;
579 }
580 *mask = '\0';
581 }
582
583 return output;
584 }
585
586 static long
587 TypeLength(char type)
588 {
589 switch (type) {
590 case 'y': return 365*24*60*60;
591 case 'M': return 31*24*60*60;
592 case 'w': return 7*24*60*60;
593 case 'd': return 24*60*60;
594 case 'h': return 60*60;
595 case 'm': return 60;
596 case 's': return 1;
597 default: return 0;
598 }
599 }
600
601 unsigned long
602 ParseInterval(const char *interval)
603 {
604 unsigned long seconds = 0;
605 int partial = 0;
606 char c;
607
608 /* process the string, resetting the count if we find a unit character */
609 while ((c = *interval++)) {
610 if (isdigit((int)c)) {
611 partial = partial*10 + c - '0';
612 } else {
613 seconds += TypeLength(c) * partial;
614 partial = 0;
615 }
616 }
617 /* assume the last chunk is seconds (the normal case) */
618 return seconds + partial;
619 }
620
621 static long
622 GetSizeMultiplier(char type)
623 {
624 switch (type) {
625 case 'G': case 'g': return 1024*1024*1024;
626 case 'M': case 'm': return 1024*1024;
627 case 'K': case 'k': return 1024;
628 case 'B': case 'b': return 1;
629 default: return 0;
630 }
631 }
632
633 unsigned long
634 ParseVolume(const char *volume)
635 {
636 unsigned long accum = 0, partial = 0;
637 char c;
638 while ((c = *volume++)) {
639 if (isdigit((int)c)) {
640 partial = partial*10 + c - '0';
641 } else {
642 accum += GetSizeMultiplier(c) * partial;
643 partial = 0;
644 }
645 }
646 return accum + partial;
647 }
648
649 int
650 parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
651 {
652 int accum, pos;
653 unsigned long t_a, t_m;
654
655 t_a = t_m = pos = 0;
656 if (addr)
657 addr->s_addr = htonl(t_a);
658 if (mask)
659 *mask = t_m;
660 while (*str) {
661 if (!isdigit(*str))
662 return 0;
663 accum = 0;
664 do {
665 accum = (accum * 10) + *str++ - '0';
666 } while (isdigit(*str));
667 if (accum > 255)
668 return 0;
669 t_a = (t_a << 8) | accum;
670 t_m = (t_m << 8) | 255;
671 pos += 8;
672 if (*str == '.') {
673 str++;
674 while (*str == '*') {
675 str++;
676 if (*str == '.') {
677 t_a <<= 8;
678 t_m <<= 8;
679 pos += 8;
680 str++;
681 } else if (*str == 0) {
682 t_a <<= 32 - pos;
683 t_m <<= 32 - pos;
684 pos = 32;
685 goto out;
686 } else
687 return 0;
688 }
689 } else if (*str == '/') {
690 int start = pos;
691 accum = 0;
692 do {
693 accum = (accum * 10) + *str++ - '0';
694 } while (isdigit(*str));
695 while (pos < start+accum && pos < 32) {
696 t_a = (t_a << 1) | 0;
697 t_m = (t_m << 1) | 1;
698 pos++;
699 }
700 if (pos != start+accum)
701 return 0;
702 } else if (*str == 0)
703 break;
704 else
705 return 0;
706 }
707 out:
708 if (pos != 32)
709 return 0;
710 if (addr)
711 addr->s_addr = htonl(t_a);
712 if (mask)
713 *mask = t_m;
714 return 1;
715 }
716
717 char *
718 unsplit_string(char *set[], unsigned int max, char *dest)
719 {
720 static char unsplit_buffer[MAXLEN*2];
721 unsigned int ii, jj, pos;
722
723 if (!dest)
724 dest = unsplit_buffer;
725 for (ii=pos=0; ii<max; ii++) {
726 for (jj=0; set[ii][jj]; jj++)
727 dest[pos++] = set[ii][jj];
728 dest[pos++] = ' ';
729 }
730 dest[--pos] = 0;
731 return dest;
732 }
733
734 char *
735 intervalString(char *output, time_t interval, struct handle_info *hi)
736 {
737 static const struct {
738 const char *msg_single;
739 const char *msg_plural;
740 long length;
741 } unit[] = {
742 { "MSG_YEAR", "MSG_YEARS", 365 * 24 * 60 * 60 },
743 { "MSG_WEEK", "MSG_WEEKS", 7 * 24 * 60 * 60 },
744 { "MSG_DAY", "MSG_DAYS", 24 * 60 * 60 },
745 { "MSG_HOUR", "MSG_HOURS", 60 * 60 },
746 { "MSG_MINUTE", "MSG_MINUTES", 60 },
747 { "MSG_SECOND", "MSG_SECONDS", 1 }
748 };
749 struct language *lang;
750 const char *msg;
751 unsigned int type, words, pos, count;
752
753 lang = hi ? hi->language : lang_C;
754 if(!interval)
755 {
756 msg = language_find_message(lang, "MSG_0_SECONDS");
757 return strcpy(output, msg);
758 }
759
760 for (type = 0, words = pos = 0;
761 interval && (words < 2) && (type < ArrayLength(unit));
762 type++) {
763 if (interval < unit[type].length)
764 continue;
765 count = interval / unit[type].length;
766 interval = interval % unit[type].length;
767
768 if (words++ == 1) {
769 msg = language_find_message(lang, "MSG_AND");
770 pos += sprintf(output + pos, "%s ", msg);
771 }
772 if (count == 1)
773 msg = language_find_message(lang, unit[type].msg_single);
774 else
775 msg = language_find_message(lang, unit[type].msg_plural);
776 pos += sprintf(output + pos, "%d%s", count, msg);
777 }
778
779 output[pos] = 0;
780 return output;
781 }
782
783 int
784 getipbyname(const char *name, unsigned long *ip)
785 {
786 struct hostent *he = gethostbyname(name);
787 if (!he)
788 return 0;
789 if (he->h_addrtype != AF_INET)
790 return 0;
791 memcpy(ip, he->h_addr_list[0], sizeof(*ip));
792 return 1;
793 }
794
795 DEFINE_LIST(string_buffer, char)
796
797 void
798 string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len)
799 {
800 while (buf->used + len >= buf->size) {
801 if (!buf->size)
802 buf->size = 16;
803 else
804 buf->size <<= 1;
805 buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
806 }
807 memcpy(buf->list + buf->used, tail, len+1);
808 buf->used += len;
809 }
810
811 void
812 string_buffer_append_string(struct string_buffer *buf, const char *tail)
813 {
814 string_buffer_append_substring(buf, tail, strlen(tail));
815 }
816
817 void
818 string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args)
819 {
820 va_list working;
821 unsigned int len;
822 int ret;
823
824 VA_COPY(working, args);
825 len = strlen(fmt);
826 if (!buf->list || ((buf->used + buf->size) < len)) {
827 buf->size = buf->used + len;
828 buf->list = realloc(buf->list, buf->size);
829 }
830 ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working);
831 if (ret <= 0) {
832 /* pre-C99 behavior; double buffer size until it is big enough */
833 va_end(working);
834 VA_COPY(working, args);
835 while ((ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working)) <= 0) {
836 buf->size += len;
837 buf->list = realloc(buf->list, buf->size);
838 va_end(working);
839 VA_COPY(working, args);
840 }
841 buf->used += ret;
842 } else if (buf->used + ret < buf->size) {
843 /* no need to increase allocation size */
844 buf->used += ret;
845 } else {
846 /* now we know exactly how much space we need */
847 if (buf->size <= buf->used + ret) {
848 buf->size = buf->used + ret + 1;
849 buf->list = realloc(buf->list, buf->size);
850 }
851 va_end(working);
852 VA_COPY(working, args);
853 buf->used += vsnprintf(buf->list + buf->used, buf->size, fmt, working);
854 }
855 va_end(working);
856 va_end(args);
857 }
858
859 void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...)
860 {
861 va_list args;
862 va_start(args, fmt);
863 string_buffer_append_vprintf(buf, fmt, args);
864 }
865
866 void
867 string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl)
868 {
869 unsigned int repl_len = strlen(repl);
870 if (from > buf->used)
871 return;
872 if (len + from > buf->used)
873 len = buf->used - from;
874 buf->used = buf->used + repl_len - len;
875 if (buf->size <= buf->used) {
876 while (buf->used >= buf->size)
877 buf->size <<= 1;
878 buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
879 }
880 memmove(buf->list+from+repl_len, buf->list+from+len, strlen(buf->list+from+len));
881 strcpy(buf->list+from, repl);
882 }
883
884 struct string_list str_tab;
885
886 const char *
887 strtab(unsigned int ii) {
888 if (ii > 65536)
889 return NULL;
890 if (ii > str_tab.size) {
891 unsigned int old_size = str_tab.size;
892 while (ii >= str_tab.size)
893 str_tab.size <<= 1;
894 str_tab.list = realloc(str_tab.list, str_tab.size*sizeof(str_tab.list[0]));
895 memset(str_tab.list+old_size, 0, (str_tab.size-old_size)*sizeof(str_tab.list[0]));
896 }
897 if (!str_tab.list[ii]) {
898 str_tab.list[ii] = malloc(12);
899 sprintf(str_tab.list[ii], "%u", ii);
900 }
901 return str_tab.list[ii];
902 }
903
904 void
905 tools_init(void)
906 {
907 unsigned int upr, lwr;
908 for (lwr=0; lwr<256; ++lwr)
909 tolower(lwr) = lwr;
910 for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr)
911 tolower(upr) = lwr;
912 #ifdef WITH_PROTOCOL_P10
913 for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr)
914 tolower(upr) = lwr;
915 for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr)
916 tolower(upr) = lwr;
917 for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr)
918 tolower(upr) = lwr;
919 #endif
920 str_tab.size = 1001;
921 str_tab.list = calloc(str_tab.size, sizeof(str_tab.list[0]));
922 }
923
924 void
925 tools_cleanup(void)
926 {
927 unsigned int ii;
928 for (ii=0; ii<str_tab.size; ++ii)
929 free(str_tab.list[ii]);
930 free(str_tab.list);
931 }
932
933 /* mysep() is my answer to the strtok/strsep
934 * issue. strsep is nice but doesn't skip
935 * multiple dilimiters, which can really
936 * offset tokens and cause huge corruption
937 * so this function will use strsep but
938 * act like strtok in that sence.
939 */
940 char *mysep(char **sepstr, char *delim)
941 {
942 static char *retstr;
943
944 if(!*sepstr || !**sepstr)
945 return(NULL);
946
947 do
948 {
949 retstr = strsep(sepstr, delim);
950 }while (retstr && !(*retstr));
951
952 return(retstr);
953 }
954
955 char *time2str(time_t thetime)
956 {
957 char *buf, *tmp;
958
959 buf = ctime(&thetime);
960 tmp = (char *)strchr(buf, '\n');
961 *tmp = '\0';
962 return(buf);
963 }
964