tdeioslave/imap4

mailaddress.cpp
1 /**********************************************************************
2  *
3  * mailaddress.cpp - mail address parser
4  * Copyright (C) 2000 Sven Carstens <s.carstens@gmx.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Send comments and bug fixes to
21  *
22  *********************************************************************/
23 
24 
25 #include "mailaddress.h"
26 #include "rfcdecoder.h"
27 #include "mimehdrline.h"
28 #include <kmime_util.h>
29 
30 mailAddress::mailAddress ()
31 {
32 }
33 
34 mailAddress::mailAddress (const mailAddress & lr):
35 user (lr.user),
36 host (lr.host),
37 rawFullName (lr.rawFullName),
38 rawComment (lr.rawComment)
39 {
40 // kdDebug(7116) << "mailAddress::mailAddress - " << getStr() << endl;
41 }
42 
43 mailAddress & mailAddress::operator = (const mailAddress & lr)
44 {
45  // Avoid a = a.
46  if (this == &lr)
47  return *this;
48 
49  user = lr.user;
50  host = lr.host;
51  rawFullName = lr.rawFullName;
52  rawComment = lr.rawComment;
53 
54 // kdDebug(7116) << "mailAddress::operator= - " << getStr() << endl;
55 
56  return *this;
57 }
58 
59 
60 
61 
62 mailAddress::~mailAddress ()
63 {
64 }
65 
66 mailAddress::mailAddress (char *aCStr)
67 {
68  parseAddress (aCStr);
69 }
70 
71 int
72 mailAddress::parseAddress (char *aCStr)
73 {
74  int retVal = 0;
75  int skip;
76  uint len;
77  int pt;
78 
79  if (aCStr)
80  {
81  //skip leading white space
82  skip = mimeHdrLine::skipWS ((const char *) aCStr);
83  if (skip > 0)
84  {
85  aCStr += skip;
86  retVal += skip;
87  }
88  while (*aCStr)
89  {
90  int advance;
91 
92  switch (*aCStr)
93  {
94  case '"':
95  advance = mimeHdrLine::parseQuoted ('"', '"', aCStr);
96  rawFullName += TQCString (aCStr, advance + 1);
97  break;
98  case '(':
99  advance = mimeHdrLine::parseQuoted ('(', ')', aCStr);
100  rawComment += TQCString (aCStr, advance + 1);
101  break;
102  case '<':
103  advance = mimeHdrLine::parseQuoted ('<', '>', aCStr);
104  user = TQCString (aCStr, advance + 1); // copy it
105  len = advance;
106  user = user.mid (1, len - 2); // strip <>
107  len -= 2;
108  pt = user.find('@');
109  host = user.right (len - pt - 1); // split it into host
110  user.truncate(pt); // and user
111  break;
112  default:
113  advance = mimeHdrLine::parseWord ((const char *) aCStr);
114  //if we've seen a FQ mailname the rest must be quoted or is just junk
115  if (user.isEmpty ())
116  {
117  if (*aCStr != ',')
118  {
119  rawFullName += TQCString (aCStr, advance + 1);
120  if (mimeHdrLine::skipWS ((const char *) &aCStr[advance]) > 0)
121  {
122  rawFullName += ' ';
123  }
124  }
125  }
126  break;
127  }
128  if (advance)
129  {
130  retVal += advance;
131  aCStr += advance;
132  }
133  else
134  break;
135  advance = mimeHdrLine::skipWS ((const char *) aCStr);
136  if (advance > 0)
137  {
138  retVal += advance;
139  aCStr += advance;
140  }
141  //reached end of current address
142  if (*aCStr == ',')
143  {
144  advance++;
145  break;
146  }
147  }
148  //let's see what we've got
149  if (rawFullName.isEmpty ())
150  {
151  if (user.isEmpty ())
152  retVal = 0;
153  else
154  {
155  if (host.isEmpty ())
156  {
157  rawFullName = user;
158  user.truncate(0);
159  }
160  }
161  }
162  else if (user.isEmpty ())
163  {
164  pt = rawFullName.find ('@');
165  if (pt >= 0)
166  {
167  user = rawFullName;
168  host = user.right (user.length () - pt - 1);
169  user.truncate(pt);
170  rawFullName.truncate(0);
171  }
172  }
173 
174 #if 0
175 // dead
176  if (!rawFullName.isEmpty ())
177  {
178 // if(fullName[0] == '"')
179 // fullName = fullName.mid(1,fullName.length()-2);
180 // fullName = fullName.simplifyWhiteSpace().stripWhiteSpace();
181 // fullName = rfcDecoder::decodeRFC2047String(fullName.ascii());
182  }
183 #endif
184  if (!rawComment.isEmpty ())
185  {
186  if (rawComment[0] == '(')
187  rawComment = rawComment.mid (1, rawComment.length () - 2);
188  rawComment = rawComment.stripWhiteSpace ();
189 // comment = rfcDecoder::decodeRFC2047String(comment.ascii());
190  }
191  }
192  else
193  {
194  //debug();
195  }
196  return retVal;
197 }
198 
199 const TQCString
200 mailAddress::getStr () const
201 {
202  TQCString retVal(128); // Should be generally big enough
203 
204  if (!rawFullName.isEmpty ())
205  {
206  TQCString tmpName( rawFullName );
207  KMime::addQuotes( tmpName, false );
208  retVal = tmpName + " ";
209  }
210  if (!user.isEmpty ())
211  {
212  retVal += '<';
213  retVal += user;
214  if (!host.isEmpty ()) {
215  retVal += '@';
216  retVal += host;
217  }
218  retVal += '>';
219  }
220  if (!rawComment.isEmpty ())
221  {
222  retVal += " (" + rawComment + ')';
223  }
224 // kdDebug(7116) << "mailAddress::getStr - '" << retVal << "'" << endl;
225  return retVal;
226 }
227 
228 bool
229 mailAddress::isEmpty () const
230 {
231  return user.isEmpty ();
232 }
233 
234 void
235 mailAddress::setFullName (const TQString & _str)
236 {
237  rawFullName = rfcDecoder::encodeRFC2047String (_str).latin1 ();
238 }
239 const TQString
240 mailAddress::getFullName () const
241 {
242  return rfcDecoder::decodeRFC2047String (rawFullName);
243 }
244 
245 void
246 mailAddress::setCommentRaw (const TQCString & _str)
247 {
248  rawComment = _str;
249 }
250 
251 void
252 mailAddress::setComment (const TQString & _str)
253 {
254  rawComment = rfcDecoder::encodeRFC2047String (_str).latin1 ();
255 }
256 const TQString
257 mailAddress::getComment () const
258 {
259  return rfcDecoder::decodeRFC2047String (rawComment);
260 }
261 
262 const TQCString &
263 mailAddress::getCommentRaw () const
264 {
265  return rawComment;
266 }
267 
268 TQString
269 mailAddress::emailAddrAsAnchor (const mailAddress & adr, bool shortAdr)
270 {
271  TQString retVal;
272  if (!adr.getFullName ().isEmpty ())
273  {
274  // should do some umlaut escaping
275  retVal += adr.getFullName () + " ";
276  }
277  if (!adr.getUser ().isEmpty () && !shortAdr)
278  {
279  retVal += "&lt;" + adr.getUser ();
280  if (!adr.getHost ().isEmpty ())
281  retVal += "@" + adr.getHost ();
282  retVal += "&gt; ";
283  }
284  if (!adr.getComment ().isEmpty ())
285  {
286  // should do some umlaut escaping
287  retVal = '(' + adr.getComment () + ')';
288  }
289 
290  if (!adr.getUser ().isEmpty ())
291  {
292  TQString mail;
293  mail = adr.getUser ();
294  if (!mail.isEmpty () && !adr.getHost ().isEmpty ())
295  mail += "@" + adr.getHost ();
296  if (!mail.isEmpty ())
297  retVal = "<A HREF=\"mailto:" + mail + "\">" + retVal + "</A>";
298  }
299  return retVal;
300 }
301 
302 TQString
303 mailAddress::emailAddrAsAnchor (const TQPtrList < mailAddress > &list, bool value)
304 {
305  TQString retVal;
306  TQPtrListIterator < mailAddress > it (list);
307 
308  while (it.current ())
309  {
310  retVal += emailAddrAsAnchor ((*it.current ()), value) + "<BR></BR>\n";
311  ++it;
312  }
313 
314  return retVal;
315 }
316 
317 
318 void mailAddress::clear() {
319  user.truncate(0);
320  host.truncate(0);
321  rawFullName.truncate(0);
322  rawComment.truncate(0);
323 }
324 
static int parseWord(const char *)
slurp one word
static int skipWS(const char *)
skip all white space characters
static int parseQuoted(char, char, const char *)
slurp one word
static const TQString decodeRFC2047String(const TQString &_str, TQString &charset, TQString &language)
decode a RFC2047 String
Definition: rfcdecoder.cpp:337
static const TQString encodeRFC2047String(const TQString &_str, TQString &charset, TQString &language)
encode a RFC2047 String