# HG changeset patch
# Node ID 2cb6d26ea428
# Parent  2793a9468b7b
diff --git a/ibis-cli/ibiscli.c b/ibis-cli/ibiscli.c
--- a/ibis-cli/ibiscli.c
+++ b/ibis-cli/ibiscli.c
@@ -36,6 +36,7 @@
 static char *nick = NULL;
 static char *server = NULL;
 static int port = 6667;
+static char *password = NULL;
 static gboolean tls = FALSE;
 
 static GOptionEntry cli_options[] = {
@@ -61,6 +62,12 @@
 		.description = N_("The port to connect to on the server."),
 		.arg_description = N_("PORT"),
 	}, {
+		.long_name = "password",
+		.short_name = 'P',
+		.arg = G_OPTION_ARG_STRING,
+		.arg_data = &password,
+		.description = N_("The server password to use."),
+	}, {
 		.long_name = "tls",
 		.short_name = 't',
 		.arg = G_OPTION_ARG_NONE,
@@ -81,7 +88,7 @@
 	g_application_hold(application);
 
 	ibis_client_set_nick(client, nick);
-	ibis_client_connect(client, server, (guint16)port, tls, NULL);
+	ibis_client_connect(client, server, (guint16)port, password, tls, NULL);
 }
 
 static void
diff --git a/ibis/ibisclient.h b/ibis/ibisclient.h
--- a/ibis/ibisclient.h
+++ b/ibis/ibisclient.h
@@ -68,15 +68,18 @@
  * @client: The instance.
  * @hostname: The hostname or IP address to connect to.
  * @port: The port to connect to.
+ * @password: (nullable): The optional server password to use.
  * @tls: Whether or not to use a TLS connection.
  * @proxy_resolver: (transfer none) (nullable): A [iface@Gio.ProxyResolver].
  *
  * Attempts to connect to @hostname on @port.
  *
+ * If @password is provided, it will be deleted immediately after use.
+ *
  * Since: 0.1
  */
 IBIS_AVAILABLE_IN_0_1
-void ibis_client_connect(IbisClient *client, const char *hostname, guint16 port, gboolean tls, GProxyResolver *proxy_resolver);
+void ibis_client_connect(IbisClient *client, const char *hostname, guint16 port, const char *password, gboolean tls, GProxyResolver *proxy_resolver);
 
 /**
  * ibis_client_disconnect:
@@ -269,15 +272,19 @@
  * ibis_client_start:
  * @client: The instance.
  * @stream: The [class@Gio.IOStream] to process.
+ * @password: (nullable): The optional server password to use.
  *
  * Starts @client.
  *
  * Starting the @client means that it should start processing @stream.
  *
+ * If @password is provided, it will be used during registration and then
+ * immediately cleared.
+ *
  * Since: 0.1
  */
 IBIS_AVAILABLE_IN_0_1
-void ibis_client_start(IbisClient *client, GIOStream *stream);
+void ibis_client_start(IbisClient *client, GIOStream *stream, const char *password);
 
 /**
  * ibis_client_write:
diff --git a/ibis/ibisclient.c b/ibis/ibisclient.c
--- a/ibis/ibisclient.c
+++ b/ibis/ibisclient.c
@@ -111,13 +111,19 @@
 }
 
 static void
-ibis_client_register(IbisClient *client) {
+ibis_client_register(IbisClient *client, const char *password) {
 	IbisMessage *message = NULL;
 
 	if(client->connected) {
 		return;
 	}
 
+	if(!ibis_str_is_empty(password)) {
+		message = ibis_message_new("PASS");
+		ibis_message_set_params(message, password, NULL);
+		ibis_client_write(client, message);
+	}
+
 	message = ibis_message_new("USER");
 	ibis_message_set_params(message, ibis_client_get_username(client), "0",
 	                        "*", ibis_client_get_realname(client), NULL);
@@ -235,9 +241,15 @@
 		g_prefix_error_literal(&error, _("Unable to connect: "));
 		ibis_client_disconnect(client, error, NULL);
 	} else {
-		ibis_client_start(client, G_IO_STREAM(connection));
+		const char *password = NULL;
+
+		password = g_object_get_data(G_OBJECT(client), "ibis-password");
+
+		ibis_client_start(client, G_IO_STREAM(connection), password);
 	}
 
+	g_object_set_data(G_OBJECT(client), "ibis-password", NULL);
+
 	g_clear_error(&error);
 	g_clear_object(&socket_client);
 	g_clear_object(&connection);
@@ -522,7 +534,8 @@
  *****************************************************************************/
 void
 ibis_client_connect(IbisClient *client, const char *hostname, guint16 port,
-                    gboolean tls, GProxyResolver *proxy_resolver)
+                    const char *password, gboolean tls,
+                    GProxyResolver *proxy_resolver)
 {
 	GSocketClient *socket_client = NULL;
 
@@ -535,6 +548,11 @@
 
 	client->cancellable = g_cancellable_new();
 
+	if(!ibis_str_is_empty(password)) {
+		g_object_set_data_full(G_OBJECT(client), "ibis-password",
+		                       g_strdup(password), g_free);
+	}
+
 	g_socket_client_connect_to_host_async(socket_client, hostname, port,
 	                                      client->cancellable,
 	                                      ibis_client_connect_cb, client);
@@ -703,7 +721,8 @@
 }
 
 void
-ibis_client_start(IbisClient *client, GIOStream *stream) {
+ibis_client_start(IbisClient *client, GIOStream *stream, const char *password)
+{
 	GInputStream *input = NULL;
 	GOutputStream *output = NULL;
 
@@ -735,7 +754,7 @@
 	                                    client);
 
 	ibis_capabilities_start(client->capabilities);
-	ibis_client_register(client);
+	ibis_client_register(client, password);
 }
 
 void
