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