X-Git-Url: https://jfr.im/git/irc/evilnet/x3.git/blobdiff_plain/2f61d1d75ac52f239e760ecea02332eedc4682bd..bc06f607a61a8d0c48642bc859998f34346024b8:/src/tools.c diff --git a/src/tools.c b/src/tools.c index 4849e85..7d1fdcd 100644 --- a/src/tools.c +++ b/src/tools.c @@ -3,7 +3,7 @@ * * This file is part of x3. * - * srvx is free software; you can redistribute it and/or modify + * x3 is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. @@ -357,6 +357,14 @@ static char irc_tolower[256]; #undef tolower #define tolower(X) irc_tolower[(unsigned char)(X)] +void +irc_strtolower(char *str) { + char *p; + for(p = str;*p;p++) { + *p = tolower(*p); + } +} + int irccasecmp(const char *stra, const char *strb) { while (*stra && (tolower(*stra) == tolower(*strb))) @@ -625,6 +633,15 @@ user_matches_glob(struct userNode *user, const char *orig_glob, int flags) if (match_ircglob(hidden_host, glob)) return 1; } + + /* Match crypt hostname */ + if (match_ircglob(user->crypthost, glob)) + return 1; + + /* Match crypt IP */ + if (match_ircglob(user->cryptip, glob)) + return 1; + /* If only matching the visible hostnames, bail early. */ if ((flags & MATCH_VISIBLE) && IsHiddenHost(user) && (IsFakeHost(user) || (hidden_host_suffix && user->handle_info))) @@ -1102,6 +1119,23 @@ char *mysep(char **sepstr, char *delim) return(retstr); } +/* Mallocing snprintf * + * + * If it overruns size, it will simply be safely truncated. + */ +char * +x3_msnprintf(const int size, const char *format, ...) +{ + va_list ap; + char* buff = calloc(sizeof(char *), size+1); + + va_start(ap, format); + vsnprintf(buff, size, format, ap); + va_end(ap); + buff = realloc(buff, strlen(buff) + 1); + return buff; +} + char *time2str(time_t thetime) { char *buf, *tmp; @@ -1158,3 +1192,125 @@ int valid_email(const char *email) return true; } +/* + * Create a string of form "foo!bar@fubar" given foo, bar and fubar + * as the parameters. If NULL, they become "*". + */ +#define NUH_BUFSIZE (NICKLEN + USERLEN + HOSTLEN + 10) +static char *make_nick_user_host(char *namebuf, const char *nick, + const char *name, const char *host) +{ + snprintf(namebuf, NUH_BUFSIZE, "%s!%s@%s", nick, name, host); + return namebuf; +} + +/* + * pretty_mask + * + * by Carlo Wood (Run), 05 Oct 1998. + * + * Canonify a mask. + * + * When the nick is longer then NICKLEN, it is cut off (its an error of course). + * When the user name or host name are too long (USERLEN and HOSTLEN + * respectively) then they are cut off at the start with a '*'. + * + * The following transformations are made: + * + * 1) xxx -> nick!*@* + * 2) xxx.xxx -> *!*@host + * 3) xxx!yyy -> nick!user@* + * 4) xxx@yyy -> *!user@host + * 5) xxx!yyy@zzz -> nick!user@host + */ +char *pretty_mask(char *mask) +{ + static char star[2] = { '*', 0 }; + static char retmask[NUH_BUFSIZE] = ""; + char *last_dot = NULL; + char *ptr = NULL; + + /* Case 1: default */ + char *nick = mask; + char *user = star; + char *host = star; + + /* Do a _single_ pass through the characters of the mask: */ + for (ptr = mask; *ptr; ++ptr) + { + if (*ptr == '!') + { + /* Case 3 or 5: Found first '!' (without finding a '@' yet) */ + user = ++ptr; + host = star; + } + else if (*ptr == '@') + { + /* Case 4: Found last '@' (without finding a '!' yet) */ + nick = star; + user = mask; + host = ++ptr; + } + else if (*ptr == '.') + { + /* Case 2: Found last '.' (without finding a '!' or '@' yet) */ + last_dot = ptr; + continue; + } + else + continue; + for (; *ptr; ++ptr) + { + if (*ptr == '@') + { + /* Case 4 or 5: Found last '@' */ + host = ptr + 1; + } + } + break; + } + if (user == star && last_dot) + { + /* Case 2: */ + nick = star; + user = star; + host = mask; + } + /* Check lengths */ + if (nick != star) + { + char *nick_end = (user != star) ? user - 1 : ptr; + if (nick_end - nick > NICKLEN) + nick[NICKLEN] = 0; + *nick_end = 0; + } + if (user != star) + { + char *user_end = (host != star) ? host - 1 : ptr; + if (user_end - user > USERLEN) + { + user = user_end - USERLEN; + *user = '*'; + } + *user_end = 0; + } + if (host != star && ptr - host > HOSTLEN) + { + host = ptr - HOSTLEN; + *host = '*'; + } + return make_nick_user_host(retmask, nick, user, host); +} + +int str_is_number(const char *str) +{ + char *ptr; + int ret = false; + for(ptr = (char *)str;*ptr;ptr++) { + if((*ptr >= '0' && *ptr <= '9') || *ptr == '-') + ret = true; + else + return false; + } + return ret; +}