From bd5a82845a653bd51707e08af44c787f737f9c8c Mon Sep 17 00:00:00 2001 From: Tom Wimmenhove Date: Thu, 13 Apr 2023 00:21:28 +0100 Subject: mmcli: added --messaging-create-sms-with-text option Added a `--messaging-create-sms-with-text' command line option that works similar to `--messaging-create-sms-with-data', except that it uses the content of the file as the message text instead of data. This allows creating mesasges containing both double and single quotes, which was not possible with the existing `--messaging-create-sms' command line option. --- cli/mmcli-modem-messaging.c | 86 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 16 deletions(-) (limited to 'cli/mmcli-modem-messaging.c') diff --git a/cli/mmcli-modem-messaging.c b/cli/mmcli-modem-messaging.c index 8912a248..d63ab480 100644 --- a/cli/mmcli-modem-messaging.c +++ b/cli/mmcli-modem-messaging.c @@ -16,6 +16,7 @@ * along with this program. If not, see . * * Copyright (C) 2012 Google, Inc. + * Copyright (C) 2023 Tom Wimmenhove */ #include "config.h" @@ -50,6 +51,7 @@ static gboolean status_flag; static gboolean list_flag; static gchar *create_str; static gchar *create_with_data_str; +static gchar *create_with_text_str; static gchar *delete_str; static GOptionEntry entries[] = { @@ -69,6 +71,10 @@ static GOptionEntry entries[] = { "Pass the given file as data contents when creating a new SMS", "[File path]" }, + { "messaging-create-sms-with-text", 0, 0, G_OPTION_ARG_FILENAME, &create_with_text_str, + "Pass the given file as message contents when creating a new SMS", + "[File path]" + }, { "messaging-delete-sms", 0, 0, G_OPTION_ARG_STRING, &delete_str, "Delete a SMS from a given modem", "[PATH|INDEX]" @@ -116,6 +122,19 @@ mmcli_modem_messaging_options_enabled (void) exit (EXIT_FAILURE); } + if (create_with_text_str && !create_str) { + g_printerr ("error: `--messaging-create-with-text' must be given along " + "with `--messaging-create-sms'\n"); + exit (EXIT_FAILURE); + } + + if (create_with_data_str && create_with_text_str) { + g_printerr ("error: `--messaging-create-with-data' and " + "`--messaging-create-with-text' cannot be used at the " + "same time\n"); + exit (EXIT_FAILURE); + } + if (status_flag) mmcli_force_sync_operation (); @@ -164,9 +183,35 @@ mmcli_modem_messaging_shutdown (void) context_free (); } +static void +get_file_contents (const gchar *filename, + gchar **contents, + gsize *contents_size) +{ + GError *error = NULL; + gchar *path; + GFile *file; + + g_debug ("Reading data from file '%s'", filename); + + file = g_file_new_for_commandline_arg (filename); + path = g_file_get_path (file); + if (!g_file_get_contents (path, + contents, + contents_size, + &error)) { + g_printerr ("error: cannot read from file '%s': '%s'\n", + filename, error->message); + exit (EXIT_FAILURE); + } + g_free (path); + g_object_unref (file); +} + static MMSmsProperties * build_sms_properties_from_input (const gchar *properties_string, - const gchar *data_file) + const gchar *data_file, + const gchar *text_file) { GError *error = NULL; MMSmsProperties *properties; @@ -178,27 +223,34 @@ build_sms_properties_from_input (const gchar *properties_string, } if (data_file) { - gchar *path; - GFile *file; gchar *contents; gsize contents_size; g_debug ("Reading data from file '%s'", data_file); + get_file_contents (data_file, &contents, &contents_size); + mm_sms_properties_set_data (properties, (guint8 *)contents, contents_size); + g_free (contents); + } - file = g_file_new_for_commandline_arg (data_file); - path = g_file_get_path (file); - if (!g_file_get_contents (path, - &contents, - &contents_size, - &error)) { - g_printerr ("error: cannot read from file '%s': '%s'\n", - data_file, error->message); + if (text_file) { + gchar *contents; + gsize contents_size; + + if (mm_sms_properties_get_text(properties)) + { + g_printerr ("error: cannot use `--messaging-create-with-text': text " + "has already been set using `--messaging-create-sms'\n"); exit (EXIT_FAILURE); } - g_free (path); - g_object_unref (file); - mm_sms_properties_set_data (properties, (guint8 *)contents, contents_size); + g_debug ("Reading message text from file '%s'", data_file); + get_file_contents (text_file, &contents, &contents_size); + + if (!g_utf8_validate (contents, contents_size, NULL)) { + g_printerr ("error: file '%s' contains invalid UTF-8\n", text_file); + exit (EXIT_FAILURE); + } + mm_sms_properties_set_text (properties, contents); g_free (contents); } @@ -380,7 +432,8 @@ get_modem_ready (GObject *source, MMSmsProperties *properties; properties = build_sms_properties_from_input (create_str, - create_with_data_str); + create_with_data_str, + create_with_text_str); g_debug ("Asynchronously creating new SMS in modem..."); mm_modem_messaging_create (ctx->modem_messaging, properties, @@ -463,7 +516,8 @@ mmcli_modem_messaging_run_synchronous (GDBusConnection *connection) MMSmsProperties *properties; properties = build_sms_properties_from_input (create_str, - create_with_data_str); + create_with_data_str, + create_with_text_str); g_debug ("Synchronously creating new SMS in modem..."); sms = mm_modem_messaging_create_sync (ctx->modem_messaging, properties, -- cgit v1.2.3-70-g09d2