mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-10-31 10:47:18 +00:00 
			
		
		
		
	git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@70122 65c4cc65-6c06-0410-ace0-fbb531ad65f3
		
			
				
	
	
		
			207 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			207 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|  * Asterisk -- An open source telephony toolkit.
 | |
|  *
 | |
|  * Copyright (C) 2004 - 2005
 | |
|  *
 | |
|  * See http://www.asterisk.org for more information about
 | |
|  * the Asterisk project. Please do not directly contact
 | |
|  * any of the maintainers of this project for assistance;
 | |
|  * the project provides a web site, mailing lists and IRC
 | |
|  * channels for your use.
 | |
|  *
 | |
|  * This program is free software, distributed under the terms of
 | |
|  * the GNU General Public License Version 2. See the LICENSE file
 | |
|  * at the top of the source tree.
 | |
|  */
 | |
| 
 | |
| /*! \file
 | |
|  *
 | |
|  * \brief Asterisk Call Manager CDR records.
 | |
|  * 
 | |
|  * See also
 | |
|  * \arg \ref AstCDR
 | |
|  * \arg \ref AstAMI
 | |
|  * \arg \ref Config_ami
 | |
|  * \ingroup cdr_drivers
 | |
|  */
 | |
| 
 | |
| #include "asterisk.h"
 | |
| 
 | |
| ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 | |
| 
 | |
| #include <sys/types.h>
 | |
| #include <strings.h>
 | |
| #include <unistd.h>
 | |
| #include <time.h>
 | |
| 
 | |
| #include "asterisk/channel.h"
 | |
| #include "asterisk/cdr.h"
 | |
| #include "asterisk/module.h"
 | |
| #include "asterisk/logger.h"
 | |
| #include "asterisk/utils.h"
 | |
| #include "asterisk/manager.h"
 | |
| #include "asterisk/config.h"
 | |
| #include "asterisk/pbx.h"
 | |
| 
 | |
| #define DATE_FORMAT 	"%Y-%m-%d %T"
 | |
| #define CONF_FILE	"cdr_manager.conf"
 | |
| #define CUSTOM_FIELDS_BUF_SIZE 1024
 | |
| 
 | |
| static char *name = "cdr_manager";
 | |
| 
 | |
| static int enablecdr = 0;
 | |
| struct ast_str *customfields;
 | |
| 
 | |
| static int load_config(void)
 | |
| {
 | |
| 	char *cat = NULL;
 | |
| 	struct ast_config *cfg;
 | |
| 	struct ast_variable *v;
 | |
| 	
 | |
| 	customfields = NULL;
 | |
| 
 | |
| 	cfg = ast_config_load(CONF_FILE);
 | |
| 	if (!cfg) {
 | |
| 		/* Standard configuration */
 | |
| 		ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
 | |
| 		enablecdr = 0;
 | |
| 		return 0;
 | |
| 	}
 | |
| 	
 | |
| 	while ( (cat = ast_category_browse(cfg, cat)) ) {
 | |
| 		if (!strcasecmp(cat, "general")) {
 | |
| 			v = ast_variable_browse(cfg, cat);
 | |
| 			while (v) {
 | |
| 				if (!strcasecmp(v->name, "enabled")) {
 | |
| 					enablecdr = ast_true(v->value);
 | |
| 				}
 | |
| 				
 | |
| 				v = v->next;
 | |
| 			}
 | |
| 		} else if (!strcasecmp(cat, "mappings")) {
 | |
| 			customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
 | |
| 			v = ast_variable_browse(cfg, cat);
 | |
| 			while (v) {
 | |
| 				if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
 | |
| 					if( (customfields->used + strlen(v->value) + strlen(v->name) + 14) < customfields->len) {
 | |
| 						ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
 | |
| 						ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
 | |
| 					} else {
 | |
| 						ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
 | |
| 						break;
 | |
| 					}
 | |
| 					
 | |
| 				}
 | |
| 				v = v->next;
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 	
 | |
| 	ast_config_destroy(cfg);
 | |
| 	return 1;
 | |
| }
 | |
| 
 | |
| static int manager_log(struct ast_cdr *cdr)
 | |
| {
 | |
| 	time_t t;
 | |
| 	struct tm timeresult;
 | |
| 	char strStartTime[80] = "";
 | |
| 	char strAnswerTime[80] = "";
 | |
| 	char strEndTime[80] = "";
 | |
| 	char buf[CUSTOM_FIELDS_BUF_SIZE];
 | |
| 	struct ast_channel dummy;
 | |
| 
 | |
| 	if (!enablecdr)
 | |
| 		return 0;
 | |
| 
 | |
| 	t = cdr->start.tv_sec;
 | |
| 	ast_localtime(&t, &timeresult, NULL);
 | |
| 	strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
 | |
| 	
 | |
| 	if (cdr->answer.tv_sec)	{
 | |
|     		t = cdr->answer.tv_sec;
 | |
|     		ast_localtime(&t, &timeresult, NULL);
 | |
| 		strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
 | |
| 	}
 | |
| 
 | |
| 	t = cdr->end.tv_sec;
 | |
| 	ast_localtime(&t, &timeresult, NULL);
 | |
| 	strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
 | |
| 
 | |
| 	/* Custom fields handling */
 | |
| 	memset(buf, 0 , sizeof(buf));
 | |
| 	if (customfields != NULL && customfields->used > 0) {
 | |
| 		memset(&dummy, 0, sizeof(dummy));
 | |
| 		dummy.cdr = cdr;
 | |
| 		pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
 | |
| 	}
 | |
| 
 | |
| 	manager_event(EVENT_FLAG_CALL, "Cdr",
 | |
| 	    "AccountCode: %s\r\n"
 | |
| 	    "Source: %s\r\n"
 | |
| 	    "Destination: %s\r\n"
 | |
| 	    "DestinationContext: %s\r\n"
 | |
| 	    "CallerID: %s\r\n"
 | |
| 	    "Channel: %s\r\n"
 | |
| 	    "DestinationChannel: %s\r\n"
 | |
| 	    "LastApplication: %s\r\n"
 | |
| 	    "LastData: %s\r\n"
 | |
| 	    "StartTime: %s\r\n"
 | |
| 	    "AnswerTime: %s\r\n"
 | |
| 	    "EndTime: %s\r\n"
 | |
| 	    "Duration: %ld\r\n"
 | |
| 	    "BillableSeconds: %ld\r\n"
 | |
| 	    "Disposition: %s\r\n"
 | |
| 	    "AMAFlags: %s\r\n"
 | |
| 	    "UniqueID: %s\r\n"
 | |
| 	    "UserField: %s\r\n"
 | |
| 	    "%s",
 | |
| 	    cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
 | |
| 	    cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
 | |
| 	    cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
 | |
| 	    ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
 | |
| 
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| static int unload_module(void)
 | |
| {
 | |
| 	ast_cdr_unregister(name);
 | |
| 	if (customfields)
 | |
| 		ast_free(customfields);
 | |
| 
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| static int load_module(void)
 | |
| {
 | |
| 	int res;
 | |
| 
 | |
| 	/* Configuration file */
 | |
| 	if (!load_config())
 | |
| 		return AST_MODULE_LOAD_DECLINE;
 | |
| 	
 | |
| 	res = ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
 | |
| 	if (res) {
 | |
| 		ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
 | |
| 	}
 | |
| 	
 | |
| 	return res;
 | |
| }
 | |
| 
 | |
| static int reload(void)
 | |
| {
 | |
| 	if (customfields) {
 | |
| 		ast_free(customfields);
 | |
| 	}
 | |
| 	
 | |
| 	load_config();
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
 | |
| 		.load = load_module,
 | |
| 		.unload = unload_module,
 | |
| 		.reload = reload,
 | |
| 	       );
 |