1 /* event_neonserv_notice.c - NeonServ v5.0
2 * Copyright (C) 2011 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_notice_cache {
19 struct ClientSocket *client;
20 struct UserNode *user;
21 struct ChanNode *chan;
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);
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(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) 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));
37 perror("malloc() failed");
40 cache->client = client;
43 cache->message = strdup(message);
44 get_userauth(user, neonserv_event_notice_nick_lookup, cache);
46 neonserv_event_notice_async1(client, user, chan, message);
49 static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup) {
50 struct neonserv_event_notice_cache *cache = data;
52 neonserv_event_notice_async1(cache->client, cache->user, cache->chan, cache->message);
58 static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
60 MYSQL_ROW row, defaultrow = NULL, chanuser;
62 printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` 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_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
68 defaultrow = mysql_fetch_row(res);
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));
74 chanuser = mysql_fetch_row(res);
76 uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
79 char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
81 char *reason = "disallowed channel NOTICE";
82 if(uaccess < noticeaccess) {
83 switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
84 case 2: //TIMEBAN: 3min
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);
94 sprintf(nameBuf, "ban_%d", banid);
95 sprintf(banidBuf, "%d", banid);
96 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
99 banmask = generate_banmask(user, banmaskBuf);
100 putsock(client, "MODE %s +b %s", chan->name, banmask);
102 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);