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