channel: Clear channel flag in error branch.
[asterisk/asterisk.git] / funcs / func_cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999-2006, Digium, Inc.
5  *
6  * Portions Copyright (C) 2005, Anthony Minessale II
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  Call Detail Record related dialplan functions
22  *
23  * \author Anthony Minessale II
24  *
25  * \ingroup functions
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39 #include "asterisk/cdr.h"
40 #include "asterisk/stasis.h"
41 #include "asterisk/stasis_message_router.h"
42
43 /*** DOCUMENTATION
44         <function name="CDR" language="en_US">
45                 <synopsis>
46                         Gets or sets a CDR variable.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="name" required="true">
50                                 <para>CDR field name:</para>
51                                 <enumlist>
52                                         <enum name="clid">
53                                                 <para>Caller ID.</para>
54                                         </enum>
55                                         <enum name="lastdata">
56                                                 <para>Last application arguments.</para>
57                                         </enum>
58                                         <enum name="disposition">
59                                                 <para>The final state of the CDR.</para>
60                                                 <enumlist>
61                                                         <enum name="0">
62                                                                 <para><literal>NO ANSWER</literal></para>
63                                                         </enum>
64                                                         <enum name="1">
65                                                                 <para><literal>NO ANSWER</literal> (NULL record)</para>
66                                                         </enum>
67                                                         <enum name="2">
68                                                                 <para><literal>FAILED</literal></para>
69                                                         </enum>
70                                                         <enum name="4">
71                                                                 <para><literal>BUSY</literal></para>
72                                                         </enum>
73                                                         <enum name="8">
74                                                                 <para><literal>ANSWERED</literal></para>
75                                                         </enum>
76                                                         <enum name="16">
77                                                                 <para><literal>CONGESTION</literal></para>
78                                                         </enum>
79                                                 </enumlist>
80                                         </enum>
81                                         <enum name="src">
82                                                 <para>Source.</para>
83                                         </enum>
84                                         <enum name="start">
85                                                 <para>Time the call started.</para>
86                                         </enum>
87                                         <enum name="amaflags">
88                                                 <para>R/W the Automatic Message Accounting (AMA) flags on the channel.
89                                                 When read from a channel, the integer value will always be returned.
90                                                 When written to a channel, both the string format or integer value
91                                                 is accepted.</para>
92                                                 <enumlist>
93                                                         <enum name="1"><para><literal>OMIT</literal></para></enum>
94                                                         <enum name="2"><para><literal>BILLING</literal></para></enum>
95                                                         <enum name="3"><para><literal>DOCUMENTATION</literal></para></enum>
96                                                 </enumlist>
97                                                 <warning><para>Accessing this setting is deprecated in CDR. Please use the CHANNEL function instead.</para></warning>
98                                         </enum>
99                                         <enum name="dst">
100                                                 <para>Destination.</para>
101                                         </enum>
102                                         <enum name="answer">
103                                                 <para>Time the call was answered.</para>
104                                         </enum>
105                                         <enum name="accountcode">
106                                                 <para>The channel's account code.</para>
107                                                 <warning><para>Accessing this setting is deprecated in CDR. Please use the CHANNEL function instead.</para></warning>
108                                         </enum>
109                                         <enum name="dcontext">
110                                                 <para>Destination context.</para>
111                                         </enum>
112                                         <enum name="end">
113                                                 <para>Time the call ended.</para>
114                                         </enum>
115                                         <enum name="uniqueid">
116                                                 <para>The channel's unique id.</para>
117                                         </enum>
118                                         <enum name="dstchannel">
119                                                 <para>Destination channel.</para>
120                                         </enum>
121                                         <enum name="duration">
122                                                 <para>Duration of the call.</para>
123                                         </enum>
124                                         <enum name="userfield">
125                                                 <para>The channel's user specified field.</para>
126                                         </enum>
127                                         <enum name="lastapp">
128                                                 <para>Last application.</para>
129                                         </enum>
130                                         <enum name="billsec">
131                                                 <para>Duration of the call once it was answered.</para>
132                                         </enum>
133                                         <enum name="channel">
134                                                 <para>Channel name.</para>
135                                         </enum>
136                                         <enum name="sequence">
137                                                 <para>CDR sequence number.</para>
138                                         </enum>
139                                 </enumlist>
140                         </parameter>
141                         <parameter name="options" required="false">
142                                 <optionlist>
143                                         <option name="f">
144                                                 <para>Returns billsec or duration fields as floating point values.</para>
145                                         </option>
146                                         <option name="u">
147                                                 <para>Retrieves the raw, unprocessed value.</para>
148                                                 <para>For example, 'start', 'answer', and 'end' will be retrieved as epoch
149                                                 values, when the <literal>u</literal> option is passed, but formatted as YYYY-MM-DD HH:MM:SS
150                                                 otherwise.  Similarly, disposition and amaflags will return their raw
151                                                 integral values.</para>
152                                         </option>
153                                 </optionlist>
154                         </parameter>
155                 </syntax>
156                 <description>
157                         <para>All of the CDR field names are read-only, except for <literal>accountcode</literal>,
158                         <literal>userfield</literal>, and <literal>amaflags</literal>. You may, however, supply
159                         a name not on the above list, and create your own variable, whose value can be changed
160                         with this function, and this variable will be stored on the CDR.</para>
161                         <note><para>CDRs can only be modified before the bridge between two channels is
162                         torn down. For example, CDRs may not be modified after the <literal>Dial</literal>
163                         application has returned.</para></note>
164                         <para>Example: exten => 1,1,Set(CDR(userfield)=test)</para>
165                 </description>
166         </function>
167         <function name="CDR_PROP" language="en_US">
168                 <synopsis>
169                         Set a property on a channel's CDR.
170                 </synopsis>
171                 <syntax>
172                         <parameter name="name" required="true">
173                                 <para>The property to set on the CDR.</para>
174                                 <enumlist>
175                                         <enum name="party_a">
176                                                 <para>Set this channel as the preferred Party A when
177                                                 channels are associated together.</para>
178                                                 <para>Write-Only</para>
179                                         </enum>
180                                         <enum name="disable">
181                                                 <para>Setting to 1 will disable CDRs for this channel.
182                                                 Setting to 0 will enable CDRs for this channel.</para>
183                                                 <para>Write-Only</para>
184                                         </enum>
185                                 </enumlist>
186                         </parameter>
187                 </syntax>
188                 <description>
189                         <para>This function sets a property on a channel's CDR. Properties
190                         alter the behavior of how the CDR operates for that channel.</para>
191                 </description>
192         </function>
193  ***/
194
195 enum cdr_option_flags {
196         OPT_UNPARSED = (1 << 1),
197         OPT_FLOAT = (1 << 2),
198 };
199
200 AST_APP_OPTIONS(cdr_func_options, {
201         AST_APP_OPTION('f', OPT_FLOAT),
202         AST_APP_OPTION('u', OPT_UNPARSED),
203 });
204
205 struct cdr_func_payload {
206         struct ast_channel *chan;
207         const char *cmd;
208         const char *arguments;
209         const char *value;
210         void *data;
211 };
212
213 struct cdr_func_data {
214         char *buf;
215         size_t len;
216 };
217
218 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_read_message_type);
219 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_write_message_type);
220 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_prop_write_message_type);
221
222 static struct timeval cdr_retrieve_time(struct ast_channel *chan, const char *time_name)
223 {
224         struct timeval time = { 0 };
225         char *value = NULL;
226         char tempbuf[128];
227         long int tv_sec;
228         long int tv_usec;
229
230         if (ast_strlen_zero(ast_channel_name(chan))) {
231                 /* Format request on a dummy channel */
232                 ast_cdr_format_var(ast_channel_cdr(chan), time_name, &value, tempbuf, sizeof(tempbuf), 1);
233         } else {
234                 ast_cdr_getvar(ast_channel_name(chan), time_name, tempbuf, sizeof(tempbuf));
235         }
236
237         /* time.tv_usec is suseconds_t, which could be int or long */
238         if (sscanf(tempbuf, "%ld.%ld", &tv_sec, &tv_usec) == 2) {
239                 time.tv_sec = tv_sec;
240                 time.tv_usec = tv_usec;
241         } else {
242                 ast_log(AST_LOG_WARNING, "Failed to fully extract '%s' from CDR\n", time_name);
243         }
244
245         return time;
246 }
247
248 static void cdr_read_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
249 {
250         struct cdr_func_payload *payload = stasis_message_data(message);
251         struct cdr_func_data *output;
252         char *info;
253         char *value = NULL;
254         struct ast_flags flags = { 0 };
255         char tempbuf[512];
256         AST_DECLARE_APP_ARGS(args,
257                 AST_APP_ARG(variable);
258                 AST_APP_ARG(options);
259         );
260
261         if (cdr_read_message_type() != stasis_message_type(message)) {
262                 return;
263         }
264
265         ast_assert(payload != NULL);
266         output = payload->data;
267         ast_assert(output != NULL);
268
269         if (ast_strlen_zero(payload->arguments)) {
270                 ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable[,option]))\n)",
271                         payload->cmd, payload->cmd);
272                 return;
273         }
274         info = ast_strdupa(payload->arguments);
275         AST_STANDARD_APP_ARGS(args, info);
276
277         if (!ast_strlen_zero(args.options)) {
278                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
279         }
280
281         if (ast_strlen_zero(ast_channel_name(payload->chan))) {
282                 /* Format request on a dummy channel */
283                 ast_cdr_format_var(ast_channel_cdr(payload->chan), args.variable, &value, tempbuf, sizeof(tempbuf), 0);
284                 if (ast_strlen_zero(value)) {
285                         return;
286                 }
287                 ast_copy_string(tempbuf, value, sizeof(tempbuf));
288                 ast_set_flag(&flags, OPT_UNPARSED);
289         } else if (ast_cdr_getvar(ast_channel_name(payload->chan), args.variable, tempbuf, sizeof(tempbuf))) {
290                 return;
291         }
292
293         if (ast_test_flag(&flags, OPT_FLOAT)
294                 && (!strcasecmp("billsec", args.variable) || !strcasecmp("duration", args.variable))) {
295                 struct timeval start = cdr_retrieve_time(payload->chan, !strcasecmp("billsec", args.variable) ? "answer" : "start");
296                 struct timeval finish = cdr_retrieve_time(payload->chan, "end");
297                 double delta;
298
299                 if (ast_tvzero(finish)) {
300                         finish = ast_tvnow();
301                 }
302
303                 if (ast_tvzero(start)) {
304                         delta = 0.0;
305                 } else {
306                         delta = (double)(ast_tvdiff_us(finish, start) / 1000000.0);
307                 }
308                 snprintf(tempbuf, sizeof(tempbuf), "%lf", delta);
309
310         } else if (!ast_test_flag(&flags, OPT_UNPARSED)) {
311                 if (!strcasecmp("start", args.variable)
312                         || !strcasecmp("end", args.variable)
313                         || !strcasecmp("answer", args.variable)) {
314                         struct timeval fmt_time;
315                         struct ast_tm tm;
316                         /* tv_usec is suseconds_t, which could be int or long */
317                         long int tv_sec;
318                         long int tv_usec;
319
320                         if (sscanf(tempbuf, "%ld.%ld", &tv_sec, &tv_usec) != 2) {
321                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
322                                         args.variable, tempbuf, ast_channel_name(payload->chan));
323                                 return;
324                         }
325                         if (tv_sec) {
326                                 fmt_time.tv_sec = tv_sec;
327                                 fmt_time.tv_usec = tv_usec;
328                                 ast_localtime(&fmt_time, &tm, NULL);
329                                 ast_strftime(tempbuf, sizeof(tempbuf), "%Y-%m-%d %T", &tm);
330                         } else {
331                                 tempbuf[0] = '\0';
332                         }
333                 } else if (!strcasecmp("disposition", args.variable)) {
334                         int disposition;
335
336                         if (sscanf(tempbuf, "%8d", &disposition) != 1) {
337                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
338                                         args.variable, tempbuf, ast_channel_name(payload->chan));
339                                 return;
340                         }
341                         snprintf(tempbuf, sizeof(tempbuf), "%s", ast_cdr_disp2str(disposition));
342                 } else if (!strcasecmp("amaflags", args.variable)) {
343                         int amaflags;
344
345                         if (sscanf(tempbuf, "%8d", &amaflags) != 1) {
346                                 ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n",
347                                         args.variable, tempbuf, ast_channel_name(payload->chan));
348                                 return;
349                         }
350                         snprintf(tempbuf, sizeof(tempbuf), "%s", ast_channel_amaflags2string(amaflags));
351                 }
352         }
353
354         ast_copy_string(output->buf, tempbuf, output->len);
355 }
356
357 static void cdr_write_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
358 {
359         struct cdr_func_payload *payload = stasis_message_data(message);
360         struct ast_flags flags = { 0 };
361         AST_DECLARE_APP_ARGS(args,
362                 AST_APP_ARG(variable);
363                 AST_APP_ARG(options);
364         );
365         char *parse;
366
367         if (cdr_write_message_type() != stasis_message_type(message)) {
368                 return;
369         }
370
371         if (!payload) {
372                 return;
373         }
374
375         if (ast_strlen_zero(payload->arguments)) {
376                 ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable)=value)\n)",
377                         payload->cmd, payload->cmd);
378                 return;
379         }
380         if (!payload->value) {
381                 ast_log(AST_LOG_WARNING, "%s requires a value (%s(variable)=value)\n)",
382                         payload->cmd, payload->cmd);
383                 return;
384         }
385         parse = ast_strdupa(payload->arguments);
386         AST_STANDARD_APP_ARGS(args, parse);
387
388         if (!ast_strlen_zero(args.options)) {
389                 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
390         }
391
392         if (!strcasecmp(args.variable, "accountcode")) {
393                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'accountcode' is deprecated. Please use the CHANNEL function instead.\n");
394                 ast_channel_lock(payload->chan);
395                 ast_channel_accountcode_set(payload->chan, payload->value);
396                 ast_channel_unlock(payload->chan);
397         } else if (!strcasecmp(args.variable, "peeraccount")) {
398                 ast_log(AST_LOG_WARNING, "The 'peeraccount' setting is not supported. Please set the 'accountcode' on the appropriate channel using the CHANNEL function.\n");
399         } else if (!strcasecmp(args.variable, "userfield")) {
400                 ast_cdr_setuserfield(ast_channel_name(payload->chan), payload->value);
401         } else if (!strcasecmp(args.variable, "amaflags")) {
402                 ast_log(AST_LOG_WARNING, "Using the CDR function to set 'amaflags' is deprecated. Please use the CHANNEL function instead.\n");
403                 if (isdigit(*payload->value)) {
404                         int amaflags;
405                         sscanf(payload->value, "%30d", &amaflags);
406                         ast_channel_lock(payload->chan);
407                         ast_channel_amaflags_set(payload->chan, amaflags);
408                         ast_channel_unlock(payload->chan);
409                 } else {
410                         ast_channel_lock(payload->chan);
411                         ast_channel_amaflags_set(payload->chan, ast_channel_string2amaflag(payload->value));
412                         ast_channel_unlock(payload->chan);
413                 }
414         } else {
415                 ast_cdr_setvar(ast_channel_name(payload->chan), args.variable, payload->value);
416         }
417         return;
418 }
419
420 static void cdr_prop_write_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
421 {
422         struct cdr_func_payload *payload = stasis_message_data(message);
423         enum ast_cdr_options option;
424         char *parse;
425         AST_DECLARE_APP_ARGS(args,
426                 AST_APP_ARG(variable);
427                 AST_APP_ARG(options);
428         );
429
430         if (cdr_prop_write_message_type() != stasis_message_type(message)) {
431                 return;
432         }
433
434         if (!payload) {
435                 return;
436         }
437
438         if (ast_strlen_zero(payload->arguments)) {
439                 ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable)=value)\n)",
440                         payload->cmd, payload->cmd);
441                 return;
442         }
443         if (ast_strlen_zero(payload->value)) {
444                 ast_log(AST_LOG_WARNING, "%s requires a value (%s(variable)=value)\n)",
445                         payload->cmd, payload->cmd);
446                 return;
447         }
448         parse = ast_strdupa(payload->arguments);
449         AST_STANDARD_APP_ARGS(args, parse);
450
451         if (!strcasecmp("party_a", args.variable)) {
452                 option = AST_CDR_FLAG_PARTY_A;
453         } else if (!strcasecmp("disable", args.variable)) {
454                 option = AST_CDR_FLAG_DISABLE_ALL;
455         } else {
456                 ast_log(AST_LOG_WARNING, "Unknown option %s used with %s\n", args.variable, payload->cmd);
457                 return;
458         }
459
460         if (ast_true(payload->value)) {
461                 ast_cdr_set_property(ast_channel_name(payload->chan), option);
462         } else {
463                 ast_cdr_clear_property(ast_channel_name(payload->chan), option);
464         }
465 }
466
467
468 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
469                     char *buf, size_t len)
470 {
471         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
472         RAII_VAR(struct cdr_func_payload *, payload, NULL, ao2_cleanup);
473         struct cdr_func_data output = { 0, };
474
475         if (!chan) {
476                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
477                 return -1;
478         }
479
480         if (!cdr_read_message_type()) {
481                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: message type not available\n",
482                         ast_channel_name(chan));
483                 return -1;
484         }
485
486         payload = ao2_alloc(sizeof(*payload), NULL);
487         if (!payload) {
488                 return -1;
489         }
490         payload->chan = chan;
491         payload->cmd = cmd;
492         payload->arguments = parse;
493         payload->data = &output;
494
495         buf[0] = '\0';/* Ensure the buffer is initialized. */
496         output.buf = buf;
497         output.len = len;
498
499         message = stasis_message_create(cdr_read_message_type(), payload);
500         if (!message) {
501                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
502                         ast_channel_name(chan));
503                 return -1;
504         }
505
506         /* If this is a request on a dummy channel, we're doing post-processing on an
507          * already dispatched CDR. Simply call the callback to calculate the value and
508          * return, instead of posting to Stasis as we would for a running channel.
509          */
510         if (ast_strlen_zero(ast_channel_name(chan))) {
511                 cdr_read_callback(NULL, NULL, message);
512         } else {
513                 RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
514
515                 if (!router) {
516                         ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
517                                 ast_channel_name(chan));
518                         return -1;
519                 }
520                 stasis_message_router_publish_sync(router, message);
521         }
522
523         return 0;
524 }
525
526 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
527                      const char *value)
528 {
529         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
530         RAII_VAR(struct cdr_func_payload *, payload, NULL, ao2_cleanup);
531         RAII_VAR(struct stasis_message_router *, router,
532                      ast_cdr_message_router(), ao2_cleanup);
533
534         if (!chan) {
535                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
536                 return -1;
537         }
538
539         if (!router) {
540                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
541                         ast_channel_name(chan));
542                 return -1;
543         }
544
545         if (!cdr_write_message_type()) {
546                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: message type not available\n",
547                         ast_channel_name(chan));
548                 return -1;
549         }
550
551         payload = ao2_alloc(sizeof(*payload), NULL);
552         if (!payload) {
553                 return -1;
554         }
555         payload->chan = chan;
556         payload->cmd = cmd;
557         payload->arguments = parse;
558         payload->value = value;
559
560         message = stasis_message_create(cdr_write_message_type(), payload);
561         if (!message) {
562                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
563                         ast_channel_name(chan));
564                 return -1;
565         }
566         stasis_message_router_publish_sync(router, message);
567
568         return 0;
569 }
570
571 static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse,
572                      const char *value)
573 {
574         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
575         RAII_VAR(struct cdr_func_payload *, payload, NULL, ao2_cleanup);
576         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
577
578         if (!chan) {
579                 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
580                 return -1;
581         }
582
583         if (!router) {
584                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
585                         ast_channel_name(chan));
586                 return -1;
587         }
588
589         if (!cdr_write_message_type()) {
590                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: message type not available\n",
591                         ast_channel_name(chan));
592                 return -1;
593         }
594
595         payload = ao2_alloc(sizeof(*payload), NULL);
596         if (!payload) {
597                 return -1;
598         }
599         payload->chan = chan;
600         payload->cmd = cmd;
601         payload->arguments = parse;
602         payload->value = value;
603
604         message = stasis_message_create(cdr_prop_write_message_type(), payload);
605         if (!message) {
606                 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
607                         ast_channel_name(chan));
608                 return -1;
609         }
610         stasis_message_router_publish_sync(router, message);
611
612         return 0;
613 }
614
615 static struct ast_custom_function cdr_function = {
616         .name = "CDR",
617         .read = cdr_read,
618         .write = cdr_write,
619 };
620
621 static struct ast_custom_function cdr_prop_function = {
622         .name = "CDR_PROP",
623         .read = NULL,
624         .write = cdr_prop_write,
625 };
626
627 static int unload_module(void)
628 {
629         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
630         int res = 0;
631
632         if (router) {
633                 stasis_message_router_remove(router, cdr_prop_write_message_type());
634                 stasis_message_router_remove(router, cdr_write_message_type());
635                 stasis_message_router_remove(router, cdr_read_message_type());
636         }
637         STASIS_MESSAGE_TYPE_CLEANUP(cdr_read_message_type);
638         STASIS_MESSAGE_TYPE_CLEANUP(cdr_write_message_type);
639         STASIS_MESSAGE_TYPE_CLEANUP(cdr_prop_write_message_type);
640         res |= ast_custom_function_unregister(&cdr_function);
641         res |= ast_custom_function_unregister(&cdr_prop_function);
642
643         return res;
644 }
645
646 static int load_module(void)
647 {
648         RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
649         int res = 0;
650
651         if (!router) {
652                 return AST_MODULE_LOAD_DECLINE;
653         }
654
655         res |= STASIS_MESSAGE_TYPE_INIT(cdr_read_message_type);
656         res |= STASIS_MESSAGE_TYPE_INIT(cdr_write_message_type);
657         res |= STASIS_MESSAGE_TYPE_INIT(cdr_prop_write_message_type);
658         res |= ast_custom_function_register(&cdr_function);
659         res |= ast_custom_function_register(&cdr_prop_function);
660         res |= stasis_message_router_add(router, cdr_prop_write_message_type(),
661                                          cdr_prop_write_callback, NULL);
662         res |= stasis_message_router_add(router, cdr_write_message_type(),
663                                          cdr_write_callback, NULL);
664         res |= stasis_message_router_add(router, cdr_read_message_type(),
665                                          cdr_read_callback, NULL);
666
667         if (res) {
668                 unload_module();
669                 return AST_MODULE_LOAD_DECLINE;
670         }
671         return AST_MODULE_LOAD_SUCCESS;
672 }
673
674 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan functions");