06f610cc1fcec3abfbc11dd776baa0c19e8c1e5c
[NeonServV5.git] / src / event_neonserv_topic.c
1 /* event_neonserv_topic.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 struct neonserv_event_topic_cache {
19     struct ClientSocket *client;
20     struct UserNode *user;
21     struct ChanNode *chan;
22     char *new_topic;
23 };
24
25 static USERAUTH_CALLBACK(neonserv_event_topic_nick_lookup);
26 static void neonserv_event_topic_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, const char *new_topic);
27
28 static void neonserv_event_topic(struct UserNode *user, struct ChanNode *chan, const char *new_topic) {
29     struct ClientSocket *client = getBotForChannel(chan);
30     if(!client) return; //we can't "see" this event
31     if(isNetworkService(user)) return; 
32     loadChannelSettings(chan);
33     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
34     if(!(user->flags & USERFLAG_ISAUTHED)) {
35         struct neonserv_event_topic_cache *cache = malloc(sizeof(*cache));
36         if (!cache) {
37             perror("malloc() failed");
38             return;
39         }
40         cache->client = client;
41         cache->user = user;
42         cache->chan = chan;
43         cache->new_topic = strdup(new_topic);
44         get_userauth(user, neonserv_event_topic_nick_lookup, cache);
45     } else
46         neonserv_event_topic_async1(client, user, chan, new_topic);
47 }
48
49 static USERAUTH_CALLBACK(neonserv_event_topic_nick_lookup) {
50     struct neonserv_event_topic_cache *cache = data;
51     if(user) {
52         neonserv_event_topic_async1(cache->client, cache->user, cache->chan, cache->new_topic);
53     }
54     free(cache->new_topic);
55     free(cache);
56 }
57
58 static void neonserv_event_topic_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, const char *new_topic) {
59     MYSQL_RES *res;
60     MYSQL_ROW row, defaultrow = NULL, chanuserrow;
61     int uaccess = 0;
62     printf_mysql_query("SELECT `channel_changetopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
63     res = mysql_use();
64     if ((row = mysql_fetch_row(res)) == NULL) return;
65     if(!row[0] || !row[1]) {
66         printf_mysql_query("SELECT `channel_changetopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_name` = 'defaults'");
67         res = mysql_use();
68         defaultrow = mysql_fetch_row(res);
69     }
70     int changetopic = atoi((row[0] ? row[0] : defaultrow[0]));
71     int topicsnarf = atoi((row[1] ? row[1] : defaultrow[1]));
72     if((user->flags & USERFLAG_ISAUTHED)) {
73         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));
74         res = mysql_use();
75         chanuserrow = mysql_fetch_row(res);
76         if(chanuserrow)
77             uaccess = ((atoi(chanuserrow[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuserrow[0]));
78     }
79     if(uaccess < changetopic) {
80         //undo topic change
81         struct ClientSocket *textclient = ((client->flags & SOCKET_FLAG_PREFERRED) ? client : get_prefered_bot(client->botid));
82         struct ChanUser *chanuser = getChanUser(user, chan);
83         if(!chanuser) return; //flying super cow?
84         if(time(0) - chanuser->changeTime > BOTWAR_DETECTION_TIME) {
85             chanuser->changeTime = time(0);
86             chanuser->chageEvents = 1;
87         } else {
88             chanuser->chageEvents++;
89             if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
90                 //BOTWAR!
91                 chanuser->changeTime = time(0);
92                 if(chanuser->chageEvents > 0) {
93                     putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (BOTWAR_ALERT_CHAN ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
94                     if(BOTWAR_ALERT_CHAN) {
95                         struct ChanNode *alertchan = getChanByName(BOTWAR_ALERT_CHAN);
96                         struct ClientSocket *alertclient;
97                         if(alertchan && (alertclient = getBotForChannel(chan)) != NULL) {
98                             char msgBuf[MAXLEN];
99                             putsock(alertclient, "PRIVMSG %s :%s", alertchan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
100                         }
101                     }
102                 }
103                 chanuser->chageEvents = -2;
104                 return;
105             }
106         }
107         reply(textclient, user, "NS_TOPIC_ACCESS", chan->name);
108         putsock(client, "TOPIC %s :%s", chan->name, chan->topic);
109     } else if(uaccess >= topicsnarf) {
110         printf_mysql_query("UPDATE `channels` SET `channel_defaulttopic` = '%s' WHERE `channel_id` = '%d'", escape_string(new_topic), chan->channel_id);
111     }
112 }
113