conf.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           conf.cpp  -  description
00003                              -------------------
00004     begin                : Thu Dec 9 2004
00005     copyright            : (C) 2004 by VooDooMan
00006     email                : vdmfun@hotmail.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010 
00011 VooDoo cIRCle - an IRC (ro)bot
00012 Copyright (C) 2004 by Marian VooDooMan Meravy (vdmfun@hotmail.com)
00013 
00014 This program is free software; you can redistribute it and/or
00015 modify it under the terms of the GNU General Public License
00016 as published by the Free Software Foundation; either version 2
00017 of the License, or (at your option) any later version.
00018 
00019 This program is distributed in the hope that it will be useful,
00020 but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License
00025 along with this program; if not, write to the Free Software
00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ****************************************************************************/
00029 
00030 /*!
00031     \file
00032     \brief Configuration file reader
00033 */
00034 
00035 //---------------------------------------------------------------------------
00036 
00037 #include <map>
00038 #include <string>
00039 
00040 #pragma hdrstop
00041 
00042 #include "conf.h"
00043 #include "ssl.h"
00044 
00045 #include "params.h"
00046 
00047 //---------------------------------------------------------------------------
00048 #ifdef __BORLANDC__
00049 #pragma package(smart_init)
00050 #endif
00051 
00052 using namespace std;
00053 
00054 bool init=false;            //!< Indicates wheter conf.txt configuration file is loaded into keys variable
00055 bool ssl_init_=false;       //!< Indicates wheter ssl.txt configuration file was loaded
00056 
00057 map<string,string> keys;    //!< If init == true, this contains loaded keys from conf.txt
00058 
00059 extern s_ssl_conf ssl_conf;
00060 
00061 /*!
00062     \brief Loads conf.txt configuration file into keys variable and sets init to true
00063     \author VooDooMan
00064     \version 1
00065     \date 2004
00066     \retval true For error
00067     \retval false Okay.
00068 */
00069 bool conf_init()
00070 {
00071     FILE* f=fopen("conf.txt","r");
00072     if(f==NULL)
00073         return true;
00074 
00075     char ln[10240+1];
00076     while(!feof(f)) {
00077         if(fgets(ln,1024*10-1,f)==NULL)
00078             break;
00079         ln[10240]=0;
00080         if(ln[strlen(ln)-1]=='\r')
00081             ln[strlen(ln)-1]=0;
00082         if(ln[strlen(ln)-1]=='\n')
00083             ln[strlen(ln)-1]=0;
00084         if(ln[strlen(ln)-1]=='\r')
00085             ln[strlen(ln)-1]=0;
00086         if(ln[strlen(ln)-1]=='\n')
00087             ln[strlen(ln)-1]=0;
00088         string id_, val_;
00089         int pos=0;
00090         for(unsigned int i1=0; i1<strlen(ln); i1++) {
00091             if(pos==0 && ln[i1]=='=') {
00092                 pos++;
00093                 continue;
00094             }
00095             if(pos==0)
00096                 id_+=ln[i1];
00097             if(pos==1)
00098                 val_+=ln[i1];
00099         }
00100         pair<string,string> p;
00101         p.first=id_;
00102         p.second=val_;
00103         keys.insert(p);
00104     }
00105     fclose(f);
00106     init=true;
00107 
00108     return false;
00109 }
00110 
00111 /*!
00112     \brief Rehashes, and re-reads conf.txt configuration file into keys variable and sets init to true
00113     \author VooDooMan
00114     \version 1
00115     \date 2004
00116 */
00117 void conf_rehash()
00118 {
00119     keys.clear();
00120     conf_init();
00121 }
00122 
00123 /*!
00124     \brief Gets variable from conf.txt configuration file. If init == false, it calls conf_init() to load file into keys variable.
00125     \author VooDooMan
00126     \version 1
00127     \date 2004
00128     \param var Key string to find in configuration file
00129     \return Value of key (var)
00130 */
00131 string conf_getvar(string var)
00132 {
00133     if(!init)
00134         conf_init();
00135     map<string,string>::iterator i;
00136     for(i=keys.begin(); i!=keys.end(); i++) {
00137         if((*i).first==var)
00138             return (*i).second;
00139     }
00140     return "";
00141 }
00142 
00143 /*!
00144     \brief Loads ssl.txt configuration file
00145     \author VooDooMan
00146     \version 1
00147     \date 2004
00148     \retval true For error
00149     \retval false Okay.
00150 */
00151 bool conf_ssl_init()
00152 {
00153     if(ssl_init_)
00154         return false;
00155 
00156     FILE* f=fopen("ssl.txt","r");
00157     if(f==NULL)
00158         return true;
00159 
00160     ssl_conf.accept_ca_certs="";
00161     ssl_conf.server_cert="";
00162     ssl_conf.server_key="";
00163     ssl_conf.client_cert="";
00164     ssl_conf.client_key="";
00165     ssl_conf.ssl_bots.clear();
00166 
00167     s_ssl_bot bot;
00168     bot.botname="";
00169     bot.cert="";
00170 
00171     bool in_general=false;
00172     string in_bot="";
00173 
00174 
00175     char ln[10240+1];
00176     while(!feof(f)) {
00177         if(fgets(ln,1024*10-1,f)==NULL)
00178             break;
00179         ln[10240]=0;
00180         if(ln[strlen(ln)-1]=='\r')
00181             ln[strlen(ln)-1]=0;
00182         if(ln[strlen(ln)-1]=='\n')
00183             ln[strlen(ln)-1]=0;
00184         if(ln[strlen(ln)-1]=='\r')
00185             ln[strlen(ln)-1]=0;
00186         if(ln[strlen(ln)-1]=='\n')
00187             ln[strlen(ln)-1]=0;
00188         if(strlen(ln) && ln[0]=='[') {
00189             if(!strcmp(ln,"[general]")) {
00190                 in_general=true;
00191                 in_bot="";
00192                 bot.botname="";
00193                 bot.cert="";
00194                 continue;
00195             }
00196             if(!in_bot.empty()) {
00197                 ssl_conf.ssl_bots.push_back(bot);
00198                 bot.botname="";
00199                 bot.cert="";
00200             }
00201             in_bot=ln;
00202             if(in_bot.length() && in_bot[0]=='[')
00203                 in_bot.erase(0,1);
00204             if(in_bot.length() && in_bot[in_bot.length()-1]==']')
00205                 in_bot.erase(in_bot.length()-1,1);
00206             bot.botname=in_bot;
00207             in_general=false;
00208             continue;
00209         }
00210         string id_, val_;
00211         int pos=0;
00212         for(unsigned int i1=0; i1<strlen(ln); i1++) {
00213             if(pos==0 && ln[i1]=='=') {
00214                 pos++;
00215                 continue;
00216             }
00217             if(pos==0)
00218                 id_+=ln[i1];
00219             if(pos==1)
00220                 val_+=ln[i1];
00221         }
00222         if(in_general && !id_.compare("accept_ca_certs")) {
00223             ssl_conf.accept_ca_certs=val_;
00224             continue;
00225         }
00226         if(in_general && !id_.compare("server_cert")) {
00227             ssl_conf.server_cert=val_;
00228             continue;
00229         }
00230         if(in_general && !id_.compare("server_key")) {
00231             ssl_conf.server_key=val_;
00232             continue;
00233         }
00234         if(in_general && !id_.compare("client_cert")) {
00235             ssl_conf.client_cert=val_;
00236             continue;
00237         }
00238         if(in_general && !id_.compare("client_key")) {
00239             ssl_conf.client_key=val_;
00240             continue;
00241         }
00242         if(!in_bot.empty() && !id_.compare("cert")) {
00243             bot.cert=val_;
00244             continue;
00245         }
00246     }
00247     fclose(f);
00248     if(!in_bot.empty()) {
00249         ssl_conf.ssl_bots.push_back(bot);
00250         bot.botname="";
00251         bot.cert="";
00252     }
00253     ssl_init_=true;
00254 
00255     return false;
00256 }
00257 

Generated on Sun Jul 10 03:22:47 2005 for VooDoo cIRCle by doxygen 1.4.3

Hosted by SourceForge.net Logo