# HG changeset patch
# Node ID a8bd0e224613605891eb6758b3e1d6213e3a3194
# Parent  dc95cd5d631099903baa39d20b44e7a025d5249b
diff --git a/ibis-cli/ibiscli.c b/ibis-cli/ibiscli.c
--- a/ibis-cli/ibiscli.c
+++ b/ibis-cli/ibiscli.c
@@ -152,6 +152,15 @@
 }
 #endif /* _WIN32 */
 
+static void
+ibiscli_network_changed_cb(GObject *self, G_GNUC_UNUSED GParamSpec *pspec,
+                           G_GNUC_UNUSED gpointer data)
+{
+	IbisClient *client = IBIS_CLIENT(self);
+
+	g_message("network name changed to '%s'", ibis_client_get_network(client));
+}
+
 /******************************************************************************
  * Main
  *****************************************************************************/
@@ -162,6 +171,8 @@
 	gint ret = 0;
 
 	client = ibis_client_new();
+	g_signal_connect(client, "notify::network",
+	                 G_CALLBACK(ibiscli_network_changed_cb), NULL);
 
 	application = g_application_new("org.imfreedom.ibis.Ibis-Cli",
 	                                G_APPLICATION_NON_UNIQUE);
diff --git a/ibis/ibisclient.h b/ibis/ibisclient.h
--- a/ibis/ibisclient.h
+++ b/ibis/ibisclient.h
@@ -223,6 +223,19 @@
 void ibis_client_set_hasl_context(IbisClient *client, HaslContext *hasl_context);
 
 /**
+ * ibis_client_get_network:
+ * @client: The instance.
+ *
+ * Gets the network that the client is connected to.
+ *
+ * Returns: (nullable): The network name if set, otherwise %NULL.
+ *
+ * Since: 0.4
+ */
+IBIS_AVAILABLE_IN_0_4
+const char *ibis_client_get_network(IbisClient *client);
+
+/**
  * ibis_client_get_nick:
  * @client: The instance.
  *
diff --git a/ibis/ibisclient.c b/ibis/ibisclient.c
--- a/ibis/ibisclient.c
+++ b/ibis/ibisclient.c
@@ -37,6 +37,7 @@
 	PROP_CONNECTED,
 	PROP_ERROR,
 	PROP_FEATURES,
+	PROP_NETWORK,
 	PROP_NICK,
 	PROP_REALNAME,
 	PROP_USERNAME,
@@ -66,6 +67,7 @@
 	GDataInputStream *input;
 	GOutputStream *output;
 
+	char *network;
 	char *nick;
 	char *alt_nick;
 	char *realname;
@@ -118,6 +120,15 @@
 }
 
 static void
+ibis_client_set_network(IbisClient *client, const char *network) {
+	g_return_if_fail(IBIS_IS_CLIENT(client));
+
+	if(g_set_str(&client->network, network)) {
+		g_object_notify_by_pspec(G_OBJECT(client), properties[PROP_NETWORK]);
+	}
+}
+
+static void
 ibis_client_register(IbisClient *client, const char *password) {
 	IbisMessage *message = NULL;
 
@@ -308,6 +319,20 @@
 	return FALSE;
 }
 
+static void
+ibis_client_network_feature_cb(IbisFeatures *features, const char *name,
+                               gpointer data)
+{
+	IbisClient *client = data;
+	const char *value = NULL;
+
+	/* This is a detailed signal, so name should always be the value of
+	 * IBIS_FEATURE_NETWORK.
+	 */
+	value = ibis_features_get_string(features, name);
+	ibis_client_set_network(client, value);
+}
+
 /******************************************************************************
  * GObject Implementation
  *****************************************************************************/
@@ -326,6 +351,7 @@
 	g_clear_error(&client->error);
 	g_clear_object(&client->features);
 
+	g_clear_pointer(&client->network, g_free);
 	g_clear_pointer(&client->alt_nick, g_free);
 	g_clear_pointer(&client->nick, g_free);
 	g_clear_pointer(&client->realname, g_free);
@@ -359,6 +385,9 @@
 	case PROP_FEATURES:
 		g_value_set_object(value, ibis_client_get_features(client));
 		break;
+	case PROP_NETWORK:
+		g_value_set_string(value, ibis_client_get_network(client));
+		break;
 	case PROP_NICK:
 		g_value_set_string(value, ibis_client_get_nick(client));
 		break;
@@ -408,7 +437,11 @@
 static void
 ibis_client_init(IbisClient *client) {
 	client->capabilities = ibis_capabilities_new();
+
 	client->features = ibis_features_new();
+	g_signal_connect_object(client->features, "changed::" IBIS_FEATURE_NETWORK,
+	                        G_CALLBACK(ibis_client_network_feature_cb),
+	                        client, 0);
 }
 
 static void
@@ -494,6 +527,19 @@
 		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
 	/**
+	 * IbisClient:network:
+	 *
+	 * The advertised name of the network from the server sending the
+	 * [const@RPL_ISUPPORT] message.
+	 *
+	 * Since: 0.4
+	 */
+	properties[PROP_NETWORK] = g_param_spec_string(
+		"network", NULL, NULL,
+		NULL,
+		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+	/**
 	 * IbisClient:nick:
 	 *
 	 * The primary nick to use.
@@ -803,6 +849,13 @@
 }
 
 const char *
+ibis_client_get_network(IbisClient *client) {
+	g_return_val_if_fail(IBIS_IS_CLIENT(client), NULL);
+
+	return client->network;
+}
+
+const char *
 ibis_client_get_nick(IbisClient *client) {
 	g_return_val_if_fail(IBIS_IS_CLIENT(client), NULL);
 
diff --git a/ibis/tests/test_client.c b/ibis/tests/test_client.c
--- a/ibis/tests/test_client.c
+++ b/ibis/tests/test_client.c
@@ -253,6 +253,40 @@
 	g_assert_finalize_object(client);
 }
 
+static void
+test_ibis_client_network_default(void) {
+	IbisClient *client = NULL;
+	const char *network = NULL;
+
+	client = ibis_client_new();
+
+	network = ibis_client_get_network(client);
+	g_assert_null(network);
+
+	g_assert_finalize_object(client);
+}
+
+static void
+test_ibis_client_network_custom(void) {
+	IbisClient *client = NULL;
+	IbisFeatures *features = NULL;
+	IbisMessage *message = NULL;
+	const char *network = NULL;
+
+	client = ibis_client_new();
+	features = ibis_client_get_features(client);
+
+	message = ibis_message_new(IBIS_RPL_ISUPPORT);
+	ibis_message_set_params(message, IBIS_FEATURE_NETWORK "=unit-test", NULL);
+	ibis_features_parse(features, message);
+	g_assert_finalize_object(message);
+
+	network = ibis_client_get_network(client);
+	g_assert_cmpstr(network, ==, "unit-test");
+
+	g_assert_finalize_object(client);
+}
+
 /******************************************************************************
  * Main
  *****************************************************************************/
@@ -288,5 +322,10 @@
 	g_test_add_func("/ibis/client/is-channel/custom",
 	                test_ibis_client_is_channel_custom);
 
+	g_test_add_func("/ibis/client/network/default",
+	                test_ibis_client_network_default);
+	g_test_add_func("/ibis/client/network/custom",
+	                test_ibis_client_network_custom);
+
 	return g_test_run();
 }
