+struct ModeBuffer* initModeBuffer(struct ClientSocket *client, struct ChanNode *chan) {
+ struct ModeBuffer *modeBuf = malloc(sizeof(*modeBuf));
+ if(!modeBuf) {
+ perror("malloc() failed");
+ return NULL;
+ }
+ modeBuf->client = client;
+ modeBuf->chan = chan;
+ modeBuf->addCount = 0;
+ modeBuf->delCount = 0;
+ return modeBuf;
+}
+
+void modeBufferSet(struct ModeBuffer *modeBuf, int add, char mode, char *param) {
+ if(add) {
+ modeBuf->addModes[modeBuf->addCount] = mode;
+ modeBuf->addModesParams[modeBuf->addCount] = (param ? strdup(param) : NULL);
+ modeBuf->addCount++;
+ modeBuf->addModes[modeBuf->addCount] = '\0';
+ } else {
+ modeBuf->delModes[modeBuf->delCount] = mode;
+ modeBuf->delModesParams[modeBuf->delCount] = (param ? strdup(param) : NULL);
+ modeBuf->delCount++;
+ modeBuf->delModes[modeBuf->delCount] = '\0';
+ }
+ if(modeBuf->addCount + modeBuf->delCount == MAXMODES)
+ flushModeBuffer(modeBuf);
+}
+
+void flushModeBuffer(struct ModeBuffer *modeBuf) {
+ char modeStr[MAXMODES+3];
+ int modePos = 0;
+ char paramStr[MAXLEN];
+ int paramPos = 0;
+ int i;
+ if(modeBuf->addCount) {
+ modeStr[modePos++] = '+';
+ for(i = 0; i < modeBuf->addCount; i++) {
+ modeStr[modePos++] = modeBuf->addModes[i];
+ if(modeBuf->addModesParams[i]) {
+ paramPos += sprintf(paramStr + paramPos, " %s", modeBuf->addModesParams[i]);
+ }
+ }
+ modeBuf->addCount = 0;
+ }
+ if(modeBuf->delCount) {
+ modeStr[modePos++] = '-';
+ for(i = 0; i < modeBuf->delCount; i++) {
+ modeStr[modePos++] = modeBuf->delModes[i];
+ if(modeBuf->delModesParams[i]) {
+ paramPos += sprintf(paramStr + paramPos, " %s", modeBuf->delModesParams[i]);
+ }
+ }
+ modeBuf->delCount = 0;
+ }
+ modeStr[modePos++] = '\0';
+ putsock(modeBuf->client, "MODE %s %s%s", modeBuf->chan->name, modeStr, paramStr);
+}
+
+void freeModeBuffer(struct ModeBuffer *modeBuf) {
+ if(modeBuf->addCount + modeBuf->delCount)
+ flushModeBuffer(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 == '*' && 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);
+}