Sat Aug 6 00:39:29 2011

Asterisk developer's documentation


func_cdr.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999-2006, Digium, Inc.
00005  *
00006  * Portions Copyright (C) 2005, Anthony Minessale II
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief  Call Detail Record related dialplan functions
00022  *
00023  * \author Anthony Minessale II 
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 238230 $")
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/types.h>
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/cdr.h"
00042 
00043 enum {
00044    OPT_RECURSIVE = (1 << 0),
00045    OPT_UNPARSED = (1 << 1),
00046    OPT_LAST = (1 << 2),
00047    OPT_SKIPLOCKED = (1 << 3),
00048 } cdr_option_flags;
00049 
00050 AST_APP_OPTIONS(cdr_func_options, {
00051    AST_APP_OPTION('l', OPT_LAST),
00052    AST_APP_OPTION('r', OPT_RECURSIVE),
00053    AST_APP_OPTION('s', OPT_SKIPLOCKED),
00054    AST_APP_OPTION('u', OPT_UNPARSED),
00055 });
00056 
00057 static int cdr_read(struct ast_channel *chan, char *cmd, char *parse,
00058           char *buf, size_t len)
00059 {
00060    char *ret;
00061    struct ast_flags flags = { 0 };
00062    struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00063    AST_DECLARE_APP_ARGS(args,
00064               AST_APP_ARG(variable);
00065               AST_APP_ARG(options);
00066    );
00067 
00068    if (ast_strlen_zero(parse))
00069       return -1;
00070 
00071    if (!cdr)
00072       return -1;
00073 
00074    AST_STANDARD_APP_ARGS(args, parse);
00075 
00076    if (!ast_strlen_zero(args.options))
00077       ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00078 
00079    if (ast_test_flag(&flags, OPT_LAST))
00080       while (cdr->next)
00081          cdr = cdr->next;
00082 
00083    if (ast_test_flag(&flags, OPT_SKIPLOCKED))
00084       while (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED) && cdr->next)
00085          cdr = cdr->next;
00086 
00087    ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
00088              ast_test_flag(&flags, OPT_RECURSIVE),
00089             ast_test_flag(&flags, OPT_UNPARSED));
00090 
00091    return ret ? 0 : -1;
00092 }
00093 
00094 static int cdr_write(struct ast_channel *chan, char *cmd, char *parse,
00095            const char *value)
00096 {
00097    struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00098    struct ast_flags flags = { 0 };
00099    AST_DECLARE_APP_ARGS(args,
00100               AST_APP_ARG(variable);
00101               AST_APP_ARG(options);
00102    );
00103 
00104    if (ast_strlen_zero(parse) || !value || !chan)
00105       return -1;
00106 
00107    if (!cdr)
00108       return -1;
00109 
00110    AST_STANDARD_APP_ARGS(args, parse);
00111 
00112    if (!ast_strlen_zero(args.options))
00113       ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00114 
00115    if (ast_test_flag(&flags, OPT_LAST))
00116       while (cdr->next)
00117          cdr = cdr->next;
00118 
00119    if (!strcasecmp(args.variable, "accountcode"))  /* the 'l' flag doesn't apply to setting the accountcode, userfield, or amaflags */
00120       ast_cdr_setaccount(chan, value);
00121    else if (!strcasecmp(args.variable, "userfield"))
00122       ast_cdr_setuserfield(chan, value);
00123    else if (!strcasecmp(args.variable, "amaflags"))
00124       ast_cdr_setamaflags(chan, value);
00125    else
00126       ast_cdr_setvar(cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
00127       /* No need to worry about the u flag, as all fields for which setting
00128        * 'u' would do anything are marked as readonly. */
00129 
00130    return 0;
00131 }
00132 
00133 static struct ast_custom_function cdr_function = {
00134    .name = "CDR",
00135    .synopsis = "Gets or sets a CDR variable",
00136    .syntax = "CDR(<name>[|options])",
00137    .read = cdr_read,
00138    .write = cdr_write,
00139    .desc =
00140 "Options:\n"
00141 "  'l' uses the most recent CDR on a channel with multiple records\n"
00142 "  'r' searches the entire stack of CDRs on the channel\n"
00143 "  's' skips any CDR's that are marked 'LOCKED' due to forkCDR() calls.\n"
00144 "      (on setting/writing CDR vars only)\n"
00145 "  'u' retrieves the raw, unprocessed value\n"
00146 "  For example, 'start', 'answer', and 'end' will be retrieved as epoch\n"
00147 "  values, when the 'u' option is passed, but formatted as YYYY-MM-DD HH:MM:SS\n"
00148 "  otherwise.  Similarly, disposition and amaflags will return their raw\n"
00149 "  integral values.\n"
00150 "  Here is a list of all the available cdr field names:\n"
00151 "    clid          lastdata       disposition\n"
00152 "    src           start          amaflags\n"
00153 "    dst           answer         accountcode\n"
00154 "    dcontext      end            uniqueid\n"
00155 "    dstchannel    duration       userfield\n"
00156 "    lastapp       billsec        channel\n"
00157 "  All of the above variables are read-only, except for accountcode,\n"
00158 "  userfield, and amaflags. You may, however,  supply\n"
00159 "  a name not on the above list, and create your own\n"
00160 "  variable, whose value can be changed with this function,\n"
00161 "  and this variable will be stored on the cdr.\n"
00162 "  For setting CDR values, the 'l' flag does not apply to\n"
00163 "  setting the accountcode, userfield, or amaflags.\n"
00164 "   raw values for disposition:\n"
00165 "       0 = NO ANSWER\n"
00166 "       1 = NO ANSWER (NULL record)\n"
00167 "       2 = FAILED\n"
00168 "       4 = BUSY\n"
00169 "       8 = ANSWERED\n"
00170 "    raw values for amaflags:\n"
00171 "       1 = OMIT\n"
00172 "       2 = BILLING\n"
00173 "       3 = DOCUMENTATION\n",
00174 };
00175 
00176 static int unload_module(void)
00177 {
00178    return ast_custom_function_unregister(&cdr_function);
00179 }
00180 
00181 static int load_module(void)
00182 {
00183    return ast_custom_function_register(&cdr_function);
00184 }
00185 
00186 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CDR dialplan function");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7