<commit_before>/*
 * mpd2irc - MPD->IRC gateway
 *
 * Copyright 2008-2011 <NAME>
 * All rights reserved. Released under the 2-clause BSD license.
 */


#include <string.h>

#include <gio/gio.h>
#include <glib.h>

#include "preferences.h"
#include "mpd.h"
#include "config.h"

static void irc_run(const gchar *command);
void irc_say(const gchar *fmt, ...);
static void irc_connected(GSocketClient *client, GAsyncResult *result,
		gpointer user_data);
static gboolean irc_callback(GSocket *socket, GIOCondition condition,
		gpointer user_data);
static void irc_source_attach(void);
static void irc_write(const gchar *fmt, ...);
static void irc_parse(const gchar *buffer);

static GSocketConnection *connection;
static gboolean connected = FALSE;
static GOutputStream *ostream;
static GInputStream *istream;

void irc_connect(void)
{
	GSocketClient *client;

	client = g_socket_client_new();
	g_socket_client_set_tls(client, prefs.irc_use_ssl);
	g_socket_client_set_tls_validation_flags(client, 0);
	g_socket_client_connect_to_host_async(client, prefs.irc_server, 6667,
			NULL, (GAsyncReadyCallback) irc_connected, NULL);
	g_object_unref(client);
}

static void irc_connected(GSocketClient *client, GAsyncResult *result,
		G_GNUC_UNUSED gpointer user_data)
{
	connection = g_socket_client_connect_finish(client, result, NULL);
	ostream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
	istream = g_io_stream_get_input_stream(G_IO_STREAM(connection));

	irc_write("NICK %s", prefs.irc_nick);
	irc_write("USER %s 0 * :%s", prefs.irc_username, prefs.irc_realname);

	irc_source_attach();
}

static void irc_run(const gchar *command)
{
	if (g_ascii_strncasecmp(command, "announce", 8) == 0) {
		if (prefs.announce)
			prefs.announce = FALSE;
		else
			prefs.announce = TRUE;

		irc_say("New song announcement %sabled",
				(prefs.announce ? "en" : "dis"));
	} else if (g_ascii_strncasecmp(command, "next", 4) == 0) {<commit_msg>Check for connection errors in irc_connected()<commit_after>/*
 * mpd2irc - MPD->IRC gateway
 *
 * Copyright 2008-2011 <NAME>
 * All rights reserved. Released under the 2-clause BSD license.
 */


#include <string.h>

#include <gio/gio.h>
#include <glib.h>

#include "preferences.h"
#include "mpd.h"
#include "config.h"

static void irc_run(const gchar *command);
void irc_say(const gchar *fmt, ...);
static void irc_connected(GSocketClient *client, GAsyncResult *result,
		gpointer user_data);
static gboolean irc_callback(GSocket *socket, GIOCondition condition,
		gpointer user_data);
static void irc_source_attach(void);
static void irc_write(const gchar *fmt, ...);
static void irc_parse(const gchar *buffer);

static GSocketConnection *connection;
static gboolean connected = FALSE;
static GOutputStream *ostream;
static GInputStream *istream;

void irc_connect(void)
{
	GSocketClient *client;

	client = g_socket_client_new();
	g_socket_client_set_tls(client, prefs.irc_use_ssl);
	g_socket_client_set_tls_validation_flags(client, 0);
	g_socket_client_connect_to_host_async(client, prefs.irc_server, 6667,
			NULL, (GAsyncReadyCallback) irc_connected, NULL);
	g_object_unref(client);
}

static void irc_connected(GSocketClient *client, GAsyncResult *result,
		G_GNUC_UNUSED gpointer user_data)
{
	GError *error = NULL;

	connection = g_socket_client_connect_finish(client, result, &error);
	if (!connection) {
		g_debug("Failed to connect to IRC: %s", error->message);
		return;
		/* TODO schedule reconnect */
	}

	ostream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
	istream = g_io_stream_get_input_stream(G_IO_STREAM(connection));

	irc_write("NICK %s", prefs.irc_nick);
	irc_write("USER %s 0 * :%s", prefs.irc_username, prefs.irc_realname);

	irc_source_attach();
}

static void irc_run(const gchar *command)
{
	if (g_ascii_strncasecmp(command, "announce", 8) == 0) {
		if (prefs.announce)
			prefs.announce = FALSE;
		else
			prefs.announce = TRUE;

		irc_say("New song announcement %sabled",
				(prefs.announce ? "en" : "dis"));
	} else if (g_ascii_strncasecmp(command, "next", 4) == 0) {
{'fraction_non_alphanumeric': 0.11913000494315373, 'fraction_numerical': 0.009886307464162136, 'mean_word_length': 6.013864818024263, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '279873', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1452, 'n_words': 329}