<commit_before>/*
Copyright (c) 2013, <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <iostream>
#include <fstream>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/exceptions.hpp>
#include <boost/foreach.hpp>
#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>

#include "drachtio-config.hpp"

#include "controller.hpp"

using namespace std ;
using boost::property_tree::ptree;
using boost::property_tree::ptree_error;

namespace drachtio {

     class DrachtioConfig::Impl {
    public:
        Impl( const char* szFilename, bool isDaemonized) : m_bIsValid(false), m_adminPort(0), m_redisPort(0), m_bDaemon(isDaemonized) {

            // default timers
            m_nTimerT1 = 500 ;
            m_nTimerT2 = 4000 ;
            m_nTimerT4 = 5000 ;
            m_nTimerT1x64 = 32000 ;

            try {
                std::filebuf fb;
                if( !fb.open (szFilename,std::ios::in) ) {
                    cerr << "Unable to open configuration file: " << szFilename << endl ;
                    return ;
                }
                std::istream is(&fb);
                
                ptree pt;
                read_xml(is, pt);
                
                /* admin configuration */
                try {
                    pt.get_child("drachtio.admin") ; // will throw if doesn't exist
                    m_adminPort = pt.get<unsigned int>("drachtio.admin.<xmlattr>.port", 8022) ;
                    m_secret = pt.get<string>("drachtio.admin.<xmlattr>.secret", "admin") ;
                    m_adminAddress = pt.get<string>("drachtio.admin") ;
                } catch( boost::property_tree::ptree_bad_path& e ) {
                    cerr << "XML tag <admin> not found; this is required to provide admin socket details" << endl ;
                    return ;
                }


                /* sip contacts */

                // old way: a single contact
                try {
                    string strUrl = pt.get<string>("drachtio.sip.contact") ;
                    m_vecSipUrl.push_back( strUrl ) ;

                } catch( boost::property_tree::ptree_bad_path& e ) {

                    //good, hopefull they moved to the new way: a parent <contacts> tag containing multiple contacts
                    try {
                         BOOST_FOREACH(ptree::value_type &v, pt.get_child("drachtio.sip.contacts")) {
                            // v.first is the name of the child.
                            // v.second is the child tree.
                            if( 0 != v.first.compare("contact") ) {
                                cerr << "Invalid child element of 'contacts':  " << v.first << endl ;
                                return ;
                            }

                            m_vecSipUrl.push_back( v.second.data() );
                        }

                    } catch( boost::property_tree::ptree_bad_path& e ) {
                        //neither <contact> nor <contacts> found: default to sip:*
                        m_vecSipUrl.push_back("sip:*") ;
                    }
                }

                m_sipOutboundProxy = pt.get<string>("drachtio.sip.outbound-proxy", "") ;

                m_tlsKeyFile = pt.get<string>("drachtio.sip.tls.key-file", "") ;
                m_tlsCertFile = pt.get<string>("drachtio.sip.tls.cert-file", "") ;
                m_tlsChainFile = pt.get<string>("drachtio.sip.tls.chain-file", "") ;

                
                /* logging configuration  */
 
                m_nSofiaLogLevel = pt.get<unsigned int>("drachtio.logging.sofia-loglevel", 1) ;
                try {
                    m_syslogAddress = pt.get<string>("drachtio.logging.syslog.address") ;
                    m_sysLogPort = pt.get<unsigned int>("drachtio.logging.syslog.port", 0) ;
                    m_syslogFacility = pt.get<string>("drachtio.logging.syslog.facility") ;
                    if( !m_bDaemon ) {
                        cout << "logging to syslog at " << m_syslogAddress << ":" << m_sysLogPort << ", using facility " 
                            << m_syslogFacility << endl ;
                    }
                } catch( boost::property_tree::ptree_bad_path& e ) {
                    if( !m_bDaemon ) {
                        cout << "syslog logging not enabled" << endl ;
                    }
                }

                try {
                    m_logFileName = pt.get<string>("drachtio.logging.file.name") ;
                    m_logArchiveDirectory = pt.get<string>("drachtio.logging.file.archive", "archive") ;
                    m_rotationSize = pt.get<unsigned int>("drachtio.logging.file.size", 5) ;
                    m_maxSize = pt.get<unsigned int>("drachtio.logging.file.maxSize", 16 * 1000 * 1000) ; //max size of stored files: 16M default
                    m_minSize = pt.get<unsigned int>("drachtio.logging.file.c", 2 * 1000 * 1000 * 1000) ;//min free space on disk: 2G default
                    m_bAutoFlush = pt.get<bool>("drachtio.logging.file.auto-flush", false) ;
                    if( !m_bDaemon ) {
                        cout << "logging to text file at " << m_logFileName << ", archiving logs to " << m_logArchiveDirectory 
                            << ", rotation size: daily at midnight or when log file grows to " << m_rotationSize << "MB " << endl ; 
                    }
                } catch( boost::property_tree::ptree_bad_path& e ) {
                    if( !m_bDaemon ) {
                        cout << "text file logging not enabled" << endl ;
                    }
                }

                if( 0 == m_logFileName.length() && 0 == m_syslogAddress.length() ) {
                    cerr << "You must configure either syslog or text file logging " << endl ;
                    return ;
                }
                else {
                    string loglevel = pt.get<string>("drachtio.logging.loglevel", "info") ;
                    
                    if( 0 == loglevel.compare("notice") ) m_loglevel = log_notice ;
                    else if( 0 == loglevel.compare("error") ) m_loglevel = log_error ;
                    else if( 0 == loglevel.compare("warning") ) m_loglevel = log_warning ;
                    else if( 0 == loglevel.compare("info") ) m_loglevel = log_info ;
                    else if( 0 == loglevel.compare("debug") ) m_loglevel = log_debug ;
                    else m_loglevel = log_info ;                    
                }

                // timers
                try {
                        m_nTimerT1 = pt.get<unsigned int>("drachtio.sip.timers.t1", 500) ; 
                        m_nTimerT2 = pt.get<unsigned int>("drachtio.sip.timers.t2", 4000) ; 
                        m_nTimerT4 = pt.get<unsigned int>("drachtio.sip.timers.t4", 5000) ; 
                        m_nTimerT1x64 = pt.get<unsigned int>("drachtio.sip.timers.t1x64", 32000) ; 

                } catch( boost::property_tree::ptree_bad_path& e ) {
                    if( !m_bDaemon ) {
                        cout << "invalid timer configuration" << endl ;
                    }
                }

                // spammers
                try {
                    BOOST_FOREACH(ptree::value_type &v, pt.get_child("drachtio.sip.spammers")) {
                        // v.first is the name of the child.
                        // v.second is the child tree.
                        if( 0 == v.first.compare("header") ) {
                            ptree pt = v.second ;

                            string header = pt.get<string>("<xmlattr>.name", ""); 
                            if( header.length() > 0 ) {
                                std::vector<string> vec ;
                                BOOST_FOREACH(ptree::value_type &v, pt ) {
                                    if( v.second.data().length() > 0 ) {
                                        vec.push_back( v.second.data() ) ;
                                    }
                                }
                                std::transform(header.begin(), header.end(), header.begin(), ::tolower) ;
                                m_mapSpammers.insert( make_pair<string,vector<string> >( header,vec ) ) ;
                            }<commit_msg>fix compile bug when using C++11 to compile std::make_pair - see http://stackoverflow.com/questions/9641960/c11-make-pair-with-specified-template-parameters-doesnt-compile<commit_after>/*
Copyright (c) 2013, <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <iostream>
#include <fstream>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/exceptions.hpp>
#include <boost/foreach.hpp>
#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>

#include "drachtio-config.hpp"

#include "controller.hpp"

using namespace std ;
using boost::property_tree::ptree;
using boost::property_tree::ptree_error;

namespace drachtio {

     class DrachtioConfig::Impl {
    public:
        Impl( const char* szFilename, bool isDaemonized) : m_bIsValid(false), m_adminPort(0), m_redisPort(0), m_bDaemon(isDaemonized) {

            // default timers
            m_nTimerT1 = 500 ;
            m_nTimerT2 = 4000 ;
            m_nTimerT4 = 5000 ;
            m_nTimerT1x64 = 32000 ;

            try {
                std::filebuf fb;
                if( !fb.open (szFilename,std::ios::in) ) {
                    cerr << "Unable to open configuration file: " << szFilename << endl ;
                    return ;
                }
                std::istream is(&fb);
                
                ptree pt;
                read_xml(is, pt);
                
                /* admin configuration */
                try {
                    pt.get_child("drachtio.admin") ; // will throw if doesn't exist
                    m_adminPort = pt.get<unsigned int>("drachtio.admin.<xmlattr>.port", 8022) ;
                    m_secret = pt.get<string>("drachtio.admin.<xmlattr>.secret", "admin") ;
                    m_adminAddress = pt.get<string>("drachtio.admin") ;
                } catch( boost::property_tree::ptree_bad_path& e ) {
                    cerr << "XML tag <admin> not found; this is required to provide admin socket details" << endl ;
                    return ;
                }


                /* sip contacts */

                // old way: a single contact
                try {
                    string strUrl = pt.get<string>("drachtio.sip.contact") ;
                    m_vecSipUrl.push_back( strUrl ) ;

                } catch( boost::property_tree::ptree_bad_path& e ) {

                    //good, hopefull they moved to the new way: a parent <contacts> tag containing multiple contacts
                    try {
                         BOOST_FOREACH(ptree::value_type &v, pt.get_child("drachtio.sip.contacts")) {
                            // v.first is the name of the child.
                            // v.second is the child tree.
                            if( 0 != v.first.compare("contact") ) {
                                cerr << "Invalid child element of 'contacts':  " << v.first << endl ;
                                return ;
                            }

                            m_vecSipUrl.push_back( v.second.data() );
                        }

                    } catch( boost::property_tree::ptree_bad_path& e ) {
                        //neither <contact> nor <contacts> found: default to sip:*
                        m_vecSipUrl.push_back("sip:*") ;
                    }
                }

                m_sipOutboundProxy = pt.get<string>("drachtio.sip.outbound-proxy", "") ;

                m_tlsKeyFile = pt.get<string>("drachtio.sip.tls.key-file", "") ;
                m_tlsCertFile = pt.get<string>("drachtio.sip.tls.cert-file", "") ;
                m_tlsChainFile = pt.get<string>("drachtio.sip.tls.chain-file", "") ;

                
                /* logging configuration  */
 
                m_nSofiaLogLevel = pt.get<unsigned int>("drachtio.logging.sofia-loglevel", 1) ;
                try {
                    m_syslogAddress = pt.get<string>("drachtio.logging.syslog.address") ;
                    m_sysLogPort = pt.get<unsigned int>("drachtio.logging.syslog.port", 0) ;
                    m_syslogFacility = pt.get<string>("drachtio.logging.syslog.facility") ;
                    if( !m_bDaemon ) {
                        cout << "logging to syslog at " << m_syslogAddress << ":" << m_sysLogPort << ", using facility " 
                            << m_syslogFacility << endl ;
                    }
                } catch( boost::property_tree::ptree_bad_path& e ) {
                    if( !m_bDaemon ) {
                        cout << "syslog logging not enabled" << endl ;
                    }
                }

                try {
                    m_logFileName = pt.get<string>("drachtio.logging.file.name") ;
                    m_logArchiveDirectory = pt.get<string>("drachtio.logging.file.archive", "archive") ;
                    m_rotationSize = pt.get<unsigned int>("drachtio.logging.file.size", 5) ;
                    m_maxSize = pt.get<unsigned int>("drachtio.logging.file.maxSize", 16 * 1000 * 1000) ; //max size of stored files: 16M default
                    m_minSize = pt.get<unsigned int>("drachtio.logging.file.c", 2 * 1000 * 1000 * 1000) ;//min free space on disk: 2G default
                    m_bAutoFlush = pt.get<bool>("drachtio.logging.file.auto-flush", false) ;
                    if( !m_bDaemon ) {
                        cout << "logging to text file at " << m_logFileName << ", archiving logs to " << m_logArchiveDirectory 
                            << ", rotation size: daily at midnight or when log file grows to " << m_rotationSize << "MB " << endl ; 
                    }
                } catch( boost::property_tree::ptree_bad_path& e ) {
                    if( !m_bDaemon ) {
                        cout << "text file logging not enabled" << endl ;
                    }
                }

                if( 0 == m_logFileName.length() && 0 == m_syslogAddress.length() ) {
                    cerr << "You must configure either syslog or text file logging " << endl ;
                    return ;
                }
                else {
                    string loglevel = pt.get<string>("drachtio.logging.loglevel", "info") ;
                    
                    if( 0 == loglevel.compare("notice") ) m_loglevel = log_notice ;
                    else if( 0 == loglevel.compare("error") ) m_loglevel = log_error ;
                    else if( 0 == loglevel.compare("warning") ) m_loglevel = log_warning ;
                    else if( 0 == loglevel.compare("info") ) m_loglevel = log_info ;
                    else if( 0 == loglevel.compare("debug") ) m_loglevel = log_debug ;
                    else m_loglevel = log_info ;                    
                }

                // timers
                try {
                        m_nTimerT1 = pt.get<unsigned int>("drachtio.sip.timers.t1", 500) ; 
                        m_nTimerT2 = pt.get<unsigned int>("drachtio.sip.timers.t2", 4000) ; 
                        m_nTimerT4 = pt.get<unsigned int>("drachtio.sip.timers.t4", 5000) ; 
                        m_nTimerT1x64 = pt.get<unsigned int>("drachtio.sip.timers.t1x64", 32000) ; 

                } catch( boost::property_tree::ptree_bad_path& e ) {
                    if( !m_bDaemon ) {
                        cout << "invalid timer configuration" << endl ;
                    }
                }

                // spammers
                try {
                    BOOST_FOREACH(ptree::value_type &v, pt.get_child("drachtio.sip.spammers")) {
                        // v.first is the name of the child.
                        // v.second is the child tree.
                        if( 0 == v.first.compare("header") ) {
                            ptree pt = v.second ;

                            string header = pt.get<string>("<xmlattr>.name", ""); 
                            if( header.length() > 0 ) {
                                std::vector<string> vec ;
                                BOOST_FOREACH(ptree::value_type &v, pt ) {
                                    if( v.second.data().length() > 0 ) {
                                        vec.push_back( v.second.data() ) ;
                                    }
                                }
                                std::transform(header.begin(), header.end(), header.begin(), ::tolower) ;
                                m_mapSpammers.insert( make_pair( header,vec ) ) ;
                            }
{'fraction_non_alphanumeric': 0.10654155495978553, 'fraction_numerical': 0.011313672922252011, 'mean_word_length': 1.4768924302788844, 'pattern_counts': {'":': 2, '<': 209, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3707995', 'n_tokens_mistral': 5349, 'n_tokens_neox': 4905, 'n_words': 1214}