added .gitignore
[NeonServV5.git] / bot_NeonServ.c
1
2 #include "bot_NeonServ.h"
3 #include "modcmd.h"
4 #include "IRCEvents.h"
5 #include "UserNode.h"
6 #include "ChanNode.h"
7 #include "ChanUser.h"
8 #include "ClientSocket.h"
9 #include "mysqlConn.h"
10
11 #define BOTID 1
12 #define CLASSNAME "NeonServ"
13
14 static CMD_BIND(neonserv_cmd_users) {
15     struct ChanUser *chanuser;
16     putsock(client, "PRIVMSG %s :[BOT JOIN] Users on this Channel:", chan->name);
17     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
18         putsock(client, "PRIVMSG %s :  %s!%s@%s [%s]  rights: %d", chan->name, chanuser->user->nick, chanuser->user->ident, chanuser->user->host, ((chanuser->user->flags & USERFLAG_ISAUTHED) ? chanuser->user->auth : "*"), chanuser->flags);
19     }
20 }
21
22 static CMD_BIND(neonserv_cmd_modes) {
23     char modeBuf[MAXLEN];
24     getModeString(chan, modeBuf);
25     putsock(client, "PRIVMSG %s :Modes: %s", chan->name, modeBuf);
26 }
27
28 static void neonserv_bot_ready(struct ClientSocket *client) {
29     MYSQL_RES *res;
30     MYSQL_ROW row;
31     
32     check_mysql();
33     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d'", client->clientid);
34     res = mysql_use();
35     
36     while ((row = mysql_fetch_row(res)) != NULL) {
37         putsock(client, "JOIN %s %s", row[0], row[1]);
38     }
39 }
40
41 static void neonserv_trigger_callback(struct ChanNode *chan, char *trigger) {
42     strcpy(trigger, "!");
43 }
44
45 static void start_bots() {
46     struct UserNode *user;
47     struct ClientSocket *client;
48     MYSQL_RES *res;
49     MYSQL_ROW row;
50     
51     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `whoisbot`, `id` FROM `bots` WHERE `botclass` = '%s' AND `active` = '1'", escape_string(CLASSNAME));
52     res = mysql_use();
53     
54     while ((row = mysql_fetch_row(res)) != NULL) {
55         
56         user = addUser(row[0]);
57         strcpy(user->ident, row[1]);
58         strcpy(user->realname, row[2]);
59         user->flags |= USERFLAG_ISBOT;
60         client = create_socket(row[3], *row[4], row[5], user);
61         client->flags |= (*row[6] == 1 ? SOCKET_FLAG_PREFERRED : 0);
62         client->botid = BOTID;
63         client->clientid = *row[7];
64         connect_socket(client);
65         
66     }
67 }
68
69 void init_NeonServ() {
70     check_mysql();
71     
72     start_bots();
73     bind_bot_ready(neonserv_bot_ready);
74     set_trigger_callback(BOTID, neonserv_trigger_callback);
75     
76     register_command(BOTID, "users", neonserv_cmd_users);
77     register_command(BOTID, "modes", neonserv_cmd_modes);
78     
79     bind_cmd_to_command(BOTID, "users", "users");
80     bind_cmd_to_command(BOTID, "modes", "modes");
81 }
82
83 void free_NeonServ() {
84     
85 }
86
87 #undef BOTID
88 #undef CLASSNAME