summaryrefslogtreecommitdiff
path: root/src/sms_main.c
diff options
context:
space:
mode:
authorJames Maki <jmaki@multitech.com>2010-04-23 11:58:20 -0500
committerJames Maki <jmaki@multitech.com>2010-04-23 11:58:20 -0500
commit14fb44b17123b27e562379f51b75ee889982688d (patch)
tree3c2344f5c42396ab839638ee12f7c2f66a2656cb /src/sms_main.c
downloadsms-utils-14fb44b17123b27e562379f51b75ee889982688d.tar.gz
sms-utils-14fb44b17123b27e562379f51b75ee889982688d.tar.bz2
sms-utils-14fb44b17123b27e562379f51b75ee889982688d.zip
initial commit
Diffstat (limited to 'src/sms_main.c')
-rw-r--r--src/sms_main.c283
1 files changed, 283 insertions, 0 deletions
diff --git a/src/sms_main.c b/src/sms_main.c
new file mode 100644
index 0000000..5dda451
--- /dev/null
+++ b/src/sms_main.c
@@ -0,0 +1,283 @@
+/*
+ * SMS Main
+ *
+ * Copyright (C) 2010 by Multi-Tech Systems
+ *
+ * Author: James Maki <jmaki@multitech.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#define __MAIN_FILE_C 1
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <errno.h>
+#include <time.h>
+
+#include "global.h"
+#include "cmd_options.h"
+#include "sms_utils.h"
+#include "sms_send.h"
+#include "sms_send_email.h"
+#include "sms_list.h"
+#include "sms_delete.h"
+#include "xprintf.h"
+#include "utils.h"
+#include "atcmd.h"
+#include "sms_config.h"
+
+struct sms_cmd {
+ const char *object;
+ int (*call)(int argc, char **argv);
+};
+
+static int pb_list(int argc, char **argv)
+{
+ int fd;
+ int ret;
+
+ fd = tty_open(Global.core.device, Global.core.baud_rate);
+ if (fd < 0) {
+ fprintf(stderr, "failed to open tty device %s\n", Global.core.device);
+ return false;
+ }
+ atcmd_init(fd, Global.core.read_timeout);
+
+ ret = print_phonebook_list(fd, Global.core.pb_store);
+
+ tty_close(fd);
+
+ return ret;
+}
+
+static struct sms_cmd top_cmds[] = {
+ {
+ .object = "send",
+ .call = sms_send,
+ },
+#if HAVE_LIBESMTP
+ {
+ .object = "send-email",
+ .call = sms_send_email,
+ },
+#endif
+ {
+ .object = "delete",
+ .call = sms_delete,
+ },
+ {
+ .object = "list",
+ .call = sms_list,
+ },
+ {
+ .object = "read",
+ .call = sms_list,
+ },
+ {
+ .object = "pb-list",
+ .call = pb_list,
+ },
+ {
+ .object = NULL,
+ .call = NULL,
+ },
+};
+
+int sms_process_cmd(int argc, char **argv) {
+ int err;
+ const struct sms_cmd *cmd;
+ char *object;
+
+ if (argc <= 0) {
+ log_notice("object expected");
+ return false;
+ }
+
+ object = *argv;
+ cmd = top_cmds;
+
+ while (cmd->object) {
+ if (!tokcmp(object, cmd->object)) {
+ if (cmd->call) {
+ err = cmd->call(argc, argv);
+ return err;
+ } else {
+ log_notice("command does not have any actions");
+ return false;
+ }
+ } else {
+ cmd++;
+ }
+ }
+
+ log_notice("unknown object");
+
+ return false;
+}
+
+static void print_version(const char *name)
+{
+ printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name);
+ printf("Copyright (C) 2010 by Multi-Tech Systems\n");
+ printf(
+"This program is free software; you may redistribute it under the terms of\n"
+"the GNU General Public License version 2 or (at your option) any later version.\n"
+"This program has absolutely no warranty.\n");
+}
+
+static void usage(FILE *out)
+{
+ fprintf(out, "Usage: sms [ OPTIONS ... ] { send | read | list | delete }\n");
+ fprintf(out, "where OPTIONS := { \n");
+ fprintf(out, " -d, --device <device> |\n");
+ fprintf(out, " -b, --baud-rate <rate> |\n");
+ fprintf(out, " -i, --interactive |\n");
+ fprintf(out, " -n, --non-interactive |\n");
+ fprintf(out, " --read-timeout <timeout> (default: 5000 milliseconds) |\n");
+ fprintf(out, " --verbose\n");
+ fprintf(out, " }\n");
+ fprintf(out, "\n");
+}
+
+static char *short_options = "+"
+ __CMD_OPT_DEVICE
+ __CMD_OPT_BAUD_RATE
+ __CMD_OPT_INTERACTIVE
+ __CMD_OPT_NON_INTERACTIVE
+ __CMD_OPT_VERBOSE;
+static struct option long_options[] = {
+ {"device", 1, NULL, CMD_OPT_DEVICE},
+ {"baud-rate", 1, NULL, CMD_OPT_BAUD_RATE},
+ {"read-timeout", 1, NULL, CMD_OPT_READ_TIMEOUT},
+ {"msg-store-read", 1, NULL, CMD_OPT_MSG_STORE_READ},
+ {"msg-store-send", 1, NULL, CMD_OPT_MSG_STORE_SEND},
+ {"msg-store-new", 1, NULL, CMD_OPT_MSG_STORE_NEW},
+ {"pb-store", 1, NULL, CMD_OPT_PHONEBOOK_STORE},
+ {"interactive", 0, NULL, CMD_OPT_INTERACTIVE},
+ {"non-interactive", 0, NULL, CMD_OPT_NON_INTERACTIVE},
+ {"verbose", 0, NULL, CMD_OPT_VERBOSE},
+ {"version", 0, NULL, CMD_OPT_VERSION},
+ {"help", 0, NULL, CMD_OPT_HELP},
+ {0, 0, 0, 0},
+};
+
+int main(int argc, char *argv[])
+{
+ int i;
+ int option_index;
+ int ret;
+ int err;
+
+#if CONFIG_USE_SYSLOG
+ openlog("sms-utils", LOG_NDELAY, LOG_FACILITY);
+# if DEBUG
+ setlogmask(LOG_UPTO(LOG_DEBUG));
+# else
+ setlogmask(LOG_UPTO(LOG_INFO));
+# endif
+#endif
+
+ xprintf_init();
+
+ err = sms_config_load();
+ if (err < 0) {
+ log_error("sms config load error");
+ exit(1);
+ }
+
+ while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
+ switch (i) {
+ case 0:
+ break;
+
+ case CMD_OPT_INTERACTIVE:
+ Global.core.interactive = true;
+ break;
+
+ case CMD_OPT_NON_INTERACTIVE:
+ Global.core.interactive = false;
+ break;
+
+ case CMD_OPT_VERBOSE:
+ Global.core.verbose = true;
+ break;
+
+ case CMD_OPT_DEVICE:
+ Global.core.device = optarg;
+ break;
+
+ case CMD_OPT_BAUD_RATE:
+ Global.core.baud_rate = atoi(optarg);
+ Global.core.baud_rate = value_to_baud(Global.core.baud_rate);
+ if (Global.core.baud_rate == (speed_t) -1) {
+ sms_send_help(stderr);
+ exit(1);
+ }
+ break;
+
+ case CMD_OPT_READ_TIMEOUT:
+ Global.core.read_timeout = atoi(optarg);
+ break;
+
+ case CMD_OPT_MSG_STORE_READ:
+ Global.core.msg_store_read = optarg;
+ break;
+
+ case CMD_OPT_MSG_STORE_SEND:
+ Global.core.msg_store_send = optarg;
+ break;
+
+ case CMD_OPT_MSG_STORE_NEW:
+ Global.core.msg_store_new = optarg;
+ break;
+
+ case CMD_OPT_PHONEBOOK_STORE:
+ Global.core.pb_store = optarg;
+ break;
+
+ case CMD_OPT_VERSION:
+ print_version("sms");
+ exit(0);
+ break;
+
+ case CMD_OPT_HELP:
+ usage(stdout);
+ exit(0);
+ break;
+
+ default:
+ usage(stderr);
+ exit(1);
+ }
+ }
+
+ if (optind >= argc) {
+ usage(stderr);
+ exit(1);
+ }
+ argc -= optind;
+ argv += optind;
+
+ optind = 1;
+
+ ret = sms_process_cmd(argc, argv);
+
+ return ret ? 0 : 1;
+}
+