1 /* event_neonserv_topic.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
18 struct neonserv_event_topic_cache {
19 struct ClientSocket *client;
20 struct UserNode *user;
21 struct ChanNode *chan;
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);
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));
37 perror("malloc() failed");
40 cache->client = client;
43 cache->new_topic = strdup(new_topic);
44 get_userauth(user, neonserv_event_topic_nick_lookup, cache);
46 neonserv_event_topic_async1(client, user, chan, new_topic);
49 static USERAUTH_CALLBACK(neonserv_event_topic_nick_lookup) {
50 struct neonserv_event_topic_cache *cache = data;
52 neonserv_event_topic_async1(cache->client, cache->user, cache->chan, cache->new_topic);
54 free(cache->new_topic);
58 static void neonserv_event_topic_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, const char *new_topic) {
60 MYSQL_ROW row, defaultrow = NULL, chanuserrow;
62 printf_mysql_query("SELECT `channel_changetopic`, `channel_topicsnarf` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
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'");
68 defaultrow = mysql_fetch_row(res);
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));
75 chanuserrow = mysql_fetch_row(res);
77 uaccess = ((atoi(chanuserrow[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuserrow[0]));
79 if(uaccess < changetopic) {
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;
88 chanuser->chageEvents++;
89 if(chanuser->chageEvents >= BOTWAR_DETECTION_EVENTS || chanuser->chageEvents < 0) {
91 chanuser->changeTime = time(0);
92 if(chanuser->chageEvents > 0) {
93 char *alertchan = get_string_field("General.alertchan");
94 putsock(client, "NOTICE @%s :%s %s", chan->name, get_language_string(user, "NS_BOTWAR_DETECTED"), (alertchan ? get_language_string(user, "NS_BOTWAR_REPORTED") : ""));
96 struct ChanNode *alertchan_chan = getChanByName(alertchan);
97 struct ClientSocket *alertclient;
98 if(alertchan_chan && (alertclient = getBotForChannel(chan)) != NULL) {
100 putsock(alertclient, "PRIVMSG %s :%s", alertchan_chan->name, build_language_string(NULL, msgBuf, "NS_BOTWAR_ALERT", chan->name, user->nick));
104 chanuser->chageEvents = -2;
108 reply(textclient, user, "NS_TOPIC_ACCESS", chan->name);
109 putsock(client, "TOPIC %s :%s", chan->name, chan->topic);
110 } else if(uaccess >= topicsnarf) {
111 printf_mysql_query("UPDATE `channels` SET `channel_defaulttopic` = '%s' WHERE `channel_id` = '%d'", escape_string(new_topic), chan->channel_id);