/* * SMS Main * * Copyright (C) 2010 by Multi-Tech Systems * * Author: James Maki * * 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 #include #include #include #include #include #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" static int global_init(void) { memset(&Global, 0, sizeof(Global)); Global.user.name = NULL; Global.user.email = NULL; Global.core.verbose = false; Global.core.interactive = true; Global.core.sms_init = true; Global.core.baud_rate = B115200; Global.core.read_timeout = 5000; Global.core.device = strdup(DEFAULT_DEVICE); Global.core.msg_store_read = strdup("SM"); Global.core.msg_store_send = strdup("SM"); Global.core.msg_store_new = strdup("SM"); Global.core.pb_store = strdup("SM"); strncpy(Global.core.model, "UNKNOWNMODEL", MODEL_LEN); strncpy(Global.core.manufacturer, "UNKNOWNMANUFACTURER", MANUFACTURER_LEN); Global.core.editor = strdup("vi"); Global.core.edit_file = strdup("${HOME}/.smsmsg"); Global.smtp.server = NULL; Global.smtp.port = 25; Global.smtp.user = NULL; Global.smtp.passwd = NULL; Global.smtp.encryption = NULL; Global.send_email.domain = NULL; return 0; } 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 = sms_device_open(); if (fd < 0) { return false; } ret = print_phonebook_list(fd, Global.core.pb_store); sms_device_close(fd); return ret; } static int sms_init(int argc, char **argv) { int fd; Global.core.sms_init = true; fd = sms_device_open(); if (fd < 0) { return false; } sms_device_close(fd); return true; } 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 = "sms-init", .call = sms_init, }, { .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 |\n"); fprintf(out, " -b, --baud-rate |\n"); fprintf(out, " -i, --interactive |\n"); fprintf(out, " -n, --non-interactive |\n"); fprintf(out, " --read-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(); global_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: free(Global.core.device); Global.core.device = strdup(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: free(Global.core.msg_store_read); Global.core.msg_store_read = strdup(optarg); break; case CMD_OPT_MSG_STORE_SEND: free(Global.core.msg_store_send); Global.core.msg_store_send = strdup(optarg); break; case CMD_OPT_MSG_STORE_NEW: free(Global.core.msg_store_new); Global.core.msg_store_new = strdup(optarg); break; case CMD_OPT_PHONEBOOK_STORE: free(Global.core.pb_store); Global.core.pb_store = strdup(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; }