5 #include "mailinglist-magic.h"
13 #include <tqstringlist.h>
15 using namespace KMail;
17 typedef TQString (*MagicDetectorFunc) (
const KMMessage *, TQCString &, TQString &);
20 static TQString check_sender(
const KMMessage *message,
21 TQCString &header_name,
22 TQString &header_value )
26 if ( header.isEmpty() )
29 if ( header.left( 6 ) ==
"owner-" )
31 header_name =
"Sender";
32 header_value = header;
33 header = header.mid( 6, header.find(
'@' ) - 6 );
36 int index = header.find(
"-owner@ " );
40 header.truncate( index );
41 header_name =
"Sender";
42 header_value = header;
49 static TQString check_x_beenthere(
const KMMessage *message,
50 TQCString &header_name,
51 TQString &header_value )
53 TQString header = message->
headerField(
"X-BeenThere" );
54 if ( header.isNull() || header.find(
'@' ) == -1 )
57 header_name =
"X-BeenThere";
58 header_value = header;
59 header.truncate( header.find(
'@' ) );
64 static TQString check_delivered_to(
const KMMessage *message,
65 TQCString &header_name,
66 TQString &header_value )
68 TQString header = message->
headerField(
"Delivered-To" );
69 if ( header.isNull() || header.left(13 ) !=
"mailing list"
70 || header.find(
'@' ) == -1 )
73 header_name =
"Delivered-To";
74 header_value = header;
76 return header.mid( 13, header.find(
'@' ) - 13 );
80 static TQString check_x_mailing_list(
const KMMessage *message,
81 TQCString &header_name,
82 TQString &header_value )
84 TQString header = message->
headerField(
"X-Mailing-List");
85 if ( header.isEmpty() )
88 if ( header.find(
'@' ) < 1 )
91 header_name =
"X-Mailing-List";
92 header_value = header;
93 if ( header[0] ==
'<' )
94 header = header.mid(1, header.find(
'@' ) - 1);
96 header.truncate( header.find(
'@' ) );
101 static TQString check_list_id(
const KMMessage *message,
102 TQCString &header_name,
103 TQString &header_value )
105 int lAnglePos, firstDotPos;
106 TQString header = message->
headerField(
"List-Id" );
107 if ( header.isEmpty() )
110 lAnglePos = header.find(
'<' );
114 firstDotPos = header.find(
'.', lAnglePos );
115 if ( firstDotPos < 0 )
118 header_name =
"List-Id";
119 header_value = header.mid( lAnglePos );
120 header = header.mid( lAnglePos + 1, firstDotPos - lAnglePos - 1 );
126 static TQString check_list_post(
const KMMessage *message,
127 TQCString &header_name,
128 TQString &header_value )
130 TQString header = message->
headerField(
"List-Post" );
131 if ( header.isEmpty() )
134 int lAnglePos = header.find(
"<mailto:" );
138 header_name =
"List-Post";
139 header_value = header;
140 header = header.mid( lAnglePos + 8, header.length());
141 header.truncate( header.find(
'@') );
146 static TQString check_mailing_list(
const KMMessage *message,
147 TQCString &header_name,
148 TQString &header_value )
150 TQString header = message->
headerField(
"Mailing-List");
151 if ( header.isEmpty() )
154 if (header.left( 5 ) !=
"list " || header.find(
'@' ) < 5 )
157 header_name =
"Mailing-List";
158 header_value = header;
159 header = header.mid(5, header.find(
'@' ) - 5);
165 static TQString check_x_loop(
const KMMessage *message,
166 TQCString &header_name,
167 TQString &header_value ){
169 if ( header.isEmpty() )
172 if (header.find(
'@' ) < 2 )
175 header_name =
"X-Loop";
176 header_value = header;
177 header.truncate(header.find(
'@' ));
182 static TQString check_x_ml_name(
const KMMessage *message,
183 TQCString &header_name,
184 TQString &header_value ){
185 TQString header = message->
headerField(
"X-ML-Name");
186 if ( header.isEmpty() )
189 header_name =
"X-ML-Name";
190 header_value = header;
191 header.truncate(header.find(
'@' ));
195 MagicDetectorFunc magic_detector[] =
200 check_x_mailing_list,
208 static const int num_detectors =
sizeof (magic_detector) /
sizeof (magic_detector[0]);
211 headerToAddress(
const TQString& header )
217 if ( header.isEmpty() )
220 while ( (start = header.find(
"<", start )) != -1 ) {
221 if ( (end = header.find(
">", ++start ) ) == -1 ) {
222 kdDebug(5006)<<k_funcinfo<<
"Serious mailing list header parsing error !"<<endl;
225 kdDebug(5006)<<
"Mailing list = "<<header.mid( start, end - start )<<endl;
226 addr.append( header.mid( start, end - start ) );
232 MailingList::detect(
const KMMessage *message )
236 mlist.setPostURLS( headerToAddress(
238 mlist.setHelpURLS( headerToAddress(
240 mlist.setSubscribeURLS( headerToAddress(
242 mlist.setUnsubscribeURLS( headerToAddress(
244 mlist.setArchiveURLS( headerToAddress(
252 MailingList::name(
const KMMessage *message, TQCString &header_name,
253 TQString &header_value )
256 header_name = TQCString();
257 header_value = TQString();
262 for (
int i = 0; i < num_detectors; i++) {
263 mlist = magic_detector[i] (message, header_name, header_value);
264 if ( !mlist.isNull() )
271 MailingList::MailingList()
272 : mFeatures( None ), mHandler(
KMail )
277 MailingList::features()
const
283 MailingList::setHandler( MailingList::Handler han )
288 MailingList::handler()
const
294 MailingList::setPostURLS (
const KURL::List& lst )
303 MailingList::postURLS()
const
309 MailingList::setSubscribeURLS(
const KURL::List& lst )
311 mFeatures |= Subscribe;
313 mFeatures ^= Subscribe;
316 mSubscribeURLS = lst;
319 MailingList::subscribeURLS()
const
321 return mSubscribeURLS;
325 MailingList::setUnsubscribeURLS(
const KURL::List& lst )
327 mFeatures |= Unsubscribe;
329 mFeatures ^= Unsubscribe;
332 mUnsubscribeURLS = lst;
334 KURL::List MailingList::unsubscribeURLS()
const
336 return mUnsubscribeURLS;
340 MailingList::setHelpURLS(
const KURL::List& lst )
350 MailingList::helpURLS()
const
356 MailingList::setArchiveURLS(
const KURL::List& lst )
358 mFeatures |= Archive;
360 mFeatures ^= Archive;
366 MailingList::archiveURLS()
const
372 MailingList::setId(
const TQString& str )
375 if ( str.isEmpty() ) {
382 MailingList::id()
const
388 MailingList::writeConfig( TDEConfig* config )
const
390 config->writeEntry(
"MailingListFeatures", mFeatures );
391 config->writeEntry(
"MailingListHandler", mHandler );
392 config->writeEntry(
"MailingListId", mId );
393 config->writeEntry(
"MailingListPostingAddress", mPostURLS.toStringList() );
394 config->writeEntry(
"MailingListSubscribeAddress", mSubscribeURLS.toStringList() );
395 config->writeEntry(
"MailingListUnsubscribeAddress", mUnsubscribeURLS.toStringList() );
396 config->writeEntry(
"MailingListArchiveAddress", mArchiveURLS.toStringList() );
397 config->writeEntry(
"MailingListHelpAddress", mHelpURLS.toStringList() );
401 MailingList::readConfig( TDEConfig* config )
403 mFeatures = config->readNumEntry(
"MailingListFeatures", 0 );
404 mHandler =
static_cast<MailingList::Handler
>(
405 config->readNumEntry(
"MailingListHandler", MailingList::KMail ) );
407 mId = config->readEntry(
"MailingListId");
408 mPostURLS = config->readListEntry(
"MailingListPostingAddress" );
409 mSubscribeURLS = config->readListEntry(
"MailingListSubscribeAddress" );
410 mUnsubscribeURLS = config->readListEntry(
"MailingListUnsubscribeAddress" );
411 mArchiveURLS = config->readListEntry(
"MailingListArchiveAddress" );
412 mHelpURLS = config->readListEntry(
"MailingListHelpAddress" );
TQString headerField(const TQCString &name) const
Returns the value of a header field with the given name.
Class is used for all Mailing List handling inside KMail.