492716dcf6d9a40299d6ef3816959608f24e7107
[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                         /* tv_usec is suseconds_t, which could be int or long */
252                         long int tv_usec;
253                         if (sscanf(tempbuf, "%ld.%ld", &fmt_time.tv_sec, &tv_usec) != 2) {
254                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
255                                                 args.variable, tempbuf, ast_channel_name(chan));
256                                 return 0;
257                         }
258                         fmt_time.tv_usec = tv_usec;
259                         ast_localtime(&fmt_time, &tm, NULL);
260                         ast_strftime(tempbuf, sizeof(*tempbuf), "%Y-%m-%d %T", &tm);
261                 } else if (!strcasecmp("disposition", args.variable)) {
262                         int disposition;
263                         if (sscanf(tempbuf, "%8d", &disposition) != 1) {
264                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
265                                                 args.variable, tempbuf, ast_channel_name(chan));
266                                 return 0;
267                         }
268                         sprintf(format_buf, "%s", ast_cdr_disp2str(disposition));
269                         strcpy(tempbuf, format_buf);
270                 } else if (!strcasecmp("amaflags", args.variable)) {
271                         int amaflags;
272                         if (sscanf(tempbuf, "%8d", &amaflags) != 1) {
273                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
274                                                 args.variable, tempbuf, ast_channel_name(chan));
275                                 return 0;
276                         }
277                         sprintf(format_buf, "%s", ast_channel_amaflags2string(amaflags));
278                         strcpy(tempbuf, format_buf);
279                 }
280         }
281
282         ast_copy_string(buf, tempbuf, len);
283         return 0;
284 }
285
286 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
287                      const char *value)
288 {
289         struct ast_flags flags = { 0 };
290         AST_DECLARE_APP_ARGS(args,
291                              AST_APP_ARG(variable);
292                              AST_APP_ARG(options);
293         );
294
295         if (ast_strlen_zero(parse) || !value || !chan)
296                 return -1;
297
298         AST_STANDARD_APP_ARGS(args, parse);
299
300         if (!ast_strlen_zero(args.options))
301                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
302
303         if (!strcasecmp(args.variable, "accountcode")) {
304                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'accountcode' is deprecated. Please use the CHANNEL function instead.\n");
305                 ast_channel_lock(chan);
306                 ast_channel_accountcode_set(chan, value);
307                 ast_channel_unlock(chan);
308         } else if (!strcasecmp(args.variable, "peeraccount")) {
309                 ast_log(AST_LOG_WARNING, "The 'peeraccount' setting is not supported. Please set the 'accountcode' on the appropriate channel using the CHANNEL function.\n");
310         } else if (!strcasecmp(args.variable, "userfield")) {
311                 ast_cdr_setuserfield(ast_channel_name(chan), value);
312         } else if (!strcasecmp(args.variable, "amaflags")) {
313                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'amaflags' is deprecated. Please use the CHANNEL function instead.\n");
314                 if (isdigit(*value)) {
315                         int amaflags;
316                         sscanf(value, "%30d", &amaflags);
317                         ast_channel_lock(chan);
318                         ast_channel_amaflags_set(chan, amaflags);
319                         ast_channel_unlock(chan);
320                 } else {
321                         ast_channel_lock(chan);
322                         ast_channel_amaflags_set(chan, ast_channel_string2amaflag(value));
323                         ast_channel_unlock(chan);
324                 }
325         } else {
326                 ast_cdr_setvar(ast_channel_name(chan), args.variable, value);
327         }
328
329         return 0;
330 }
331
332 static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse,
333                      const char *value)
334 {
335         enum ast_cdr_options option;
336
337         if (!strcasecmp("party_a", cmd)) {
338                 option = AST_CDR_FLAG_PARTY_A;
339         } else if (!strcasecmp("disable", cmd)) {
340                 option = AST_CDR_FLAG_DISABLE_ALL;
341         } else {
342                 ast_log(AST_LOG_WARNING, "Unknown option %s used with CDR_PROP\n", cmd);
343                 return 0;
344         }
345
346         if (ast_true(value)) {
347                 ast_cdr_set_property(ast_channel_name(chan), option);
348         } else {
349                 ast_cdr_clear_property(ast_channel_name(chan), option);
350         }
351         return 0;
352 }
353
354 static struct ast_custom_function cdr_function = {
355         .name = "CDR",
356         .read = cdr_read,
357         .write = cdr_write,
358 };
359
360 static struct ast_custom_function cdr_prop_function = {
361         .name = "CDR_PROP",
362         .read = NULL,
363         .write = cdr_prop_write,
364 };
365
366 static int unload_module(void)
367 {
368         int res = 0;
369
370         res |= ast_custom_function_unregister(&cdr_function);
371         res |= ast_custom_function_unregister(&cdr_prop_function);
372
373         return res;
374 }
375
376 static int load_module(void)
377 {
378         int res = 0;
379
380         res |= ast_custom_function_register(&cdr_function);
381         res |= ast_custom_function_register(&cdr_prop_function);
382
383         return res;
384 }
385
386 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan functions");