added NeonKick bot (just kicking users from channels they have no access in - e.g...
[NeonServV5.git] / src / modules / NeonKick.mod / bot_NeonKick.c
1 /* bot_NeonKick.c - NeonServ v5.5
2  * Copyright (C) 2011-2012  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 #include "../module.h"
18 #include "../botid.h"
19
20 #include "bot_NeonKick.h"
21 #include "../../modcmd.h"
22 #include "../../IRCParser.h"
23 #include "../../IRCEvents.h"
24 #include "../../UserNode.h"
25 #include "../../ChanNode.h"
26 #include "../../ChanUser.h"
27 #include "../../ModeNode.h"
28 #include "../../BanNode.h"
29 #include "../../ClientSocket.h"
30 #include "../../mysqlConn.h"
31 #include "../../lang.h"
32 #include "../../HandleInfoHandler.h"
33 #include "../../WHOHandler.h"
34 #include "../../DBHelper.h"
35 #include "../../tools.h"
36 #include "../../timeq.h"
37 #include "../../version.h"
38 #include "../../EventLogger.h"
39 #include "../../bots.h"
40
41 #define BOTID NEONKICK_BOTID
42 #define BOTALIAS "NeonKick"
43
44 static void neonkick_bot_ready(struct ClientSocket *client) {
45     if(client->botid != BOTID)
46         return;
47     MYSQL_RES *res;
48     MYSQL_ROW row;
49     
50     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
51     res = mysql_use();
52     if ((row = mysql_fetch_row(res)) != NULL) {
53         if(row[1] && row[2]) {
54             putsock(client, "OPER %s %s", row[1], row[2]);
55         }
56         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
57     }
58     
59     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d' AND `suspended` = '0'", client->clientid);
60     res = mysql_use();
61     
62     while ((row = mysql_fetch_row(res)) != NULL) {
63         putsock(client, "JOIN %s %s", row[0], row[1]);
64     }
65 }
66
67 static void neonkick_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
68     //this bot doesn't have a trigger
69     strcpy(trigger, "");
70 }
71
72 static void start_bots(int type) {
73     struct ClientSocket *client;
74     MYSQL_RES *res, *res2;
75     MYSQL_ROW row;
76     
77     if(type == MODSTATE_STARTSTOP) {
78         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind`, `secret` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
79         res = mysql_use();
80         
81         while ((row = mysql_fetch_row(res)) != NULL) {
82             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
83             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
84             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
85             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
86             client->flags |= (strcmp(row[11], "0") ? SOCKET_FLAG_SECRET_BOT : 0);
87             client->flags |= SOCKET_FLAG_SILENT;
88             client->botid = BOTID;
89             client->clientid = atoi(row[7]);
90             connect_socket(client);
91         }
92     }
93     
94     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
95     res2 = mysql_use();
96     while ((row = mysql_fetch_row(res2)) != NULL) {
97         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
98             if(row[2] && strcmp(row[2], "")) {
99                 bind_set_parameters(BOTID, row[0], row[2]);
100             }
101             if(row[3]) {
102                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
103             }
104             if(row[4]) {
105                 bind_set_channel_access(BOTID, row[0], row[4]);
106             }
107             if(strcmp(row[5], "0"))
108                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
109         }
110     }
111     bind_unbound_required_functions(BOTID);
112 }
113
114 static void neonkick_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
115         if(client->botid != BOTID)
116                 return;
117     MYSQL_RES *res;
118     MYSQL_ROW row;
119     printf_mysql_query("SELECT `botid`, `bot_channels`.`id`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `channel_name` = '%s' AND `botclass` = '%d'", escape_string(channel), client->botid);
120     res = mysql_use();
121     if ((row = mysql_fetch_row(res)) == NULL) {
122         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
123         return;
124     }
125     if(!strcmp(row[2], "1")) {
126         return;
127     }
128     int botid = atoi(row[0]);
129     struct ClientSocket *bot;
130     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
131         if(bot->clientid == botid)
132             break;
133     }
134     if(bot) {
135         struct ChanNode *chan = getChanByName(channel);
136         if(!(chan && isUserOnChan(bot->user, chan)))
137             putsock(bot, "JOIN %s", channel);
138     }
139 }
140
141 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
142     char *uname = "";
143     int cid = 0;
144     MYSQL_RES *res;
145     MYSQL_ROW row;
146     if(user) {
147         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
148     }
149     if(chan) {
150         loadChannelSettings(chan);
151         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
152             cid = chan->channel_id;
153     }
154     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
155     res = mysql_use();
156     if ((row = mysql_fetch_row(res)) != NULL) {
157         return row[0];
158     } else
159         return NULL;
160 }
161
162 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
163     char *uname = "";
164     int cid = 0;
165     MYSQL_RES *res;
166     MYSQL_ROW row;
167     if(user) {
168         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
169     }
170     if(chan) {
171         loadChannelSettings(chan);
172         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
173             cid = chan->channel_id;
174     }
175     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
176     res = mysql_use();
177     if ((row = mysql_fetch_row(res)) != NULL) {
178         if(strcmp(row[1], value))
179             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
180     } else
181         printf_mysql_query("INSERT INTO `fundata` (`user`, `cid`, `name`, `value`) VALUES ('%s', '%d', '%s', '%s')", escape_string(uname), cid, escape_string(setting), escape_string(value));
182 }
183
184 #include "event_neonkick_join.c"
185
186 void init_NeonKick(int type) {
187     set_bot_alias(BOTID, BOTALIAS);
188     start_bots(type);
189     
190     if(type == MODSTATE_REBIND) return;
191     
192     //register events
193     bind_bot_ready(neonkick_bot_ready, module_id);
194         bind_invite(neonkick_event_invite, module_id);
195     bind_join(neonkick_event_join, module_id);
196     
197     set_trigger_callback(BOTID, module_id, neonkick_trigger_callback);
198 }
199
200 void free_NeonKick(int type) {
201     unbind_allcmd(BOTID);
202     if(type == MODSTATE_STARTSTOP) {
203         //disconnect all our bots
204         struct ClientSocket *client;
205         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
206             if(client->botid == BOTID) {
207                 unbind_botwise_allcmd(0, client->clientid);
208                 close_socket(client);
209                 break;
210             }
211         }
212     }
213 }
214
215 #undef BOTID
216 #undef BOTALIAS