Whitespace and curly braces.
[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
43 /*** DOCUMENTATION
44         <function name="CDR" language="en_US">
45                 <synopsis>
46                         Gets or sets a CDR variable.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="name" required="true">
50                                 <para>CDR field name:</para>
51                                 <enumlist>
52                                         <enum name="clid">
53                                                 <para>Caller ID.</para>
54                                         </enum>
55                                         <enum name="lastdata">
56                                                 <para>Last application arguments.</para>
57                                         </enum>
58                                         <enum name="disposition">
59                                                 <para>The final state of the CDR.</para>
60                                                 <enumlist>
61                                                         <enum name="0">
62                                                                 <para><literal>NO ANSWER</literal></para>
63                                                         </enum>
64                                                         <enum name="1">
65                                                                 <para><literal>NO ANSWER</literal> (NULL record)</para>
66                                                         </enum>
67                                                         <enum name="2">
68                                                                 <para><literal>FAILED</literal></para>
69                                                         </enum>
70                                                         <enum name="4">
71                                                                 <para><literal>BUSY</literal></para>
72                                                         </enum>
73                                                         <enum name="8">
74                                                                 <para><literal>ANSWERED</literal></para>
75                                                         </enum>
76                                                         <enum name="16">
77                                                                 <para><literal>CONGESTION</literal></para>
78                                                         </enum>
79                                                 </enumlist>
80                                         </enum>
81                                         <enum name="src">
82                                                 <para>Source.</para>
83                                         </enum>
84                                         <enum name="start">
85                                                 <para>Time the call started.</para>
86                                         </enum>
87                                         <enum name="amaflags">
88                                                 <para>R/W the Automatic Message Accounting (AMA) flags on the channel.
89                                                 When read from a channel, the integer value will always be returned.
90                                                 When written to a channel, both the string format or integer value
91                                                 is accepted.</para>
92                                                 <enumlist>
93                                                         <enum name="1"><para><literal>OMIT</literal></para></enum>
94                                                         <enum name="2"><para><literal>BILLING</literal></para></enum>
95                                                         <enum name="3"><para><literal>DOCUMENTATION</literal></para></enum>
96                                                 </enumlist>
97                                                 <warning><para>Accessing this setting is deprecated in CDR. Please use the CHANNEL function instead.</para></warning>
98                                         </enum>
99                                         <enum name="dst">
100                                                 <para>Destination.</para>
101                                         </enum>
102                                         <enum name="answer">
103                                                 <para>Time the call was answered.</para>
104                                         </enum>
105                                         <enum name="accountcode">
106                                                 <para>The channel's account code.</para>
107                                                 <warning><para>Accessing this setting is deprecated in CDR. Please use the CHANNEL function instead.</para></warning>
108                                         </enum>
109                                         <enum name="dcontext">
110                                                 <para>Destination context.</para>
111                                         </enum>
112                                         <enum name="end">
113                                                 <para>Time the call ended.</para>
114                                         </enum>
115                                         <enum name="uniqueid">
116                                                 <para>The channel's unique id.</para>
117                                         </enum>
118                                         <enum name="dstchannel">
119                                                 <para>Destination channel.</para>
120                                         </enum>
121                                         <enum name="duration">
122                                                 <para>Duration of the call.</para>
123                                         </enum>
124                                         <enum name="userfield">
125                                                 <para>The channel's user specified field.</para>
126                                         </enum>
127                                         <enum name="lastapp">
128                                                 <para>Last application.</para>
129                                         </enum>
130                                         <enum name="billsec">
131                                                 <para>Duration of the call once it was answered.</para>
132                                         </enum>
133                                         <enum name="channel">
134                                                 <para>Channel name.</para>
135                                         </enum>
136                                         <enum name="sequence">
137                                                 <para>CDR sequence number.</para>
138                                         </enum>
139                                 </enumlist>
140                         </parameter>
141                         <parameter name="options" required="false">
142                                 <optionlist>
143                                         <option name="f">
144                                                 <para>Returns billsec or duration fields as floating point values.</para>
145                                         </option>
146                                         <option name="u">
147                                                 <para>Retrieves the raw, unprocessed value.</para>
148                                                 <para>For example, 'start', 'answer', and 'end' will be retrieved as epoch
149                                                 values, when the <literal>u</literal> option is passed, but formatted as YYYY-MM-DD HH:MM:SS
150                                                 otherwise.  Similarly, disposition and amaflags will return their raw
151                                                 integral values.</para>
152                                         </option>
153                                 </optionlist>
154                         </parameter>
155                 </syntax>
156                 <description>
157                         <para>All of the CDR field names are read-only, except for <literal>accountcode</literal>,
158                         <literal>userfield</literal>, and <literal>amaflags</literal>. You may, however, supply
159                         a name not on the above list, and create your own variable, whose value can be changed
160                         with this function, and this variable will be stored on the CDR.</para>
161                         <note><para>CDRs can only be modified before the bridge between two channels is
162                         torn down. For example, CDRs may not be modified after the <literal>Dial</literal>
163                         application has returned.</para></note>
164                         <para>Example: exten => 1,1,Set(CDR(userfield)=test)</para>
165                 </description>
166         </function>
167         <function name="CDR_PROP" language="en_US">
168                 <synopsis>
169                         Set a property on a channel's CDR.
170                 </synopsis>
171                 <syntax>
172                         <parameter name="name" required="true">
173                                 <para>The property to set on the CDR.</para>
174                                 <enumlist>
175                                         <enum name="party_a">
176                                                 <para>Set this channel as the preferred Party A when
177                                                 channels are associated together.</para>
178                                                 <para>Write-Only</para>
179                                         </enum>
180                                         <enum name="disable">
181                                                 <para>Disable CDRs for this channel.</para>
182                                                 <para>Write-Only</para>
183                                         </enum>
184                                 </enumlist>
185                         </parameter>
186                 </syntax>
187                 <description>
188                         <para>This function sets a property on a channel's CDR. Properties
189                         alter the behavior of how the CDR operates for that channel.</para>
190                 </description>
191         </function>
192  ***/
193
194 enum cdr_option_flags {
195         OPT_UNPARSED = (1 << 1),
196         OPT_FLOAT = (1 << 2),
197 };
198
199 AST_APP_OPTIONS(cdr_func_options, {
200         AST_APP_OPTION('f', OPT_FLOAT),
201         AST_APP_OPTION('u', OPT_UNPARSED),
202 });
203
204 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
205                     char *buf, size_t len)
206 {
207         char format_buf[128];
208         char *value = NULL;
209         struct ast_flags flags = { 0 };
210         char tempbuf[512];
211         char *info;
212         AST_DECLARE_APP_ARGS(args,
213                 AST_APP_ARG(variable);
214                 AST_APP_ARG(options);
215         );
216
217         if (!chan) {
218                 return -1;
219         }
220
221         if (ast_strlen_zero(parse)) {
222                 ast_log(AST_LOG_WARNING, "FUNC_CDR requires a variable (FUNC_CDR(variable[,option]))\n)");
223                 return -1;
224         }
225         info = ast_strdupa(parse);
226         AST_STANDARD_APP_ARGS(args, info);
227
228         if (!ast_strlen_zero(args.options)) {
229                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
230         }
231
232         if (ast_strlen_zero(ast_channel_name(chan))) {
233                 /* Format request on a dummy channel */
234                 ast_cdr_format_var(ast_channel_cdr(chan), args.variable, &value, tempbuf, sizeof(tempbuf), 0);
235                 if (ast_strlen_zero(value)) {
236                         return 0;
237                 }
238                 ast_copy_string(tempbuf, value, sizeof(tempbuf));
239                 ast_set_flag(&flags, OPT_UNPARSED);
240         } else if (ast_cdr_getvar(ast_channel_name(chan), args.variable, tempbuf, sizeof(tempbuf))) {
241                 return 0;
242         }
243
244         if (ast_test_flag(&flags, OPT_FLOAT)
245                 && (!strcasecmp("billsec", args.variable) || !strcasecmp("duration", args.variable))) {
246                 long ms;
247                 double dtime;
248
249                 if (sscanf(tempbuf, "%30ld", &ms) != 1) {
250                         ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
251                                 args.variable, tempbuf, ast_channel_name(chan));
252                         return 0;
253                 }
254                 dtime = (double)(ms / 1000.0);
255                 sprintf(tempbuf, "%lf", dtime);
256         } else if (!ast_test_flag(&flags, OPT_UNPARSED)) {
257                 if (!strcasecmp("start", args.variable)
258                         || !strcasecmp("end", args.variable)
259                         || !strcasecmp("answer", args.variable)) {
260                         struct timeval fmt_time;
261                         struct ast_tm tm;
262                         /* tv_usec is suseconds_t, which could be int or long */
263                         long int tv_usec;
264
265                         if (sscanf(tempbuf, "%ld.%ld", &fmt_time.tv_sec, &tv_usec) != 2) {
266                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
267                                         args.variable, tempbuf, ast_channel_name(chan));
268                                 return 0;
269                         }
270                         fmt_time.tv_usec = tv_usec;
271                         ast_localtime(&fmt_time, &tm, NULL);
272                         ast_strftime(tempbuf, sizeof(*tempbuf), "%Y-%m-%d %T", &tm);
273                 } else if (!strcasecmp("disposition", args.variable)) {
274                         int disposition;
275
276                         if (sscanf(tempbuf, "%8d", &disposition) != 1) {
277                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
278                                         args.variable, tempbuf, ast_channel_name(chan));
279                                 return 0;
280                         }
281                         sprintf(format_buf, "%s", ast_cdr_disp2str(disposition));
282                         strcpy(tempbuf, format_buf);
283                 } else if (!strcasecmp("amaflags", args.variable)) {
284                         int amaflags;
285
286                         if (sscanf(tempbuf, "%8d", &amaflags) != 1) {
287                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
288                                         args.variable, tempbuf, ast_channel_name(chan));
289                                 return 0;
290                         }
291                         sprintf(format_buf, "%s", ast_channel_amaflags2string(amaflags));
292                         strcpy(tempbuf, format_buf);
293                 }
294         }
295
296         ast_copy_string(buf, tempbuf, len);
297         return 0;
298 }
299
300 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
301                      const char *value)
302 {
303         struct ast_flags flags = { 0 };
304         AST_DECLARE_APP_ARGS(args,
305                 AST_APP_ARG(variable);
306                 AST_APP_ARG(options);
307         );
308
309         if (ast_strlen_zero(parse) || !value || !chan) {
310                 return -1;
311         }
312
313         AST_STANDARD_APP_ARGS(args, parse);
314
315         if (!ast_strlen_zero(args.options)) {
316                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
317         }
318
319         if (!strcasecmp(args.variable, "accountcode")) {
320                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'accountcode' is deprecated. Please use the CHANNEL function instead.\n");
321                 ast_channel_lock(chan);
322                 ast_channel_accountcode_set(chan, value);
323                 ast_channel_unlock(chan);
324         } else if (!strcasecmp(args.variable, "peeraccount")) {
325                 ast_log(AST_LOG_WARNING, "The 'peeraccount' setting is not supported. Please set the 'accountcode' on the appropriate channel using the CHANNEL function.\n");
326         } else if (!strcasecmp(args.variable, "userfield")) {
327                 ast_cdr_setuserfield(ast_channel_name(chan), value);
328         } else if (!strcasecmp(args.variable, "amaflags")) {
329                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'amaflags' is deprecated. Please use the CHANNEL function instead.\n");
330                 if (isdigit(*value)) {
331                         int amaflags;
332                         sscanf(value, "%30d", &amaflags);
333                         ast_channel_lock(chan);
334                         ast_channel_amaflags_set(chan, amaflags);
335                         ast_channel_unlock(chan);
336                 } else {
337                         ast_channel_lock(chan);
338                         ast_channel_amaflags_set(chan, ast_channel_string2amaflag(value));
339                         ast_channel_unlock(chan);
340                 }
341         } else {
342                 ast_cdr_setvar(ast_channel_name(chan), args.variable, value);
343         }
344
345         return 0;
346 }
347
348 static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse,
349                      const char *value)
350 {
351         enum ast_cdr_options option;
352
353         if (!strcasecmp("party_a", cmd)) {
354                 option = AST_CDR_FLAG_PARTY_A;
355         } else if (!strcasecmp("disable", cmd)) {
356                 option = AST_CDR_FLAG_DISABLE_ALL;
357         } else {
358                 ast_log(AST_LOG_WARNING, "Unknown option %s used with CDR_PROP\n", cmd);
359                 return 0;
360         }
361
362         if (ast_true(value)) {
363                 ast_cdr_set_property(ast_channel_name(chan), option);
364         } else {
365                 ast_cdr_clear_property(ast_channel_name(chan), option);
366         }
367         return 0;
368 }
369
370 static struct ast_custom_function cdr_function = {
371         .name = "CDR",
372         .read = cdr_read,
373         .write = cdr_write,
374 };
375
376 static struct ast_custom_function cdr_prop_function = {
377         .name = "CDR_PROP",
378         .read = NULL,
379         .write = cdr_prop_write,
380 };
381
382 static int unload_module(void)
383 {
384         int res = 0;
385
386         res |= ast_custom_function_unregister(&cdr_function);
387         res |= ast_custom_function_unregister(&cdr_prop_function);
388
389         return res;
390 }
391
392 static int load_module(void)
393 {
394         int res = 0;
395
396         res |= ast_custom_function_register(&cdr_function);
397         res |= ast_custom_function_register(&cdr_prop_function);
398
399         return res;
400 }
401
402 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan functions");