Debian Codegraph @

apps/app_cdr.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Martin Pycko <[email protected]>
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 <[email protected]>
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="ResetCDR" language="en_US">
42 <since>
43 <version>12.0.0</version>
44 </since>
45 <synopsis>
46 Resets the Call Data Record.
47 </synopsis>
48 <syntax>
49 <parameter name="options">
50 <optionlist>
51 <option name="v">
52 <para>Save the CDR variables during the reset.</para>
53 </option>
54 </optionlist>
55 </parameter>
56 </syntax>
57 <description>
58 <para>This application causes the Call Data Record to be reset.
59 Depending on the flags passed in, this can have several effects.
60 With no options, a reset does the following:</para>
61 <para>1. The <literal>start</literal> time is set to the current time.</para>
62 <para>2. If the channel is answered, the <literal>answer</literal> time is set to the
63 current time.</para>
64 <para>3. All variables are wiped from the CDR. Note that this step
65 can be prevented with the <literal>v</literal> option.</para>
66 </description>
67 <see-also>
68 <ref type="application">ForkCDR</ref>
69 <ref type="function">CDR_PROP</ref>
70 </see-also>
71 </application>
72 ***/
73
74 static const char resetcdr_app[] = "ResetCDR";
75
76 enum reset_cdr_options {
77 OPT_DISABLE_DISPATCH = (1 << 0),
78 OPT_KEEP_VARS = (1 << 1),
79 OPT_ENABLE = (1 << 2),
80 };
81
82 AST_APP_OPTIONS(resetcdr_opts, {
83 AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS),
84 });
85
86 STASIS_MESSAGE_TYPE_DEFN_LOCAL(appcdr_message_type);
87
88 /*! \internal \brief Payload for the Stasis message sent to manipulate a CDR */
89 struct app_cdr_message_payload {
90 /*! The name of the channel to be manipulated */
91 const char *channel_name;
92 /*! Reset the CDR */
93 unsigned int reset:1;
94 /*! If resetting the CDR, keep the variables */
95 unsigned int keep_variables:1;
96 };
97
98 static void appcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
99 {
100 struct app_cdr_message_payload *payload;
101
102 if (stasis_message_type(message) != appcdr_message_type()) {
103 return;
104 }
105
106 payload = stasis_message_data(message);
107 if (!payload) {
108 return;
109 }
110
111 if (payload->reset) {
112 if (ast_cdr_reset(payload->channel_name, payload->keep_variables)) {
113 ast_log(AST_LOG_WARNING, "Failed to reset CDRs on channel %s\n", payload->channel_name);
114 }
115 }
116 }
117
118 static int publish_app_cdr_message(struct ast_channel *chan, struct app_cdr_message_payload *payload)
119 {
120 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
121 RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
122
123 if (!router) {
124 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
125 ast_channel_name(chan));
126 return -1;
127 }
128
129 message = stasis_message_create(appcdr_message_type(), payload);
130 if (!message) {
131 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
132 payload->channel_name);
133 return -1;
134 }
135 stasis_message_router_publish_sync(router, message);
136
137 return 0;
138 }
139
140 static int resetcdr_exec(struct ast_channel *chan, const char *data)
141 {
142 RAII_VAR(struct app_cdr_message_payload *, payload,
143 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
144 char *args;
145 struct ast_flags flags = { 0 };
146
147 if (!payload) {
148 return -1;
149 }
150
151 if (!ast_strlen_zero(data)) {
152 args = ast_strdupa(data);
153 ast_app_parse_options(resetcdr_opts, &flags, NULL, args);
154 }
155
156 payload->channel_name = ast_channel_name(chan);
157 payload->reset = 1;
158
159 if (ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
160 payload->keep_variables = 1;
161 }
162
163 return publish_app_cdr_message(chan, payload);
164 }
165
166 static int unload_module(void)
167 {
168 RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
169
170 if (router) {
171 stasis_message_router_remove(router, appcdr_message_type());
172 }
173 STASIS_MESSAGE_TYPE_CLEANUP(appcdr_message_type);
174 ast_unregister_application(resetcdr_app);
175 return 0;
176 }
177
178 static int load_module(void)
179 {
180 RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
181 int res = 0;
182
183 if (!router) {
184 return AST_MODULE_LOAD_DECLINE;
185 }
186
187 res |= STASIS_MESSAGE_TYPE_INIT(appcdr_message_type);
188 res |= ast_register_application_xml(resetcdr_app, resetcdr_exec);
189 res |= stasis_message_router_add(router, appcdr_message_type(), appcdr_callback, NULL);
190
191 if (res) {
192 unload_module();
193 return AST_MODULE_LOAD_DECLINE;
194 }
195 return AST_MODULE_LOAD_SUCCESS;
196 }
197
198 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Tell Asterisk to not maintain a CDR for the current call");