/* sexchat
* Copyright (C) 2025 Aleteoryx.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* IRCv3 batch CAP */
#include <string.h>
#include "hexchat.h"
#include "batch.h"
#include "proto-irc.h"
void
batch_new (ircconn *serv, char *reftag, char *type, char **args)
{
struct batch *bat;
if (batch_lookup (serv, reftag))
return;
if (strcmp (type, "soju.im/bouncer-networks") == 0)
{
bat = (struct batch *)g_new0 (struct batch_sojuim_bouncer_networks, 1);
bat->type = BATCH_SOJUIM_BOUNCER_NETWORKS;
} else
{
bat = (struct batch *)g_new0 (struct batch_replay, 1);
bat->type = BATCH_REPLAY;
}
bat->reftag = g_strdup (reftag);
serv->batches = g_slist_prepend (serv->batches, bat);
}
void
batch_finalize (ircconn *serv, char *reftag)
{
struct batch *bat;
bat = batch_lookup (serv, reftag);
if (bat == NULL)
return;
serv->batches = g_slist_remove (serv->batches, bat);
switch (bat->type)
{
case BATCH_SOJUIM_BOUNCER_NETWORKS:
break;
case BATCH_REPLAY:
{
struct batch_replay *bat2 = (struct batch_replay *)bat;
GSList *el;
char *line;
bat2->messages = g_slist_reverse (bat2->messages);
for (el = bat2->messages; el; el = el->next)
{
line = (char *)el->data;
irc_inline (serv, line, strlen (line));
g_free (line);
}
g_slist_free (bat2->messages);
}
}
g_free (bat);
}
gboolean
batch_event (ircconn *serv, char *reftag, char *raw, char **word, char **word_eol)
{
struct batch *bat;
bat = batch_lookup (serv, reftag);
if (bat == NULL)
return FALSE;
switch (bat->type)
{
case BATCH_SOJUIM_BOUNCER_NETWORKS:
break;
case BATCH_REPLAY:
{
struct batch_replay *bat2 = (struct batch_replay *)bat;
bat2->messages = g_slist_prepend (bat2->messages, g_strdup (raw));
}
}
return TRUE;
}
struct batch *
batch_lookup (ircconn *serv, char *reftag)
{
GSList *el;
struct batch *bat;
for (el = serv->batches; el; el = el->next)
{
bat = (struct batch *)el->data;
if (strcmp (bat->reftag, reftag) == 0)
return bat;
}
return NULL;
}