Thu Oct 8 21:56:01 2009

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: 103684 $")
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 } cdr_option_flags;
00048 
00049 AST_APP_OPTIONS(cdr_func_options, {
00050    AST_APP_OPTION('l', OPT_LAST),
00051    AST_APP_OPTION('r', OPT_RECURSIVE),
00052    AST_APP_OPTION('u', OPT_UNPARSED),
00053 });
00054 
00055 static int cdr_read(struct ast_channel *chan, char *cmd, char *parse,
00056           char *buf, size_t len)
00057 {
00058    char *ret;
00059    struct ast_flags flags = { 0 };
00060    struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00061    AST_DECLARE_APP_ARGS(args,
00062               AST_APP_ARG(variable);
00063               AST_APP_ARG(options);
00064    );
00065 
00066    if (ast_strlen_zero(parse))
00067       return -1;
00068 
00069    if (!cdr)
00070       return -1;
00071 
00072    AST_STANDARD_APP_ARGS(args, parse);
00073 
00074    if (!ast_strlen_zero(args.options))
00075       ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00076 
00077    if (ast_test_flag(&flags, OPT_LAST))
00078       while (cdr->next)
00079          cdr = cdr->next;
00080 
00081    ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
00082              ast_test_flag(&flags, OPT_RECURSIVE),
00083             ast_test_flag(&flags, OPT_UNPARSED));
00084 
00085    return 0;
00086 }
00087 
00088 static int cdr_write(struct ast_channel *chan, char *cmd, char *parse,
00089            const char *value)
00090 {
00091    struct ast_flags flags = { 0 };
00092    AST_DECLARE_APP_ARGS(args,
00093               AST_APP_ARG(variable);
00094               AST_APP_ARG(options);
00095    );
00096 
00097    if (ast_strlen_zero(parse) || !value || !chan)
00098       return -1;
00099 
00100    AST_STANDARD_APP_ARGS(args, parse);
00101 
00102    if (!ast_strlen_zero(args.options))
00103       ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00104 
00105    if (!strcasecmp(args.variable, "accountcode"))
00106       ast_cdr_setaccount(chan, value);
00107    else if (!strcasecmp(args.variable, "userfield"))
00108       ast_cdr_setuserfield(chan, value);
00109    else if (!strcasecmp(args.variable, "amaflags"))
00110       ast_cdr_setamaflags(chan, value);
00111    else if (chan->cdr)
00112       ast_cdr_setvar(chan->cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
00113       /* No need to worry about the u flag, as all fields for which setting
00114        * 'u' would do anything are marked as readonly. */
00115 
00116    return 0;
00117 }
00118 
00119 static struct ast_custom_function cdr_function = {
00120    .name = "CDR",
00121    .synopsis = "Gets or sets a CDR variable",
00122    .syntax = "CDR(<name>[|options])",
00123    .read = cdr_read,
00124    .write = cdr_write,
00125    .desc =
00126 "Options:\n"
00127 "  'l' uses the most recent CDR on a channel with multiple records\n"
00128 "  'r' searches the entire stack of CDRs on the channel\n"
00129 "  'u' retrieves the raw, unprocessed value\n"
00130 "  For example, 'start', 'answer', and 'end' will be retrieved as epoch\n"
00131 "  values, when the 'u' option is passed, but formatted as YYYY-MM-DD HH:MM:SS\n"
00132 "  otherwise.  Similarly, disposition and amaflags will return their raw\n"
00133 "  integral values.\n"
00134 "  Here is a list of all the available cdr field names:\n"
00135 "    clid          lastdata       disposition\n"
00136 "    src           start          amaflags\n"
00137 "    dst           answer         accountcode\n"
00138 "    dcontext      end            uniqueid\n"
00139 "    dstchannel    duration       userfield\n"
00140 "    lastapp       billsec        channel\n"
00141 "  All of the above variables are read-only, except for accountcode,\n"
00142 "  userfield, and amaflags. You may, however,  supply\n"
00143 "  a name not on the above list, and create your own\n"
00144 "  variable, whose value can be changed with this function,\n"
00145 "  and this variable will be stored on the cdr.\n"
00146 "   raw values for disposition:\n"
00147 "       1 = NO ANSWER\n"
00148 "  2 = BUSY\n"
00149 "  3 = FAILED\n"
00150 "  4 = ANSWERED\n"
00151 "    raw values for amaflags:\n"
00152 "       1 = OMIT\n"
00153 "       2 = BILLING\n"
00154 "       3 = DOCUMENTATION\n",
00155 };
00156 
00157 static int unload_module(void)
00158 {
00159    return ast_custom_function_unregister(&cdr_function);
00160 }
00161 
00162 static int load_module(void)
00163 {
00164    return ast_custom_function_register(&cdr_function);
00165 }
00166 
00167 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CDR dialplan function");

Generated on Thu Oct 8 21:56:01 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6