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, "setvar")) { /* JDG variable support */
171 strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
172 strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
174 } else if (!strcasecmp(buf, "account")) {
175 strncpy(o->account, c, sizeof(o->account) - 1);
177 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
180 ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
184 strncpy(o->fn, fn, sizeof(o->fn) - 1);
185 /* Check sanity of times */
186 if (o->retrytime < o->waittime + 5)
187 o->retrytime = o->waittime + 5;
188 if (!strlen(o->tech) || !strlen(o->dest) || (!strlen(o->app) && !strlen(o->exten))) {
189 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
195 static void safe_append(struct outgoing *o, time_t now, char *s)
199 fd = open(o->fn, O_WRONLY|O_APPEND);
203 fprintf(f, "%s: %d (%ld) (%ld)\n", s, o->retries, (long)ast_mainpid, (long) now);
210 static void *attempt_thread(void *data)
212 struct outgoing *o = data;
214 if (strlen(o->app)) {
215 if (option_verbose > 2)
216 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);
217 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);
219 if (option_verbose > 2)
220 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);
221 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);
224 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
225 if (o->retries >= o->maxretries + 1) {
226 /* Max retries exceeded */
227 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
230 /* Notate that the call is still active */
231 safe_append(o, time(NULL), "EndRetry");
234 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
235 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
242 static void launch_service(struct outgoing *o)
246 pthread_attr_init(&attr);
247 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
248 if (pthread_create(&t,&attr,attempt_thread, o) == -1) {
249 ast_log(LOG_WARNING, "Unable to create thread :(\n");
254 static int scan_service(char *fn, time_t now, time_t atime)
259 o = malloc(sizeof(struct outgoing));
264 if (!apply_outgoing(o, fn, f)) {
266 printf("Retries: %d, max: %d\n", o->retries, o->maxretries);
268 if (o->retries <= o->maxretries) {
269 if (o->callingpid && (o->callingpid == ast_mainpid)) {
270 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
272 /* Increment retries */
274 /* If someone else was calling, they're presumably gone now
275 so abort their retry and continue as we were... */
277 safe_append(o, time(NULL), "AbortRetry");
279 /* Add a retry line at the end */
280 fseek(f, 0L, SEEK_END);
281 fprintf(f, "StartRetry: %d %d (%ld)\n", ast_mainpid, o->retries, (long) now);
285 /* Update the file time */
287 tbuf.modtime = now + o->retrytime;
288 if (utime(o->fn, &tbuf))
289 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", fn, strerror(errno));
293 ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
301 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
307 ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
311 ast_log(LOG_WARNING, "Out of memory :(\n");
315 static void *scan_thread(void *unused)
322 time_t last = 0, next = 0, now;
327 if (!stat(qdir, &st)) {
328 if ((st.st_mtime != last) || (next && (now > next))) {
330 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
331 printf("Ooh, something changed / timeout\n");
337 while((de = readdir(dir))) {
338 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
339 if (!stat(fn, &st)) {
340 if (S_ISREG(st.st_mode)) {
341 if (st.st_mtime <= now) {
342 res = scan_service(fn, now, st.st_atime);
344 /* Update next service time */
345 if (!next || (res < next)) {
349 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
351 /* Update "next" update if necessary */
352 if (!next || (st.st_mtime < next))
357 ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
361 ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
364 ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
369 int unload_module(void)
374 int load_module(void)
378 snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
379 if (mkdir(qdir, 0700) && (errno != EEXIST)) {
380 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
383 pthread_attr_init(&attr);
384 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
385 if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
386 ast_log(LOG_WARNING, "Unable to create thread :(\n");
392 char *description(void)
404 return ASTERISK_GPL_KEY;