minor simplification of a conditional statement
[asterisk/asterisk.git] / cdr / cdr_custom.c
index 6aad620..80cd47e 100644 (file)
  * \ingroup cdr_drivers
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <time.h>
 
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/channel.h"
 #include "asterisk/cdr.h"
 #include "asterisk/module.h"
@@ -57,13 +57,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 AST_MUTEX_DEFINE_STATIC(lock);
 
-static char *desc = "Customizable Comma Separated Values CDR Backend";
-
 static char *name = "cdr-custom";
 
 static FILE *mf = NULL;
 
-static char master[AST_CONFIG_MAX_PATH];
+static char master[PATH_MAX];
 static char format[1024]="";
 
 static int load_config(int reload) 
@@ -74,21 +72,22 @@ static int load_config(int reload)
 
        strcpy(format, "");
        strcpy(master, "");
+       ast_mutex_lock(&lock);
        if((cfg = ast_config_load("cdr_custom.conf"))) {
                var = ast_variable_browse(cfg, "mappings");
                while(var) {
-                       ast_mutex_lock(&lock);
                        if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
-                               if (strlen(var->value) > (sizeof(format) - 2))
+                               if (strlen(var->value) > (sizeof(format) - 1))
                                        ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
-                               strncpy(format, var->value, sizeof(format) - 2);
+                               ast_copy_string(format, var->value, sizeof(format) - 1);
                                strcat(format,"\n");
                                snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
-                               ast_mutex_unlock(&lock);
+                               if (var->next) {
+                                       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); 
+                                       break;
+                               }
                        } else
                                ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
-                       if (var->next)
-                               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); 
                        var = var->next;
                }
                ast_config_destroy(cfg);
@@ -99,6 +98,7 @@ static int load_config(int reload)
                else
                        ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
        }
+       ast_mutex_unlock(&lock);
        
        return res;
 }
@@ -137,12 +137,7 @@ static int custom_log(struct ast_cdr *cdr)
        return 0;
 }
 
-static const char *description(void)
-{
-       return desc;
-}
-
-static int unload_module(void *mod)
+static int unload_module(void)
 {
        if (mf)
                fclose(mf);
@@ -150,29 +145,29 @@ static int unload_module(void *mod)
        return 0;
 }
 
-static int load_module(void *mod)
+static int load_module(void)
 {
        int res = 0;
 
        if (!load_config(0)) {
-               res = ast_cdr_register(name, desc, custom_log);
+               res = ast_cdr_register(name, ast_module_info->description, custom_log);
                if (res)
                        ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
                if (mf)
                        fclose(mf);
-       }
-       return res;
+               return res;
+       } else 
+               return AST_MODULE_LOAD_DECLINE;
 }
 
-static int reload(void *mod)
+static int reload(void)
 {
        return load_config(1);
 }
 
-static const char *key(void)
-{
-       return ASTERISK_GPL_KEY;
-}
-
-STD_MOD(MOD_1 | NO_USECOUNT, reload, NULL, NULL);
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Customizable Comma Separated Values CDR Backend",
+               .load = load_module,
+               .unload = unload_module,
+               .reload = reload,
+              );