chan_vpb: Fix compile error and __ast_channel_alloc() prototype const inconsistency.
[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         struct ast_flags flags = { 0 };
209         char tempbuf[128];
210         char *info;
211         AST_DECLARE_APP_ARGS(args,
212                              AST_APP_ARG(variable);
213                              AST_APP_ARG(options);
214         );
215
216         if (!chan) {
217                 return -1;
218         }
219
220         if (ast_strlen_zero(parse)) {
221                 ast_log(AST_LOG_WARNING, "FUNC_CDR requires a variable (FUNC_CDR(variable[,option]))\n)");
222                 return -1;
223         }
224         info = ast_strdupa(parse);
225         AST_STANDARD_APP_ARGS(args, info);
226
227         if (!ast_strlen_zero(args.options)) {
228                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
229         }
230
231         if (ast_cdr_getvar(ast_channel_name(chan), args.variable, tempbuf, sizeof(tempbuf))) {
232                 return 0;
233         }
234
235         if (ast_test_flag(&flags, OPT_FLOAT) && (!strcasecmp("billsec", args.variable) || !strcasecmp("duration", args.variable))) {
236                 long ms;
237                 double dtime;
238                 if (sscanf(tempbuf, "%30ld", &ms) != 1) {
239                         ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
240                                         args.variable, tempbuf, ast_channel_name(chan));
241                         return 0;
242                 }
243                 dtime = (double)(ms / 1000.0);
244                 sprintf(tempbuf, "%lf", dtime);
245         } else if (!ast_test_flag(&flags, OPT_UNPARSED)) {
246                 if (!strcasecmp("start", args.variable)
247                                 || !strcasecmp("end", args.variable)
248                                 || !strcasecmp("answer", args.variable)) {
249                         struct timeval fmt_time;
250                         struct ast_tm tm;
251                         if (sscanf(tempbuf, "%ld.%ld", &fmt_time.tv_sec, &fmt_time.tv_usec) != 2) {
252                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
253                                                 args.variable, tempbuf, ast_channel_name(chan));
254                                 return 0;
255                         }
256                         ast_localtime(&fmt_time, &tm, NULL);
257                         ast_strftime(tempbuf, sizeof(*tempbuf), "%Y-%m-%d %T", &tm);
258                 } else if (!strcasecmp("disposition", args.variable)) {
259                         int disposition;
260                         if (sscanf(tempbuf, "%8d", &disposition) != 1) {
261                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
262                                                 args.variable, tempbuf, ast_channel_name(chan));
263                                 return 0;
264                         }
265                         sprintf(format_buf, "%s", ast_cdr_disp2str(disposition));
266                         strcpy(tempbuf, format_buf);
267                 } else if (!strcasecmp("amaflags", args.variable)) {
268                         int amaflags;
269                         if (sscanf(tempbuf, "%8d", &amaflags) != 1) {
270                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
271                                                 args.variable, tempbuf, ast_channel_name(chan));
272                                 return 0;
273                         }
274                         sprintf(format_buf, "%s", ast_channel_amaflags2string(amaflags));
275                         strcpy(tempbuf, format_buf);
276                 }
277         }
278
279         ast_copy_string(buf, tempbuf, len);
280         return 0;
281 }
282
283 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
284                      const char *value)
285 {
286         struct ast_flags flags = { 0 };
287         AST_DECLARE_APP_ARGS(args,
288                              AST_APP_ARG(variable);
289                              AST_APP_ARG(options);
290         );
291
292         if (ast_strlen_zero(parse) || !value || !chan)
293                 return -1;
294
295         AST_STANDARD_APP_ARGS(args, parse);
296
297         if (!ast_strlen_zero(args.options))
298                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
299
300         if (!strcasecmp(args.variable, "accountcode")) {
301                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'accountcode' is deprecated. Please use the CHANNEL function instead.\n");
302                 ast_channel_lock(chan);
303                 ast_channel_accountcode_set(chan, value);
304                 ast_channel_unlock(chan);
305         } else if (!strcasecmp(args.variable, "peeraccount")) {
306                 ast_log(AST_LOG_WARNING, "The 'peeraccount' setting is not supported. Please set the 'accountcode' on the appropriate channel using the CHANNEL function.\n");
307         } else if (!strcasecmp(args.variable, "userfield")) {
308                 ast_cdr_setuserfield(ast_channel_name(chan), value);
309         } else if (!strcasecmp(args.variable, "amaflags")) {
310                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'amaflags' is deprecated. Please use the CHANNEL function instead.\n");
311                 if (isdigit(*value)) {
312                         int amaflags;
313                         sscanf(value, "%30d", &amaflags);
314                         ast_channel_lock(chan);
315                         ast_channel_amaflags_set(chan, amaflags);
316                         ast_channel_unlock(chan);
317                 } else {
318                         ast_channel_lock(chan);
319                         ast_channel_amaflags_set(chan, ast_channel_string2amaflag(value));
320                         ast_channel_unlock(chan);
321                 }
322         } else {
323                 ast_cdr_setvar(ast_channel_name(chan), args.variable, value);
324         }
325
326         return 0;
327 }
328
329 static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse,
330                      const char *value)
331 {
332         enum ast_cdr_options option;
333
334         if (!strcasecmp("party_a", cmd)) {
335                 option = AST_CDR_FLAG_PARTY_A;
336         } else if (!strcasecmp("disable", cmd)) {
337                 option = AST_CDR_FLAG_DISABLE_ALL;
338         } else {
339                 ast_log(AST_LOG_WARNING, "Unknown option %s used with CDR_PROP\n", cmd);
340                 return 0;
341         }
342
343         if (ast_true(value)) {
344                 ast_cdr_set_property(ast_channel_name(chan), option);
345         } else {
346                 ast_cdr_clear_property(ast_channel_name(chan), option);
347         }
348         return 0;
349 }
350
351 static struct ast_custom_function cdr_function = {
352         .name = "CDR",
353         .read = cdr_read,
354         .write = cdr_write,
355 };
356
357 static struct ast_custom_function cdr_prop_function = {
358         .name = "CDR_PROP",
359         .read = NULL,
360         .write = cdr_prop_write,
361 };
362
363 static int unload_module(void)
364 {
365         int res = 0;
366
367         res |= ast_custom_function_unregister(&cdr_function);
368         res |= ast_custom_function_unregister(&cdr_prop_function);
369
370         return res;
371 }
372
373 static int load_module(void)
374 {
375         int res = 0;
376
377         res |= ast_custom_function_register(&cdr_function);
378         res |= ast_custom_function_register(&cdr_prop_function);
379
380         return res;
381 }
382
383 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan functions");