a8df98e856dc49f8b143eb4979ebe17c964bd52a
[NeonServV5.git] / event_neonserv_ctcp.c
1
2 struct neonserv_event_ctcp_cache {
3     struct ClientSocket *client;
4     struct UserNode *user;
5     struct ChanNode *chan;
6     char *command;
7     char *text;
8 };
9
10 static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup);
11 static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text);
12 static int neonserv_ctcp(char *buffer, char *command, char *text);
13
14 static void neonserv_event_chanctcp(struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
15     struct ClientSocket *client = getBotForChannel(chan);
16     if(!client) return; //we can't "see" this event
17     if(user->flags & (USERFLAG_ISBOT | USERFLAG_ISIRCOP)) return; 
18     loadChannelSettings(chan);
19     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) return;
20     if(!(user->flags & USERFLAG_ISAUTHED)) {
21         struct neonserv_event_ctcp_cache *cache = malloc(sizeof(*cache));
22         if (!cache) {
23             perror("malloc() failed");
24             return;
25         }
26         cache->client = client;
27         cache->user = user;
28         cache->chan = chan;
29         cache->command = strdup(command);
30         cache->text = (text ? strdup(text) : NULL);
31         get_userauth(user, neonserv_event_ctcp_nick_lookup, cache);
32     } else
33         neonserv_event_ctcp_async1(client, user, chan, command, text);
34 }
35
36 static USERAUTH_CALLBACK(neonserv_event_ctcp_nick_lookup) {
37     struct neonserv_event_ctcp_cache *cache = data;
38     if(user) {
39         neonserv_event_ctcp_async1(cache->client, cache->user, cache->chan, cache->command, cache->text);
40     }
41     free(cache->command);
42     if(cache->text)
43         free(cache->text);
44     free(cache);
45 }
46
47 static void neonserv_event_ctcp_async1(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char *text) {
48     MYSQL_RES *res;
49     MYSQL_ROW row, defaultrow = NULL, chanuser;
50     int uaccess = 0;
51     printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
52     res = mysql_use();
53     if ((row = mysql_fetch_row(res)) == NULL) return;
54     if(!row[0] || !row[1]) {
55         printf_mysql_query("SELECT `channel_ctcp`, `channel_ctcpreaction` FROM `channels` WHERE `channel_name` = 'defaults'");
56         res = mysql_use();
57         defaultrow = mysql_fetch_row(res);
58     }
59     int ctcpaccess = atoi((row[0] ? row[0] : defaultrow[0]));
60     if((user->flags & USERFLAG_ISAUTHED)) {
61         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));
62         res = mysql_use();
63         chanuser = mysql_fetch_row(res);
64         if(chanuser)
65             uaccess = ((atoi(chanuser[1]) & DB_CHANUSER_SUSPENDED) ? 0 : atoi(chanuser[0]));
66     }
67     int duration = 0;
68     char banmaskBuf[NICKLEN+USERLEN+HOSTLEN+3];
69     char *banmask = NULL;
70     char *reason = "disallowed channel CTCP";
71     if(uaccess < ctcpaccess) {
72         switch(atoi((row[1] ? row[1] : defaultrow[1]))) {
73             case 2: //TIMEBAN: 3min
74                 duration = 180;
75             case 3: //TIMEBAN: 1h
76                 if(!duration)
77                     duration = 3600;
78                 banmask = generate_banmask(user, banmaskBuf);
79                 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));
80                 int banid = (int) mysql_insert_id(mysql_conn);
81                 char nameBuf[MAXLEN];
82                 char banidBuf[20];
83                 sprintf(nameBuf, "ban_%d", banid);
84                 sprintf(banidBuf, "%d", banid);
85                 timeq_add_name(nameBuf, duration, channel_ban_timeout, strdup(banidBuf));
86             case 1: //KICKBAN
87                 if(!banmask)
88                     banmask = generate_banmask(user, banmaskBuf);
89                 putsock(client, "MODE %s +b %s", chan->name, banmask);
90             case 0: //KICK
91                 putsock(client, "KICK %s %s :%s", chan->name, user->nick, reason);
92                 break;
93         }
94     }
95 }
96
97 static void neonserv_event_privctcp(struct UserNode *user, struct UserNode *target, char *command, char *text) {
98     char ctcpBuf[MAXLEN];
99     if(neonserv_ctcp(ctcpBuf, command, text)) {
100         struct ClientSocket *bot;
101         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
102             if(bot->user == target) break;
103         }
104         if(bot)
105             putsock(bot, "NOTICE %s :\001%s\001", user->nick, ctcpBuf);
106     }
107 }
108
109 static int neonserv_ctcp(char *buffer, char *command, char *text) {
110     if(!stricmp(command, "VERSION")) {
111         sprintf(buffer, "VERSION NeonServ v" NEONSERV_VERSION " by pk910 (%s)", (strcmp(revision, "") ? revision : "-"));
112         return 1;
113     }
114     if(!stricmp(command, "FINGER")) {
115         sprintf(buffer, "FINGER NeonServ v" NEONSERV_VERSION " (%s) build %s lines C code using " COMPILER " (see +netinfo)", (strcmp(revision, "") ? revision : "-"), codelines);
116         return 1;
117     }
118     if(!stricmp(command, "PING")) {
119         sprintf(buffer, "PING %s", (text ? text : "0"));
120         return 1;
121     }
122     if(!stricmp(command, "TIME")) {
123         time_t rawtime;
124         struct tm *timeinfo;
125         char timeBuf[80];
126         time(&rawtime);
127         timeinfo = localtime(&rawtime);
128         strftime(timeBuf, 80, "%c", timeinfo);
129         sprintf(buffer, "TIME %s", timeBuf);
130         return 1;
131     }
132     return 0;
133 }