Fri Jul 24 00:40:44 2009

Asterisk developer's documentation


cdr_manager.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2004 - 2005
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Asterisk Call Manager CDR records.
00020  *
00021  * See also
00022  * \arg \ref AstCDR
00023  * \arg \ref AstAMI
00024  * \arg \ref Config_ami
00025  * \ingroup cdr_drivers
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 158135 $")
00031 
00032 #include <time.h>
00033 
00034 #include "asterisk/channel.h"
00035 #include "asterisk/cdr.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/manager.h"
00039 #include "asterisk/config.h"
00040 #include "asterisk/pbx.h"
00041 
00042 #define DATE_FORMAT  "%Y-%m-%d %T"
00043 #define CONF_FILE "cdr_manager.conf"
00044 #define CUSTOM_FIELDS_BUF_SIZE 1024
00045 
00046 static char *name = "cdr_manager";
00047 
00048 static int enablecdr = 0;
00049 struct ast_str *customfields;
00050 
00051 static int manager_log(struct ast_cdr *cdr);
00052 
00053 static int load_config(int reload)
00054 {
00055    char *cat = NULL;
00056    struct ast_config *cfg;
00057    struct ast_variable *v;
00058    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00059    int newenablecdr = 0;
00060 
00061    cfg = ast_config_load(CONF_FILE, config_flags);
00062    if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00063       return 0;
00064 
00065    if (reload && customfields) {
00066       ast_free(customfields);
00067    }
00068    customfields = NULL;
00069 
00070    if (!cfg) {
00071       /* Standard configuration */
00072       ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
00073       if (enablecdr)
00074          ast_cdr_unregister(name);
00075       enablecdr = 0;
00076       return 0;
00077    }
00078 
00079    while ( (cat = ast_category_browse(cfg, cat)) ) {
00080       if (!strcasecmp(cat, "general")) {
00081          v = ast_variable_browse(cfg, cat);
00082          while (v) {
00083             if (!strcasecmp(v->name, "enabled"))
00084                newenablecdr = ast_true(v->value);
00085 
00086             v = v->next;
00087          }
00088       } else if (!strcasecmp(cat, "mappings")) {
00089          customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
00090          v = ast_variable_browse(cfg, cat);
00091          while (v) {
00092             if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
00093                if( (customfields->used + strlen(v->value) + strlen(v->name) + 14) < customfields->len) {
00094                   ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
00095                   ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
00096                } else {
00097                   ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
00098                   break;
00099                }
00100 
00101             }
00102             v = v->next;
00103          }
00104       }
00105    }
00106 
00107    ast_config_destroy(cfg);
00108 
00109    if (enablecdr && !newenablecdr)
00110       ast_cdr_unregister(name);
00111    else if (!enablecdr && newenablecdr)
00112       ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
00113    enablecdr = newenablecdr;
00114 
00115    return 1;
00116 }
00117 
00118 static int manager_log(struct ast_cdr *cdr)
00119 {
00120    struct ast_tm timeresult;
00121    char strStartTime[80] = "";
00122    char strAnswerTime[80] = "";
00123    char strEndTime[80] = "";
00124    char buf[CUSTOM_FIELDS_BUF_SIZE];
00125    struct ast_channel dummy;
00126 
00127    if (!enablecdr)
00128       return 0;
00129 
00130    ast_localtime(&cdr->start, &timeresult, NULL);
00131    ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
00132 
00133    if (cdr->answer.tv_sec) {
00134       ast_localtime(&cdr->answer, &timeresult, NULL);
00135       ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
00136    }
00137 
00138    ast_localtime(&cdr->end, &timeresult, NULL);
00139    ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
00140 
00141    buf[0] = 0;
00142    /* Custom fields handling */
00143    if (customfields != NULL && customfields->used > 0) {
00144       memset(&dummy, 0, sizeof(dummy));
00145       dummy.cdr = cdr;
00146       pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
00147    }
00148 
00149    manager_event(EVENT_FLAG_CDR, "Cdr",
00150        "AccountCode: %s\r\n"
00151        "Source: %s\r\n"
00152        "Destination: %s\r\n"
00153        "DestinationContext: %s\r\n"
00154        "CallerID: %s\r\n"
00155        "Channel: %s\r\n"
00156        "DestinationChannel: %s\r\n"
00157        "LastApplication: %s\r\n"
00158        "LastData: %s\r\n"
00159        "StartTime: %s\r\n"
00160        "AnswerTime: %s\r\n"
00161        "EndTime: %s\r\n"
00162        "Duration: %ld\r\n"
00163        "BillableSeconds: %ld\r\n"
00164        "Disposition: %s\r\n"
00165        "AMAFlags: %s\r\n"
00166        "UniqueID: %s\r\n"
00167        "UserField: %s\r\n"
00168        "%s",
00169        cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
00170        cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
00171        cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
00172        ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
00173 
00174    return 0;
00175 }
00176 
00177 static int unload_module(void)
00178 {
00179    ast_cdr_unregister(name);
00180    if (customfields)
00181       ast_free(customfields);
00182 
00183    return 0;
00184 }
00185 
00186 static int load_module(void)
00187 {
00188    /* Configuration file */
00189    if (!load_config(0))
00190       return AST_MODULE_LOAD_DECLINE;
00191 
00192    return AST_MODULE_LOAD_SUCCESS;
00193 }
00194 
00195 static int reload(void)
00196 {
00197    return load_config(1);
00198 }
00199 
00200 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
00201       .load = load_module,
00202       .unload = unload_module,
00203       .reload = reload,
00204           );

Generated on Fri Jul 24 00:40:44 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7