2020-12-08 02:16:12 +00:00
/*
* This file is part of uIRCd . ( https : //git.redxen.eu/caskd/uIRCd)
* Copyright ( c ) 2019 , 2020 Alex - David Denes
*
* uIRCd is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* any later version .
*
* uIRCd is distributed in the hope that it will be useful ,
* 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 uIRCd . If not , see < https : //www.gnu.org/licenses/>.
*/
# include "configuration.h"
2021-01-02 14:12:13 +00:00
# include "channels.h"
# include "connection.h"
# include "logging.h"
# include "memory.h"
2020-12-08 22:20:42 +00:00
# ifdef UIRCD_FEATURE_LIBCONFIG
2021-01-02 14:12:13 +00:00
# include <libconfig.h> // config_t config_setting_t config_init() ...
2020-12-09 01:33:36 +00:00
int
parse_configfile ( char * config_path , Connection * conn )
2020-12-08 02:16:12 +00:00
{
2020-12-09 01:33:36 +00:00
config_t conf ;
int res = - 1 ;
2020-12-09 02:29:24 +00:00
config_setting_t * root ;
2020-12-08 02:16:12 +00:00
config_init ( & conf ) ;
if ( config_read_file ( & conf , config_path ) ) {
2020-12-09 02:29:24 +00:00
if ( ( root = config_lookup ( & conf , " . " ) ) ! = NULL ) {
Mapping rootmaps [ ] = {
2020-12-09 01:33:36 +00:00
{ " address " , & conn - > data . address } ,
{ " service " , & conn - > data . service } ,
{ " quitmsg " , & conn - > data . quitmsg } ,
{ " logdir " , & conn - > data . path } ,
2020-12-08 02:16:12 +00:00
} ;
2020-12-09 02:29:24 +00:00
mapconf ( root , rootmaps , sizeof ( rootmaps ) / sizeof ( * rootmaps ) ) ;
2020-12-30 20:28:21 +00:00
// Timeout is saved as a time_t (long) and may not be negative, 0 is default (unset)
signed int tmp = - 1 ;
config_setting_lookup_int ( root , " timeout " , & tmp ) ;
if ( tmp > 0 ) conn - > data . timeout = tmp ;
2020-12-09 02:29:24 +00:00
// Channels are a array
config_setting_t * chans ;
if ( ( chans = config_lookup ( & conf , " .channels " ) ) ! = NULL ) {
// TODO: Check if config_setting_length can return negative values
2020-12-17 00:27:30 +00:00
for ( unsigned int i = 0 ; i < ( unsigned int ) config_setting_length ( chans ) ; i + + ) {
config_setting_t * chanelem = config_setting_get_elem ( chans , i ) ;
const char * name = NULL , * key = NULL ;
config_setting_lookup_string ( chanelem , " name " , & name ) ;
config_setting_lookup_string ( chanelem , " key " , & key ) ;
LOG ( LOG_DEBUG , " Got channel #%i: %s " , i , name ) ;
2021-01-04 18:07:06 +00:00
resize_chanarray ( & conn - > info . channels ) ;
2021-01-12 17:04:21 +00:00
if ( name ! = NULL ) set_channel ( & conn - > info . channels [ get_channelindex ( name , conn - > info . channels ) ] , name , key , true ) ;
2020-12-08 22:20:42 +00:00
}
2020-12-08 02:16:12 +00:00
}
2020-12-09 02:29:24 +00:00
config_setting_t * user ;
if ( ( user = config_lookup ( & conf , " .user " ) ) ! = NULL ) {
Mapping usermaps [ ] = {
{ " nickname " , & conn - > user . nickname } ,
{ " username " , & conn - > user . username } ,
{ " realname " , & conn - > user . realname } ,
{ " password " , & conn - > user . password } ,
} ;
mapconf ( user , usermaps , sizeof ( usermaps ) / sizeof ( * usermaps ) ) ;
}
2021-01-12 17:04:21 +00:00
LOG ( LOG_DEBUG , " %s. " , " Parsed configuration file successfully " ) ;
2020-12-08 02:16:12 +00:00
res = 1 ;
}
} else
2021-01-12 17:04:21 +00:00
LOG ( LOG_WARN , " Encountered a error while reading the config file. %s in %s line %d " , config_error_text ( & conf ) , config_error_file ( & conf ) , config_error_line ( & conf ) ) ;
2020-12-08 02:16:12 +00:00
config_destroy ( & conf ) ;
return res ;
}
2020-12-08 22:20:42 +00:00
2020-12-09 02:29:24 +00:00
void
mapconf ( config_setting_t * setting , Mapping * mapping , unsigned long len )
{
for ( unsigned long i = 0 ; i < len ; i + + ) {
const char * var = NULL ;
config_setting_lookup_string ( setting , mapping [ i ] . str , & var ) ;
if ( var ! = NULL ) {
if ( allocate_copy ( mapping [ i ] . save , var ) ) {
LOG ( LOG_DEBUG , " Saved %s to %p " , var , ( const void * ) mapping [ i ] . save ) ;
} else {
LOG ( LOG_WARN , " Failed to allocate memory to save config variable %s = %s " , mapping [ i ] . str , var ) ;
}
}
}
}
2020-12-17 00:27:30 +00:00
# endif /* UIRCD_FEATURE_LIBCONFIG */
int
set_config_defaults ( Connection * conn )
{
Mapping defsalloc [ ] = {
2021-01-12 17:04:21 +00:00
{ UIRCD_DEFAULT_ADDR , & conn - > data . address } , { UIRCD_DEFAULT_PORT , & conn - > data . service } , { UIRCD_DEFAULT_QUITMSG , & conn - > data . quitmsg } ,
{ UIRCD_DEFAULT_NICK , & conn - > user . nickname } , { UIRCD_DEFAULT_USER , & conn - > user . username } , { UIRCD_DEFAULT_REAL , & conn - > user . realname } ,
2020-12-17 00:27:30 +00:00
} ;
for ( unsigned int i = 0 ; i < sizeof ( defsalloc ) / sizeof ( * defsalloc ) ; i + + ) {
if ( ! allocate_copy ( defsalloc [ i ] . save , defsalloc [ i ] . str ) ) return 0 ;
}
2020-12-30 18:03:04 +00:00
conn - > data . timeout = 0 ;
2020-12-17 00:27:30 +00:00
return 1 ;
}