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$")
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/callerid.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/options.h"
49 #include "asterisk/utils.h"
52 * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
53 * The spool file contains a header
57 /*! Always delete the call file after a call succeeds or the
58 * maximum number of retries is exceeded, even if the
59 * modification time of the call file is in the future.
61 SPOOL_FLAG_ALWAYS_DELETE = (1 << 0),
62 /* Don't unlink the call file after processing, move in qdonedir */
63 SPOOL_FLAG_ARCHIVE = (1 << 1)
66 static char qdir[255];
67 static char qdonedir[255];
71 /*! Current number of retries */
73 /*! Maximum number of retries permitted */
75 /*! How long to wait between retries (in seconds) */
77 /*! How long to wait for an answer */
79 /*! PID which is currently calling */
82 /*! What to connect to outgoing */
90 /* If extension/context/priority */
91 char exten[AST_MAX_EXTENSION];
92 char context[AST_MAX_CONTEXT];
95 /* CallerID Information */
100 char account[AST_MAX_ACCOUNT_CODE];
102 /*! Variables and Functions */
103 struct ast_variable *vars;
105 /*! Maximum length of call */
109 struct ast_flags options;
112 static void init_outgoing(struct outgoing *o)
117 ast_set_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE);
120 static void free_outgoing(struct outgoing *o)
125 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
130 struct ast_variable *var;
132 while(fgets(buf, sizeof(buf), f)) {
136 while ((c = strchr(c, '#'))) {
137 if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
144 while ((c = strchr(c, ';'))) {
145 if ((c > buf) && (c[-1] == '\\')) {
146 memmove(c - 1, c, strlen(c) + 1);
154 /* Trim trailing white space */
155 while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33)
156 buf[strlen(buf) - 1] = '\0';
157 if (!ast_strlen_zero(buf)) {
158 c = strchr(buf, ':');
162 while ((*c) && (*c < 33))
165 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
167 if (!strcasecmp(buf, "channel")) {
168 ast_copy_string(o->tech, c, sizeof(o->tech));
169 if ((c2 = strchr(o->tech, '/'))) {
172 ast_copy_string(o->dest, c2, sizeof(o->dest));
174 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
177 } else if (!strcasecmp(buf, "callerid")) {
178 ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
179 } else if (!strcasecmp(buf, "application")) {
180 ast_copy_string(o->app, c, sizeof(o->app));
181 } else if (!strcasecmp(buf, "data")) {
182 ast_copy_string(o->data, c, sizeof(o->data));
183 } else if (!strcasecmp(buf, "maxretries")) {
184 if (sscanf(c, "%d", &o->maxretries) != 1) {
185 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
188 } else if (!strcasecmp(buf, "context")) {
189 ast_copy_string(o->context, c, sizeof(o->context));
190 } else if (!strcasecmp(buf, "extension")) {
191 ast_copy_string(o->exten, c, sizeof(o->exten));
192 } else if (!strcasecmp(buf, "priority")) {
193 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
194 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
197 } else if (!strcasecmp(buf, "retrytime")) {
198 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
199 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
202 } else if (!strcasecmp(buf, "waittime")) {
203 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
204 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
207 } else if (!strcasecmp(buf, "retry")) {
209 } else if (!strcasecmp(buf, "startretry")) {
210 if (sscanf(c, "%ld", &o->callingpid) != 1) {
211 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
214 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
217 } else if (!strcasecmp(buf, "delayedretry")) {
218 } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
222 var = ast_variable_new(c, c2, fn);
228 ast_log(LOG_WARNING, "Malformed \"%s\" argument. Should be \"%s: variable=value\"\n", buf, buf);
229 } else if (!strcasecmp(buf, "account")) {
230 ast_copy_string(o->account, c, sizeof(o->account));
231 } else if (!strcasecmp(buf, "alwaysdelete")) {
232 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ALWAYS_DELETE);
233 } else if (!strcasecmp(buf, "archive")) {
234 ast_set2_flag(&o->options, ast_true(c), SPOOL_FLAG_ARCHIVE);
236 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
239 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
242 ast_copy_string(o->fn, fn, sizeof(o->fn));
243 if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
244 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
250 static void safe_append(struct outgoing *o, time_t now, char *s)
256 if ((fd = open(o->fn, O_WRONLY | O_APPEND)) < 0)
259 if ((f = fdopen(fd, "a"))) {
260 fprintf(f, "\n%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
265 /* Update the file time */
267 tbuf.modtime = now + o->retrytime;
268 if (utime(o->fn, &tbuf))
269 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
273 * \brief Remove a call file from the outgoing queue optionally moving it in the archive dir
275 * \param o the pointer to outgoing struct
276 * \param status the exit status of the call. Can be "Completed", "Failed" or "Expired"
278 static int remove_from_queue(struct outgoing *o, const char *status)
285 if (!ast_test_flag(&o->options, SPOOL_FLAG_ALWAYS_DELETE)) {
286 struct stat current_file_status;
288 if (!stat(o->fn, ¤t_file_status)) {
289 if (time(NULL) < current_file_status.st_mtime)
294 if (!ast_test_flag(&o->options, SPOOL_FLAG_ARCHIVE)) {
299 if (ast_mkdir(qdonedir, 0777)) {
300 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir);
305 if ((fd = open(o->fn, O_WRONLY | O_APPEND))) {
306 if ((f = fdopen(fd, "a"))) {
307 fprintf(f, "Status: %s\n", status);
313 if (!(bname = strrchr(o->fn, '/')))
317 snprintf(newfn, sizeof(newfn), "%s/%s", qdonedir, bname);
318 /* a existing call file the archive dir is overwritten */
320 if (rename(o->fn, newfn) != 0) {
327 static void *attempt_thread(void *data)
329 struct outgoing *o = data;
331 if (!ast_strlen_zero(o->app)) {
332 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);
333 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);
335 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);
336 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);
339 ast_log(LOG_NOTICE, "Call failed to go through, reason (%d) %s\n", reason, ast_channel_reason2str(reason));
340 if (o->retries >= o->maxretries + 1) {
341 /* Max retries exceeded */
342 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" : "");
343 remove_from_queue(o, "Expired");
345 /* Notate that the call is still active */
346 safe_append(o, time(NULL), "EndRetry");
349 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
350 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
351 remove_from_queue(o, "Completed");
357 static void launch_service(struct outgoing *o)
362 if ((ret = ast_pthread_create_detached(&t, NULL, attempt_thread, o))) {
363 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
368 static int scan_service(char *fn, time_t now, time_t atime)
370 struct outgoing *o = NULL;
374 if (!(o = ast_calloc(1, sizeof(*o)))) {
375 ast_log(LOG_WARNING, "Out of memory ;(\n");
381 /* Attempt to open the file */
382 if (!(f = fopen(fn, "r+"))) {
383 remove_from_queue(o, "Failed");
385 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
389 /* Read in and verify the contents */
390 if (apply_outgoing(o, fn, f)) {
391 remove_from_queue(o, "Failed");
393 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
399 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
402 if (o->retries <= o->maxretries) {
404 if (o->callingpid && (o->callingpid == ast_mainpid)) {
405 safe_append(o, time(NULL), "DelayedRetry");
406 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
409 /* Increment retries */
411 /* If someone else was calling, they're presumably gone now
412 so abort their retry and continue as we were... */
414 safe_append(o, time(NULL), "AbortRetry");
416 safe_append(o, now, "StartRetry");
421 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" : "");
422 remove_from_queue(o, "Expired");
429 static void *scan_thread(void *unused)
436 time_t last = 0, next = 0, now;
443 if (stat(qdir, &st)) {
444 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
448 /* Make sure it is time for us to execute our check */
449 if ((st.st_mtime == last) && (next && (next > now)))
453 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
454 printf("Ooh, something changed / timeout\n");
459 if (!(dir = opendir(qdir))) {
460 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
464 while ((de = readdir(dir))) {
465 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
467 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
470 if (!S_ISREG(st.st_mode))
472 if (st.st_mtime <= now) {
473 res = scan_service(fn, now, st.st_atime);
475 /* Update next service time */
476 if (!next || (res < next)) {
480 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
482 /* Update "next" update if necessary */
483 if (!next || (st.st_mtime < next))
492 static int unload_module(void)
497 static int load_module(void)
501 snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
502 if (ast_mkdir(qdir, 0777)) {
503 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
506 snprintf(qdonedir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing_done");
508 if ((ret = ast_pthread_create_detached_background(&thread, NULL, scan_thread, NULL))) {
509 ast_log(LOG_WARNING, "Unable to create thread :( (returned error: %d)\n", ret);
516 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Outgoing Spool Support");