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$")
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/options.h"
43 #include "asterisk/utils.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 /*! Current number of retries */
67 /*! Maximum number of retries permitted */
69 /*! How long to wait between retries (in seconds) */
71 /*! How long to wait for an answer */
73 /*! PID which is currently calling */
76 /*! What to connect to outgoing */
84 /* If extension/context/priority */
85 char exten[AST_MAX_EXTENSION];
86 char context[AST_MAX_CONTEXT];
89 /* CallerID Information */
94 char account[AST_MAX_ACCOUNT_CODE];
96 /*! Variables and Functions */
97 struct ast_variable *vars;
99 /*! Maximum length of call */
103 struct ast_flags options;
106 static void init_outgoing(struct outgoing *o)
111 ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
114 static void free_outgoing(struct outgoing *o)
119 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
124 struct ast_variable *var;
126 while(fgets(buf, sizeof(buf), f)) {
130 while ((c = strchr(c, '#'))) {
131 if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
138 while ((c = strchr(c, ';'))) {
139 if ((c > buf) && (c[-1] == '\\')) {
140 memmove(c - 1, c, strlen(c) + 1);
148 /* Trim trailing white space */
149 while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33)
150 buf[strlen(buf) - 1] = '\0';
151 if (!ast_strlen_zero(buf)) {
152 c = strchr(buf, ':');
156 while ((*c) && (*c < 33))
159 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
161 if (!strcasecmp(buf, "channel")) {
162 ast_copy_string(o->tech, c, sizeof(o->tech));
163 if ((c2 = strchr(o->tech, '/'))) {
166 ast_copy_string(o->dest, c2, sizeof(o->dest));
168 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
171 } else if (!strcasecmp(buf, "callerid")) {
172 ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
173 } else if (!strcasecmp(buf, "application")) {
174 ast_copy_string(o->app, c, sizeof(o->app));
175 } else if (!strcasecmp(buf, "data")) {
176 ast_copy_string(o->data, c, sizeof(o->data));
177 } else if (!strcasecmp(buf, "maxretries")) {
178 if (sscanf(c, "%d", &o->maxretries) != 1) {
179 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
182 } else if (!strcasecmp(buf, "context")) {
183 ast_copy_string(o->context, c, sizeof(o->context));
184 } else if (!strcasecmp(buf, "extension")) {
185 ast_copy_string(o->exten, c, sizeof(o->exten));
186 } else if (!strcasecmp(buf, "priority")) {
187 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
188 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
191 } else if (!strcasecmp(buf, "retrytime")) {
192 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
193 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
196 } else if (!strcasecmp(buf, "waittime")) {
197 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
198 ast_log(LOG_WARNING, "Invalid waittime at line %d of %s\n", lineno, fn);
201 } else if (!strcasecmp(buf, "retry")) {
203 } else if (!strcasecmp(buf, "startretry")) {
204 if (sscanf(c, "%ld", &o->callingpid) != 1) {
205 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
208 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
211 } else if (!strcasecmp(buf, "delayedretry")) {
212 } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
216 var = ast_variable_new(c, c2, fn);
222 ast_log(LOG_WARNING, "Malformed \"%s\" argument. Should be \"%s: variable=value\"\n", buf, buf);
223 } else if (!strcasecmp(buf, "account")) {
224 ast_copy_string(o->account, c, sizeof(o->account));
225 } else if (!strcasecmp(buf, "alwaysdelete")) {
226 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ALWAYS_DELETE);
227 } else if (!strcasecmp(buf, "archive")) {
228 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE);
230 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
233 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
236 ast_copy_string(o->fn, fn, sizeof(o->fn));
237 if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
238 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
244 static void safe_append(struct outgoing *o, time_t now, char *s)
250 if ((fd = open(o->fn, O_WRONLY | O_APPEND)) < 0)
253 if ((f = fdopen(fd, "a"))) {
254 fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
259 /* Update the file time */
261 tbuf.modtime = now + o->retrytime;
262 if (utime(o->fn, &tbuf))
263 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
267 * \brief Remove a call file from the outgoing queue optionally moving it in the archive dir
269 * \param o the pointer to outgoing struct
270 * \param status the exit status of the call. Can be "Completed", "Failed" or "Expired"
272 static int remove_from_queue(struct outgoing *o, const char *status)
279 if (!ast_test_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE)) {
280 struct stat current_file_status;
282 if (!stat(o->fn, ¤t_file_status)) {
283 if (time(NULL) < current_file_status.st_mtime)
288 if (!ast_test_flag(&o->options, SPOOL_FLAG_ARCHIVE)) {
293 if (ast_mkdir(qdonedir, 0777)) {
294 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
299 if ((fd = open(o->fn, O_WRONLY | O_APPEND))) {
300 if ((f = fdopen(fd, "a"))) {
301 fprintf(f, "Status: %s\n", status);
307 if (!(bname = strrchr(o->fn, '/')))
311 snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
312 /* a existing call file the archive dir is overwritten */
314 if (rename(o->fn, newfn) != 0) {
321 static void *attempt_thread(void *data)
323 struct outgoing *o = data;
325 if (!ast_strlen_zero(o->app)) {
326 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);
327 res = ast_pbx_outgoing_app(o->tech, AST_FORMAT_SLINEAR, 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);
329 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);
330 res = ast_pbx_outgoing_exten(o->tech, AST_FORMAT_SLINEAR, 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);
333 ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason));
334 if (o->retries >= o->maxretries + 1) {
335 /* Max retries exceeded */
336 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" : "");
337 remove_from_queue(o, "Expired");
339 /* Notate that the call is still active */
340 safe_append(o, time(NULL), "EndRetry");
343 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
344 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
345 remove_from_queue(o, "Completed");
351 static void launch_service(struct outgoing *o)
356 if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
357 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
362 static int scan_service(char *fn, time_t now, time_t atime)
364 struct outgoing *o = NULL;
368 if (!(o = ast_calloc(1, sizeof(*o)))) {
369 ast_log(LOG_WARNING, "Out of memory ;(\n");
375 /* Attempt to open the file */
376 if (!(f = fopen(fn, "r+"))) {
377 remove_from_queue(o, "Failed");
379 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
383 /* Read in and verify the contents */
384 if (apply_outgoing(o, fn, f)) {
385 remove_from_queue(o, "Failed");
387 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
393 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
396 if (o->retries <= o->maxretries) {
398 if (o->callingpid && (o->callingpid == ast_mainpid)) {
399 safe_append(o, time(NULL), "DelayedRetry");
400 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
403 /* Increment retries */
405 /* If someone else was calling, they're presumably gone now
406 so abort their retry and continue as we were... */
408 safe_append(o, time(NULL), "AbortRetry");
410 safe_append(o, now, "StartRetry");
415 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" : "");
416 remove_from_queue(o, "Expired");
423 static void *scan_thread(void *unused)
430 time_t last = 0, next = 0, now;
437 if (stat(qdir, &st)) {
438 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
442 /* Make sure it is time for us to execute our check */
443 if ((st.st_mtime == last) && (next && (next > now)))
447 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
448 printf("Ooh, something changed / timeout\n");
453 if (!(dir = opendir(qdir))) {
454 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
458 while ((de = readdir(dir))) {
459 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
461 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
464 if (!S_ISREG(st.st_mode))
466 if (st.st_mtime <= now) {
467 res = scan_service(fn, now, st.st_atime);
469 /* Update next service time */
470 if (!next || (res < next)) {
474 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
476 /* Update "next" update if necessary */
477 if (!next || (st.st_mtime < next))
486 static int unload_module(void)
491 static int load_module(void)
495 snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
496 if (ast_mkdir(qdir, 0777)) {
497 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
500 snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
502 if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
503 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
510 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Outgoing Spool Support");