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