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
00377 if ( src.at( pos1 ) == ' ' )
00378 ++pos1;
00379 pos2=pos1;
00380
00381 if (src[pos2]!='\n') {
00382 while(1) {
00383 pos2=src.find("\n", pos2+1);
00384 if(pos2==-1 || pos2==len || ( src[pos2+1]!=' ' && src[pos2+1]!='\t') )
00385 break;
00386 else
00387 folded = true;
00388 }
00389 }
00390
00391 if(pos2<0) pos2=len+1;
00392
00393 if (!folded)
00394 return src.mid(pos1, pos2-pos1);
00395 else
00396 return (src.mid(pos1, pos2-pos1).replace(QRegExp("\\s*\\n\\s*")," "));
00397 }
00398 else {
00399 return QCString(0);
00400 }
00401 }
00402
00403
00404 QCString CRLFtoLF(const QCString &s)
00405 {
00406 QCString ret=s.copy();
00407 ret.replace(QRegExp("\\r\\n"), "\n");
00408 return ret;
00409 }
00410
00411
00412 QCString CRLFtoLF(const char *s)
00413 {
00414 QCString ret=s;
00415 ret.replace(QRegExp("\\r\\n"), "\n");
00416 return ret;
00417 }
00418
00419
00420 QCString LFtoCRLF(const QCString &s)
00421 {
00422 QCString ret=s.copy();
00423 ret.replace(QRegExp("\\n"), "\r\n");
00424 return ret;
00425 }
00426
00427
00428 void removeQuots(QCString &str)
00429 {
00430 bool inQuote=false;
00431
00432 for (int i=0; i < (int)str.length(); i++) {
00433 if (str[i] == '"') {
00434 str.remove(i,1);
00435 i--;
00436 inQuote = !inQuote;
00437 } else {
00438 if (inQuote && (str[i] == '\\'))
00439 str.remove(i,1);
00440 }
00441 }
00442 }
00443
00444
00445 void removeQuots(QString &str)
00446 {
00447 bool inQuote=false;
00448
00449 for (int i=0; i < (int)str.length(); i++) {
00450 if (str[i] == '"') {
00451 str.remove(i,1);
00452 i--;
00453 inQuote = !inQuote;
00454 } else {
00455 if (inQuote && (str[i] == '\\'))
00456 str.remove(i,1);
00457 }
00458 }
00459 }
00460
00461
00462 void addQuotes(QCString &str, bool forceQuotes)
00463 {
00464 bool needsQuotes=false;
00465 for (unsigned int i=0; i < str.length(); i++) {
00466 if (strchr("()<>@,.;:[]=\\\"",str[i])!=0)
00467 needsQuotes = true;
00468 if (str[i]=='\\' || str[i]=='\"') {
00469 str.insert(i, '\\');
00470 i++;
00471 }
00472 }
00473
00474 if (needsQuotes || forceQuotes) {
00475 str.insert(0,'\"');
00476 str.append("\"");
00477 }
00478 }
00479
00480 int DateFormatter::mDaylight = -1;
00481 DateFormatter::DateFormatter(FormatType fType)
00482 : mFormat( fType ), mCurrentTime( 0 )
00483 {
00484
00485 }
00486
00487 DateFormatter::~DateFormatter()
00488 {}
00489
00490 DateFormatter::FormatType
00491 DateFormatter::getFormat() const
00492 {
00493 return mFormat;
00494 }
00495
00496 void
00497 DateFormatter::setFormat( FormatType t )
00498 {
00499 mFormat = t;
00500 }
00501
00502 QString
00503 DateFormatter::dateString( time_t otime , const QString& lang ,
00504 bool shortFormat, bool includeSecs ) const
00505 {
00506 switch ( mFormat ) {
00507 case Fancy:
00508 return fancy( otime );
00509 break;
00510 case Localized:
00511 return localized( otime, shortFormat, includeSecs, lang );
00512 break;
00513 case CTime:
00514 return cTime( otime );
00515 break;
00516 case Iso:
00517 return isoDate( otime );
00518 break;
00519 case Custom:
00520 return custom( otime );
00521 break;
00522 }
00523 return QString::null;
00524 }
00525
00526 QString
00527 DateFormatter::dateString(const QDateTime& dtime, const QString& lang,
00528 bool shortFormat, bool includeSecs ) const
00529 {
00530 return DateFormatter::dateString( qdateToTimeT(dtime), lang, shortFormat, includeSecs );
00531 }
00532
00533 QCString
00534 DateFormatter::rfc2822(time_t otime) const
00535 {
00536 QDateTime tmp;
00537 QCString ret;
00538
00539 tmp.setTime_t(otime);
00540
00541 ret = tmp.toString("ddd, dd MMM yyyy hh:mm:ss ").latin1();
00542 ret += zone(otime);
00543
00544 return ret;
00545 }
00546
00547 QString
00548 DateFormatter::custom(time_t t) const
00549 {
00550 if ( mCustomFormat.isEmpty() )
00551 return QString::null;
00552
00553 int z = mCustomFormat.find("Z");
00554 QDateTime d;
00555 QString ret = mCustomFormat;
00556
00557 d.setTime_t(t);
00558 if ( z != -1 ) {
00559 ret.replace(z,1,zone(t));
00560 }
00561
00562 ret = d.toString(ret);
00563
00564 return ret;
00565 }
00566
00567 void
00568 DateFormatter::setCustomFormat(const QString& format)
00569 {
00570 mCustomFormat = format;
00571 mFormat = Custom;
00572 }
00573
00574 QString
00575 DateFormatter::getCustomFormat() const
00576 {
00577 return mCustomFormat;
00578 }
00579
00580
00581 QCString
00582 DateFormatter::zone(time_t otime) const
00583 {
00584 QCString ret;
00585 #if defined(HAVE_TIMEZONE) || defined(HAVE_TM_GMTOFF)
00586 struct tm *local = localtime( &otime );
00587 #endif
00588
00589 #if defined(HAVE_TIMEZONE)
00590
00591
00592 int secs = abs(timezone);
00593 int neg = (timezone>0)?1:0;
00594 int hours = secs/3600;
00595 int mins = (secs - hours*3600)/60;
00596
00597
00598 if ( local->tm_isdst > 0 ) {
00599 mDaylight = 1;
00600 if ( neg )
00601 --hours;
00602 else
00603 ++hours;
00604 } else
00605 mDaylight = 0;
00606
00607 ret.sprintf("%c%.2d%.2d",(neg)?'-':'+', hours, mins);
00608
00609 #elif defined(HAVE_TM_GMTOFF)
00610
00611 int secs = abs( local->tm_gmtoff );
00612 int neg = (local->tm_gmtoff<0)?1:0;
00613 int hours = secs/3600;
00614 int mins = (secs - hours*3600)/60;
00615
00616 if ( local->tm_isdst > 0 )
00617 mDaylight = 1;
00618 else
00619 mDaylight = 0;
00620
00621 ret.sprintf("%c%.2d%.2d",(neg)?'-':'+', hours, mins);
00622
00623 #else
00624
00625 QDateTime d1 = QDateTime::fromString( asctime(gmtime(&otime)) );
00626 QDateTime d2 = QDateTime::fromString( asctime(localtime(&otime)) );
00627 int secs = d1.secsTo(d2);
00628 int neg = (secs<0)?1:0;
00629 secs = abs(secs);
00630 int hours = secs/3600;
00631 int mins = (secs - hours*3600)/60;
00632
00633 ret.sprintf("%c%.2d%.2d",(neg)?'-':'+', hours, mins);
00634
00635 #endif
00636
00637 return ret;
00638 }
00639
00640 time_t
00641 DateFormatter::qdateToTimeT(const QDateTime& dt) const
00642 {
00643 QDateTime epoch( QDate(1970, 1,1), QTime(00,00,00) );
00644 time_t otime;
00645 time( &otime );
00646
00647 QDateTime d1 = QDateTime::fromString( asctime(gmtime(&otime)) );
00648 QDateTime d2 = QDateTime::fromString( asctime(localtime(&otime)) );
00649 time_t drf = epoch.secsTo( dt ) - d1.secsTo( d2 );
00650
00651 return drf;
00652 }
00653
00654 QString
00655 DateFormatter::fancy(time_t otime) const
00656 {
00657 KLocale *locale = KGlobal::locale();
00658
00659 if ( otime <= 0 )
00660 return i18n( "unknown" );
00661
00662 if ( !mCurrentTime ) {
00663 time( &mCurrentTime );
00664 mDate.setTime_t( mCurrentTime );
00665 }
00666
00667 QDateTime old;
00668 old.setTime_t( otime );
00669
00670
00671 if ( mCurrentTime + 60 * 60 >= otime ) {
00672 time_t diff = mCurrentTime - otime;
00673
00674 if ( diff < 24 * 60 * 60 ) {
00675 if ( old.date().year() == mDate.date().year() &&
00676 old.date().dayOfYear() == mDate.date().dayOfYear() )
00677 return i18n( "Today %1" ).arg( locale->
00678 formatTime( old.time(), true ) );
00679 }
00680 if ( diff < 2 * 24 * 60 * 60 ) {
00681 QDateTime yesterday( mDate.addDays( -1 ) );
00682 if ( old.date().year() == yesterday.date().year() &&
00683 old.date().dayOfYear() == yesterday.date().dayOfYear() )
00684 return i18n( "Yesterday %1" ).arg( locale->
00685 formatTime( old.time(), true) );
00686 }
00687 for ( int i = 3; i < 7; i++ )
00688 if ( diff < i * 24 * 60 * 60 ) {
00689 QDateTime weekday( mDate.addDays( -i + 1 ) );
00690 if ( old.date().year() == weekday.date().year() &&
00691 old.date().dayOfYear() == weekday.date().dayOfYear() )
00692 return i18n( "1. weekday, 2. time", "%1 %2" ).
00693 #if KDE_IS_VERSION( 3, 1, 90 )
00694 arg( locale->calendar()->weekDayName( old.date() ) ).
00695 #else
00696 arg( locale->weekDayName( old.date().dayOfWeek() ) ).
00697 #endif
00698 arg( locale->formatTime( old.time(), true) );
00699 }
00700 }
00701
00702 return locale->formatDateTime( old );
00703
00704 }
00705
00706 QString
00707 DateFormatter::localized(time_t otime, bool shortFormat, bool includeSecs,
00708 const QString& localeLanguage ) const
00709 {
00710 QDateTime tmp;
00711 QString ret;
00712 KLocale *locale = KGlobal::locale();
00713
00714 tmp.setTime_t( otime );
00715
00716
00717 if ( !localeLanguage.isEmpty() ) {
00718 locale=new KLocale(localeLanguage);
00719 locale->setLanguage(localeLanguage);
00720 locale->setCountry(localeLanguage);
00721 ret = locale->formatDateTime( tmp, shortFormat, includeSecs );
00722 delete locale;
00723 } else {
00724 ret = locale->formatDateTime( tmp, shortFormat, includeSecs );
00725 }
00726
00727 return ret;
00728 }
00729
00730 QString
00731 DateFormatter::cTime(time_t otime) const
00732 {
00733 return QString::fromLatin1( ctime( &otime ) ).stripWhiteSpace() ;
00734 }
00735
00736 QString
00737 DateFormatter::isoDate(time_t otime) const
00738 {
00739 char cstr[64];
00740 strftime( cstr, 63, "%Y-%m-%d %H:%M:%S", localtime(&otime) );
00741 return QString( cstr );
00742 }
00743
00744
00745 void
00746 DateFormatter::reset()
00747 {
00748 mCurrentTime = 0;
00749 }
00750
00751 QString
00752 DateFormatter::formatDate(DateFormatter::FormatType t, time_t otime,
00753 const QString& data, bool shortFormat, bool includeSecs )
00754 {
00755 DateFormatter f( t );
00756 if ( t == DateFormatter::Custom ) {
00757 f.setCustomFormat( data );
00758 }
00759 return f.dateString( otime, data, shortFormat, includeSecs );
00760 }
00761
00762 QString
00763 DateFormatter::formatCurrentDate( DateFormatter::FormatType t, const QString& data,
00764 bool shortFormat, bool includeSecs )
00765 {
00766 DateFormatter f( t );
00767 if ( t == DateFormatter::Custom ) {
00768 f.setCustomFormat( data );
00769 }
00770 return f.dateString( time(0), data, shortFormat, includeSecs );
00771 }
00772
00773 QCString
00774 DateFormatter::rfc2822FormatDate( time_t t )
00775 {
00776 DateFormatter f;
00777 return f.rfc2822( t );
00778 }
00779
00780 bool
00781 DateFormatter::isDaylight()
00782 {
00783 if ( mDaylight == -1 ) {
00784 time_t ntime = time( 0 );
00785 struct tm *local = localtime( &ntime );
00786 if ( local->tm_isdst > 0 ) {
00787 mDaylight = 1;
00788 return true;
00789 } else {
00790 mDaylight = 0;
00791 return false;
00792 }
00793 } else if ( mDaylight != 0 )
00794 return true;
00795 else
00796 return false;
00797 }
00798
00799 }