rearranged NeonServ code to be modular
[NeonServV5.git] / src / modules / NeonServ.mod / event_neonserv_topic.c
diff --git a/src/modules/NeonServ.mod/event_neonserv_topic.c b/src/modules/NeonServ.mod/event_neonserv_topic.c
new file mode 100644 (file)
index 0000000..577e0a3
--- /dev/null
@@ -0,0 +1,114 @@
+/* event_neonserv_topic.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program 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 3 of the License, or
+ * (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
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>. 
+ */
+
+struct neonserv_event_topic_cache {
+    struct ClientSocket *client;
+    struct UserNode *user;
+    struct ChanNode *chan;
+    char *new_topic;
+};
+
+static USERAUTH_CALLBACK(neonserv_event_topic_nick_lookup);
+static void neonserv_event_topic_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, const char *new_topic);
+
+static void neonserv_event_topic(struct UserNode *user, struct ChanNode *chan, const char *new_topic) {
+    struct ClientSocket *client = getBotForChannel(chan);
+    if(!client) return; //we can't "see" this event
+    if(isNetworkService(user)) return; 
+    loadChannelSettings(chan);
+    if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
+    if(!(user->flags & USERFLAG_ISAUTHED)) {
+        struct neonserv_event_topic_cache *cache = malloc(sizeof(*cache));
+        if (!cache) {
+            perror("malloc() failed");
+            return;
+        }
+        cache->client = client;
+        cache->user = user;
+        cache->chan = chan;
+        cache->new_topic = strdup(new_topic);
+        get_userauth(user, neonserv_event_topic_nick_lookup, cache);
+    } else
+        neonserv_event_topic_async1(client, user, chan, new_topic);
+}
+
+static USERAUTH_CALLBACK(neonserv_event_topic_nick_lookup) {
+    struct neonserv_event_topic_cache *cache = data;
+    if(user) {
+        neonserv_event_topic_async1(cache->client, cache->user, cache->chan, cache->new_topic);
+    }
+    free(cache->new_topic);
+    free(cache);
+}
+
+static void neonserv_event_topic_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, const char *new_topic) {
+    MYSQL_RES *res;
+    MYSQL_ROW row, defaultrow = NULL, chanuserrow;
+    int uaccess = 0;
+    printf_mysql_query("SELECT `channel_changetopic`, `channel_topicsnarf` 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_changetopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_name` = 'defaults'");
+        res = mysql_use();
+        defaultrow = mysql_fetch_row(res);
+    }
+    int changetopic = atoi((row[0] ? row[0] : defaultrow[0]));
+    int topicsnarf = atoi((row[1] ? row[1] : defaultrow[1]));
+    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();
+        chanuserrow = mysql_fetch_row(res);
+        if(chanuserrow)
+            uaccess = ((atoi(chanuserrow[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuserrow[0]));
+    }
+    if(uaccess < changetopic) {
+        //undo topic change
+        struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
+        struct ChanUser *chanuser = getChanUser(user, chan);
+        if(!chanuser) return; //flying super cow?
+        if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
+            chanuser->changeTime = time(0);
+            chanuser->chageEvents = 1;
+        } else {
+            chanuser->chageEvents++;
+            if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
+                //BOTWAR!
+                chanuser->changeTime = time(0);
+                if(chanuser->chageEvents > 0) {
+                    char *alertchan = get_string_field("General.alertchan");
+                    putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (alertchan ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
+                    if(alertchan) {
+                        struct ChanNode *alertchan_chan = getChanByName(alertchan);
+                        struct ClientSocket *alertclient;
+                        if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
+                            char msgBuf[MAXLEN];
+                            putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
+                        }
+                    }
+                }
+                chanuser->chageEvents = -2;
+                return;
+            }
+        }
+        reply(textclient, user, "NS_TOPIC_ACCESS", chan->name);
+        putsock(client, "TOPIC %s :%s", chan->name, chan->topic);
+    } else if(uaccess >= topicsnarf) {
+        printf_mysql_query("UPDATE `channels` SET `channel_defaulttopic` = '%s' WHERE `channel_id` = '%d'", escape_string(new_topic), chan->channel_id);
+    }
+}
+