*** VERSION 5.4.0 ***
[NeonServV5.git] / src / modules / DummyServ.mod / bot_DummyServ.c
1 /* bot_DummyServ.c - NeonServ v5.4
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
19 #include "bot_DummyServ.h"
20 #include "../../modcmd.h"
21 #include "../../IRCParser.h"
22 #include "../../IRCEvents.h"
23 #include "../../UserNode.h"
24 #include "../../ChanNode.h"
25 #include "../../ChanUser.h"
26 #include "../../ModeNode.h"
27 #include "../../BanNode.h"
28 #include "../../ClientSocket.h"
29 #include "../../mysqlConn.h"
30 #include "../../lang.h"
31 #include "../../HandleInfoHandler.h"
32 #include "../../WHOHandler.h"
33 #include "../../DBHelper.h"
34 #include "../../tools.h"
35 #include "../../timeq.h"
36 #include "../../version.h"
37 #include "../../EventLogger.h"
38 #include "../../bots.h"
39
40 #define BOTID 3
41 #define BOTALIAS "DummyServ"
42
43 static void dummyserv_bot_ready(struct ClientSocket *client) {
44     MYSQL_RES *res;
45     MYSQL_ROW row;
46     
47     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
48     res = mysql_use();
49     if ((row = mysql_fetch_row(res)) != NULL) {
50         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
51     }
52     
53     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);
54     res = mysql_use();
55     
56     while ((row = mysql_fetch_row(res)) != NULL) {
57         putsock(client, "JOIN %s %s", row[0], row[1]);
58     }
59 }
60
61 static void dummyserv_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
62     //this bot doesn't have a trigger
63     strcpy(trigger, "");
64 }
65
66 static void start_bots(int type) {
67     struct ClientSocket *client;
68     MYSQL_RES *res, *res2;
69     MYSQL_ROW row;
70     
71     if(type == MODSTATE_STARTSTOP) {
72         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
73         res = mysql_use();
74         
75         while ((row = mysql_fetch_row(res)) != NULL) {
76             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
77             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
78             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
79             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
80             client->flags |= SOCKET_FLAG_SILENT;
81             client->botid = BOTID;
82             client->clientid = atoi(row[7]);
83             connect_socket(client);
84         }
85     }
86     
87     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
88     res2 = mysql_use();
89     while ((row = mysql_fetch_row(res2)) != NULL) {
90         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
91             if(row[2] && strcmp(row[2], "")) {
92                 bind_set_parameters(BOTID, row[0], row[2]);
93             }
94             if(row[3]) {
95                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
96             }
97             if(row[4]) {
98                 bind_set_channel_access(BOTID, row[0], row[4]);
99             }
100             if(strcmp(row[5], "0"))
101                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
102         }
103     }
104     bind_unbound_required_functions(BOTID);
105 }
106
107 void init_DummyServ(int type) {
108     set_bot_alias(BOTID, BOTALIAS);
109     start_bots(type);
110     
111     if(type == MODSTATE_REBIND) return;
112     
113     //register events
114     bind_bot_ready(dummyserv_bot_ready, module_id);
115     
116     set_trigger_callback(BOTID, module_id, dummyserv_trigger_callback);
117 }
118
119 void loop_DummyServ() {
120     
121 }
122
123 void free_DummyServ(int type) {
124     unbind_allcmd(BOTID);
125     if(type == MODSTATE_STARTSTOP) {
126         //disconnect all our bots
127         struct ClientSocket *client;
128         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
129             if(client->botid == BOTID) {
130                 unbind_botwise_allcmd(0, client->clientid);
131                 close_socket(client);
132                 break;
133             }
134         }
135     }
136 }
137
138 #undef BOTID
139 #undef BOTALIAS