44ecf4d6b4106a2d553d6c9f2d15ce3725d958a7
[asterisk/asterisk.git] / funcs / func_cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999-2006, Digium, Inc.
5  *
6  * Portions Copyright (C) 2005, Anthony Minessale II
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief  Call Detail Record related dialplan functions
22  *
23  * \author Anthony Minessale II
24  *
25  * \ingroup functions
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
41 #include "asterisk/cdr.h"
42 #include "asterisk/stasis.h"
43 #include "asterisk/stasis_message_router.h"
44
45 /*** DOCUMENTATION
46         <function name="CDR" language="en_US">
47                 <synopsis>
48                         Gets or sets a CDR variable.
49                 </synopsis>
50                 <syntax>
51                         <parameter name="name" required="true">
52                                 <para>CDR field name:</para>
53                                 <enumlist>
54                                         <enum name="clid">
55                                                 <para>Caller ID.</para>
56                                         </enum>
57                                         <enum name="lastdata">
58                                                 <para>Last application arguments.</para>
59                                         </enum>
60                                         <enum name="disposition">
61                                                 <para>The final state of the CDR.</para>
62                                                 <enumlist>
63                                                         <enum name="0">
64                                                                 <para><literal>NO ANSWER</literal></para>
65                                                         </enum>
66                                                         <enum name="1">
67                                                                 <para><literal>NO ANSWER</literal> (NULL record)</para>
68                                                         </enum>
69                                                         <enum name="2">
70                                                                 <para><literal>FAILED</literal></para>
71                                                         </enum>
72                                                         <enum name="4">
73                                                                 <para><literal>BUSY</literal></para>
74                                                         </enum>
75                                                         <enum name="8">
76                                                                 <para><literal>ANSWERED</literal></para>
77                                                         </enum>
78                                                         <enum name="16">
79                                                                 <para><literal>CONGESTION</literal></para>
80                                                         </enum>
81                                                 </enumlist>
82                                         </enum>
83                                         <enum name="src">
84                                                 <para>Source.</para>
85                                         </enum>
86                                         <enum name="start">
87                                                 <para>Time the call started.</para>
88                                         </enum>
89                                         <enum name="amaflags">
90                                                 <para>R/W the Automatic Message Accounting (AMA) flags on the channel.
91                                                 When read from a channel, the integer value will always be returned.
92                                                 When written to a channel, both the string format or integer value
93                                                 is accepted.</para>
94                                                 <enumlist>
95                                                         <enum name="1"><para><literal>OMIT</literal></para></enum>
96                                                         <enum name="2"><para><literal>BILLING</literal></para></enum>
97                                                         <enum name="3"><para><literal>DOCUMENTATION</literal></para></enum>
98                                                 </enumlist>
99                                                 <warning><para>Accessing this setting is deprecated in CDR. Please use the CHANNEL function instead.</para></warning>
100                                         </enum>
101                                         <enum name="dst">
102                                                 <para>Destination.</para>
103                                         </enum>
104                                         <enum name="answer">
105                                                 <para>Time the call was answered.</para>
106                                         </enum>
107                                         <enum name="accountcode">
108                                                 <para>The channel's account code.</para>
109                                                 <warning><para>Accessing this setting is deprecated in CDR. Please use the CHANNEL function instead.</para></warning>
110                                         </enum>
111                                         <enum name="dcontext">
112                                                 <para>Destination context.</para>
113                                         </enum>
114                                         <enum name="end">
115                                                 <para>Time the call ended.</para>
116                                         </enum>
117                                         <enum name="uniqueid">
118                                                 <para>The channel's unique id.</para>
119                                         </enum>
120                                         <enum name="dstchannel">
121                                                 <para>Destination channel.</para>
122                                         </enum>
123                                         <enum name="duration">
124                                                 <para>Duration of the call.</para>
125                                         </enum>
126                                         <enum name="userfield">
127                                                 <para>The channel's user specified field.</para>
128                                         </enum>
129                                         <enum name="lastapp">
130                                                 <para>Last application.</para>
131                                         </enum>
132                                         <enum name="billsec">
133                                                 <para>Duration of the call once it was answered.</para>
134                                         </enum>
135                                         <enum name="channel">
136                                                 <para>Channel name.</para>
137                                         </enum>
138                                         <enum name="sequence">
139                                                 <para>CDR sequence number.</para>
140                                         </enum>
141                                 </enumlist>
142                         </parameter>
143                         <parameter name="options" required="false">
144                                 <optionlist>
145                                         <option name="f">
146                                                 <para>Returns billsec or duration fields as floating point values.</para>
147                                         </option>
148                                         <option name="u">
149                                                 <para>Retrieves the raw, unprocessed value.</para>
150                                                 <para>For example, 'start', 'answer', and 'end' will be retrieved as epoch
151                                                 values, when the <literal>u</literal> option is passed, but formatted as YYYY-MM-DD HH:MM:SS
152                                                 otherwise.  Similarly, disposition and amaflags will return their raw
153                                                 integral values.</para>
154                                         </option>
155                                 </optionlist>
156                         </parameter>
157                 </syntax>
158                 <description>
159                         <para>All of the CDR field names are read-only, except for <literal>accountcode</literal>,
160                         <literal>userfield</literal>, and <literal>amaflags</literal>. You may, however, supply
161                         a name not on the above list, and create your own variable, whose value can be changed
162                         with this function, and this variable will be stored on the CDR.</para>
163                         <note><para>CDRs can only be modified before the bridge between two channels is
164                         torn down. For example, CDRs may not be modified after the <literal>Dial</literal>
165                         application has returned.</para></note>
166                         <para>Example: exten => 1,1,Set(CDR(userfield)=test)</para>
167                 </description>
168         </function>
169         <function name="CDR_PROP" language="en_US">
170                 <synopsis>
171                         Set a property on a channel's CDR.
172                 </synopsis>
173                 <syntax>
174                         <parameter name="name" required="true">
175                                 <para>The property to set on the CDR.</para>
176                                 <enumlist>
177                                         <enum name="party_a">
178                                                 <para>Set this channel as the preferred Party A when
179                                                 channels are associated together.</para>
180                                                 <para>Write-Only</para>
181                                         </enum>
182                                         <enum name="disable">
183                                                 <para>Disable CDRs for this channel.</para>
184                                                 <para>Write-Only</para>
185                                         </enum>
186                                 </enumlist>
187                         </parameter>
188                 </syntax>
189                 <description>
190                         <para>This function sets a property on a channel's CDR. Properties
191                         alter the behavior of how the CDR operates for that channel.</para>
192                 </description>
193         </function>
194  ***/
195
196 enum cdr_option_flags {
197         OPT_UNPARSED = (1 << 1),
198         OPT_FLOAT = (1 << 2),
199 };
200
201 AST_APP_OPTIONS(cdr_func_options, {
202         AST_APP_OPTION('f', OPT_FLOAT),
203         AST_APP_OPTION('u', OPT_UNPARSED),
204 });
205
206 struct cdr_func_payload {
207         struct ast_channel *chan;
208         const char *cmd;
209         const char *arguments;
210         const char *value;
211         void *data;
212 };
213
214 struct cdr_func_data {
215         char *buf;
216         size_t len;
217 };
218
219 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_read_message_type);
220 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_write_message_type);
221 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_prop_write_message_type);
222
223 static void cdr_read_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
224 {
225         struct cdr_func_payload *payload = stasis_message_data(message);
226         struct cdr_func_data *output;
227         char *info;
228         char *value = NULL;
229         struct ast_flags flags = { 0 };
230         char tempbuf[512];
231         AST_DECLARE_APP_ARGS(args,
232                 AST_APP_ARG(variable);
233                 AST_APP_ARG(options);
234         );
235
236         if (cdr_read_message_type() != stasis_message_type(message)) {
237                 return;
238         }
239
240         ast_assert(payload != NULL);
241         output = payload->data;
242         ast_assert(output != NULL);
243
244         if (ast_strlen_zero(payload->arguments)) {
245                 ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable[,option]))\n)",
246                         payload->cmd, payload->cmd);
247                 return;
248         }
249         info = ast_strdupa(payload->arguments);
250         AST_STANDARD_APP_ARGS(args, info);
251
252         if (!ast_strlen_zero(args.options)) {
253                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
254         }
255
256         if (ast_strlen_zero(ast_channel_name(payload->chan))) {
257                 /* Format request on a dummy channel */
258                 ast_cdr_format_var(ast_channel_cdr(payload->chan), args.variable, &value, tempbuf, sizeof(tempbuf), 0);
259                 if (ast_strlen_zero(value)) {
260                         return;
261                 }
262                 ast_copy_string(tempbuf, value, sizeof(tempbuf));
263                 ast_set_flag(&flags, OPT_UNPARSED);
264         } else if (ast_cdr_getvar(ast_channel_name(payload->chan), args.variable, tempbuf, sizeof(tempbuf))) {
265                 return;
266         }
267
268         if (ast_test_flag(&flags, OPT_FLOAT)
269                 && (!strcasecmp("billsec", args.variable) || !strcasecmp("duration", args.variable))) {
270                 long ms;
271                 double dtime;
272
273                 if (sscanf(tempbuf, "%30ld", &ms) != 1) {
274                         ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
275                                 args.variable, tempbuf, ast_channel_name(payload->chan));
276                         return;
277                 }
278                 dtime = (double)(ms / 1000.0);
279                 snprintf(tempbuf, sizeof(tempbuf), "%lf", dtime);
280         } else if (!ast_test_flag(&flags, OPT_UNPARSED)) {
281                 if (!strcasecmp("start", args.variable)
282                         || !strcasecmp("end", args.variable)
283                         || !strcasecmp("answer", args.variable)) {
284                         struct timeval fmt_time;
285                         struct ast_tm tm;
286                         /* tv_usec is suseconds_t, which could be int or long */
287                         long int tv_usec;
288
289                         if (sscanf(tempbuf, "%ld.%ld", &fmt_time.tv_sec, &tv_usec) != 2) {
290                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
291                                         args.variable, tempbuf, ast_channel_name(payload->chan));
292                                 return;
293                         }
294                         if (fmt_time.tv_sec) {
295                                 fmt_time.tv_usec = tv_usec;
296                                 ast_localtime(&fmt_time, &tm, NULL);
297                                 ast_strftime(tempbuf, sizeof(tempbuf), "%Y-%m-%d %T", &tm);
298                         } else {
299                                 tempbuf[0] = '\0';
300                         }
301                 } else if (!strcasecmp("disposition", args.variable)) {
302                         int disposition;
303
304                         if (sscanf(tempbuf, "%8d", &disposition) != 1) {
305                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
306                                         args.variable, tempbuf, ast_channel_name(payload->chan));
307                                 return;
308                         }
309                         snprintf(tempbuf, sizeof(tempbuf), "%s", ast_cdr_disp2str(disposition));
310                 } else if (!strcasecmp("amaflags", args.variable)) {
311                         int amaflags;
312
313                         if (sscanf(tempbuf, "%8d", &amaflags) != 1) {
314                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
315                                         args.variable, tempbuf, ast_channel_name(payload->chan));
316                                 return;
317                         }
318                         snprintf(tempbuf, sizeof(tempbuf), "%s", ast_channel_amaflags2string(amaflags));
319                 }
320         }
321
322         ast_copy_string(output->buf, tempbuf, output->len);
323 }
324
325 static void cdr_write_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
326 {
327         struct cdr_func_payload *payload = stasis_message_data(message);
328         struct ast_flags flags = { 0 };
329         AST_DECLARE_APP_ARGS(args,
330                 AST_APP_ARG(variable);
331                 AST_APP_ARG(options);
332         );
333         char *parse;
334
335         if (cdr_write_message_type() != stasis_message_type(message)) {
336                 return;
337         }
338
339         if (!payload) {
340                 return;
341         }
342
343         if (ast_strlen_zero(payload->arguments)) {
344                 ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable)=value)\n)",
345                         payload->cmd, payload->cmd);
346                 return;
347         }
348         if (ast_strlen_zero(payload->value)) {
349                 ast_log(AST_LOG_WARNING, "%s requires a value (%s(variable)=value)\n)",
350                         payload->cmd, payload->cmd);
351                 return;
352         }
353         parse = ast_strdupa(payload->arguments);
354         AST_STANDARD_APP_ARGS(args, parse);
355
356         if (!ast_strlen_zero(args.options)) {
357                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
358         }
359
360         if (!strcasecmp(args.variable, "accountcode")) {
361                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'accountcode' is deprecated. Please use the CHANNEL function instead.\n");
362                 ast_channel_lock(payload->chan);
363                 ast_channel_accountcode_set(payload->chan, payload->value);
364                 ast_channel_unlock(payload->chan);
365         } else if (!strcasecmp(args.variable, "peeraccount")) {
366                 ast_log(AST_LOG_WARNING, "The 'peeraccount' setting is not supported. Please set the 'accountcode' on the appropriate channel using the CHANNEL function.\n");
367         } else if (!strcasecmp(args.variable, "userfield")) {
368                 ast_cdr_setuserfield(ast_channel_name(payload->chan), payload->value);
369         } else if (!strcasecmp(args.variable, "amaflags")) {
370                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'amaflags' is deprecated. Please use the CHANNEL function instead.\n");
371                 if (isdigit(*payload->value)) {
372                         int amaflags;
373                         sscanf(payload->value, "%30d", &amaflags);
374                         ast_channel_lock(payload->chan);
375                         ast_channel_amaflags_set(payload->chan, amaflags);
376                         ast_channel_unlock(payload->chan);
377                 } else {
378                         ast_channel_lock(payload->chan);
379                         ast_channel_amaflags_set(payload->chan, ast_channel_string2amaflag(payload->value));
380                         ast_channel_unlock(payload->chan);
381                 }
382         } else {
383                 ast_cdr_setvar(ast_channel_name(payload->chan), args.variable, payload->value);
384         }
385         return;
386 }
387
388 static void cdr_prop_write_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
389 {
390         struct cdr_func_payload *payload = stasis_message_data(message);
391         enum ast_cdr_options option;
392         char *parse;
393         AST_DECLARE_APP_ARGS(args,
394                 AST_APP_ARG(variable);
395                 AST_APP_ARG(options);
396         );
397
398         if (cdr_prop_write_message_type() != stasis_message_type(message)) {
399                 return;
400         }
401
402         if (!payload) {
403                 return;
404         }
405
406         if (ast_strlen_zero(payload->arguments)) {
407                 ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable)=value)\n)",
408                         payload->cmd, payload->cmd);
409                 return;
410         }
411         if (ast_strlen_zero(payload->value)) {
412                 ast_log(AST_LOG_WARNING, "%s requires a value (%s(variable)=value)\n)",
413                         payload->cmd, payload->cmd);
414                 return;
415         }
416         parse = ast_strdupa(payload->arguments);
417         AST_STANDARD_APP_ARGS(args, parse);
418
419         if (!strcasecmp("party_a", args.variable)) {
420                 option = AST_CDR_FLAG_PARTY_A;
421         } else if (!strcasecmp("disable", args.variable)) {
422                 option = AST_CDR_FLAG_DISABLE_ALL;
423         } else {
424                 ast_log(AST_LOG_WARNING, "Unknown option %s used with %s\n", args.variable, payload->cmd);
425                 return;
426         }
427
428         if (ast_true(payload->value)) {
429                 ast_cdr_set_property(ast_channel_name(payload->chan), option);
430         } else {
431                 ast_cdr_clear_property(ast_channel_name(payload->chan), option);
432         }
433 }
434
435
436 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
437                     char *buf, size_t len)
438 {
439         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
440         RAII_VAR(struct cdr_func_payload *, payload,
441                 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
442         struct cdr_func_data output = { 0, };
443
444         if (!chan) {
445                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
446                 return -1;
447         }
448
449         if (!payload) {
450                 return -1;
451         }
452         payload->chan = chan;
453         payload->cmd = cmd;
454         payload->arguments = parse;
455         payload->data = &output;
456
457         buf[0] = '\0';/* Ensure the buffer is initialized. */
458         output.buf = buf;
459         output.len = len;
460
461         message = stasis_message_create(cdr_read_message_type(), payload);
462         if (!message) {
463                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
464                         ast_channel_name(chan));
465                 return -1;
466         }
467
468         /* If this is a request on a dummy channel, we're doing post-processing on an
469          * already dispatched CDR. Simply call the callback to calculate the value and
470          * return, instead of posting to Stasis as we would for a running channel.
471          */
472         if (ast_strlen_zero(ast_channel_name(chan))) {
473                 cdr_read_callback(NULL, NULL, message);
474         } else {
475                 RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
476
477                 if (!router) {
478                         ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
479                                 ast_channel_name(chan));
480                         return -1;
481                 }
482                 stasis_message_router_publish_sync(router, message);
483         }
484
485         return 0;
486 }
487
488 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
489                      const char *value)
490 {
491         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
492         RAII_VAR(struct cdr_func_payload *, payload,
493                      ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
494         RAII_VAR(struct stasis_message_router *, router,
495                      ast_cdr_message_router(), ao2_cleanup);
496
497         if (!chan) {
498                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
499                 return -1;
500         }
501
502         if (!router) {
503                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
504                         ast_channel_name(chan));
505                 return -1;
506         }
507
508         if (!payload) {
509                 return -1;
510         }
511         payload->chan = chan;
512         payload->cmd = cmd;
513         payload->arguments = parse;
514         payload->value = value;
515
516         message = stasis_message_create(cdr_write_message_type(), payload);
517         if (!message) {
518                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
519                         ast_channel_name(chan));
520                 return -1;
521         }
522         stasis_message_router_publish_sync(router, message);
523
524         return 0;
525 }
526
527 static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse,
528                      const char *value)
529 {
530         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
531         RAII_VAR(struct cdr_func_payload *, payload,
532                 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
533         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
534
535         if (!chan) {
536                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
537                 return -1;
538         }
539
540         if (!router) {
541                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
542                         ast_channel_name(chan));
543                 return -1;
544         }
545
546         if (!payload) {
547                 return -1;
548         }
549         payload->chan = chan;
550         payload->cmd = cmd;
551         payload->arguments = parse;
552         payload->value = value;
553
554         message = stasis_message_create(cdr_prop_write_message_type(), payload);
555         if (!message) {
556                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
557                         ast_channel_name(chan));
558                 return -1;
559         }
560         stasis_message_router_publish_sync(router, message);
561
562         return 0;
563 }
564
565 static struct ast_custom_function cdr_function = {
566         .name = "CDR",
567         .read = cdr_read,
568         .write = cdr_write,
569 };
570
571 static struct ast_custom_function cdr_prop_function = {
572         .name = "CDR_PROP",
573         .read = NULL,
574         .write = cdr_prop_write,
575 };
576
577 static int unload_module(void)
578 {
579         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
580         int res = 0;
581
582         if (router) {
583                 stasis_message_router_remove(router, cdr_prop_write_message_type());
584                 stasis_message_router_remove(router, cdr_write_message_type());
585                 stasis_message_router_remove(router, cdr_read_message_type());
586         }
587         STASIS_MESSAGE_TYPE_CLEANUP(cdr_read_message_type);
588         STASIS_MESSAGE_TYPE_CLEANUP(cdr_write_message_type);
589         STASIS_MESSAGE_TYPE_CLEANUP(cdr_prop_write_message_type);
590         res |= ast_custom_function_unregister(&cdr_function);
591         res |= ast_custom_function_unregister(&cdr_prop_function);
592
593         return res;
594 }
595
596 static int load_module(void)
597 {
598         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
599         int res = 0;
600
601         if (!router) {
602                 return AST_MODULE_LOAD_FAILURE;
603         }
604
605         res |= STASIS_MESSAGE_TYPE_INIT(cdr_read_message_type);
606         res |= STASIS_MESSAGE_TYPE_INIT(cdr_write_message_type);
607         res |= STASIS_MESSAGE_TYPE_INIT(cdr_prop_write_message_type);
608         res |= ast_custom_function_register(&cdr_function);
609         res |= ast_custom_function_register(&cdr_prop_function);
610         res |= stasis_message_router_add(router, cdr_prop_write_message_type(),
611                                          cdr_prop_write_callback, NULL);
612         res |= stasis_message_router_add(router, cdr_write_message_type(),
613                                          cdr_write_callback, NULL);
614         res |= stasis_message_router_add(router, cdr_read_message_type(),
615                                          cdr_read_callback, NULL);
616
617         if (res) {
618                 return AST_MODULE_LOAD_FAILURE;
619         }
620         return AST_MODULE_LOAD_SUCCESS;
621 }
622
623 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan functions");