| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-15 15:44:26 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | /*!
 | 
					
						
							|  |  |  |  * \file | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \brief Asterisk Call Manager CDR records. | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * See also | 
					
						
							|  |  |  |  * \arg \ref AstCDR | 
					
						
							|  |  |  |  * \arg \ref AstAMI | 
					
						
							|  |  |  |  * \arg \ref Config_ami | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup cdr_drivers | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:13:06 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-06 21:09:59 +00:00
										 |  |  | #include <time.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/cdr.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/manager.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | #include "asterisk/pbx.h"
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define DATE_FORMAT 	"%Y-%m-%d %T"
 | 
					
						
							|  |  |  | #define CONF_FILE	"cdr_manager.conf"
 | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | #define CUSTOM_FIELDS_BUF_SIZE 1024
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | static const char name[] = "cdr_manager"; | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int enablecdr = 0; | 
					
						
							| 
									
										
										
										
											2009-06-20 20:56:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 17:34:30 +00:00
										 |  |  | static struct ast_str *customfields; | 
					
						
							| 
									
										
										
										
											2009-06-21 16:11:48 +00:00
										 |  |  | AST_RWLOCK_DEFINE_STATIC(customfields_lock); | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | static int manager_log(struct ast_cdr *cdr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | static int load_config(int reload) | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	char *cat = NULL; | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	struct ast_config *cfg; | 
					
						
							|  |  |  | 	struct ast_variable *v; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | 	int newenablecdr = 0; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cfg = ast_config_load(CONF_FILE, config_flags); | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 	if (cfg == CONFIG_STATUS_FILEUNCHANGED) { | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-19 19:25:14 +00:00
										 |  |  | 	if (cfg == CONFIG_STATUS_FILEINVALID) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE); | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2008-11-19 19:25:14 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	if (!cfg) { | 
					
						
							|  |  |  | 		/* Standard configuration */ | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n"); | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | 		if (enablecdr) | 
					
						
							|  |  |  | 			ast_cdr_unregister(name); | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 		enablecdr = 0; | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-21 16:11:48 +00:00
										 |  |  | 	if (reload) { | 
					
						
							|  |  |  | 		ast_rwlock_wrlock(&customfields_lock); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (reload && customfields) { | 
					
						
							|  |  |  | 		ast_free(customfields); | 
					
						
							|  |  |  | 		customfields = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	while ( (cat = ast_category_browse(cfg, cat)) ) { | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 		if (!strcasecmp(cat, "general")) { | 
					
						
							|  |  |  | 			v = ast_variable_browse(cfg, cat); | 
					
						
							|  |  |  | 			while (v) { | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | 				if (!strcasecmp(v->name, "enabled")) | 
					
						
							|  |  |  | 					newenablecdr = ast_true(v->value); | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 				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)) { | 
					
						
							| 
									
										
										
										
											2008-12-13 08:36:35 +00:00
										 |  |  | 					if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) { | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 						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; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 				v = v->next; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-21 16:11:48 +00:00
										 |  |  | 	if (reload) { | 
					
						
							|  |  |  | 		ast_rwlock_unlock(&customfields_lock); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 	ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (enablecdr && !newenablecdr) | 
					
						
							|  |  |  | 		ast_cdr_unregister(name); | 
					
						
							|  |  |  | 	else if (!enablecdr && newenablecdr) | 
					
						
							|  |  |  | 		ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log); | 
					
						
							| 
									
										
										
										
											2007-09-29 18:21:36 +00:00
										 |  |  | 	enablecdr = newenablecdr; | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int manager_log(struct ast_cdr *cdr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	struct ast_tm timeresult; | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	char strStartTime[80] = ""; | 
					
						
							|  |  |  | 	char strAnswerTime[80] = ""; | 
					
						
							|  |  |  | 	char strEndTime[80] = ""; | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	char buf[CUSTOM_FIELDS_BUF_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	if (!enablecdr) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	ast_localtime(&cdr->start, &timeresult, NULL); | 
					
						
							|  |  |  | 	ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult); | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	if (cdr->answer.tv_sec)	{ | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 		ast_localtime(&cdr->answer, &timeresult, NULL); | 
					
						
							|  |  |  | 		ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult); | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	ast_localtime(&cdr->end, &timeresult, NULL); | 
					
						
							|  |  |  | 	ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult); | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-21 16:11:48 +00:00
										 |  |  | 	buf[0] = '\0'; | 
					
						
							|  |  |  | 	ast_rwlock_rdlock(&customfields_lock); | 
					
						
							|  |  |  | 	if (customfields && ast_str_strlen(customfields)) { | 
					
						
							| 
									
										
										
										
											2009-06-26 15:28:53 +00:00
										 |  |  | 		struct ast_channel *dummy = ast_dummy_channel_alloc(); | 
					
						
							| 
									
										
										
										
											2009-05-26 12:14:14 +00:00
										 |  |  | 		if (!dummy) { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n"); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		dummy->cdr = ast_cdr_dup(cdr); | 
					
						
							|  |  |  | 		pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1); | 
					
						
							|  |  |  | 		ast_channel_release(dummy); | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-21 16:11:48 +00:00
										 |  |  | 	ast_rwlock_unlock(&customfields_lock); | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-05 16:46:47 +00:00
										 |  |  | 	manager_event(EVENT_FLAG_CDR, "Cdr", | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	    "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" | 
					
						
							| 
									
										
										
										
											2006-03-01 17:53:05 +00:00
										 |  |  | 	    "Duration: %ld\r\n" | 
					
						
							|  |  |  | 	    "BillableSeconds: %ld\r\n" | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	    "Disposition: %s\r\n" | 
					
						
							|  |  |  | 	    "AMAFlags: %s\r\n" | 
					
						
							|  |  |  | 	    "UniqueID: %s\r\n" | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	    "UserField: %s\r\n" | 
					
						
							|  |  |  | 	    "%s", | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	    cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel, | 
					
						
							|  |  |  | 	    cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime, | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	    cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), | 
					
						
							|  |  |  | 	    ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	ast_cdr_unregister(name); | 
					
						
							| 
									
										
										
										
											2007-06-19 20:38:21 +00:00
										 |  |  | 	if (customfields) | 
					
						
							|  |  |  | 		ast_free(customfields); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 	if (load_config(0)) { | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2009-06-21 16:00:23 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-09-21 17:36:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int reload(void) | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	return load_config(1); | 
					
						
							| 
									
										
										
										
											2004-07-25 14:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend", | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 		.load = load_module, | 
					
						
							|  |  |  | 		.unload = unload_module, | 
					
						
							|  |  |  | 		.reload = reload, | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | 		.load_pri = AST_MODPRI_CDR_DRIVER, | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	       ); |