bf2ae92c6392712140a3927e5e514cd765263ec3
[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 "modcmd.h"
20 #include "IRCParser.h"
21 #include "IRCEvents.h"
22 #include "UserNode.h"
23 #include "ChanNode.h"
24 #include "ChanUser.h"
25 #include "ModeNode.h"
26 #include "BanNode.h"
27 #include "ClientSocket.h"
28 #include "mysqlConn.h"
29 #include "lang.h"
30 #include "HandleInfoHandler.h"
31 #include "WHOHandler.h"
32 #include "DBHelper.h"
33 #include "tools.h"
34 #include "timeq.h"
35 #include "version.h"
36 #include "EventLogger.h"
37 #include "bots.h"
38 #include "cmd_neonserv.h"
39
40 #define BOTID 2
41
42 static const struct default_language_entry msgtab[] = {
43     {NULL, NULL}
44 };
45
46 static int loadNeonSpamSettings(struct ChanNode *chan);
47 static void createSpamNode(struct ChanUser *chanuser);
48
49 //EVENTS
50 //#include "event_neonspam_join.c"
51 #include "event_neonspam_chanmsg.c"
52
53 static void neonspam_bot_ready(struct ClientSocket *client) {
54     MYSQL_RES *res;
55     MYSQL_ROW row;
56     
57     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
58     res = mysql_use();
59     if ((row = mysql_fetch_row(res)) != NULL) {
60         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
61     }
62     
63     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);
64     res = mysql_use();
65     
66     while ((row = mysql_fetch_row(res)) != NULL) {
67         putsock(client, "JOIN %s %s", row[0], row[1]);
68     }
69 }
70
71 static void neonspam_trigger_callback(struct ChanNode *chan, char *trigger) {
72     MYSQL_RES *res;
73     MYSQL_ROW row;
74     loadChannelSettings(chan);
75     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);
76     res = mysql_use();
77     row = mysql_fetch_row(res);
78     strcpy(trigger, (strlen(row[0]) ? row[0] : "~"));
79 }
80
81 static void start_bots() {
82     struct UserNode *user;
83     struct ClientSocket *client;
84     MYSQL_RES *res, *res2;
85     MYSQL_ROW row;
86     
87     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
88     res = mysql_use();
89     
90     while ((row = mysql_fetch_row(res)) != NULL) {
91         
92         user = addUser(row[0]);
93         strcpy(user->ident, row[1]);
94         strcpy(user->realname, row[2]);
95         user->flags |= USERFLAG_ISBOT;
96         client = create_socket(row[3], atoi(row[4]), row[5], user);
97         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
98         client->botid = BOTID;
99         client->clientid = atoi(row[7]);
100         connect_socket(client);
101         printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access` FROM `bot_binds` WHERE `botclass` = '%d'", client->botid);
102         res2 = mysql_use();
103         while ((row = mysql_fetch_row(res2)) != NULL) {
104             if(bind_cmd_to_command(BOTID, row[0], row[1])) {
105                 if(row[2] && strcmp(row[2], "")) {
106                     bind_set_parameters(BOTID, row[0], row[2]);
107                 }
108                 if(row[3]) {
109                     bind_set_global_access(BOTID, row[0], atoi(row[3]));
110                 }
111                 if(row[4]) {
112                     bind_set_channel_access(BOTID, row[0], row[4]);
113                 }
114             }
115         }
116     }
117 }
118
119 static int loadNeonSpamSettings(struct ChanNode *chan) {
120     struct NeonSpamSettings *settings = malloc(sizeof(*settings));
121     if(!settings) {
122         perror("malloc() failed");
123         return 0;
124     }
125     settings->flags = SPAMSETTINGS_SCANVOICE | SPAMSETTINGS_FLOODSCAN | SPAMSETTINGS_SPAMSCAN;
126     settings->spam_amount = 3;
127     settings->flood_amount = 4;
128     settings->flood_time = 5;
129     chan->spam_settings = settings;
130     return 1;
131 }
132
133 static void createSpamNode(struct ChanUser *chanuser) {
134     struct NeonSpamNode *spamnode = malloc(sizeof(*spamnode));
135     if(!spamnode) {
136         perror("malloc() failed");
137         return;
138     }
139     spamnode->lastmsg = 0;
140     spamnode->spamcount = 0;
141     spamnode->floodpenalty = 0;
142     spamnode->last_penalty_update = time(0);
143     chanuser->spamnode = spamnode;
144 }
145
146 void init_NeonSpam() {
147     
148     #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,PRIVS,FLAGS) register_command(BOTID, NAME, FUNCTION, PARAMCOUNT, PRIVS, 0, FLAGS)
149     //               NAME              FUNCTION        PARAMS     PRIVS                FLAGS
150     USER_COMMAND("netinfo",      neonserv_cmd_netinfo,   0, NULL,                   0);
151     USER_COMMAND("version",      neonserv_cmd_version,   0, NULL,                   0);
152     #undef USER_COMMAND
153     
154     #define OPER_COMMAND(NAME,FUNCTION,PARAMCOUNT,GACCESS,FLAGS) register_command(BOTID, NAME, FUNCTION, PARAMCOUNT, NULL, GACCESS, FLAGS)
155     //            NAME            FUNCTION              PARAMS  ACCS  FLAGS
156     OPER_COMMAND("register",     neonserv_cmd_register,  1,     200,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
157     OPER_COMMAND("unregister",   neonserv_cmd_unregister,1,     200,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_CHAN_PARAM | CMDFLAG_OPLOG);
158     OPER_COMMAND("bind",         neonserv_cmd_bind,      2,     900,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
159     OPER_COMMAND("unbind",       neonserv_cmd_unbind,    1,     900,  CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_OPLOG);
160     #undef OPER_COMMAND
161     
162     start_bots();
163     
164     //register events
165     bind_bot_ready(neonspam_bot_ready);
166     //bind_join(neonspam_event_join);
167     bind_chanmsg(neonspam_event_chanmsg);
168     bind_privctcp(general_event_privctcp);
169     
170     set_trigger_callback(BOTID, neonspam_trigger_callback);
171     
172     register_default_language_table(msgtab);
173 }
174
175 void loop_NeonSpam() {
176     
177 }
178
179 void free_NeonSpam() {
180     
181 }
182
183 #undef BOTID