c3846ed56b7491ce270fcfe6af9e04dcc69c1706
[NeonServV5.git] / src / event_neonserv_notice.c
1 /* event_neonserv_notice.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_notice_cache {
19     struct ClientSocket *client;
20     struct UserNode *user;
21     struct ChanNode *chan;
22     char *message;
23 };
24
25 static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup);
26 static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message);
27
28 static void neonserv_event_channotice(struct UserNode *user, struct ChanNode *chan, char *message) {
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_notice_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->message = strdup(message);
44         get_userauth(user, neonserv_event_notice_nick_lookup, cache);
45     } else
46         neonserv_event_notice_async1(client, user, chan, message);
47 }
48
49 static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup) {
50     struct neonserv_event_notice_cache *cache = data;
51     if(user) {
52         neonserv_event_notice_async1(cache->client, cache->user, cache->chan, cache->message);
53     }
54     free(cache->message);
55     free(cache);
56 }
57
58 static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
59     MYSQL_RES *res;
60     MYSQL_ROW row, defaultrow = NULL, chanuser;
61     int uaccess = 0;
62     printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` 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_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
67         res = mysql_use();
68         defaultrow = mysql_fetch_row(res);
69     }
70     int noticeaccess = atoi((row[0] ? row[0] : defaultrow[0]));
71     if((user->flags & USERFLAG_ISAUTHED)) {
72         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));
73         res = mysql_use();
74         chanuser = mysql_fetch_row(res);
75         if(chanuser)
76             uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
77     }
78     int duration = 0;
79     char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
80     char *banmask = NULL;
81     char *reason = "disallowed channel NOTICE";
82     if(uaccess < noticeaccess) {
83         switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
84             case 2: //TIMEBAN: 3min
85                 duration = 180;
86             case 3: //TIMEBAN: 1h
87                 if(!duration)
88                     duration = 3600;
89                 banmask = generate_banmask(user, banmaskBuf);
90                 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));
91                 int banid = (int) mysql_insert_id(mysql_conn);
92                 char nameBuf[MAXLEN];
93                 char banidBuf[20];
94                 sprintf(nameBuf, "ban_%d", banid);
95                 sprintf(banidBuf, "%d", banid);
96                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
97             case 1: //KICKBAN
98                 if(!banmask)
99                     banmask = generate_banmask(user, banmaskBuf);
100                 putsock(client, "MODE %s +b %s", chan->name, banmask);
101             case 0: //KICK
102                 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
103                 break;
104         }
105     }
106 }
107