modified code to use IOHandler functions instead of own ones
[NeonServV5.git] / src / modules / DummyServ.mod / bot_DummyServ.c
1 /* bot_DummyServ.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_DummyServ.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 DUMMYSERV_BOTID
42 #define BOTALIAS "DummyServ"
43
44 static void dummyserv_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 dummyserv_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` 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 |= SOCKET_FLAG_SILENT;
87             client->botid = BOTID;
88             client->clientid = atoi(row[7]);
89             connect_socket(client);
90         }
91     }
92     
93     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
94     res2 = mysql_use();
95     while ((row = mysql_fetch_row(res2)) != NULL) {
96         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
97             if(row[2] && strcmp(row[2], "")) {
98                 bind_set_parameters(BOTID, row[0], row[2]);
99             }
100             if(row[3]) {
101                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
102             }
103             if(row[4]) {
104                 bind_set_channel_access(BOTID, row[0], row[4]);
105             }
106             if(strcmp(row[5], "0"))
107                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
108         }
109     }
110     bind_unbound_required_functions(BOTID);
111 }
112
113 static void dummyserv_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
114         if(client->botid != BOTID)
115                 return;
116     MYSQL_RES *res;
117     MYSQL_ROW row;
118     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);
119     res = mysql_use();
120     if ((row = mysql_fetch_row(res)) == NULL) {
121         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
122         return;
123     }
124     if(!strcmp(row[2], "1")) {
125         return;
126     }
127     int botid = atoi(row[0]);
128     struct ClientSocket *bot;
129     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
130         if(bot->clientid == botid)
131             break;
132     }
133     if(bot) {
134         struct ChanNode *chan = getChanByName(channel);
135         if(!(chan && isUserOnChan(bot->user, chan)))
136             putsock(bot, "JOIN %s", channel);
137     }
138 }
139
140 void init_DummyServ(int type) {
141     set_bot_alias(BOTID, BOTALIAS);
142     start_bots(type);
143     
144     if(type == MODSTATE_REBIND) return;
145     
146     //register events
147     bind_bot_ready(dummyserv_bot_ready, module_id);
148         bind_invite(dummyserv_event_invite, module_id);
149     
150     set_trigger_callback(BOTID, module_id, dummyserv_trigger_callback);
151 }
152
153 void free_DummyServ(int type) {
154     unbind_allcmd(BOTID);
155     if(type == MODSTATE_STARTSTOP) {
156         //disconnect all our bots
157         struct ClientSocket *client;
158         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
159             if(client->botid == BOTID) {
160                 unbind_botwise_allcmd(0, client->clientid);
161                 close_socket(client);
162                 break;
163             }
164         }
165     }
166 }
167
168 #undef BOTID
169 #undef BOTALIAS