added cmd_ban and all functions depending on
[NeonServV5.git] / tools.c
diff --git a/tools.c b/tools.c
index 274187229bc3584fde6e50e925841a475907ebd0..93fbd323e2c42d56e6c4455711e6628a6034f326 100644 (file)
--- a/tools.c
+++ b/tools.c
@@ -370,6 +370,89 @@ void freeModeBuffer(struct ModeBuffer *modeBuf) {
     free(modeBuf);
 }
 
+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++;
+    return !*text;
+}
+
+char* generate_banmask(struct UserNode *user, char *buffer) {
+    char *userhost = user->host;
+    
+    if(isFakeHost(user->host)) {
+        sprintf(buffer, "*!*@%s", userhost);
+        return buffer;
+    }
+    
+    //check if the hostname has more than 4 connections (trusted host)
+    if(countUsersWithHost(userhost) > 4) {
+        sprintf(buffer, "*!%s@%s", user->ident, userhost);
+        return buffer;
+    } else {
+        sprintf(buffer, "*!*@%s", userhost);
+        return 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 == '*' && !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;
+    }
+    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);
 }