changed Makefile; build all commands as an own file
[NeonServV5.git] / event_neonserv_notice.c
1
2 struct neonserv_event_notice_cache {
3     struct ClientSocket *client;
4     struct UserNode *user;
5     struct ChanNode *chan;
6     char *message;
7 };
8
9 static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup);
10 static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message);
11
12 static void neonserv_event_channotice(struct UserNode *user, struct ChanNode *chan, char *message) {
13     struct ClientSocket *client = getBotForChannel(chan);
14     if(!client) return; //we can't "see" this event
15     if(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) return; 
16     loadChannelSettings(chan);
17     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
18     if(!(user->flags & USERFLAG_ISAUTHED)) {
19         struct neonserv_event_notice_cache *cache = malloc(sizeof(*cache));
20         if (!cache) {
21             perror("malloc() failed");
22             return;
23         }
24         cache->client = client;
25         cache->user = user;
26         cache->chan = chan;
27         cache->message = strdup(message);
28         get_userauth(user, neonserv_event_notice_nick_lookup, cache);
29     } else
30         neonserv_event_notice_async1(client, user, chan, message);
31 }
32
33 static USERAUTH_CALLBACK(neonserv_event_notice_nick_lookup) {
34     struct neonserv_event_notice_cache *cache = data;
35     if(user) {
36         neonserv_event_notice_async1(cache->client, cache->user, cache->chan, cache->message);
37     }
38     free(cache->message);
39     free(cache);
40 }
41
42 static void neonserv_event_notice_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
43     MYSQL_RES *res;
44     MYSQL_ROW row, defaultrow = NULL, chanuser;
45     int uaccess = 0;
46     printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
47     res = mysql_use();
48     if ((row = mysql_fetch_row(res)) == NULL) return;
49     if(!row[0] || !row[1]) {
50         printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
51         res = mysql_use();
52         defaultrow = mysql_fetch_row(res);
53     }
54     int noticeaccess = atoi((row[0] ? row[0] : defaultrow[0]));
55     if((user->flags & USERFLAG_ISAUTHED)) {
56         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));
57         res = mysql_use();
58         chanuser = mysql_fetch_row(res);
59         if(chanuser)
60             uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
61     }
62     int duration = 0;
63     char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
64     char *banmask = NULL;
65     char *reason = "disallowed channel NOTICE";
66     if(uaccess < noticeaccess) {
67         switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
68             case 2: //TIMEBAN: 3min
69                 duration = 180;
70             case 3: //TIMEBAN: 1h
71                 if(!duration)
72                     duration = 3600;
73                 banmask = generate_banmask(user, banmaskBuf);
74                 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));
75                 int banid = (int) mysql_insert_id(mysql_conn);
76                 char nameBuf[MAXLEN];
77                 char banidBuf[20];
78                 sprintf(nameBuf, "ban_%d", banid);
79                 sprintf(banidBuf, "%d", banid);
80                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
81             case 1: //KICKBAN
82                 if(!banmask)
83                     banmask = generate_banmask(user, banmaskBuf);
84                 putsock(client, "MODE %s +b %s", chan->name, banmask);
85             case 0: //KICK
86                 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
87                 break;
88         }
89     }
90 }
91