]> jfr.im git - irc/evilnet/x3.git/blob - src/tools.c
This should fix the nickserv module complaining about not enough parameters when...
[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 const char *m = glob, *n = text;
249 const char *m_tmp = glob, *n_tmp = text;
250 int star_p;
251
252 for (;;) switch (*m) {
253 case '\0':
254 if (!*n)
255 return 1;
256 backtrack:
257 if (m_tmp == glob)
258 return 0;
259 m = m_tmp;
260 n = ++n_tmp;
261 break;
262 case '\\':
263 m++;
264 /* allow escaping to force capitalization */
265 if (*m++ != *n++)
266 return 0;
267 break;
268 case '*': case '?':
269 for (star_p = 0; ; m++) {
270 if (*m == '*')
271 star_p = 1;
272 else if (*m == '?') {
273 if (!*n++)
274 goto backtrack;
275 } else break;
276 }
277 if (star_p) {
278 if (!*m)
279 return 1;
280 else if (*m == '\\') {
281 m_tmp = ++m;
282 if (!*m)
283 return 0;
284 for (n_tmp = n; *n && *n != *m; n++) ;
285 } else {
286 m_tmp = m;
287 for (n_tmp = n; *n && tolower(*n) != tolower(*m); n++) ;
288 }
289 }
290 /* and fall through */
291 default:
292 if (!*n)
293 return *m == '\0';
294 if (tolower(*m) != tolower(*n))
295 goto backtrack;
296 m++;
297 n++;
298 break;
299 }
300 }
301
302 extern const char *hidden_host_suffix;
303
304 /* Prevent *@* *@** *@*a* type masks, while allowing anything else. This is the best way iv found to detect
305 * a global matching mask; if it matches this string, it'll match almost anything :) */
306 int is_overmask(char *mask)
307 {
308 return(match_ircglob("abcdefghijklmnopqrstuv!frcmbghilnrtoasde@apdic.yfa.dsfsdaffsdasfdasfd.abcdefghijklmnopqrstuvwxyz.asdfasfdfsdsfdasfda.ydfbe", mask));
309 }
310
311 int user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick)
312 {
313 /* A new glob function, the old one had many false positives.
314 * look at IsSetHost(user) etc and include_nick, build a nick!user@host string
315 * from user and save it in a variable. Compare with match_ircglob() and
316 * return the results. Match any of the possible user@host combos (ugh) -Rubin
317 */
318 char *matchstr_user;
319 char *matchstr_host;
320 char *matchstr_full = NULL;
321
322 if(IsSetHost(user)) /* S: line sethosts */
323 {
324 /* Grab host and user from sethost instead of real host */
325 char *buff;
326 buff = alloca(strlen(user->sethost) + 1);
327 strcpy(buff, user->sethost);
328 matchstr_user = mysep(&buff, "@");
329 matchstr_host = mysep(&buff, "@");
330
331 matchstr_full = alloca(strlen(user->nick) + strlen(matchstr_user) + strlen(matchstr_host) + 4);
332 if(include_nick)
333 sprintf(matchstr_full, "%s!%s@%s", user->nick, matchstr_user, matchstr_host);
334 else
335 sprintf(matchstr_full, "%s@%s", matchstr_user, matchstr_host);
336 if(match_ircglob(matchstr_full, orig_glob))
337 return(1);
338 }
339 else if(IsFakeHost(user)) /* Fakehost */
340 {
341 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(user->fakehost) + 4);
342 if(include_nick)
343 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, user->fakehost);
344 else
345 sprintf(matchstr_full, "%s@%s", user->ident, user->fakehost);
346 if(match_ircglob(matchstr_full, orig_glob))
347 return(1);
348 }
349 else if(hidden_host_suffix && user->handle_info) /* name.users.network.org host */
350 {
351 matchstr_host = alloca(strlen(user->handle_info->handle) + strlen(hidden_host_suffix) + 2);
352 sprintf(matchstr_host, "%s.%s", user->handle_info->handle, hidden_host_suffix);
353 matchstr_user = user->ident;
354
355 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(matchstr_host) + 4);
356 if(include_nick)
357 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, matchstr_host);
358 else
359 sprintf(matchstr_full, "%s@%s", user->ident, matchstr_host);
360 if(match_ircglob(matchstr_full, orig_glob))
361 return(1);
362 }
363
364 /* Check normal hostname */
365 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(user->hostname) + 4);
366 if(include_nick)
367 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, user->hostname);
368 else
369 sprintf(matchstr_full, "%s@%s", user->ident, user->hostname);
370 if(match_ircglob(matchstr_full, orig_glob))
371 return(1);
372
373 /* Check IP hostname (could skip this if same as above?)*/
374 matchstr_host = inet_ntoa(user->ip);
375 matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(matchstr_host) + 4);
376 if(include_nick)
377 sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, matchstr_host);
378 else
379 sprintf(matchstr_full, "%s@%s", user->ident, matchstr_host);
380 if(match_ircglob(matchstr_full, orig_glob))
381 return(1);
382
383 return(0); /* Didnt match anything */
384 }
385
386 int
387 user_matches_glob_broken(struct userNode *user, const char *orig_glob, int include_nick)
388 {
389 char *glob, *marker;
390 char *setident = NULL, *sethostname = NULL; // sethost - reed/apples
391
392 /* Make a writable copy of the glob */
393 glob = alloca(strlen(orig_glob)+1);
394 strcpy(glob, orig_glob);
395 /* Check the nick, if it's present */
396 if (include_nick) {
397 if (!(marker = strchr(glob, '!'))) {
398 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);
399 return 0;
400 }
401 *marker = 0;
402 if (!match_ircglob(user->nick, glob)) return 0;
403 glob = marker + 1;
404 }
405 /* Check the ident */
406 if (!(marker = strchr(glob, '@'))) {
407 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);
408 return 0;
409 }
410 *marker = 0;
411
412 // sethost - reed/apples
413 if (IsSetHost(user)) {
414 setident = alloca(strcspn(user->sethost, "@")+2);
415 safestrncpy(setident, user->sethost, strcspn(user->sethost, "@")+1);
416 sethostname = strchr(user->sethost, '@') + 1;
417 }
418
419 if (!match_ircglob(user->ident, glob) && (IsSetHost(user) && !match_ircglob(setident, glob)))
420 return 0;
421 glob = marker + 1;
422 /* If it might be an IP glob, test that. */
423 if (!glob[strspn(glob, "0123456789./*?")]
424 && match_ircglob(inet_ntoa(user->ip), glob))
425 return 1;
426 /* Check for a fakehost match. */
427 if (IsFakeHost(user) && match_ircglob(user->fakehost, glob))
428 return 1;
429 if (IsSetHost(user) && match_ircglob(sethostname, glob))
430 return 1;
431 /* Check for an account match. */
432 if (hidden_host_suffix && user->handle_info) {
433 char hidden_host[HOSTLEN+1];
434 snprintf(hidden_host, sizeof(hidden_host), "%s.%s", user->handle_info->handle, hidden_host_suffix);
435 if (match_ircglob(hidden_host, glob))
436 return 1;
437 }
438 /* None of the above; could only be a hostname match. */
439 return match_ircglob(user->hostname, glob);
440 }
441
442 int
443 is_ircmask(const char *text)
444 {
445 while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text)))
446 text++;
447 if (*text++ != '!')
448 return 0;
449 while (*text && *text != '@' && !isspace((char)*text))
450 text++;
451 if (*text++ != '@')
452 return 0;
453 while (*text && !isspace((char)*text))
454 text++;
455 return !*text;
456 }
457
458 int
459 is_gline(const char *text)
460 {
461 if (*text == '@')
462 return 0;
463 text += strcspn(text, "@!% \t\r\n");
464 if (*text++ != '@')
465 return 0;
466 if (!*text)
467 return 0;
468 while (*text && (isalnum((char)*text) || strchr(".-?*:", *text)))
469 text++;
470 return !*text;
471 }
472
473 int
474 is_shun(const char *text)
475 {
476 if (*text == '@')
477 return 0;
478 text += strcspn(text, "@!% \t\r\n");
479 if (*text++ != '@')
480 return 0;
481 if (!*text)
482 return 0;
483 while (*text && (isalnum((char)*text) || strchr(".-?*:", *text)))
484 text++;
485 return !*text;
486 }
487
488 int
489 split_ircmask(char *text, char **nick, char **ident, char **host)
490 {
491 char *start;
492
493 start = text;
494 while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text))
495 text++;
496 if (*text != '!' || ((text - start) > NICKLEN))
497 return 0;
498 *text = 0;
499 if (nick)
500 *nick = start;
501
502 start = ++text;
503 while (*text && *text != '@' && !isspace((char)*text))
504 text++;
505 if (*text != '@' || ((text - start) > USERLEN))
506 return 0;
507 *text = 0;
508 if (ident)
509 *ident = start;
510
511 start = ++text;
512 while (*text && (isalnum((char)*text) || strchr(".-?*:", *text)))
513 text++;
514 if (host)
515 *host = start;
516 return !*text && ((text - start) <= HOSTLEN) && nick && ident && host;
517 }
518
519 char *
520 sanitize_ircmask(char *input)
521 {
522 unsigned int length, flag;
523 char *mask, *start, *output;
524
525 /* Sanitize everything in place; input *must* be a valid
526 hostmask. */
527 output = input;
528 flag = 0;
529
530 /* The nick is truncated at the end. */
531 length = 0;
532 mask = input;
533 while(*input++ != '!')
534 {
535 length++;
536 }
537 if(length > NICKLEN)
538 {
539 mask += NICKLEN;
540 *mask++ = '!';
541
542 /* This flag is used to indicate following parts should
543 be shifted. */
544 flag = 1;
545 }
546 else
547 {
548 mask = input;
549 }
550
551 /* The ident and host must be truncated at the beginning and
552 replaced with a '*' to be compatible with ircu. */
553 length = 0;
554 start = input;
555 while(*input++ != '@')
556 {
557 length++;
558 }
559 if(length > USERLEN || flag)
560 {
561 if(length > USERLEN)
562 {
563 start = input - USERLEN;
564 *mask++ = '*';
565 }
566 while(*start != '@')
567 {
568 *mask++ = *start++;
569 }
570 *mask++ = '@';
571
572 flag = 1;
573 }
574 else
575 {
576 mask = input;
577 }
578
579 length = 0;
580 start = input;
581 while(*input++)
582 {
583 length++;
584 }
585 if(length > HOSTLEN || flag)
586 {
587 if(length > HOSTLEN)
588 {
589 start = input - HOSTLEN;
590 *mask++ = '*';
591 }
592 while(*start)
593 {
594 *mask++ = *start++;
595 }
596 *mask = '\0';
597 }
598
599 return output;
600 }
601
602 static long
603 TypeLength(char type)
604 {
605 switch (type) {
606 case 'y': return 365*24*60*60;
607 case 'M': return 30*24*60*60;
608 case 'w': return 7*24*60*60;
609 case 'd': return 24*60*60;
610 case 'h': return 60*60;
611 case 'm': return 60;
612 case 's': return 1;
613 default: return 0;
614 }
615 }
616
617 /* This function is not entirely accurate as it does not take into account leap units
618 * or varying months. TODO: use proper dateadd functions to calculate real seconds
619 * from now for the units (eg 1M should be give us seconds till todays date next month)
620 */
621 unsigned long
622 ParseInterval(const char *interval)
623 {
624 unsigned long seconds = 0;
625 int partial = 0;
626 char c;
627
628 /* process the string, resetting the count if we find a unit character */
629 while ((c = *interval++)) {
630 if (isdigit((int)c)) {
631 partial = partial*10 + c - '0';
632 } else {
633 seconds += TypeLength(c) * partial;
634 partial = 0;
635 }
636 }
637 /* assume the last chunk is seconds (the normal case) */
638 return seconds + partial;
639 }
640
641 static long
642 GetSizeMultiplier(char type)
643 {
644 switch (type) {
645 case 'G': case 'g': return 1024*1024*1024;
646 case 'M': case 'm': return 1024*1024;
647 case 'K': case 'k': return 1024;
648 case 'B': case 'b': return 1;
649 default: return 0;
650 }
651 }
652
653 unsigned long
654 ParseVolume(const char *volume)
655 {
656 unsigned long accum = 0, partial = 0;
657 char c;
658 while ((c = *volume++)) {
659 if (isdigit((int)c)) {
660 partial = partial*10 + c - '0';
661 } else {
662 accum += GetSizeMultiplier(c) * partial;
663 partial = 0;
664 }
665 }
666 return accum + partial;
667 }
668
669 int
670 parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
671 {
672 int accum, pos;
673 unsigned long t_a, t_m;
674
675 t_a = t_m = pos = 0;
676 if (addr)
677 addr->s_addr = htonl(t_a);
678 if (mask)
679 *mask = t_m;
680 while (*str) {
681 if (!isdigit(*str))
682 return 0;
683 accum = 0;
684 do {
685 accum = (accum * 10) + *str++ - '0';
686 } while (isdigit(*str));
687 if (accum > 255)
688 return 0;
689 t_a = (t_a << 8) | accum;
690 t_m = (t_m << 8) | 255;
691 pos += 8;
692 if (*str == '.') {
693 str++;
694 while (*str == '*') {
695 str++;
696 if (*str == '.') {
697 t_a <<= 8;
698 t_m <<= 8;
699 pos += 8;
700 str++;
701 } else if (*str == 0) {
702 t_a <<= 32 - pos;
703 t_m <<= 32 - pos;
704 pos = 32;
705 goto out;
706 } else
707 return 0;
708 }
709 } else if (*str == '/') {
710 int start = pos;
711 accum = 0;
712 do {
713 accum = (accum * 10) + *str++ - '0';
714 } while (isdigit(*str));
715 while (pos < start+accum && pos < 32) {
716 t_a = (t_a << 1) | 0;
717 t_m = (t_m << 1) | 1;
718 pos++;
719 }
720 if (pos != start+accum)
721 return 0;
722 } else if (*str == 0)
723 break;
724 else
725 return 0;
726 }
727 out:
728 if (pos != 32)
729 return 0;
730 if (addr)
731 addr->s_addr = htonl(t_a);
732 if (mask)
733 *mask = t_m;
734 return 1;
735 }
736
737 char *
738 unsplit_string(char *set[], unsigned int max, char *dest)
739 {
740 static char unsplit_buffer[MAXLEN*2];
741 unsigned int ii, jj, pos;
742
743 if (!dest)
744 dest = unsplit_buffer;
745 for (ii=pos=0; ii<max; ii++) {
746 for (jj=0; set[ii][jj]; jj++)
747 dest[pos++] = set[ii][jj];
748 dest[pos++] = ' ';
749 }
750 dest[--pos] = 0;
751 return dest;
752 }
753
754 char *
755 intervalString(char *output, time_t interval, struct handle_info *hi)
756 {
757 static const struct {
758 const char *msg_single;
759 const char *msg_plural;
760 long length;
761 } unit[] = {
762 { "MSG_YEAR", "MSG_YEARS", 365 * 24 * 60 * 60 },
763 { "MSG_WEEK", "MSG_WEEKS", 7 * 24 * 60 * 60 },
764 { "MSG_DAY", "MSG_DAYS", 24 * 60 * 60 },
765 { "MSG_HOUR", "MSG_HOURS", 60 * 60 },
766 { "MSG_MINUTE", "MSG_MINUTES", 60 },
767 { "MSG_SECOND", "MSG_SECONDS", 1 }
768 };
769 struct language *lang;
770 const char *msg;
771 unsigned int type, words, pos, count;
772
773 lang = hi ? hi->language : lang_C;
774 if(!interval)
775 {
776 msg = language_find_message(lang, "MSG_0_SECONDS");
777 return strcpy(output, msg);
778 }
779
780 for (type = 0, words = pos = 0;
781 interval && (words < 2) && (type < ArrayLength(unit));
782 type++) {
783 if (interval < unit[type].length)
784 continue;
785 count = interval / unit[type].length;
786 interval = interval % unit[type].length;
787
788 if (words++ == 1) {
789 msg = language_find_message(lang, "MSG_AND");
790 pos += sprintf(output + pos, "%s ", msg);
791 }
792 if (count == 1)
793 msg = language_find_message(lang, unit[type].msg_single);
794 else
795 msg = language_find_message(lang, unit[type].msg_plural);
796 pos += sprintf(output + pos, "%d%s", count, msg);
797 }
798
799 output[pos] = 0;
800 return output;
801 }
802
803 int
804 getipbyname(const char *name, unsigned long *ip)
805 {
806 struct hostent *he = gethostbyname(name);
807 if (!he)
808 return 0;
809 if (he->h_addrtype != AF_INET)
810 return 0;
811 memcpy(ip, he->h_addr_list[0], sizeof(*ip));
812 return 1;
813 }
814
815 DEFINE_LIST(string_buffer, char)
816
817 void
818 string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len)
819 {
820 while (buf->used + len >= buf->size) {
821 if (!buf->size)
822 buf->size = 16;
823 else
824 buf->size <<= 1;
825 buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
826 }
827 memcpy(buf->list + buf->used, tail, len+1);
828 buf->used += len;
829 }
830
831 void
832 string_buffer_append_string(struct string_buffer *buf, const char *tail)
833 {
834 string_buffer_append_substring(buf, tail, strlen(tail));
835 }
836
837 void
838 string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args)
839 {
840 va_list working;
841 unsigned int len;
842 int ret;
843
844 VA_COPY(working, args);
845 len = strlen(fmt);
846 if (!buf->list || ((buf->used + buf->size) < len)) {
847 buf->size = buf->used + len;
848 buf->list = realloc(buf->list, buf->size);
849 }
850 ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working);
851 if (ret <= 0) {
852 /* pre-C99 behavior; double buffer size until it is big enough */
853 va_end(working);
854 VA_COPY(working, args);
855 while ((ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working)) <= 0) {
856 buf->size += len;
857 buf->list = realloc(buf->list, buf->size);
858 va_end(working);
859 VA_COPY(working, args);
860 }
861 buf->used += ret;
862 } else if (buf->used + ret < buf->size) {
863 /* no need to increase allocation size */
864 buf->used += ret;
865 } else {
866 /* now we know exactly how much space we need */
867 if (buf->size <= buf->used + ret) {
868 buf->size = buf->used + ret + 1;
869 buf->list = realloc(buf->list, buf->size);
870 }
871 va_end(working);
872 VA_COPY(working, args);
873 buf->used += vsnprintf(buf->list + buf->used, buf->size, fmt, working);
874 }
875 va_end(working);
876 va_end(args);
877 }
878
879 void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...)
880 {
881 va_list args;
882 va_start(args, fmt);
883 string_buffer_append_vprintf(buf, fmt, args);
884 }
885
886 void
887 string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl)
888 {
889 unsigned int repl_len = strlen(repl);
890 if (from > buf->used)
891 return;
892 if (len + from > buf->used)
893 len = buf->used - from;
894 buf->used = buf->used + repl_len - len;
895 if (buf->size <= buf->used) {
896 while (buf->used >= buf->size)
897 buf->size <<= 1;
898 buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
899 }
900 memmove(buf->list+from+repl_len, buf->list+from+len, strlen(buf->list+from+len));
901 strcpy(buf->list+from, repl);
902 }
903
904 struct string_list str_tab;
905
906 const char *
907 strtab(unsigned int ii) {
908 if (ii > 65536)
909 return NULL;
910 if (ii > str_tab.size) {
911 unsigned int old_size = str_tab.size;
912 while (ii >= str_tab.size)
913 str_tab.size <<= 1;
914 str_tab.list = realloc(str_tab.list, str_tab.size*sizeof(str_tab.list[0]));
915 memset(str_tab.list+old_size, 0, (str_tab.size-old_size)*sizeof(str_tab.list[0]));
916 }
917 if (!str_tab.list[ii]) {
918 str_tab.list[ii] = malloc(12);
919 sprintf(str_tab.list[ii], "%u", ii);
920 }
921 return str_tab.list[ii];
922 }
923
924 void
925 tools_init(void)
926 {
927 unsigned int upr, lwr;
928 for (lwr=0; lwr<256; ++lwr)
929 tolower(lwr) = lwr;
930 for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr)
931 tolower(upr) = lwr;
932 #ifdef WITH_PROTOCOL_P10
933 for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr)
934 tolower(upr) = lwr;
935 for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr)
936 tolower(upr) = lwr;
937 for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr)
938 tolower(upr) = lwr;
939 #endif
940 str_tab.size = 1001;
941 str_tab.list = calloc(str_tab.size, sizeof(str_tab.list[0]));
942 }
943
944 void
945 tools_cleanup(void)
946 {
947 unsigned int ii;
948 for (ii=0; ii<str_tab.size; ++ii)
949 free(str_tab.list[ii]);
950 free(str_tab.list);
951 }
952
953 /* mysep() is my answer to the strtok/strsep
954 * issue. strsep is nice but doesn't skip
955 * multiple dilimiters, which can really
956 * offset tokens and cause huge corruption
957 * so this function will use strsep but
958 * act like strtok in that sense.
959 */
960 char *mysep(char **sepstr, char *delim)
961 {
962 static char *retstr;
963
964 if(!*sepstr || !**sepstr)
965 return(NULL);
966
967 do
968 {
969 retstr = strsep(sepstr, delim);
970 }while (retstr && !(*retstr));
971
972 return(retstr);
973 }
974
975 char *time2str(time_t thetime)
976 {
977 char *buf, *tmp;
978
979 buf = ctime(&thetime);
980 tmp = (char *)strchr(buf, '\n');
981 *tmp = '\0';
982 return(buf);
983 }
984
985 char* x3_strtok(char **save, char *str, char *fs)
986 {
987 char *pos = *save; /* keep last position across calls */
988 char *tmp;
989
990 if (str)
991 pos = str; /* new string scan */
992
993 while (pos && *pos && strchr(fs, *pos) != NULL)
994 pos++; /* skip leading separators */
995
996 if (!pos || !*pos)
997 return (pos = *save = NULL); /* string contains only sep's */
998
999 tmp = pos; /* now, keep position of the token */
1000
1001 while (*pos && strchr(fs, *pos) == NULL)
1002 pos++; /* skip content of the token */
1003
1004 if (*pos)
1005 *pos++ = '\0'; /* remove first sep after the token */
1006 else
1007 pos = NULL; /* end of string */
1008
1009 *save = pos;
1010 return (tmp);
1011 }
1012
1013 int valid_email(const char *email)
1014 {
1015 unsigned int i;
1016 for (i=0;i<strlen(email);i++)
1017 {
1018 if(!isalnum(email[i]) &&
1019 email[i] != '.' &&
1020 email[i] != '@' &&
1021 email[i] != '-' &&
1022 email[i] != '+' &&
1023 email[i] != '_' )
1024 return false;
1025 }
1026 if(strchr(email, '@') == NULL)
1027 return false;
1028 return true;
1029 }
1030