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.
This commit is contained in:
Tom Wimmenhove
2023-04-13 00:21:28 +01:00
committed by Aleksander Morgado
parent d95114a576
commit bd5a82845a
3 changed files with 77 additions and 16 deletions

View File

@@ -102,6 +102,10 @@ _mmcli()
_filedir _filedir
return 0 return 0
;; ;;
'--messaging-create-sms-with-text')
_filedir
return 0
;;
'--messaging-delete-sms') '--messaging-delete-sms')
COMPREPLY=( $(compgen -W "[PATH|INDEX]" -- $cur) ) COMPREPLY=( $(compgen -W "[PATH|INDEX]" -- $cur) )
return 0 return 0

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright (C) 2012 Google, Inc. * Copyright (C) 2012 Google, Inc.
* Copyright (C) 2023 Tom Wimmenhove
*/ */
#include "config.h" #include "config.h"
@@ -50,6 +51,7 @@ static gboolean status_flag;
static gboolean list_flag; static gboolean list_flag;
static gchar *create_str; static gchar *create_str;
static gchar *create_with_data_str; static gchar *create_with_data_str;
static gchar *create_with_text_str;
static gchar *delete_str; static gchar *delete_str;
static GOptionEntry entries[] = { static GOptionEntry entries[] = {
@@ -69,6 +71,10 @@ static GOptionEntry entries[] = {
"Pass the given file as data contents when creating a new SMS", "Pass the given file as data contents when creating a new SMS",
"[File path]" "[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, { "messaging-delete-sms", 0, 0, G_OPTION_ARG_STRING, &delete_str,
"Delete a SMS from a given modem", "Delete a SMS from a given modem",
"[PATH|INDEX]" "[PATH|INDEX]"
@@ -116,6 +122,19 @@ mmcli_modem_messaging_options_enabled (void)
exit (EXIT_FAILURE); 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) if (status_flag)
mmcli_force_sync_operation (); mmcli_force_sync_operation ();
@@ -164,9 +183,35 @@ mmcli_modem_messaging_shutdown (void)
context_free (); 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 * static MMSmsProperties *
build_sms_properties_from_input (const gchar *properties_string, build_sms_properties_from_input (const gchar *properties_string,
const gchar *data_file) const gchar *data_file,
const gchar *text_file)
{ {
GError *error = NULL; GError *error = NULL;
MMSmsProperties *properties; MMSmsProperties *properties;
@@ -178,27 +223,34 @@ build_sms_properties_from_input (const gchar *properties_string,
} }
if (data_file) { if (data_file) {
gchar *path;
GFile *file;
gchar *contents; gchar *contents;
gsize contents_size; gsize contents_size;
g_debug ("Reading data from file '%s'", data_file); 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); if (text_file) {
path = g_file_get_path (file); gchar *contents;
if (!g_file_get_contents (path, gsize contents_size;
&contents,
&contents_size, if (mm_sms_properties_get_text(properties))
&error)) { {
g_printerr ("error: cannot read from file '%s': '%s'\n", g_printerr ("error: cannot use `--messaging-create-with-text': text "
data_file, error->message); "has already been set using `--messaging-create-sms'\n");
exit (EXIT_FAILURE); 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); g_free (contents);
} }
@@ -380,7 +432,8 @@ get_modem_ready (GObject *source,
MMSmsProperties *properties; MMSmsProperties *properties;
properties = build_sms_properties_from_input (create_str, 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..."); g_debug ("Asynchronously creating new SMS in modem...");
mm_modem_messaging_create (ctx->modem_messaging, mm_modem_messaging_create (ctx->modem_messaging,
properties, properties,
@@ -463,7 +516,8 @@ mmcli_modem_messaging_run_synchronous (GDBusConnection *connection)
MMSmsProperties *properties; MMSmsProperties *properties;
properties = build_sms_properties_from_input (create_str, 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..."); g_debug ("Synchronously creating new SMS in modem...");
sms = mm_modem_messaging_create_sync (ctx->modem_messaging, sms = mm_modem_messaging_create_sync (ctx->modem_messaging,
properties, properties,

View File

@@ -536,6 +536,9 @@ be 'sm', 'me', 'mt', 'sr', 'bm', 'ta'.
.B \-\-messaging\-create\-sms\-with\-data=PATH .B \-\-messaging\-create\-sms\-with\-data=PATH
Use \fBPATH\fR to a filename as the data to create a new SMS. Use \fBPATH\fR to a filename as the data to create a new SMS.
.TP .TP
.B \-\-messaging\-create\-sms\-with\-text=PATH
Use \fBPATH\fR to a filename as the message to create a new SMS.
.TP
.B \-\-messaging\-delete\-sms=[PATH|INDEX] .B \-\-messaging\-delete\-sms=[PATH|INDEX]
Delete an SMS from a given modem. Delete an SMS from a given modem.