2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Full-featured outgoing call spool support
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
35 #include "asterisk/lock.h"
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/callerid.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/options.h"
46 * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
47 * The spool file contains a header
51 /*! Always delete the call file after a call succeeds or the
52 * maximum number of retries is exceeded, even if the
53 * modification time of the call file is in the future.
55 SPOOL_FLAG_ALWAYS_DELETE = (1 << 0),
56 /* Don't unlink the call file after processing, move in qdonedir */
57 SPOOL_FLAG_ARCHIVE = (1 << 1)
60 static char qdir[255];
61 static char qdonedir[255];
65 int retries; /*!< Current number of retries */
66 int maxretries; /*!< Maximum number of retries permitted */
67 int retrytime; /*!< How long to wait between retries (in seconds) */
68 int waittime; /*!< How long to wait for an answer */
69 long callingpid; /*!< PID which is currently calling */
70 int format; /*!< Formats (codecs) for this call */
72 char tech[256]; /*!< Which channel driver to use for outgoing call */
73 char dest[256]; /*!< Which device/line to use for outgoing call */
75 char app[256]; /*!< If application: Application name */
76 char data[256]; /*!< If applicatoin: Application data */
78 char exten[AST_MAX_EXTENSION]; /*!< If extension/context/priority: Extension in dialplan */
79 char context[AST_MAX_CONTEXT]; /*!< If extension/context/priority: Dialplan context */
80 int priority; /*!< If extension/context/priority: Dialplan priority */
82 char cid_num[256]; /*!< CallerID Information: Number/extension */
83 char cid_name[256]; /*!< CallerID Information: Name */
85 char account[AST_MAX_ACCOUNT_CODE]; /*!< account code */
87 struct ast_variable *vars; /*!< Variables and Functions */
89 int maxlen; /*!< Maximum length of call */
91 struct ast_flags options; /*!< options */
94 static void init_outgoing(struct outgoing *o)
99 o->format = AST_FORMAT_SLINEAR;
100 ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
103 static void free_outgoing(struct outgoing *o)
106 ast_variables_destroy(o->vars);
111 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
116 struct ast_variable *var, *last = o->vars;
118 while (last && last->next) {
122 while(fgets(buf, sizeof(buf), f)) {
126 while ((c = strchr(c, '#'))) {
127 if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
134 while ((c = strchr(c, ';'))) {
135 if ((c > buf) && (c[-1] == '\\')) {
136 memmove(c - 1, c, strlen(c) + 1);
144 /* Trim trailing white space */
145 while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33)
146 buf[strlen(buf) - 1] = '\0';
147 if (!ast_strlen_zero(buf)) {
148 c = strchr(buf, ':');
152 while ((*c) && (*c < 33))
155 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
157 if (!strcasecmp(buf, "channel")) {
158 ast_copy_string(o->tech, c, sizeof(o->tech));
159 if ((c2 = strchr(o->tech, '/'))) {
162 ast_copy_string(o->dest, c2, sizeof(o->dest));
164 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
167 } else if (!strcasecmp(buf, "callerid")) {
168 ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
169 } else if (!strcasecmp(buf, "application")) {
170 ast_copy_string(o->app, c, sizeof(o->app));
171 } else if (!strcasecmp(buf, "data")) {
172 ast_copy_string(o->data, c, sizeof(o->data));
173 } else if (!strcasecmp(buf, "maxretries")) {
174 if (sscanf(c, "%d", &o->maxretries) != 1) {
175 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
178 } else if (!strcasecmp(buf, "codecs")) {
179 ast_parse_allow_disallow(NULL, &o->format, c, 1);
180 } else if (!strcasecmp(buf, "context")) {
181 ast_copy_string(o->context, c, sizeof(o->context));
182 } else if (!strcasecmp(buf, "extension")) {
183 ast_copy_string(o->exten, c, sizeof(o->exten));
184 } else if (!strcasecmp(buf, "priority")) {
185 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
186 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
189 } else if (!strcasecmp(buf, "retrytime")) {
190 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
191 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
194 } else if (!strcasecmp(buf, "waittime")) {
195 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
196 ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, fn);
199 } else if (!strcasecmp(buf, "retry")) {
201 } else if (!strcasecmp(buf, "startretry")) {
202 if (sscanf(c, "%ld", &o->callingpid) != 1) {
203 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
206 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
209 } else if (!strcasecmp(buf, "delayedretry")) {
210 } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
214 var = ast_variable_new(c, c2, fn);
216 /* Always insert at the end, because some people want to treat the spool file as a script */
225 ast_log(LOG_WARNING, "Malformed \"%s\" argument. Should be \"%s: variable=value\"\n", buf, buf);
226 } else if (!strcasecmp(buf, "account")) {
227 ast_copy_string(o->account, c, sizeof(o->account));
228 } else if (!strcasecmp(buf, "alwaysdelete")) {
229 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ALWAYS_DELETE);
230 } else if (!strcasecmp(buf, "archive")) {
231 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE);
233 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
236 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
239 ast_copy_string(o->fn, fn, sizeof(o->fn));
240 if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
241 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
247 static void safe_append(struct outgoing *o, time_t now, char *s)
253 if ((fd = open(o->fn, O_WRONLY | O_APPEND)) < 0)
256 if ((f = fdopen(fd, "a"))) {
257 fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
262 /* Update the file time */
264 tbuf.modtime = now + o->retrytime;
265 if (utime(o->fn, &tbuf))
266 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
270 * \brief Remove a call file from the outgoing queue optionally moving it in the archive dir
272 * \param o the pointer to outgoing struct
273 * \param status the exit status of the call. Can be "Completed", "Failed" or "Expired"
275 static int remove_from_queue(struct outgoing *o, const char *status)
282 if (!ast_test_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE)) {
283 struct stat current_file_status;
285 if (!stat(o->fn, ¤t_file_status)) {
286 if (time(NULL) < current_file_status.st_mtime)
291 if (!ast_test_flag(&o->options, SPOOL_FLAG_ARCHIVE)) {
296 if (ast_mkdir(qdonedir, 0777)) {
297 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
302 if ((fd = open(o->fn, O_WRONLY | O_APPEND))) {
303 if ((f = fdopen(fd, "a"))) {
304 fprintf(f, "Status: %s\n", status);
310 if (!(bname = strrchr(o->fn, '/')))
314 snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
315 /* a existing call file the archive dir is overwritten */
317 if (rename(o->fn, newfn) != 0) {
324 static void *attempt_thread(void *data)
326 struct outgoing *o = data;
328 if (!ast_strlen_zero(o->app)) {
329 ast_verb(3, "Attempting call on %s/%s for application %s(%s) (Retry %d)\n", o->tech, o->dest, o->app, o->data, o->retries);
330 res = ast_pbx_outgoing_app(o->tech, o->format, o->dest, o->waittime * 1000, o->app, o->data, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL);
333 ast_verb(3, "Attempting call on %s/%s for %s@%s:%d (Retry %d)\n", o->tech, o->dest, o->exten, o->context,o->priority, o->retries);
334 res = ast_pbx_outgoing_exten(o->tech, o->format, o->dest, o->waittime * 1000, o->context, o->exten, o->priority, &reason, 2 /* wait to finish */, o->cid_num, o->cid_name, o->vars, o->account, NULL);
338 ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason));
339 if (o->retries >= o->maxretries + 1) {
340 /* Max retries exceeded */
341 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt%s\n", o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
342 remove_from_queue(o, "Expired");
344 /* Notate that the call is still active */
345 safe_append(o, time(NULL), "EndRetry");
348 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
349 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
350 remove_from_queue(o, "Completed");
356 static void launch_service(struct outgoing *o)
361 if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
362 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
367 static int scan_service(char *fn, time_t now, time_t atime)
369 struct outgoing *o = NULL;
373 if (!(o = ast_calloc(1, sizeof(*o)))) {
374 ast_log(LOG_WARNING, "Out of memory ;(\n");
380 /* Attempt to open the file */
381 if (!(f = fopen(fn, "r+"))) {
382 remove_from_queue(o, "Failed");
384 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
388 /* Read in and verify the contents */
389 if (apply_outgoing(o, fn, f)) {
390 remove_from_queue(o, "Failed");
392 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
398 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
401 if (o->retries <= o->maxretries) {
403 if (o->callingpid && (o->callingpid == ast_mainpid)) {
404 safe_append(o, time(NULL), "DelayedRetry");
405 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
408 /* Increment retries */
410 /* If someone else was calling, they're presumably gone now
411 so abort their retry and continue as we were... */
413 safe_append(o, time(NULL), "AbortRetry");
415 safe_append(o, now, "StartRetry");
420 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt%s\n", o->tech, o->dest, o->retries - 1, ((o->retries - 1) != 1) ? "s" : "");
421 remove_from_queue(o, "Expired");
428 static void *scan_thread(void *unused)
435 time_t last = 0, next = 0, now;
436 struct timespec ts = { .tv_sec = 1 };
438 while (!ast_fully_booted) {
439 nanosleep(&ts, NULL);
444 nanosleep(&ts, NULL);
447 if (stat(qdir, &st)) {
448 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
452 /* Make sure it is time for us to execute our check */
453 if ((st.st_mtime == last) && (next && (next > now)))
457 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
458 printf("Ooh, something changed / timeout\n");
463 if (!(dir = opendir(qdir))) {
464 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
468 while ((de = readdir(dir))) {
469 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
471 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
474 if (!S_ISREG(st.st_mode))
476 if (st.st_mtime <= now) {
477 res = scan_service(fn, now, st.st_atime);
479 /* Update next service time */
480 if (!next || (res < next)) {
484 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
486 /* Expired entry: must recheck on the next go-around */
490 /* Update "next" update if necessary */
491 if (!next || (st.st_mtime < next))
500 static int unload_module(void)
505 static int load_module(void)
509 snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
510 if (ast_mkdir(qdir, 0777)) {
511 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
512 return AST_MODULE_LOAD_DECLINE;
514 snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
516 if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
517 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
518 return AST_MODULE_LOAD_FAILURE;
521 return AST_MODULE_LOAD_SUCCESS;
524 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Outgoing Spool Support");