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