sms_client.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:11k
- /* -------------------------------------------------------------------- */
- /* SMS Client, send messages to mobile phones and pagers */
- /* */
- /* sms_client.c */
- /* */
- /* Copyright (C) 1997,1998 Angelo Masci */
- /* */
- /* This library is free software; you can redistribute it and/or */
- /* modify it under the terms of the GNU Library General Public */
- /* License as published by the Free Software Foundation; either */
- /* version 2 of the License, or (at your option) any later version. */
- /* */
- /* This library 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 */
- /* Library General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU Library General Public */
- /* License along with this library; if not, write to the Free */
- /* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
- /* */
- /* You can contact the author at this e-mail address: */
- /* */
- /* angelo@styx.demon.co.uk */
- /* */
- /* -------------------------------------------------------------------- */
- /* $Id: sms_client.c,v 5.1 1998/02/01 07:10:39 root Exp root $
- -------------------------------------------------------------------- */
- #include <stdio.h>
- #include <errno.h>
- #include <signal.h>
- #include <unistd.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <time.h>
- #if defined(LINUX)
- #include <getopt.h>
- #endif
- #if defined(NEXT)
- #include <libc.h>
- #endif
- #include "sms_error.h"
- #include "sms_list.h"
- #include "expect.h"
- #include "logfile.h"
- #include "parserc.h"
- #include "driver/driver.h"
- #include "common.h"
- #include "sms_lock.h"
- #include "sms_resource.h"
- /* -------------------------------------------------------------------- */
- #if !defined(MVERSION)
- #error "MVERSION undefined"
- #else
- #define VERSION MVERSION
- #endif
- #if !defined(MLOGFILE)
- #error "MLOGFILE undefined"
- #else
- #define LOGFILE MLOGFILE
- #endif
- #if !defined(MLOGLEVEL)
- #error "MLOGLEVEL undefined"
- #else
- #define LOGLEVEL MLOGLEVEL
- #endif
- #if !defined(MSERVICEDIR)
- #error "MSERVICEDIR undefined"
- #else
- #define SERVICEDIR MSERVICEDIR
- #endif
- #define CONFIG_FILE (MSERVICEDIR "/sms_config")
- #define SMSLOCK_FILE (MSERVICEDIR "/smslock")
-
- /* -------------------------------------------------------------------- */
- static char
- *SMS_lock_action,
- *SMS_lockfile,
- *SMS_default_service;
- static long
- SMS_lock_retry_delay;
-
- /* -------------------------------------------------------------------- */
- #define MAXMESSAGELEN 150
- #define MAXMESSAGES 256
- /* -------------------------------------------------------------------- */
- static RESOURCE resource_list[] =
- {
- { RESOURCE_STRING, "SMS_default_service", 0, 1, NULL, 0, "CELLNET", 0, &SMS_default_service },
- { RESOURCE_STRING, "SMS_lock_action", 0, 1, NULL, 0, "BLOCK", 0, &SMS_lock_action },
- { RESOURCE_NUMERIC, "SMS_lock_retry_delay", 0, 0, NULL, 5000000, NULL, 0, &SMS_lock_retry_delay },
- { RESOURCE_STRING, "SMS_lockfile", 0, 1, NULL, 0, SMSLOCK_FILE, 0, &SMS_lockfile },
- { RESOURCE_NULL, NULL, 0, 1, NULL, 0, NULL, 0, NULL }
- };
- /* -------------------------------------------------------------------- */
- void get_message(char *message);
- void usage(char *file);
- int main(int, char *[]);
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- void get_message(char *message)
- {
- int i,
- c;
- if (isatty(fileno(stdin)))
- { printf("Enter your message and end with Control-Dn");
- }
-
- for (i=0; i<MAXMESSAGELEN; i++)
- {
- c = getchar();
- if (c == EOF)
- { break;
- }
- message[i] = c;
- }
- if (i == MAXMESSAGELEN)
- {
- lprintf(LOG_ERROR, "Message too longn");
- exit(EMESSAGETOOLONG);
- }
- message[i] = ' ';
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- void usage(char *file)
- {
- char blank[128],
- *src,
- *dst;
- lprintf(LOG_STANDARD, "Usage: %s [-v]n", file);
- lprintf(LOG_STANDARD, "Usage: %s [-d]n", file);
- lprintf(LOG_STANDARD, " %s [-q][-l loglevel]n", file);
- src = file;
- dst = blank;
- while(*src != ' ')
- {
- *dst = ' ';
-
- dst++;
- src++;
- }
- *dst = ' ';
- lprintf(LOG_STANDARD, " %s [service:]number|name[,name|[service:]number...]n", blank);
- lprintf(LOG_STANDARD, " %s [message] ...n", blank);
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void SMS_release_lock(void)
- { resource_unlock(SMS_lockfile);
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void SMS_obtain_lock(char *sms_lockfile)
- {
- int no_block,
- notify;
-
- resource_check_lockdir(sms_lockfile);
- /* Exit with message if lockdir */
- /* does not exist or we dont */
- /* have write permissions */
- if (strcmp(SMS_lock_action, "NO_BLOCK") == 0)
- { no_block = TRUE;
- }
- else if (strcmp(SMS_lock_action, "BLOCK") == 0)
- { no_block = FALSE;
- }
- else
- { lprintf(LOG_WARNING, "SMS_lock_action invalid, defaulting to NO_BLOCKn");
- no_block = TRUE;
- }
- notify = TRUE;
- while(resource_lock(sms_lockfile))
- {
- if (no_block)
- { lprintf(LOG_ERROR, "Could not obtain lock for sms_clientn");
- lprintf(LOG_STANDARD, "Another SMS Client is already running.n");
- exit(-1);
- }
- if (notify)
- { lprintf(LOG_VERBOSE, "Blocking on lockfile '%s'n", sms_lockfile);
- lprintf(LOG_STANDARD, "Another SMS Client is already running.n");
- lprintf(LOG_STANDARD, "Waiting...n");
- notify = FALSE;
- }
- resource_wait(sms_lockfile, SMS_lock_retry_delay);
- }
- if (!notify)
- { lprintf(LOG_STANDARD, "The other SMS Client has finished.n");
- lprintf(LOG_STANDARD, "Continuing...n");
- }
- atexit(SMS_release_lock); /* Establish EXIT handler */
- /* to release the lockfile if */
- /* we leave prematurely */
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- int main(int argc, char *argv[])
- {
- DEVICE_ENTRY
- *device;
- char *mservice,
- message[MAXMESSAGES][MAXMESSAGELEN +1],
- *ptr,
- *protocol;
-
- int i, num_messages;
- SMS_parent_list
- *parent_node,
- *parent_list;
- SMS_list
- *list,
- *numbers,
- *delivered;
-
-
- FILE *fp[2];
-
- int num_args,
- nind,
- mind,
- c,
- delivery_error = 0;
- time_t start_time,
- end_time,
- driver_start_time,
- driver_end_time;
-
- /* ---------------------------- */
- set_logfile(LOGFILE);
- set_loglevel(LOGLEVEL);
- set_consolelog(TRUE);
- /* ---------------------------- */
- while ((c = getopt (argc, argv, "vql:m:d")) != -1)
- {
- switch (c)
- {
- case 'q':
- set_consolelog(FALSE);
- break;
- case 'd':
- display_drivers();
- exit(0);
- case 'v':
- lprintf(LOG_STANDARD, "sms_client %sn", VERSION);
- exit(0);
- case 'l':
- set_loglevel((int)strtol(optarg, &ptr, 10));
- if (ptr == optarg)
- {
- lprintf(LOG_ERROR, "Option l requires an argumentn");
- usage(argv[0]);
- exit(EUSAGE);
- }
-
- break;
- case '?':
- #if !defined(NEXT)
- lprintf(LOG_ERROR, "Unknown option `-%c'n", optopt);
- #endif
- usage(argv[0]);
- exit(EUSAGE);
- default:
- abort ();
- }
- }
-
- /* ---------------------------- */
- num_args = argc - optind;
- if (num_args < 1)
- {
- usage(argv[0]);
- exit(EUSAGE);
- }
- nind = optind;
- mind = optind +1;
- /* ---------------------------- */
- if (read_resource_file(CONFIG_FILE, resource_list, TRUE) != RESOURCE_FILE_OK)
- { lprintf(LOG_ERROR, "Unrecoverable Failure Parsing file %sn", CONFIG_FILE);
- exit(1);
- }
- /* ---------------------------- */
- /* Get and expand NAMES|NUMBERS */
-
- SMS_dual_openrc(fp);
- numbers = SMS_expandnumber(fp, "<NULL>", argv[nind], SMS_default_service);
- SMS_dual_closerc(fp);
- /* ---------------------------- */
- /* Check NAMES|NUMBERS */
- if (validate_expanded_numbers(numbers))
- {
- lprintf(LOG_ERROR, "Expanding namesn");
- exit(ENAMEEXPANSION);
- }
- /* ---------------------------- */
- if (num_args >= 2)
- {
- for (i=mind; i<argc; i++)
- {
- if (sms_strlen(argv[i]) > MAXMESSAGELEN)
- {
- lprintf(LOG_ERROR, "Message %d too longn", (i - mind +1));
- exit(EMESSAGETOOLONG);
- }
- sms_strcpy(message[i - mind], argv[i]);
- }
- num_messages = argc - mind;
- }
- else
- { get_message(message[0]);
- num_messages = 1;
- }
- /* ------------------------------------------------------------ */
- /* If sms_lock_action is set to BLOCK any other instances of */
- /* sms_client which are running will cause this one to block. */
- /* When the other instances have finished executing and the */
- /* lockfile has been released this instance can attemp to */
- /* get the lockfile and run */
- /* ------------------------------------------------------------ */
- SMS_obtain_lock(SMS_lockfile);
- /* ------------------------------------------------------------ */
- time(&start_time);
- parent_list = gather(numbers);
- parent_node = get_first_parent(parent_list);
- while (parent_node != NULL)
- {
- list = get_child(parent_node);
- mservice = get_service(list);
- protocol = get_protocol(mservice);
- device = get_device(protocol);
- if (device == NULL)
- { lprintf(LOG_ERROR, "Driver for service %s NOT foundn", mservice);
- }
- else
- { for (i=0; i<num_messages; i++)
- {
- time(&driver_start_time);
- (*device->init)(mservice, device);
- (*device->main)(list, message[i], device->env);
- time(&driver_end_time);
- lprintf(LOG_STANDARD, "%s Service Time: %d Secondsn", mservice, (int)(driver_end_time - driver_start_time));
- delivered = list;
- while (delivered != NULL)
- {
- if (get_delivery(delivered))
- {
- lprintf(LOG_WARNING, "Could not deliver message %d to %s on %s delivery code %dn",
- i,
- get_name(delivered),
- get_number(delivered),
- get_delivery(delivered));
- delivery_error = EDELIVERY;
- }
-
- lprintf(LOG_STANDARD, "[%03d] %s:%s "%s"n",
- get_delivery(delivered),
- get_service(delivered),
- get_number(delivered),
- message[i]);
- delivered = get_next(delivered);
- }
- }
- }
-
- parent_node = get_next_parent(parent_node);
- }
- time(&end_time);
- lprintf(LOG_STANDARD, "Total Elapsed Time: %d Secondsn", (int)(end_time - start_time));
- free_list(numbers);
- SMS_release_lock();
- return delivery_error;
- }