2011-02-10 13:12:10 +01:00
/**
* libtransport - - C + + library for easy XMPP Transports development
*
* Copyright ( C ) 2011 , Jan Kaluza < hanzz . k @ gmail . com >
*
* This program 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 2 of the License , or
* ( at your option ) any later version .
*
* This program 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 this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02111 - 1301 USA
*/
2015-11-18 14:05:57 +01:00
# include "transport/Config.h"
2011-02-10 13:12:10 +01:00
# include <fstream>
2015-11-18 14:05:57 +01:00
2012-04-08 20:24:14 +04:00
# ifdef _WIN32
2011-10-20 08:55:51 +02:00
# include <direct.h>
# define getcwd _getcwd
# include <windows.h>
2012-04-13 17:05:52 +04:00
# ifdef _MSC_VER
2011-10-20 08:55:51 +02:00
# define PATH_MAX MAX_PATH
# endif
2012-04-13 17:05:52 +04:00
# endif
2011-02-10 13:12:10 +01:00
2012-04-10 10:13:22 +02:00
# include "iostream"
# include "boost/version.hpp"
2012-09-04 08:16:16 +02:00
# include "boost/algorithm/string.hpp"
2012-04-10 10:13:22 +02:00
# define BOOST_MAJOR_VERSION BOOST_VERSION / 100000
# define BOOST_MINOR_VERSION BOOST_VERSION / 100 % 1000
2011-02-10 13:12:10 +01:00
using namespace boost : : program_options ;
namespace Transport {
2012-04-10 10:13:22 +02:00
static int getRandomPort ( const std : : string & s ) {
2012-03-21 15:46:25 +01:00
unsigned long r = 0 ;
BOOST_FOREACH ( char c , s ) {
r + = ( int ) c ;
}
srand ( time ( NULL ) + r ) ;
return 30000 + rand ( ) % 10000 ;
}
2011-02-10 13:12:10 +01:00
2011-11-03 20:44:33 +01:00
bool Config : : load ( const std : : string & configfile , boost : : program_options : : options_description & opts , const std : : string & jid ) {
2011-02-10 13:12:10 +01:00
std : : ifstream ifs ( configfile . c_str ( ) ) ;
if ( ! ifs . is_open ( ) )
return false ;
2011-08-21 13:41:23 +02:00
m_file = configfile ;
2012-09-12 08:15:10 +02:00
m_jid = jid ;
2011-11-03 20:44:33 +01:00
bool ret = load ( ifs , opts , jid ) ;
2011-08-21 13:41:23 +02:00
ifs . close ( ) ;
2012-08-01 13:26:04 +04:00
# ifndef WIN32
2011-09-08 15:10:17 +02:00
char path [ PATH_MAX ] = " " ;
if ( m_file . find_first_of ( " / " ) ! = 0 ) {
getcwd ( path , PATH_MAX ) ;
m_file = std : : string ( path ) + " / " + m_file ;
}
2012-08-01 13:26:04 +04:00
# endif
2011-09-08 15:10:17 +02:00
2011-08-21 13:41:23 +02:00
return ret ;
}
2011-11-03 20:44:33 +01:00
bool Config : : load ( std : : istream & ifs , boost : : program_options : : options_description & opts , const std : : string & _jid ) {
2011-08-21 13:41:23 +02:00
m_unregistered . clear ( ) ;
2011-02-10 13:12:10 +01:00
opts . add_options ( )
2011-02-13 22:47:28 +01:00
( " service.jid " , value < std : : string > ( ) - > default_value ( " " ) , " Transport Jabber ID " )
2017-06-09 21:34:39 +03:00
( " service.server " , value < std : : string > ( ) - > default_value ( " 127.0.0.1 " ) , " Server to connect to " )
2011-02-13 22:47:28 +01:00
( " service.password " , value < std : : string > ( ) - > default_value ( " " ) , " Password used to auth the server " )
( " service.port " , value < int > ( ) - > default_value ( 0 ) , " Port the server is listening on " )
2011-09-09 10:20:58 +02:00
( " service.user " , value < std : : string > ( ) - > default_value ( " " ) , " The name of user Spectrum runs as. " )
( " service.group " , value < std : : string > ( ) - > default_value ( " " ) , " The name of group Spectrum runs as. " )
2011-05-13 10:54:03 +02:00
( " service.backend " , value < std : : string > ( ) - > default_value ( " libpurple_backend " ) , " Backend " )
2011-02-14 10:27:12 +01:00
( " service.protocol " , value < std : : string > ( ) - > default_value ( " " ) , " Protocol " )
2011-10-28 08:55:47 +02:00
( " service.pidfile " , value < std : : string > ( ) - > default_value ( " /var/run/spectrum2/$jid.pid " ) , " Full path to pid file " )
2012-07-18 13:54:17 +02:00
( " service.portfile " , value < std : : string > ( ) - > default_value ( " /var/run/spectrum2/$jid.port " ) , " File to store backend_port to. It's used by spectrum2_manager. " )
2011-10-28 08:55:47 +02:00
( " service.working_dir " , value < std : : string > ( ) - > default_value ( " /var/lib/spectrum2/$jid " ) , " Working dir " )
2012-04-09 09:33:14 +02:00
( " service.allowed_servers " , value < std : : vector < std : : string > > ( ) - > multitoken ( ) , " Only users from these servers can connect " )
2011-03-29 21:59:14 +02:00
( " service.server_mode " , value < bool > ( ) - > default_value ( false ) , " True if Spectrum should behave as server " )
2011-06-12 17:22:56 +02:00
( " service.users_per_backend " , value < int > ( ) - > default_value ( 100 ) , " Number of users per one legacy network backend " )
2017-06-09 21:34:39 +03:00
( " service.backend_host " , value < std : : string > ( ) - > default_value ( " 127.0.0.1 " ) , " Host to bind backend server to " )
2011-11-07 19:51:38 +01:00
( " service.backend_port " , value < std : : string > ( ) - > default_value ( " 0 " ) , " Port to bind backend server to " )
2011-06-16 22:26:12 +02:00
( " service.cert " , value < std : : string > ( ) - > default_value ( " " ) , " PKCS#12 Certificate. " )
( " service.cert_password " , value < std : : string > ( ) - > default_value ( " " ) , " PKCS#12 Certificate password. " )
2012-04-09 01:40:30 +04:00
( " service.admin_jid " , value < std : : vector < std : : string > > ( ) - > multitoken ( ) , " Administrator jid. " )
2011-06-26 13:36:54 +02:00
( " service.admin_password " , value < std : : string > ( ) - > default_value ( " " ) , " Administrator password. " )
2011-07-29 00:50:43 +02:00
( " service.reuse_old_backends " , value < bool > ( ) - > default_value ( true ) , " True if Spectrum should use old backends which were full in the past. " )
2011-09-08 16:15:41 +02:00
( " service.idle_reconnect_time " , value < int > ( ) - > default_value ( 0 ) , " Time in seconds after which idle users are reconnected to let their backend die. " )
2011-09-27 20:16:59 +02:00
( " service.memory_collector_time " , value < int > ( ) - > default_value ( 0 ) , " Time in seconds after which backend with most memory is set to die. " )
2011-08-08 15:04:08 +02:00
( " service.more_resources " , value < bool > ( ) - > default_value ( false ) , " Allow more resources to be connected in server mode at the same time. " )
2011-09-21 22:53:37 +02:00
( " service.enable_privacy_lists " , value < bool > ( ) - > default_value ( true ) , " " )
2012-07-31 16:48:31 +02:00
( " service.enable_xhtml " , value < bool > ( ) - > default_value ( true ) , " " )
2012-10-18 09:04:20 +02:00
( " service.max_room_list_size " , value < int > ( ) - > default_value ( 100 ) , " " )
2012-12-27 15:02:21 +01:00
( " service.login_delay " , value < int > ( ) - > default_value ( 0 ) , " " )
2012-10-29 14:08:54 +01:00
( " service.jid_escaping " , value < bool > ( ) - > default_value ( true ) , " " )
2012-11-13 12:37:42 +01:00
( " service.vip_only " , value < bool > ( ) - > default_value ( false ) , " " )
( " service.vip_message " , value < std : : string > ( ) - > default_value ( " " ) , " " )
2015-11-24 12:26:26 +01:00
( " service.reconnect_all_users " , value < bool > ( ) - > default_value ( false ) , " " )
2016-02-19 12:51:17 +01:00
( " service.frontend " , value < std : : string > ( ) - > default_value ( " xmpp " ) , " " )
2016-02-18 14:30:18 +01:00
( " service.web_directory " , value < std : : string > ( ) - > default_value ( " " ) , " Full path to directory used to save files to which the links are sent to users. " )
( " service.web_url " , value < std : : string > ( ) - > default_value ( " " ) , " URL on which files in web_directory are accessible. " )
2011-11-03 20:44:33 +01:00
( " vhosts.vhost " , value < std : : vector < std : : string > > ( ) - > multitoken ( ) , " " )
2011-07-10 14:30:43 +02:00
( " identity.name " , value < std : : string > ( ) - > default_value ( " Spectrum 2 Transport " ) , " Name showed in service discovery. " )
( " identity.category " , value < std : : string > ( ) - > default_value ( " gateway " ) , " Disco#info identity category. 'gateway' by default. " )
( " identity.type " , value < std : : string > ( ) - > default_value ( " " ) , " Type of transport ('icq','msn','gg','irc', ...) " )
2011-02-14 10:27:12 +01:00
( " registration.enable_public_registration " , value < bool > ( ) - > default_value ( true ) , " True if users should be able to register. " )
( " registration.language " , value < std : : string > ( ) - > default_value ( " en " ) , " Default language for registration form " )
2011-11-12 12:56:56 +01:00
( " registration.instructions " , value < std : : string > ( ) - > default_value ( " Enter your legacy network username and password. " ) , " Instructions showed to user in registration form " )
2016-02-19 12:51:17 +01:00
( " registration.username_label " , value < std : : string > ( ) - > default_value ( " 3rd-party network username " ) , " Label for username field " )
( " registration.password_label " , value < std : : string > ( ) - > default_value ( " 3rd-party network password " ) , " Label for password field " )
2011-02-14 10:27:12 +01:00
( " registration.username_mask " , value < std : : string > ( ) - > default_value ( " " ) , " Username mask " )
2012-08-10 21:07:26 -03:00
( " registration.allowed_usernames " , value < std : : string > ( ) - > default_value ( " " ) , " Allowed usernames " )
2012-03-26 18:10:25 +02:00
( " registration.auto_register " , value < bool > ( ) - > default_value ( false ) , " Register new user automatically when the presence arrives. " )
2011-11-12 12:56:56 +01:00
( " registration.encoding " , value < std : : string > ( ) - > default_value ( " utf8 " ) , " Default encoding in registration form " )
2012-01-27 15:11:24 +01:00
( " registration.require_local_account " , value < bool > ( ) - > default_value ( false ) , " True if users have to have a local account to register to this transport from remote servers. " )
2012-12-06 14:57:38 +01:00
( " registration.notify_jid " , value < std : : vector < std : : string > > ( ) - > multitoken ( ) , " Send message to this JID if user registers/unregisters " )
2012-01-27 15:11:24 +01:00
( " registration.local_username_label " , value < std : : string > ( ) - > default_value ( " Local username: " ) , " Label for local usernme field " )
2012-01-27 18:33:18 +01:00
( " registration.local_account_server " , value < std : : string > ( ) - > default_value ( " localhost " ) , " The server on which the local accounts will be checked for validity " )
( " registration.local_account_server_timeout " , value < int > ( ) - > default_value ( 10000 ) , " Timeout when checking local user on local_account_server (msecs) " )
2012-04-08 16:44:31 +02:00
( " gateway_responder.prompt " , value < std : : string > ( ) - > default_value ( " Contact ID " ) , " Value of <prompt> </promt> field " )
( " gateway_responder.label " , value < std : : string > ( ) - > default_value ( " Enter legacy network contact ID. " ) , " Label for add contact ID field " )
2011-06-13 23:31:06 +02:00
( " database.type " , value < std : : string > ( ) - > default_value ( " none " ) , " Database type. " )
2012-02-17 11:26:46 +01:00
( " database.database " , value < std : : string > ( ) - > default_value ( " /var/lib/spectrum2/$jid/database.sql " ) , " Database used to store data " )
2011-08-06 15:31:23 +02:00
( " database.server " , value < std : : string > ( ) - > default_value ( " localhost " ) , " Database server. " )
( " database.user " , value < std : : string > ( ) - > default_value ( " " ) , " Database user. " )
( " database.password " , value < std : : string > ( ) - > default_value ( " " ) , " Database Password. " )
( " database.port " , value < int > ( ) - > default_value ( 0 ) , " Database port. " )
2011-02-13 22:47:28 +01:00
( " database.prefix " , value < std : : string > ( ) - > default_value ( " " ) , " Prefix of tables in database " )
2011-11-01 22:13:35 +01:00
( " database.encryption_key " , value < std : : string > ( ) - > default_value ( " " ) , " Encryption key. " )
2012-11-13 12:37:42 +01:00
( " database.vip_statement " , value < std : : string > ( ) - > default_value ( " " ) , " Encryption key. " )
2011-06-21 13:37:01 +02:00
( " logging.config " , value < std : : string > ( ) - > default_value ( " " ) , " Path to log4cxx config file which is used for Spectrum 2 instance " )
( " logging.backend_config " , value < std : : string > ( ) - > default_value ( " " ) , " Path to log4cxx config file which is used for backends " )
2011-07-19 20:46:38 +02:00
( " backend.default_avatar " , value < std : : string > ( ) - > default_value ( " " ) , " Full path to default avatar " )
( " backend.avatars_directory " , value < std : : string > ( ) - > default_value ( " " ) , " Path to directory with avatars " )
2011-08-18 00:29:46 +02:00
( " backend.no_vcard_fetch " , value < bool > ( ) - > default_value ( false ) , " True if VCards for buddies should not be fetched. Only avatars will be forwarded. " )
2012-05-27 03:03:25 +05:30
( " proxy.server " , value < std : : string > ( ) - > default_value ( " localhost " ) , " Proxy IP. " )
( " proxy.user " , value < std : : string > ( ) - > default_value ( " " ) , " Proxy user. " )
( " proxy.password " , value < std : : string > ( ) - > default_value ( " " ) , " Proxy Password. " )
( " proxy.port " , value < int > ( ) - > default_value ( 0 ) , " Proxy port. " )
2016-01-02 16:18:50 +01:00
( " twitter.fetch_timeout " , value < int > ( ) - > default_value ( 90000 ) , " Proxy port. " )
2012-05-27 03:03:25 +05:30
2011-02-10 13:12:10 +01:00
;
2011-08-16 21:51:19 +02:00
parsed_options parsed = parse_config_file ( ifs , opts , true ) ;
2011-11-03 20:44:33 +01:00
bool found_working = false ;
bool found_pidfile = false ;
2012-07-30 10:05:35 +02:00
bool found_portfile = false ;
2011-11-07 19:59:38 +01:00
bool found_backend_port = false ;
2012-02-17 11:33:00 +01:00
bool found_database = false ;
2011-10-28 08:55:47 +02:00
std : : string jid = " " ;
2011-11-03 20:44:33 +01:00
BOOST_FOREACH ( option & opt , parsed . options ) {
2011-10-28 08:55:47 +02:00
if ( opt . string_key = = " service.jid " ) {
2011-11-03 20:44:33 +01:00
if ( _jid . empty ( ) ) {
jid = opt . value [ 0 ] ;
}
else {
opt . value [ 0 ] = _jid ;
jid = _jid ;
}
}
else if ( opt . string_key = = " service.backend_port " ) {
2011-11-07 19:59:38 +01:00
found_backend_port = true ;
2011-11-03 20:44:33 +01:00
if ( opt . value [ 0 ] = = " 0 " ) {
2012-03-21 15:46:25 +01:00
opt . value [ 0 ] = boost : : lexical_cast < std : : string > ( getRandomPort ( _jid . empty ( ) ? jid : _jid ) ) ;
2011-11-03 20:44:33 +01:00
}
}
else if ( opt . string_key = = " service.working_dir " ) {
found_working = true ;
}
else if ( opt . string_key = = " service.pidfile " ) {
found_pidfile = true ;
2011-10-28 08:55:47 +02:00
}
2012-07-30 10:05:35 +02:00
else if ( opt . string_key = = " service.portfile " ) {
found_portfile = true ;
}
2012-02-17 11:33:00 +01:00
else if ( opt . string_key = = " database.database " ) {
found_database = true ;
}
2011-10-28 08:55:47 +02:00
}
2011-11-03 20:44:33 +01:00
if ( ! found_working ) {
std : : vector < std : : string > value ;
value . push_back ( " /var/lib/spectrum2/$jid " ) ;
parsed . options . push_back ( boost : : program_options : : basic_option < char > ( " service.working_dir " , value ) ) ;
}
if ( ! found_pidfile ) {
std : : vector < std : : string > value ;
value . push_back ( " /var/run/spectrum2/$jid.pid " ) ;
parsed . options . push_back ( boost : : program_options : : basic_option < char > ( " service.pidfile " , value ) ) ;
}
2012-07-30 10:05:35 +02:00
if ( ! found_portfile ) {
std : : vector < std : : string > value ;
value . push_back ( " /var/run/spectrum2/$jid.port " ) ;
parsed . options . push_back ( boost : : program_options : : basic_option < char > ( " service.portfile " , value ) ) ;
}
2011-11-07 19:59:38 +01:00
if ( ! found_backend_port ) {
std : : vector < std : : string > value ;
2012-03-21 15:46:25 +01:00
std : : string p = boost : : lexical_cast < std : : string > ( getRandomPort ( _jid . empty ( ) ? jid : _jid ) ) ;
2011-12-09 13:51:50 +01:00
value . push_back ( p ) ;
2011-11-07 19:59:38 +01:00
parsed . options . push_back ( boost : : program_options : : basic_option < char > ( " service.backend_port " , value ) ) ;
}
2012-02-17 11:33:00 +01:00
if ( ! found_database ) {
std : : vector < std : : string > value ;
value . push_back ( " /var/lib/spectrum2/$jid/database.sql " ) ;
parsed . options . push_back ( boost : : program_options : : basic_option < char > ( " database.database " , value ) ) ;
}
2011-11-03 20:44:33 +01:00
2012-10-03 09:48:11 +02:00
std : : list < std : : string > has_key ;
2011-10-28 08:55:47 +02:00
BOOST_FOREACH ( option & opt , parsed . options ) {
2011-08-16 21:51:19 +02:00
if ( opt . unregistered ) {
2012-10-03 09:48:11 +02:00
if ( std : : find ( has_key . begin ( ) , has_key . end ( ) , opt . string_key ) = = has_key . end ( ) ) {
has_key . push_back ( opt . string_key ) ;
2016-02-24 17:06:10 +01:00
if ( opt . value [ 0 ] = = " true " | | opt . value [ 0 ] = = " 1 " ) {
m_unregistered [ opt . string_key ] = variable_value ( true , false ) ;
}
2017-01-19 06:42:44 -05:00
else if ( opt . value [ 0 ] = = " false " | | opt . value [ 0 ] = = " 0 " ) {
2016-02-24 17:06:10 +01:00
m_unregistered [ opt . string_key ] = variable_value ( false , false ) ;
}
else {
m_unregistered [ opt . string_key ] = variable_value ( opt . value [ 0 ] , false ) ;
}
2012-10-03 09:48:11 +02:00
}
else {
std : : list < std : : string > list ;
try {
list = m_unregistered [ opt . string_key ] . as < std : : list < std : : string > > ( ) ;
}
catch ( . . . ) {
list . push_back ( m_unregistered [ opt . string_key ] . as < std : : string > ( ) ) ;
}
list . push_back ( opt . value [ 0 ] ) ;
m_unregistered [ opt . string_key ] = variable_value ( list , false ) ;
}
2011-08-16 21:51:19 +02:00
}
2011-10-28 08:55:47 +02:00
else if ( opt . value [ 0 ] . find ( " $jid " ) ! = std : : string : : npos ) {
boost : : replace_all ( opt . value [ 0 ] , " $jid " , jid ) ;
}
2011-08-16 21:51:19 +02:00
}
2012-10-03 09:48:11 +02:00
// Load configs passed by command line
if ( m_argc ! = 0 & & m_argv ) {
basic_command_line_parser < char > parser = command_line_parser ( m_argc , m_argv ) . options ( opts ) . allow_unregistered ( ) ;
parsed_options parsed = parser . run ( ) ;
BOOST_FOREACH ( option & opt , parsed . options ) {
if ( opt . unregistered & & ! opt . value . empty ( ) ) {
m_unregistered [ opt . string_key ] = variable_value ( opt . value [ 0 ] , false ) ;
}
}
store ( parsed , m_variables ) ;
}
2011-08-16 21:51:19 +02:00
store ( parsed , m_variables ) ;
2011-06-16 23:57:05 +02:00
notify ( m_variables ) ;
2011-02-10 13:12:10 +01:00
2011-02-13 22:52:46 +01:00
onConfigReloaded ( ) ;
2011-02-10 13:12:10 +01:00
return true ;
}
2011-08-21 13:41:23 +02:00
bool Config : : load ( std : : istream & ifs ) {
options_description opts ( " Transport options " ) ;
return load ( ifs , opts ) ;
}
2011-11-03 20:44:33 +01:00
bool Config : : load ( const std : : string & configfile , const std : : string & jid ) {
2012-04-08 14:17:32 +02:00
try {
options_description opts ( " Transport options " ) ;
return load ( configfile , opts , jid ) ;
} catch ( const boost : : program_options : : multiple_occurrences & e ) {
2012-04-10 10:13:22 +02:00
# if (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 42)
2012-04-08 14:17:32 +02:00
std : : cerr < < configfile < < " parsing error: " < < e . what ( ) < < " from option: " < < e . get_option_name ( ) < < std : : endl ;
2012-04-10 10:13:22 +02:00
# else
std : : cerr < < configfile < < " parsing error: " < < e . what ( ) < < std : : endl ;
# endif
2012-04-08 14:17:32 +02:00
return false ;
}
2011-02-10 13:12:10 +01:00
}
2011-07-09 17:07:44 +02:00
bool Config : : reload ( ) {
if ( m_file . empty ( ) ) {
return false ;
}
2012-09-12 08:15:10 +02:00
return load ( m_file , m_jid ) ;
2011-07-09 17:07:44 +02:00
}
2012-09-04 08:16:16 +02:00
Config : : SectionValuesCont Config : : getSectionValues ( const std : : string & sectionName ) {
SectionValuesCont sectionValues ;
std : : string sectionSearchString = sectionName + " . " ;
BOOST_FOREACH ( const Variables : : value_type & varItem , m_variables ) {
if ( boost : : istarts_with ( varItem . first , sectionSearchString ) )
sectionValues [ varItem . first ] = varItem . second ;
}
BOOST_FOREACH ( const UnregisteredCont : : value_type & varItem , m_unregistered ) {
if ( boost : : istarts_with ( varItem . first , sectionSearchString ) )
sectionValues [ varItem . first ] = varItem . second ;
}
return sectionValues ;
}
std : : string Config : : getCommandLineArgs ( ) const {
std : : ostringstream commandLineArgs ;
// Return the command-line arguments that were passed to us originally (but remove the initial .exe part)
for ( int i = 1 ; i < m_argc ; + + i ) {
commandLineArgs < < " \" " < < m_argv [ i ] < < " \" " ;
}
return commandLineArgs . str ( ) ;
}
2012-09-12 13:04:39 +02:00
void Config : : updateBackendConfig ( const std : : string & backendConfig ) {
options_description opts ( " Backend options " ) ;
opts . add_options ( )
( " registration.needPassword " , value < bool > ( ) - > default_value ( true ) , " " )
2015-12-04 20:21:36 +01:00
( " registration.needRegistration " , value < bool > ( ) - > default_value ( true ) , " " )
2012-09-12 13:04:39 +02:00
( " registration.extraField " , value < std : : vector < std : : string > > ( ) - > multitoken ( ) , " " )
2012-12-22 21:07:29 +01:00
( " features.receipts " , value < bool > ( ) - > default_value ( false ) , " " )
2013-01-26 15:22:25 +01:00
( " features.muc " , value < bool > ( ) - > default_value ( false ) , " " )
2013-02-09 11:09:38 +01:00
( " features.rawxml " , value < bool > ( ) - > default_value ( false ) , " " )
2013-02-11 21:24:19 +01:00
( " features.disable_jid_escaping " , value < bool > ( ) - > default_value ( false ) , " " )
2015-12-01 06:39:01 +01:00
( " features.send_buddies_on_login " , value < bool > ( ) - > default_value ( false ) , " " )
2012-09-12 13:04:39 +02:00
;
std : : stringstream ifs ( backendConfig ) ;
parsed_options parsed = parse_config_file ( ifs , opts , true ) ;
store ( parsed , m_backendConfig ) ;
notify ( m_backendConfig ) ;
2012-12-22 21:07:29 +01:00
onBackendConfigUpdated ( ) ;
2013-02-11 21:24:19 +01:00
if ( CONFIG_BOOL_DEFAULTED ( this , " features.disable_jid_escaping " , false ) ) {
Variables : : iterator it ( m_variables . find ( " service.jid_escaping " ) ) ;
boost : : program_options : : variable_value & vx ( it - > second ) ;
vx . value ( ) = false ;
}
2012-09-12 13:04:39 +02:00
}
2012-09-04 14:58:04 +02:00
Config * Config : : createFromArgs ( int argc , char * * argv , std : : string & error , std : : string & host , int & port ) {
2012-09-05 15:40:30 +02:00
std : : string jid ;
2012-09-04 14:58:04 +02:00
std : : ostringstream os ;
std : : string configFile ;
boost : : program_options : : variables_map vm ;
boost : : program_options : : options_description desc ( " Usage: spectrum <config_file.cfg> \n Allowed options " ) ;
desc . add_options ( )
( " help " , " help " )
( " host,h " , boost : : program_options : : value < std : : string > ( & host ) - > default_value ( " " ) , " Host to connect to " )
( " port,p " , boost : : program_options : : value < int > ( & port ) - > default_value ( 10000 ) , " Port to connect to " )
2012-09-05 15:40:30 +02:00
( " no-daemonize,n " , " Do not run spectrum as daemon " )
( " no-debug,d " , " Create coredumps on crash " )
( " jid,j " , boost : : program_options : : value < std : : string > ( & jid ) - > default_value ( " " ) , " Specify JID of transport manually " )
2012-09-04 14:58:04 +02:00
( " config " , boost : : program_options : : value < std : : string > ( & configFile ) - > default_value ( " " ) , " Config file " )
;
os < < desc ;
try
{
boost : : program_options : : positional_options_description p ;
p . add ( " config " , - 1 ) ;
boost : : program_options : : store ( boost : : program_options : : command_line_parser ( argc , argv ) .
options ( desc ) . positional ( p ) . allow_unregistered ( ) . run ( ) , vm ) ;
boost : : program_options : : notify ( vm ) ;
if ( vm . count ( " help " ) )
{
error = os . str ( ) ;
return NULL ;
}
if ( vm . count ( " config " ) = = 0 ) {
error = os . str ( ) ;
return NULL ;
}
}
catch ( std : : runtime_error & e )
{
2013-01-09 09:50:53 +01:00
error = std : : string ( e . what ( ) ) + " \n " + os . str ( ) ;
2012-09-04 14:58:04 +02:00
return NULL ;
}
catch ( . . . )
{
error = os . str ( ) ;
return NULL ;
}
Config * config = new Config ( argc , argv ) ;
if ( ! config - > load ( configFile ) ) {
error = " Can't open " + configFile + " configuration file. \n " ;
delete config ;
return NULL ;
}
return config ;
}
2011-02-10 13:12:10 +01:00
}