]>
jfr.im git - irc/evilnet/x3.git/blob - src/tools.c
1 /* tools.c - miscellaneous utility functions
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
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 if (IsSetHost(user
) && match_ircglob(sethostname
, glob
))
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
))
437 /* None of the above; could only be a hostname match. */
438 return match_ircglob(user
->hostname
, glob
);
442 is_ircmask(const char *text
)
444 while (*text
&& (isalnum((char)*text
) || strchr("-_[]|\\`^{}?*", *text
)))
448 while (*text
&& *text
!= '@' && !isspace((char)*text
))
452 while (*text
&& !isspace((char)*text
))
458 is_gline(const char *text
)
462 text
+= strcspn(text
, "@!% \t\r\n");
467 while (*text
&& (isalnum((char)*text
) || strchr(".-?*:", *text
)))
473 split_ircmask(char *text
, char **nick
, char **ident
, char **host
)
478 while (isalnum((char)*text
) || strchr("=[]\\`^{}?*", *text
))
480 if (*text
!= '!' || ((text
- start
) > NICKLEN
))
487 while (*text
&& *text
!= '@' && !isspace((char)*text
))
489 if (*text
!= '@' || ((text
- start
) > USERLEN
))
496 while (*text
&& (isalnum((char)*text
) || strchr(".-?*:", *text
)))
500 return !*text
&& ((text
- start
) <= HOSTLEN
) && nick
&& ident
&& host
;
504 sanitize_ircmask(char *input
)
506 unsigned int length
, flag
;
507 char *mask
, *start
, *output
;
509 /* Sanitize everything in place; input *must* be a valid
514 /* The nick is truncated at the end. */
517 while(*input
++ != '!')
526 /* This flag is used to indicate following parts should
535 /* The ident and host must be truncated at the beginning and
536 replaced with a '*' to be compatible with ircu. */
539 while(*input
++ != '@')
543 if(length
> USERLEN
|| flag
)
547 start
= input
- USERLEN
;
569 if(length
> HOSTLEN
|| flag
)
573 start
= input
- HOSTLEN
;
587 TypeLength(char 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;
602 ParseInterval(const char *interval
)
604 unsigned long seconds
= 0;
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';
613 seconds
+= TypeLength(c
) * partial
;
617 /* assume the last chunk is seconds (the normal case) */
618 return seconds
+ partial
;
622 GetSizeMultiplier(char 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;
634 ParseVolume(const char *volume
)
636 unsigned long accum
= 0, partial
= 0;
638 while ((c
= *volume
++)) {
639 if (isdigit((int)c
)) {
640 partial
= partial
*10 + c
- '0';
642 accum
+= GetSizeMultiplier(c
) * partial
;
646 return accum
+ partial
;
650 parse_ipmask(const char *str
, struct in_addr
*addr
, unsigned long *mask
)
653 unsigned long t_a
, t_m
;
657 addr
->s_addr
= htonl(t_a
);
665 accum
= (accum
* 10) + *str
++ - '0';
666 } while (isdigit(*str
));
669 t_a
= (t_a
<< 8) | accum
;
670 t_m
= (t_m
<< 8) | 255;
674 while (*str
== '*') {
681 } else if (*str
== 0) {
689 } else if (*str
== '/') {
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;
700 if (pos
!= start
+accum
)
702 } else if (*str
== 0)
711 addr
->s_addr
= htonl(t_a
);
718 unsplit_string(char *set
[], unsigned int max
, char *dest
)
720 static char unsplit_buffer
[MAXLEN
*2];
721 unsigned int ii
, jj
, pos
;
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
];
735 intervalString(char *output
, time_t interval
, struct handle_info
*hi
)
737 static const struct {
738 const char *msg_single
;
739 const char *msg_plural
;
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 }
749 struct language
*lang
;
751 unsigned int type
, words
, pos
, count
;
753 lang
= hi
? hi
->language
: lang_C
;
756 msg
= language_find_message(lang
, "MSG_0_SECONDS");
757 return strcpy(output
, msg
);
760 for (type
= 0, words
= pos
= 0;
761 interval
&& (words
< 2) && (type
< ArrayLength(unit
));
763 if (interval
< unit
[type
].length
)
765 count
= interval
/ unit
[type
].length
;
766 interval
= interval
% unit
[type
].length
;
769 msg
= language_find_message(lang
, "MSG_AND");
770 pos
+= sprintf(output
+ pos
, "%s ", msg
);
773 msg
= language_find_message(lang
, unit
[type
].msg_single
);
775 msg
= language_find_message(lang
, unit
[type
].msg_plural
);
776 pos
+= sprintf(output
+ pos
, "%d%s", count
, msg
);
784 getipbyname(const char *name
, unsigned long *ip
)
786 struct hostent
*he
= gethostbyname(name
);
789 if (he
->h_addrtype
!= AF_INET
)
791 memcpy(ip
, he
->h_addr_list
[0], sizeof(*ip
));
795 DEFINE_LIST(string_buffer
, char)
798 string_buffer_append_substring(struct string_buffer
*buf
, const char *tail
, unsigned int len
)
800 while (buf
->used
+ len
>= buf
->size
) {
805 buf
->list
= realloc(buf
->list
, buf
->size
*sizeof(buf
->list
[0]));
807 memcpy(buf
->list
+ buf
->used
, tail
, len
+1);
812 string_buffer_append_string(struct string_buffer
*buf
, const char *tail
)
814 string_buffer_append_substring(buf
, tail
, strlen(tail
));
818 string_buffer_append_vprintf(struct string_buffer
*buf
, const char *fmt
, va_list args
)
824 VA_COPY(working
, args
);
826 if (!buf
->list
|| ((buf
->used
+ buf
->size
) < len
)) {
827 buf
->size
= buf
->used
+ len
;
828 buf
->list
= realloc(buf
->list
, buf
->size
);
830 ret
= vsnprintf(buf
->list
+ buf
->used
, buf
->size
- buf
->used
, fmt
, working
);
832 /* pre-C99 behavior; double buffer size until it is big enough */
834 VA_COPY(working
, args
);
835 while ((ret
= vsnprintf(buf
->list
+ buf
->used
, buf
->size
- buf
->used
, fmt
, working
)) <= 0) {
837 buf
->list
= realloc(buf
->list
, buf
->size
);
839 VA_COPY(working
, args
);
842 } else if (buf
->used
+ ret
< buf
->size
) {
843 /* no need to increase allocation size */
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
);
852 VA_COPY(working
, args
);
853 buf
->used
+= vsnprintf(buf
->list
+ buf
->used
, buf
->size
, fmt
, working
);
859 void string_buffer_append_printf(struct string_buffer
*buf
, const char *fmt
, ...)
863 string_buffer_append_vprintf(buf
, fmt
, args
);
867 string_buffer_replace(struct string_buffer
*buf
, unsigned int from
, unsigned int len
, const char *repl
)
869 unsigned int repl_len
= strlen(repl
);
870 if (from
> buf
->used
)
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
)
878 buf
->list
= realloc(buf
->list
, buf
->size
*sizeof(buf
->list
[0]));
880 memmove(buf
->list
+from
+repl_len
, buf
->list
+from
+len
, strlen(buf
->list
+from
+len
));
881 strcpy(buf
->list
+from
, repl
);
884 struct string_list str_tab
;
887 strtab(unsigned int ii
) {
890 if (ii
> str_tab
.size
) {
891 unsigned int old_size
= str_tab
.size
;
892 while (ii
>= str_tab
.size
)
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]));
897 if (!str_tab
.list
[ii
]) {
898 str_tab
.list
[ii
] = malloc(12);
899 sprintf(str_tab
.list
[ii
], "%u", ii
);
901 return str_tab
.list
[ii
];
907 unsigned int upr
, lwr
;
908 for (lwr
=0; lwr
<256; ++lwr
)
910 for (upr
='A', lwr
='a'; lwr
<= 'z'; ++upr
, ++lwr
)
912 #ifdef WITH_PROTOCOL_P10
913 for (upr
='[', lwr
='{'; lwr
<= '~'; ++upr
, ++lwr
)
915 for (upr
=0xc0, lwr
=0xe0; lwr
<= 0xf6; ++upr
, ++lwr
)
917 for (upr
=0xd8, lwr
=0xf8; lwr
<= 0xfe; ++upr
, ++lwr
)
921 str_tab
.list
= calloc(str_tab
.size
, sizeof(str_tab
.list
[0]));
928 for (ii
=0; ii
<str_tab
.size
; ++ii
)
929 free(str_tab
.list
[ii
]);
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.
940 char *mysep(char **sepstr
, char *delim
)
944 if(!*sepstr
|| !**sepstr
)
949 retstr
= strsep(sepstr
, delim
);
950 }while (retstr
&& !(*retstr
));
955 char *time2str(time_t thetime
)
959 buf
= ctime(&thetime
);
960 tmp
= (char *)strchr(buf
, '\n');