/*
 * Ibis - IRCv3 Library
 * Copyright (C) 2022-2024 Ibis Developers
 *
 * Ibis is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library 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 library 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 library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "ibissource.h"

#include "ibisstring.h"

/******************************************************************************
 * Public API
 *****************************************************************************/
void
ibis_source_parse(const char *source, char **nick, char **user, char **host) {
	GRegex *regex = NULL;
	GMatchInfo *info = NULL;
	gboolean matched = FALSE;

	g_return_if_fail(!ibis_str_is_empty(source));
	g_return_if_fail(nick != NULL || user != NULL || host != NULL);

	/* If we find any \r \n or spaces in our source, it's invalid so we just
	 * bail immediately.
	 */
	matched = g_regex_match_simple("[\r\n ]", source, G_REGEX_DEFAULT,
	                               G_REGEX_MATCH_DEFAULT);
	if(matched) {
		return;
	}

	regex = g_regex_new("^(?P<nick>[^ \r\n!]+)(?:!(?P<user>[^@]+)(?:@(?P<host>[^!@]+))?)?$",
	                    G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);

	matched = g_regex_match(regex, source, G_REGEX_MATCH_DEFAULT, &info);
	if(!matched) {
		if(nick != NULL) {
			*nick = g_strdup(source);
		}

		g_clear_pointer(&info, g_match_info_unref);
		g_clear_pointer(&regex, g_regex_unref);

		return;
	}

	if(nick != NULL) {
		*nick = g_match_info_fetch_named(info, "nick");
	}

	if(user != NULL) {
		char *tmp = g_match_info_fetch_named(info, "user");

		if(!ibis_str_is_empty(tmp)) {
			*user = tmp;
		} else {
			g_free(tmp);
		}
	}

	if(host != NULL) {
		char *tmp = g_match_info_fetch_named(info, "host");

		if(!ibis_str_is_empty(tmp)) {
			*host = tmp;
		} else {
			g_free(tmp);
		}
	}

	g_clear_pointer(&info, g_match_info_unref);
	g_clear_pointer(&regex, g_regex_unref);
}

char *
ibis_source_serialize(const char *nick, const char *user, const char *host) {
	GString *str = NULL;

	g_return_val_if_fail(!ibis_str_is_empty(nick), NULL);

	str = g_string_new(nick);

	if(!ibis_str_is_empty(user)) {
		g_string_append_printf(str, "!%s", user);

		if(!ibis_str_is_empty(host)) {
			g_string_append_printf(str, "@%s", host);
		}
	}

	return g_string_free_and_steal(str);
}

/**
 * ibis_source_find_offset: (skip)
 * @source: The source.
 * @prefixes: The prefixes.
 *
 * Finds offset where the prefixes end and the source starts.
 *
 * Returns: The pointer in @source where the source starts and @prefixes ends.
 */
static const char *
ibis_source_find_offset(const char *source, const char *prefixes) {
	int source_offset = 0;

	g_return_val_if_fail(!ibis_str_is_empty(source), NULL);
	g_return_val_if_fail(!ibis_str_is_empty(prefixes), NULL);

	/* Prefixes are supposed to be in order of rank from high to low. So we
	 * walk prefixes checking if the current index of source matches the
	 * current prefix.
	 */
	for(int i = 0; prefixes[i] != '\0'; i++) {
		if(source[source_offset] == prefixes[i]) {
			source_offset += 1;
		}

		if(source[source_offset] == '\0') {
			break;
		}
	}

	return source + source_offset;
}

char *
ibis_source_strip_prefix(const char *source, const char *prefixes) {
	const char *offset = NULL;

	g_return_val_if_fail(!ibis_str_is_empty(source), NULL);
	g_return_val_if_fail(!ibis_str_is_empty(prefixes), NULL);

	offset = ibis_source_find_offset(source, prefixes);

	return g_strdup(offset);
}

char *
ibis_source_get_prefix(const char *source, const char *prefixes) {
	const char *offset = NULL;

	g_return_val_if_fail(!ibis_str_is_empty(source), NULL);
	g_return_val_if_fail(!ibis_str_is_empty(prefixes), NULL);

	offset = ibis_source_find_offset(source, prefixes);

	if(offset - source > 0) {
		return g_strndup(source, offset - source);
	}

	return NULL;
}
