2 * Asterisk -- A telephony toolkit for Linux.
4 * Full-featured outgoing call spool support
6 * Copyright (C) 2002, Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
33 #include "../astconf.h"
36 * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
37 * The spool file contains a header
40 static char *tdesc = "Outgoing Spool Support";
41 static char qdir[255];
45 /* Current number of retries */
47 /* Maximum number of retries permitted */
49 /* How long to wait between retries (in seconds) */
51 /* How long to wait for an answer */
53 /* PID which is currently calling */
56 /* What to connect to outgoing */
64 /* If extension/context/priority */
69 /* CallerID Information */
72 /* Channel variables */
73 char variable[10*256];
77 /* Maximum length of call */
82 static void init_outgoing(struct outgoing *o)
84 memset(o, 0, sizeof(struct outgoing));
90 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
96 fgets(buf, sizeof(buf), f);
100 c = strchr(buf, '#');
103 c = strchr(buf, ';');
107 /* Trim trailing white space */
108 while(strlen(buf) && buf[strlen(buf) - 1] < 33)
109 buf[strlen(buf) - 1] = '\0';
111 c = strchr(buf, ':');
118 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
120 if (!strcasecmp(buf, "channel")) {
121 strncpy(o->tech, c, sizeof(o->tech) - 1);
122 if ((c2 = strchr(o->tech, '/'))) {
125 strncpy(o->dest, c2, sizeof(o->dest) - 1);
127 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
130 } else if (!strcasecmp(buf, "callerid")) {
131 strncpy(o->callerid, c, sizeof(o->callerid) - 1);
132 } else if (!strcasecmp(buf, "application")) {
133 strncpy(o->app, c, sizeof(o->app) - 1);
134 } else if (!strcasecmp(buf, "data")) {
135 strncpy(o->data, c, sizeof(o->data) - 1);
136 } else if (!strcasecmp(buf, "maxretries")) {
137 if (sscanf(c, "%d", &o->maxretries) != 1) {
138 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
141 } else if (!strcasecmp(buf, "context")) {
142 strncpy(o->context, c, sizeof(o->context) - 1);
143 } else if (!strcasecmp(buf, "extension")) {
144 strncpy(o->exten, c, sizeof(o->exten) - 1);
145 } else if (!strcasecmp(buf, "priority")) {
146 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
147 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
150 } else if (!strcasecmp(buf, "retrytime")) {
151 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
152 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
155 } else if (!strcasecmp(buf, "waittime")) {
156 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
157 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
160 } else if (!strcasecmp(buf, "retry")) {
162 } else if (!strcasecmp(buf, "startretry")) {
163 if (sscanf(c, "%d", &o->callingpid) != 1) {
164 ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
167 } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
170 } else if (!strcasecmp(buf, "delayedretry")) {
171 } else if (!strcasecmp(buf, "setvar")) { /* JDG variable support */
172 strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
173 strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
175 } else if (!strcasecmp(buf, "account")) {
176 strncpy(o->account, c, sizeof(o->account) - 1);
178 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
181 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
185 strncpy(o->fn, fn, sizeof(o->fn) - 1);
186 if (!strlen(o->tech) || !strlen(o->dest) || (!strlen(o->app) && !strlen(o->exten))) {
187 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
193 static void safe_append(struct outgoing *o, time_t now, char *s)
198 fd = open(o->fn, O_WRONLY|O_APPEND);
202 fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
207 /* Update the file time */
209 tbuf.modtime = now + o->retrytime;
210 if (utime(o->fn, &tbuf))
211 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
214 static void *attempt_thread(void *data)
216 struct outgoing *o = data;
218 if (strlen(o->app)) {
219 if (option_verbose > 2)
220 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);
221 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->callerid, o->variable, o->account);
223 if (option_verbose > 2)
224 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);
225 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->callerid, o->variable, o->account);
228 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
229 if (o->retries >= o->maxretries + 1) {
230 /* Max retries exceeded */
231 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
234 /* Notate that the call is still active */
235 safe_append(o, time(NULL), "EndRetry");
238 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
239 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
246 static void launch_service(struct outgoing *o)
250 pthread_attr_init(&attr);
251 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
252 if (pthread_create(&t,&attr,attempt_thread, o) == -1) {
253 ast_log(LOG_WARNING, "Unable to create thread :(\n");
258 static int scan_service(char *fn, time_t now, time_t atime)
263 o = malloc(sizeof(struct outgoing));
268 if (!apply_outgoing(o, fn, f)) {
270 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
273 if (o->retries <= o->maxretries) {
274 if (o->callingpid && (o->callingpid == ast_mainpid)) {
275 safe_append(o, time(NULL), "DelayedRetry");
276 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
278 /* Increment retries */
280 /* If someone else was calling, they're presumably gone now
281 so abort their retry and continue as we were... */
283 safe_append(o, time(NULL), "AbortRetry");
285 safe_append(o, now, "StartRetry");
291 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
298 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
304 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
308 ast_log(LOG_WARNING, "Out of memory :(\n");
312 static void *scan_thread(void *unused)
319 time_t last = 0, next = 0, now;
324 if (!stat(qdir, &st)) {
325 if ((st.st_mtime != last) || (next && (now > next))) {
327 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
328 printf("Ooh, something changed / timeout\n");
334 while((de = readdir(dir))) {
335 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
336 if (!stat(fn, &st)) {
337 if (S_ISREG(st.st_mode)) {
338 if (st.st_mtime <= now) {
339 res = scan_service(fn, now, st.st_atime);
341 /* Update next service time */
342 if (!next || (res < next)) {
346 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
348 /* Update "next" update if necessary */
349 if (!next || (st.st_mtime < next))
354 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
358 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
361 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
366 int unload_module(void)
371 int load_module(void)
375 snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
376 if (mkdir(qdir, 0700) && (errno != EEXIST)) {
377 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
380 pthread_attr_init(&attr);
381 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
382 if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
383 ast_log(LOG_WARNING, "Unable to create thread :(\n");
389 char *description(void)
401 return ASTERISK_GPL_KEY;