git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / apps / app_cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Martin Pycko <martinp@digium.com>
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 Applications connected with CDR engine
22  *
23  * \author Martin Pycko <martinp@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_REGISTER_FILE()
35
36 #include "asterisk/channel.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39 #include "asterisk/stasis.h"
40 #include "asterisk/stasis_message_router.h"
41
42 /*** DOCUMENTATION
43         <application name="NoCDR" language="en_US">
44                 <synopsis>
45                         Tell Asterisk to not maintain a CDR for this channel.
46                 </synopsis>
47                 <syntax />
48                 <description>
49                         <para>This application will tell Asterisk not to maintain a CDR for
50                         the current channel. This does <emphasis>NOT</emphasis> mean that
51                         information is not tracked; rather, if the channel is hung up no
52                         CDRs will be created for that channel.</para>
53                         <para>If a subsequent call to ResetCDR occurs, all non-finalized
54                         CDRs created for the channel will be enabled.</para>
55                         <note><para>This application is deprecated. Please use the CDR_PROP
56                         function to disable CDRs on a channel.</para></note>
57                 </description>
58                 <see-also>
59                         <ref type="application">ResetCDR</ref>
60                         <ref type="function">CDR_PROP</ref>
61                 </see-also>
62         </application>
63         <application name="ResetCDR" language="en_US">
64                 <synopsis>
65                         Resets the Call Data Record.
66                 </synopsis>
67                 <syntax>
68                         <parameter name="options">
69                                 <optionlist>
70                                         <option name="v">
71                                                 <para>Save the CDR variables during the reset.</para>
72                                         </option>
73                                         <option name="e">
74                                                 <para>Enable the CDRs for this channel only (negate
75                                                 effects of NoCDR).</para>
76                                         </option>
77                                 </optionlist>
78                         </parameter>
79                 </syntax>
80                 <description>
81                         <para>This application causes the Call Data Record to be reset.
82                         Depending on the flags passed in, this can have several effects.
83                         With no options, a reset does the following:</para>
84                         <para>1. The <literal>start</literal> time is set to the current time.</para>
85                         <para>2. If the channel is answered, the <literal>answer</literal> time is set to the
86                         current time.</para>
87                         <para>3. All variables are wiped from the CDR. Note that this step
88                         can be prevented with the <literal>v</literal> option.</para>
89                         <para>On the other hand, if the <literal>e</literal> option is
90                         specified, the effects of the NoCDR application will be lifted. CDRs
91                         will be re-enabled for this channel.</para>
92                         <note><para>The <literal>e</literal> option is deprecated. Please
93                         use the CDR_PROP function instead.</para></note>
94                 </description>
95                 <see-also>
96                         <ref type="application">ForkCDR</ref>
97                         <ref type="application">NoCDR</ref>
98                         <ref type="function">CDR_PROP</ref>
99                 </see-also>
100         </application>
101  ***/
102
103 static const char nocdr_app[] = "NoCDR";
104 static const char resetcdr_app[] = "ResetCDR";
105
106 enum reset_cdr_options {
107         OPT_DISABLE_DISPATCH = (1 << 0),
108         OPT_KEEP_VARS = (1 << 1),
109         OPT_ENABLE = (1 << 2),
110 };
111
112 AST_APP_OPTIONS(resetcdr_opts, {
113         AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS),
114         AST_APP_OPTION('e', AST_CDR_FLAG_DISABLE_ALL),
115 });
116
117 STASIS_MESSAGE_TYPE_DEFN_LOCAL(appcdr_message_type);
118
119 /*! \internal \brief Payload for the Stasis message sent to manipulate a CDR */
120 struct app_cdr_message_payload {
121         /*! The name of the channel to be manipulated */
122         const char *channel_name;
123         /*! Disable the CDR for this channel */
124         int disable:1;
125         /*! Re-enable the CDR for this channel */
126         int reenable:1;
127         /*! Reset the CDR */
128         int reset:1;
129         /*! If reseting the CDR, keep the variables */
130         int keep_variables:1;
131 };
132
133 static void appcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
134 {
135         struct app_cdr_message_payload *payload;
136
137         if (stasis_message_type(message) != appcdr_message_type()) {
138                 return;
139         }
140
141         payload = stasis_message_data(message);
142         if (!payload) {
143                 return;
144         }
145
146         if (payload->disable) {
147                 if (ast_cdr_set_property(payload->channel_name, AST_CDR_FLAG_DISABLE_ALL)) {
148                         ast_log(AST_LOG_WARNING, "Failed to disable CDRs on channel %s\n",
149                                 payload->channel_name);
150                 }
151         }
152
153         if (payload->reenable) {
154                 if (ast_cdr_clear_property(payload->channel_name, AST_CDR_FLAG_DISABLE_ALL)) {
155                         ast_log(AST_LOG_WARNING, "Failed to enable CDRs on channel %s\n",
156                                 payload->channel_name);
157                 }
158         }
159
160         if (payload->reset) {
161                 if (ast_cdr_reset(payload->channel_name, payload->keep_variables)) {
162                         ast_log(AST_LOG_WARNING, "Failed to reset CDRs on channel %s\n",
163                                 payload->channel_name);
164                 }
165         }
166 }
167
168 static int publish_app_cdr_message(struct ast_channel *chan, struct app_cdr_message_payload *payload)
169 {
170         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
171         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
172
173         if (!router) {
174                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
175                         ast_channel_name(chan));
176                 return -1;
177         }
178
179         message = stasis_message_create(appcdr_message_type(), payload);
180         if (!message) {
181                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
182                         payload->channel_name);
183                 return -1;
184         }
185         stasis_message_router_publish_sync(router, message);
186
187         return 0;
188 }
189
190 static int resetcdr_exec(struct ast_channel *chan, const char *data)
191 {
192         RAII_VAR(struct app_cdr_message_payload *, payload,
193                 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
194         char *args;
195         struct ast_flags flags = { 0 };
196
197         if (!payload) {
198                 return -1;
199         }
200
201         if (!ast_strlen_zero(data)) {
202                 args = ast_strdupa(data);
203                 ast_app_parse_options(resetcdr_opts, &flags, NULL, args);
204         }
205
206         payload->channel_name = ast_channel_name(chan);
207         payload->reset = 1;
208
209         if (ast_test_flag(&flags, AST_CDR_FLAG_DISABLE_ALL)) {
210                 payload->reenable = 1;
211         }
212
213         if (ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
214                 payload->keep_variables = 1;
215         }
216
217         return publish_app_cdr_message(chan, payload);
218 }
219
220 static int nocdr_exec(struct ast_channel *chan, const char *data)
221 {
222         RAII_VAR(struct app_cdr_message_payload *, payload,
223                 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
224
225         if (!payload) {
226                 return -1;
227         }
228
229         payload->channel_name = ast_channel_name(chan);
230         payload->disable = 1;
231
232         return publish_app_cdr_message(chan, payload);
233 }
234
235 static int unload_module(void)
236 {
237         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
238
239         if (router) {
240                 stasis_message_router_remove(router, appcdr_message_type());
241         }
242         STASIS_MESSAGE_TYPE_CLEANUP(appcdr_message_type);
243         ast_unregister_application(nocdr_app);
244         ast_unregister_application(resetcdr_app);
245         return 0;
246 }
247
248 static int load_module(void)
249 {
250         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
251         int res = 0;
252
253         if (!router) {
254                 return AST_MODULE_LOAD_FAILURE;
255         }
256
257         res |= STASIS_MESSAGE_TYPE_INIT(appcdr_message_type);
258         res |= ast_register_application_xml(nocdr_app, nocdr_exec);
259         res |= ast_register_application_xml(resetcdr_app, resetcdr_exec);
260         res |= stasis_message_router_add(router, appcdr_message_type(),
261                                          appcdr_callback, NULL);
262
263         if (res) {
264                 return AST_MODULE_LOAD_FAILURE;
265         }
266         return AST_MODULE_LOAD_SUCCESS;
267 }
268
269 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Tell Asterisk to not maintain a CDR for the current call");