# HG changeset patch
# Node ID 9014754da2cf
# Parent  5ac321d71801
diff --git a/libpurple/protocols/ircv3/meson.build b/libpurple/protocols/ircv3/meson.build
--- a/libpurple/protocols/ircv3/meson.build
+++ b/libpurple/protocols/ircv3/meson.build
@@ -7,6 +7,7 @@
 	'purpleircv3protocol.c',
 	'purpleircv3protocolim.c',
 	'purpleircv3sasl.c',
+	'purpleircv3source.c',
 ]
 
 IRCV3_HEADERS = [
@@ -18,6 +19,7 @@
 	'purpleircv3protocol.h',
 	'purpleircv3protocolim.h',
 	'purpleircv3sasl.h',
+	'purpleircv3source.h',
 ]
 
 if not DYNAMIC_IRCV3
diff --git a/libpurple/protocols/ircv3/purpleircv3source.h b/libpurple/protocols/ircv3/purpleircv3source.h
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/ircv3/purpleircv3source.h
@@ -0,0 +1,50 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * 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, see <https://www.gnu.org/licenses/>.
+ */
+#if !defined(PURPLE_IRCV3_GLOBAL_HEADER_INSIDE) && \
+    !defined(PURPLE_IRCV3_COMPILATION)
+# error "only <libpurple/protocols/ircv3.h> may be included directly"
+#endif
+
+#ifndef PURPLE_IRCV3_SOURCE_H
+#define PURPLE_IRCV3_SOURCE_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+/**
+ * purple_ircv3_source_parse:
+ * @source: The source to parse.
+ * @nick: (out) (transfer full): A return address for the nick.
+ * @user: (out) (transfer full): A return address for the user.
+ * @host: (out) (transfer full): A return address for the host.
+ *
+ * Parses a `source` string like `pidgy!~u@53unc8n42i868.irc` into its nick,
+ * user, and host parts per https://modern.ircdocs.horse/#source.
+ *
+ * If the user or host aren't present in @source, but a return address is
+ * provided for them, that pointer will be set to %NULL.
+ *
+ * Since: 3.0.0
+ */
+void purple_ircv3_source_parse(const char *source, char **nick, char **user, char **host);
+
+G_END_DECLS
+
+#endif /* PURPLE_IRCV3_SOURCE_H */
diff --git a/libpurple/protocols/ircv3/purpleircv3source.c b/libpurple/protocols/ircv3/purpleircv3source.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/ircv3/purpleircv3source.c
@@ -0,0 +1,87 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * 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, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <purple.h>
+
+#include "purpleircv3source.h"
+
+/******************************************************************************
+ * Public API
+ *****************************************************************************/
+void
+purple_ircv3_source_parse(const char *source, char **nick, char **user,
+                          char **host)
+{
+	GRegex *regex = NULL;
+	GMatchInfo *info = NULL;
+	gboolean matched = FALSE;
+
+	g_return_if_fail(!purple_strempty(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(!purple_strempty(tmp)) {
+			*user = tmp;
+		} else {
+			g_free(tmp);
+		}
+	}
+
+	if(host != NULL) {
+		char *tmp = g_match_info_fetch_named(info, "host");
+
+		if(!purple_strempty(tmp)) {
+			*host = tmp;
+		} else {
+			g_free(tmp);
+		}
+	}
+
+	g_clear_pointer(&info, g_match_info_unref);
+	g_clear_pointer(&regex, g_regex_unref);
+}
diff --git a/libpurple/protocols/ircv3/tests/meson.build b/libpurple/protocols/ircv3/tests/meson.build
--- a/libpurple/protocols/ircv3/tests/meson.build
+++ b/libpurple/protocols/ircv3/tests/meson.build
@@ -1,5 +1,6 @@
 TESTS = [
 	'parser',
+	'source',
 ]
 
 foreach prog : TESTS
diff --git a/libpurple/protocols/ircv3/tests/test_ircv3_source.c b/libpurple/protocols/ircv3/tests/test_ircv3_source.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/ircv3/tests/test_ircv3_source.c
@@ -0,0 +1,143 @@
+/*
+ * Purple - Internet Messaging Library
+ * Copyright (C) Pidgin Developers <devel@pidgin.im>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+
+#include <purple.h>
+
+#include "../purpleircv3source.h"
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_ircv3_source_parse_return_address_required(void) {
+	if(g_test_subprocess()) {
+		purple_ircv3_source_parse("pidgy", NULL, NULL, NULL);
+	}
+
+	g_test_trap_subprocess(NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
+	g_test_trap_assert_stderr("*nick != NULL || user != NULL || host != NULL' failed*");
+}
+
+static void
+test_ircv3_source_parse_nick(void) {
+	char *nick = NULL;
+	char *user = NULL;
+	char *host = NULL;
+
+	/* Once to make sure that user and host are null. */
+	purple_ircv3_source_parse("pidgy", &nick, &user, &host);
+	g_assert_cmpstr(nick, ==, "pidgy");
+	g_clear_pointer(&nick, g_free);
+
+	g_assert_null(user);
+	g_assert_null(host);
+
+	/* Again to make sure it works without user and host. */
+	purple_ircv3_source_parse("pidgy", &nick, NULL, NULL);
+	g_assert_cmpstr(nick, ==, "pidgy");
+	g_clear_pointer(&nick, g_free);
+}
+
+static void
+test_ircv3_source_parse_user(void) {
+	char *nick = NULL;
+	char *user = NULL;
+	char *host = NULL;
+
+	/* Once to make sure host is null. */
+	purple_ircv3_source_parse("pidgy!~u", &nick, &user, &host);
+	g_assert_cmpstr(nick, ==, "pidgy");
+	g_clear_pointer(&nick, g_free);
+
+	g_assert_cmpstr(user, ==, "~u");
+	g_clear_pointer(&user, g_free);
+
+	g_assert_null(host);
+
+	/* Again to make sure nick and host aren't required. */
+	purple_ircv3_source_parse("pidgy!~u", NULL, &user, NULL);
+	g_assert_cmpstr(user, ==, "~u");
+	g_clear_pointer(&user, g_free);
+}
+
+static void
+test_ircv3_source_parse_host(void) {
+	char *nick = NULL;
+	char *user = NULL;
+	char *host = NULL;
+
+	/* Once to make sure everything works. */
+	purple_ircv3_source_parse("pidgy!~u@53unc8n42i868.irc", &nick, &user,
+	                          &host);
+	g_assert_cmpstr(nick, ==, "pidgy");
+	g_clear_pointer(&nick, g_free);
+
+	g_assert_cmpstr(user, ==, "~u");
+	g_clear_pointer(&user, g_free);
+
+	g_assert_cmpstr(host, ==, "53unc8n42i868.irc");
+	g_clear_pointer(&host, g_free);
+
+	/* Again to make sure nick and host aren't required. */
+	purple_ircv3_source_parse("pidgy!~u@53unc8n42i868.irc", NULL, NULL, &host);
+	g_assert_cmpstr(host, ==, "53unc8n42i868.irc");
+	g_clear_pointer(&host, g_free);
+}
+
+static void
+test_ircv3_source_parse_baddies(void) {
+	char *nick = NULL;
+
+	purple_ircv3_source_parse("\n", &nick, NULL, NULL);
+	g_assert_null(nick);
+
+	purple_ircv3_source_parse("a\nb", &nick, NULL, NULL);
+	g_assert_null(nick);
+
+	purple_ircv3_source_parse("\r", &nick, NULL, NULL);
+	g_assert_null(nick);
+
+	purple_ircv3_source_parse("c\rd", &nick, NULL, NULL);
+	g_assert_null(nick);
+
+	purple_ircv3_source_parse(" ", &nick, NULL, NULL);
+	g_assert_null(nick);
+
+	purple_ircv3_source_parse("e f", &nick, NULL, NULL);
+	g_assert_null(nick);
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+int
+main(int argc, char *argv[]) {
+	g_test_init(&argc, &argv, NULL);
+
+	g_test_add_func("/ircv3/source/parse/return-address-required",
+	                test_ircv3_source_parse_return_address_required);
+	g_test_add_func("/ircv3/source/parse/nick", test_ircv3_source_parse_nick);
+	g_test_add_func("/ircv3/source/parse/user", test_ircv3_source_parse_user);
+	g_test_add_func("/ircv3/source/parse/host", test_ircv3_source_parse_host);
+	g_test_add_func("/ircv3/source/parse/baddies",
+	                test_ircv3_source_parse_baddies);
+
+	return g_test_run();
+}
\ No newline at end of file
