<commit_before>/**
 * WarfaceBot, a blind XMPP client for Warface (FPS)
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <wb_tools.h>
#include <wb_stream.h>
#include <wb_session.h>
#include <wb_xmpp.h>
#include <wb_xmpp_wf.h>
#include <wb_game.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void xmpp_iq_join_channel_cb(const char *msg)
{
    /* Answer
      <iq from='masterserver@warface/pve_12' to='xxxxxx@warface/GameClient' type='result'>
       <query xmlns='urn:cryonline:k01'>
        <data query_name='join_channel' compressedData='...' originalSize='13480'/>
       </query>
      </iq>
     */

    char *data = wf_get_query_content(msg);

    if (xmpp_is_error(msg))
    {
        fprintf(stderr, "Failed to join channel\n");
        return;
    }

    if (data != NULL)
    {
        char *exp = get_info(data, "experience='", "'", "EXPERIENCE");

        if (exp != NULL)
            session.experience = strtol(exp, NULL, 10);

        char *m = data;

        while ((m = strstr(m, "<notif")))
        {
            char *notif = get_info(m, "<notif", "</notif>", NULL);

            xmpp_iq_confirm_notification(notif);
            free(notif);
            ++m;
        }

        free(exp);
        free(data);
    }

    /* Inform to k01 our status */
    xmpp_iq_player_status(STATUS_ONLINE | STATUS_LOBBY);
}

void xmpp_iq_join_channel(const char *channel)
{
    int is_switch = session.channel != NULL;
    t_uid id;

    idh_generate_unique_id(&id);
    idh_register(&id, xmpp_iq_join_channel_cb, 0);

    if (channel != NULL)
    {
        free(session.channel);
        session.channel = strdup(channel);
    }

    /* Join CryOnline channel */
    send_stream_format(session.wfs,<commit_msg>Fix switch/join_channel factorizing<commit_after>/**
 * WarfaceBot, a blind XMPP client for Warface (FPS)
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <wb_tools.h>
#include <wb_stream.h>
#include <wb_session.h>
#include <wb_xmpp.h>
#include <wb_xmpp_wf.h>
#include <wb_game.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void xmpp_iq_join_channel_cb(const char *msg)
{
    /* Answer
      <iq from='masterserver@warface/pve_12' to='xxxxxx@warface/GameClient' type='result'>
       <query xmlns='urn:cryonline:k01'>
        <data query_name='join_channel' compressedData='...' originalSize='13480'/>
       </query>
      </iq>
     */

    char *data = wf_get_query_content(msg);

    if (xmpp_is_error(msg))
    {
        fprintf(stderr, "Failed to join channel\n");
        return;
    }

    if (data != NULL)
    {
        char *exp = get_info(data, "experience='", "'", "EXPERIENCE");

        if (exp != NULL)
            session.experience = strtol(exp, NULL, 10);

        char *m = data;

        while ((m = strstr(m, "<notif")))
        {
            char *notif = get_info(m, "<notif", "</notif>", NULL);

            xmpp_iq_confirm_notification(notif);
            free(notif);
            ++m;
        }

        free(exp);
        free(data);
    }

    /* Inform to k01 our status */
    xmpp_iq_player_status(STATUS_ONLINE | STATUS_LOBBY);
}

void xmpp_iq_join_channel(const char *channel)
{
    int is_switch = session.status >= STATUS_LOBBY;
    t_uid id;

    idh_generate_unique_id(&id);
    idh_register(&id, xmpp_iq_join_channel_cb, 0);

    if (channel != NULL)
    {
        free(session.channel);
        session.channel = strdup(channel);
    }

    /* Join CryOnline channel */
    send_stream_format(session.wfs,
{'fraction_non_alphanumeric': 0.12274143302180686, 'fraction_numerical': 0.007892004153686396, 'mean_word_length': 2.8838709677419354, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1199011', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1556, 'n_words': 449}