app_dial and app_queue: Make lock the forwarding channel while taking the channel...
[asterisk/asterisk.git] / apps / app_forkcdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Anthony Minessale anthmct@yahoo.com
5  * Development of this app Sponsered/Funded  by TAAN Softworks Corp
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief Fork CDR application
21  *
22  * \author Anthony Minessale anthmct@yahoo.com
23  *
24  * \note Development of this app Sponsored/Funded by TAAN Softworks Corp
25  * 
26  * \ingroup applications
27  */
28
29 /*** MODULEINFO
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/cdr.h"
41 #include "asterisk/app.h"
42 #include "asterisk/module.h"
43 #include "asterisk/stasis.h"
44 #include "asterisk/stasis_message_router.h"
45
46 /*** DOCUMENTATION
47         <application name="ForkCDR" language="en_US">
48                 <synopsis>
49                         Forks the current Call Data Record for this channel.
50                 </synopsis>
51                 <syntax>
52                         <parameter name="options">
53                                 <optionlist>
54                                         <option name="a">
55                                                 <para>If the channel is answered, set the answer time on
56                                                 the forked CDR to the current time. If this option is
57                                                 not used, the answer time on the forked CDR will be the
58                                                 answer time on the original CDR. If the channel is not
59                                                 answered, this option has no effect.</para>
60                                                 <para>Note that this option is implicitly assumed if the
61                                                 <literal>r</literal> option is used.</para>
62                                         </option>
63                                         <option name="e">
64                                                 <para>End (finalize) the original CDR.</para>
65                                         </option>
66                                         <option name="r">
67                                                 <para>Reset the start and answer times on the forked CDR.
68                                                 This will set the start and answer times (if the channel
69                                                 is answered) to be set to the current time.</para>
70                                                 <para>Note that this option implicitly assumes the
71                                                 <literal>a</literal> option.</para>
72                                         </option>
73                                         <option name="v">
74                                                 <para>Do not copy CDR variables and attributes from the
75                                                 original CDR to the forked CDR.</para>
76                                                 <warning><para>This option has changed. Previously, the
77                                                 variables were removed from the original CDR. This no
78                                                 longer occurs - this option now controls whether or not
79                                                 a forked CDR inherits the variables from the original
80                                                 CDR.</para></warning>
81                                         </option>
82                                 </optionlist>
83                         </parameter>
84                 </syntax>
85                 <description>
86                         <para>Causes the Call Data Record engine to fork a new CDR starting
87                         from the time the application is executed. The forked CDR will be
88                         linked to the end of the CDRs associated with the channel.</para>
89                 </description>
90                 <see-also>
91                         <ref type="function">CDR</ref>
92                         <ref type="application">NoCDR</ref>
93                         <ref type="application">ResetCDR</ref>
94                 </see-also>
95         </application>
96  ***/
97
98 static char *app = "ForkCDR";
99
100 AST_APP_OPTIONS(forkcdr_exec_options, {
101         AST_APP_OPTION('a', AST_CDR_FLAG_SET_ANSWER),
102         AST_APP_OPTION('e', AST_CDR_FLAG_FINALIZE),
103         AST_APP_OPTION('r', AST_CDR_FLAG_RESET),
104         AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS),
105 });
106
107 STASIS_MESSAGE_TYPE_DEFN_LOCAL(forkcdr_message_type);
108
109 /*! \internal \brief Message payload for the Stasis message sent to fork the CDR */
110 struct fork_cdr_message_payload {
111         /*! The name of the channel whose CDR will be forked */
112         const char *channel_name;
113         /*! Option flags that control how the CDR will be forked */
114         struct ast_flags *flags;
115 };
116
117 static void forkcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
118 {
119         struct fork_cdr_message_payload *payload;
120
121         if (stasis_message_type(message) != forkcdr_message_type()) {
122                 return;
123         }
124
125         payload = stasis_message_data(message);
126         if (!payload) {
127                 return;
128         }
129
130         if (ast_cdr_fork(payload->channel_name, payload->flags)) {
131                 ast_log(AST_LOG_WARNING, "Failed to fork CDR for channel %s\n",
132                         payload->channel_name);
133         }
134 }
135
136 static int forkcdr_exec(struct ast_channel *chan, const char *data)
137 {
138         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
139         RAII_VAR(struct fork_cdr_message_payload *, payload, ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
140         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
141
142         char *parse;
143         struct ast_flags flags = { 0, };
144         AST_DECLARE_APP_ARGS(args,
145                 AST_APP_ARG(options);
146         );
147
148         parse = ast_strdupa(data);
149
150         AST_STANDARD_APP_ARGS(args, parse);
151
152         if (!ast_strlen_zero(args.options)) {
153                 ast_app_parse_options(forkcdr_exec_options, &flags, NULL, args.options);
154         }
155
156         if (!payload) {
157                 return -1;
158         }
159
160         if (!router) {
161                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
162                         ast_channel_name(chan));
163                 return -1;
164         }
165
166         payload->channel_name = ast_channel_name(chan);
167         payload->flags = &flags;
168         message = stasis_message_create(forkcdr_message_type(), payload);
169         if (!message) {
170                 ast_log(AST_LOG_WARNING, "Failed to fork CDR for channel %s: unable to create message\n",
171                         ast_channel_name(chan));
172                 return -1;
173         }
174         stasis_message_router_publish_sync(router, message);
175
176         return 0;
177 }
178
179 static int unload_module(void)
180 {
181         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
182
183         if (router) {
184                 stasis_message_router_remove(router, forkcdr_message_type());
185         }
186         STASIS_MESSAGE_TYPE_CLEANUP(forkcdr_message_type);
187         ast_unregister_application(app);
188         return 0;
189 }
190
191 static int load_module(void)
192 {
193         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
194         int res = 0;
195
196         if (!router) {
197                 return AST_MODULE_LOAD_FAILURE;
198         }
199
200         res |= STASIS_MESSAGE_TYPE_INIT(forkcdr_message_type);
201         res |= ast_register_application_xml(app, forkcdr_exec);
202         res |= stasis_message_router_add(router, forkcdr_message_type(),
203                                          forkcdr_callback, NULL);
204
205         if (res) {
206                 return AST_MODULE_LOAD_FAILURE;
207         }
208         return AST_MODULE_LOAD_SUCCESS;
209 }
210
211 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Fork The CDR into 2 separate entities");