Merged revisions 46714 via svnmerge from
[asterisk/asterisk.git] / cdr / cdr_custom.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Includes code and algorithms from the Zapata library.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Custom Comma Separated Value CDR records.
24  *
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \arg See also \ref AstCDR
28  *
29  * Logs in LOG_DIR/cdr_custom
30  * \ingroup cdr_drivers
31  */
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <errno.h>
41
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <time.h>
45
46 #include "asterisk/channel.h"
47 #include "asterisk/cdr.h"
48 #include "asterisk/module.h"
49 #include "asterisk/config.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/utils.h"
53
54 #define CUSTOM_LOG_DIR "/cdr_custom"
55
56 #define DATE_FORMAT "%Y-%m-%d %T"
57
58 AST_MUTEX_DEFINE_STATIC(lock);
59
60 static char *name = "cdr-custom";
61
62 static FILE *mf = NULL;
63
64 static char master[PATH_MAX];
65 static char format[1024]="";
66
67 static int load_config(int reload) 
68 {
69         struct ast_config *cfg;
70         struct ast_variable *var;
71         int res = -1;
72
73         strcpy(format, "");
74         strcpy(master, "");
75         if((cfg = ast_config_load("cdr_custom.conf"))) {
76                 var = ast_variable_browse(cfg, "mappings");
77                 while(var) {
78                         ast_mutex_lock(&lock);
79                         if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
80                                 if (strlen(var->value) > (sizeof(format) - 1))
81                                         ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
82                                 ast_copy_string(format, var->value, sizeof(format) - 1);
83                                 strcat(format,"\n");
84                                 snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
85                                 ast_mutex_unlock(&lock);
86                         } else
87                                 ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
88                         if (var->next)
89                                 ast_log(LOG_NOTICE, "Sorry, only one mapping is supported at this time, mapping '%s' will be ignored at line %d.\n", var->next->name, var->next->lineno); 
90                         var = var->next;
91                 }
92                 ast_config_destroy(cfg);
93                 res = 0;
94         } else {
95                 if (reload)
96                         ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
97                 else
98                         ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
99         }
100         
101         return res;
102 }
103
104
105
106 static int custom_log(struct ast_cdr *cdr)
107 {
108         /* Make sure we have a big enough buf */
109         char buf[2048];
110         struct ast_channel dummy;
111
112         /* Abort if no master file is specified */
113         if (ast_strlen_zero(master))
114                 return 0;
115
116         memset(buf, 0 , sizeof(buf));
117         /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
118         memset(&dummy, 0, sizeof(dummy));
119         dummy.cdr = cdr;
120         pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
121
122         /* because of the absolutely unconditional need for the
123            highest reliability possible in writing billing records,
124            we open write and close the log file each time */
125         mf = fopen(master, "a");
126         if (!mf) {
127                 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
128         }
129         if (mf) {
130                 fputs(buf, mf);
131                 fflush(mf); /* be particularly anal here */
132                 fclose(mf);
133                 mf = NULL;
134         }
135         return 0;
136 }
137
138 static int unload_module(void)
139 {
140         if (mf)
141                 fclose(mf);
142         ast_cdr_unregister(name);
143         return 0;
144 }
145
146 static int load_module(void)
147 {
148         int res = 0;
149
150         if (!load_config(0)) {
151                 res = ast_cdr_register(name, ast_module_info->description, custom_log);
152                 if (res)
153                         ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
154                 if (mf)
155                         fclose(mf);
156                 return res;
157         } else 
158                 return AST_MODULE_LOAD_DECLINE;
159 }
160
161 static int reload(void)
162 {
163         return load_config(1);
164 }
165
166 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Customizable Comma Separated Values CDR Backend",
167                 .load = load_module,
168                 .unload = unload_module,
169                 .reload = reload,
170                );
171