kmail

antispamconfig.cpp

Go to the documentation of this file.
00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002     antispamconfig.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2004 Patrick Audley <paudley@blackcat.ca>
00006     Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org>
00007 
00008     KMail is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     KMail is distributed in the hope that it will be useful, but
00014     WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 
00022     In addition, as a special exception, the copyright holders give
00023     permission to link the code of this program with any edition of
00024     the Qt library by Trolltech AS, Norway (or with modified versions
00025     of Qt that use the same license as Qt), and distribute linked
00026     combinations including the two.  You must obey the GNU General
00027     Public License in all respects for all of the code used other than
00028     Qt.  If you modify this file, you may extend this exception to
00029     your version of the file, but you are not obligated to do so.  If
00030     you do not wish to do so, delete this exception statement from
00031     your version.
00032 */
00033 
00034 #include "antispamconfig.h"
00035 
00036 #include <kasciistricmp.h>
00037 
00038 #include <kstaticdeleter.h>
00039 #include <kconfig.h>
00040 
00041 using namespace KMail;
00042 
00043 AntiSpamConfig * AntiSpamConfig::sSelf = 0;
00044 static KStaticDeleter<AntiSpamConfig> antispamconfig_sd;
00045 
00046 AntiSpamConfig * AntiSpamConfig::instance() {
00047   if ( !sSelf ) {
00048     antispamconfig_sd.setObject( sSelf, new AntiSpamConfig() );
00049     sSelf->readConfig();
00050   }
00051   return sSelf;
00052 }
00053 
00054 void AntiSpamConfig::readConfig()
00055 {
00056   mAgents.clear();
00057   KConfig config( "kmail.antispamrc", true );
00058   config.setReadDefaults( true );
00059   KConfigGroup general( &config, "General" );
00060   unsigned int totalTools = general.readUnsignedNumEntry( "tools", 0 );
00061   for ( unsigned int i = 1; i <= totalTools; ++i ) {
00062     KConfigGroup tool( &config, QString("Spamtool #%1").arg( i ) );
00063     if ( tool.hasKey( "ScoreHeader" ) ) {
00064       QString name      = tool.readEntry( "ScoreName" );
00065       QCString header   = tool.readEntry( "ScoreHeader" ).latin1();
00066       QCString type     = tool.readEntry( "ScoreType" ).latin1();
00067       QString score     = tool.readEntryUntranslated( "ScoreValueRegexp" );
00068       QString threshold = tool.readEntryUntranslated( "ScoreThresholdRegexp" );
00069       SpamAgentTypes typeE = SpamAgentNone;
00070       if ( kasciistricmp( type.data(), "bool" ) == 0 )
00071     typeE = SpamAgentBool;
00072       else if ( kasciistricmp( type.data(), "decimal" ) == 0 )
00073     typeE = SpamAgentFloat;
00074       else if ( kasciistricmp( type.data(), "percentage" ) == 0 )
00075     typeE = SpamAgentFloatLarge;
00076       else if ( kasciistricmp( type.data(), "adjusted" ) == 0 )
00077     typeE = SpamAgentAdjustedFloat;
00078       mAgents.append( SpamAgent( name, typeE, header, QRegExp( score ),
00079                                  QRegExp( threshold ) ) );
00080     }
00081   }
00082 }
00083 
00084 const SpamAgents AntiSpamConfig::uniqueAgents() const
00085 {
00086     QStringList seenAgents;
00087     SpamAgents agents;
00088     SpamAgents::ConstIterator it( mAgents.begin() );
00089     SpamAgents::ConstIterator end( mAgents.end() );
00090     for ( ; it != end ; ++it ) {
00091         const QString agent( ( *it ).name() );
00092         if ( seenAgents.find( agent ) == seenAgents.end() ) {
00093             agents.append( *it );
00094             seenAgents.append( agent );
00095         }
00096     }
00097     return agents;
00098 }