miscellaneous bugfixes and indentation fixes
[srvx.git] / src / tools.c
index 502668435a1969d5b316c02c0af6c0af9fb7f400..53214dc56775256348582b1548debfd660db497b 100644 (file)
@@ -1,11 +1,12 @@
 /* tools.c - miscellaneous utility functions
  * Copyright 2000-2004 srvx Development Team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This file is part of srvx.
+ *
+ * srvx 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.  Important limitations are
- * listed in the COPYING file that accompanies this software.
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, email srvx-maintainers@srvx.net.
+ * along with srvx; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
 
+#include "helpfile.h"
 #include "log.h"
 #include "nickserv.h"
 #include "recdb.h"
@@ -113,7 +116,8 @@ split_line(char *line, int irc_colon, int argv_size, char *argv[])
     int argc = 0;
     int n;
     while (*line && (argc < argv_size)) {
-       while (*line == ' ') *line++ = 0;
+       while (*line == ' ')
+            *line++ = 0;
        if (*line == ':' && irc_colon && argc > 0) {
            /* the rest is a single parameter */
            argv[argc++] = line + 1;
@@ -124,14 +128,14 @@ split_line(char *line, int irc_colon, int argv_size, char *argv[])
        argv[argc++] = line;
        if (argc >= argv_size)
             break;
-       while (*line != ' ' && *line) line++;
+       while (*line != ' ' && *line)
+            line++;
     }
 #ifdef NDEBUG
     n = 0;
 #else
-    for (n=argc; n<argv_size; n++) {
+    for (n=argc; n<argv_size; n++)
         argv[n] = (char*)0xFEEDBEEF;
-    }
 #endif
     return argc;
 }
@@ -250,21 +254,30 @@ match_ircglob(const char *text, const char *glob)
             glob++;
             /* intentionally not tolower(...) so people can force
              * capitalization, or we can overload \ in the future */
-            if (*text++ != *glob++) return 0;
+            if (*text++ != *glob++)
+                return 0;
             break;
        case '*':
         case '?':
             star_p = q_cnt = 0;
             do {
-                if (*glob == '*') star_p = 1;
-                else if (*glob == '?') q_cnt++;
-                else break;
+                if (*glob == '*')
+                    star_p = 1;
+                else if (*glob == '?')
+                    q_cnt++;
+                else
+                    break;
                 glob++;
             } while (1);
-            while (q_cnt) { if (!*text++) return 0; q_cnt--; }
+            while (q_cnt) {
+                if (!*text++)
+                    return 0;
+                q_cnt--;
+            }
             if (star_p) {
                 /* if this is the last glob character, it will match any text */
-                if (!*glob) return 1;
+                if (!*glob)
+                    return 1;
                 /* Thanks to the loop above, we know that the next
                  * character is a normal character.  So just look for
                  * the right character.
@@ -279,11 +292,14 @@ match_ircglob(const char *text, const char *glob)
             }
             /* if !star_p, fall through to normal character case,
              * first checking to see if ?s carried us to the end */
-            if (!*glob && !*text) return 1;
+            if (!*glob && !*text)
+                return 1;
        default:
-           if (!*text) return 0;
+           if (!*text)
+                return 0;
            while (*text && *glob && *glob != '*' && *glob != '?' && *glob != '\\') {
-               if (tolower(*text++) != tolower(*glob++)) return 0;
+               if (tolower(*text++) != tolower(*glob++))
+                    return 0;
            }
        }
     }
@@ -324,6 +340,8 @@ user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick
         return match_ircglob(inet_ntoa(user->ip), glob);
     } else {
         /* The host part of the mask isn't IP-based */
+        if (IsFakeHost(user) && match_ircglob(user->fakehost, glob))
+            return 1;
         if (hidden_host_suffix && user->handle_info) {
             char hidden_host[HOSTLEN+1];
             snprintf(hidden_host, sizeof(hidden_host), "%s.%s", user->handle_info->handle, hidden_host_suffix);
@@ -337,22 +355,31 @@ user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick
 int
 is_ircmask(const char *text)
 {
-    while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text))) text++;
-    if (*text++ != '!') return 0;
-    while (*text && *text != '@' && !isspace((char)*text)) text++;
-    if (*text++ != '@') return 0;
-    while (*text && !isspace((char)*text)) text++;
+    while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text)))
+        text++;
+    if (*text++ != '!')
+        return 0;
+    while (*text && *text != '@' && !isspace((char)*text))
+        text++;
+    if (*text++ != '@')
+        return 0;
+    while (*text && !isspace((char)*text))
+        text++;
     return !*text;
 }
 
 int
 is_gline(const char *text)
 {
-    if (*text == '@') return 0;
+    if (*text == '@')
+        return 0;
     text += strcspn(text, "@!% \t\r\n");
-    if (*text++ != '@') return 0;
-    if (!*text) return 0;
-    while (*text && (isalnum((char)*text) || strchr(".-?*", *text))) text++;
+    if (*text++ != '@')
+        return 0;
+    if (!*text)
+        return 0;
+    while (*text && (isalnum((char)*text) || strchr(".-?*", *text)))
+        text++;
     return !*text;
 }
 
@@ -362,20 +389,28 @@ split_ircmask(char *text, char **nick, char **ident, char **host)
     char *start;
 
     start = text;
-    while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text)) text++;
-    if (*text != '!' || ((text - start) > NICKLEN)) return 0;
+    while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text))
+        text++;
+    if (*text != '!' || ((text - start) > NICKLEN))
+        return 0;
     *text = 0;
-    if (nick) *nick = start;
+    if (nick)
+        *nick = start;
 
     start = ++text;
-    while (*text && *text != '@' && !isspace((char)*text)) text++;
-    if (*text != '@' || ((text - start) > USERLEN)) return 0;
+    while (*text && *text != '@' && !isspace((char)*text))
+        text++;
+    if (*text != '@' || ((text - start) > USERLEN))
+        return 0;
     *text = 0;
-    if (ident) *ident = start;
+    if (ident)
+        *ident = start;
     
     start = ++text;
-    while (*text && (isalnum((char)*text) || strchr(".-?*", *text))) text++;
-    if (host) *host = start;
+    while (*text && (isalnum((char)*text) || strchr(".-?*", *text)))
+        text++;
+    if (host)
+        *host = start;
     return !*text && ((text - start) <= HOSTLEN) && nick && ident && host;
 }
 
@@ -532,15 +567,19 @@ parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
     unsigned long t_a, t_m;
 
     t_a = t_m = pos = 0;
-    if (addr) addr->s_addr = htonl(t_a);
-    if (mask) *mask = t_m;
+    if (addr)
+        addr->s_addr = htonl(t_a);
+    if (mask)
+        *mask = t_m;
     while (*str) {
-        if (!isdigit(*str)) return 0;
+        if (!isdigit(*str))
+            return 0;
         accum = 0;
         do {
             accum = (accum * 10) + *str++ - '0';
         } while (isdigit(*str));
-        if (accum > 255) return 0;
+        if (accum > 255)
+            return 0;
         t_a = (t_a << 8) | accum;
         t_m = (t_m << 8) | 255;
         pos += 8;
@@ -558,9 +597,8 @@ parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
                     t_m <<= 32 - pos;
                     pos = 32;
                     goto out;
-                } else {
+                } else
                     return 0;
-                }
             }
         } else if (*str == '/') {
             int start = pos;
@@ -573,17 +611,20 @@ parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
                 t_m = (t_m << 1) | 1;
                 pos++;
             }
-            if (pos != start+accum) return 0;
-        } else if (*str == 0) {
+            if (pos != start+accum)
+                return 0;
+        } else if (*str == 0)
             break;
-        } else {
+        else
             return 0;
-        }
     }
 out:
-    if (pos != 32) return 0;
-    if (addr) addr->s_addr = htonl(t_a);
-    if (mask) *mask = t_m;
+    if (pos != 32)
+        return 0;
+    if (addr)
+        addr->s_addr = htonl(t_a);
+    if (mask)
+        *mask = t_m;
     return 1;
 }
 
@@ -593,11 +634,11 @@ unsplit_string(char *set[], unsigned int max, char *dest)
     static char unsplit_buffer[MAXLEN*2];
     unsigned int ii, jj, pos;
 
-    if (!dest) dest = unsplit_buffer;
+    if (!dest)
+        dest = unsplit_buffer;
     for (ii=pos=0; ii<max; ii++) {
-        for (jj=0; set[ii][jj]; jj++) {
+        for (jj=0; set[ii][jj]; jj++)
             dest[pos++] = set[ii][jj];
-        }
         dest[pos++] = ' ';
     }
     dest[--pos] = 0;
@@ -605,25 +646,29 @@ unsplit_string(char *set[], unsigned int max, char *dest)
 }
 
 char *
-intervalString2(char *output, time_t interval, int brief)
+intervalString(char *output, time_t interval, struct handle_info *hi)
 {
     static const struct {
-        const char *name;
+        const char *msg_single;
+        const char *msg_plural;
         long length;
     } unit[] = {
-        { "year", 365 * 24 * 60 * 60 },
-        { "week",   7 * 24 * 60 * 60 },
-        { "day",        24 * 60 * 60 },
-        { "hour",            60 * 60 },
-        { "minute",               60 },
-        { "second",                1 }
+        { "MSG_YEAR",   "MSG_YEARS", 365 * 24 * 60 * 60 },
+        { "MSG_WEEK",   "MSG_WEEKS",   7 * 24 * 60 * 60 },
+        { "MSG_DAY",    "MSG_DAYS",        24 * 60 * 60 },
+        { "MSG_HOUR",   "MSG_HOURS",            60 * 60 },
+        { "MSG_MINUTE", "MSG_MINUTES",               60 },
+        { "MSG_SECOND", "MSG_SECONDS",                1 }
     };
+    struct language *lang;
+    const char *msg;
     unsigned int type, words, pos, count;
 
+    lang = hi ? hi->language : lang_C;
     if(!interval)
     {
-       strcpy(output, brief ? "0s" : "0 seconds");
-       return output;
+        msg = language_find_message(lang, "MSG_0_SECONDS");
+       return strcpy(output, msg);
     }
 
     for (type = 0, words = pos = 0;
@@ -634,15 +679,15 @@ intervalString2(char *output, time_t interval, int brief)
         count = interval / unit[type].length;
         interval = interval % unit[type].length;
 
-        if (brief)
-            pos += sprintf(output + pos, "%d%c", count, unit[type].name[0]);
-        else if (words == 1)
-            pos += sprintf(output + pos, " and %d %s", count, unit[type].name);
+        if (words++ == 1) {
+            msg = language_find_message(lang, "MSG_AND");
+            pos += sprintf(output + pos, " %s ", msg);
+        }
+        if (count == 1)
+            msg = language_find_message(lang, unit[type].msg_single);
         else
-            pos += sprintf(output + pos, "%d %s", count, unit[type].name);
-        if (count != 1)
-            output[pos++] = 's';
-        words++;
+            msg = language_find_message(lang, unit[type].msg_plural);
+        pos += sprintf(output + pos, "%d %s", count, msg);
     }
 
     output[pos] = 0;
@@ -653,14 +698,12 @@ int
 getipbyname(const char *name, unsigned long *ip)
 {
     struct hostent *he = gethostbyname(name);
-    if (he) {
-       if (he->h_addrtype != AF_INET)
-            return 0;
-       memcpy(ip, he->h_addr_list[0], sizeof(*ip));
-       return 1;
-    } else {
-       return 0;
-    }
+    if (!he)
+        return 0;
+    if (he->h_addrtype != AF_INET)
+        return 0;
+    memcpy(ip, he->h_addr_list[0], sizeof(*ip));
+    return 1;
 }
 
 DEFINE_LIST(string_buffer, char)
@@ -703,7 +746,7 @@ string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list
         /* pre-C99 behavior; double buffer size until it is big enough */
         va_end(working);
         VA_COPY(working, args);
-        while ((ret = vsnprintf(buf->list + buf->used, buf->size, fmt, working)) == -1) {
+        while ((ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working)) <= 0) {
             buf->size += len;
             buf->list = realloc(buf->list, buf->size);
             va_end(working);
@@ -738,13 +781,14 @@ void
 string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl)
 {
     unsigned int repl_len = strlen(repl);
-    if (from > buf->used) return;
-    if (len + from > buf->used) len = buf->used - from;
+    if (from > buf->used)
+        return;
+    if (len + from > buf->used)
+        len = buf->used - from;
     buf->used = buf->used + repl_len - len;
     if (buf->size <= buf->used) {
-        while (buf->used >= buf->size) {
+        while (buf->used >= buf->size)
             buf->size <<= 1;
-        }
         buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
     }
     memmove(buf->list+from+repl_len, buf->list+from+len, strlen(buf->list+from+len));
@@ -755,10 +799,12 @@ struct string_list str_tab;
 
 const char *
 strtab(unsigned int ii) {
-    if (ii > 65536) return NULL;
+    if (ii > 65536)
+        return NULL;
     if (ii > str_tab.size) {
         unsigned int old_size = str_tab.size;
-        while (ii >= str_tab.size) str_tab.size <<= 1;
+        while (ii >= str_tab.size)
+            str_tab.size <<= 1;
         str_tab.list = realloc(str_tab.list, str_tab.size*sizeof(str_tab.list[0]));
         memset(str_tab.list+old_size, 0, (str_tab.size-old_size)*sizeof(str_tab.list[0]));
     }
@@ -773,12 +819,17 @@ void
 tools_init(void)
 {
     unsigned int upr, lwr;
-    for (lwr=0; lwr<256; ++lwr) tolower(lwr) = lwr;
-    for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr) tolower(upr) = lwr;
+    for (lwr=0; lwr<256; ++lwr)
+        tolower(lwr) = lwr;
+    for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr)
+        tolower(upr) = lwr;
 #ifdef WITH_PROTOCOL_P10
-    for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr) tolower(upr) = lwr;
-    for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr) tolower(upr) = lwr;
-    for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr) tolower(upr) = lwr;
+    for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr)
+        tolower(upr) = lwr;
+    for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr)
+        tolower(upr) = lwr;
+    for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr)
+        tolower(upr) = lwr;
 #endif
     str_tab.size = 1001;
     str_tab.list = calloc(str_tab.size, sizeof(str_tab.list[0]));
@@ -788,8 +839,7 @@ void
 tools_cleanup(void)
 {
     unsigned int ii;
-    for (ii=0; ii<str_tab.size; ++ii) {
-        if (str_tab.list[ii]) free(str_tab.list[ii]);
-    }
+    for (ii=0; ii<str_tab.size; ++ii)
+        free(str_tab.list[ii]);
     free(str_tab.list);
 }