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