add missing ~ to ast_parse_allow_deny that was breaking disallow
[asterisk/asterisk.git] / cdr.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Call Detail Record API 
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
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  * Includes code and algorithms from the Zapata library.
14  *
15  */
16
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/callerid.h>
22 #include <asterisk/causes.h>
23 #include <asterisk/options.h>
24 #include <asterisk/utils.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
30 char ast_default_accountcode[20] = "";
31
32 AST_MUTEX_DEFINE_STATIC(cdrlock);
33
34 static struct ast_cdr_beitem {
35         char name[20];
36         char desc[80];
37         ast_cdrbe be;
38         struct ast_cdr_beitem *next;
39 } *bes = NULL;
40
41
42 /*
43  * We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
44  * through our fingers somehow.  If someone allocates a CDR, it must be completely handled normally
45  * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
46  * isn't properly generated and posted.
47  */
48
49 int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
50 {
51         struct ast_cdr_beitem *i;
52         if (!name)
53                 return -1;
54         if (!be) {
55                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
56                 return -1;
57         }
58         ast_mutex_lock(&cdrlock);
59         i = bes;
60         while(i) {
61                 if (!strcasecmp(name, i->name))
62                         break;
63                 i = i->next;
64         }
65         ast_mutex_unlock(&cdrlock);
66         if (i) {
67                 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
68                 return -1;
69         }
70         i = malloc(sizeof(struct ast_cdr_beitem));
71         if (!i)         
72                 return -1;
73         memset(i, 0, sizeof(struct ast_cdr_beitem));
74         strncpy(i->name, name, sizeof(i->name) - 1);
75         strncpy(i->desc, desc, sizeof(i->desc) - 1);
76         i->be = be;
77         ast_mutex_lock(&cdrlock);
78         i->next = bes;
79         bes = i;
80         ast_mutex_unlock(&cdrlock);
81         return 0;
82 }
83
84 void ast_cdr_unregister(char *name)
85 {
86         struct ast_cdr_beitem *i, *prev = NULL;
87         ast_mutex_lock(&cdrlock);
88         i = bes;
89         while(i) {
90                 if (!strcasecmp(name, i->name)) {
91                         if (prev)
92                                 prev->next = i->next;
93                         else
94                                 bes = i->next;
95                         break;
96                 }
97                 i = i->next;
98         }
99         if (option_verbose > 1)
100                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
101         ast_mutex_unlock(&cdrlock);
102         if (i) 
103                 free(i);
104 }
105
106 void ast_cdr_free(struct ast_cdr *cdr)
107 {
108         char *chan;
109         struct ast_cdr *next; 
110         while (cdr) {
111                 next = cdr->next;
112                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
113                 if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
114                         ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
115                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
116                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
117                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
118                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
119                 free(cdr);
120                 cdr = next;
121         }
122 }
123
124 struct ast_cdr *ast_cdr_alloc(void)
125 {
126         struct ast_cdr *cdr;
127         cdr = malloc(sizeof(struct ast_cdr));
128         if (cdr) {
129                 memset(cdr, 0, sizeof(struct ast_cdr));
130         }
131         return cdr;
132 }
133
134 void ast_cdr_start(struct ast_cdr *cdr)
135 {
136         char *chan; 
137         while (cdr) {
138                 if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
139                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
140                         if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
141                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
142                         if (cdr->start.tv_sec || cdr->start.tv_usec)
143                                 ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
144                         gettimeofday(&cdr->start, NULL);
145                 }
146                         cdr = cdr->next;
147         }
148 }
149
150 void ast_cdr_answer(struct ast_cdr *cdr)
151 {
152         char *chan; 
153         while (cdr) {
154                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
155                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
156                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
157                 if (cdr->disposition < AST_CDR_ANSWERED)
158                         cdr->disposition = AST_CDR_ANSWERED;
159                 if (!cdr->answer.tv_sec && !cdr->answer.tv_usec) {
160                         gettimeofday(&cdr->answer, NULL);
161                 }
162                 cdr = cdr->next;
163         }
164 }
165
166 void ast_cdr_busy(struct ast_cdr *cdr)
167 {
168         char *chan; 
169         while (cdr) {
170                 if (!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
171                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
172                         if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
173                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
174                         if (cdr->disposition < AST_CDR_BUSY)
175                                 cdr->disposition = AST_CDR_BUSY;
176                 }
177                 cdr = cdr->next;
178         }
179 }
180
181 void ast_cdr_failed(struct ast_cdr *cdr)
182 {
183         char *chan; 
184         while (cdr) {
185                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
186                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
187                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
188                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
189                         cdr->disposition = AST_CDR_FAILED;
190                 cdr = cdr->next;
191         }
192 }
193
194 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
195 {
196         int res = 0;
197         while (cdr) {
198                 switch(cause) {
199                         case AST_CAUSE_BUSY:
200                                 ast_cdr_busy(cdr);
201                                 break;
202                         case AST_CAUSE_FAILURE:
203                                 ast_cdr_failed(cdr);
204                                 break;
205                         case AST_CAUSE_NORMAL:
206                                 break;
207                         case AST_CAUSE_NOTDEFINED:
208                                 res = -1;
209                                 break;
210                         default:
211                                 res = -1;
212                                 ast_log(LOG_WARNING, "We don't handle that cause yet\n");
213                 }
214                 cdr = cdr->next;
215         }
216         return res;
217 }
218
219 void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
220 {
221         char *chan; 
222         while (cdr) {
223                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
224                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
225                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
226                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
227                         strncpy(cdr->dstchannel, chann, sizeof(cdr->dstchannel) - 1);
228                 cdr = cdr->next;
229         }
230 }
231
232 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
233 {
234         char *chan; 
235         while (cdr) {
236                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
237                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
238                         if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
239                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
240                         if (!app)
241                                 app = "";
242                         strncpy(cdr->lastapp, app, sizeof(cdr->lastapp) - 1);
243                         if (!data)
244                                 data = "";
245                         strncpy(cdr->lastdata, data, sizeof(cdr->lastdata) - 1);
246                 }
247                 cdr = cdr->next;
248         }
249 }
250
251 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
252 {
253         char tmp[AST_MAX_EXTENSION] = "";
254         char *num;
255         while (cdr) {
256                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
257                         /* Grab source from ANI or normal Caller*ID */
258                         if (c->cid.cid_ani)
259                                 num = c->cid.cid_ani;
260                         else
261                                 num = c->cid.cid_num;
262                         
263                         if (c->cid.cid_name && num)
264                                 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
265                         else if (c->cid.cid_name)
266                                 strncpy(tmp, c->cid.cid_name, sizeof(tmp) - 1);
267                         else if (num)
268                                 strncpy(tmp, num, sizeof(tmp) - 1);
269                         else
270                                 strcpy(tmp, "");
271                         strncpy(cdr->clid, tmp, sizeof(cdr->clid) - 1);
272                         if (num)
273                                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
274                         else
275                                 strcpy(cdr->src, "");
276                 }
277                 cdr = cdr->next;
278         }
279         return 0;
280 }
281
282 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
283 {
284         char *chan;
285         char *num;
286         char tmp[AST_MAX_EXTENSION] = "";
287         while (cdr) {
288                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
289                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
290                         if (!ast_strlen_zero(cdr->channel)) 
291                                 ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
292                         strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
293                         /* Grab source from ANI or normal Caller*ID */
294                         if (c->cid.cid_ani)
295                                 num = c->cid.cid_ani;
296                         else
297                                 num = c->cid.cid_num;
298                         
299                         if (c->cid.cid_name && num)
300                                 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
301                         else if (c->cid.cid_name)
302                                 strncpy(tmp, c->cid.cid_name, sizeof(tmp) - 1);
303                         else if (num)
304                                 strncpy(tmp, num, sizeof(tmp) - 1);
305                         else
306                                 strcpy(tmp, "");
307                         strncpy(cdr->clid, tmp, sizeof(cdr->clid) - 1);
308                         if (num)
309                                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
310                         else
311                                 strcpy(cdr->src, "");
312
313                         if (c->_state == AST_STATE_UP)
314                                 cdr->disposition = AST_CDR_ANSWERED;
315                         else
316                                 cdr->disposition = AST_CDR_NOANSWER;
317                         if (c->amaflags)
318                                 cdr->amaflags = c->amaflags;
319                         else
320                                 cdr->amaflags = ast_default_amaflags;
321                         strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
322                         /* Destination information */
323                         strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
324                         strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
325                         /* Unique call identifier */
326                         strncpy(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid) - 1);
327                 }
328                 cdr = cdr->next;
329         }
330         return 0;
331 }
332
333 void ast_cdr_end(struct ast_cdr *cdr)
334 {
335         char *chan;
336         while (cdr) {
337                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
338                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
339                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
340                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
341                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
342                 if (!cdr->end.tv_sec && !cdr->end.tv_usec) 
343                         gettimeofday(&cdr->end, NULL);
344                 cdr = cdr->next;
345         }
346 }
347
348 char *ast_cdr_disp2str(int disposition)
349 {
350         switch (disposition) {
351         case AST_CDR_NOANSWER:
352                 return "NO ANSWER";
353         case AST_CDR_FAILED:
354                 return "FAILED";                
355         case AST_CDR_BUSY:
356                 return "BUSY";          
357         case AST_CDR_ANSWERED:
358                 return "ANSWERED";
359         default:
360                 return "UNKNOWN";
361         }
362 }
363
364 char *ast_cdr_flags2str(int flag)
365 {
366         switch(flag) {
367         case AST_CDR_OMIT:
368                 return "OMIT";
369         case AST_CDR_BILLING:
370                 return "BILLING";
371         case AST_CDR_DOCUMENTATION:
372                 return "DOCUMENTATION";
373         }
374         return "Unknown";
375 }
376
377 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
378 {
379         struct ast_cdr *cdr = chan->cdr;
380
381         strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
382         while (cdr) {
383                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
384                         strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
385                 cdr = cdr->next;
386         }
387         return 0;
388 }
389
390 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
391 {
392         struct ast_cdr *cdr = chan->cdr;
393         int newflag;
394
395         newflag = ast_cdr_amaflags2int(flag);
396         if (newflag) {
397                 cdr->amaflags = newflag;
398         }
399         return 0;
400 }
401
402 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
403 {
404         struct ast_cdr *cdr = chan->cdr;
405
406         while (cdr) {
407                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) 
408                         strncpy(cdr->userfield, userfield, sizeof(cdr->userfield) - 1);
409                 cdr = cdr->next;
410         }
411         return 0;
412 }
413
414 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
415 {
416         struct ast_cdr *cdr = chan->cdr;
417
418         while (cdr)
419         {
420
421                 int len = strlen(cdr->userfield);
422                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED))
423                         strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
424                 cdr = cdr->next;
425         }
426         return 0;
427 }
428
429 int ast_cdr_update(struct ast_channel *c)
430 {
431         struct ast_cdr *cdr = c->cdr;
432         char *num;
433         char tmp[AST_MAX_EXTENSION] = "";
434         /* Grab source from ANI or normal Caller*ID */
435         while (cdr) {
436                 if(!ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
437                         /* Grab source from ANI or normal Caller*ID */
438                         if (c->cid.cid_ani)
439                                 num = c->cid.cid_ani;
440                         else
441                                 num = c->cid.cid_num;
442                         
443                         if (c->cid.cid_name && num)
444                                 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
445                         else if (c->cid.cid_name)
446                                 strncpy(tmp, c->cid.cid_name, sizeof(tmp) - 1);
447                         else if (num)
448                                 strncpy(tmp, num, sizeof(tmp) - 1);
449                         else
450                                 strcpy(tmp, "");
451                         strncpy(cdr->clid, tmp, sizeof(cdr->clid) - 1);
452                         if (num)
453                                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
454                         else
455                                 strcpy(cdr->src, "");
456
457                         /* Copy account code et-al */   
458                         strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
459                         /* Destination information */
460                         if (ast_strlen_zero(c->macroexten))
461                                 strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
462                         else
463                                 strncpy(cdr->dst, c->macroexten, sizeof(cdr->dst) - 1);
464                         if (ast_strlen_zero(c->macrocontext))
465                                 strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
466                         else
467                                 strncpy(cdr->dcontext, c->macrocontext, sizeof(cdr->dcontext) - 1);
468                 }
469                 cdr = cdr->next;
470         }
471
472         return 0;
473 }
474
475 int ast_cdr_amaflags2int(const char *flag)
476 {
477         if (!strcasecmp(flag, "default"))
478                 return 0;
479         if (!strcasecmp(flag, "omit"))
480                 return AST_CDR_OMIT;
481         if (!strcasecmp(flag, "billing"))
482                 return AST_CDR_BILLING;
483         if (!strcasecmp(flag, "documentation"))
484                 return AST_CDR_DOCUMENTATION;
485         return -1;
486 }
487
488 void ast_cdr_post(struct ast_cdr *cdr)
489 {
490         char *chan;
491         struct ast_cdr_beitem *i;
492         while (cdr) {
493                 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
494                 if (ast_cdr_has_flag(cdr,AST_CDR_FLAG_POSTED))
495                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
496                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
497                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
498                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
499                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
500                 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
501                 if (cdr->answer.tv_sec || cdr->answer.tv_usec) {
502                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
503                 } else
504                         cdr->billsec = 0;
505                 ast_cdr_add_flag(cdr,AST_CDR_FLAG_POSTED);
506                 ast_mutex_lock(&cdrlock);
507                 i = bes;
508                 while(i) {
509                         i->be(cdr);
510                         i = i->next;
511                 }
512                 ast_mutex_unlock(&cdrlock);
513                 cdr = cdr->next;
514         }
515 }
516
517 void ast_cdr_reset(struct ast_cdr *cdr, int flags)
518 {
519         while (cdr) {
520                 /* Post if requested */
521                 if (ast_cdr_compare_flag(flags,AST_CDR_FLAG_LOCKED) || !ast_cdr_has_flag(cdr,AST_CDR_FLAG_LOCKED)) {
522                         if (ast_cdr_compare_flag(flags,AST_CDR_FLAG_POSTED)) {
523                                 ast_cdr_end(cdr);
524                                 ast_cdr_post(cdr);
525                         }
526                         /* Reset to initial state */
527                         cdr->flags=0;
528                         memset(&cdr->start, 0, sizeof(cdr->start));
529                         memset(&cdr->end, 0, sizeof(cdr->end));
530                         memset(&cdr->answer, 0, sizeof(cdr->answer));
531                         cdr->billsec = 0;
532                         cdr->duration = 0;
533                         ast_cdr_start(cdr);
534                         cdr->disposition = AST_CDR_NOANSWER;
535                 }
536                         
537                 cdr = cdr->next;
538         }
539         
540 }
541
542 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr) 
543 {
544         struct ast_cdr *ret;
545         if (cdr) {
546                 ret = cdr;
547                 while(cdr->next)
548                         cdr = cdr->next;
549                 cdr->next = newcdr;
550         } else {
551                 ret = newcdr;
552         }
553         return ret;
554 }