mailaddress.cc
00001 /********************************************************************** 00002 * 00003 * mailaddress.cc - mail address parser 00004 * Copyright (C) 2000 Sven Carstens <s.carstens@gmx.de> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * Send comments and bug fixes to 00021 * 00022 *********************************************************************/ 00023 00024 00025 #include "mailaddress.h" 00026 #include "rfcdecoder.h" 00027 #include "mimehdrline.h" 00028 #include <kmime_util.h> 00029 00030 mailAddress::mailAddress () 00031 { 00032 } 00033 00034 mailAddress::mailAddress (const mailAddress & lr): 00035 user (lr.user), 00036 host (lr.host), 00037 rawFullName (lr.rawFullName), 00038 rawComment (lr.rawComment) 00039 { 00040 // kdDebug(7116) << "mailAddress::mailAddress - " << getStr() << endl; 00041 } 00042 00043 mailAddress & mailAddress::operator = (const mailAddress & lr) 00044 { 00045 // Avoid a = a. 00046 if (this == &lr) 00047 return *this; 00048 00049 user = lr.user; 00050 host = lr.host; 00051 rawFullName = lr.rawFullName; 00052 rawComment = lr.rawComment; 00053 00054 // kdDebug(7116) << "mailAddress::operator= - " << getStr() << endl; 00055 00056 return *this; 00057 } 00058 00059 00060 00061 00062 mailAddress::~mailAddress () 00063 { 00064 } 00065 00066 mailAddress::mailAddress (char *aCStr) 00067 { 00068 parseAddress (aCStr); 00069 } 00070 00071 int 00072 mailAddress::parseAddress (char *aCStr) 00073 { 00074 int retVal = 0; 00075 int skip; 00076 uint len; 00077 int pt; 00078 00079 if (aCStr) 00080 { 00081 //skip leading white space 00082 skip = mimeHdrLine::skipWS ((const char *) aCStr); 00083 if (skip > 0) 00084 { 00085 aCStr += skip; 00086 retVal += skip; 00087 } 00088 while (*aCStr) 00089 { 00090 int advance; 00091 00092 switch (*aCStr) 00093 { 00094 case '"': 00095 advance = mimeHdrLine::parseQuoted ('"', '"', aCStr); 00096 rawFullName += TQCString (aCStr, advance + 1); 00097 break; 00098 case '(': 00099 advance = mimeHdrLine::parseQuoted ('(', ')', aCStr); 00100 rawComment += TQCString (aCStr, advance + 1); 00101 break; 00102 case '<': 00103 advance = mimeHdrLine::parseQuoted ('<', '>', aCStr); 00104 user = TQCString (aCStr, advance + 1); // copy it 00105 len = advance; 00106 user = user.mid (1, len - 2); // strip <> 00107 len -= 2; 00108 pt = user.find('@'); 00109 host = user.right (len - pt - 1); // split it into host 00110 user.truncate(pt); // and user 00111 break; 00112 default: 00113 advance = mimeHdrLine::parseWord ((const char *) aCStr); 00114 //if we've seen a FQ mailname the rest must be quoted or is just junk 00115 if (user.isEmpty ()) 00116 { 00117 if (*aCStr != ',') 00118 { 00119 rawFullName += TQCString (aCStr, advance + 1); 00120 if (mimeHdrLine::skipWS ((const char *) &aCStr[advance]) > 0) 00121 { 00122 rawFullName += ' '; 00123 } 00124 } 00125 } 00126 break; 00127 } 00128 if (advance) 00129 { 00130 retVal += advance; 00131 aCStr += advance; 00132 } 00133 else 00134 break; 00135 advance = mimeHdrLine::skipWS ((const char *) aCStr); 00136 if (advance > 0) 00137 { 00138 retVal += advance; 00139 aCStr += advance; 00140 } 00141 //reached end of current address 00142 if (*aCStr == ',') 00143 { 00144 advance++; 00145 break; 00146 } 00147 } 00148 //let's see what we've got 00149 if (rawFullName.isEmpty ()) 00150 { 00151 if (user.isEmpty ()) 00152 retVal = 0; 00153 else 00154 { 00155 if (host.isEmpty ()) 00156 { 00157 rawFullName = user; 00158 user.truncate(0); 00159 } 00160 } 00161 } 00162 else if (user.isEmpty ()) 00163 { 00164 pt = rawFullName.find ('@'); 00165 if (pt >= 0) 00166 { 00167 user = rawFullName; 00168 host = user.right (user.length () - pt - 1); 00169 user.truncate(pt); 00170 rawFullName.truncate(0); 00171 } 00172 } 00173 00174 #if 0 00175 // dead 00176 if (!rawFullName.isEmpty ()) 00177 { 00178 // if(fullName[0] == '"') 00179 // fullName = fullName.mid(1,fullName.length()-2); 00180 // fullName = fullName.simplifyWhiteSpace().stripWhiteSpace(); 00181 // fullName = rfcDecoder::decodeRFC2047String(fullName.ascii()); 00182 } 00183 #endif 00184 if (!rawComment.isEmpty ()) 00185 { 00186 if (rawComment[0] == '(') 00187 rawComment = rawComment.mid (1, rawComment.length () - 2); 00188 rawComment = rawComment.stripWhiteSpace (); 00189 // comment = rfcDecoder::decodeRFC2047String(comment.ascii()); 00190 } 00191 } 00192 else 00193 { 00194 //debug(); 00195 } 00196 return retVal; 00197 } 00198 00199 const TQCString 00200 mailAddress::getStr () const 00201 { 00202 TQCString retVal(128); // Should be generally big enough 00203 00204 if (!rawFullName.isEmpty ()) 00205 { 00206 TQCString tmpName( rawFullName ); 00207 KMime::addQuotes( tmpName, false ); 00208 retVal = tmpName + " "; 00209 } 00210 if (!user.isEmpty ()) 00211 { 00212 retVal += '<'; 00213 retVal += user; 00214 if (!host.isEmpty ()) { 00215 retVal += '@'; 00216 retVal += host; 00217 } 00218 retVal += '>'; 00219 } 00220 if (!rawComment.isEmpty ()) 00221 { 00222 retVal += " (" + rawComment + ')'; 00223 } 00224 // kdDebug(7116) << "mailAddress::getStr - '" << retVal << "'" << endl; 00225 return retVal; 00226 } 00227 00228 bool 00229 mailAddress::isEmpty () const 00230 { 00231 return user.isEmpty (); 00232 } 00233 00234 void 00235 mailAddress::setFullName (const TQString & _str) 00236 { 00237 rawFullName = rfcDecoder::encodeRFC2047String (_str).latin1 (); 00238 } 00239 const TQString 00240 mailAddress::getFullName () const 00241 { 00242 return rfcDecoder::decodeRFC2047String (rawFullName); 00243 } 00244 00245 void 00246 mailAddress::setCommentRaw (const TQCString & _str) 00247 { 00248 rawComment = _str; 00249 } 00250 00251 void 00252 mailAddress::setComment (const TQString & _str) 00253 { 00254 rawComment = rfcDecoder::encodeRFC2047String (_str).latin1 (); 00255 } 00256 const TQString 00257 mailAddress::getComment () const 00258 { 00259 return rfcDecoder::decodeRFC2047String (rawComment); 00260 } 00261 00262 const TQCString & 00263 mailAddress::getCommentRaw () const 00264 { 00265 return rawComment; 00266 } 00267 00268 TQString 00269 mailAddress::emailAddrAsAnchor (const mailAddress & adr, bool shortAdr) 00270 { 00271 TQString retVal; 00272 if (!adr.getFullName ().isEmpty ()) 00273 { 00274 // should do some umlaut escaping 00275 retVal += adr.getFullName () + " "; 00276 } 00277 if (!adr.getUser ().isEmpty () && !shortAdr) 00278 { 00279 retVal += "<" + adr.getUser (); 00280 if (!adr.getHost ().isEmpty ()) 00281 retVal += "@" + adr.getHost (); 00282 retVal += "> "; 00283 } 00284 if (!adr.getComment ().isEmpty ()) 00285 { 00286 // should do some umlaut escaping 00287 retVal = '(' + adr.getComment () + ')'; 00288 } 00289 00290 if (!adr.getUser ().isEmpty ()) 00291 { 00292 TQString mail; 00293 mail = adr.getUser (); 00294 if (!mail.isEmpty () && !adr.getHost ().isEmpty ()) 00295 mail += "@" + adr.getHost (); 00296 if (!mail.isEmpty ()) 00297 retVal = "<A HREF=\"mailto:" + mail + "\">" + retVal + "</A>"; 00298 } 00299 return retVal; 00300 } 00301 00302 TQString 00303 mailAddress::emailAddrAsAnchor (const TQPtrList < mailAddress > &list, bool value) 00304 { 00305 TQString retVal; 00306 TQPtrListIterator < mailAddress > it (list); 00307 00308 while (it.current ()) 00309 { 00310 retVal += emailAddrAsAnchor ((*it.current ()), value) + "<BR></BR>\n"; 00311 ++it; 00312 } 00313 00314 return retVal; 00315 } 00316 00317 00318 void mailAddress::clear() { 00319 user.truncate(0); 00320 host.truncate(0); 00321 rawFullName.truncate(0); 00322 rawComment.truncate(0); 00323 } 00324