00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
#include "email.h"
00032
#include <kdebug.h>
00033
00034
00035 QStringList KPIM::splitEmailAddrList(
const QString& aStr)
00036 {
00037
00038
00039
00040
00041
00042
00043
00044
00045
QStringList list;
00046
00047
if (aStr.isEmpty())
00048
return list;
00049
00050
QString addr;
00051 uint addrstart = 0;
00052
int commentlevel = 0;
00053
bool insidequote =
false;
00054
00055
for (uint index=0; index<aStr.length(); index++) {
00056
00057
00058
switch (aStr[index].latin1()) {
00059
case '"' :
00060
if (commentlevel == 0)
00061 insidequote = !insidequote;
00062
break;
00063
case '(' :
00064
if (!insidequote)
00065 commentlevel++;
00066
break;
00067
case ')' :
00068
if (!insidequote) {
00069
if (commentlevel > 0)
00070 commentlevel--;
00071
else {
00072 kdDebug(5300) <<
"Error in address splitting: Unmatched ')'"
00073 << endl;
00074
return list;
00075 }
00076 }
00077
break;
00078
case '\\' :
00079 index++;
00080
break;
00081
case ',' :
00082
if (!insidequote && (commentlevel == 0)) {
00083 addr = aStr.mid(addrstart, index-addrstart);
00084
if (!addr.isEmpty())
00085 list += addr.simplifyWhiteSpace();
00086 addrstart = index+1;
00087 }
00088
break;
00089 }
00090 }
00091
00092
if (!insidequote && (commentlevel == 0)) {
00093 addr = aStr.mid(addrstart, aStr.length()-addrstart);
00094
if (!addr.isEmpty())
00095 list += addr.simplifyWhiteSpace();
00096 }
00097
else
00098 kdDebug(5300) <<
"Error in address splitting: "
00099 <<
"Unexpected end of address list"
00100 << endl;
00101
00102
return list;
00103 }
00104
00105
00106 QCString KPIM::getEmailAddr(
const QString& aStr)
00107 {
00108
int a, i, j, len, found = 0;
00109
QChar c;
00110
00111 a = aStr.find(
'@');
00112
if (a<0)
return aStr.latin1();
00113
00114
for (i = a - 1; i >= 0; i--) {
00115 c = aStr[i];
00116
if (c ==
'<' || c ==
'(' || c ==
' ') found = 1;
00117
if (found)
break;
00118 }
00119
00120 found = 0;
00121
00122
for (j = a + 1; j < (
int)aStr.length(); j++) {
00123 c = aStr[j];
00124
if (c ==
'>' || c ==
')' || c ==
' ') found = 1;
00125
if (found)
break;
00126 }
00127
00128 len = j - (i + 1);
00129
return aStr.mid(i+1,len).latin1();
00130 }
00131
00132 bool KPIM::getNameAndMail(
const QString& aStr,
QString& name,
QString& mail)
00133 {
00134 name = QString::null;
00135 mail = QString::null;
00136
00137
const int len=aStr.length();
00138
const char cQuotes =
'"';
00139
00140
bool bInComment, bInQuotesOutsideOfEmail;
00141
int i=0, iAd=0, iMailStart=0, iMailEnd=0;
00142
QChar c;
00143
00144
00145
00146 bInComment =
false;
00147
while( i < len ){
00148 c = aStr[i];
00149
if( !bInComment ){
00150
if(
'(' == c ){
00151 bInComment =
true;
00152 }
else{
00153
if(
'@' == c ){
00154 iAd = i;
00155
break;
00156 }
00157 }
00158 }
else{
00159
if(
')' == c ){
00160 bInComment =
false;
00161 }
00162 }
00163 ++i;
00164 }
00165
00166
if( !iAd ){
00167
00168
00169
00170
for( i = 0; len > i; ++i ) {
00171 c = aStr[i];
00172
if(
'<' != c )
00173 name.append( c );
00174
else
00175
break;
00176 }
00177 mail = aStr.mid( i+1 );
00178
00179 }
else{
00180
00181
00182
00183
00184 bInComment =
false;
00185 bInQuotesOutsideOfEmail =
false;
00186
for( i = iAd-1; 0 <= i; --i ) {
00187 c = aStr[i];
00188
if( bInComment ){
00189
if(
'(' == c ){
00190
if( !name.isEmpty() )
00191 name.prepend(
' ' );
00192 bInComment =
false;
00193 }
else{
00194 name.prepend( c );
00195 }
00196 }
else if( bInQuotesOutsideOfEmail ){
00197
if( cQuotes == c )
00198 bInQuotesOutsideOfEmail =
false;
00199 name.prepend( c );
00200 }
else{
00201
00202
if(
',' == c )
00203
break;
00204
00205
if( iMailStart ){
00206
if( cQuotes == c )
00207 bInQuotesOutsideOfEmail =
true;
00208 name.prepend( c );
00209 }
else{
00210
switch( c ){
00211
case '<':
00212 iMailStart = i;
00213
break;
00214
case ')':
00215
if( !name.isEmpty() )
00216 name.prepend(
' ' );
00217 bInComment =
true;
00218
break;
00219
default:
00220
if(
' ' != c )
00221 mail.prepend( c );
00222 }
00223 }
00224 }
00225 }
00226
00227 name = name.simplifyWhiteSpace();
00228 mail = mail.simplifyWhiteSpace();
00229
00230
if( mail.isEmpty() )
00231
return false;
00232
00233 mail.append(
'@');
00234
00235
00236
00237
00238 bInComment =
false;
00239 bInQuotesOutsideOfEmail =
false;
00240
for( i = iAd+1; len > i; ++i ) {
00241 c = aStr[i];
00242
if( bInComment ){
00243
if(
')' == c ){
00244
if( !name.isEmpty() )
00245 name.append(
' ' );
00246 bInComment =
false;
00247 }
else{
00248 name.append( c );
00249 }
00250 }
else if( bInQuotesOutsideOfEmail ){
00251
if( cQuotes == c )
00252 bInQuotesOutsideOfEmail =
false;
00253 name.append( c );
00254 }
else{
00255
00256
if(
',' == c )
00257
break;
00258
00259
if( iMailEnd ){
00260
if( cQuotes == c )
00261 bInQuotesOutsideOfEmail =
true;
00262 name.append( c );
00263 }
else{
00264
switch( c ){
00265
case '>':
00266 iMailEnd = i;
00267
break;
00268
case '(':
00269
if( !name.isEmpty() )
00270 name.append(
' ' );
00271 bInComment =
true;
00272
break;
00273
default:
00274
if(
' ' != c )
00275 mail.append( c );
00276 }
00277 }
00278 }
00279 }
00280 }
00281
00282 name = name.simplifyWhiteSpace();
00283 mail = mail.simplifyWhiteSpace();
00284
00285
return ! (name.isEmpty() || mail.isEmpty());
00286 }
00287
00288 bool KPIM::compareEmail(
const QString& email1,
const QString& email2,
00289
bool matchName )
00290 {
00291
QString e1Name, e1Email, e2Name, e2Email;
00292
00293
getNameAndMail( email1, e1Name, e1Email );
00294
getNameAndMail( email2, e2Name, e2Email );
00295
00296
return e1Email == e2Email &&
00297 ( !matchName || ( e1Name == e2Name ) );
00298 }