Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Preferences.cc

Go to the documentation of this file.
00001 /* Preferences.cc - source file for the mailfilter program
00002  * Copyright (c) 2000  Andreas Bauer <baueran@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00017  * USA.
00018  */
00019 
00020 #include <string>
00021 #include <list>
00022 #include <fstream>
00023 #include <stdlib.h>
00024 #include "Preferences.hh"
00025 #include "mailfilter.hh"
00026 
00027 namespace pref {
00028 
00029   Preferences::Preferences(string file) {
00030     string curLine;
00031     serverInfo popServer;
00032     maxsize = 0;
00033     icase = TRUE;
00034     mode = VERBOSE;
00035     ifstream prefsFile(file.c_str());
00036     
00037     if (prefsFile) {
00038       while (getline(prefsFile, curLine)) {
00039         if (curLine[0] == '#')
00040       continue;
00041     if ( (curLine.find("SERVER") == 0))
00042       popServer.name = *(getPrefValues(curLine)).begin();
00043     else if ( (curLine.find("USER") == 0))
00044       popServer.user = *(getPrefValues(curLine)).begin();
00045     else if ( (curLine.find("PASS") == 0))
00046       popServer.pass = getStoredPassword(curLine);
00047     else if ( (curLine.find("PORT") == 0)) {
00048       popServer.port = atoi( (*(getPrefValues(curLine)).begin()).c_str() );
00049       servers.push_back(popServer);    // always push here, cause after this either another server gets configured, or we're done
00050     }
00051     else if ( (curLine.find("MAXSIZE") == 0))
00052       maxsize = atoi( (*(getPrefValues(curLine)).begin()).c_str() );
00053     else if ( (curLine.find("LOGFILE") == 0))
00054       logfile = (*(getPrefValues(curLine)).begin());
00055     else if ( (curLine.find("MODE") == 0)) {
00056       if (*(getPrefValues(curLine)).begin() == "silent")
00057         mode = SILENT;
00058       else
00059         mode = VERBOSE;
00060     }
00061     else if( (curLine.find("ICASE") == 0)) {
00062       if (*(getPrefValues(curLine)).begin() == "no")
00063         icase = FALSE;
00064     }
00065     else if ( curLine.find("DENY") == 0 )
00066       filters.push_back(getFilter(curLine));
00067       }
00068     }
00069     else
00070       throw IOException();
00071     
00072     prefsFile.close();
00073   }
00074   
00075   
00076   Preferences::~Preferences() {
00077     servers.clear();
00078     filters.clear();
00079   }
00080   
00081   
00082   vector<serverInfo>* Preferences::getServers() {
00083     return &servers;
00084   }
00085   
00086   
00087   // Gets the values from an entry inside the preferences file
00088   // e.g. TEST=value1, value2
00089   // would result in returning 'value1' and 'value2' (I've stolen this from my own application Archiebald)
00090   list<string> Preferences::getPrefValues(string prefLine) {
00091     list<string> parsedValues;
00092     int startSearch = prefLine.find('=') + 1;
00093     int newPos = 0;
00094     
00095     if (startSearch != -1) {
00096       for (int i = startSearch; i <= (int)prefLine.length(); i++) {
00097     newPos = prefLine.find(',', i);
00098     
00099     if (newPos == -1) {
00100       parsedValues.push_back( prefLine.substr( i, prefLine.length() ) );
00101       break;
00102     }
00103     else {
00104       parsedValues.push_back( prefLine.substr( i, newPos - i ) );
00105       i = newPos + 1;
00106     }
00107       }
00108       
00109       return parsedValues;
00110     }
00111     else throw MalformedPrefsFile();
00112   }
00113 
00114 
00115   int Preferences::getMaxsize() {
00116     return maxsize;
00117   }
00118 
00119   bool Preferences::getIcase(){
00120     return icase;
00121   }
00122 
00123   // Extract and compile filter information from preferences file
00124   string Preferences::getFilter(string curLine) {
00125     int start = curLine.find('=') + 1;
00126 
00127     if (start != -1) {
00128       return(curLine.substr(start));
00129     }
00130     else
00131       throw MalformedPrefsFile();
00132   }
00133 
00134 
00135   // Extract password from preferences file
00136   string Preferences::getStoredPassword(string curLine) {
00137     int start = curLine.find('=') + 1;
00138     
00139     if (start != -1) {
00140       return(curLine.substr(start));
00141     }
00142     else
00143       throw MalformedPrefsFile();
00144   }
00145 
00146 
00147   string Preferences::getLogfile() {
00148     return logfile;
00149   }
00150 
00151 
00152   void Preferences::setLogfile(string newLogfile) {
00153     logfile = newLogfile;
00154   }
00155 
00156   int Preferences::getMode() {
00157     return mode;
00158   }
00159 
00160 
00161   void Preferences::setMode(int newMode) {
00162     mode = newMode;
00163   }
00164 
00165 
00166   // Return vector with all compiled filters
00167   vector<string>* Preferences::getFilters() {
00168     return &filters;
00169   }
00170 
00171 }

Generated at Tue Dec 19 19:21:03 2000 for mailfilter by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000