modules_k/speeddial/speeddial.c
31ccf6a2
 /*
  * $Id$
  *
b9ffa479
  * Copyright (C) 2004-2006 Voice Sistem SRL
31ccf6a2
  *
9b64b9f3
  * This file is part of Kamailio.
31ccf6a2
  *
9b64b9f3
  * Kamailio is free software; you can redistribute it and/or
31ccf6a2
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
  * History:
  * ---------
  * 
  */
 
 
 #include <stdio.h>
 #include <string.h>
 #include "../../sr_module.h"
0b4cc6b6
 #include "../../lib/srdb1/db.h"
31ccf6a2
 #include "../../dprint.h"
 #include "../../error.h"
 #include "../../mem/mem.h"
b5263e8c
 #include "../../mod_fix.h"
31ccf6a2
 
 #include "sdlookup.h"
 
 MODULE_VERSION
 
 
 /* Module destroy function prototype */
 static void destroy(void);
 
 
 /* Module child-init function prototype */
 static int child_init(int rank);
 
 
 /* Module initialization function prototype */
 static int mod_init(void);
 
 /* Module parameter variables */
e2cf6343
 static str db_url    = str_init(DEFAULT_RODB_URL);
 str user_column      = str_init("username");
 str domain_column    = str_init("domain");
 str sd_user_column   = str_init("sd_username");
 str sd_domain_column = str_init("sd_domain");
 str new_uri_column   = str_init("new_uri");
 int use_domain       = 0;
 static str domain_prefix    = {NULL, 0};
31ccf6a2
 
e2cf6343
 str dstrip_s = {NULL, 0};
31ccf6a2
 
 
 db_func_t db_funcs;      /* Database functions */
0b4cc6b6
 db1_con_t* db_handle=0;   /* Database connection handle */
31ccf6a2
 
 
 /* Exported functions */
 static cmd_export_t cmds[] = {
b5263e8c
 	{"sd_lookup", (cmd_function)sd_lookup, 1, fixup_spve_null, 0,
 		REQUEST_ROUTE},
 	{"sd_lookup", (cmd_function)sd_lookup, 2, fixup_spve_spve, 0,
 		REQUEST_ROUTE},
80998a7f
 	{0, 0, 0, 0, 0, 0}
31ccf6a2
 };
 
 
 /* Exported parameters */
 static param_export_t params[] = {
e2cf6343
 	{"db_url",           STR_PARAM, &db_url.s             },
 	{"user_column",      STR_PARAM, &user_column.s        },
 	{"domain_column",    STR_PARAM, &domain_column.s      },
 	{"sd_user_column",   STR_PARAM, &sd_user_column.s     },
 	{"sd_domain_column", STR_PARAM, &sd_domain_column.s   },
 	{"new_uri_column",   STR_PARAM, &new_uri_column.s     },
 	{"use_domain",       INT_PARAM, &use_domain           },
 	{"domain_prefix",    STR_PARAM, &domain_prefix.s      },
31ccf6a2
 	{0, 0, 0}
 };
 
 
 /* Module interface */
 struct module_exports exports = {
 	"speeddial", 
51716422
 	DEFAULT_DLFLAGS, /* dlopen flags */
31ccf6a2
 	cmds,       /* Exported functions */
 	params,     /* Exported parameters */
739aec8b
 	0,          /* exported statistics */
de26de44
 	0,          /* exported MI functions */
71c26efd
 	0,          /* exported pseudo-variables */
3eee3a4e
 	0,          /* extra processes */
31ccf6a2
 	mod_init,   /* module initialization function */
 	0,          /* response function */
 	destroy,    /* destroy function */
 	child_init  /* child initialization function */
 };
 
 
 /**
  *
  */
 static int child_init(int rank)
 {
82a5ef65
 	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
 		return 0; /* do nothing for the main process */
 
e2cf6343
 	db_handle = db_funcs.init(&db_url);
31ccf6a2
 	if (!db_handle)
 	{
de7fe5e9
 		LM_ERR("failed to connect database\n");
31ccf6a2
 		return -1;
 	}
 	return 0;
 
 }
 
 
 /**
  *
  */
 static int mod_init(void)
 {
e2cf6343
 	db_url.len = strlen(db_url.s);
 	user_column.len = strlen(user_column.s);
 	domain_column.len = strlen(domain_column.s);
 	sd_user_column.len = strlen(sd_user_column.s);
 	sd_domain_column.len  = strlen(sd_domain_column.s);
 	new_uri_column.len = strlen(new_uri_column.s);
 	if (domain_prefix.s)
 		domain_prefix.len = strlen(domain_prefix.s);
 
31ccf6a2
     /* Find a database module */
e2cf6343
 	if (db_bind_mod(&db_url, &db_funcs))
31ccf6a2
 	{
de7fe5e9
 		LM_ERR("failed to bind database module\n");
31ccf6a2
 		return -1;
 	}
 	if (!DB_CAPABILITY(db_funcs, DB_CAP_QUERY))
 	{
de7fe5e9
 		LM_ERR("Database modules does not "
31ccf6a2
 			"provide all functions needed by SPEEDDIAL module\n");
 		return -1;
 	}
e2cf6343
 	if (domain_prefix.s && domain_prefix.len > 0) {
 		dstrip_s.s = domain_prefix.s;
 		dstrip_s.len = domain_prefix.len;
31ccf6a2
 	}
 
 	return 0;
 }
 
 
 /**
  *
  */
 static void destroy(void)
 {
 	if (db_handle)
 		db_funcs.close(db_handle);
 }