major Makefile and build process improvements, including removal of all hardcoded...
[asterisk/asterisk.git] / pbx / pbx_spool.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Full-featured outgoing call spool support
5  * 
6  * Copyright (C) 2002 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include "asterisk/lock.h"
15 #include "asterisk/file.h"
16 #include "asterisk/logger.h"
17 #include "asterisk/channel.h"
18 #include "asterisk/callerid.h"
19 #include "asterisk/pbx.h"
20 #include "asterisk/module.h"
21 #include "asterisk/options.h"
22 #include "asterisk/utils.h"
23 #include <sys/stat.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <utime.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <dirent.h>
30 #include <string.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include "asterisk.h"
35
36 /*
37  * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
38  * The spool file contains a header 
39  */
40
41 static char *tdesc = "Outgoing Spool Support";
42 static char qdir[255];
43
44 struct outgoing {
45         char fn[256];
46         /* Current number of retries */
47         int retries;
48         /* Maximum number of retries permitted */
49         int maxretries;
50         /* How long to wait between retries (in seconds) */
51         int retrytime;
52         /* How long to wait for an answer */
53         int waittime;
54         /* PID which is currently calling */
55         int callingpid;
56         
57         /* What to connect to outgoing */
58         char tech[256];
59         char dest[256];
60         
61         /* If application */
62         char app[256];
63         char data[256];
64
65         /* If extension/context/priority */
66         char exten[256];
67         char context[256];
68         int priority;
69
70         /* CallerID Information */
71         char cid_num[256];
72         char cid_name[256];
73
74         /* Channel variables */
75         char variable[10*256];
76         /* Account code */
77         char account[256];
78         
79         /* Maximum length of call */
80         int maxlen;
81         
82 };
83
84 static void init_outgoing(struct outgoing *o)
85 {
86         memset(o, 0, sizeof(struct outgoing));
87         o->priority = 1;
88         o->retrytime = 300;
89         o->waittime = 45;
90 }
91
92 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
93 {
94         char buf[256];
95         char *c, *c2;
96         int lineno = 0;
97         while(fgets(buf, sizeof(buf), f)) {
98                 lineno++;
99                         /* Trim comments */
100                         c = buf;
101                         while ((c = strchr(c, '#'))) {
102                                 if ((c == buf) || (*(c-1) == ' ') || (*(c-1) == '\t'))
103                                         *c = '\0';
104                                 else
105                                         c++;
106                         }
107                         c = strchr(buf, ';');
108                         if (c)
109                                  *c = '\0';
110
111                         /* Trim trailing white space */
112                         while(!ast_strlen_zero(buf) && buf[strlen(buf) - 1] < 33)
113                                 buf[strlen(buf) - 1] = '\0';
114                         if (!ast_strlen_zero(buf)) {
115                                 c = strchr(buf, ':');
116                                 if (c) {
117                                         *c = '\0';
118                                         c++;
119                                         while ((*c) && (*c < 33))
120                                                 c++;
121 #if 0
122                                         printf("'%s' is '%s' at line %d\n", buf, c, lineno);
123 #endif                                  
124                                         if (!strcasecmp(buf, "channel")) {
125                                                 strncpy(o->tech, c, sizeof(o->tech) - 1);
126                                                 if ((c2 = strchr(o->tech, '/'))) {
127                                                         *c2 = '\0';
128                                                         c2++;
129                                                         strncpy(o->dest, c2, sizeof(o->dest) - 1);
130                                                 } else {
131                                                         ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
132                                                         o->tech[0] = '\0';
133                                                 }
134                                         } else if (!strcasecmp(buf, "callerid")) {
135                                                 ast_callerid_split(c, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
136                                         } else if (!strcasecmp(buf, "application")) {
137                                                 strncpy(o->app, c, sizeof(o->app) - 1);
138                                         } else if (!strcasecmp(buf, "data")) {
139                                                 strncpy(o->data, c, sizeof(o->data) - 1);
140                                         } else if (!strcasecmp(buf, "maxretries")) {
141                                                 if (sscanf(c, "%d", &o->maxretries) != 1) {
142                                                         ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
143                                                         o->maxretries = 0;
144                                                 }
145                                         } else if (!strcasecmp(buf, "context")) {
146                                                 strncpy(o->context, c, sizeof(o->context) - 1);
147                                         } else if (!strcasecmp(buf, "extension")) {
148                                                 strncpy(o->exten, c, sizeof(o->exten) - 1);
149                                         } else if (!strcasecmp(buf, "priority")) {
150                                                 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
151                                                         ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
152                                                         o->priority = 1;
153                                                 }
154                                         } else if (!strcasecmp(buf, "retrytime")) {
155                                                 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
156                                                         ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
157                                                         o->retrytime = 300;
158                                                 }
159                                         } else if (!strcasecmp(buf, "waittime")) {
160                                                 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
161                                                         ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
162                                                         o->waittime = 45;
163                                                 }
164                                         } else if (!strcasecmp(buf, "retry")) {
165                                                 o->retries++;
166                                         } else if (!strcasecmp(buf, "startretry")) {
167                                                 if (sscanf(c, "%d", &o->callingpid) != 1) {
168                                                         ast_log(LOG_WARNING, "Unable to retrieve calling PID!\n");
169                                                         o->callingpid = 0;
170                                                 }
171                                         } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
172                                                 o->callingpid = 0;
173                                                 o->retries++;
174                                         } else if (!strcasecmp(buf, "delayedretry")) {
175                                         } else if (!strcasecmp(buf, "setvar")) { /* JDG variable support */
176                                                 strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
177                                                 strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
178  
179                                         } else if (!strcasecmp(buf, "account")) {
180                                                 strncpy(o->account, c, sizeof(o->account) - 1);
181                                         } else {
182                                                 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
183                                         }
184                                 } else
185                                         ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
186                         }
187         }
188         strncpy(o->fn, fn, sizeof(o->fn) - 1);
189         if (ast_strlen_zero(o->tech) || ast_strlen_zero(o->dest) || (ast_strlen_zero(o->app) && ast_strlen_zero(o->exten))) {
190                 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
191                 return -1;
192         }
193         return 0;
194 }
195
196 static void safe_append(struct outgoing *o, time_t now, char *s)
197 {
198         int fd;
199         FILE *f;
200         struct utimbuf tbuf;
201         fd = open(o->fn, O_WRONLY|O_APPEND);
202         if (fd > -1) {
203                 f = fdopen(fd, "a");
204                 if (f) {
205                         fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
206                         fclose(f);
207                 } else
208                         close(fd);
209                 /* Update the file time */
210                 tbuf.actime = now;
211                 tbuf.modtime = now + o->retrytime;
212                 if (utime(o->fn, &tbuf))
213                         ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
214         }
215 }
216
217 static void *attempt_thread(void *data)
218 {
219         struct outgoing *o = data;
220         int res, reason;
221         if (!ast_strlen_zero(o->app)) {
222                 if (option_verbose > 2)
223                         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);
224                 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->variable, o->account, NULL);
225         } else {
226                 if (option_verbose > 2)
227                         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);
228                 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->variable, o->account, NULL);
229         }
230         if (res) {
231                 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
232                 if (o->retries >= o->maxretries + 1) {
233                         /* Max retries exceeded */
234                         ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
235                         unlink(o->fn);
236                 } else {
237                         /* Notate that the call is still active */
238                         safe_append(o, time(NULL), "EndRetry");
239                 }
240         } else {
241                 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
242                 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
243                 unlink(o->fn);
244         }
245         free(o);
246         return NULL;
247 }
248
249 static void launch_service(struct outgoing *o)
250 {
251         pthread_t t;
252         pthread_attr_t attr;
253         pthread_attr_init(&attr);
254         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
255         if (ast_pthread_create(&t,&attr,attempt_thread, o) == -1) {
256                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
257                 free(o);
258         }
259 }
260
261 static int scan_service(char *fn, time_t now, time_t atime)
262 {
263         struct outgoing *o;
264         FILE *f;
265         o = malloc(sizeof(struct outgoing));
266         if (o) {
267                 init_outgoing(o);
268                 f = fopen(fn, "r+");
269                 if (f) {
270                         if (!apply_outgoing(o, fn, f)) {
271 #if 0
272                                 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
273 #endif
274                                 fclose(f);
275                                 if (o->retries <= o->maxretries) {
276                                         if (o->callingpid && (o->callingpid == ast_mainpid)) {
277                                                 safe_append(o, time(NULL), "DelayedRetry");
278                                                 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
279                                         } else {
280                                                 /* Increment retries */
281                                                 o->retries++;
282                                                 /* If someone else was calling, they're presumably gone now
283                                                    so abort their retry and continue as we were... */
284                                                 if (o->callingpid)
285                                                         safe_append(o, time(NULL), "AbortRetry");
286
287                                                 safe_append(o, now, "StartRetry");
288                                                 launch_service(o);
289                                         }
290                                         now += o->retrytime;
291                                         return now;
292                                 } else {
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);
294                                         free(o);
295                                         unlink(fn);
296                                         return 0;
297                                 }
298                         } else {
299                                 free(o);
300                                 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
301                                 fclose(f);
302                                 unlink(fn);
303                         }
304                 } else {
305                         free(o);
306                         ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
307                         unlink(fn);
308                 }
309         } else
310                 ast_log(LOG_WARNING, "Out of memory :(\n");
311         return -1;
312 }
313
314 static void *scan_thread(void *unused)
315 {
316         struct stat st;
317         DIR *dir;
318         struct dirent *de;
319         char fn[256];
320         int res;
321         time_t last = 0, next = 0, now;
322         for(;;) {
323                 /* Wait a sec */
324                 sleep(1);
325                 time(&now);
326                 if (!stat(qdir, &st)) {
327                         if ((st.st_mtime != last) || (next && (now > next))) {
328 #if 0
329                                 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
330                                 printf("Ooh, something changed / timeout\n");
331 #endif                          
332                                 next = 0;
333                                 last = st.st_mtime;
334                                 dir = opendir(qdir);
335                                 if (dir) {
336                                         while((de = readdir(dir))) {
337                                                 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
338                                                 if (!stat(fn, &st)) {
339                                                         if (S_ISREG(st.st_mode)) {
340                                                                 if (st.st_mtime <= now) {
341                                                                         res = scan_service(fn, now, st.st_atime);
342                                                                         if (res > 0) {
343                                                                                 /* Update next service time */
344                                                                                 if (!next || (res < next)) {
345                                                                                         next = res;
346                                                                                 }
347                                                                         } else if (res)
348                                                                                 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
349                                                                 } else {
350                                                                         /* Update "next" update if necessary */
351                                                                         if (!next || (st.st_mtime < next))
352                                                                                 next = st.st_mtime;
353                                                                 }
354                                                         }
355                                                 } else
356                                                         ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
357                                         }
358                                         closedir(dir);
359                                 } else
360                                         ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
361                         }
362                 } else
363                         ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
364         }
365         return NULL;
366 }
367
368 int unload_module(void)
369 {
370         return -1;
371 }
372
373 int load_module(void)
374 {
375         pthread_t thread;
376         pthread_attr_t attr;
377         snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing");
378         if (mkdir(qdir, 0700) && (errno != EEXIST)) {
379                 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
380                 return 0;
381         }
382         pthread_attr_init(&attr);
383         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
384         if (ast_pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
385                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
386                 return -1;
387         }
388         return 0;
389 }
390
391 char *description(void)
392 {
393         return tdesc;
394 }
395
396 int usecount(void)
397 {
398         return 1;
399 }
400
401 char *key()
402 {
403         return ASTERISK_GPL_KEY;
404 }