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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "mailinglist-magic.h"
#include "kmmessage.h"
#include <tdeconfig.h>
#include <kurl.h>
#include <kdebug.h>
#include <tqstringlist.h>
using namespace KMail;
typedef TQString (*MagicDetectorFunc) (const KMMessage *, TQCString &, TQString &);
/* Sender: (owner-([^@]+)|([^@+]-owner)@ */
static TQString check_sender(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
TQString header = message->headerField( "Sender" );
if ( header.isEmpty() )
return TQString();
if ( header.left( 6 ) == "owner-" )
{
header_name = "Sender";
header_value = header;
header = header.mid( 6, header.find( '@' ) - 6 );
} else {
int index = header.find( "-owner@ " );
if ( index == -1 )
return TQString();
header.truncate( index );
header_name = "Sender";
header_value = header;
}
return header;
}
/* X-BeenThere: ([^@]+) */
static TQString check_x_beenthere(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
TQString header = message->headerField( "X-BeenThere" );
if ( header.isNull() || header.find( '@' ) == -1 )
return TQString();
header_name = "X-BeenThere";
header_value = header;
header.truncate( header.find( '@' ) );
return header;
}
/* Delivered-To:: <([^@]+) */
static TQString check_delivered_to(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
TQString header = message->headerField( "Delivered-To" );
if ( header.isNull() || header.left(13 ) != "mailing list"
|| header.find( '@' ) == -1 )
return TQString();
header_name = "Delivered-To";
header_value = header;
return header.mid( 13, header.find( '@' ) - 13 );
}
/* X-Mailing-List: <?([^@]+) */
static TQString check_x_mailing_list(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
TQString header = message->headerField( "X-Mailing-List");
if ( header.isEmpty() )
return TQString();
if ( header.find( '@' ) < 1 )
return TQString();
header_name = "X-Mailing-List";
header_value = header;
if ( header[0] == '<' )
header = header.mid(1, header.find( '@' ) - 1);
else
header.truncate( header.find( '@' ) );
return header;
}
/* List-Id: [^<]* <([^.]+) */
static TQString check_list_id(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
int lAnglePos, firstDotPos;
TQString header = message->headerField( "List-Id" );
if ( header.isEmpty() )
return TQString();
lAnglePos = header.find( '<' );
if ( lAnglePos < 0 )
return TQString();
firstDotPos = header.find( '.', lAnglePos );
if ( firstDotPos < 0 )
return TQString();
header_name = "List-Id";
header_value = header.mid( lAnglePos );
header = header.mid( lAnglePos + 1, firstDotPos - lAnglePos - 1 );
return header;
}
/* List-Post: <mailto:[^< ]*>) */
static TQString check_list_post(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
TQString header = message->headerField( "List-Post" );
if ( header.isEmpty() )
return TQString();
int lAnglePos = header.find( "<mailto:" );
if ( lAnglePos < 0 )
return TQString();
header_name = "List-Post";
header_value = header;
header = header.mid( lAnglePos + 8, header.length());
header.truncate( header.find('@') );
return header;
}
/* Mailing-List: list ([^@]+) */
static TQString check_mailing_list(const KMMessage *message,
TQCString &header_name,
TQString &header_value )
{
TQString header = message->headerField( "Mailing-List");
if ( header.isEmpty() )
return TQString();
if (header.left( 5 ) != "list " || header.find( '@' ) < 5 )
return TQString();
header_name = "Mailing-List";
header_value = header;
header = header.mid(5, header.find( '@' ) - 5);
return header;
}
/* X-Loop: ([^@]+) */
static TQString check_x_loop(const KMMessage *message,
TQCString &header_name,
TQString &header_value ){
TQString header = message->headerField( "X-Loop");
if ( header.isEmpty() )
return TQString();
if (header.find( '@' ) < 2 )
return TQString();
header_name = "X-Loop";
header_value = header;
header.truncate(header.find( '@' ));
return header;
}
/* X-ML-Name: (.+) */
static TQString check_x_ml_name(const KMMessage *message,
TQCString &header_name,
TQString &header_value ){
TQString header = message->headerField( "X-ML-Name");
if ( header.isEmpty() )
return TQString();
header_name = "X-ML-Name";
header_value = header;
header.truncate(header.find( '@' ));
return header;
}
MagicDetectorFunc magic_detector[] =
{
check_list_id,
check_list_post,
check_sender,
check_x_mailing_list,
check_mailing_list,
check_delivered_to,
check_x_beenthere,
check_x_loop,
check_x_ml_name
};
static const int num_detectors = sizeof (magic_detector) / sizeof (magic_detector[0]);
static TQStringList
headerToAddress( const TQString& header )
{
TQStringList addr;
int start = 0;
int end = 0;
if ( header.isEmpty() )
return addr;
while ( (start = header.find( "<", start )) != -1 ) {
if ( (end = header.find( ">", ++start ) ) == -1 ) {
kdDebug(5006)<<k_funcinfo<<"Serious mailing list header parsing error !"<<endl;
return addr;
}
kdDebug(5006)<<"Mailing list = "<<header.mid( start, end - start )<<endl;
addr.append( header.mid( start, end - start ) );
}
return addr;
}
MailingList
MailingList::detect( const KMMessage *message )
{
MailingList mlist;
mlist.setPostURLS( headerToAddress(
message->headerField( "List-Post" ) ) );
mlist.setHelpURLS( headerToAddress(
message->headerField( "List-Help" ) ) );
mlist.setSubscribeURLS( headerToAddress(
message->headerField( "List-Subscribe" ) ) );
mlist.setUnsubscribeURLS( headerToAddress(
message->headerField( "List-Unsubscribe" ) ) );
mlist.setArchiveURLS( headerToAddress(
message->headerField( "List-Archive" ) ) );
mlist.setId( message->headerField( "List-Id" ) );
return mlist;
}
TQString
MailingList::name( const KMMessage *message, TQCString &header_name,
TQString &header_value )
{
TQString mlist;
header_name = TQCString();
header_value = TQString();
if ( !message )
return TQString();
for (int i = 0; i < num_detectors; i++) {
mlist = magic_detector[i] (message, header_name, header_value);
if ( !mlist.isNull() )
return mlist;
}
return TQString();
}
MailingList::MailingList()
: mFeatures( None ), mHandler( KMail )
{
}
int
MailingList::features() const
{
return mFeatures;
}
void
MailingList::setHandler( MailingList::Handler han )
{
mHandler = han;
}
MailingList::Handler
MailingList::handler() const
{
return mHandler;
}
void
MailingList::setPostURLS ( const KURL::List& lst )
{
mFeatures |= Post;
if ( lst.empty() ) {
mFeatures ^= Post;
}
mPostURLS = lst;
}
KURL::List
MailingList::postURLS() const
{
return mPostURLS;
}
void
MailingList::setSubscribeURLS( const KURL::List& lst )
{
mFeatures |= Subscribe;
if ( lst.empty() ) {
mFeatures ^= Subscribe;
}
mSubscribeURLS = lst;
}
KURL::List
MailingList::subscribeURLS() const
{
return mSubscribeURLS;
}
void
MailingList::setUnsubscribeURLS( const KURL::List& lst )
{
mFeatures |= Unsubscribe;
if ( lst.empty() ) {
mFeatures ^= Unsubscribe;
}
mUnsubscribeURLS = lst;
}
KURL::List MailingList::unsubscribeURLS() const
{
return mUnsubscribeURLS;
}
void
MailingList::setHelpURLS( const KURL::List& lst )
{
mFeatures |= Help;
if ( lst.empty() ) {
mFeatures ^= Help;
}
mHelpURLS = lst;
}
KURL::List
MailingList::helpURLS() const
{
return mHelpURLS;
}
void
MailingList::setArchiveURLS( const KURL::List& lst )
{
mFeatures |= Archive;
if ( lst.empty() ) {
mFeatures ^= Archive;
}
mArchiveURLS = lst;
}
KURL::List
MailingList::archiveURLS() const
{
return mArchiveURLS;
}
void
MailingList::setId( const TQString& str )
{
mFeatures |= Id;
if ( str.isEmpty() ) {
mFeatures ^= Id;
}
mId = str;
}
TQString
MailingList::id() const
{
return mId;
}
void
MailingList::writeConfig( TDEConfig* config ) const
{
config->writeEntry( "MailingListFeatures", mFeatures );
config->writeEntry( "MailingListHandler", mHandler );
config->writeEntry( "MailingListId", mId );
config->writeEntry( "MailingListPostingAddress", mPostURLS.toStringList() );
config->writeEntry( "MailingListSubscribeAddress", mSubscribeURLS.toStringList() );
config->writeEntry( "MailingListUnsubscribeAddress", mUnsubscribeURLS.toStringList() );
config->writeEntry( "MailingListArchiveAddress", mArchiveURLS.toStringList() );
config->writeEntry( "MailingListHelpAddress", mHelpURLS.toStringList() );
}
void
MailingList::readConfig( TDEConfig* config )
{
mFeatures = config->readNumEntry( "MailingListFeatures", 0 );
mHandler = static_cast<MailingList::Handler>(
config->readNumEntry( "MailingListHandler", MailingList::KMail ) );
mId = config->readEntry("MailingListId");
mPostURLS = config->readListEntry( "MailingListPostingAddress" );
mSubscribeURLS = config->readListEntry( "MailingListSubscribeAddress" );
mUnsubscribeURLS = config->readListEntry( "MailingListUnsubscribeAddress" );
mArchiveURLS = config->readListEntry( "MailingListArchiveAddress" );
mHelpURLS = config->readListEntry( "MailingListHelpAddress" );
}
|