(closes issue #12846)
[asterisk-bristuff.git] / apps / app_forkcdr.c
blobf8ab05f3fa7f2860fc25708121ab576073c707d6
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Anthony Minessale anthmct@yahoo.com
5 * Development of this app Sponsered/Funded by TAAN Softworks Corp
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.
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.
18 /*! \file
20 * \brief Fork CDR application
22 * \author Anthony Minessale anthmct@yahoo.com
24 * \note Development of this app Sponsored/Funded by TAAN Softworks Corp
26 * \ingroup applications
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/cdr.h"
43 #include "asterisk/app.h"
44 #include "asterisk/module.h"
46 static char *app = "ForkCDR";
47 static char *synopsis =
48 "Forks the Call Data Record";
49 static char *descrip =
50 " ForkCDR([options]): Causes the Call Data Record to fork an additional\n"
51 "cdr record starting from the time of the fork call. This new cdr record will\n"
52 "be linked to end of the list of cdr records attached to the channel. The original CDR is\n"
53 "has a LOCKED flag set, which forces most cdr operations to skip it, except\n"
54 "for the functions that set the answer and end times, which ignore the LOCKED\n"
55 "flag. This allows all the cdr records in the channel to be 'ended' together\n"
56 "when the channel is closed.\n"
57 "The CDR() func (when setting CDR values) normally ignores the LOCKED flag also,\n"
58 "but has options to vary its behavior. The 'T' option (described below), can\n"
59 "override this behavior, but beware the risks.\n"
60 "\n"
61 "Detailed Behavior Description:\n"
62 "First, this app finds the last cdr record in the list, and makes\n"
63 "a copy of it. This new copy will be the newly forked cdr record.\n"
64 "Next, this new record is linked to the end of the cdr record list.\n"
65 "Next, The new cdr record is RESET (unless you use an option to prevent this)\n"
66 "This means that:\n"
67 " 1. All flags are unset on the cdr record\n"
68 " 2. the start, end, and answer times are all set to zero.\n"
69 " 3. the billsec and duration fields are set to zero.\n"
70 " 4. the start time is set to the current time.\n"
71 " 5. the disposition is set to NULL.\n"
72 "Next, unless you specified the 'v' option, all variables will be\n"
73 "removed from the original cdr record. Thus, the 'v' option allows\n"
74 "any CDR variables to be replicated to all new forked cdr records.\n"
75 "Without the 'v' option, the variables on the original are effectively\n"
76 "moved to the new forked cdr record.\n"
77 "Next, if the 's' option is set, the provided variable and value\n"
78 "are set on the original cdr record.\n"
79 "Next, if the 'a' option is given, and the original cdr record has an\n"
80 "answer time set, then the new forked cdr record will have its answer\n"
81 "time set to its start time. If the old answer time were carried forward,\n"
82 "the answer time would be earlier than the start time, giving strange\n"
83 "duration and billsec times.\n"
84 "Next, if the 'd' option was specified, the disposition is copied from\n"
85 "the original cdr record to the new forked cdr.\n"
86 "Next, if the 'D' option was specified, the destination channel field\n"
87 "in the new forked CDR is erased.\n"
88 "Next, if the 'e' option was specified, the 'end' time for the original\n"
89 "cdr record is set to the current time. Future hang-up or ending events\n"
90 "will not override this time stamp.\n"
91 "Next, If the 'A' option is specified, the original cdr record will have\n"
92 "it ANS_LOCKED flag set, which prevent future answer events\n"
93 "from updating the original cdr record's disposition. Normally, an\n"
94 "'ANSWERED' event would mark all cdr records in the chain as 'ANSWERED'.\n"
95 "Next, if the 'T' option is specified, the original cdr record will have\n"
96 "its 'DONT_TOUCH' flag set, which will force the cdr_answer, cdr_end, and\n"
97 "cdr_setvar functions to leave that cdr record alone.\n"
98 "And, last but not least, the original cdr record has its LOCKED flag\n"
99 "set. Almost all internal CDR functions (except for the funcs that set\n"
100 "the end, and answer times, and set a variable) will honor this flag\n"
101 "and leave a LOCKED cdr record alone.\n"
102 "This means that the newly created forked cdr record will affected\n"
103 "by events transpiring within Asterisk, with the previously noted\n"
104 "exceptions.\n"
105 " Options:\n"
106 " a - update the answer time on the NEW CDR just after it's been inited..\n"
107 " The new CDR may have been answered already, the reset that forkcdr.\n"
108 " does will erase the answer time. This will bring it back, but\n"
109 " the answer time will be a copy of the fork/start time. It will.\n"
110 " only do this if the initial cdr was indeed already answered..\n"
111 " A - Lock the original CDR against the answer time being updated.\n"
112 " This will allow the disposition on the original CDR to remain the same.\n"
113 " d - Copy the disposition forward from the old cdr, after the .\n"
114 " init..\n"
115 " D - Clear the dstchannel on the new CDR after reset..\n"
116 " e - end the original CDR. Do this after all the necc. data.\n"
117 " is copied from the original CDR to the new forked CDR..\n"
118 " R - do NOT reset the new cdr..\n"
119 " s(name=val) - Set the CDR var 'name' in the original CDR, with value.\n"
120 " 'val'.\n"
121 " T - Mark the original CDR with a DONT_TOUCH flag. setvar, answer, and end\n"
122 " cdr funcs will obey this flag; normally they don't honor the LOCKED\n"
123 " flag set on the original CDR record.\n"
124 " Beware-- using this flag may cause CDR's not to have their end times\n"
125 " updated! It is suggested that if you specify this flag, you might\n"
126 " wish to use the 'e' flag as well!\n"
127 " v - When the new CDR is forked, it gets a copy of the vars attached\n"
128 " to the current CDR. The vars attached to the original CDR are removed\n"
129 " unless this option is specified.\n";
132 enum {
133 OPT_SETANS = (1 << 0),
134 OPT_SETDISP = (1 << 1),
135 OPT_RESETDEST = (1 << 2),
136 OPT_ENDCDR = (1 << 3),
137 OPT_NORESET = (1 << 4),
138 OPT_KEEPVARS = (1 << 5),
139 OPT_VARSET = (1 << 6),
140 OPT_ANSLOCK = (1 << 7),
141 OPT_DONTOUCH = (1 << 8),
144 enum {
145 OPT_ARG_VARSET = 0,
146 /* note: this entry _MUST_ be the last one in the enum */
147 OPT_ARG_ARRAY_SIZE,
150 AST_APP_OPTIONS(forkcdr_exec_options, {
151 AST_APP_OPTION('a', OPT_SETANS),
152 AST_APP_OPTION('A', OPT_ANSLOCK),
153 AST_APP_OPTION('d', OPT_SETDISP),
154 AST_APP_OPTION('D', OPT_RESETDEST),
155 AST_APP_OPTION('e', OPT_ENDCDR),
156 AST_APP_OPTION('R', OPT_NORESET),
157 AST_APP_OPTION_ARG('s', OPT_VARSET, OPT_ARG_VARSET),
158 AST_APP_OPTION('T', OPT_DONTOUCH),
159 AST_APP_OPTION('v', OPT_KEEPVARS),
162 static void ast_cdr_fork(struct ast_channel *chan, struct ast_flags optflags, char *set)
164 struct ast_cdr *cdr;
165 struct ast_cdr *newcdr;
166 struct ast_flags flags = { AST_CDR_FLAG_KEEP_VARS };
168 cdr = chan->cdr;
170 while (cdr->next)
171 cdr = cdr->next;
173 if (!(newcdr = ast_cdr_dup(cdr)))
174 return;
176 ast_cdr_append(cdr, newcdr);
178 if (!ast_test_flag(&optflags, OPT_NORESET))
179 ast_cdr_reset(newcdr, &flags);
181 if (!ast_test_flag(cdr, AST_CDR_FLAG_KEEP_VARS))
182 ast_cdr_free_vars(cdr, 0);
184 if (!ast_strlen_zero(set)) {
185 char *varname = ast_strdupa(set), *varval;
186 varval = strchr(varname,'=');
187 if (varval) {
188 *varval = 0;
189 varval++;
190 ast_cdr_setvar(cdr, varname, varval, 0);
194 if (ast_test_flag(&optflags, OPT_SETANS) && !ast_tvzero(cdr->answer))
195 newcdr->answer = newcdr->start;
197 if (ast_test_flag(&optflags, OPT_SETDISP))
198 newcdr->disposition = cdr->disposition;
200 if (ast_test_flag(&optflags, OPT_RESETDEST))
201 newcdr->dstchannel[0] = 0;
203 if (ast_test_flag(&optflags, OPT_ENDCDR))
204 ast_cdr_end(cdr);
206 if (ast_test_flag(&optflags, OPT_ANSLOCK))
207 ast_set_flag(cdr, AST_CDR_FLAG_ANSLOCKED);
209 if (ast_test_flag(&optflags, OPT_DONTOUCH))
210 ast_set_flag(cdr, AST_CDR_FLAG_DONT_TOUCH);
212 ast_set_flag(cdr, AST_CDR_FLAG_CHILD | AST_CDR_FLAG_LOCKED);
215 static int forkcdr_exec(struct ast_channel *chan, void *data)
217 int res = 0;
218 struct ast_module_user *u;
219 char *argcopy = NULL;
220 struct ast_flags flags = {0};
221 char *opts[OPT_ARG_ARRAY_SIZE];
222 AST_DECLARE_APP_ARGS(arglist,
223 AST_APP_ARG(options);
226 if (!chan->cdr) {
227 ast_log(LOG_WARNING, "Channel does not have a CDR\n");
228 return 0;
231 u = ast_module_user_add(chan);
233 argcopy = ast_strdupa(data);
235 AST_STANDARD_APP_ARGS(arglist, argcopy);
237 if (!ast_strlen_zero(arglist.options)) {
238 ast_app_parse_options(forkcdr_exec_options, &flags, opts, arglist.options);
239 } else
240 opts[OPT_ARG_VARSET] = 0;
242 if (!ast_strlen_zero(data))
243 ast_set2_flag(chan->cdr, ast_test_flag(&flags, OPT_KEEPVARS), AST_CDR_FLAG_KEEP_VARS);
245 ast_cdr_fork(chan, flags, opts[OPT_ARG_VARSET]);
247 ast_module_user_remove(u);
248 return res;
251 static int unload_module(void)
253 int res;
255 res = ast_unregister_application(app);
257 ast_module_user_hangup_all();
259 return res;
262 static int load_module(void)
264 return ast_register_application(app, forkcdr_exec, synopsis, descrip);
267 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Fork The CDR into 2 separate entities");