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