2 * Asterisk -- A telephony toolkit for Linux.
4 * Full-featured outgoing call spool support
6 * Copyright (C) 2002 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/lock.h"
31 #include "asterisk/file.h"
32 #include "asterisk/logger.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/callerid.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/options.h"
38 #include "asterisk/utils.h"
41 * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
42 * The spool file contains a header
45 static char *tdesc = "Outgoing Spool Support";
46 static char qdir[255];
50 /* Current number of retries */
52 /* Maximum number of retries permitted */
54 /* How long to wait between retries (in seconds) */
56 /* How long to wait for an answer */
58 /* PID which is currently calling */
61 /* What to connect to outgoing */
69 /* If extension/context/priority */
74 /* CallerID Information */
78 /* Variables and Functions */
79 struct ast_variable *vars;
81 /* Maximum length of call */
86 static void init_outgoing(struct outgoing *o)
88 memset(o, 0, sizeof(struct outgoing));
94 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
99 struct ast_variable *var;
101 while(fgets(buf, sizeof(buf), f)) {
105 while ((c = strchr(c, '#'))) {
106 if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
111 c = strchr(buf, ';');
115 /* Trim trailing white space */
116 while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33)
117 buf[strlen(buf) - 1] = '\0';
118 if (!ast_strlen_zero(buf)) {
119 c = strchr(buf, ':');
123 while ((*c) && (*c < 33))
126 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
128 if (!strcasecmp(buf, "channel")) {
129 strncpy(o->tech, c, sizeof(o->tech) - 1);
130 if ((c2 = strchr(o->tech, '/'))) {
133 strncpy(o->dest, c2, sizeof(o->dest) - 1);
135 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
138 } else if (!strcasecmp(buf, "callerid")) {
139 ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
140 } else if (!strcasecmp(buf, "application")) {
141 strncpy(o->app, c, sizeof(o->app) - 1);
142 } else if (!strcasecmp(buf, "data")) {
143 strncpy(o->data, c, sizeof(o->data) - 1);
144 } else if (!strcasecmp(buf, "maxretries")) {
145 if (sscanf(c, "%d", &o->maxretries) != 1) {
146 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
149 } else if (!strcasecmp(buf, "context")) {
150 strncpy(o->context, c, sizeof(o->context) - 1);
151 } else if (!strcasecmp(buf, "extension")) {
152 strncpy(o->exten, c, sizeof(o->exten) - 1);
153 } else if (!strcasecmp(buf, "priority")) {
154 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
155 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
158 } else if (!strcasecmp(buf, "retrytime")) {
159 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
160 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
163 } else if (!strcasecmp(buf, "waittime")) {
164 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
165 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
168 } else if (!strcasecmp(buf, "retry")) {
170 } else if (!strcasecmp(buf, "startretry")) {
171 if (sscanf(c, "%d", &o->callingpid) != 1) {
172 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
175 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
178 } else if (!strcasecmp(buf, "delayedretry")) {
179 } else if (!strcasecmp(buf, "setvar") || !strcasecmp(buf, "set")) {
182 var = ast_variable_new(c, c2);
187 } else if (!strcasecmp(buf, "account")) {
188 var = ast_variable_new("CDR(accountcode|r)", c);
194 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
197 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
200 strncpy(o->fn, fn, sizeof(o->fn) - 1);
201 if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
202 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
208 static void safe_append(struct outgoing *o, time_t now, char *s)
213 fd = open(o->fn, O_WRONLY|O_APPEND);
217 fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
221 /* Update the file time */
223 tbuf.modtime = now + o->retrytime;
224 if (utime(o->fn, &tbuf))
225 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
229 static void *attempt_thread(void *data)
231 struct outgoing *o = data;
233 if (!ast_strlen_zero(o->app)) {
234 if (option_verbose > 2)
235 ast_verbose(VERBOSE_PREFIX_3 "Attempting call on %s/%s for application %s(%s) (Retry %d)\n", o->tech, o->dest, o->app, o->data, o->retries);
236 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, NULL);
238 if (option_verbose > 2)
239 ast_verbose(VERBOSE_PREFIX_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);
240 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, NULL);
243 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
244 if (o->retries >= o->maxretries + 1) {
245 /* Max retries exceeded */
246 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" : "");
249 /* Notate that the call is still active */
250 safe_append(o, time(NULL), "EndRetry");
253 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
254 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
261 static void launch_service(struct outgoing *o)
265 pthread_attr_init(&attr);
266 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
267 if (ast_pthread_create(&t,&attr,attempt_thread, o) == -1) {
268 ast_log(LOG_WARNING, "Unable to create thread :(\n");
273 static int scan_service(char *fn, time_t now, time_t atime)
277 o = malloc(sizeof(struct outgoing));
282 if (!apply_outgoing(o, fn, f)) {
284 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
287 if (o->retries <= o->maxretries) {
288 if (o->callingpid && (o->callingpid == ast_mainpid)) {
289 safe_append(o, time(NULL), "DelayedRetry");
290 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
292 /* Increment retries */
294 /* If someone else was calling, they're presumably gone now
295 so abort their retry and continue as we were... */
297 safe_append(o, time(NULL), "AbortRetry");
299 safe_append(o, now, "StartRetry");
305 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" : "");
312 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
318 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
322 ast_log(LOG_WARNING, "Out of memory :(\n");
326 static void *scan_thread(void *unused)
333 time_t last = 0, next = 0, now;
338 if (!stat(qdir, &st)) {
339 if ((st.st_mtime != last) || (next && (now > next))) {
341 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
342 printf("Ooh, something changed / timeout\n");
348 while((de = readdir(dir))) {
349 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
350 if (!stat(fn, &st)) {
351 if (S_ISREG(st.st_mode)) {
352 if (st.st_mtime <= now) {
353 res = scan_service(fn, now, st.st_atime);
355 /* Update next service time */
356 if (!next || (res < next)) {
360 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
362 /* Update "next" update if necessary */
363 if (!next || (st.st_mtime < next))
368 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
372 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
375 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
380 int unload_module(void)
385 int load_module(void)
389 snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
390 if (mkdir(qdir, 0700) && (errno != EEXIST)) {
391 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
394 pthread_attr_init(&attr);
395 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
396 if (ast_pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
397 ast_log(LOG_WARNING, "Unable to create thread :(\n");
403 char *description(void)
415 return ASTERISK_GPL_KEY;