/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple 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/>.
 */

#ifdef G_LOG_DOMAIN
# undef G_LOG_DOMAIN
#endif /* G_LOG_DOMAIN */
#define G_LOG_DOMAIN "PurpleUtil"

#include <purpleconfig.h>

#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>

#include <ctype.h>

#include <birb.h>

#include "util.h"
#include "purplepath.h"

void
purple_util_init(void) {
}

void
purple_util_uninit(void) {
	purple_util_set_user_dir(NULL);
}

/**************************************************************************
 * String Functions
 **************************************************************************/
char *
purple_strreplace(const char *string, const char *delimiter,
                  const char *replacement)
{
	char **split;
	char *ret;

	g_return_val_if_fail(string      != NULL, NULL);
	g_return_val_if_fail(delimiter   != NULL, NULL);
	g_return_val_if_fail(replacement != NULL, NULL);

	split = g_strsplit(string, delimiter, 0);
	ret = g_strjoinv(replacement, split);
	g_strfreev(split);

	return ret;
}

/**************************************************************************
 * URI/URL Functions
 **************************************************************************/

/* Originally lifted from
 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
 * ... and slightly modified to be a bit more rfc822 compliant
 * ... and modified a bit more to make domain checking rfc1035 compliant
 *     with the exception permitted in rfc1101 for domains to start with digit
 *     but not completely checking to avoid conflicts with IP addresses
 */
gboolean
purple_email_is_valid(const char *address)
{
	g_return_val_if_fail(!birb_str_is_empty(address), FALSE);

	return g_regex_match_simple("^[^@]+@[^@:]+(:\\d+)?$", address,
	                            G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT);
}

/**************************************************************************
 * UTF8 String Functions
 **************************************************************************/
int
purple_utf8_strcasecmp(const char *a, const char *b) {
	char *a_norm = NULL;
	char *b_norm = NULL;
	int ret = -1;

	if(!a && b) {
		return -1;
	} else if(!b && a) {
		return 1;
	} else if(!a && !b) {
		return 0;
	}

	if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL)) {
		g_critical("purple_utf8_strcasecmp: One or both parameters are "
		           "invalid UTF8");

		return ret;
	}

	a_norm = g_utf8_casefold(a, -1);
	b_norm = g_utf8_casefold(b, -1);
	ret = g_utf8_collate(a_norm, b_norm);
	g_free(a_norm);
	g_free(b_norm);

	return ret;
}
