Only update utime in pbx_spool.c if the file exists
[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                 /* Update the file time */
207                 tbuf.actime = now;
208                 tbuf.modtime = now + o->retrytime;
209                 if (utime(o->fn, &tbuf))
210                         ast_log(LOG_WARNING, "Unable to set utime on %s: %s\n", o->fn, strerror(errno));
211         }
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         FILE *f;
262         o = malloc(sizeof(struct outgoing));
263         if (o) {
264                 init_outgoing(o);
265                 f = fopen(fn, "r+");
266                 if (f) {
267                         if (!apply_outgoing(o, fn, f)) {
268 #if 0
269                                 printf("Filename: %s, Retries: %d, max: %d\n", fn, o->retries, o->maxretries);
270 #endif
271                                 fclose(f);
272                                 if (o->retries <= o->maxretries) {
273                                         if (o->callingpid && (o->callingpid == ast_mainpid)) {
274                                                 safe_append(o, time(NULL), "DelayedRetry");
275                                                 ast_log(LOG_DEBUG, "Delaying retry since we're currently running '%s'\n", o->fn);
276                                         } else {
277                                                 /* Increment retries */
278                                                 o->retries++;
279                                                 /* If someone else was calling, they're presumably gone now
280                                                    so abort their retry and continue as we were... */
281                                                 if (o->callingpid)
282                                                         safe_append(o, time(NULL), "AbortRetry");
283
284                                                 safe_append(o, now, "StartRetry");
285                                                 launch_service(o);
286                                         }
287                                         now += o->retrytime;
288                                         return now;
289                                 } else {
290                                         ast_log(LOG_EVENT, "Queued call to %s/%s expired without completion after %d attempt(s)\n", o->tech, o->dest, o->retries - 1);
291                                         free(o);
292                                         unlink(fn);
293                                         return 0;
294                                 }
295                         } else {
296                                 free(o);
297                                 ast_log(LOG_WARNING, "Invalid file contents in %s, deleting\n", fn);
298                                 fclose(f);
299                                 unlink(fn);
300                         }
301                 } else {
302                         free(o);
303                         ast_log(LOG_WARNING, "Unable to open %s: %s, deleting\n", fn, strerror(errno));
304                         unlink(fn);
305                 }
306         } else
307                 ast_log(LOG_WARNING, "Out of memory :(\n");
308         return -1;
309 }
310
311 static void *scan_thread(void *unused)
312 {
313         struct stat st;
314         DIR *dir;
315         struct dirent *de;
316         char fn[256];
317         int res;
318         time_t last = 0, next = 0, now;
319         for(;;) {
320                 /* Wait a sec */
321                 sleep(1);
322                 time(&now);
323                 if (!stat(qdir, &st)) {
324                         if ((st.st_mtime != last) || (next && (now > next))) {
325 #if 0
326                                 printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
327                                 printf("Ooh, something changed / timeout\n");
328 #endif                          
329                                 next = 0;
330                                 last = st.st_mtime;
331                                 dir = opendir(qdir);
332                                 if (dir) {
333                                         while((de = readdir(dir))) {
334                                                 snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
335                                                 if (!stat(fn, &st)) {
336                                                         if (S_ISREG(st.st_mode)) {
337                                                                 if (st.st_mtime <= now) {
338                                                                         res = scan_service(fn, now, st.st_atime);
339                                                                         if (res > 0) {
340                                                                                 /* Update next service time */
341                                                                                 if (!next || (res < next)) {
342                                                                                         next = res;
343                                                                                 }
344                                                                         } else if (res)
345                                                                                 ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
346                                                                 } else {
347                                                                         /* Update "next" update if necessary */
348                                                                         if (!next || (st.st_mtime < next))
349                                                                                 next = st.st_mtime;
350                                                                 }
351                                                         }
352                                                 } else
353                                                         ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
354                                         }
355                                         closedir(dir);
356                                 } else
357                                         ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
358                         }
359                 } else
360                         ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
361         }
362         return NULL;
363 }
364
365 int unload_module(void)
366 {
367         return -1;
368 }
369
370 int load_module(void)
371 {
372         pthread_t thread;
373         pthread_attr_t attr;
374         snprintf((char *)qdir,sizeof(qdir)-1,"%s/%s",(char *)ast_config_AST_SPOOL_DIR,"outgoing");
375         if (mkdir(qdir, 0700) && (errno != EEXIST)) {
376                 ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir);
377                 return 0;
378         }
379         pthread_attr_init(&attr);
380         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
381         if (pthread_create(&thread,&attr,scan_thread, NULL) == -1) {
382                 ast_log(LOG_WARNING, "Unable to create thread :(\n");
383                 return -1;
384         }
385         return 0;
386 }
387
388 char *description(void)
389 {
390         return tdesc;
391 }
392
393 int usecount(void)
394 {
395         return 1;
396 }
397
398 char *key()
399 {
400         return ASTERISK_GPL_KEY;
401 }