Merge james's patch (bug #7)
[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         
54         /* What to connect to outgoing */
55         char tech[256];
56         char dest[256];
57         
58         /* If application */
59         char app[256];
60         char data[256];
61
62         /* If extension/context/priority */
63         char exten[256];
64         char context[256];
65         int priority;
66
67         /* CallerID Information */
68         char callerid[256];
69
70         /* Channel variables */
71         char variable[10*256];
72         
73         /* Maximum length of call */
74         int maxlen;
75         
76 };
77
78 static void init_outgoing(struct outgoing *o)
79 {
80         memset(o, 0, sizeof(struct outgoing));
81         o->priority = 1;
82         o->retrytime = 300;
83         o->waittime = 45;
84 }
85
86 static int apply_outgoing(struct outgoing *o, char *fn, FILE *f)
87 {
88         char buf[256];
89         char *c, *c2;
90         int lineno = 0;
91         while(!feof(f)) {
92                 fgets(buf, sizeof(buf), f);
93                 lineno++;
94                 if (!feof(f)) {
95                         /* Trim comments */
96                         c = strchr(buf, '#');
97                         if (c)
98                                  *c = '\0';
99                         c = strchr(buf, ';');
100                         if (c)
101                                  *c = '\0';
102
103                         /* Trim trailing white space */
104                         while(strlen(buf) && buf[strlen(buf) - 1] < 33)
105                                 buf[strlen(buf) - 1] = '\0';
106                         if (strlen(buf)) {
107                                 c = strchr(buf, ':');
108                                 if (c) {
109                                         *c = '\0';
110                                         c++;
111                                         while(*c < 33)
112                                                 c++;
113 #if 0
114                                         printf("'%s' is '%s' at line %d\n", buf, c, lineno);
115 #endif                                  
116                                         if (!strcasecmp(buf, "channel")) {
117                                                 strncpy(o->tech, c, sizeof(o->tech) - 1);
118                                                 if ((c2 = strchr(o->tech, '/'))) {
119                                                         *c2 = '\0';
120                                                         c2++;
121                                                         strncpy(o->dest, c2, sizeof(o->dest) - 1);
122                                                 } else {
123                                                         ast_log(LOG_NOTICE, "Channel should be in form Tech/Dest at line %d of %s\n", lineno, fn);
124                                                         strcpy(o->tech, "");
125                                                 }
126                                         } else if (!strcasecmp(buf, "callerid")) {
127                                                 strncpy(o->callerid, c, sizeof(o->callerid) - 1);
128                                         } else if (!strcasecmp(buf, "application")) {
129                                                 strncpy(o->app, c, sizeof(o->app) - 1);
130                                         } else if (!strcasecmp(buf, "data")) {
131                                                 strncpy(o->data, c, sizeof(o->data) - 1);
132                                         } else if (!strcasecmp(buf, "maxretries")) {
133                                                 if (sscanf(c, "%d", &o->maxretries) != 1) {
134                                                         ast_log(LOG_WARNING, "Invalid max retries at line %d of %s\n", lineno, fn);
135                                                         o->maxretries = 0;
136                                                 }
137                                         } else if (!strcasecmp(buf, "context")) {
138                                                 strncpy(o->context, c, sizeof(o->context) - 1);
139                                         } else if (!strcasecmp(buf, "extension")) {
140                                                 strncpy(o->exten, c, sizeof(o->exten) - 1);
141                                         } else if (!strcasecmp(buf, "priority")) {
142                                                 if ((sscanf(c, "%d", &o->priority) != 1) || (o->priority < 1)) {
143                                                         ast_log(LOG_WARNING, "Invalid priority at line %d of %s\n", lineno, fn);
144                                                         o->priority = 1;
145                                                 }
146                                         } else if (!strcasecmp(buf, "retrytime")) {
147                                                 if ((sscanf(c, "%d", &o->retrytime) != 1) || (o->retrytime < 1)) {
148                                                         ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
149                                                         o->retrytime = 300;
150                                                 }
151                                         } else if (!strcasecmp(buf, "waittime")) {
152                                                 if ((sscanf(c, "%d", &o->waittime) != 1) || (o->waittime < 1)) {
153                                                         ast_log(LOG_WARNING, "Invalid retrytime at line %d of %s\n", lineno, fn);
154                                                         o->waittime = 45;
155                                                 }
156                                         } else if (!strcasecmp(buf, "retry")) {
157                                                 o->retries++;
158                                         } else if (!strcasecmp(buf, "setvar")) { /* JDG variable support */
159                                                 strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
160                                                 strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
161  
162                                         } else {
163                                                 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
164                                         }
165                                 } else
166                                         ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
167                         }
168                 }
169         }
170         strncpy(o->fn, fn, sizeof(o->fn) - 1);
171         /* Check sanity of times */
172         if (o->retrytime < o->waittime + 5)
173                 o->retrytime = o->waittime + 5;
174         if (!strlen(o->tech) || !strlen(o->dest) || (!strlen(o->app) && !strlen(o->exten))) {
175                 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
176                 return -1;
177         }
178         return 0;
179 }
180
181 static void *attempt_thread(void *data)
182 {
183         struct outgoing *o = data;
184         int res, reason;
185         if (strlen(o->app)) {
186                 if (option_verbose > 2)
187                         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);
188                 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);
189         } else {
190                 if (option_verbose > 2)
191                         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);
192                 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 );
193         }
194         if (res) {
195                 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
196                 if (o->retries >= o->maxretries + 1) {
197                         /* Max retries exceeded */
198                         ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
199                         unlink(o->fn);
200                 }
201         } else {
202                 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
203                 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
204                 unlink(o->fn);
205         }
206         free(o);
207         return NULL;
208 }
209
210 static void launch_service(struct outgoing *o)
211 {
212         pthread_t t;
213         pthread_attr_t attr;
214         pthread_attr_init(&attr);
215         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
216         if (pthread_create(&t,&attr,attempt_thread, o) == -1) {
217                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
218                 free(o);
219         }
220 }
221
222 static int scan_service(char *fn, time_t now, time_t atime)
223 {
224         struct outgoing *o;
225         struct utimbuf tbuf;
226         FILE *f;
227         o = malloc(sizeof(struct outgoing));
228         if (o) {
229                 init_outgoing(o);
230                 f = fopen(fn, "r+");
231                 if (f) {
232                         if (!apply_outgoing(o, fn, f)) {
233                                 /* Update the file time */
234                                 tbuf.actime = atime;
235                                 tbuf.modtime = now + o->retrytime;
236                                 if (utime(o->fn, &tbuf))
237                                         ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", fn, strerror(errno));
238                                 /* Increment retries */
239                                 o->retries++;
240 #if 0
241                                 printf("Retries: %d, max: %d\n", o->retries, o->maxretries);
242 #endif
243                                 if (o->retries <= o->maxretries + 1) {
244                                         /* Add a retry line at the end */
245                                         fseek(f, 0L, SEEK_END);
246                                         fprintf(f, "Retry: %d (%ld)\n", o->retries, now);
247                                         fclose(f);
248                                         now += o->retrytime;
249                                         launch_service(o);
250                                         return now;
251                                 } else {
252                                         ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
253                                         fclose(f);
254                                         free(o);
255                                         unlink(fn);
256                                         return 0;
257                                 }
258                         } else {
259                                 free(o);
260                                 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
261                                 fclose(f);
262                                 unlink(fn);
263                         }
264                 } else {
265                         free(o);
266                         ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
267                         unlink(fn);
268                 }
269         } else
270                 ast_log(LOG_WARNING, "Out of memory :(\n");
271         return -1;
272 }
273
274 static void *scan_thread(void *unused)
275 {
276         struct stat st;
277         DIR *dir;
278         struct dirent *de;
279         char fn[256];
280         int res;
281         time_t last = 0, next = 0, now;
282         for(;;) {
283                 /* Wait a sec */
284                 sleep(1);
285                 time(&now);
286                 if (!stat(qdir, &st)) {
287                         if ((st.st_mtime != last) || (next && (now > next))) {
288 #if 0
289                                 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
290                                 printf("Ooh, something changed / timeout\n");
291 #endif                          
292                                 next = 0;
293                                 last = st.st_mtime;
294                                 dir = opendir(qdir);
295                                 if (dir) {
296                                         while((de = readdir(dir))) {
297                                                 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
298                                                 if (!stat(fn, &st)) {
299                                                         if (S_ISREG(st.st_mode)) {
300                                                                 if (st.st_mtime <= now) {
301                                                                         res = scan_service(fn, now, st.st_atime);
302                                                                         if (res > 0) {
303                                                                                 /* Update next service time */
304                                                                                 if (!next || (res < next)) {
305                                                                                         next = res;
306                                                                                 }
307                                                                         } else if (res)
308                                                                                 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
309                                                                 } else {
310                                                                         /* Update "next" update if necessary */
311                                                                         if (!next || (st.st_mtime < next))
312                                                                                 next = st.st_mtime;
313                                                                 }
314                                                         }
315                                                 } else
316                                                         ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
317                                         }
318                                         closedir(dir);
319                                 } else
320                                         ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
321                         }
322                 } else
323                         ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
324         }
325         return NULL;
326 }
327
328 int unload_module(void)
329 {
330         return -1;
331 }
332
333 int load_module(void)
334 {
335         pthread_t thread;
336         pthread_attr_t attr;
337         snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
338 printf("%s\n",qdir);
339         if (mkdir(qdir, 0700) && (errno != EEXIST)) {
340                 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
341                 return 0;
342         }
343         pthread_attr_init(&attr);
344         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
345         if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
346                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
347                 return -1;
348         }
349         return 0;
350 }
351
352 char *description(void)
353 {
354         return tdesc;
355 }
356
357 int usecount(void)
358 {
359         return 1;
360 }
361
362 char *key()
363 {
364         return ASTERISK_GPL_KEY;
365 }