added event_ctcp, event_notice and event_quit
authorpk910 <philipp@zoelle1.de>
Sun, 25 Sep 2011 00:18:56 +0000 (02:18 +0200)
committerpk910 <philipp@zoelle1.de>
Wed, 28 Sep 2011 19:29:13 +0000 (21:29 +0200)
bot_NeonServ.c
event_neonserv_ctcp.c [new file with mode: 0644]
event_neonserv_join.c
event_neonserv_notice.c [new file with mode: 0644]
event_neonserv_quit.c [new file with mode: 0644]

index bc7f7c61e7c54460873a3b45591a2e681f7b36e0..8b4ba9c38991217f36e119bb780cb6f65a9b0388 100644 (file)
@@ -317,11 +317,11 @@ INCLUDE ALL CMD's HERE
 //EVENTS
 #include "event_neonserv_join.c"
 #include "event_neonserv_part.c"
-//#include "event_neonserv_quit.c"
+#include "event_neonserv_quit.c"
 //#include "event_neonserv_kick.c"
 //#include "event_neonserv_mode.c"
-//#include "event_neonserv_ctcp.c"
-//#include "event_neonserv_notice.c"
+#include "event_neonserv_ctcp.c"
+#include "event_neonserv_notice.c"
 //#include "event_neonserv_invite.c"
 
 static struct ClientSocket *getBotForChannel(struct ChanNode *chan) {
@@ -517,6 +517,10 @@ void init_NeonServ() {
     bind_bot_ready(neonserv_bot_ready);
     bind_join(neonserv_event_join);
     bind_part(neonserv_event_part);
+    bind_quit(neonserv_event_quit);
+    bind_chanctcp(neonserv_event_chanctcp);
+    bind_privctcp(neonserv_event_privctcp);
+    bind_channotice(neonserv_event_channotice);
     
     set_trigger_callback(BOTID, neonserv_trigger_callback);
     
diff --git a/event_neonserv_ctcp.c b/event_neonserv_ctcp.c
new file mode 100644 (file)
index 0000000..a8df98e
--- /dev/null
@@ -0,0 +1,133 @@
+
+struct neonserv_event_ctcp_cache {
+    struct ClientSocket *client;
+    struct UserNode *user;
+    struct ChanNode *chan;
+    char *command;
+    char *text;
+};
+
+static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup);
+static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text);
+static int neonserv_ctcp(char *buffer, char *command, char *text);
+
+static void neonserv_event_chanctcp(struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
+    struct ClientSocket *client = getBotForChannel(chan);
+    if(!client) return; //we can't "see" this event
+    if(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) return; 
+    loadChannelSettings(chan);
+    if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
+    if(!(user->flags & USERFLAG_ISAUTHED)) {
+        struct neonserv_event_ctcp_cache *cache = malloc(sizeof(*cache));
+        if (!cache) {
+            perror("malloc() failed");
+            return;
+        }
+        cache->client = client;
+        cache->user = user;
+        cache->chan = chan;
+        cache->command = strdup(command);
+        cache->text = (text ? strdup(text) : NULL);
+        get_userauth(user, neonserv_event_ctcp_nick_lookup, cache);
+    } else
+        neonserv_event_ctcp_async1(client, user, chan, command, text);
+}
+
+static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup) {
+    struct neonserv_event_ctcp_cache *cache = data;
+    if(user) {
+        neonserv_event_ctcp_async1(cache->client, cache->user, cache->chan, cache->command, cache->text);
+    }
+    free(cache->command);
+    if(cache->text)
+        free(cache->text);
+    free(cache);
+}
+
+static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
+    MYSQL_RES *res;
+    MYSQL_ROW row, defaultrow = NULL, chanuser;
+    int uaccess = 0;
+    printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) == NULL) return;
+    if(!row[0] || !row[1]) {
+        printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
+        res = mysql_use();
+        defaultrow = mysql_fetch_row(res);
+    }
+    int ctcpaccess = atoi((row[0] ? row[0] : defaultrow[0]));
+    if((user->flags & USERFLAG_ISAUTHED)) {
+        printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' AND `user_user` = '%s'", chan->channel_id, escape_string(user->auth));
+        res = mysql_use();
+        chanuser = mysql_fetch_row(res);
+        if(chanuser)
+            uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
+    }
+    int duration = 0;
+    char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
+    char *banmask = NULL;
+    char *reason = "disallowed channel CTCP";
+    if(uaccess < ctcpaccess) {
+        switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
+            case 2: //TIMEBAN: 3min
+                duration = 180;
+            case 3: //TIMEBAN: 1h
+                if(!duration)
+                    duration = 3600;
+                banmask = generate_banmask(user, banmaskBuf);
+                printf_mysql_query("INSERT INTO `bans` (`ban_channel`, `ban_mask`, `ban_triggered`, `ban_timeout`, `ban_owner`, `ban_reason`) VALUES ('%d', '%s', UNIX_TIMESTAMP(), '%lu', '%d', '%s')", chan->channel_id, escape_string(banmask), (unsigned long) (time(0) + duration), 0, escape_string(reason));
+                int banid = (int) mysql_insert_id(mysql_conn);
+                char nameBuf[MAXLEN];
+                char banidBuf[20];
+                sprintf(nameBuf, "ban_%d", banid);
+                sprintf(banidBuf, "%d", banid);
+                timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
+            case 1: //KICKBAN
+                if(!banmask)
+                    banmask = generate_banmask(user, banmaskBuf);
+                putsock(client, "MODE %s +b %s", chan->name, banmask);
+            case 0: //KICK
+                putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
+                break;
+        }
+    }
+}
+
+static void neonserv_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
+    char ctcpBuf[MAXLEN];
+    if(neonserv_ctcp(ctcpBuf, command, text)) {
+        struct ClientSocket *bot;
+        for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+            if(bot->user == target) break;
+        }
+        if(bot)
+            putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
+    }
+}
+
+static int neonserv_ctcp(char *buffer, char *command, char *text) {
+    if(!stricmp(command, "VERSION")) {
+        sprintf(buffer, "VERSION NeonServ v" NEONSERV_VERSION " by pk910 (%s)", (strcmp(revision, "") ? revision : "-"));
+        return 1;
+    }
+    if(!stricmp(command, "FINGER")) {
+        sprintf(buffer, "FINGER NeonServ v" NEONSERV_VERSION " (%s) build %s lines C code using " COMPILER " (see +netinfo)", (strcmp(revision, "") ? revision : "-"), codelines);
+        return 1;
+    }
+    if(!stricmp(command, "PING")) {
+        sprintf(buffer, "PING %s", (text ? text : "0"));
+        return 1;
+    }
+    if(!stricmp(command, "TIME")) {
+        time_t rawtime;
+        struct tm *timeinfo;
+        char timeBuf[80];
+        time(&rawtime);
+        timeinfo = localtime(&rawtime);
+        strftime(timeBuf, 80, "%c", timeinfo);
+        sprintf(buffer, "TIME %s", timeBuf);
+        return 1;
+    }
+    return 0;
+}
index b2ac486925eb83bce7acbf119951abdf5db86206..a117c18b0c7afa4145ea63a05c42c46a83864d6a 100644 (file)
@@ -28,7 +28,7 @@ static void neonserv_event_join(struct ChanUser *chanuser) {
         res = mysql_use();
         if ((row = mysql_fetch_row(res)) != NULL) {
             putsock(client, "MODE %s +b %s", chanuser->chan->name, ban);
-            putsock(client, "KICK %s %s :(%s) %s", chanuser->chan->name, chanuser->user->nick, row[1], row[0]);
+            putsock(client, "KICK %s %s :(%s) %s", chanuser->chan->name, chanuser->user->nick, (row[1] ? row[1] : client->user->nick), row[0]);
             return;
         }
     }
diff --git a/event_neonserv_notice.c b/event_neonserv_notice.c
new file mode 100644 (file)
index 0000000..a566a4e
--- /dev/null
@@ -0,0 +1,91 @@
+
+struct neonserv_event_notice_cache {
+    struct ClientSocket *client;
+    struct UserNode *user;
+    struct ChanNode *chan;
+    char *message;
+};
+
+static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup);
+static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message);
+
+static void neonserv_event_channotice(struct UserNode *user, struct ChanNode *chan, char *message) {
+    struct ClientSocket *client = getBotForChannel(chan);
+    if(!client) return; //we can't "see" this event
+    if(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) return; 
+    loadChannelSettings(chan);
+    if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
+    if(!(user->flags & USERFLAG_ISAUTHED)) {
+        struct neonserv_event_notice_cache *cache = malloc(sizeof(*cache));
+        if (!cache) {
+            perror("malloc() failed");
+            return;
+        }
+        cache->client = client;
+        cache->user = user;
+        cache->chan = chan;
+        cache->message = strdup(message);
+        get_userauth(user, neonserv_event_notice_nick_lookup, cache);
+    } else
+        neonserv_event_notice_async1(client, user, chan, message);
+}
+
+static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup) {
+    struct neonserv_event_notice_cache *cache = data;
+    if(user) {
+        neonserv_event_notice_async1(cache->client, cache->user, cache->chan, cache->message);
+    }
+    free(cache->message);
+    free(cache);
+}
+
+static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
+    MYSQL_RES *res;
+    MYSQL_ROW row, defaultrow = NULL, chanuser;
+    int uaccess = 0;
+    printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) == NULL) return;
+    if(!row[0] || !row[1]) {
+        printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
+        res = mysql_use();
+        defaultrow = mysql_fetch_row(res);
+    }
+    int noticeaccess = atoi((row[0] ? row[0] : defaultrow[0]));
+    if((user->flags & USERFLAG_ISAUTHED)) {
+        printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` WHERE `chanuser_cid` = '%d' AND `user_user` = '%s'", chan->channel_id, escape_string(user->auth));
+        res = mysql_use();
+        chanuser = mysql_fetch_row(res);
+        if(chanuser)
+            uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
+    }
+    int duration = 0;
+    char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
+    char *banmask = NULL;
+    char *reason = "disallowed channel NOTICE";
+    if(uaccess < noticeaccess) {
+        switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
+            case 2: //TIMEBAN: 3min
+                duration = 180;
+            case 3: //TIMEBAN: 1h
+                if(!duration)
+                    duration = 3600;
+                banmask = generate_banmask(user, banmaskBuf);
+                printf_mysql_query("INSERT INTO `bans` (`ban_channel`, `ban_mask`, `ban_triggered`, `ban_timeout`, `ban_owner`, `ban_reason`) VALUES ('%d', '%s', UNIX_TIMESTAMP(), '%lu', '%d', '%s')", chan->channel_id, escape_string(banmask), (unsigned long) (time(0) + duration), 0, escape_string(reason));
+                int banid = (int) mysql_insert_id(mysql_conn);
+                char nameBuf[MAXLEN];
+                char banidBuf[20];
+                sprintf(nameBuf, "ban_%d", banid);
+                sprintf(banidBuf, "%d", banid);
+                timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
+            case 1: //KICKBAN
+                if(!banmask)
+                    banmask = generate_banmask(user, banmaskBuf);
+                putsock(client, "MODE %s +b %s", chan->name, banmask);
+            case 0: //KICK
+                putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
+                break;
+        }
+    }
+}
+
diff --git a/event_neonserv_quit.c b/event_neonserv_quit.c
new file mode 100644 (file)
index 0000000..30d467f
--- /dev/null
@@ -0,0 +1,14 @@
+
+static void neonserv_event_quit(struct UserNode *user, char *reason) {
+    MYSQL_RES *res;
+    MYSQL_ROW chanuserrow;
+    struct ChanUser *chanuser;
+    if((user->flags & USERFLAG_ISAUTHED)) {
+        for(chanuser = getUserChannels(user, NULL); chanuser; chanuser = getUserChannels(user, chanuser)) {
+            printf_mysql_query("SELECT `chanuser_id` FROM `chanusers` LEFT JOIN `users` ON `chanuser_uid` = `user_id` LEFT JOIN `channels` ON `chanuser_cid` = `channel_id` WHERE `channel_name` = '%s' AND `user_user` = '%s'", escape_string(chanuser->chan->name), escape_string(user->auth));
+            res = mysql_use();
+            if((chanuserrow = mysql_fetch_row(res)) != NULL)
+                printf_mysql_query("UPDATE `chanusers` SET `chanuser_seen` = UNIX_TIMESTAMP() WHERE `chanuser_id` = '%s'", chanuserrow[0]);
+        }
+    }
+}