summaryrefslogtreecommitdiffstats
path: root/kmail/antispamwizard.h
blob: e9707e8223f33073d9e01ca309cc35669a122870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*  -*- mode: C++ -*-
    This file is part of KMail.
    Copyright (c) 2003 Andreas Gungl <a.gungl@gmx.de>

    KMail is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    KMail 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  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/
#ifndef KMAIL_ANTISPAMWIZARD_H
#define KMAIL_ANTISPAMWIZARD_H

#include <kconfig.h>
#include <klistbox.h>
#include <kwizard.h>

#include <tqcheckbox.h>
#include <tqdict.h>
#include <tqlayout.h>

class KActionCollection;
class KMFolder;
class KMFolderTree;
class TQLabel;

namespace KMail {

  class SimpleFolderTree;
  class FolderRequester;

  class ASWizInfoPage;
  class ASWizSpamRulesPage;
  class ASWizVirusRulesPage;
  class ASWizSummaryPage;

  //---------------------------------------------------------------------------
  /**
    @short KMail anti-spam wizard.
    @author Andreas Gungl <a.gungl@gmx.de>

    The wizard helps to create filter rules to let KMail operate
    with external anti-spam tools. The wizard tries to detect the
    tools, but the user can overide the preselections.
    Then the user can decide what funtionality shall be supported
    by the created filter rules.
    The wizard will append the created filter rules after the
    last existing rule to keep possible conflicts with existing
    filter configurations minimal.

    Anti-virus support was added by Fred Emmott <fred87@users.sf.net>

    The configuration for the tools to get checked and set up
    is read from a config file. The structure of the file is as
    following:
    <pre>
    [General]
    tools=1

    [Spamtool #1]
    Ident=spamassassin
    Version=0
    Priority=1
    VisibleName=&Spamassassin
    Executable=spamassassin -V
    URL=http://spamassassin.org
    PipeFilterName=SpamAssassin Check
    PipeCmdDetect=spamassassin -L
    ExecCmdSpam=sa-learn --spam --no-rebuild --single
    ExecCmdHam=sa-learn --ham --no-rebuild --single
    DetectionHeader=X-Spam-Flag
    DetectionPattern=yes
    DetectionPattern2=
    DetectionOnly=0
    UseRegExp=0
    SupportsBayes=1
    SupportsUnsure=0
    ServerSided=0
    type=spam
    </pre>
    The name of the config file is kmail.antispamrc
    and it's expected in the config dir of KDE.

  */
  class AntiSpamWizard : public KWizard
  {
    Q_OBJECT

    public:
      /** The wizard can be used for setting up anti-spam tools and for
          setting up anti-virus tools.
      */
      enum WizardMode { AntiSpam, AntiVirus };

      /** Constructor that needs to initialize from the main folder tree
        of KMail.
        @param mode The mode the wizard should run in.
        @param parent The parent widget for the wizard.
        @param mainFolderTree The main folder tree from which the folders
          are copied to allow the selection of a spam folder in a tree
          within one of the wizard pages.
      */
      AntiSpamWizard( WizardMode mode,
                      TQWidget * parent, KMFolderTree * mainFolderTree );

    protected:
      /**
        Instances of this class store the settings for one tool as read from
        the config file. Visible name and What's this text can not get
        translated!
      */
      class SpamToolConfig
      {
        public:
          SpamToolConfig() {}
          SpamToolConfig( TQString toolId, int configVersion, int prio,
                        TQString name, TQString exec, TQString url, TQString filter,
                        TQString detection, TQString spam, TQString ham,
                        TQString header, TQString pattern, TQString pattern2,
                        TQString serverPattern,
                        bool detectionOnly, bool regExp, bool bayesFilter, 
                        bool tristateDetection, WizardMode type );

          int getVersion() const { return mVersion; }
          int getPrio() const { return mPrio; }
          TQString getId()  const { return mId; }
          TQString getVisibleName()  const { return mVisibleName; }
          TQString getExecutable() const { return mExecutable; }
          TQString getWhatsThisText() const { return mWhatsThisText; }
          TQString getFilterName() const { return mFilterName; }
          TQString getDetectCmd() const { return mDetectCmd; }
          TQString getSpamCmd() const { return mSpamCmd; }
          TQString getHamCmd() const { return mHamCmd; }
          TQString getDetectionHeader() const { return mDetectionHeader; }
          TQString getDetectionPattern() const { return mDetectionPattern; }
          TQString getDetectionPattern2() const { return mDetectionPattern2; }
          TQString getServerPattern() const { return mServerPattern; }
          bool isServerBased() const;
          bool isDetectionOnly() const { return mDetectionOnly; }
          bool isUseRegExp() const { return mUseRegExp; }
          bool useBayesFilter() const { return mSupportsBayesFilter; }
          bool hasTristateDetection() const { return mSupportsUnsure; }
          WizardMode getType() const { return mType; }
          // convinience methods for types
          bool isSpamTool() const { return ( mType == AntiSpam ); }
          bool isVirusTool() const { return ( mType == AntiVirus ); }

        private:
          // used to identifiy configs for the same tool
          TQString mId;
          // The version of the config data, used for merging and
          // detecting newer configs
          int mVersion;
          // the priority of the tool in the list presented to the user
          int mPrio;
          // the name as shown by the checkbox in the dialog page
          TQString mVisibleName;
          // the command to check the existance of the tool
          TQString mExecutable;
          // the What's This help text (e.g. url for the tool)
          TQString mWhatsThisText;
          // name for the created filter in the filter list
          TQString mFilterName;
          // pipe through cmd used to detect spam messages
          TQString mDetectCmd;
          // pipe through cmd to let the tool learn a spam message
          TQString mSpamCmd;
          // pipe through cmd to let the tool learn a ham message
          TQString mHamCmd;
          // by which header are messages marked as spam
          TQString mDetectionHeader;
          // what header pattern is used to mark spam messages
          TQString mDetectionPattern;
          // what header pattern is used to mark unsure messages
          TQString mDetectionPattern2;
          // what header pattern is used in the account to check for a certain server
          TQString mServerPattern;
          // filter cannot search actively but relies on pattern by regExp or contain rule
          bool mDetectionOnly;
          // filter searches for the pattern by regExp or contain rule
          bool mUseRegExp;
          // can the tool learn spam and ham, has it a bayesian algorithm
          bool mSupportsBayesFilter;
          // differentiate between ham, spam and a third "unsure" state
          bool mSupportsUnsure;
          // Is the tool AntiSpam or AntiVirus
          WizardMode mType;
      };

      /**
        Instances of this class control reading the configuration of the
        anti-spam tools from global and user config files as well as the
        merging of different config versions.
      */
      class ConfigReader
      {
        public:
          ConfigReader( WizardMode mode,
                        TQValueList<SpamToolConfig> & configList );
          ~ConfigReader( );

          TQValueList<SpamToolConfig> & getToolList() { return mToolList; }

          void readAndMergeConfig();

        private:
          TQValueList<SpamToolConfig> & mToolList;
          KConfig *mConfig;
          WizardMode mMode;

          SpamToolConfig readToolConfig( KConfigGroup & configGroup );
          SpamToolConfig createDummyConfig();

          void mergeToolConfig( SpamToolConfig config );
          void sortToolList();
      };

      /** Evaluate the settings made and create the appropriate filter rules. */
      void accept();

    protected slots:
      /** Modify the status of the wizard to reflect the selection of spam tools. */
      void checkProgramsSelections();
      /** Modify the status of the wizard to reflect the selected functionality. */
      void checkVirusRulesSelections();
      /** Check if the spam tools are available via the PATH */
      void checkToolAvailability();
      /** Show a help topic */
      void slotHelpClicked();
      /** Create the summary text based on the current settings */
      void slotBuildSummary();

    private:
      /* Check for the availability of an executible along the PATH */
      int checkForProgram( const TQString &executable );
      /* generic checks if any option in a page is checked */
      bool anyVirusOptionChecked();
      /* convenience method calling the appropriate filter manager method */
      const TQString uniqueNameFor( const TQString & name );
      /* convenience method to sort out new and existing filters */
      void sortFilterOnExistance( const TQString & intendedFilterName,
                                  TQString & newFilters, 
                                  TQString & replaceFilters );

      /* The pages in the wizard */
      ASWizInfoPage * mInfoPage;
      ASWizSpamRulesPage * mSpamRulesPage;
      ASWizVirusRulesPage * mVirusRulesPage;
      ASWizSummaryPage * mSummaryPage;

      /* The configured tools and it's settings to be used in the wizard. */
      TQValueList<SpamToolConfig> mToolList;

      /* Are any spam tools selected? */
      bool mSpamToolsUsed;
      /* Are any virus tools selected? */
      bool mVirusToolsUsed;

      WizardMode mMode;
  };


  //---------------------------------------------------------------------------
  class ASWizPage : public QWidget
  {
    public:
      ASWizPage( TQWidget *parent, const char *name, 
                 const TQString *bannerName = 0);

    protected:
      TQBoxLayout *mLayout;

    private:
      TQPixmap *mPixmap;
      TQLabel *mBannerLabel;
  };


  //---------------------------------------------------------------------------
  class ASWizInfoPage : public ASWizPage
  {
    Q_OBJECT

    public:
      ASWizInfoPage( AntiSpamWizard::WizardMode mode,
                     TQWidget *parent, const char *name );

      void setScanProgressText( const TQString &toolName );
      void addAvailableTool( const TQString &visibleName );
      bool isProgramSelected( const TQString &visibleName );

    private slots:
      void processSelectionChange();

    signals:
      void selectionChanged();

    private:
      TQLabel *mIntroText;
      TQLabel *mScanProgressText;
      TQLabel *mSelectionHint;
      KListBox *mToolsList;
  };

  //---------------------------------------------------------------------------
  class ASWizSpamRulesPage : public ASWizPage
  {
    Q_OBJECT

    public:
      ASWizSpamRulesPage( TQWidget * parent, const char * name, KMFolderTree * mainFolderTree );

      bool markAsReadSelected() const;
      bool moveSpamSelected() const;
      bool moveUnsureSelected() const;

      TQString selectedSpamFolderName() const;
      TQString selectedUnsureFolderName() const;

      void allowUnsureFolderSelection( bool enabled );

    private slots:
      void processSelectionChange();
      void processSelectionChange( KMFolder* );

    signals:
      void selectionChanged();

    private:
      TQCheckBox * mMarkRules;
      TQCheckBox * mMoveSpamRules;
      TQCheckBox * mMoveUnsureRules;
      FolderRequester *mFolderReqForSpamFolder;
      FolderRequester *mFolderReqForUnsureFolder;
  };

  //-------------------------------------------------------------------------
  class ASWizVirusRulesPage : public ASWizPage
  {
    Q_OBJECT

    public:
      ASWizVirusRulesPage( TQWidget * parent, const char * name, KMFolderTree * mainFolderTree );

      bool pipeRulesSelected() const;
      bool moveRulesSelected() const;
      bool markReadRulesSelected() const;

      TQString selectedFolderName() const;

    private slots:
      void processSelectionChange();
    signals:
      void selectionChanged();

    private:
      TQCheckBox * mPipeRules;
      TQCheckBox * mMoveRules;
      SimpleFolderTree *mFolderTree;
      TQCheckBox * mMarkRules;
  };

  //---------------------------------------------------------------------------
  class ASWizSummaryPage : public ASWizPage
  {
    Q_OBJECT

    public:
      ASWizSummaryPage( TQWidget * parent, const char * name );

      void setSummaryText( const TQString & text );

    private:
      TQLabel * mSummaryText;
  };


} // namespace KMail

#endif // KMAIL_ANTISPAMWIZARD_H