summaryrefslogtreecommitdiffstats
path: root/kshowmail/headerfilter.cpp
blob: 10bedf0cbd4e5507c79b0e13a9772a35328984ca (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
//
// C++ Implementation: headerfilter
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "headerfilter.h"

HeaderFilter::HeaderFilter()
{
  //get the application config object
  config = KApplication::kApplication()->config();

  //the filter list shall delete all filters if it will be deleted itself
  filters.setAutoDelete( true );

  //load settings
  load();
}


HeaderFilter::~HeaderFilter()
{
}

FilterAction_Type HeaderFilter::check( QString from, QString to, uint size, QString subject, QString header, QString account, QString& mailboxName ) const
{
  //return PASS, if filter is not active
  if( !active )
    return FActPass;
  
  //check for matching with blacklist or whitelist
  FilterAction_Type action = senderlist.check( from );
  if( action != FActNone ) return action;

  //check for matching with filters
  QPtrListIterator<FilterItem> it( filters );
  FilterItem* filter;
  while( ( filter = it.current() ) != NULL )
  {
    ++it;
    action = filter->check( from, to, size, subject, header, account, mailboxName );

    if( action != FActNone ) return action;
  }

  //no matching; return default action
  if( defaultAction == FActMove )
  {
    mailboxName.remove( 0, mailboxName.length() );
    mailboxName.append( mailbox );
  }
  return defaultAction;

}

void HeaderFilter::load( )
{
  //order sender list to load its settings
  senderlist.load();

  //set group
  config->setGroup( CONFIG_GROUP_FILTER );

  //get filter active state
  active = config->readBoolEntry( CONFIG_ENTRY_FILTER_ACTIVE, DEFAULT_FILTER_ACTIVE );

  //get number of filter items
  numberFilterItems = config->readNumEntry( CONFIG_ENTRY_FILTER_NUMBER_OF_FILTERS, 0 );

  //get default action
  switch( config->readNumEntry( CONFIG_ENTRY_FILTER_OTHERS_ACTION, DEFAULT_FILTER_OTHERS_ACTION ) )
  {
    case CONFIG_VALUE_FILTER_OTHERS_ACTION_PASS       : defaultAction = FActPass; break;
    case CONFIG_VALUE_FILTER_OTHERS_ACTION_DELETE     : defaultAction = FActDelete; break;
    case CONFIG_VALUE_FILTER_OTHERS_ACTION_MARK       : defaultAction = FActMark; break;
    case CONFIG_VALUE_FILTER_OTHERS_ACTION_MOVE       : defaultAction = FActMove; break;
    case CONFIG_VALUE_FILTER_OTHERS_ACTION_IGNORE     : defaultAction = FActIgnore; break;
    case CONFIG_VALUE_FILTER_OTHERS_ACTION_SPAMCHECK  : defaultAction = FActSpamcheck; break;
    default                                           : kdError() << "Header Filter: Unknown default filter action. Set PASS." << endl;
                                                        defaultAction = FActPass;
                                                        break;
  }

  //get mailbox name if default action is MOVE
  if( defaultAction == FActMove )
    mailbox = config->readEntry( CONFIG_ENTRY_FILTER_OTHERS_MAILBOX, DEFAULT_FILTER_ACTION_MOVE_MAILBOX );

  //now we get the filters
  //first clear the list
  filters.clear();

  for( uint filterNr = 1; filterNr <= numberFilterItems; filterNr++ )
  {
    filters.append( new FilterItem( filterNr ) ); //a new created filter item loads its settings itself
  }
}

void HeaderFilter::print( )
{
  kdDebug() << "Header Filter Settings:" << endl;
  kdDebug() << "-----------------------" << endl;

  //print active state
  if( active )
    kdDebug() << "Header filter is active." << endl;
  else
    kdDebug() << "Header filter is not active." << endl;

  //print settings of black and white list
  senderlist.print();

  //print filters
  kdDebug() << endl;
  kdDebug() << "Number of filters: " << numberFilterItems << endl << endl;

  QPtrListIterator<FilterItem> it( filters );
  FilterItem* filter;
  while( ( filter = it.current() ) != NULL )
  {
    ++it;
    filter->print();
    kdDebug() << endl;
  }

  //print default action for not matched mails
  switch( defaultAction )
  {
    case FActPass       : kdDebug() << "Default action for other mails: PASS" << endl; break;
    case FActDelete     : kdDebug() << "Default action for other mails: DELETE" << endl; break;
    case FActMark       : kdDebug() << "Default action for other mails: MARK" << endl; break;
    case FActIgnore     : kdDebug() << "Default action for other mails: IGNORE" << endl;
    case FActMove       : kdDebug() << "Default action for other mails: MOVE to " << mailbox << endl; break;
    case FActSpamcheck  : kdDebug() << "Default action for other mails: SPAMCHECK" << endl; break;
    default             : kdDebug() << "Unknown default action for other mails" << endl; break;
  }


}

bool HeaderFilter::isActive()
{
  return active;
}