kmail

mailinglist-magic.cpp
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 #include "mailinglist-magic.h"
6 
7 #include "kmmessage.h"
8 
9 #include <tdeconfig.h>
10 #include <kurl.h>
11 #include <kdebug.h>
12 
13 #include <tqstringlist.h>
14 
15 using namespace KMail;
16 
17 typedef TQString (*MagicDetectorFunc) (const KMMessage *, TQCString &, TQString &);
18 
19 /* Sender: (owner-([^@]+)|([^@+]-owner)@ */
20 static TQString check_sender(const KMMessage *message,
21  TQCString &header_name,
22  TQString &header_value )
23 {
24  TQString header = message->headerField( "Sender" );
25 
26  if ( header.isEmpty() )
27  return TQString();
28 
29  if ( header.left( 6 ) == "owner-" )
30  {
31  header_name = "Sender";
32  header_value = header;
33  header = header.mid( 6, header.find( '@' ) - 6 );
34 
35  } else {
36  int index = header.find( "-owner@ " );
37  if ( index == -1 )
38  return TQString();
39 
40  header.truncate( index );
41  header_name = "Sender";
42  header_value = header;
43  }
44 
45  return header;
46 }
47 
48 /* X-BeenThere: ([^@]+) */
49 static TQString check_x_beenthere(const KMMessage *message,
50  TQCString &header_name,
51  TQString &header_value )
52 {
53  TQString header = message->headerField( "X-BeenThere" );
54  if ( header.isNull() || header.find( '@' ) == -1 )
55  return TQString();
56 
57  header_name = "X-BeenThere";
58  header_value = header;
59  header.truncate( header.find( '@' ) );
60  return header;
61 }
62 
63 /* Delivered-To:: <([^@]+) */
64 static TQString check_delivered_to(const KMMessage *message,
65  TQCString &header_name,
66  TQString &header_value )
67 {
68  TQString header = message->headerField( "Delivered-To" );
69  if ( header.isNull() || header.left(13 ) != "mailing list"
70  || header.find( '@' ) == -1 )
71  return TQString();
72 
73  header_name = "Delivered-To";
74  header_value = header;
75 
76  return header.mid( 13, header.find( '@' ) - 13 );
77 }
78 
79 /* X-Mailing-List: <?([^@]+) */
80 static TQString check_x_mailing_list(const KMMessage *message,
81  TQCString &header_name,
82  TQString &header_value )
83 {
84  TQString header = message->headerField( "X-Mailing-List");
85  if ( header.isEmpty() )
86  return TQString();
87 
88  if ( header.find( '@' ) < 1 )
89  return TQString();
90 
91  header_name = "X-Mailing-List";
92  header_value = header;
93  if ( header[0] == '<' )
94  header = header.mid(1, header.find( '@' ) - 1);
95  else
96  header.truncate( header.find( '@' ) );
97  return header;
98 }
99 
100 /* List-Id: [^<]* <([^.]+) */
101 static TQString check_list_id(const KMMessage *message,
102  TQCString &header_name,
103  TQString &header_value )
104 {
105  int lAnglePos, firstDotPos;
106  TQString header = message->headerField( "List-Id" );
107  if ( header.isEmpty() )
108  return TQString();
109 
110  lAnglePos = header.find( '<' );
111  if ( lAnglePos < 0 )
112  return TQString();
113 
114  firstDotPos = header.find( '.', lAnglePos );
115  if ( firstDotPos < 0 )
116  return TQString();
117 
118  header_name = "List-Id";
119  header_value = header.mid( lAnglePos );
120  header = header.mid( lAnglePos + 1, firstDotPos - lAnglePos - 1 );
121  return header;
122 }
123 
124 
125 /* List-Post: <mailto:[^< ]*>) */
126 static TQString check_list_post(const KMMessage *message,
127  TQCString &header_name,
128  TQString &header_value )
129 {
130  TQString header = message->headerField( "List-Post" );
131  if ( header.isEmpty() )
132  return TQString();
133 
134  int lAnglePos = header.find( "<mailto:" );
135  if ( lAnglePos < 0 )
136  return TQString();
137 
138  header_name = "List-Post";
139  header_value = header;
140  header = header.mid( lAnglePos + 8, header.length());
141  header.truncate( header.find('@') );
142  return header;
143 }
144 
145 /* Mailing-List: list ([^@]+) */
146 static TQString check_mailing_list(const KMMessage *message,
147  TQCString &header_name,
148  TQString &header_value )
149 {
150  TQString header = message->headerField( "Mailing-List");
151  if ( header.isEmpty() )
152  return TQString();
153 
154  if (header.left( 5 ) != "list " || header.find( '@' ) < 5 )
155  return TQString();
156 
157  header_name = "Mailing-List";
158  header_value = header;
159  header = header.mid(5, header.find( '@' ) - 5);
160  return header;
161 }
162 
163 
164 /* X-Loop: ([^@]+) */
165 static TQString check_x_loop(const KMMessage *message,
166  TQCString &header_name,
167  TQString &header_value ){
168  TQString header = message->headerField( "X-Loop");
169  if ( header.isEmpty() )
170  return TQString();
171 
172  if (header.find( '@' ) < 2 )
173  return TQString();
174 
175  header_name = "X-Loop";
176  header_value = header;
177  header.truncate(header.find( '@' ));
178  return header;
179 }
180 
181 /* X-ML-Name: (.+) */
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() )
187  return TQString();
188 
189  header_name = "X-ML-Name";
190  header_value = header;
191  header.truncate(header.find( '@' ));
192  return header;
193 }
194 
195 MagicDetectorFunc magic_detector[] =
196 {
197  check_list_id,
198  check_list_post,
199  check_sender,
200  check_x_mailing_list,
201  check_mailing_list,
202  check_delivered_to,
203  check_x_beenthere,
204  check_x_loop,
205  check_x_ml_name
206 };
207 
208 static const int num_detectors = sizeof (magic_detector) / sizeof (magic_detector[0]);
209 
210 static TQStringList
211 headerToAddress( const TQString& header )
212 {
213  TQStringList addr;
214  int start = 0;
215  int end = 0;
216 
217  if ( header.isEmpty() )
218  return addr;
219 
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;
223  return addr;
224  }
225  kdDebug(5006)<<"Mailing list = "<<header.mid( start, end - start )<<endl;
226  addr.append( header.mid( start, end - start ) );
227  }
228  return addr;
229 }
230 
232 MailingList::detect( const KMMessage *message )
233 {
234  MailingList mlist;
235 
236  mlist.setPostURLS( headerToAddress(
237  message->headerField( "List-Post" ) ) );
238  mlist.setHelpURLS( headerToAddress(
239  message->headerField( "List-Help" ) ) );
240  mlist.setSubscribeURLS( headerToAddress(
241  message->headerField( "List-Subscribe" ) ) );
242  mlist.setUnsubscribeURLS( headerToAddress(
243  message->headerField( "List-Unsubscribe" ) ) );
244  mlist.setArchiveURLS( headerToAddress(
245  message->headerField( "List-Archive" ) ) );
246  mlist.setId( message->headerField( "List-Id" ) );
247 
248  return mlist;
249 }
250 
251 TQString
252 MailingList::name( const KMMessage *message, TQCString &header_name,
253  TQString &header_value )
254 {
255  TQString mlist;
256  header_name = TQCString();
257  header_value = TQString();
258 
259  if ( !message )
260  return TQString();
261 
262  for (int i = 0; i < num_detectors; i++) {
263  mlist = magic_detector[i] (message, header_name, header_value);
264  if ( !mlist.isNull() )
265  return mlist;
266  }
267 
268  return TQString();
269 }
270 
271 MailingList::MailingList()
272  : mFeatures( None ), mHandler( KMail )
273 {
274 }
275 
276 int
277 MailingList::features() const
278 {
279  return mFeatures;
280 }
281 
282 void
283 MailingList::setHandler( MailingList::Handler han )
284 {
285  mHandler = han;
286 }
287 MailingList::Handler
288 MailingList::handler() const
289 {
290  return mHandler;
291 }
292 
293 void
294 MailingList::setPostURLS ( const KURL::List& lst )
295 {
296  mFeatures |= Post;
297  if ( lst.empty() ) {
298  mFeatures ^= Post;
299  }
300  mPostURLS = lst;
301 }
302 KURL::List
303 MailingList::postURLS() const
304 {
305  return mPostURLS;
306 }
307 
308 void
309 MailingList::setSubscribeURLS( const KURL::List& lst )
310 {
311  mFeatures |= Subscribe;
312  if ( lst.empty() ) {
313  mFeatures ^= Subscribe;
314  }
315 
316  mSubscribeURLS = lst;
317 }
318 KURL::List
319 MailingList::subscribeURLS() const
320 {
321  return mSubscribeURLS;
322 }
323 
324 void
325 MailingList::setUnsubscribeURLS( const KURL::List& lst )
326 {
327  mFeatures |= Unsubscribe;
328  if ( lst.empty() ) {
329  mFeatures ^= Unsubscribe;
330  }
331 
332  mUnsubscribeURLS = lst;
333 }
334 KURL::List MailingList::unsubscribeURLS() const
335 {
336  return mUnsubscribeURLS;
337 }
338 
339 void
340 MailingList::setHelpURLS( const KURL::List& lst )
341 {
342  mFeatures |= Help;
343  if ( lst.empty() ) {
344  mFeatures ^= Help;
345  }
346 
347  mHelpURLS = lst;
348 }
349 KURL::List
350 MailingList::helpURLS() const
351 {
352  return mHelpURLS;
353 }
354 
355 void
356 MailingList::setArchiveURLS( const KURL::List& lst )
357 {
358  mFeatures |= Archive;
359  if ( lst.empty() ) {
360  mFeatures ^= Archive;
361  }
362 
363  mArchiveURLS = lst;
364 }
365 KURL::List
366 MailingList::archiveURLS() const
367 {
368  return mArchiveURLS;
369 }
370 
371 void
372 MailingList::setId( const TQString& str )
373 {
374  mFeatures |= Id;
375  if ( str.isEmpty() ) {
376  mFeatures ^= Id;
377  }
378 
379  mId = str;
380 }
381 TQString
382 MailingList::id() const
383 {
384  return mId;
385 }
386 
387 void
388 MailingList::writeConfig( TDEConfig* config ) const
389 {
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() );
398 }
399 
400 void
401 MailingList::readConfig( TDEConfig* config )
402 {
403  mFeatures = config->readNumEntry( "MailingListFeatures", 0 );
404  mHandler = static_cast<MailingList::Handler>(
405  config->readNumEntry( "MailingListHandler", MailingList::KMail ) );
406 
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" );
413 }
This is a Mime Message.
Definition: kmmessage.h:68
TQString headerField(const TQCString &name) const
Returns the value of a header field with the given name.
Definition: kmmessage.cpp:2289
Class is used for all Mailing List handling inside KMail.
folderdiaquotatab.h
Definition: aboutdata.cpp:40