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 */
54 /* What to connect to outgoing */
62 /* If extension/context/priority */
67 /* CallerID Information */
70 /* Channel variables */
71 char variable[10*256];
75 /* Maximum length of call */
80 static void init_outgoing(struct outgoing *o)
82 memset(o, 0, sizeof(struct outgoing));
88 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
94 fgets(buf, sizeof(buf), f);
101 c = strchr(buf, ';');
105 /* Trim trailing white space */
106 while(strlen(buf) && buf[strlen(buf) - 1] < 33)
107 buf[strlen(buf) - 1] = '\0';
109 c = strchr(buf, ':');
116 printf("'%s' is '%s' at line %d\n", buf, c, lineno);
118 if (!strcasecmp(buf, "channel")) {
119 strncpy(o->tech, c, sizeof(o->tech) - 1);
120 if ((c2 = strchr(o->tech, '/'))) {
123 strncpy(o->dest, c2, sizeof(o->dest) - 1);
125 ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
128 } else if (!strcasecmp(buf, "callerid")) {
129 strncpy(o->callerid, c, sizeof(o->callerid) - 1);
130 } else if (!strcasecmp(buf, "application")) {
131 strncpy(o->app, c, sizeof(o->app) - 1);
132 } else if (!strcasecmp(buf, "data")) {
133 strncpy(o->data, c, sizeof(o->data) - 1);
134 } else if (!strcasecmp(buf, "maxretries")) {
135 if (sscanf(c, "%d", &o->maxretries) != 1) {
136 ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
139 } else if (!strcasecmp(buf, "context")) {
140 strncpy(o->context, c, sizeof(o->context) - 1);
141 } else if (!strcasecmp(buf, "extension")) {
142 strncpy(o->exten, c, sizeof(o->exten) - 1);
143 } else if (!strcasecmp(buf, "priority")) {
144 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
145 ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
148 } else if (!strcasecmp(buf, "retrytime")) {
149 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
150 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
153 } else if (!strcasecmp(buf, "waittime")) {
154 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
155 ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
158 } else if (!strcasecmp(buf, "retry")) {
160 } else if (!strcasecmp(buf, "setvar")) { /* JDG variable support */
161 strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
162 strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
164 } else if (!strcasecmp(buf, "account")) {
165 strncpy(o->account, c, sizeof(o->account) - 1);
167 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
170 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
174 strncpy(o->fn, fn, sizeof(o->fn) - 1);
175 /* Check sanity of times */
176 if (o->retrytime < o->waittime + 5)
177 o->retrytime = o->waittime + 5;
178 if (!strlen(o->tech) || !strlen(o->dest) || (!strlen(o->app) && !strlen(o->exten))) {
179 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
185 static void *attempt_thread(void *data)
187 struct outgoing *o = data;
189 if (strlen(o->app)) {
190 if (option_verbose > 2)
191 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);
192 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);
194 if (option_verbose > 2)
195 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);
196 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);
199 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
200 if (o->retries >= o->maxretries + 1) {
201 /* Max retries exceeded */
202 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
206 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
207 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
214 static void launch_service(struct outgoing *o)
218 pthread_attr_init(&attr);
219 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
220 if (pthread_create(&t,&attr,attempt_thread, o) == -1) {
221 ast_log(LOG_WARNING, "Unable to create thread :(\n");
226 static int scan_service(char *fn, time_t now, time_t atime)
231 o = malloc(sizeof(struct outgoing));
236 if (!apply_outgoing(o, fn, f)) {
237 /* Update the file time */
239 tbuf.modtime = now + o->retrytime;
240 if (utime(o->fn, &tbuf))
241 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", fn, strerror(errno));
242 /* Increment retries */
245 printf("Retries: %d, max: %d\n", o->retries, o->maxretries);
247 if (o->retries <= o->maxretries + 1) {
248 /* Add a retry line at the end */
249 fseek(f, 0L, SEEK_END);
250 fprintf(f, "Retry: %d (%ld)\n", o->retries, (long) now);
256 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
264 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
270 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
274 ast_log(LOG_WARNING, "Out of memory :(\n");
278 static void *scan_thread(void *unused)
285 time_t last = 0, next = 0, now;
290 if (!stat(qdir, &st)) {
291 if ((st.st_mtime != last) || (next && (now > next))) {
293 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
294 printf("Ooh, something changed / timeout\n");
300 while((de = readdir(dir))) {
301 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
302 if (!stat(fn, &st)) {
303 if (S_ISREG(st.st_mode)) {
304 if (st.st_mtime <= now) {
305 res = scan_service(fn, now, st.st_atime);
307 /* Update next service time */
308 if (!next || (res < next)) {
312 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
314 /* Update "next" update if necessary */
315 if (!next || (st.st_mtime < next))
320 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
324 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
327 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
332 int unload_module(void)
337 int load_module(void)
341 snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
342 if (mkdir(qdir, 0700) && (errno != EEXIST)) {
343 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
346 pthread_attr_init(&attr);
347 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
348 if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
349 ast_log(LOG_WARNING, "Unable to create thread :(\n");
355 char *description(void)
367 return ASTERISK_GPL_KEY;