d5e2b960ffb29c61716f4451e26fb8767c794c58
[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, Digium
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/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
21 #include <pthread.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <utime.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <string.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include "../astconf.h"
34
35 /*
36  * pbx_spool is similar in spirit to qcall, but with substantially enhanced functionality...
37  * The spool file contains a header 
38  */
39
40 static char *tdesc = "Outgoing Spool Support";
41 static char qdir[255];
42
43 struct outgoing {
44         char fn[256];
45         /* Current number of retries */
46         int retries;
47         /* Maximum number of retries permitted */
48         int maxretries;
49         /* How long to wait between retries (in seconds) */
50         int retrytime;
51         /* How long to wait for an answer */
52         int waittime;
53         /* PID which is currently calling */
54         int callingpid;
55         
56         /* What to connect to outgoing */
57         char tech[256];
58         char dest[256];
59         
60         /* If application */
61         char app[256];
62         char data[256];
63
64         /* If extension/context/priority */
65         char exten[256];
66         char context[256];
67         int priority;
68
69         /* CallerID Information */
70         char callerid[256];
71
72         /* Channel variables */
73         char variable[10*256];
74         /* Account code */
75         char account[256];
76         
77         /* Maximum length of call */
78         int maxlen;
79         
80 };
81
82 static void init_outgoing(struct outgoing *o)
83 {
84         memset(o, 0, sizeof(struct outgoing));
85         o->priority = 1;
86         o->retrytime = 300;
87         o->waittime = 45;
88 }
89
90 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
91 {
92         char buf[256];
93         char *c, *c2;
94         int lineno = 0;
95         while(!feof(f)) {
96                 fgets(buf, sizeof(buf), f);
97                 lineno++;
98                 if (!feof(f)) {
99                         /* Trim comments */
100                         c = strchr(buf, '#');
101                         if (c)
102                                  *c = '\0';
103                         c = strchr(buf, ';');
104                         if (c)
105                                  *c = '\0';
106
107                         /* Trim trailing white space */
108                         while(strlen(buf) && buf[strlen(buf) - 1] < 33)
109                                 buf[strlen(buf) - 1] = '\0';
110                         if (strlen(buf)) {
111                                 c = strchr(buf, ':');
112                                 if (c) {
113                                         *c = '\0';
114                                         c++;
115                                         while(*c < 33)
116                                                 c++;
117 #if 0
118                                         printf("'%s' is '%s' at line %d\n", buf, c, lineno);
119 #endif                                  
120                                         if (!strcasecmp(buf, "channel")) {
121                                                 strncpy(o->tech, c, sizeof(o->tech) - 1);
122                                                 if ((c2 = strchr(o->tech, '/'))) {
123                                                         *c2 = '\0';
124                                                         c2++;
125                                                         strncpy(o->dest, c2, sizeof(o->dest) - 1);
126                                                 } else {
127                                                         ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
128                                                         strcpy(o->tech, "");
129                                                 }
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);
139                                                         o->maxretries = 0;
140                                                 }
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);
148                                                         o->priority = 1;
149                                                 }
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);
153                                                         o->retrytime = 300;
154                                                 }
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);
158                                                         o->waittime = 45;
159                                                 }
160                                         } else if (!strcasecmp(buf, "retry")) {
161                                                 o->retries++;
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");
165                                                         o->callingpid = 0;
166                                                 }
167                                         } else if (!strcasecmp(buf, "endretry") || !strcasecmp(buf, "abortretry")) {
168                                                 o->callingpid = 0;
169                                                 o->retries++;
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);
173  
174                                         } else if (!strcasecmp(buf, "account")) {
175                                                 strncpy(o->account, c, sizeof(o->account) - 1);
176                                         } else {
177                                                 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
178                                         }
179                                 } else
180                                         ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
181                         }
182                 }
183         }
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);
190                 return -1;
191         }
192         return 0;
193 }
194
195 static void safe_append(struct outgoing *o, time_t now, char *s)
196 {
197         int fd;
198         FILE *f;
199         fd = open(o->fn, O_WRONLY|O_APPEND);
200         if (fd > -1) {
201                 f = fdopen(fd, "a");
202                 if (f) {
203                         fprintf(f, "%s: %d (%ld) (%ld)\n", s, o->retries, (long)ast_mainpid, (long) now);
204                         fclose(f);
205                 } else
206                         close(fd);
207         }
208 }
209
210 static void *attempt_thread(void *data)
211 {
212         struct outgoing *o = data;
213         int res, reason;
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);
218         } else {
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);
222         }
223         if (res) {
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);
228                         unlink(o->fn);
229                 } else {
230                         /* Notate that the call is still active */
231                         safe_append(o, time(NULL), "EndRetry");
232                 }
233         } else {
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);
236                 unlink(o->fn);
237         }
238         free(o);
239         return NULL;
240 }
241
242 static void launch_service(struct outgoing *o)
243 {
244         pthread_t t;
245         pthread_attr_t attr;
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");
250                 free(o);
251         }
252 }
253
254 static int scan_service(char *fn, time_t now, time_t atime)
255 {
256         struct outgoing *o;
257         struct utimbuf tbuf;
258         FILE *f;
259         o = malloc(sizeof(struct outgoing));
260         if (o) {
261                 init_outgoing(o);
262                 f = fopen(fn, "r+");
263                 if (f) {
264                         if (!apply_outgoing(o, fn, f)) {
265 #if 0
266                                 printf("Retries: %d, max: %d\n", o->retries, o->maxretries);
267 #endif
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);
271                                         } else {
272                                                 /* Increment retries */
273                                                 o->retries++;
274                                                 /* If someone else was calling, they're presumably gone now
275                                                    so abort their retry and continue as we were... */
276                                                 if (o->callingpid)
277                                                         safe_append(o, time(NULL), "AbortRetry");
278                                                         
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);
282                                                 fclose(f);
283                                                 launch_service(o);
284                                         }
285                                         /* Update the file time */
286                                         tbuf.actime = atime;
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));
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                                         fclose(f);
295                                         free(o);
296                                         unlink(fn);
297                                         return 0;
298                                 }
299                         } else {
300                                 free(o);
301                                 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
302                                 fclose(f);
303                                 unlink(fn);
304                         }
305                 } else {
306                         free(o);
307                         ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
308                         unlink(fn);
309                 }
310         } else
311                 ast_log(LOG_WARNING, "Out of memory :(\n");
312         return -1;
313 }
314
315 static void *scan_thread(void *unused)
316 {
317         struct stat st;
318         DIR *dir;
319         struct dirent *de;
320         char fn[256];
321         int res;
322         time_t last = 0, next = 0, now;
323         for(;;) {
324                 /* Wait a sec */
325                 sleep(1);
326                 time(&now);
327                 if (!stat(qdir, &st)) {
328                         if ((st.st_mtime != last) || (next && (now > next))) {
329 #if 0
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");
332 #endif                          
333                                 next = 0;
334                                 last = st.st_mtime;
335                                 dir = opendir(qdir);
336                                 if (dir) {
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);
343                                                                         if (res > 0) {
344                                                                                 /* Update next service time */
345                                                                                 if (!next || (res < next)) {
346                                                                                         next = res;
347                                                                                 }
348                                                                         } else if (res)
349                                                                                 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
350                                                                 } else {
351                                                                         /* Update "next" update if necessary */
352                                                                         if (!next || (st.st_mtime < next))
353                                                                                 next = st.st_mtime;
354                                                                 }
355                                                         }
356                                                 } else
357                                                         ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
358                                         }
359                                         closedir(dir);
360                                 } else
361                                         ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
362                         }
363                 } else
364                         ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
365         }
366         return NULL;
367 }
368
369 int unload_module(void)
370 {
371         return -1;
372 }
373
374 int load_module(void)
375 {
376         pthread_t thread;
377         pthread_attr_t attr;
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);
381                 return 0;
382         }
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");
387                 return -1;
388         }
389         return 0;
390 }
391
392 char *description(void)
393 {
394         return tdesc;
395 }
396
397 int usecount(void)
398 {
399         return 1;
400 }
401
402 char *key()
403 {
404         return ASTERISK_GPL_KEY;
405 }