0116eee7fa5644cf03f429d6a07f67e46124598d
[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 (!payload) {
445                 return -1;
446         }
447         payload->chan = chan;
448         payload->cmd = cmd;
449         payload->arguments = parse;
450         payload->data = &output;
451
452         buf[0] = '\0';/* Ensure the buffer is initialized. */
453         output.buf = buf;
454         output.len = len;
455
456         message = stasis_message_create(cdr_read_message_type(), payload);
457         if (!message) {
458                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
459                         ast_channel_name(chan));
460                 return -1;
461         }
462
463         /* If this is a request on a dummy channel, we're doing post-processing on an
464          * already dispatched CDR. Simply call the callback to calculate the value and
465          * return, instead of posting to Stasis as we would for a running channel.
466          */
467         if (ast_strlen_zero(ast_channel_name(chan))) {
468                 cdr_read_callback(NULL, NULL, message);
469         } else {
470                 RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
471
472                 if (!router) {
473                         ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
474                                 ast_channel_name(chan));
475                         return -1;
476                 }
477                 stasis_message_router_publish_sync(router, message);
478         }
479
480         return 0;
481 }
482
483 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
484                      const char *value)
485 {
486         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
487         RAII_VAR(struct cdr_func_payload *, payload,
488                      ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
489         RAII_VAR(struct stasis_message_router *, router,
490                      ast_cdr_message_router(), ao2_cleanup);
491
492         if (!router) {
493                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
494                         ast_channel_name(chan));
495                 return -1;
496         }
497
498         if (!payload) {
499                 return -1;
500         }
501         payload->chan = chan;
502         payload->cmd = cmd;
503         payload->arguments = parse;
504         payload->value = value;
505
506         message = stasis_message_create(cdr_write_message_type(), payload);
507         if (!message) {
508                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
509                         ast_channel_name(chan));
510                 return -1;
511         }
512         stasis_message_router_publish_sync(router, message);
513
514         return 0;
515 }
516
517 static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse,
518                      const char *value)
519 {
520         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
521         RAII_VAR(struct cdr_func_payload *, payload,
522                 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
523         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
524
525         if (!router) {
526                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
527                         ast_channel_name(chan));
528                 return -1;
529         }
530
531         if (!payload) {
532                 return -1;
533         }
534         payload->chan = chan;
535         payload->cmd = cmd;
536         payload->arguments = parse;
537         payload->value = value;
538
539         message = stasis_message_create(cdr_prop_write_message_type(), payload);
540         if (!message) {
541                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
542                         ast_channel_name(chan));
543                 return -1;
544         }
545         stasis_message_router_publish_sync(router, message);
546
547         return 0;
548 }
549
550 static struct ast_custom_function cdr_function = {
551         .name = "CDR",
552         .read = cdr_read,
553         .write = cdr_write,
554 };
555
556 static struct ast_custom_function cdr_prop_function = {
557         .name = "CDR_PROP",
558         .read = NULL,
559         .write = cdr_prop_write,
560 };
561
562 static int unload_module(void)
563 {
564         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
565         int res = 0;
566
567         if (router) {
568                 stasis_message_router_remove(router, cdr_prop_write_message_type());
569                 stasis_message_router_remove(router, cdr_write_message_type());
570                 stasis_message_router_remove(router, cdr_read_message_type());
571         }
572         STASIS_MESSAGE_TYPE_CLEANUP(cdr_read_message_type);
573         STASIS_MESSAGE_TYPE_CLEANUP(cdr_write_message_type);
574         STASIS_MESSAGE_TYPE_CLEANUP(cdr_prop_write_message_type);
575         res |= ast_custom_function_unregister(&cdr_function);
576         res |= ast_custom_function_unregister(&cdr_prop_function);
577
578         return res;
579 }
580
581 static int load_module(void)
582 {
583         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
584         int res = 0;
585
586         if (!router) {
587                 return AST_MODULE_LOAD_FAILURE;
588         }
589
590         res |= STASIS_MESSAGE_TYPE_INIT(cdr_read_message_type);
591         res |= STASIS_MESSAGE_TYPE_INIT(cdr_write_message_type);
592         res |= STASIS_MESSAGE_TYPE_INIT(cdr_prop_write_message_type);
593         res |= ast_custom_function_register(&cdr_function);
594         res |= ast_custom_function_register(&cdr_prop_function);
595         res |= stasis_message_router_add(router, cdr_prop_write_message_type(),
596                                          cdr_prop_write_callback, NULL);
597         res |= stasis_message_router_add(router, cdr_write_message_type(),
598                                          cdr_write_callback, NULL);
599         res |= stasis_message_router_add(router, cdr_read_message_type(),
600                                          cdr_read_callback, NULL);
601
602         if (res) {
603                 return AST_MODULE_LOAD_FAILURE;
604         }
605         return AST_MODULE_LOAD_SUCCESS;
606 }
607
608 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan functions");