Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / bot_NeonHelp.c
1 /* bot_NeonHelp.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
18 #include "bot_NeonHelp.h"
19 #include "modcmd.h"
20 #include "cmd_neonhelp.h"
21 #include "mysqlConn.h"
22 #include "ClientSocket.h"
23 #include "UserNode.h"
24 #include "ChanNode.h"
25 #include "ChanUser.h"
26 #include "IRCEvents.h"
27 #include "IRCParser.h"
28 #include "bots.h"
29 #include "DBHelper.h"
30
31 #define BOTID 4
32 #define BOTALIAS "NeonHelp"
33
34 static void neonhelp_bot_ready(struct ClientSocket *client) {
35     MYSQL_RES *res;
36     MYSQL_ROW row;
37     
38     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
39     res = mysql_use();
40     if ((row = mysql_fetch_row(res)) != NULL) {
41         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
42     }
43     
44     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);
45     res = mysql_use();
46     
47     while ((row = mysql_fetch_row(res)) != NULL) {
48         putsock(client, "JOIN %s %s", row[0], row[1]);
49     }
50 }
51
52 static void neonhelp_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
53     MYSQL_RES *res;
54     MYSQL_ROW row;
55     loadChannelSettings(chan);
56     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
57         strcpy(trigger, "!");
58         return;
59     }
60     printf_mysql_query("SELECT `trigger`, `defaulttrigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
61     res = mysql_use();
62     row = mysql_fetch_row(res);
63     if(row[0] && *row[0])
64         strcpy(trigger, row[0]);
65     else
66         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "!"));
67 }
68
69 static void start_bots() {
70     struct ClientSocket *client;
71     MYSQL_RES *res, *res2;
72     MYSQL_ROW row;
73     
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->botid = BOTID;
83         client->clientid = atoi(row[7]);
84         connect_socket(client);
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_NeonHelp() {
108     
109     set_bot_alias(BOTID, BOTALIAS);
110     start_bots();
111     
112     //register events
113     bind_bot_ready(neonhelp_bot_ready);
114     
115     set_trigger_callback(BOTID, neonhelp_trigger_callback);
116 }
117
118 void loop_NeonHelp() {
119     
120 }
121
122 void free_NeonHelp() {
123     
124 }
125
126 #undef BOTID
127 #undef BOTALIAS