rewrote IRC cache parser to be (hopefully) more stable
[NeonServV5.git] / src / modules / NeonFun.mod / bot_NeonFun.c
index f203f9114eb99e99f4f949a50a967d467a2750af..6fad25665145f94df573241197898b68c2eacd10 100644 (file)
 #include "../../EventLogger.h"
 #include "../../bots.h"
 #include "game_uno.h"
+#include "game_4wins.h"
 
 #define BOTID NEONFUN_BOTID
 #define BOTALIAS "NeonFun"
 
 static void neonfun_bot_ready(struct ClientSocket *client) {
+    if(client->botid != BOTID)
+        return;
     MYSQL_RES *res;
     MYSQL_ROW row;
     
-    printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
+    printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
     res = mysql_use();
     if ((row = mysql_fetch_row(res)) != NULL) {
+        if(row[1] && row[2]) {
+            putsock(client, "OPER %s %s", row[1], row[2]);
+        }
         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
     }
     
@@ -95,6 +101,7 @@ static void start_bots(int type) {
             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
             client->flags |= SOCKET_FLAG_SILENT;
+            client->flags |= SOCKET_FLAG_REQUEST_INVITE | SOCKET_FLAG_REQUEST_OP;
             client->botid = BOTID;
             client->clientid = atoi(row[7]);
             connect_socket(client);
@@ -121,19 +128,47 @@ static void start_bots(int type) {
     bind_unbound_required_functions(BOTID);
 }
 
-static void neonfun_parted(struct ChanUser *chanuser, char *reason) {
+static void neonfun_parted(struct ChanUser *chanuser, int quit, char *reason) {
     uno_event_part(chanuser);
-}
-
-static void neonfun_quitted(struct UserNode *user, char *reason) {
-    uno_event_quit(user);
+    fourwins_event_part(chanuser);
 }
 
 static int neonfun_freechan(struct ChanNode *chan) {
     uno_event_freechan(chan);
+    fourwins_event_freechan(chan);
     return 0;
 }
 
+static void neonfun_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
+       if(client->botid != BOTID)
+               return;
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    printf_mysql_query("SELECT `botid`, `bot_channels`.`id`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `channel_name` = '%s' AND `botclass` = '%d'", escape_string(channel), client->botid);
+    res = mysql_use();
+    if ((row = mysql_fetch_row(res)) == NULL) {
+        reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
+        return;
+    }
+    if(!strcmp(row[2], "1")) {
+        reply(client, user, "MODCMD_CHAN_SUSPENDED");
+        return;
+    }
+    int botid = atoi(row[0]);
+    struct ClientSocket *bot;
+    for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
+        if(bot->clientid == botid)
+            break;
+    }
+    if(bot) {
+        struct ChanNode *chan = getChanByName(channel);
+        if(chan && isUserOnChan(bot->user, chan)) {
+            reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
+        } else
+            putsock(bot, "JOIN %s", channel);
+    }
+}
+
 void init_NeonFun(int type) {
     set_bot_alias(BOTID, BOTALIAS);
     start_bots(type);
@@ -143,9 +178,9 @@ void init_NeonFun(int type) {
     //register events
     bind_bot_ready(neonfun_bot_ready, module_id);
     bind_part(neonfun_parted, module_id);
-    bind_quit(neonfun_quitted, module_id);
     bind_freechan(neonfun_freechan, module_id);
-    
+    bind_invite(neonfun_event_invite, module_id);
+       
     set_trigger_callback(BOTID, module_id, neonfun_trigger_callback);
 }
 
@@ -232,5 +267,42 @@ void uno_reply(struct uno_game *game, struct UserNode *user, const char *text, .
     write_socket(client, sendBuf, pos+1);
 }
 
+void fourwins_reply(struct fourwins_game *game, const char *text, ...) {
+    struct ClientSocket *client = game->textbot;
+    struct fourwins_guest *guest;
+    int guest_count = 0;
+    for(guest = game->guests; guest; guest = guest->next) {
+        guest_count++;
+    }
+    struct ChanUser *chanusers[guest_count + 2];
+    chanusers[0] = game->player[0];
+    chanusers[1] = game->player[1];
+    guest_count = 0;
+    for(guest = game->guests; guest; guest = guest->next) {
+        chanusers[2 + (guest_count++)] = guest->chanuser;
+    }
+    int i;
+    guest_count += 2;
+    for(i = 0; i < guest_count; i++) {
+        const char *reply_format = get_language_string(chanusers[i]->user, text);
+        if(reply_format == NULL)
+            reply_format = text;
+        char formatBuf[MAXLEN];
+        sprintf(formatBuf, "NOTICE %s :[4WINS] %s", chanusers[i]->user->nick, reply_format);
+        va_list arg_list;
+        char sendBuf[MAXLEN];
+        int pos;
+        if (!(client->flags & SOCKET_FLAG_CONNECTED)) return;
+        sendBuf[0] = '\0';
+        va_start(arg_list, text);
+        pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
+        va_end(arg_list);
+        if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
+        sendBuf[pos] = '\n';
+        sendBuf[pos+1] = '\0';
+        write_socket(client, sendBuf, pos+1);
+    }
+}
+
 #undef BOTID
 #undef BOTALIAS