mms-send.c 4.77 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2016 Jolla Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <glib.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Generated headers */
#include "org.nemomobile.MmsEngine.h"

static const char pname[] = "mms-send";

enum app_ret_value {
    RET_OK,
    RET_ERR_CMDLINE,
    RET_ERR_SEND
};

#define MMS_SEND_REQUEST_DELIVERY_REPORT (0x01)
#define MMS_SEND_REQUEST_READ_REPORT     (0x02)
#define MMS_SEND_FLAG_VERBOSE            (0x04)

/* Protocol flags */
#define MMS_SEND_REQUEST_PROTOCOL_FLAGS  (\
        MMS_SEND_REQUEST_DELIVERY_REPORT |\
        MMS_SEND_REQUEST_READ_REPORT     )

static
char*
mms_send(
    char* files[],
    int count,
46
    const char* imsi,
47 48 49 50
    const char* to,
    const char* subject,
    int flags)
{
51
    char* imsi_out = NULL;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    GError* error = NULL;
    OrgNemomobileMmsEngine* proxy =
        org_nemomobile_mms_engine_proxy_new_for_bus_sync(
            G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
            "org.nemomobile.MmsEngine", "/", NULL, &error);
    if (proxy) {
        int i;
        gboolean ok = TRUE;
        char* fname = g_malloc(PATH_MAX);
        GVariant* parts;
        GVariantBuilder b;

        g_variant_builder_init(&b, G_VARIANT_TYPE("a(sss)"));
        for (i=0; ok && i<count; i++) {
            if (realpath(files[i], fname)) {
                if (g_file_test(fname, G_FILE_TEST_IS_REGULAR)) {
                    g_variant_builder_add(&b, "(sss)", fname, "", "");
                } else {
                    fprintf(stderr, "%s: no such file: %s\n", pname, fname);
                    ok = FALSE;
                }
            } else {
                fprintf(stderr, "%s: %s\n", fname, strerror(errno));
                ok = FALSE;
            }
        }

        parts = g_variant_ref_sink(g_variant_builder_end(&b));
        if (ok) {
            char** to_list = g_strsplit(to, ",", 0);
            const char* none = NULL;
            if (!org_nemomobile_mms_engine_call_send_message_sync(proxy,
84
                0, imsi, (const gchar* const*)to_list, &none, &none, subject,
85
                flags & MMS_SEND_REQUEST_PROTOCOL_FLAGS,
86
                parts, &imsi_out, NULL, &error)) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100
                /* D-Bus error */
                fprintf(stderr, "%s\n", error->message);
                g_error_free(error);
            }
            g_free(to_list);
        }

        g_free(fname);
        g_variant_unref(parts);
        g_object_unref(proxy);
    } else if (error) {
        fprintf(stderr, "%s\n", error->message);
        g_error_free(error);
    }
101
    return imsi_out;
102 103 104 105 106 107 108
}

int main(int argc, char* argv[])
{
    int ret = RET_ERR_CMDLINE;
    gboolean ok, verbose = FALSE, dr = FALSE, rr = FALSE;
    GError* error = NULL;
109
    char* imsi = "";
110
    char* subject = "";
111 112 113
    GOptionEntry entries[] = {
        { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
          "Enable verbose output", NULL },
114 115
        { "imsi", 'i', 0, G_OPTION_ARG_STRING, &imsi,
          "Select the SIM card", "IMSI" },
116 117
        { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
          "Set message subject", "TEXT" },
118 119 120 121 122 123 124 125 126 127 128 129
        { "delivery-report", 'd', 0, G_OPTION_ARG_NONE, &dr,
          "Request delivery report", NULL },
        { "read-report", 'r', 0, G_OPTION_ARG_NONE, &rr,
          "Request read report", NULL },
        { NULL }
    };
    GOptionContext* options = g_option_context_new("TO FILES...");
    g_option_context_add_main_entries(options, entries, NULL);
    ok = g_option_context_parse(options, &argc, &argv, &error);
    if (ok) {
        if (argc > 2) {
            int flags = 0;
130
            char* res;
131 132 133
            if (verbose) flags |= MMS_SEND_FLAG_VERBOSE;
            if (dr) flags |= MMS_SEND_REQUEST_DELIVERY_REPORT;
            if (rr) flags |= MMS_SEND_REQUEST_READ_REPORT;
134 135 136 137
            res = mms_send(argv+2, argc-2, imsi, argv[1], subject, flags);
            if (res) {
                if (verbose) printf("%s\n", res);
                g_free(res);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                ret = RET_OK;
            }
        } else {
            char* help = g_option_context_get_help(options, TRUE, NULL);
            fprintf(stderr, "%s", help);
            g_free(help);
        }
    } else {
        printf("%s: %s\n", pname, error->message);
        g_error_free(error);
    }
    g_option_context_free(options);
    return ret;
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */