Make pbx_spool update utime with every safe_append
[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, "delayedretry")) {
171                                         } else if (!strcasecmp(buf, "setvar")) { /* JDG variable support */
172                                                 strncat(o->variable, c, sizeof(o->variable) - strlen(o->variable) - 1);
173                                                 strncat(o->variable, "|", sizeof(o->variable) - strlen(o->variable) - 1);
174  
175                                         } else if (!strcasecmp(buf, "account")) {
176                                                 strncpy(o->account, c, sizeof(o->account) - 1);
177                                         } else {
178                                                 ast_log(LOG_WARNING, "Unknown keyword '%s' at line %d of %s\n", buf, lineno, fn);
179                                         }
180                                 } else
181                                         ast_log(LOG_NOTICE, "Syntax error at line %d of %s\n", lineno, fn);
182                         }
183                 }
184         }
185         strncpy(o->fn, fn, sizeof(o->fn) - 1);
186         if (!strlen(o->tech) || !strlen(o->dest) || (!strlen(o->app) && !strlen(o->exten))) {
187                 ast_log(LOG_WARNING, "At least one of app or extension must be specified, along with tech and dest in file %s\n", fn);
188                 return -1;
189         }
190         return 0;
191 }
192
193 static void safe_append(struct outgoing *o, time_t now, char *s)
194 {
195         int fd;
196         FILE *f;
197         struct utimbuf tbuf;
198         fd = open(o->fn, O_WRONLY|O_APPEND);
199         if (fd > -1) {
200                 f = fdopen(fd, "a");
201                 if (f) {
202                         fprintf(f, "%s: %ld %d (%ld)\n", s, (long)ast_mainpid, o->retries, (long) now);
203                         fclose(f);
204                 } else
205                         close(fd);
206         }
207         /* Update the file time */
208         tbuf.actime = now;
209         tbuf.modtime = now + o->retrytime;
210         if (utime(o->fn, &tbuf))
211                 ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
212 }
213
214 static void *attempt_thread(void *data)
215 {
216         struct outgoing *o = data;
217         int res, reason;
218         if (strlen(o->app)) {
219                 if (option_verbose > 2)
220                         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);
221                 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);
222         } else {
223                 if (option_verbose > 2)
224                         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);
225                 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);
226         }
227         if (res) {
228                 ast_log(LOG_NOTICE, "Call failed to go through, reason %d\n", reason);
229                 if (o->retries >= o->maxretries + 1) {
230                         /* Max retries exceeded */
231                         ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
232                         unlink(o->fn);
233                 } else {
234                         /* Notate that the call is still active */
235                         safe_append(o, time(NULL), "EndRetry");
236                 }
237         } else {
238                 ast_log(LOG_NOTICE, "Call completed to %s/%s\n", o->tech, o->dest);
239                 ast_log(LOG_EVENT, "Queued call to %s/%s completed\n", o->tech, o->dest);
240                 unlink(o->fn);
241         }
242         free(o);
243         return NULL;
244 }
245
246 static void launch_service(struct outgoing *o)
247 {
248         pthread_t t;
249         pthread_attr_t attr;
250         pthread_attr_init(&attr);
251         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
252         if (pthread_create(&t,&attr,attempt_thread, o) == -1) {
253                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
254                 free(o);
255         }
256 }
257
258 static int scan_service(char *fn, time_t now, time_t atime)
259 {
260         struct outgoing *o;
261         struct utimbuf tbuf;
262         FILE *f;
263         o = malloc(sizeof(struct outgoing));
264         if (o) {
265                 init_outgoing(o);
266                 f = fopen(fn, "r+");
267                 if (f) {
268                         if (!apply_outgoing(o, fn, f)) {
269 #if 0
270                                 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
271 #endif
272                                 fclose(f);
273                                 if (o->retries <= o->maxretries) {
274                                         if (o->callingpid && (o->callingpid == ast_mainpid)) {
275                                                 safe_append(o, time(NULL), "DelayedRetry");
276                                                 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
277                                         } else {
278                                                 /* Increment retries */
279                                                 o->retries++;
280                                                 /* If someone else was calling, they're presumably gone now
281                                                    so abort their retry and continue as we were... */
282                                                 if (o->callingpid)
283                                                         safe_append(o, time(NULL), "AbortRetry");
284
285                                                 safe_append(o, now, "StartRetry");
286                                                 launch_service(o);
287                                         }
288                                         now += o->retrytime;
289                                         return now;
290                                 } else {
291                                         ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
292                                         free(o);
293                                         unlink(fn);
294                                         return 0;
295                                 }
296                         } else {
297                                 free(o);
298                                 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
299                                 fclose(f);
300                                 unlink(fn);
301                         }
302                 } else {
303                         free(o);
304                         ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
305                         unlink(fn);
306                 }
307         } else
308                 ast_log(LOG_WARNING, "Out of memory :(\n");
309         return -1;
310 }
311
312 static void *scan_thread(void *unused)
313 {
314         struct stat st;
315         DIR *dir;
316         struct dirent *de;
317         char fn[256];
318         int res;
319         time_t last = 0, next = 0, now;
320         for(;;) {
321                 /* Wait a sec */
322                 sleep(1);
323                 time(&now);
324                 if (!stat(qdir, &st)) {
325                         if ((st.st_mtime != last) || (next && (now > next))) {
326 #if 0
327                                 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
328                                 printf("Ooh, something changed / timeout\n");
329 #endif                          
330                                 next = 0;
331                                 last = st.st_mtime;
332                                 dir = opendir(qdir);
333                                 if (dir) {
334                                         while((de = readdir(dir))) {
335                                                 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
336                                                 if (!stat(fn, &st)) {
337                                                         if (S_ISREG(st.st_mode)) {
338                                                                 if (st.st_mtime <= now) {
339                                                                         res = scan_service(fn, now, st.st_atime);
340                                                                         if (res > 0) {
341                                                                                 /* Update next service time */
342                                                                                 if (!next || (res < next)) {
343                                                                                         next = res;
344                                                                                 }
345                                                                         } else if (res)
346                                                                                 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
347                                                                 } else {
348                                                                         /* Update "next" update if necessary */
349                                                                         if (!next || (st.st_mtime < next))
350                                                                                 next = st.st_mtime;
351                                                                 }
352                                                         }
353                                                 } else
354                                                         ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
355                                         }
356                                         closedir(dir);
357                                 } else
358                                         ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
359                         }
360                 } else
361                         ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
362         }
363         return NULL;
364 }
365
366 int unload_module(void)
367 {
368         return -1;
369 }
370
371 int load_module(void)
372 {
373         pthread_t thread;
374         pthread_attr_t attr;
375         snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
376         if (mkdir(qdir, 0700) && (errno != EEXIST)) {
377                 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
378                 return 0;
379         }
380         pthread_attr_init(&attr);
381         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
382         if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
383                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
384                 return -1;
385         }
386         return 0;
387 }
388
389 char *description(void)
390 {
391         return tdesc;
392 }
393
394 int usecount(void)
395 {
396         return 1;
397 }
398
399 char *key()
400 {
401         return ASTERISK_GPL_KEY;
402 }