1 /* bot_NeonKick.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 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/>.
17 #include "../module.h"
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"
41 #define BOTID NEONKICK_BOTID
42 #define BOTALIAS "NeonKick"
44 static void neonkick_bot_ready(struct ClientSocket *client) {
45 if(client->botid != BOTID)
50 printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
52 if ((row = mysql_fetch_row(res)) != NULL) {
53 if(row[1] && row[2]) {
54 putsock(client, "OPER %s %s", row[1], row[2]);
56 putsock(client, "MODE %s +%s", client->user->nick, row[0]);
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);
62 while ((row = mysql_fetch_row(res)) != NULL) {
63 putsock(client, "JOIN %s %s", row[0], row[1]);
67 static void neonkick_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
68 //this bot doesn't have a trigger
72 static void start_bots(int type) {
73 struct ClientSocket *client;
74 MYSQL_RES *res, *res2;
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);
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);
94 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
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]);
102 bind_set_global_access(BOTID, row[0], atoi(row[3]));
105 bind_set_channel_access(BOTID, row[0], row[4]);
107 if(strcmp(row[5], "0"))
108 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
111 bind_unbound_required_functions(BOTID);
114 static void neonkick_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
115 if(client->botid != BOTID)
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);
121 if ((row = mysql_fetch_row(res)) == NULL) {
122 reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
125 if(!strcmp(row[2], "1")) {
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)
135 struct ChanNode *chan = getChanByName(channel);
136 if(!(chan && isUserOnChan(bot->user, chan)))
137 putsock(bot, "JOIN %s", channel);
141 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
147 uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
150 loadChannelSettings(chan);
151 if(chan->flags & CHANFLAG_CHAN_REGISTERED)
152 cid = chan->channel_id;
154 printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
156 if ((row = mysql_fetch_row(res)) != NULL) {
162 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
168 uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
171 loadChannelSettings(chan);
172 if(chan->flags & CHANFLAG_CHAN_REGISTERED)
173 cid = chan->channel_id;
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));
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]);
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));
184 #include "event_neonkick_join.c"
186 void init_NeonKick(int type) {
187 set_bot_alias(BOTID, BOTALIAS);
190 if(type == MODSTATE_REBIND) return;
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);
197 set_trigger_callback(BOTID, module_id, neonkick_trigger_callback);
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);