From aa50b2bf4fbf69a623b7cd55a25fa9a37374b3c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Thu, 27 Dec 2012 22:21:22 +0100 Subject: [PATCH] `convert' command can submit current message --- TODO | 1 + src/shigofumi.c | 72 +++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/TODO b/TODO index e103c6e..79f3e13 100644 --- a/TODO +++ b/TODO @@ -21,3 +21,4 @@ * Confirm message sending * Allow to publish sender name while composing * Get sender name +* Test message conversion diff --git a/src/shigofumi.c b/src/shigofumi.c index be1c59c..61c49fa 100644 --- a/src/shigofumi.c +++ b/src/shigofumi.c @@ -3999,11 +3999,12 @@ static int do_convert(const struct isds_document *document) { } -static void shi_convert_file_usage(const char *command) { +static void shi_convert_file_or_message_usage(const char *command) { oprintf(_( -"Usage: %s FILE [NAME]\n" +"Usage: %s [FILE [NAME]]\n" "Submit local FILE to authorized conversion under NAME. If NAME is missing,\n" -"it will use FILE name.\n"), command); +"it will use FILE name. If FILE is missing, it will submit current message.\n"), + command); oprintf(_( "\n" "If Czech POINT deposit accepts document, it will return document identifier\n" @@ -4014,36 +4015,63 @@ static void shi_convert_file_usage(const char *command) { } -static int shi_convert_file(int argc, const char **argv) { - int fd; +static int shi_convert_file_or_message(int argc, const char **argv) { + int fd = -1; struct isds_document document; int retval = 0; - if (!argv || argc > 3 || !argv[1] || !*argv[1]) { - shi_convert_file_usage((argv)?argv[0]:NULL); + if (!argv || argc > 3) { + shi_convert_file_or_message_usage((argv)?argv[0]:NULL); return -1; } memset(&document, 0, sizeof(document)); - if (argc == 3 && argv[2] && *argv[2]) - document.dmFileDescr = locale2utf8(argv[2]); - else - document.dmFileDescr = locale2utf8(argv[1]); - if (!document.dmFileDescr) { - printf(_("Could not convert document name to UTF-8\n")); - return -1; - } + if (NULL == argv[1] || !*argv[1]) { + /* Convert current message */ + if (!message) { + fprintf(stderr, _("No message loaded\n")); + return -1; + } + if (!message->raw || !message->raw_length) { + fprintf(stderr, + _("Current message is missing raw representation\n")); + return -1; + } + document.dmFileDescr = astrcat( + (NULL != message->envelope && NULL != message->envelope->dmID) ? + message->envelope->dmID : + "unknown", + ".zfo"); + if (NULL == document.dmFileDescr) { + printf(_("Could not build document name from message ID\n")); + return -1; + } + document.data = message->raw; + document.data_length = message->raw_length; + } else { + /* Convert local file */ + if (argc == 3 && argv[2] && *argv[2]) + document.dmFileDescr = locale2utf8(argv[2]); + else + document.dmFileDescr = locale2utf8(argv[1]); + if (!document.dmFileDescr) { + printf(_("Could not convert document name to UTF-8\n")); + return -1; + } - printf(_("Loading document from file `%s'...\n"), argv[1]); - if (mmap_file(argv[1], &fd, &document.data, &document.data_length)) { - free(document.dmFileDescr); - return -1; + printf(_("Loading document from file `%s'...\n"), argv[1]); + if (mmap_file(argv[1], &fd, &document.data, &document.data_length)) { + free(document.dmFileDescr); + return -1; + } } retval = do_convert(&document); - munmap_file(fd, document.data, document.data_length); + if (0 <= fd) { + munmap_file(fd, document.data, document.data_length); + } free(document.dmFileDescr); return retval; } @@ -4311,9 +4339,9 @@ int shi_batch(char *lines) { shi_commercialsending_usage, ARGTYPE_BOXID }, \ { "compose", shi_compose, N_("compose a message"), shi_compose_usage, \ ARGTYPE_FILE }, \ - { "convert", shi_convert_file, \ + { "convert", shi_convert_file_or_message, \ N_("submit local document for authorized conversion"), \ - shi_convert_file_usage, ARGTYPE_FILE }, \ + shi_convert_file_or_message_usage, ARGTYPE_FILE }, \ { "copying", shi_copying, N_("show this program licence excerpt"), NULL, \ ARGTYPE_NONE }, \ { "debug", shi_debug, N_("set debugging"), shi_debug_usage, \ -- 2.11.4.GIT