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