made cmd_register more multibot compatible
[NeonServV5.git] / src / bot_NeonSpam.c
1 /* bot_NeonSpam.c - NeonServ v5.1
2  * Copyright (C) 2011  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_NeonSpam.h"
19 #include "cmd_neonserv.h"
20
21 #define BOTID 2
22
23 static const struct default_language_entry msgtab[] = {
24     {NULL, NULL}
25 };
26
27 //EVENTS
28 //#include "event_neonspam_join.c"
29
30 static void neonspam_bot_ready(struct ClientSocket *client) {
31     MYSQL_RES *res;
32     MYSQL_ROW row;
33     
34     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
35     res = mysql_use();
36     if ((row = mysql_fetch_row(res)) != NULL) {
37         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
38     }
39     
40     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);
41     res = mysql_use();
42     
43     while ((row = mysql_fetch_row(res)) != NULL) {
44         putsock(client, "JOIN %s %s", row[0], row[1]);
45     }
46 }
47
48 static void neonspam_trigger_callback(struct ChanNode *chan, char *trigger) {
49     MYSQL_RES *res;
50     MYSQL_ROW row;
51     loadChannelSettings(chan);
52     printf_mysql_query("SELECT `trigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
53     res = mysql_use();
54     row = mysql_fetch_row(res);
55     strcpy(trigger, (strlen(row[0]) ? row[0] : "~"));
56 }
57
58 static void start_bots() {
59     struct UserNode *user;
60     struct ClientSocket *client;
61     MYSQL_RES *res, *res2;
62     MYSQL_ROW row;
63     
64     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
65     res = mysql_use();
66     
67     while ((row = mysql_fetch_row(res)) != NULL) {
68         
69         user = addUser(row[0]);
70         strcpy(user->ident, row[1]);
71         strcpy(user->realname, row[2]);
72         user->flags |= USERFLAG_ISBOT;
73         client = create_socket(row[3], atoi(row[4]), row[5], user);
74         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
75         client->botid = BOTID;
76         client->clientid = atoi(row[7]);
77         connect_socket(client);
78         printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", client->botid);
79         res2 = mysql_use();
80         while ((row = mysql_fetch_row(res2)) != NULL) {
81             if(bind_cmd_to_command(BOTID, row[0], row[1])) {
82                 if(row[2] && strcmp(row[2], "")) {
83                     bind_set_parameters(BOTID, row[0], row[2]);
84                 }
85                 if(row[3]) {
86                     bind_set_global_access(BOTID, row[0], atoi(row[3]));
87                 }
88                 if(row[4]) {
89                     bind_set_channel_access(BOTID, row[0], row[4]);
90                 }
91             }
92         }
93     }
94     
95 }
96
97 void init_NeonSpam() {
98     
99     #define OPER_COMMAND(NAME,FUNCTION,PARAMCOUNT,GACCESS,FLAGS) register_command(BOTID, NAME, FUNCTION, PARAMCOUNT, NULL, GACCESS, FLAGS)
100     //            NAME            FUNCTION              PARAMS  ACCS  FLAGS
101     OPER_COMMAND("register",     neonserv_cmd_register,  1,     200,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
102     OPER_COMMAND("unregister",   neonserv_cmd_unregister,1,     200,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
103     OPER_COMMAND("bind",         neonserv_cmd_bind,      2,     900,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
104     OPER_COMMAND("unbind",       neonserv_cmd_unbind,    1,     900,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
105     #undef OPER_COMMAND
106     
107     start_bots();
108     
109     //register events
110     bind_bot_ready(neonspam_bot_ready);
111     //bind_join(neonspam_event_join);
112     
113     set_trigger_callback(BOTID, neonspam_trigger_callback);
114     
115     register_default_language_table(msgtab);
116 }
117
118 void loop_NeonSpam() {
119     
120 }
121
122 void free_NeonSpam() {
123     
124 }
125
126 #undef BOTID