forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilterMaster.java
226 lines (201 loc) · 7.19 KB
/
FilterMaster.java
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
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is the Grendel mail/news client.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1997
* Netscape Communications Corporation. All Rights Reserved.
*
* Class FilterMaster.
*
* Created: David Williams <[email protected]>, 1 Oct 1997.
*/
package grendel.filters;
import java.io.Reader;
import java.io.FileReader;
import java.io.File;
import java.io.EOFException;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Store;
import javax.mail.Session;
import javax.mail.MessagingException;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
import javax.mail.search.FromTerm;
import javax.mail.event.MessageCountEvent;
import grendel.storage.BerkeleyStore;
import grendel.ui.StoreFactory;
import grendel.filters.FilterSyntaxException;
import grendel.filters.MoveFilterActionFactory;
import grendel.filters.DeleteFilterActionFactory;
import grendel.filters.SubjectTermFactory;
public class FilterMaster extends Object {
// class slots
static private FilterMaster fTheMaster = null;
// class methods
public static FilterMaster Get() {
if (fTheMaster == null)
fTheMaster = new FilterMaster();
return fTheMaster;
}
// instance slots
private Hashtable fFilters; // to hold named filters
private Hashtable fFilterActionFactories;
private Hashtable fFilterTermFactories;
// constructor API
private FilterMaster() {
fFilters = new Hashtable();
fFilterTermFactories = new Hashtable();
fFilterActionFactories = new Hashtable();
// Register built-in terms.
registerFilterTermFactory(new SubjectTermFactory());
// Register built-in filter actions.
registerFilterActionFactory(new MoveFilterActionFactory());
registerFilterActionFactory(new DeleteFilterActionFactory());
// Locate and read the filters description file.
String home_dir = System.getProperties().getProperty("user.home");
File home_dir_file = new File(home_dir);
File mail_filters_file = new File(home_dir_file, "mail.filters");
FileReader fr = null;
try {
fr = new FileReader(mail_filters_file);
} catch (FileNotFoundException e) {
System.err.println("mail.filters not found, skipping loadFilters()");
fr = null;
}
// We must do this as loadFilters calls Get(), and we don't want to
// go recursive. This is a reasonable thing to do, because at this
// point, the constructor has succeeded, so we know we will be the
// FilterMaster.
fTheMaster = this;
if (fr != null)
loadFilters(fr);
}
// instance methods
private void loadFilters(Reader reader) {
IFilter filter;
FilterRulesParser parser = new FilterRulesParser(reader);
try {
while ((filter = parser.getNext()) != null) {
String name = filter.toString();
// System.err.println("got filter: " + name);
fFilters.put(name, filter);
}
} catch (FilterSyntaxException e) {
System.err.println("Error in filter rules: " + e);
} catch (IOException e) {
System.err.println("IO error reading filter rules: " + e);
}
parser = null;
}
/*package*/
void registerFilterActionFactory(IFilterActionFactory factory) {
String key = factory.getName();
if (fFilterActionFactories.get(key) != null) {
// FIXME
System.err.println("FilterMaster.registerFilterAction(): duplicate");
return;
}
fFilterActionFactories.put(key, factory);
}
/*package*/
IFilterActionFactory getFilterActionFactory(String name) {
return (IFilterActionFactory)fFilterActionFactories.get(name);
}
/*package*/
void registerFilterTermFactory(IFilterTermFactory factory) {
String key = factory.getName();
if (fFilterTermFactories.get(key) != null) {
// FIXME
System.err.println("FilterMaster.registerFilterTerm(): duplicate");
return;
}
fFilterTermFactories.put(key, factory);
}
/*package*/
IFilterTermFactory getFilterTermFactory(String name) {
return (IFilterTermFactory)fFilterTermFactories.get(name);
}
// tell me what you can do. Something like this will need to be created
// so that you can build a UI. Caller uses object.toString() on the
// elements to create user visible labels, etc..
public Enumeration getFilterActionFactories() {
return fFilterActionFactories.elements();
}
public Enumeration getFilterTermFactories() {
return fFilterTermFactories.elements();
}
public Enumeration getFilters() {
return fFilters.elements();
}
// The verb that this whole sub-system is about. Apply all the defined
// filters to the specified message.
public void applyFilters(Message message) {
boolean deleted = false;
try {
deleted = message.isSet(Flags.Flag.DELETED);
} catch (MessagingException e) {
deleted = false;
}
if (deleted)
return; // don't need to be here
Enumeration filters = getFilters();
while (filters.hasMoreElements()) {
IFilter filter = (IFilter)filters.nextElement();
if (filter.match(message)) {
filter.exorcise(message);
break; // only do first match
}
}
}
// Utilities:
public Folder getFolder(String name) {
Session session = StoreFactory.Instance().getSession();
// ### Definitely wrong:
Store store = BerkeleyStore.GetDefaultStore(session);
Folder folder;
try {
folder = store.getFolder(name);
if (!folder.exists())
folder = null;
} catch (MessagingException e) {
folder = null;
}
return folder;
}
// For Testing.
public void applyFiltersToTestInbox() {
Folder inbox = getFolder("TestInbox");
if (inbox != null) {
synchronized (inbox) {
Message[] messages;
try {
messages = inbox.getMessages();
} catch (MessagingException e) {
messages = null;
}
if (messages != null) {
for (int i = 0; i < messages.length; i++) {
applyFilters(messages[i]);
}
}
}
}
}
}