| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-12-19 21:13:41 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Includes code and algorithms from the Zapata library. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | /*!
 | 
					
						
							|  |  |  |  * \file | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \brief Comma Separated Value CDR records. | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \arg See also \ref AstCDR | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup cdr_drivers | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:13:06 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>extended</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-20 23:16:15 +00:00
										 |  |  | #include "asterisk/paths.h"	/* use ast_config_AST_LOG_DIR */
 | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | #include "asterisk/config.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"
 | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | #include "asterisk/lock.h"
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-30 15:03:20 +00:00
										 |  |  | #define CSV_LOG_DIR "/cdr-csv"
 | 
					
						
							|  |  |  | #define CSV_MASTER  "/Master.csv"
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define DATE_FORMAT "%Y-%m-%d %T"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | static int usegmtime = 0; | 
					
						
							| 
									
										
										
										
											2010-07-09 11:06:19 +00:00
										 |  |  | static int accountlogs; | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | static int loguniqueid = 0; | 
					
						
							|  |  |  | static int loguserfield = 0; | 
					
						
							| 
									
										
										
										
											2008-04-09 19:00:40 +00:00
										 |  |  | static int loaded = 0; | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | static const char config[] = "cdr.conf"; | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-30 04:41:18 +00:00
										 |  |  | /* #define CSV_LOGUNIQUEID 1 */ | 
					
						
							| 
									
										
										
										
											2004-01-11 05:04:16 +00:00
										 |  |  | /* #define CSV_LOGUSERFIELD 1 */ | 
					
						
							| 
									
										
										
										
											2003-05-30 04:41:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-19 21:13:41 +00:00
										 |  |  | /*----------------------------------------------------
 | 
					
						
							|  |  |  |   The values are as follows: | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-19 21:13:41 +00:00
										 |  |  |   "accountcode", 	accountcode is the account name of detail records, Master.csv contains all records * | 
					
						
							|  |  |  |   			Detail records are configured on a channel basis, IAX and SIP are determined by user * | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 			DAHDI is determined by channel in dahdi.conf | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  |   "source", | 
					
						
							|  |  |  |   "destination", | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  |   "destination context", | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  |   "callerid", | 
					
						
							|  |  |  |   "channel", | 
					
						
							|  |  |  |   "destination channel",	(if applicable) | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  |   "last application",	Last application run on the channel | 
					
						
							|  |  |  |   "last app argument",	argument to the last channel | 
					
						
							|  |  |  |   "start time", | 
					
						
							|  |  |  |   "answer time", | 
					
						
							|  |  |  |   "end time", | 
					
						
							|  |  |  |   duration,   		Duration is the whole length that the entire call lasted. ie. call rx'd to hangup | 
					
						
							|  |  |  |   			"end time" minus "start time" | 
					
						
							|  |  |  |   billable seconds, 	the duration that a call was up after other end answered which will be <= to duration | 
					
						
							|  |  |  |   			"end time" minus "answer time" | 
					
						
							|  |  |  |   "disposition",    	ANSWERED, NO ANSWER, BUSY | 
					
						
							|  |  |  |   "amaflags",       	DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode. | 
					
						
							|  |  |  |   "uniqueid",           unique call identifier | 
					
						
							|  |  |  |   "userfield"		user field set via SetCDRUserField | 
					
						
							| 
									
										
										
										
											2004-12-19 21:13:41 +00:00
										 |  |  | ----------------------------------------------------------*/ | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char *name = "csv"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | AST_MUTEX_DEFINE_STATIC(mf_lock); | 
					
						
							|  |  |  | AST_MUTEX_DEFINE_STATIC(acf_lock); | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | static int load_config(int reload) | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ast_config *cfg; | 
					
						
							|  |  |  | 	struct ast_variable *var; | 
					
						
							| 
									
										
										
										
											2006-09-20 20:40:39 +00:00
										 |  |  | 	const char *tmp; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-19 19:25:14 +00:00
										 |  |  | 	if (!(cfg = ast_config_load(config, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) { | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "unable to load config: %s\n", config); | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) | 
					
						
							| 
									
										
										
										
											2008-05-15 17:58:22 +00:00
										 |  |  | 		return 1; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 11:06:19 +00:00
										 |  |  | 	accountlogs = 1; | 
					
						
							| 
									
										
										
										
											2008-11-21 17:08:16 +00:00
										 |  |  | 	usegmtime = 0; | 
					
						
							|  |  |  | 	loguniqueid = 0; | 
					
						
							|  |  |  | 	loguserfield = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	if (!(var = ast_variable_browse(cfg, "csv"))) { | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 		ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, "csv", "usegmtime"))) { | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 		usegmtime = ast_true(tmp); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 		if (usegmtime) | 
					
						
							| 
									
										
										
										
											2007-06-14 19:39:12 +00:00
										 |  |  | 			ast_debug(1, "logging time in GMT\n"); | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 11:06:19 +00:00
										 |  |  | 	/* Turn on/off separate files per accountcode. Default is on (as before) */ | 
					
						
							|  |  |  | 	if ((tmp = ast_variable_retrieve(cfg, "csv", "accountlogs"))) { | 
					
						
							|  |  |  |  		accountlogs = ast_true(tmp); | 
					
						
							|  |  |  |  		if (accountlogs) { | 
					
						
							|  |  |  | 			ast_debug(1, "logging in separate files per accountcode\n"); | 
					
						
							|  |  |  |  		} | 
					
						
							|  |  |  |  	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid"))) { | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 		loguniqueid = ast_true(tmp); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 		if (loguniqueid) | 
					
						
							| 
									
										
										
										
											2007-06-14 19:39:12 +00:00
										 |  |  | 			ast_debug(1, "logging CDR field UNIQUEID\n"); | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, "csv", "loguserfield"))) { | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 		loguserfield = ast_true(tmp); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 		if (loguserfield) | 
					
						
							| 
									
										
										
										
											2007-06-14 19:39:12 +00:00
										 |  |  | 			ast_debug(1, "logging CDR user-defined field\n"); | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 	return 1; | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:07:09 +00:00
										 |  |  | static int append_string(char *buf, const char *s, size_t bufsize) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	int pos = strlen(buf), spos = 0, error = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	if (pos >= bufsize - 4) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	buf[pos++] = '\"'; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	while(pos < bufsize - 3) { | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		if (!s[spos]) { | 
					
						
							|  |  |  | 			error = 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (s[spos] == '\"') | 
					
						
							|  |  |  | 			buf[pos++] = '\"'; | 
					
						
							|  |  |  | 		buf[pos++] = s[spos]; | 
					
						
							|  |  |  | 		spos++; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	buf[pos++] = '\"'; | 
					
						
							|  |  |  | 	buf[pos++] = ','; | 
					
						
							|  |  |  | 	buf[pos++] = '\0'; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	return error; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | static int append_int(char *buf, int s, size_t bufsize) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char tmp[32]; | 
					
						
							|  |  |  | 	int pos = strlen(buf); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	snprintf(tmp, sizeof(tmp), "%d", s); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	if (pos + strlen(tmp) > bufsize - 3) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	strncat(buf, tmp, bufsize - strlen(buf) - 1); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	pos = strlen(buf); | 
					
						
							|  |  |  | 	buf[pos++] = ','; | 
					
						
							|  |  |  | 	buf[pos++] = '\0'; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 00:52:23 +00:00
										 |  |  | static int append_date(char *buf, struct timeval when, size_t bufsize) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	char tmp[80] = ""; | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	struct ast_tm tm; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	if (strlen(buf) > bufsize - 3) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 00:52:23 +00:00
										 |  |  | 	if (ast_tvzero(when)) { | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 		strncat(buf, ",", bufsize - strlen(buf) - 1); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 00:52:23 +00:00
										 |  |  | 	ast_localtime(&when, &tm, usegmtime ? "GMT" : NULL); | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	return append_string(buf, tmp, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-01-11 05:04:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	buf[0] = '\0'; | 
					
						
							|  |  |  | 	/* Account code */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->accountcode, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Source */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->src, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Destination */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->dst, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Destination context */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->dcontext, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Caller*ID */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->clid, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Channel */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->channel, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Destination Channel */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->dstchannel, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Last Application */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->lastapp, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Last Data */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, cdr->lastdata, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Start Time */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_date(buf, cdr->start, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Answer Time */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_date(buf, cdr->answer, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* End Time */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_date(buf, cdr->end, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Duration */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_int(buf, cdr->duration, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Billable seconds */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_int(buf, cdr->billsec, bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Disposition */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* AMA Flags */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize); | 
					
						
							| 
									
										
										
										
											2003-05-30 04:41:18 +00:00
										 |  |  | 	/* Unique ID */ | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 	if (loguniqueid) | 
					
						
							|  |  |  | 		append_string(buf, cdr->uniqueid, bufsize); | 
					
						
							| 
									
										
										
										
											2004-01-11 05:04:16 +00:00
										 |  |  | 	/* append the user field */ | 
					
						
							| 
									
										
										
										
											2006-03-02 21:04:04 +00:00
										 |  |  | 	if(loguserfield) | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 		append_string(buf, cdr->userfield,bufsize); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* If we hit the end of our buffer, log an error */ | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	if (strlen(buf) < bufsize - 5) { | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		/* Trim off trailing comma */ | 
					
						
							|  |  |  | 		buf[strlen(buf) - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 		strncat(buf, "\n", bufsize - strlen(buf) - 1); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int writefile(char *s, char *acc) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-06-09 20:40:10 +00:00
										 |  |  | 	char tmp[PATH_MAX]; | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	FILE *f; | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	if (strchr(acc, '/') || (acc[0] == '.')) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-18 09:46:18 +00:00
										 |  |  | 	snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc); | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ast_mutex_lock(&acf_lock); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	if (!(f = fopen(tmp, "a"))) { | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | 		ast_mutex_unlock(&acf_lock); | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to open file %s : %s\n", tmp, strerror(errno)); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	fputs(s, f); | 
					
						
							| 
									
										
										
										
											2004-10-07 17:50:15 +00:00
										 |  |  | 	fflush(f); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	fclose(f); | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | 	ast_mutex_unlock(&acf_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int csv_log(struct ast_cdr *cdr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-09-13 20:13:58 +00:00
										 |  |  | 	FILE *mf = NULL; | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	/* Make sure we have a big enough buf */ | 
					
						
							|  |  |  | 	char buf[1024]; | 
					
						
							| 
									
										
										
										
											2006-06-09 20:40:10 +00:00
										 |  |  | 	char csvmaster[PATH_MAX]; | 
					
						
							| 
									
										
										
										
											2004-07-14 13:57:15 +00:00
										 |  |  | 	snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  | 	printf("[CDR] %s ('%s' -> '%s') Dur: %ds Bill: %ds Disp: %s Flags: %s Account: [%s]\n", cdr->channel, cdr->src, cdr->dst, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), cdr->accountcode); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	if (build_csv_record(buf, sizeof(buf), cdr)) { | 
					
						
							| 
									
										
										
										
											2004-06-13 21:25:10 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes.  CDR not recorded!\n", (int)sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 	/* because of the absolutely unconditional need for the
 | 
					
						
							|  |  |  | 	   highest reliability possible in writing billing records, | 
					
						
							|  |  |  | 	   we open write and close the log file each time */ | 
					
						
							|  |  |  | 	ast_mutex_lock(&mf_lock); | 
					
						
							|  |  |  | 	if ((mf = fopen(csvmaster, "a"))) { | 
					
						
							|  |  |  | 		fputs(buf, mf); | 
					
						
							|  |  |  | 		fflush(mf); /* be particularly anal here */ | 
					
						
							|  |  |  | 		fclose(mf); | 
					
						
							|  |  |  | 		mf = NULL; | 
					
						
							|  |  |  | 		ast_mutex_unlock(&mf_lock); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 		ast_mutex_unlock(&mf_lock); | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 11:06:19 +00:00
										 |  |  | 	if (accountlogs && !ast_strlen_zero(cdr->accountcode)) { | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 		if (writefile(buf, cdr->accountcode)) | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno)); | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-09-19 20:06:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	ast_cdr_unregister(name); | 
					
						
							| 
									
										
										
										
											2008-04-09 19:00:40 +00:00
										 |  |  | 	loaded = 0; | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res; | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	if(!load_config(0)) | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-09 19:00:40 +00:00
										 |  |  | 	if ((res = ast_cdr_register(name, ast_module_info->description, csv_log))) { | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n"); | 
					
						
							| 
									
										
										
										
											2008-04-09 19:00:40 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		loaded = 1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int reload(void) | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-04-09 19:00:40 +00:00
										 |  |  | 	if (load_config(1)) { | 
					
						
							|  |  |  | 		loaded = 1; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		loaded = 0; | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "No [csv] section in cdr.conf.  Unregistering backend.\n"); | 
					
						
							|  |  |  | 		ast_cdr_unregister(name); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2001-10-18 15:18:45 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Comma Separated Values 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
										 |  |  | 	       ); |