completed cmd_adduser and added CMDFLAG_REGISTERED_CHAN flag
[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     
25     {NULL, NULL}
26 };
27
28 /*
29 INCLUDE ALL CMD's HERE
30 */
31 #include "cmd_neonserv_users.c"
32 #include "cmd_neonserv_modes.c"
33 #include "cmd_neonserv_adduser.c"
34
35 static void neonserv_bot_ready(struct ClientSocket *client) {
36     MYSQL_RES *res;
37     MYSQL_ROW row;
38     
39     check_mysql();
40     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
41     res = mysql_use();
42     if ((row = mysql_fetch_row(res)) != NULL) {
43         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
44     }
45     
46     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d'", client->clientid);
47     res = mysql_use();
48     
49     while ((row = mysql_fetch_row(res)) != NULL) {
50         putsock(client, "JOIN %s %s", row[0], row[1]);
51     }
52 }
53
54 static void neonserv_trigger_callback(struct ChanNode *chan, char *trigger) {
55     strcpy(trigger, "!");
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`, `whoisbot`, `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` FROM `bot_binds` WHERE `botid` = '%d'", client->clientid);
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(atoi(row[3]) > 0) {
86                     bind_set_gaccess(BOTID, row[0], atoi(row[3]));
87                 }
88             }
89         }
90     }
91 }
92
93 void init_NeonServ() {
94     check_mysql();
95     
96     register_command(BOTID, "users", neonserv_cmd_users, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REQUIRE_AUTH);
97     register_command(BOTID, "modes", neonserv_cmd_modes, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
98     
99     register_command(BOTID, "adduser", neonserv_cmd_adduser, 2, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
100     
101     start_bots();
102     bind_bot_ready(neonserv_bot_ready);
103     set_trigger_callback(BOTID, neonserv_trigger_callback);
104     
105     register_default_language_table(msgtab);
106 }
107
108 void free_NeonServ() {
109     
110 }
111
112 #undef BOTID