]>
jfr.im git - irc/evilnet/x3.git/blob - src/tools.c
85912a61c63caf9b554ec00eb3610686362b8a53
1 /* tools.c - miscellaneous utility functions
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of srvx.
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.
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.
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.
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
32 #ifdef HAVE_ARPA_INET_H
33 #include <arpa/inet.h>
37 #define NUMNICKBASE (1 << NUMNICKLOG)
38 #define NUMNICKMASK (NUMNICKBASE - 1)
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','[',']'
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
61 base64toint(const char* s
, int count
)
65 i
= (i
<< NUMNICKLOG
) + convert2n
[(unsigned char)*s
++];
71 const char* inttobase64(char* buf
, unsigned int v
, unsigned int count
)
75 buf
[--count
] = convert2y
[(unsigned char)(v
& NUMNICKMASK
)];
81 static char irc_tolower
[256];
83 #define tolower(X) irc_tolower[(unsigned char)(X)]
86 irccasecmp(const char *stra
, const char *strb
) {
87 while (*stra
&& (tolower(*stra
) == tolower(*strb
)))
89 return tolower(*stra
) - tolower(*strb
);
93 ircncasecmp(const char *stra
, const char *strb
, unsigned int len
) {
95 while (*stra
&& (tolower(*stra
) == tolower(*strb
)) && len
)
96 stra
++, strb
++, len
--;
97 return tolower(*stra
) - tolower(*strb
);
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
)
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
))
114 split_line(char *line
, int irc_colon
, int argv_size
, char *argv
[])
118 while (*line
&& (argc
< argv_size
)) {
121 if (*line
== ':' && irc_colon
&& argc
> 0) {
122 /* the rest is a single parameter */
123 argv
[argc
++] = line
+ 1;
129 if (argc
>= argv_size
)
131 while (*line
!= ' ' && *line
)
137 for (n
=argc
; n
<argv_size
; n
++)
138 argv
[n
] = (char*)0xFEEDBEEF;
143 /* This is ircu's mmatch() function, from match.c. */
144 int mmatch(const char *old_mask
, const char *new_mask
)
146 register const char *m
= old_mask
;
147 register const char *n
= new_mask
;
168 for (m
--; (m
> old_mask
) && (*m
== '?'); m
--)
170 if ((*m
== '*') && (m
> old_mask
) && (m
[-1] != '\\'))
176 /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
177 if ((*na
== '\\') && ((na
[1] == '*') || (na
[1] == '?')))
188 if ((*m
== '\\') && ((m
[1] == '*') || (m
[1] == '?')))
196 /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
197 if ((*n
== '\\') && ((n
[1] == '*') || (n
[1] == '?')))
206 * This `if' has been changed compared to match() to do the following:
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)))
215 * Here `any' also includes \* and \? !
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!)).
221 if ((*m
== '*' && !mq
) ||
222 ((!mq
|| nq
) && tolower(*m
) == tolower(*n
)) ||
223 (*m
== '?' && !mq
&& (*n
!= '*' || nq
)))
236 /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
237 if ((*na
== '\\') && ((na
[1] == '*') || (na
[1] == '?')))
246 match_ircglob(const char *text
, const char *glob
)
248 unsigned int star_p
, q_cnt
;
255 /* intentionally not tolower(...) so people can force
256 * capitalization, or we can overload \ in the future */
257 if (*text
++ != *glob
++)
266 else if (*glob
== '?')
278 /* if this is the last glob character, it will match any text */
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.
285 for (; *text
; text
++) {
286 if ((tolower(*text
) == tolower(*glob
))
287 && match_ircglob(text
+1, glob
+1)) {
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
)
300 while (*text
&& *glob
&& *glob
!= '*' && *glob
!= '?' && *glob
!= '\\') {
301 if (tolower(*text
++) != tolower(*glob
++))
308 extern const char *hidden_host_suffix
;
310 int user_matches_glob(struct userNode
*user
, const char *orig_glob
, int include_nick
)
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
319 char *matchstr_full
= NULL
;
321 if(IsSetHost(user
)) /* S: line sethosts */
323 /* Grab host and user from sethost instead of real host */
325 buff
= alloca(strlen(user
->sethost
) + 1);
326 strcpy(buff
, user
->sethost
);
327 matchstr_user
= mysep(&buff
, "@");
328 matchstr_host
= mysep(&buff
, "@");
330 matchstr_full
= alloca(strlen(user
->nick
) + strlen(matchstr_user
) + strlen(matchstr_host
) + 4);
332 sprintf(matchstr_full
, "%s!%s@%s", user
->nick
, matchstr_user
, matchstr_host
);
334 sprintf(matchstr_full
, "%s@%s", matchstr_user
, matchstr_host
);
335 if(match_ircglob(matchstr_full
, orig_glob
))
338 else if(IsFakeHost(user
)) /* Fakehost */
340 matchstr_full
= alloca(strlen(user
->nick
) + strlen(user
->ident
) + strlen(user
->fakehost
) + 4);
342 sprintf(matchstr_full
, "%s!%s@%s", user
->nick
, user
->ident
, user
->fakehost
);
344 sprintf(matchstr_full
, "%s@%s", user
->ident
, user
->fakehost
);
345 if(match_ircglob(matchstr_full
, orig_glob
))
348 else if(hidden_host_suffix
&& user
->handle_info
) /* name.users.network.org host */
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
;
354 matchstr_full
= alloca(strlen(user
->nick
) + strlen(user
->ident
) + strlen(matchstr_host
) + 4);
356 sprintf(matchstr_full
, "%s!%s@%s", user
->nick
, user
->ident
, matchstr_host
);
358 sprintf(matchstr_full
, "%s@%s", user
->ident
, matchstr_host
);
359 if(match_ircglob(matchstr_full
, orig_glob
))
363 /* Check normal hostname */
364 matchstr_full
= alloca(strlen(user
->nick
) + strlen(user
->ident
) + strlen(user
->hostname
) + 4);
366 sprintf(matchstr_full
, "%s!%s@%s", user
->nick
, user
->ident
, user
->hostname
);
368 sprintf(matchstr_full
, "%s@%s", user
->ident
, user
->hostname
);
369 if(match_ircglob(matchstr_full
, orig_glob
))
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);
376 sprintf(matchstr_full
, "%s!%s@%s", user
->nick
, user
->ident
, matchstr_host
);
378 sprintf(matchstr_full
, "%s@%s", user
->ident
, matchstr_host
);
379 if(match_ircglob(matchstr_full
, orig_glob
))
382 return(0); /* Didnt match anything */
386 user_matches_glob_broken(struct userNode
*user
, const char *orig_glob
, int include_nick
)
389 char *setident
= NULL
, *sethostname
= NULL
; // sethost - reed/apples
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 */
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
);
401 if (!match_ircglob(user
->nick
, glob
)) return 0;
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
);
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;
418 if (!match_ircglob(user
->ident
, glob
) && (IsSetHost(user
) && !match_ircglob(setident
, glob
)))
421 /* If it might be an IP glob, test that. */
422 if (!glob
[strspn(glob
, "0123456789./*?")]
423 && match_ircglob(inet_ntoa(user
->ip
), glob
))
425 /* Check for a fakehost match. */
426 if (IsFakeHost(user
) && match_ircglob(user
->fakehost
, glob
))
428 /* Check for an account match. */
429 if (hidden_host_suffix
&& user
->handle_info
) {
430 char hidden_host
[HOSTLEN
+1];
431 snprintf(hidden_host
, sizeof(hidden_host
), "%s.%s", user
->handle_info
->handle
, hidden_host_suffix
);
432 if (match_ircglob(hidden_host
, glob
))
435 /* None of the above; could only be a hostname match. */
436 return match_ircglob(user
->hostname
, glob
);
440 is_ircmask(const char *text
)
442 while (*text
&& (isalnum((char)*text
) || strchr("-_[]|\\`^{}?*", *text
)))
446 while (*text
&& *text
!= '@' && !isspace((char)*text
))
450 while (*text
&& !isspace((char)*text
))
456 is_gline(const char *text
)
460 text
+= strcspn(text
, "@!% \t\r\n");
465 while (*text
&& (isalnum((char)*text
) || strchr(".-?*:", *text
)))
471 split_ircmask(char *text
, char **nick
, char **ident
, char **host
)
476 while (isalnum((char)*text
) || strchr("=[]\\`^{}?*", *text
))
478 if (*text
!= '!' || ((text
- start
) > NICKLEN
))
485 while (*text
&& *text
!= '@' && !isspace((char)*text
))
487 if (*text
!= '@' || ((text
- start
) > USERLEN
))
494 while (*text
&& (isalnum((char)*text
) || strchr(".-?*:", *text
)))
498 return !*text
&& ((text
- start
) <= HOSTLEN
) && nick
&& ident
&& host
;
502 sanitize_ircmask(char *input
)
504 unsigned int length
, flag
;
505 char *mask
, *start
, *output
;
507 /* Sanitize everything in place; input *must* be a valid
512 /* The nick is truncated at the end. */
515 while(*input
++ != '!')
524 /* This flag is used to indicate following parts should
533 /* The ident and host must be truncated at the beginning and
534 replaced with a '*' to be compatible with ircu. */
537 while(*input
++ != '@')
541 if(length
> USERLEN
|| flag
)
545 start
= input
- USERLEN
;
567 if(length
> HOSTLEN
|| flag
)
571 start
= input
- HOSTLEN
;
585 TypeLength(char type
)
588 case 'y': return 365*24*60*60;
589 case 'M': return 31*24*60*60;
590 case 'w': return 7*24*60*60;
591 case 'd': return 24*60*60;
592 case 'h': return 60*60;
600 ParseInterval(const char *interval
)
602 unsigned long seconds
= 0;
606 /* process the string, resetting the count if we find a unit character */
607 while ((c
= *interval
++)) {
608 if (isdigit((int)c
)) {
609 partial
= partial
*10 + c
- '0';
611 seconds
+= TypeLength(c
) * partial
;
615 /* assume the last chunk is seconds (the normal case) */
616 return seconds
+ partial
;
620 GetSizeMultiplier(char type
)
623 case 'G': case 'g': return 1024*1024*1024;
624 case 'M': case 'm': return 1024*1024;
625 case 'K': case 'k': return 1024;
626 case 'B': case 'b': return 1;
632 ParseVolume(const char *volume
)
634 unsigned long accum
= 0, partial
= 0;
636 while ((c
= *volume
++)) {
637 if (isdigit((int)c
)) {
638 partial
= partial
*10 + c
- '0';
640 accum
+= GetSizeMultiplier(c
) * partial
;
644 return accum
+ partial
;
648 parse_ipmask(const char *str
, struct in_addr
*addr
, unsigned long *mask
)
651 unsigned long t_a
, t_m
;
655 addr
->s_addr
= htonl(t_a
);
663 accum
= (accum
* 10) + *str
++ - '0';
664 } while (isdigit(*str
));
667 t_a
= (t_a
<< 8) | accum
;
668 t_m
= (t_m
<< 8) | 255;
672 while (*str
== '*') {
679 } else if (*str
== 0) {
687 } else if (*str
== '/') {
691 accum
= (accum
* 10) + *str
++ - '0';
692 } while (isdigit(*str
));
693 while (pos
< start
+accum
&& pos
< 32) {
694 t_a
= (t_a
<< 1) | 0;
695 t_m
= (t_m
<< 1) | 1;
698 if (pos
!= start
+accum
)
700 } else if (*str
== 0)
709 addr
->s_addr
= htonl(t_a
);
716 unsplit_string(char *set
[], unsigned int max
, char *dest
)
718 static char unsplit_buffer
[MAXLEN
*2];
719 unsigned int ii
, jj
, pos
;
722 dest
= unsplit_buffer
;
723 for (ii
=pos
=0; ii
<max
; ii
++) {
724 for (jj
=0; set
[ii
][jj
]; jj
++)
725 dest
[pos
++] = set
[ii
][jj
];
733 intervalString(char *output
, time_t interval
, struct handle_info
*hi
)
735 static const struct {
736 const char *msg_single
;
737 const char *msg_plural
;
740 { "MSG_YEAR", "MSG_YEARS", 365 * 24 * 60 * 60 },
741 { "MSG_WEEK", "MSG_WEEKS", 7 * 24 * 60 * 60 },
742 { "MSG_DAY", "MSG_DAYS", 24 * 60 * 60 },
743 { "MSG_HOUR", "MSG_HOURS", 60 * 60 },
744 { "MSG_MINUTE", "MSG_MINUTES", 60 },
745 { "MSG_SECOND", "MSG_SECONDS", 1 }
747 struct language
*lang
;
749 unsigned int type
, words
, pos
, count
;
751 lang
= hi
? hi
->language
: lang_C
;
754 msg
= language_find_message(lang
, "MSG_0_SECONDS");
755 return strcpy(output
, msg
);
758 for (type
= 0, words
= pos
= 0;
759 interval
&& (words
< 2) && (type
< ArrayLength(unit
));
761 if (interval
< unit
[type
].length
)
763 count
= interval
/ unit
[type
].length
;
764 interval
= interval
% unit
[type
].length
;
767 msg
= language_find_message(lang
, "MSG_AND");
768 pos
+= sprintf(output
+ pos
, " %s ", msg
);
771 msg
= language_find_message(lang
, unit
[type
].msg_single
);
773 msg
= language_find_message(lang
, unit
[type
].msg_plural
);
774 pos
+= sprintf(output
+ pos
, "%d %s", count
, msg
);
782 getipbyname(const char *name
, unsigned long *ip
)
784 struct hostent
*he
= gethostbyname(name
);
787 if (he
->h_addrtype
!= AF_INET
)
789 memcpy(ip
, he
->h_addr_list
[0], sizeof(*ip
));
793 DEFINE_LIST(string_buffer
, char)
796 string_buffer_append_substring(struct string_buffer
*buf
, const char *tail
, unsigned int len
)
798 while (buf
->used
+ len
>= buf
->size
) {
803 buf
->list
= realloc(buf
->list
, buf
->size
*sizeof(buf
->list
[0]));
805 memcpy(buf
->list
+ buf
->used
, tail
, len
+1);
810 string_buffer_append_string(struct string_buffer
*buf
, const char *tail
)
812 string_buffer_append_substring(buf
, tail
, strlen(tail
));
816 string_buffer_append_vprintf(struct string_buffer
*buf
, const char *fmt
, va_list args
)
822 VA_COPY(working
, args
);
824 if (!buf
->list
|| ((buf
->used
+ buf
->size
) < len
)) {
825 buf
->size
= buf
->used
+ len
;
826 buf
->list
= realloc(buf
->list
, buf
->size
);
828 ret
= vsnprintf(buf
->list
+ buf
->used
, buf
->size
- buf
->used
, fmt
, working
);
830 /* pre-C99 behavior; double buffer size until it is big enough */
832 VA_COPY(working
, args
);
833 while ((ret
= vsnprintf(buf
->list
+ buf
->used
, buf
->size
- buf
->used
, fmt
, working
)) <= 0) {
835 buf
->list
= realloc(buf
->list
, buf
->size
);
837 VA_COPY(working
, args
);
840 } else if (buf
->used
+ ret
< buf
->size
) {
841 /* no need to increase allocation size */
844 /* now we know exactly how much space we need */
845 if (buf
->size
<= buf
->used
+ ret
) {
846 buf
->size
= buf
->used
+ ret
+ 1;
847 buf
->list
= realloc(buf
->list
, buf
->size
);
850 VA_COPY(working
, args
);
851 buf
->used
+= vsnprintf(buf
->list
+ buf
->used
, buf
->size
, fmt
, working
);
857 void string_buffer_append_printf(struct string_buffer
*buf
, const char *fmt
, ...)
861 string_buffer_append_vprintf(buf
, fmt
, args
);
865 string_buffer_replace(struct string_buffer
*buf
, unsigned int from
, unsigned int len
, const char *repl
)
867 unsigned int repl_len
= strlen(repl
);
868 if (from
> buf
->used
)
870 if (len
+ from
> buf
->used
)
871 len
= buf
->used
- from
;
872 buf
->used
= buf
->used
+ repl_len
- len
;
873 if (buf
->size
<= buf
->used
) {
874 while (buf
->used
>= buf
->size
)
876 buf
->list
= realloc(buf
->list
, buf
->size
*sizeof(buf
->list
[0]));
878 memmove(buf
->list
+from
+repl_len
, buf
->list
+from
+len
, strlen(buf
->list
+from
+len
));
879 strcpy(buf
->list
+from
, repl
);
882 struct string_list str_tab
;
885 strtab(unsigned int ii
) {
888 if (ii
> str_tab
.size
) {
889 unsigned int old_size
= str_tab
.size
;
890 while (ii
>= str_tab
.size
)
892 str_tab
.list
= realloc(str_tab
.list
, str_tab
.size
*sizeof(str_tab
.list
[0]));
893 memset(str_tab
.list
+old_size
, 0, (str_tab
.size
-old_size
)*sizeof(str_tab
.list
[0]));
895 if (!str_tab
.list
[ii
]) {
896 str_tab
.list
[ii
] = malloc(12);
897 sprintf(str_tab
.list
[ii
], "%u", ii
);
899 return str_tab
.list
[ii
];
905 unsigned int upr
, lwr
;
906 for (lwr
=0; lwr
<256; ++lwr
)
908 for (upr
='A', lwr
='a'; lwr
<= 'z'; ++upr
, ++lwr
)
910 #ifdef WITH_PROTOCOL_P10
911 for (upr
='[', lwr
='{'; lwr
<= '~'; ++upr
, ++lwr
)
913 for (upr
=0xc0, lwr
=0xe0; lwr
<= 0xf6; ++upr
, ++lwr
)
915 for (upr
=0xd8, lwr
=0xf8; lwr
<= 0xfe; ++upr
, ++lwr
)
919 str_tab
.list
= calloc(str_tab
.size
, sizeof(str_tab
.list
[0]));
926 for (ii
=0; ii
<str_tab
.size
; ++ii
)
927 free(str_tab
.list
[ii
]);
931 /* mysep() is my answer to the strtok/strsep
932 * issue. strsep is nice but doesn't skip
933 * multiple dilimiters, which can really
934 * offset tokens and cause huge corruption
935 * so this function will use strsep but
936 * act like strtok in that sence.
938 char *mysep(char **sepstr
, char *delim
)
942 if(!*sepstr
|| !**sepstr
)
947 retstr
= strsep(sepstr
, delim
);
948 }while (retstr
&& !(*retstr
));
953 char *time2str(time_t thetime
)
957 buf
= ctime(&thetime
);
958 tmp
= (char *)strchr(buf
, '\n');