# HG changeset patch
# Node ID 788920c5b7a9c11f357f268dd6bd911b2c0a05fe
# Parent  19076ac4ad5c6fe5eff68007a5925769e432579f
diff --git a/ibis/ibisconstants.h b/ibis/ibisconstants.h
--- a/ibis/ibisconstants.h
+++ b/ibis/ibisconstants.h
@@ -1285,6 +1285,28 @@
 #define IBIS_FEATURE_PREFIX "PREFIX" IBIS_AVAILABLE_MACRO_IN_0_3
 
 /**
+ * IBIS_FEATURE_PREFIX_MODES:
+ *
+ * A constant for the modes portion of [const@FEATURE_PREFIX].
+ *
+ * This is a synthetic feature that isn't part of any of the specifications.
+ *
+ * Since: 0.8
+ */
+#define IBIS_FEATURE_PREFIX_MODES "PREFIX-MODES" IBIS_AVAILABLE_MACRO_IN_0_8
+
+/**
+ * IBIS_FEATURE_PREFIX_PREFIXES:
+ *
+ * A constant for the prefixes portion of [const@FEATURE_PREFIX].
+ *
+ * This is a synthetic feature that isn't part of any of the specifications.
+ *
+ * Since: 0.8
+ */
+#define IBIS_FEATURE_PREFIX_PREFIXES "PREFIX-PREFIXES" IBIS_AVAILABLE_MACRO_IN_0_8
+
+/**
  * IBIS_FEATURE_SAFELIST:
  *
  * A constant for the `SAFELIST` feature.
diff --git a/ibis/ibisfeatures.c b/ibis/ibisfeatures.c
--- a/ibis/ibisfeatures.c
+++ b/ibis/ibisfeatures.c
@@ -81,7 +81,8 @@
 	existing_value = g_hash_table_lookup(features->features,
 	                                     GINT_TO_POINTER(quark));
 	if(existing_value != NULL &&
-	   ibis_str_equal(g_value_get_string(existing_value), svalue)) {
+	   ibis_str_equal(g_value_get_string(existing_value), svalue))
+	{
 		return;
 	}
 
@@ -144,6 +145,42 @@
 	g_signal_emit(features, signals[SIG_CHANGED], quark, feature);
 }
 
+/**
+ * ibis_features_parse_prefix: (skip)
+ * @features: The instance.
+ * @value: The value of the PREFIX feature.
+ *
+ * Parses the value of the PREFIX feature and adds the synthetic MODES and
+ * PREFIXES features.
+ */
+static void
+ibis_features_parse_prefix(IbisFeatures *features, const char *prefix) {
+	GRegex *regex = NULL;
+	GMatchInfo *info = NULL;
+	gboolean matches = FALSE;
+
+	regex = g_regex_new("^\\((?P<modes>.+)\\)(?P<prefixes>.+)$",
+	                    G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT,
+	                    NULL);
+
+	matches = g_regex_match(regex, prefix, G_REGEX_MATCH_DEFAULT, &info);
+	if(matches) {
+		char *value = NULL;
+
+		value = g_match_info_fetch_named(info, "modes");
+		ibis_features_set_string(features, IBIS_FEATURE_PREFIX_MODES, value);
+		g_free(value);
+
+		value = g_match_info_fetch_named(info, "prefixes");
+		ibis_features_set_string(features, IBIS_FEATURE_PREFIX_PREFIXES,
+		                         value);
+		g_free(value);
+	}
+
+	g_clear_pointer(&info, g_match_info_unref);
+	g_clear_pointer(&regex, g_regex_unref);
+}
+
 static void
 ibis_features_set_feature(IbisFeatures *features, const char *feature,
                           const char *value)
@@ -208,6 +245,7 @@
 		                       ibis_str_to_uint(value));
 	} else if(ibis_str_equal(feature, IBIS_FEATURE_PREFIX)) {
 		ibis_features_set_string(features, IBIS_FEATURE_PREFIX, value);
+		ibis_features_parse_prefix(features, value);
 	} else if(ibis_str_equal(feature, IBIS_FEATURE_SAFELIST)) {
 		ibis_features_set_boolean(features, IBIS_FEATURE_SAFELIST, TRUE);
 	} else if(ibis_str_equal(feature, IBIS_FEATURE_SILENCE)) {
@@ -307,6 +345,7 @@
 ibis_features_clear(IbisFeatures *features) {
 	GHashTableIter iter;
 	gpointer key = NULL;
+	const char *default_prefix = "(ov)@+";
 
 	g_return_if_fail(IBIS_IS_FEATURES(features));
 
@@ -324,7 +363,8 @@
 	 * https://modern.ircdocs.horse/#rplisupport-parameters
 	 */
 	ibis_features_set_string(features, IBIS_FEATURE_CHANTYPES, "#");
-	ibis_features_set_string(features, IBIS_FEATURE_PREFIX, "(ov)@+");
+	ibis_features_set_string(features, IBIS_FEATURE_PREFIX, default_prefix);
+	ibis_features_parse_prefix(features, default_prefix);
 }
 
 gboolean
@@ -576,6 +616,20 @@
 	return ibis_features_get_string(features, IBIS_FEATURE_PREFIX);
 }
 
+const char *
+ibis_features_get_prefix_modes(IbisFeatures *features) {
+	g_return_val_if_fail(IBIS_IS_FEATURES(features), NULL);
+
+	return ibis_features_get_string(features, IBIS_FEATURE_PREFIX_MODES);
+}
+
+const char *
+ibis_features_get_prefix_prefixes(IbisFeatures *features) {
+	g_return_val_if_fail(IBIS_IS_FEATURES(features), NULL);
+
+	return ibis_features_get_string(features, IBIS_FEATURE_PREFIX_PREFIXES);
+}
+
 gboolean
 ibis_features_get_safelist(IbisFeatures *features) {
 	g_return_val_if_fail(IBIS_IS_FEATURES(features), FALSE);
diff --git a/ibis/ibisfeatures.h b/ibis/ibisfeatures.h
--- a/ibis/ibisfeatures.h
+++ b/ibis/ibisfeatures.h
@@ -107,6 +107,7 @@
  * @features: The instance.
  *
  * Gets the AWAYLEN parameter from @features.
+ *
  * See [const@FEATURE_AWAYLEN].
  *
  * Returns: AWAYLEN or 0 if not set.
@@ -121,6 +122,7 @@
  * @features: The instance.
  *
  * Gets the CASEMAPPING parameter from @features.
+ *
  * See [const@FEATURE_CASEMAPPING].
  *
  * Returns: CASEMAPPING or %NULL if not set.
@@ -135,6 +137,7 @@
  * @features: The instance.
  *
  * Gets the CHANLIMIT parameter from @features.
+ *
  * See [const@FEATURE_CHANLIMIT].
  *
  * Returns: CHANLIMIT or %NULL if not set.
@@ -149,6 +152,7 @@
  * @features: The instance.
  *
  * Gets the CHANMODES parameter from @features.
+ *
  * See [const@FEATURE_CHANMODES].
  *
  * Returns: CHANMODES or %NULL if not set.
@@ -163,6 +167,7 @@
  * @features: The instance.
  *
  * Gets the CHANTYPES parameter from @features.
+ *
  * See [const@FEATURE_CHANTYPES].
  *
  * Returns: CHANTYPES or the default value of '#' if not set.
@@ -177,6 +182,7 @@
  * @features: The instance.
  *
  * Gets the CHANNELLEN parameter from @features.
+ *
  * See [const@FEATURE_CHANNELLEN].
  *
  * Returns: CHANNELLEN or 0 if not set.
@@ -191,6 +197,7 @@
  * @features: The instance.
  *
  * Gets the ELIST parameter from @features.
+ *
  * See [const@FEATURE_ELIST].
  *
  * Returns: ELIST or %NULL if not set.
@@ -205,6 +212,7 @@
  * @features: The instance.
  *
  * Gets the EXCEPTS parameter from @features.
+ *
  * See [const@FEATURE_EXCEPTS].
  *
  * Returns: EXCEPTS or 0 if not set.
@@ -219,6 +227,7 @@
  * @features: The instance.
  *
  * Gets the EXTBAN parameter from @features.
+ *
  * See [const@FEATURE_EXTBAN].
  *
  * Returns: EXTBAN or %NULL if not set.
@@ -233,6 +242,7 @@
  * @features: The instance.
  *
  * Gets the HOSTLEN parameter from @features.
+ *
  * See [const@FEATURE_HOSTLEN].
  *
  * Returns: HOSTLEN or 0 if not set.
@@ -247,6 +257,7 @@
  * @features: The instance.
  *
  * Gets the INVEX parameter from @features.
+ *
  * See [const@FEATURE_INVEX].
  *
  * Returns: INVEX or 0 if not set.
@@ -261,6 +272,7 @@
  * @features: The instance.
  *
  * Gets the KICKLEN parameter from @features.
+ *
  * See [const@FEATURE_KICKLEN].
  *
  * Returns: KICKLEN or 0 if not set.
@@ -275,6 +287,7 @@
  * @features: The instance.
  *
  * Gets the MAXLIST parameter from @features.
+ *
  * See [const@FEATURE_MAXLIST].
  *
  * Returns: MAXLIST or %NULL if not set.
@@ -289,6 +302,7 @@
  * @features: The instance.
  *
  * Gets the MAXTARGETS parameter from @features.
+ *
  * See [const@FEATURE_MAXTARGETS].
  *
  * Returns: MAXTARGETS or 0 if not set.
@@ -303,6 +317,7 @@
  * @features: The instance.
  *
  * Gets the MODES parameter from @features.
+ *
  * See [const@FEATURE_MODES].
  *
  * Returns: MODES or 0 if not set.
@@ -317,6 +332,7 @@
  * @features: The instance.
  *
  * Gets the NETWORK parameter from @features.
+ *
  * See [const@FEATURE_NETWORK].
  *
  * Returns: NETWORK or %NULL if not set.
@@ -331,6 +347,7 @@
  * @features: The instance.
  *
  * Gets the NICKLEN parameter from @features.
+ *
  * See [const@FEATURE_NICKLEN].
  *
  * Returns: NICKLEN or 0 if not set.
@@ -345,6 +362,7 @@
  * @features: The instance.
  *
  * Gets the PREFIX parameter from @features.
+ *
  * See [const@FEATURE_PREFIX].
  *
  * Returns: PREFIX or the default value of '(ov)@+' if not set.
@@ -355,10 +373,41 @@
 const char *ibis_features_get_prefix(IbisFeatures *features);
 
 /**
+ * ibis_features_get_prefix_modes:
+ * @features: The instance.
+ *
+ * Gets the modes portion of the prefix feature.
+ *
+ * This is a synthetic feature that isn't defined by an specification.
+ *
+ * Returns: The modes portion of [const@FEATURE_PREFIX].
+ *
+ * Since: 0.8
+ */
+IBIS_AVAILABLE_IN_0_8
+const char *ibis_features_get_prefix_modes(IbisFeatures *features);
+
+/**
+ * ibis_features_get_prefix_prefixes:
+ * @features: The instance.
+ *
+ * Gets the prefixes portion of the prefix feature.
+ *
+ * This is a synthetic feature that isn't define by an specification.
+ *
+ * Returns: The prefixes portion of [const@FEATURE_PREFIX].
+ *
+ * Since: 0.8
+ */
+IBIS_AVAILABLE_IN_0_8
+const char *ibis_features_get_prefix_prefixes(IbisFeatures *features);
+
+/**
  * ibis_features_get_safelist:
  * @features: The instance.
  *
  * Gets the SAFELIST parameter from @features.
+ *
  * See [const@FEATURE_SAFELIST].
  *
  * Returns: %TRUE if SAFELIST parameter is advertised, otherwise %FALSE.
@@ -373,6 +422,7 @@
  * @features: The instance.
  *
  * Gets the SILENCE parameter from @features.
+ *
  * See [const@FEATURE_SILENCE].
  *
  * Returns: SILENCE or 0 if not set.
@@ -387,6 +437,7 @@
  * @features: The instance.
  *
  * Gets the STATUSMSG parameter from @features.
+ *
  * See [const@FEATURE_STATUSMSG].
  *
  * Returns: STATUSMSG or %NULL if not set.
@@ -401,6 +452,7 @@
  * @features: The instance.
  *
  * Gets the TARGMAX parameter from @features.
+ *
  * See [const@FEATURE_TARGMAX].
  *
  * Returns: TARGMAX or %NULL if not set.
@@ -415,6 +467,7 @@
  * @features: The instance.
  *
  * Gets the TOPICLEN parameter from @features.
+ *
  * See [const@FEATURE_TOPICLEN].
  *
  * Returns: TOPICLEN or 0 if not set.
@@ -429,6 +482,7 @@
  * @features: The instance.
  *
  * Gets the USERLEN parameter from @features.
+ *
  * See [const@FEATURE_USERLEN].
  *
  * Returns: USERLEN or 0 if not set.
diff --git a/ibis/tests/test_features.c b/ibis/tests/test_features.c
--- a/ibis/tests/test_features.c
+++ b/ibis/tests/test_features.c
@@ -113,7 +113,9 @@
 	g_assert_cmpuint(notify_counter, ==, 0);
 
 	g_assert_true(ibis_features_parse(features, message));
-	g_assert_cmpuint(notify_counter, ==, g_strv_length(msg_params));
+
+	/* We have 2 synthetic features we need to account for. */
+	g_assert_cmpuint(notify_counter, ==, g_strv_length(msg_params) + 2);
 
 	g_assert_cmpuint(ibis_features_get_awaylen(features), ==, 390);
 	g_assert_cmpstr(ibis_features_get_casemapping(features), ==,
@@ -135,6 +137,8 @@
 	g_assert_cmpstr(ibis_features_get_network(features), ==, "ErgoTest");
 	g_assert_cmpuint(ibis_features_get_nicklen(features), ==, 32);
 	g_assert_cmpstr(ibis_features_get_prefix(features), ==, "(qaohv)~&@%+");
+	g_assert_cmpstr(ibis_features_get_prefix_modes(features), ==, "qaohv");
+	g_assert_cmpstr(ibis_features_get_prefix_prefixes(features), ==, "~&@%+");
 	g_assert_true(ibis_features_get_safelist(features));
 	g_assert_cmpuint(ibis_features_get_silence(features), ==, 15);
 	g_assert_cmpstr(ibis_features_get_statusmsg(features), ==, "~&@%+");
@@ -217,6 +221,8 @@
 	g_assert_cmpstr(ibis_features_get_network(features), ==, NULL);
 	g_assert_cmpuint(ibis_features_get_nicklen(features), ==, 0);
 	g_assert_cmpstr(ibis_features_get_prefix(features), ==, "(ov)@+");
+	g_assert_cmpstr(ibis_features_get_prefix_modes(features), ==, "ov");
+	g_assert_cmpstr(ibis_features_get_prefix_prefixes(features), ==, "@+");
 	g_assert_false(ibis_features_get_safelist(features));
 	g_assert_cmpuint(ibis_features_get_silence(features), ==, 0);
 	g_assert_cmpstr(ibis_features_get_statusmsg(features), ==, NULL);
@@ -237,11 +243,11 @@
 	g_signal_connect(features, "changed",
 	                 G_CALLBACK(test_ibis_features_changed_counter), &counter);
 
-	/* There are 2 default features, they should get removed and reset on a
-	 * clear.
+	/* There are 2 default and 2 synthetic feature that should get removed and
+	 * reset on a clear which means we should see 8 changes.
 	 */
 	ibis_features_clear(features);
-	g_assert_cmpuint(counter, ==, 4);
+	g_assert_cmpuint(counter, ==, 8);
 
 	/* Build a simple RPL_ISUPPORT message and parse it. */
 	message = ibis_message_new(IBIS_RPL_ISUPPORT);
@@ -252,10 +258,10 @@
 	ibis_features_parse(features, message);
 	g_assert_cmpuint(counter, ==, 2);
 
-	/* Clear again, and make sure we got called 6 times, 4 removals, 2 adds. */
+	/* Clear again. Make sure we got called 10 times, 6 removals, 4 adds. */
 	counter = 0;
 	ibis_features_clear(features);
-	g_assert_cmpuint(counter, ==, 6);
+	g_assert_cmpuint(counter, ==, 10);
 
 	/* Cleanup. */
 	g_assert_finalize_object(message);
