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