dc710965984d55c071e0e0a50cda5e3eb122568d
[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>ANSWERED, NO ANSWER, BUSY, FAILED.</para>
60                                         </enum>
61                                         <enum name="src">
62                                                 <para>Source.</para>
63                                         </enum>
64                                         <enum name="start">
65                                                 <para>Time the call started.</para>
66                                         </enum>
67                                         <enum name="amaflags">
68                                                 <para>DOCUMENTATION, BILL, IGNORE, etc.</para>
69                                         </enum>
70                                         <enum name="dst">
71                                                 <para>Destination.</para>
72                                         </enum>
73                                         <enum name="answer">
74                                                 <para>Time the call was answered.</para>
75                                         </enum>
76                                         <enum name="accountcode">
77                                                 <para>The channel's account code.</para>
78                                         </enum>
79                                         <enum name="dcontext">
80                                                 <para>Destination context.</para>
81                                         </enum>
82                                         <enum name="end">
83                                                 <para>Time the call ended.</para>
84                                         </enum>
85                                         <enum name="uniqueid">
86                                                 <para>The channel's unique id.</para>
87                                         </enum>
88                                         <enum name="dstchannel">
89                                                 <para>Destination channel.</para>
90                                         </enum>
91                                         <enum name="duration">
92                                                 <para>Duration of the call.</para>
93                                         </enum>
94                                         <enum name="userfield">
95                                                 <para>The channel's user specified field.</para>
96                                         </enum>
97                                         <enum name="lastapp">
98                                                 <para>Last application.</para>
99                                         </enum>
100                                         <enum name="billsec">
101                                                 <para>Duration of the call once it was answered.</para>
102                                         </enum>
103                                         <enum name="channel">
104                                                 <para>Channel name.</para>
105                                         </enum>
106                                         <enum name="sequence">
107                                                 <para>CDR sequence number.</para>
108                                         </enum>
109                                 </enumlist>
110                         </parameter>
111                         <parameter name="options" required="false">
112                                 <optionlist>
113                                         <option name="f">
114                                                 <para>Returns billsec or duration fields as floating point values.</para>
115                                         </option>
116                                         <option name="l">
117                                                 <para>Uses the most recent CDR on a channel with multiple records</para>
118                                         </option>
119                                         <option name="r">
120                                                 <para>Searches the entire stack of CDRs on the channel.</para>
121                                         </option>
122                                         <option name="s">
123                                                 <para>Skips any CDR's that are marked 'LOCKED' due to forkCDR() calls.
124                                                 (on setting/writing CDR vars only)</para>
125                                         </option>
126                                         <option name="u">
127                                                 <para>Retrieves the raw, unprocessed value.</para>
128                                                 <para>For example, 'start', 'answer', and 'end' will be retrieved as epoch
129                                                 values, when the <literal>u</literal> option is passed, but formatted as YYYY-MM-DD HH:MM:SS
130                                                 otherwise.  Similarly, disposition and amaflags will return their raw
131                                                 integral values.</para>
132                                         </option>
133                                 </optionlist>
134                         </parameter>
135                 </syntax>
136                 <description>
137                         <para>All of the CDR field names are read-only, except for <literal>accountcode</literal>,
138                         <literal>userfield</literal>, and <literal>amaflags</literal>. You may, however, supply
139                         a name not on the above list, and create your own variable, whose value can be changed
140                         with this function, and this variable will be stored on the cdr.</para>
141                         <note><para>For setting CDR values, the <literal>l</literal> flag does not apply to
142                         setting the <literal>accountcode</literal>, <literal>userfield</literal>, or
143                         <literal>amaflags</literal>.</para></note>
144                         <para>Raw values for <literal>disposition</literal>:</para>
145                         <enumlist>
146                                 <enum name="0">
147                                         <para>NO ANSWER</para>
148                                 </enum>
149                                 <enum name="1">
150                                         <para>NO ANSWER (NULL record)</para>
151                                 </enum>
152                                 <enum name="2">
153                                         <para>FAILED</para>
154                                 </enum>
155                                 <enum name="4">
156                                         <para>BUSY</para>
157                                 </enum>
158                                 <enum name="8">
159                                         <para>ANSWERED</para>
160                                 </enum>
161                         </enumlist>
162                         <para>Raw values for <literal>amaflags</literal>:</para>
163                         <enumlist>
164                                 <enum name="1">
165                                         <para>OMIT</para>
166                                 </enum>
167                                 <enum name="2">
168                                         <para>BILLING</para>
169                                 </enum>
170                                 <enum name="3">
171                                         <para>DOCUMENTATION</para>
172                                 </enum>
173                         </enumlist>
174                         <para>Example: exten => 1,1,Set(CDR(userfield)=test)</para>
175                 </description>
176         </function>
177  ***/
178
179 enum cdr_option_flags {
180         OPT_RECURSIVE = (1 << 0),
181         OPT_UNPARSED = (1 << 1),
182         OPT_LAST = (1 << 2),
183         OPT_SKIPLOCKED = (1 << 3),
184         OPT_FLOAT = (1 << 4),
185 };
186
187 AST_APP_OPTIONS(cdr_func_options, {
188         AST_APP_OPTION('f', OPT_FLOAT),
189         AST_APP_OPTION('l', OPT_LAST),
190         AST_APP_OPTION('r', OPT_RECURSIVE),
191         AST_APP_OPTION('s', OPT_SKIPLOCKED),
192         AST_APP_OPTION('u', OPT_UNPARSED),
193 });
194
195 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
196                     char *buf, size_t len)
197 {
198         char *ret;
199         struct ast_flags flags = { 0 };
200         struct ast_cdr *cdr;
201         AST_DECLARE_APP_ARGS(args,
202                              AST_APP_ARG(variable);
203                              AST_APP_ARG(options);
204         );
205
206         if (ast_strlen_zero(parse) || !chan)
207                 return -1;
208
209         ast_channel_lock(chan);
210         cdr = chan->cdr;
211         if (!cdr) {
212                 ast_channel_unlock(chan);
213                 return -1;
214         }
215
216         AST_STANDARD_APP_ARGS(args, parse);
217
218         if (!ast_strlen_zero(args.options))
219                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
220
221         if (ast_test_flag(&flags, OPT_LAST))
222                 while (cdr->next)
223                         cdr = cdr->next;
224
225         if (ast_test_flag(&flags, OPT_SKIPLOCKED))
226                 while (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED) && cdr->next)
227                         cdr = cdr->next;
228
229         if (!strcasecmp("billsec", args.variable) && ast_test_flag(&flags, OPT_FLOAT)) {
230                 if (!ast_tvzero(cdr->answer)) {
231                         double hrtime;
232
233                         if(!ast_tvzero(cdr->end))
234                                 hrtime = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
235                         else
236                                 hrtime = (double)(ast_tvdiff_us(ast_tvnow(), cdr->answer) / 1000000.0);
237
238                         snprintf(buf, len, "%lf", hrtime);
239                 } else {
240                         snprintf(buf, len, "%lf", 0.0);
241                 }
242                 ret = buf;
243         } else if (!strcasecmp("duration", args.variable) && ast_test_flag(&flags, OPT_FLOAT)) {
244                         double hrtime;
245
246                         if(!ast_tvzero(cdr->end))
247                                 hrtime = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
248                         else
249                                 hrtime = (double)(ast_tvdiff_us(ast_tvnow(), cdr->start) / 1000000.0);
250
251                         snprintf(buf, len, "%lf", hrtime);
252
253                 if (!ast_strlen_zero(buf)) {
254                         ret = buf;
255                 }
256         } else {
257                 ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
258                                ast_test_flag(&flags, OPT_RECURSIVE),
259                                    ast_test_flag(&flags, OPT_UNPARSED));
260         }
261
262         ast_channel_unlock(chan);
263         return ret ? 0 : -1;
264 }
265
266 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
267                      const char *value)
268 {
269         struct ast_cdr *cdr;
270         struct ast_flags flags = { 0 };
271         AST_DECLARE_APP_ARGS(args,
272                              AST_APP_ARG(variable);
273                              AST_APP_ARG(options);
274         );
275
276         if (ast_strlen_zero(parse) || !value || !chan)
277                 return -1;
278
279         ast_channel_lock(chan);
280         cdr = chan->cdr;
281         if (!cdr) {
282                 ast_channel_unlock(chan);
283                 return -1;
284         }
285
286         AST_STANDARD_APP_ARGS(args, parse);
287
288         if (!ast_strlen_zero(args.options))
289                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
290
291         if (ast_test_flag(&flags, OPT_LAST))
292                 while (cdr->next)
293                         cdr = cdr->next;
294
295         if (!strcasecmp(args.variable, "accountcode"))  /* the 'l' flag doesn't apply to setting the accountcode, userfield, or amaflags */
296                 ast_cdr_setaccount(chan, value);
297         else if (!strcasecmp(args.variable, "peeraccount"))
298                 ast_cdr_setpeeraccount(chan, value);
299         else if (!strcasecmp(args.variable, "userfield"))
300                 ast_cdr_setuserfield(chan, value);
301         else if (!strcasecmp(args.variable, "amaflags"))
302                 ast_cdr_setamaflags(chan, value);
303         else
304                 ast_cdr_setvar(cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
305                 /* No need to worry about the u flag, as all fields for which setting
306                  * 'u' would do anything are marked as readonly. */
307
308         ast_channel_unlock(chan);
309         return 0;
310 }
311
312 static struct ast_custom_function cdr_function = {
313         .name = "CDR",
314         .read = cdr_read,
315         .write = cdr_write,
316 };
317
318 static int unload_module(void)
319 {
320         return ast_custom_function_unregister(&cdr_function);
321 }
322
323 static int load_module(void)
324 {
325         return ast_custom_function_register(&cdr_function);
326 }
327
328 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan function");