You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
324 lines
7.1 KiB
324 lines
7.1 KiB
15 years ago
|
/**********************************************************************
|
||
|
*
|
||
|
* mailaddress.cc - mail address parser
|
||
|
* Copyright (C) 2000 Sven Carstens
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 2 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
*
|
||
|
* Send comments and bug fixes to
|
||
|
*
|
||
|
*********************************************************************/
|
||
|
|
||
|
|
||
|
#include "mailaddress.h"
|
||
|
#include "rfcdecoder.h"
|
||
|
#include "mimehdrline.h"
|
||
|
#include <kmime_util.h>
|
||
|
|
||
|
mailAddress::mailAddress ()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
mailAddress::mailAddress (const mailAddress & lr):
|
||
|
user (lr.user),
|
||
|
host (lr.host),
|
||
|
rawFullName (lr.rawFullName),
|
||
|
rawComment (lr.rawComment)
|
||
|
{
|
||
|
// kdDebug(7116) << "mailAddress::mailAddress - " << getStr() << endl;
|
||
|
}
|
||
|
|
||
|
mailAddress & mailAddress::operator = (const mailAddress & lr)
|
||
|
{
|
||
|
// Avoid a = a.
|
||
|
if (this == &lr)
|
||
|
return *this;
|
||
|
|
||
|
user = lr.user;
|
||
|
host = lr.host;
|
||
|
rawFullName = lr.rawFullName;
|
||
|
rawComment = lr.rawComment;
|
||
|
|
||
|
// kdDebug(7116) << "mailAddress::operator= - " << getStr() << endl;
|
||
|
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
mailAddress::~mailAddress ()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
mailAddress::mailAddress (char *aCStr)
|
||
|
{
|
||
|
parseAddress (aCStr);
|
||
|
}
|
||
|
|
||
|
int
|
||
|
mailAddress::parseAddress (char *aCStr)
|
||
|
{
|
||
|
int retVal = 0;
|
||
|
int skip;
|
||
|
uint len;
|
||
|
int pt;
|
||
|
|
||
|
if (aCStr)
|
||
|
{
|
||
|
//skip leading white space
|
||
|
skip = mimeHdrLine::skipWS ((const char *) aCStr);
|
||
|
if (skip > 0)
|
||
|
{
|
||
|
aCStr += skip;
|
||
|
retVal += skip;
|
||
|
}
|
||
|
while (*aCStr)
|
||
|
{
|
||
|
int advance;
|
||
|
|
||
|
switch (*aCStr)
|
||
|
{
|
||
|
case '"':
|
||
|
advance = mimeHdrLine::parseQuoted ('"', '"', aCStr);
|
||
15 years ago
|
rawFullName += TQCString (aCStr, advance + 1);
|
||
15 years ago
|
break;
|
||
|
case '(':
|
||
|
advance = mimeHdrLine::parseQuoted ('(', ')', aCStr);
|
||
15 years ago
|
rawComment += TQCString (aCStr, advance + 1);
|
||
15 years ago
|
break;
|
||
|
case '<':
|
||
|
advance = mimeHdrLine::parseQuoted ('<', '>', aCStr);
|
||
15 years ago
|
user = TQCString (aCStr, advance + 1); // copy it
|
||
15 years ago
|
len = advance;
|
||
|
user = user.mid (1, len - 2); // strip <>
|
||
|
len -= 2;
|
||
14 years ago
|
pt = user.find('@');
|
||
15 years ago
|
host = user.right (len - pt - 1); // split it into host
|
||
|
user.truncate(pt); // and user
|
||
|
break;
|
||
|
default:
|
||
|
advance = mimeHdrLine::parseWord ((const char *) aCStr);
|
||
|
//if we've seen a FQ mailname the rest must be quoted or is just junk
|
||
|
if (user.isEmpty ())
|
||
|
{
|
||
|
if (*aCStr != ',')
|
||
|
{
|
||
15 years ago
|
rawFullName += TQCString (aCStr, advance + 1);
|
||
15 years ago
|
if (mimeHdrLine::skipWS ((const char *) &aCStr[advance]) > 0)
|
||
|
{
|
||
|
rawFullName += ' ';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
if (advance)
|
||
|
{
|
||
|
retVal += advance;
|
||
|
aCStr += advance;
|
||
|
}
|
||
|
else
|
||
|
break;
|
||
|
advance = mimeHdrLine::skipWS ((const char *) aCStr);
|
||
|
if (advance > 0)
|
||
|
{
|
||
|
retVal += advance;
|
||
|
aCStr += advance;
|
||
|
}
|
||
|
//reached end of current address
|
||
|
if (*aCStr == ',')
|
||
|
{
|
||
|
advance++;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
//let's see what we've got
|
||
|
if (rawFullName.isEmpty ())
|
||
|
{
|
||
|
if (user.isEmpty ())
|
||
|
retVal = 0;
|
||
|
else
|
||
|
{
|
||
|
if (host.isEmpty ())
|
||
|
{
|
||
|
rawFullName = user;
|
||
|
user.truncate(0);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if (user.isEmpty ())
|
||
|
{
|
||
14 years ago
|
pt = rawFullName.find ('@');
|
||
15 years ago
|
if (pt >= 0)
|
||
|
{
|
||
|
user = rawFullName;
|
||
|
host = user.right (user.length () - pt - 1);
|
||
|
user.truncate(pt);
|
||
|
rawFullName.truncate(0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#if 0
|
||
|
// dead
|
||
|
if (!rawFullName.isEmpty ())
|
||
|
{
|
||
|
// if(fullName[0] == '"')
|
||
|
// fullName = fullName.mid(1,fullName.length()-2);
|
||
|
// fullName = fullName.simplifyWhiteSpace().stripWhiteSpace();
|
||
|
// fullName = rfcDecoder::decodeRFC2047String(fullName.ascii());
|
||
|
}
|
||
|
#endif
|
||
|
if (!rawComment.isEmpty ())
|
||
|
{
|
||
|
if (rawComment[0] == '(')
|
||
|
rawComment = rawComment.mid (1, rawComment.length () - 2);
|
||
|
rawComment = rawComment.stripWhiteSpace ();
|
||
|
// comment = rfcDecoder::decodeRFC2047String(comment.ascii());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//debug();
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
14 years ago
|
const TQCString
|
||
15 years ago
|
mailAddress::getStr ()
|
||
|
{
|
||
15 years ago
|
TQCString retVal(128); // Should be generally big enough
|
||
15 years ago
|
|
||
|
if (!rawFullName.isEmpty ())
|
||
|
{
|
||
|
KMime::addQuotes( rawFullName, false );
|
||
|
retVal = rawFullName + " ";
|
||
|
}
|
||
|
if (!user.isEmpty ())
|
||
|
{
|
||
|
retVal += '<';
|
||
|
retVal += user;
|
||
|
if (!host.isEmpty ()) {
|
||
|
retVal += '@';
|
||
|
retVal += host;
|
||
|
}
|
||
|
retVal += '>';
|
||
|
}
|
||
|
if (!rawComment.isEmpty ())
|
||
|
{
|
||
|
retVal = '(' + rawComment + ')';
|
||
|
}
|
||
|
// kdDebug(7116) << "mailAddress::getStr - '" << retVal << "'" << endl;
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
mailAddress::isEmpty () const
|
||
|
{
|
||
|
return user.isEmpty ();
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mailAddress::setFullName (const TQString & _str)
|
||
15 years ago
|
{
|
||
|
rawFullName = rfcDecoder::encodeRFC2047String (_str).latin1 ();
|
||
|
}
|
||
14 years ago
|
const TQString
|
||
15 years ago
|
mailAddress::getFullName () const
|
||
|
{
|
||
|
return rfcDecoder::decodeRFC2047String (rawFullName);
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mailAddress::setCommentRaw (const TQCString & _str)
|
||
15 years ago
|
{
|
||
|
rawComment = _str;
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mailAddress::setComment (const TQString & _str)
|
||
15 years ago
|
{
|
||
|
rawComment = rfcDecoder::encodeRFC2047String (_str).latin1 ();
|
||
|
}
|
||
14 years ago
|
const TQString
|
||
15 years ago
|
mailAddress::getComment () const
|
||
|
{
|
||
|
return rfcDecoder::decodeRFC2047String (rawComment);
|
||
|
}
|
||
|
|
||
15 years ago
|
const TQCString &
|
||
15 years ago
|
mailAddress::getCommentRaw () const
|
||
|
{
|
||
|
return rawComment;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mailAddress::emailAddrAsAnchor (const mailAddress & adr, bool shortAdr)
|
||
|
{
|
||
15 years ago
|
TQString retVal;
|
||
15 years ago
|
if (!adr.getFullName ().isEmpty ())
|
||
|
{
|
||
|
// should do some umlaut escaping
|
||
|
retVal += adr.getFullName () + " ";
|
||
|
}
|
||
|
if (!adr.getUser ().isEmpty () && !shortAdr)
|
||
|
{
|
||
|
retVal += "<" + adr.getUser ();
|
||
|
if (!adr.getHost ().isEmpty ())
|
||
|
retVal += "@" + adr.getHost ();
|
||
|
retVal += "> ";
|
||
|
}
|
||
|
if (!adr.getComment ().isEmpty ())
|
||
|
{
|
||
|
// should do some umlaut escaping
|
||
|
retVal = '(' + adr.getComment () + ')';
|
||
|
}
|
||
|
|
||
|
if (!adr.getUser ().isEmpty ())
|
||
|
{
|
||
15 years ago
|
TQString mail;
|
||
15 years ago
|
mail = adr.getUser ();
|
||
|
if (!mail.isEmpty () && !adr.getHost ().isEmpty ())
|
||
|
mail += "@" + adr.getHost ();
|
||
|
if (!mail.isEmpty ())
|
||
|
retVal = "<A HREF=\"mailto:" + mail + "\">" + retVal + "</A>";
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mailAddress::emailAddrAsAnchor (const TQPtrList < mailAddress > &list, bool value)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString retVal;
|
||
|
TQPtrListIterator < mailAddress > it (list);
|
||
15 years ago
|
|
||
|
while (it.current ())
|
||
|
{
|
||
|
retVal += emailAddrAsAnchor ((*it.current ()), value) + "<BR></BR>\n";
|
||
|
++it;
|
||
|
}
|
||
|
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
|
||
|
void mailAddress::clear() {
|
||
|
user.truncate(0);
|
||
|
host.truncate(0);
|
||
|
rawFullName.truncate(0);
|
||
|
rawComment.truncate(0);
|
||
|
}
|
||
|
|