added cmd_clvl and moved all the database request functions to DBHelper.c
[NeonServV5.git] / bot_NeonServ.c
1
2 #include "bot_NeonServ.h"
3 #include "modcmd.h"
4 #include "IRCEvents.h"
5 #include "IRCParser.h"
6 #include "UserNode.h"
7 #include "ChanNode.h"
8 #include "ChanUser.h"
9 #include "ClientSocket.h"
10 #include "mysqlConn.h"
11 #include "lang.h"
12 #include "HandleInfoHandler.h"
13 #include "WHOHandler.h"
14
15 #define BOTID 1
16
17 static const struct default_language_entry msgtab[] = {
18     {"NS_USER_UNKNOWN", "User with nick \002%s\002 does not exist."},
19     {"NS_AUTH_UNKNOWN", "Account \002%s\002 has not been registered."},
20     {"NS_USER_NEED_AUTH", "%s must first authenticate with \002AuthServ\002."},
21     {"NS_INVALID_ACCESS", "\002%d\002 is an invalid access level."},
22     {"NS_ADDUSER_ALREADY_ADDED", "%s is already on the \002%s\002 user list (with access %d)."},
23     {"NS_ADDUSER_DONE", "Added %s to the %s user list with access %d."},
24     {"NS_NOT_ON_USERLIST", "%s lacks access to \002%s\002."},
25     {"NS_DELUSER_DONE", "Deleted %s (with access %d) from the %s user list."},
26     {"NS_ACCESS_OUTRANKED", "You cannot give users access greater than or equal to your own."},
27     {"NS_USER_OUTRANKED", "\002%s\002 outranks you (command has no effect)."},
28     {"NS_ACCESS_DENIED", "Access denied."},
29     {"NS_CLVL_DONE", "%s now has access \002%d\002 in %s."},
30     {NULL, NULL}
31 };
32
33 /*
34 INCLUDE ALL CMD's HERE
35 */
36 #include "cmd_neonserv_users.c"
37 #include "cmd_neonserv_modes.c"
38 #include "cmd_neonserv_adduser.c"
39 #include "cmd_neonserv_deluser.c"
40
41 static void neonserv_bot_ready(struct ClientSocket *client) {
42     MYSQL_RES *res;
43     MYSQL_ROW row;
44     
45     check_mysql();
46     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
47     res = mysql_use();
48     if ((row = mysql_fetch_row(res)) != NULL) {
49         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
50     }
51     
52     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d'", client->clientid);
53     res = mysql_use();
54     
55     while ((row = mysql_fetch_row(res)) != NULL) {
56         putsock(client, "JOIN %s %s", row[0], row[1]);
57     }
58 }
59
60 static void neonserv_trigger_callback(struct ChanNode *chan, char *trigger) {
61     strcpy(trigger, "!");
62 }
63
64 static void start_bots() {
65     struct UserNode *user;
66     struct ClientSocket *client;
67     MYSQL_RES *res, *res2;
68     MYSQL_ROW row;
69     
70     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `whoisbot`, `id` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
71     res = mysql_use();
72     
73     while ((row = mysql_fetch_row(res)) != NULL) {
74         
75         user = addUser(row[0]);
76         strcpy(user->ident, row[1]);
77         strcpy(user->realname, row[2]);
78         user->flags |= USERFLAG_ISBOT;
79         client = create_socket(row[3], atoi(row[4]), row[5], user);
80         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
81         client->botid = BOTID;
82         client->clientid = atoi(row[7]);
83         connect_socket(client);
84         printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access` FROM `bot_binds` WHERE `botid` = '%d'", client->clientid);
85         res2 = mysql_use();
86         while ((row = mysql_fetch_row(res2)) != NULL) {
87             if(bind_cmd_to_command(BOTID, row[0], row[1])) {
88                 if(row[2] && strcmp(row[2], "")) {
89                     bind_set_parameters(BOTID, row[0], row[2]);
90                 }
91                 if(atoi(row[3]) > 0) {
92                     bind_set_gaccess(BOTID, row[0], atoi(row[3]));
93                 }
94             }
95         }
96     }
97 }
98
99 void init_NeonServ() {
100     check_mysql();
101     
102     register_command(BOTID, "users", neonserv_cmd_users, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REQUIRE_AUTH);
103     register_command(BOTID, "modes", neonserv_cmd_modes, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
104     
105     register_command(BOTID, "adduser", neonserv_cmd_adduser, 2, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
106     register_command(BOTID, "deluser", neonserv_cmd_deluser, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
107     
108     start_bots();
109     bind_bot_ready(neonserv_bot_ready);
110     set_trigger_callback(BOTID, neonserv_trigger_callback);
111     
112     register_default_language_table(msgtab);
113 }
114
115 void free_NeonServ() {
116     
117 }
118
119 #undef BOTID