changed Makefile; build all commands as an own file
[NeonServV5.git] / tools.c
diff --git a/tools.c b/tools.c
index 938bf790247cc9ffa215d5dba234c2d0d524d083..097684855d8230a66cb21819ceb9240dce68a5cb 100644 (file)
--- a/tools.c
+++ b/tools.c
@@ -5,8 +5,8 @@
 #include "ClientSocket.h"
 
 static const struct default_language_entry msgtab[] = {
-    {"TIME_MASK_2_ITEMS", "%s and %s"},
-    {"TIME_MASK_3_ITEMS", "%s, %s and %s"},
+    {"TIME_MASK_2_ITEMS", "%s and %s"}, /* {ARGS: "2 days", "1 hour"} */
+    {"TIME_MASK_3_ITEMS", "%s, %s and %s"}, /* {ARGS: "2 days", "1 hour", "20 minutes"} */
     {"TIME_YEAR", "year"},
     {"TIME_YEARS", "years"},
     {"TIME_MONTH", "month"},
@@ -117,14 +117,44 @@ int table_add(struct Table *table, char **entry) {
     int col;
     if(table->entrys == table->length) return 0;
     for(col = 0; col < table->width; col++) {
-        table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) ? entry[col] : strdup(entry[col]));
-        if(strlen(entry[col]) > table->maxwidth[col])
+        table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col]));
+        if(table->contents[table->entrys][col])
+            table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
+        if(entry[col] && strlen(entry[col]) > table->maxwidth[col])
             table->maxwidth[col] = strlen(entry[col]);
     }
     table->entrys++;
     return 1;
 }
 
+int table_change(struct Table *table, int row, char **entry) {
+    int col;
+    if(row >= table->length) return 0;
+    for(col = 0; col < table->width; col++) {
+        if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER))
+            free(table->contents[row][col]);
+        table->contents[row][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col]));
+        if(table->contents[row][col])
+            table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
+        if(entry[col] && strlen(entry[col]) > table->maxwidth[col])
+            table->maxwidth[col] = strlen(entry[col]);
+    }
+    return 1;
+}
+
+int table_change_field(struct Table *table, int row, int col, char *entry) {
+    if(row >= table->length) return 0;
+    if(col >= table->width) return 0;
+    if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER))
+        free(table->contents[row][col]);
+    table->contents[row][col] = (((table->flags & TABLE_FLAG_USE_POINTER) || !entry) ? entry : strdup(entry));
+    if(table->contents[row][col])
+        table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
+    if(entry && strlen(entry) > table->maxwidth[col])
+        table->maxwidth[col] = strlen(entry);
+    return 1;
+}
+
 int table_set_bold(struct Table *table, int collum, int bold) {
     if(bold)
         table->col_flags[collum] |= TABLE_FLAG_COL_BOLD;
@@ -146,6 +176,7 @@ char **table_end(struct Table *table) {
         table->table_lines[row] = malloc(tablewidth * sizeof(*table->table_lines[row]));
         pos = 0;
         for(col = 0; col < table->width; col++) {
+            if(!(table->col_flags[col] & TABLE_FLAG_COL_CONTENTS)) continue;
             if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
                 table->table_lines[row][pos++] = '\002';
             for(i = 0; i < strlen(table->contents[row][col]); i++) {
@@ -171,7 +202,8 @@ void table_free(struct Table *table) {
     for(row = 0; row < table->length; row++) {
         if(!(table->flags & TABLE_FLAG_USE_POINTER) && table->entrys > row) {
             for(col = 0; col < table->width; col++) {
-                free(table->contents[row][col]);
+                if(table->contents[row][col])
+                    free(table->contents[row][col]);
             }
         }
         free(table->contents[row]);
@@ -227,7 +259,7 @@ char* timeToStr(struct UserNode *user, int seconds, int items, char *buf) {
             }
             buf[p++] = ' ';
         }
-        buf[p-1] = '\0';
+        buf[(p ? p-1 : 0)] = '\0';
     }
     return buf;
 }
@@ -247,12 +279,17 @@ int strToTime(struct UserNode *user, char *str) {
     int unit_multiplikator;
     while(*str) {
         p = str;
+        while(*p && !isdigit(*p)) //skip leading chars
+            p++;
+        str = p;
         while(*p && isdigit(*p)) //get the value
             p++;
         tmpchar = *p;
         *p = '\0';
         cvalue = isdigit(*str) ? atoi(str) : 0;
         *p = tmpchar;
+        while(*p == ' ') //skip spaces
+            p++;
         str = p;
         while(*p && !isdigit(*p)) //get the unit
             p++;
@@ -386,16 +423,8 @@ int is_ircmask(const char *text) {
 
 char* generate_banmask(struct UserNode *user, char *buffer) {
     char *userhost = user->host;
-    char *p1, *p2 = userhost;
     
-    //find the last dot to identify if the hostmask is a hidden host
-    while((p1 = strstr(userhost, "."))) {
-        p2 = p1;
-    }
-    //TLD database: http://www.iana.org/domains/root/db/
-    //the longest TLD i found was 6 chars long (ignoring the stange exotic ones :D)
-    //but we even ignore '.museum' and '.travel' so we can say that the TLD of our mask needs to be less than 4 chars to be a real domain
-    if(strlen(p2+1) > 4) {
+    if(isFakeHost(user->host)) {
         sprintf(buffer, "*!*@%s", userhost);
         return buffer;
     }
@@ -410,6 +439,60 @@ char* generate_banmask(struct UserNode *user, char *buffer) {
     }
 }
 
+char* make_banmask(char *input, char* buffer) {
+    char *nick = NULL, *ident = NULL, *host = NULL;
+    char tmp[HOSTLEN];
+    char *p;
+    if((p = strstr(input, "!"))) {
+        nick = input;
+        *p = '\0';
+        ident = p+1;
+        if((p = strstr(ident, "@"))) {
+            *p = '\0';
+            host = p+1;
+        }
+    } else if((p = strstr(input, "@"))) {
+        ident = input;
+        *p = '\0';
+        host = p+1;
+    } else if((p = strstr(input, "."))) {
+        host = input;
+    } else if(*input == '*' && input[1] != '\0' && !strstr(input+1, "*")) {
+        //AUTH MASK
+        p = getAuthFakehost(input+1);
+        if(p)
+            host = p;
+        else {
+            sprintf(tmp, "%s.*", input+1);
+            host = tmp;
+        }
+    } else {
+        struct UserNode *user = searchUserByNick(input);
+        if(user)
+            return generate_banmask(user, buffer);
+        else
+            nick = input;
+    }
+    if(nick && *nick == '\0') nick = NULL;
+    if(ident && *ident == '\0') ident = NULL;
+    if(host && *host == '\0') host = NULL;
+    sprintf(buffer, "%s!%s@%s", (nick ? nick : "*"), (ident ? ident : "*"), (host ? host : "*"));
+    return buffer;
+}
+
+int isFakeHost(char *host) {
+    char *p1, *p2 = host;
+    
+    //find the last dot to identify if the hostmask is a fake host
+    while((p1 = strstr(p2, "."))) {
+        p2 = p1 + 1;
+    }
+    //TLD database: http://www.iana.org/domains/root/db/
+    //the longest TLD i found was 6 chars long (ignoring the stange exotic ones :D)
+    //but we even ignore '.museum' and '.travel' so we can say that the TLD of our mask needs to be less than 4 chars to be a real domain
+    return (strlen(p2+1) > 4);
+}
+
 void init_tools() {
     register_default_language_table(msgtab);
 }