00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 #include <config.h>
00019 #endif
00020
00021 #include "kmime_util.h"
00022
00023 #include <kmdcodec.h>
00024 #include <kglobal.h>
00025 #include <klocale.h>
00026 #include <kcharsets.h>
00027 #include <kdeversion.h>
00028 #if KDE_IS_VERSION( 3, 1, 90 )
00029 #include <kcalendarsystem.h>
00030 #endif
00031
00032 #include <qtextcodec.h>
00033 #include <qstrlist.h>
00034 #include <qregexp.h>
00035
00036 #include <stdlib.h>
00037 #include <ctype.h>
00038 #include <time.h>
00039 #include <unistd.h>
00040
00041 using namespace KMime;
00042
00043 namespace KMime {
00044
00045 QStrIList c_harsetCache;
00046 QStrIList l_anguageCache;
00047
00048 const char* cachedCharset(const QCString &name)
00049 {
00050 int idx=c_harsetCache.find(name.data());
00051 if(idx>-1)
00052 return c_harsetCache.at(idx);
00053
00054 c_harsetCache.append(name.upper().data());
00055
00056 return c_harsetCache.last();
00057 }
00058
00059 const char* cachedLanguage(const QCString &name)
00060 {
00061 int idx=l_anguageCache.find(name.data());
00062 if(idx>-1)
00063 return l_anguageCache.at(idx);
00064
00065 l_anguageCache.append(name.upper().data());
00066
00067 return l_anguageCache.last();
00068 }
00069
00070 bool isUsAscii(const QString &s)
00071 {
00072 uint sLength = s.length();
00073 for (uint i=0; i<sLength; i++)
00074 if (s.at(i).latin1()<=0)
00075 return false;
00076
00077 return true;
00078 }
00079
00080
00081 const uchar specialsMap[16] = {
00082 0x00, 0x00, 0x00, 0x00,
00083 0x20, 0xCA, 0x00, 0x3A,
00084 0x80, 0x00, 0x00, 0x1C,
00085 0x00, 0x00, 0x00, 0x00
00086 };
00087
00088
00089 const uchar tSpecialsMap[16] = {
00090 0x00, 0x00, 0x00, 0x00,
00091 0x20, 0xC9, 0x00, 0x3F,
00092 0x80, 0x00, 0x00, 0x1C,
00093 0x00, 0x00, 0x00, 0x00
00094 };
00095
00096
00097 const uchar aTextMap[16] = {
00098 0x00, 0x00, 0x00, 0x00,
00099 0x5F, 0x35, 0xFF, 0xC5,
00100 0x7F, 0xFF, 0xFF, 0xE3,
00101 0xFF, 0xFF, 0xFF, 0xFE
00102 };
00103
00104
00105 const uchar tTextMap[16] = {
00106 0x00, 0x00, 0x00, 0x00,
00107 0x5F, 0x36, 0xFF, 0xC0,
00108 0x7F, 0xFF, 0xFF, 0xE3,
00109 0xFF, 0xFF, 0xFF, 0xFE
00110 };
00111
00112
00113 const uchar eTextMap[16] = {
00114 0x00, 0x00, 0x00, 0x00,
00115 0x40, 0x35, 0xFF, 0xC0,
00116 0x7F, 0xFF, 0xFF, 0xE0,
00117 0x7F, 0xFF, 0xFF, 0xE0
00118 };
00119
00120 #if defined(_AIX) && defined(truncate)
00121 #undef truncate
00122 #endif
00123
00124 QString decodeRFC2047String(const QCString &src, const char **usedCS,
00125 const QCString &defaultCS, bool forceCS)
00126 {
00127 QCString result, str;
00128 QCString declaredCS;
00129 char *pos, *dest, *beg, *end, *mid, *endOfLastEncWord=0;
00130 char encoding = '\0';
00131 bool valid, onlySpacesSinceLastWord=false;
00132 const int maxLen=400;
00133 int i;
00134
00135 if(src.find("=?") < 0)
00136 result = src.copy();
00137 else {
00138 result.truncate(src.length());
00139 for (pos=src.data(), dest=result.data(); *pos; pos++)
00140 {
00141 if (pos[0]!='=' || pos[1]!='?')
00142 {
00143 *dest++ = *pos;
00144 if (onlySpacesSinceLastWord)
00145 onlySpacesSinceLastWord = (pos[0]==' ' || pos[1]=='\t');
00146 continue;
00147 }
00148 beg = pos+2;
00149 end = beg;
00150 valid = TRUE;
00151
00152 declaredCS="";
00153 for (i=2,pos+=2; i<maxLen && (*pos!='?'&&(ispunct(*pos)||isalnum(*pos))); i++) {
00154 declaredCS+=(*pos);
00155 pos++;
00156 }
00157 if (*pos!='?' || i<4 || i>=maxLen) valid = FALSE;
00158 else
00159 {
00160
00161 encoding = toupper(pos[1]);
00162 if (pos[2]!='?' || (encoding!='Q' && encoding!='B'))
00163 valid = FALSE;
00164 pos+=3;
00165 i+=3;
00166 }
00167 if (valid)
00168 {
00169 mid = pos;
00170
00171 while (i<maxLen && *pos && !(*pos=='?' && *(pos+1)=='='))
00172 {
00173 i++;
00174 pos++;
00175 }
00176 end = pos+2;
00177 if (i>=maxLen || !*pos) valid = FALSE;
00178 }
00179
00180 if (valid) {
00181
00182 if (onlySpacesSinceLastWord)
00183 dest=endOfLastEncWord;
00184
00185 if (mid < pos) {
00186 str = QCString(mid, (int)(pos - mid + 1));
00187 if (encoding == 'Q')
00188 {
00189
00190 for (i=str.length()-1; i>=0; i--)
00191 if (str[i]=='_') str[i]=' ';
00192 str = KCodecs::quotedPrintableDecode(str);
00193 }
00194 else
00195 {
00196 str = KCodecs::base64Decode(str);
00197 }
00198 for (i=0; str[i]; i++) {
00199 *dest++ = str[i];
00200 }
00201 }
00202
00203 endOfLastEncWord=dest;
00204 onlySpacesSinceLastWord=true;
00205
00206 pos = end -1;
00207 }
00208 else
00209 {
00210 pos = beg - 2;
00211 *dest++ = *pos++;
00212 *dest++ = *pos;
00213 }
00214 }
00215 *dest = '\0';
00216 }
00217
00218
00219 QTextCodec *codec=0;
00220 bool ok=true;
00221 if (forceCS || declaredCS.isEmpty()) {
00222 codec=KGlobal::charsets()->codecForName(defaultCS);
00223 (*usedCS)=cachedCharset(defaultCS);
00224 }
00225 else {
00226 codec=KGlobal::charsets()->codecForName(declaredCS, ok);
00227 if(!ok) {
00228 codec=KGlobal::charsets()->codecForName(defaultCS);
00229 (*usedCS)=cachedCharset(defaultCS);
00230 }
00231 else
00232 (*usedCS)=cachedCharset(declaredCS);
00233 }
00234
00235 return codec->toUnicode(result.data(), result.length());
00236 }
00237
00238
00239 QCString encodeRFC2047String(const QString &src, const char *charset,
00240 bool addressHeader, bool allow8BitHeaders)
00241 {
00242 QCString encoded8Bit, result, usedCS;
00243 unsigned int start=0,end=0;
00244 bool nonAscii=false, ok=true, useQEncoding=false;
00245 QTextCodec *codec=0;
00246
00247 usedCS=charset;
00248 codec=KGlobal::charsets()->codecForName(usedCS, ok);
00249
00250 if(!ok) {
00251
00252 usedCS=KGlobal::locale()->encoding();
00253 codec=KGlobal::charsets()->codecForName(usedCS, ok);
00254 }
00255
00256 if (usedCS.find("8859-")>=0)
00257 useQEncoding=true;
00258
00259 encoded8Bit=codec->fromUnicode(src);
00260
00261 if(allow8BitHeaders)
00262 return encoded8Bit;
00263
00264 uint encoded8BitLength = encoded8Bit.length();
00265 for (unsigned int i=0; i<encoded8BitLength; i++) {
00266 if (encoded8Bit[i]==' ')
00267 start = i+1;
00268
00269
00270 if (((signed char)encoded8Bit[i]<0) || (encoded8Bit[i] == '\033') ||
00271 (addressHeader && (strchr("\"()<>@,.;:\\[]=",encoded8Bit[i])!=0))) {
00272 end = start;
00273 nonAscii=true;
00274 break;
00275 }
00276 }
00277
00278 if (nonAscii) {
00279 while ((end<encoded8Bit.length())&&(encoded8Bit[end]!=' '))
00280 end++;
00281
00282 for (unsigned int x=end;x<encoded8Bit.length();x++)
00283 if (((signed char)encoded8Bit[x]<0) || (encoded8Bit[x] == '\033') ||
00284 (addressHeader && (strchr("\"()<>@,.;:\\[]=",encoded8Bit[x])!=0))) {
00285 end = encoded8Bit.length();
00286
00287 while ((end<encoded8Bit.length())&&(encoded8Bit[end]!=' '))
00288 end++;
00289 }
00290
00291 result = encoded8Bit.left(start)+"=?"+usedCS;
00292
00293 if (useQEncoding) {
00294 result += "?Q?";
00295
00296 char c,hexcode;
00297 for (unsigned int i=start;i<end;i++) {
00298 c = encoded8Bit[i];
00299 if (c == ' ')
00300 result+='_';
00301 else
00302 if (((c>='a')&&(c<='z'))||
00303 ((c>='A')&&(c<='Z'))||
00304 ((c>='0')&&(c<='9')))
00305 result+=c;
00306 else {
00307 result += "=";
00308 hexcode = ((c & 0xF0) >> 4) + 48;
00309 if (hexcode >= 58) hexcode += 7;
00310 result += hexcode;
00311 hexcode = (c & 0x0F) + 48;
00312 if (hexcode >= 58) hexcode += 7;
00313 result += hexcode;
00314 }
00315 }
00316 } else {
00317 result += "?B?"+KCodecs::base64Encode(encoded8Bit.mid(start,end-start), false);
00318 }
00319
00320 result +="?=";
00321 result += encoded8Bit.right(encoded8Bit.length()-end);
00322 }
00323 else
00324 result = encoded8Bit;
00325
00326 return result;
00327 }
00328
00329 QCString uniqueString()
00330 {
00331 static char chars[] = "0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00332 time_t now;
00333 QCString ret;
00334 char p[11];
00335 int pos, ran;
00336 unsigned int timeval;
00337
00338 p[10]='\0';
00339 now=time(0);
00340 ran=1+(int) (1000.0*rand()/(RAND_MAX+1.0));
00341 timeval=(now/ran)+getpid();
00342
00343 for(int i=0; i<10; i++){
00344 pos=(int) (61.0*rand()/(RAND_MAX+1.0));
00345
00346 p[i]=chars[pos];
00347 }
00348 ret.sprintf("%d.%s", timeval, p);
00349
00350 return ret;
00351 }
00352
00353
00354 QCString multiPartBoundary()
00355 {
00356 QCString ret;
00357 ret="nextPart"+uniqueString();
00358 return ret;
00359 }
00360
00361 QCString extractHeader(const QCString &src, const char *name)
00362 {
00363 QCString n=QCString(name)+": ";
00364 int pos1=-1, pos2=0, len=src.length()-1;
00365 bool folded(false);
00366
00367 if (n.lower() == src.left(n.length()).lower()) {
00368 pos1 = 0;
00369 } else {
00370 n.prepend("\n");
00371 pos1 = src.find(n,0,false);
00372 }
00373
00374 if (pos1>-1) {
00375 pos1+=n.length();
00376 pos2=pos1;
00377
00378 if (src[pos2]!='\n') {
00379 while(1) {
00380 pos2=src.find("\n", pos2+1);
00381 if(pos2==-1 || pos2==len || ( src[pos2+1]!=' ' && src[pos2+1]!='\t') )
00382 break;
00383 else
00384 folded = true;
00385 }
00386 }
00387
00388 if(pos2<0) pos2=len+1;
00389
00390 if (!folded)
00391 return src.mid(pos1, pos2-pos1);
00392 else
00393 return (src.mid(pos1, pos2-pos1).replace(QRegExp("\\s*\\n\\s*")," "));
00394 }
00395 else {
00396 return QCString(0);
00397 }
00398 }
00399
00400
00401 QCString CRLFtoLF(const QCString &s)
00402 {
00403 QCString ret=s.copy();
00404 ret.replace(QRegExp("\\r\\n"), "\n");
00405 return ret;
00406 }
00407
00408
00409 QCString CRLFtoLF(const char *s)
00410 {
00411 QCString ret=s;
00412 ret.replace(QRegExp("\\r\\n"), "\n");
00413 return ret;
00414 }
00415
00416
00417 QCString LFtoCRLF(const QCString &s)
00418 {
00419 QCString ret=s.copy();
00420 ret.replace(QRegExp("\\n"), "\r\n");
00421 return ret;
00422 }
00423
00424
00425 void removeQuots(QCString &str)
00426 {
00427 bool inQuote=false;
00428
00429 for (int i=0; i < (int)str.length(); i++) {
00430 if (str[i] == '"') {
00431 str.remove(i,1);
00432 i--;
00433 inQuote = !inQuote;
00434 } else {
00435 if (inQuote && (str[i] == '\\'))
00436 str.remove(i,1);
00437 }
00438 }
00439 }
00440
00441
00442 void removeQuots(QString &str)
00443 {
00444 bool inQuote=false;
00445
00446 for (int i=0; i < (int)str.length(); i++) {
00447 if (str[i] == '"') {
00448 str.remove(i,1);
00449 i--;
00450 inQuote = !inQuote;
00451 } else {
00452 if (inQuote && (str[i] == '\\'))
00453 str.remove(i,1);
00454 }
00455 }
00456 }
00457
00458
00459 void addQuotes(QCString &str, bool forceQuotes)
00460 {
00461 bool needsQuotes=false;
00462 for (unsigned int i=0; i < str.length(); i++) {
00463 if (strchr("()<>@,.;:[]=\\\"",str[i])!=0)
00464 needsQuotes = true;
00465 if (str[i]=='\\' || str[i]=='\"') {
00466 str.insert(i, '\\');
00467 i++;
00468 }
00469 }
00470
00471 if (needsQuotes || forceQuotes) {
00472 str.insert(0,'\"');
00473 str.append("\"");
00474 }
00475 }
00476
00477 int DateFormatter::mDaylight = -1;
00478 DateFormatter::DateFormatter(FormatType fType)
00479 : mFormat( fType ), mCurrentTime( 0 )
00480 {
00481
00482 }
00483
00484 DateFormatter::~DateFormatter()
00485 {}
00486
00487 DateFormatter::FormatType
00488 DateFormatter::getFormat() const
00489 {
00490 return mFormat;
00491 }
00492
00493 void
00494 DateFormatter::setFormat( FormatType t )
00495 {
00496 mFormat = t;
00497 }
00498
00499 QString
00500 DateFormatter::dateString( time_t otime , const QString& lang ,
00501 bool shortFormat, bool includeSecs ) const
00502 {
00503 switch ( mFormat ) {
00504 case Fancy:
00505 return fancy( otime );
00506 break;
00507 case Localized:
00508 return localized( otime, shortFormat, includeSecs, lang );
00509 break;
00510 case CTime:
00511 return cTime( otime );
00512 break;
00513 case Iso:
00514 return isoDate( otime );
00515 break;
00516 case Custom:
00517 return custom( otime );
00518 break;
00519 }
00520 return QString::null;
00521 }
00522
00523 QString
00524 DateFormatter::dateString(const QDateTime& dtime, const QString& lang,
00525 bool shortFormat, bool includeSecs ) const
00526 {
00527 return DateFormatter::dateString( qdateToTimeT(dtime), lang, shortFormat, includeSecs );
00528 }
00529
00530 QCString
00531 DateFormatter::rfc2822(time_t otime) const
00532 {
00533 QDateTime tmp;
00534 QCString ret;
00535
00536 tmp.setTime_t(otime);
00537
00538 ret = tmp.toString("ddd, dd MMM yyyy hh:mm:ss ").latin1();
00539 ret += zone(otime);
00540
00541 return ret;
00542 }
00543
00544 QString
00545 DateFormatter::custom(time_t t) const
00546 {
00547 if ( mCustomFormat.isEmpty() )
00548 return QString::null;
00549
00550 int z = mCustomFormat.find("Z");
00551 QDateTime d;
00552 QString ret = mCustomFormat;
00553
00554 d.setTime_t(t);
00555 if ( z != -1 ) {
00556 ret.replace(z,1,zone(t));
00557 }
00558
00559 ret = d.toString(ret);
00560
00561 return ret;
00562 }
00563
00564 void
00565 DateFormatter::setCustomFormat(const QString& format)
00566 {
00567 mCustomFormat = format;
00568 mFormat = Custom;
00569 }
00570
00571 QString
00572 DateFormatter::getCustomFormat() const
00573 {
00574 return mCustomFormat;
00575 }
00576
00577
00578 QCString
00579 DateFormatter::zone(time_t otime) const
00580 {
00581 QCString ret;
00582 #if defined(HAVE_TIMEZONE) || defined(HAVE_TM_GMTOFF)
00583 struct tm *local = localtime( &otime );
00584 #endif
00585
00586 #if defined(HAVE_TIMEZONE)
00587
00588
00589 int secs = abs(timezone);
00590 int neg = (timezone>0)?1:0;
00591 int hours = secs/3600;
00592 int mins = (secs - hours*3600)/60;
00593
00594
00595 if ( local->tm_isdst > 0 ) {
00596 mDaylight = 1;
00597 if ( neg )
00598 --hours;
00599 else
00600 ++hours;
00601 } else
00602 mDaylight = 0;
00603
00604 ret.sprintf("%c%.2d%.2d",(neg)?'-':'+', hours, mins);
00605
00606 #elif defined(HAVE_TM_GMTOFF)
00607
00608 int secs = abs( local->tm_gmtoff );
00609 int neg = (local->tm_gmtoff<0)?1:0;
00610 int hours = secs/3600;
00611 int mins = (secs - hours*3600)/60;
00612
00613 if ( local->tm_isdst > 0 )
00614 mDaylight = 1;
00615 else
00616 mDaylight = 0;
00617
00618 ret.sprintf("%c%.2d%.2d",(neg)?'-':'+', hours, mins);
00619
00620 #else
00621
00622 QDateTime d1 = QDateTime::fromString( asctime(gmtime(&otime)) );
00623 QDateTime d2 = QDateTime::fromString( asctime(localtime(&otime)) );
00624 int secs = d1.secsTo(d2);
00625 int neg = (secs<0)?1:0;
00626 secs = abs(secs);
00627 int hours = secs/3600;
00628 int mins = (secs - hours*3600)/60;
00629
00630 ret.sprintf("%c%.2d%.2d",(neg)?'-':'+', hours, mins);
00631
00632 #endif
00633
00634 return ret;
00635 }
00636
00637 time_t
00638 DateFormatter::qdateToTimeT(const QDateTime& dt) const
00639 {
00640 QDateTime epoch( QDate(1970, 1,1), QTime(00,00,00) );
00641 time_t otime;
00642 time( &otime );
00643
00644 QDateTime d1 = QDateTime::fromString( asctime(gmtime(&otime)) );
00645 QDateTime d2 = QDateTime::fromString( asctime(localtime(&otime)) );
00646 time_t drf = epoch.secsTo( dt ) - d1.secsTo( d2 );
00647
00648 return drf;
00649 }
00650
00651 QString
00652 DateFormatter::fancy(time_t otime) const
00653 {
00654 KLocale *locale = KGlobal::locale();
00655
00656 if ( otime <= 0 )
00657 return i18n( "unknown" );
00658
00659 if ( !mCurrentTime ) {
00660 time( &mCurrentTime );
00661 mDate.setTime_t( mCurrentTime );
00662 }
00663
00664 QDateTime old;
00665 old.setTime_t( otime );
00666
00667
00668 if ( mCurrentTime + 60 * 60 >= otime ) {
00669 time_t diff = mCurrentTime - otime;
00670
00671 if ( diff < 24 * 60 * 60 ) {
00672 if ( old.date().year() == mDate.date().year() &&
00673 old.date().dayOfYear() == mDate.date().dayOfYear() )
00674 return i18n( "Today %1" ).arg( locale->
00675 formatTime( old.time(), true ) );
00676 }
00677 if ( diff < 2 * 24 * 60 * 60 ) {
00678 QDateTime yesterday( mDate.addDays( -1 ) );
00679 if ( old.date().year() == yesterday.date().year() &&
00680 old.date().dayOfYear() == yesterday.date().dayOfYear() )
00681 return i18n( "Yesterday %1" ).arg( locale->
00682 formatTime( old.time(), true) );
00683 }
00684 for ( int i = 3; i < 7; i++ )
00685 if ( diff < i * 24 * 60 * 60 ) {
00686 QDateTime weekday( mDate.addDays( -i + 1 ) );
00687 if ( old.date().year() == weekday.date().year() &&
00688 old.date().dayOfYear() == weekday.date().dayOfYear() )
00689 return i18n( "1. weekday, 2. time", "%1 %2" ).
00690 #if KDE_IS_VERSION( 3, 1, 90 )
00691 arg( locale->calendar()->weekDayName( old.date() ) ).
00692 #else
00693 arg( locale->weekDayName( old.date().dayOfWeek() ) ).
00694 #endif
00695 arg( locale->formatTime( old.time(), true) );
00696 }
00697 }
00698
00699 return locale->formatDateTime( old );
00700
00701 }
00702
00703 QString
00704 DateFormatter::localized(time_t otime, bool shortFormat, bool includeSecs,
00705 const QString& localeLanguage ) const
00706 {
00707 QDateTime tmp;
00708 QString ret;
00709 KLocale *locale = KGlobal::locale();
00710
00711 tmp.setTime_t( otime );
00712
00713
00714 if ( !localeLanguage.isEmpty() ) {
00715 locale=new KLocale(localeLanguage);
00716 locale->setLanguage(localeLanguage);
00717 locale->setCountry(localeLanguage);
00718 ret = locale->formatDateTime( tmp, shortFormat, includeSecs );
00719 delete locale;
00720 } else {
00721 ret = locale->formatDateTime( tmp, shortFormat, includeSecs );
00722 }
00723
00724 return ret;
00725 }
00726
00727 QString
00728 DateFormatter::cTime(time_t otime) const
00729 {
00730 return QString::fromLatin1( ctime( &otime ) ).stripWhiteSpace() ;
00731 }
00732
00733 QString
00734 DateFormatter::isoDate(time_t otime) const
00735 {
00736 char cstr[64];
00737 strftime( cstr, 63, "%Y-%m-%d %H:%M:%S", localtime(&otime) );
00738 return QString( cstr );
00739 }
00740
00741
00742 void
00743 DateFormatter::reset()
00744 {
00745 mCurrentTime = 0;
00746 }
00747
00748 QString
00749 DateFormatter::formatDate(DateFormatter::FormatType t, time_t otime,
00750 const QString& data, bool shortFormat, bool includeSecs )
00751 {
00752 DateFormatter f( t );
00753 if ( t == DateFormatter::Custom ) {
00754 f.setCustomFormat( data );
00755 }
00756 return f.dateString( otime, data, shortFormat, includeSecs );
00757 }
00758
00759 QString
00760 DateFormatter::formatCurrentDate( DateFormatter::FormatType t, const QString& data,
00761 bool shortFormat, bool includeSecs )
00762 {
00763 DateFormatter f( t );
00764 if ( t == DateFormatter::Custom ) {
00765 f.setCustomFormat( data );
00766 }
00767 return f.dateString( time(0), data, shortFormat, includeSecs );
00768 }
00769
00770 QCString
00771 DateFormatter::rfc2822FormatDate( time_t t )
00772 {
00773 DateFormatter f;
00774 return f.rfc2822( t );
00775 }
00776
00777 bool
00778 DateFormatter::isDaylight()
00779 {
00780 if ( mDaylight == -1 ) {
00781 time_t ntime = time( 0 );
00782 struct tm *local = localtime( &ntime );
00783 if ( local->tm_isdst > 0 ) {
00784 mDaylight = 1;
00785 return true;
00786 } else {
00787 mDaylight = 0;
00788 return false;
00789 }
00790 } else if ( mDaylight != 0 )
00791 return true;
00792 else
00793 return false;
00794 }
00795
00796 }