kioslaves

mailaddress.cc

Go to the documentation of this file.
00001 /**********************************************************************
00002  *
00003  *   mailaddress.cc  - mail address parser
00004  *   Copyright (C) 2000 Sven Carstens
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 += QCString (aCStr, advance + 1);
00097         break;
00098       case '(':
00099         advance = mimeHdrLine::parseQuoted ('(', ')', aCStr);
00100         rawComment += QCString (aCStr, advance + 1);
00101         break;
00102       case '<':
00103         advance = mimeHdrLine::parseQuoted ('<', '>', aCStr);
00104         user = QCString (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 += QCString (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 QCString
00200 mailAddress::getStr ()
00201 {
00202   QCString retVal(128); // Should be generally big enough
00203 
00204   if (!rawFullName.isEmpty ())
00205   {
00206     KMime::addQuotes( rawFullName, false );
00207     retVal = rawFullName + " ";
00208   }
00209   if (!user.isEmpty ())
00210   {
00211     retVal += '<';
00212     retVal += user;
00213     if (!host.isEmpty ()) {
00214       retVal += '@';
00215       retVal += host;
00216     }
00217     retVal += '>';
00218   }
00219   if (!rawComment.isEmpty ())
00220   {
00221     retVal = '(' + rawComment + ')';
00222   }
00223 //  kdDebug(7116) << "mailAddress::getStr - '" << retVal << "'" << endl;
00224   return retVal;
00225 }
00226 
00227 bool
00228 mailAddress::isEmpty () const
00229 {
00230   return user.isEmpty ();
00231 }
00232 
00233 void
00234 mailAddress::setFullName (const QString & _str)
00235 {
00236   rawFullName = rfcDecoder::encodeRFC2047String (_str).latin1 ();
00237 }
00238 const QString
00239 mailAddress::getFullName () const
00240 {
00241   return rfcDecoder::decodeRFC2047String (rawFullName);
00242 }
00243 
00244 void
00245 mailAddress::setCommentRaw (const QCString & _str)
00246 {
00247   rawComment = _str;
00248 }
00249 
00250 void
00251 mailAddress::setComment (const QString & _str)
00252 {
00253   rawComment = rfcDecoder::encodeRFC2047String (_str).latin1 ();
00254 }
00255 const QString
00256 mailAddress::getComment () const
00257 {
00258   return rfcDecoder::decodeRFC2047String (rawComment);
00259 }
00260 
00261 const QCString &
00262 mailAddress::getCommentRaw () const
00263 {
00264   return rawComment;
00265 }
00266 
00267 QString
00268 mailAddress::emailAddrAsAnchor (const mailAddress & adr, bool shortAdr)
00269 {
00270   QString retVal;
00271   if (!adr.getFullName ().isEmpty ())
00272   {
00273     // should do some umlaut escaping
00274     retVal += adr.getFullName () + " ";
00275   }
00276   if (!adr.getUser ().isEmpty () && !shortAdr)
00277   {
00278     retVal += "&lt;" + adr.getUser ();
00279     if (!adr.getHost ().isEmpty ())
00280       retVal += "@" + adr.getHost ();
00281     retVal += "&gt; ";
00282   }
00283   if (!adr.getComment ().isEmpty ())
00284   {
00285     // should do some umlaut escaping
00286     retVal = '(' + adr.getComment () + ')';
00287   }
00288 
00289   if (!adr.getUser ().isEmpty ())
00290   {
00291     QString mail;
00292     mail = adr.getUser ();
00293     if (!mail.isEmpty () && !adr.getHost ().isEmpty ())
00294       mail += "@" + adr.getHost ();
00295     if (!mail.isEmpty ())
00296       retVal = "<A HREF=\"mailto:" + mail + "\">" + retVal + "</A>";
00297   }
00298   return retVal;
00299 }
00300 
00301 QString
00302 mailAddress::emailAddrAsAnchor (const QPtrList < mailAddress > &list, bool value)
00303 {
00304   QString retVal;
00305   QPtrListIterator < mailAddress > it (list);
00306 
00307   while (it.current ())
00308   {
00309     retVal += emailAddrAsAnchor ((*it.current ()), value) + "<BR></BR>\n";
00310     ++it;
00311   }
00312 
00313   return retVal;
00314 }
00315 
00316 
00317 void mailAddress::clear() {
00318   user.truncate(0);
00319   host.truncate(0);
00320   rawFullName.truncate(0);
00321   rawComment.truncate(0);
00322 }
00323