kmail

kmfoldermbox.cpp

00001 /* -*- c-basic-offset: 2 -*-
00002  * kmail: KDE mail client
00003  * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  */
00020 #include <config.h>
00021 #include <qfileinfo.h>
00022 #include <qregexp.h>
00023 
00024 #include "kmfoldermbox.h"
00025 #include "folderstorage.h"
00026 #include "kmfolder.h"
00027 #include "kmkernel.h"
00028 #include "kmmsgdict.h"
00029 #include "undostack.h"
00030 #include "kcursorsaver.h"
00031 #include "jobscheduler.h"
00032 #include "compactionjob.h"
00033 #include "util.h"
00034 
00035 #include <kdebug.h>
00036 #include <klocale.h>
00037 #include <kmessagebox.h>
00038 #include <knotifyclient.h>
00039 #include <kprocess.h>
00040 #include <kconfig.h>
00041 
00042 #include <stdio.h>
00043 #include <errno.h>
00044 #include <assert.h>
00045 #include <unistd.h>
00046 
00047 #ifdef HAVE_FCNTL_H
00048 #include <fcntl.h>
00049 #endif
00050 
00051 #include <stdlib.h>
00052 #include <sys/types.h>
00053 #include <sys/stat.h>
00054 #include <sys/file.h>
00055 #include "broadcaststatus.h"
00056 using KPIM::BroadcastStatus;
00057 
00058 #ifndef MAX_LINE
00059 #define MAX_LINE 4096
00060 #endif
00061 #ifndef INIT_MSGS
00062 #define INIT_MSGS 8
00063 #endif
00064 
00065 // Regular expression to find the line that seperates messages in a mail
00066 // folder:
00067 #define MSG_SEPERATOR_START "From "
00068 #define MSG_SEPERATOR_START_LEN (sizeof(MSG_SEPERATOR_START) - 1)
00069 #define MSG_SEPERATOR_REGEX "^From .*[0-9][0-9]:[0-9][0-9]"
00070 
00071 
00072 //-----------------------------------------------------------------------------
00073 KMFolderMbox::KMFolderMbox(KMFolder* folder, const char* name)
00074   : KMFolderIndex(folder, name)
00075 {
00076   mStream         = 0;
00077   mFilesLocked    = false;
00078   mReadOnly       = false;
00079   mLockType       = lock_none;
00080 }
00081 
00082 
00083 //-----------------------------------------------------------------------------
00084 KMFolderMbox::~KMFolderMbox()
00085 {
00086   if (mOpenCount>0) close("~kmfoldermbox", true);
00087   if (kmkernel->undoStack()) kmkernel->undoStack()->folderDestroyed( folder() );
00088 }
00089 
00090 //-----------------------------------------------------------------------------
00091 int KMFolderMbox::open(const char *owner)
00092 {
00093   int rc = 0;
00094 
00095   mOpenCount++;
00096   kmkernel->jobScheduler()->notifyOpeningFolder( folder() );
00097 
00098   if (mOpenCount > 1) {
00099      assert(mStream);
00100      return 0;  // already open
00101   }
00102 
00103   assert(!folder()->name().isEmpty());
00104 
00105   mFilesLocked = false;
00106   mStream = fopen(QFile::encodeName(location()), "r+"); // messages file
00107   if (!mStream)
00108   {
00109     KNotifyClient::event( 0, "warning",
00110     i18n("Cannot open file \"%1\":\n%2").arg(location()).arg(strerror(errno)));
00111     kdDebug(5006) << "Cannot open folder `" << location() << "': " << strerror(errno) << endl;
00112     mOpenCount = 0;
00113     return errno;
00114   }
00115 
00116   lock();
00117 
00118   if (!folder()->path().isEmpty())
00119   {
00120      KMFolderIndex::IndexStatus index_status = indexStatus();
00121      // test if index file exists and is up-to-date
00122      if (KMFolderIndex::IndexOk != index_status)
00123      {
00124        // only show a warning if the index file exists, otherwise it can be
00125        // silently regenerated
00126        if (KMFolderIndex::IndexTooOld == index_status) {
00127         QString msg = i18n("<qt><p>The index of folder '%2' seems "
00128                            "to be out of date. To prevent message "
00129                            "corruption the index will be "
00130                            "regenerated. As a result deleted "
00131                            "messages might reappear and status "
00132                            "flags might be lost.</p>"
00133                            "<p>Please read the corresponding entry "
00134                            "in the <a href=\"%1\">FAQ section of the manual "
00135                            "of KMail</a> for "
00136                            "information about how to prevent this "
00137                            "problem from happening again.</p></qt>")
00138                       .arg("help:/kmail/faq.html#faq-index-regeneration")
00139                       .arg(name());
00140         // When KMail is starting up we have to show a non-blocking message
00141         // box so that the initialization can continue. We don't show a
00142         // queued message box when KMail isn't starting up because queued
00143         // message boxes don't have a "Don't ask again" checkbox.
00144         if (kmkernel->startingUp())
00145         {
00146           KConfigGroup configGroup( KMKernel::config(), "Notification Messages" );
00147           bool showMessage =
00148             configGroup.readBoolEntry( "showIndexRegenerationMessage", true );
00149           if (showMessage)
00150             KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
00151                                            msg, i18n("Index Out of Date"),
00152                                            KMessageBox::AllowLink );
00153         }
00154         else
00155         {
00156             KCursorSaver idle(KBusyPtr::idle());
00157             KMessageBox::information( 0, msg, i18n("Index Out of Date"),
00158                                       "showIndexRegenerationMessage",
00159                                       KMessageBox::AllowLink );
00160         }
00161        }
00162        QString str;
00163        mIndexStream = 0;
00164        str = i18n("Folder `%1' changed. Recreating index.")
00165              .arg(name());
00166        emit statusMsg(str);
00167      } else {
00168        mIndexStream = fopen(QFile::encodeName(indexLocation()), "r+"); // index file
00169        if ( mIndexStream ) {
00170          fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
00171          updateIndexStreamPtr();
00172        }
00173      }
00174 
00175      if (!mIndexStream)
00176        rc = createIndexFromContents();
00177      else
00178        if (!readIndex())
00179          rc = createIndexFromContents();
00180   }
00181   else
00182   {
00183     mAutoCreateIndex = false;
00184     rc = createIndexFromContents();
00185   }
00186 
00187   mChanged = false;
00188 
00189   fcntl(fileno(mStream), F_SETFD, FD_CLOEXEC);
00190   if (mIndexStream)
00191      fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
00192 
00193   return rc;
00194 }
00195 
00196 //----------------------------------------------------------------------------
00197 int KMFolderMbox::canAccess()
00198 {
00199   assert(!folder()->name().isEmpty());
00200 
00201   if (access(QFile::encodeName(location()), R_OK | W_OK) != 0) {
00202     kdDebug(5006) << "KMFolderMbox::access call to access function failed" << endl;
00203       return 1;
00204   }
00205   return 0;
00206 }
00207 
00208 //-----------------------------------------------------------------------------
00209 int KMFolderMbox::create()
00210 {
00211   int rc;
00212   int old_umask;
00213 
00214   assert(!folder()->name().isEmpty());
00215   assert(mOpenCount == 0);
00216 
00217   kdDebug(5006) << "Creating folder " << name() << endl;
00218   if (access(QFile::encodeName(location()), F_OK) == 0) {
00219     kdDebug(5006) << "KMFolderMbox::create call to access function failed." << endl;
00220     kdDebug(5006) << "File:: " << endl;
00221     kdDebug(5006) << "Error " << endl;
00222     return EEXIST;
00223   }
00224 
00225   old_umask = umask(077);
00226   mStream = fopen(QFile::encodeName(location()), "w+"); //sven; open RW
00227   umask(old_umask);
00228 
00229   if (!mStream) return errno;
00230 
00231   fcntl(fileno(mStream), F_SETFD, FD_CLOEXEC);
00232 
00233   if (!folder()->path().isEmpty())
00234   {
00235     old_umask = umask(077);
00236     mIndexStream = fopen(QFile::encodeName(indexLocation()), "w+"); //sven; open RW
00237     updateIndexStreamPtr(true);
00238     umask(old_umask);
00239 
00240     if (!mIndexStream) return errno;
00241     fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
00242   }
00243   else
00244   {
00245     mAutoCreateIndex = false;
00246   }
00247 
00248   mOpenCount++;
00249   mChanged = false;
00250 
00251   rc = writeIndex();
00252   if (!rc) lock();
00253   return rc;
00254 }
00255 
00256 
00257 //-----------------------------------------------------------------------------
00258 void KMFolderMbox::reallyDoClose(const char *owner)
00259 {
00260   if (mAutoCreateIndex)
00261   {
00262       if (KMFolderIndex::IndexOk != indexStatus()) {
00263           kdDebug(5006) << "Critical error: " << location() <<
00264               " has been modified by an external application while KMail was running." << endl;
00265           //      exit(1); backed out due to broken nfs
00266       }
00267 
00268       updateIndex();
00269       writeConfig();
00270   }
00271 
00272   if (!noContent()) {
00273     if (mStream) unlock();
00274     mMsgList.clear(true);
00275 
00276     if (mStream) fclose(mStream);
00277     if (mIndexStream) {
00278       fclose(mIndexStream);
00279       updateIndexStreamPtr(true);
00280     }
00281   }
00282   mOpenCount   = 0;
00283   mStream      = 0;
00284   mIndexStream = 0;
00285   mFilesLocked = false;
00286   mUnreadMsgs  = -1;
00287 
00288   mMsgList.reset(INIT_MSGS);
00289 }
00290 
00291 //-----------------------------------------------------------------------------
00292 void KMFolderMbox::sync()
00293 {
00294   if (mOpenCount > 0)
00295     if (!mStream || fsync(fileno(mStream)) ||
00296         !mIndexStream || fsync(fileno(mIndexStream))) {
00297     kmkernel->emergencyExit( i18n("Could not sync index file <b>%1</b>: %2").arg( indexLocation() ).arg(errno ? QString::fromLocal8Bit(strerror(errno)) : i18n("Internal error. Please copy down the details and report a bug.")));
00298     }
00299 }
00300 
00301 //-----------------------------------------------------------------------------
00302 int KMFolderMbox::lock()
00303 {
00304   int rc;
00305   struct flock fl;
00306   fl.l_type=F_WRLCK;
00307   fl.l_whence=0;
00308   fl.l_start=0;
00309   fl.l_len=0;
00310   fl.l_pid=-1;
00311   QCString cmd_str;
00312   assert(mStream != 0);
00313   mFilesLocked = false;
00314   mReadOnly = false;
00315 
00316   switch( mLockType )
00317   {
00318     case FCNTL:
00319       rc = fcntl(fileno(mStream), F_SETLKW, &fl);
00320 
00321       if (rc < 0)
00322       {
00323         kdDebug(5006) << "Cannot lock folder `" << location() << "': "
00324                   << strerror(errno) << " (" << errno << ")" << endl;
00325         mReadOnly = true;
00326         return errno;
00327       }
00328 
00329       if (mIndexStream)
00330       {
00331         rc = fcntl(fileno(mIndexStream), F_SETLK, &fl);
00332 
00333         if (rc < 0)
00334         {
00335           kdDebug(5006) << "Cannot lock index of folder `" << location() << "': "
00336                     << strerror(errno) << " (" << errno << ")" << endl;
00337           rc = errno;
00338           fl.l_type = F_UNLCK;
00339           /*rc =*/ fcntl(fileno(mIndexStream), F_SETLK, &fl);
00340           mReadOnly = true;
00341           return rc;
00342         }
00343       }
00344       break;
00345 
00346     case procmail_lockfile:
00347       cmd_str = "lockfile -l20 -r5 ";
00348       if (!mProcmailLockFileName.isEmpty())
00349         cmd_str += QFile::encodeName(KProcess::quote(mProcmailLockFileName));
00350       else
00351         cmd_str += QFile::encodeName(KProcess::quote(location() + ".lock"));
00352 
00353       rc = system( cmd_str.data() );
00354       if( rc != 0 )
00355       {
00356         kdDebug(5006) << "Cannot lock folder `" << location() << "': "
00357                   << strerror(rc) << " (" << rc << ")" << endl;
00358         mReadOnly = true;
00359         return rc;
00360       }
00361       if( mIndexStream )
00362       {
00363         cmd_str = "lockfile -l20 -r5 " + QFile::encodeName(KProcess::quote(indexLocation() + ".lock"));
00364         rc = system( cmd_str.data() );
00365         if( rc != 0 )
00366         {
00367           kdDebug(5006) << "Cannot lock index of folder `" << location() << "': "
00368                     << strerror(rc) << " (" << rc << ")" << endl;
00369           mReadOnly = true;
00370           return rc;
00371         }
00372       }
00373       break;
00374 
00375     case mutt_dotlock:
00376       cmd_str = "mutt_dotlock " + QFile::encodeName(KProcess::quote(location()));
00377       rc = system( cmd_str.data() );
00378       if( rc != 0 )
00379       {
00380         kdDebug(5006) << "Cannot lock folder `" << location() << "': "
00381                   << strerror(rc) << " (" << rc << ")" << endl;
00382         mReadOnly = true;
00383         return rc;
00384       }
00385       if( mIndexStream )
00386       {
00387         cmd_str = "mutt_dotlock " + QFile::encodeName(KProcess::quote(indexLocation()));
00388         rc = system( cmd_str.data() );
00389         if( rc != 0 )
00390         {
00391           kdDebug(5006) << "Cannot lock index of folder `" << location() << "': "
00392                     << strerror(rc) << " (" << rc << ")" << endl;
00393           mReadOnly = true;
00394           return rc;
00395         }
00396       }
00397       break;
00398 
00399     case mutt_dotlock_privileged:
00400       cmd_str = "mutt_dotlock -p " + QFile::encodeName(KProcess::quote(location()));
00401       rc = system( cmd_str.data() );
00402       if( rc != 0 )
00403       {
00404         kdDebug(5006) << "Cannot lock folder `" << location() << "': "
00405                   << strerror(rc) << " (" << rc << ")" << endl;
00406         mReadOnly = true;
00407         return rc;
00408       }
00409       if( mIndexStream )
00410       {
00411         cmd_str = "mutt_dotlock -p " + QFile::encodeName(KProcess::quote(indexLocation()));
00412         rc = system( cmd_str.data() );
00413         if( rc != 0 )
00414         {
00415           kdDebug(5006) << "Cannot lock index of folder `" << location() << "': "
00416                     << strerror(rc) << " (" << rc << ")" << endl;
00417           mReadOnly = true;
00418           return rc;
00419         }
00420       }
00421       break;
00422 
00423     case lock_none:
00424     default:
00425       break;
00426   }
00427 
00428 
00429   mFilesLocked = true;
00430   return 0;
00431 }
00432 
00433 //-------------------------------------------------------------
00434 FolderJob*
00435 KMFolderMbox::doCreateJob( KMMessage *msg, FolderJob::JobType jt,
00436                            KMFolder *folder, QString, const AttachmentStrategy* ) const
00437 {
00438   MboxJob *job = new MboxJob( msg, jt, folder );
00439   job->setParent( this );
00440   return job;
00441 }
00442 
00443 //-------------------------------------------------------------
00444 FolderJob*
00445 KMFolderMbox::doCreateJob( QPtrList<KMMessage>& msgList, const QString& sets,
00446                            FolderJob::JobType jt, KMFolder *folder ) const
00447 {
00448   MboxJob *job = new MboxJob( msgList, sets, jt, folder );
00449   job->setParent( this );
00450   return job;
00451 }
00452 
00453 //-----------------------------------------------------------------------------
00454 int KMFolderMbox::unlock()
00455 {
00456   int rc;
00457   struct flock fl;
00458   fl.l_type=F_UNLCK;
00459   fl.l_whence=0;
00460   fl.l_start=0;
00461   fl.l_len=0;
00462   QCString cmd_str;
00463 
00464   assert(mStream != 0);
00465   mFilesLocked = false;
00466 
00467   switch( mLockType )
00468   {
00469     case FCNTL:
00470       if (mIndexStream) fcntl(fileno(mIndexStream), F_SETLK, &fl);
00471       fcntl(fileno(mStream), F_SETLK, &fl);
00472       rc = errno;
00473       break;
00474 
00475     case procmail_lockfile:
00476       cmd_str = "rm -f ";
00477       if (!mProcmailLockFileName.isEmpty())
00478         cmd_str += QFile::encodeName(KProcess::quote(mProcmailLockFileName));
00479       else
00480         cmd_str += QFile::encodeName(KProcess::quote(location() + ".lock"));
00481 
00482       rc = system( cmd_str.data() );
00483       if( mIndexStream )
00484       {
00485         cmd_str = "rm -f " + QFile::encodeName(KProcess::quote(indexLocation() + ".lock"));
00486         rc = system( cmd_str.data() );
00487       }
00488       break;
00489 
00490     case mutt_dotlock:
00491       cmd_str = "mutt_dotlock -u " + QFile::encodeName(KProcess::quote(location()));
00492       rc = system( cmd_str.data() );
00493       if( mIndexStream )
00494       {
00495         cmd_str = "mutt_dotlock -u " + QFile::encodeName(KProcess::quote(indexLocation()));
00496         rc = system( cmd_str.data() );
00497       }
00498       break;
00499 
00500     case mutt_dotlock_privileged:
00501       cmd_str = "mutt_dotlock -p -u " + QFile::encodeName(KProcess::quote(location()));
00502       rc = system( cmd_str.data() );
00503       if( mIndexStream )
00504       {
00505         cmd_str = "mutt_dotlock -p -u " + QFile::encodeName(KProcess::quote(indexLocation()));
00506         rc = system( cmd_str.data() );
00507       }
00508       break;
00509 
00510     case lock_none:
00511     default:
00512       rc = 0;
00513       break;
00514   }
00515 
00516   return rc;
00517 }
00518 
00519 
00520 //-----------------------------------------------------------------------------
00521 KMFolderIndex::IndexStatus KMFolderMbox::indexStatus()
00522 {
00523   QFileInfo contInfo(location());
00524   QFileInfo indInfo(indexLocation());
00525 
00526   if (!contInfo.exists()) return KMFolderIndex::IndexOk;
00527   if (!indInfo.exists()) return KMFolderIndex::IndexMissing;
00528 
00529   // Check whether the mbox file is more than 5 seconds newer than the index
00530   // file. The 5 seconds are added to reduce the number of false alerts due
00531   // to slightly out of sync clocks of the NFS server and the local machine.
00532   return ( contInfo.lastModified() > indInfo.lastModified().addSecs(5) )
00533       ? KMFolderIndex::IndexTooOld
00534       : KMFolderIndex::IndexOk;
00535 }
00536 
00537 
00538 //-----------------------------------------------------------------------------
00539 int KMFolderMbox::createIndexFromContents()
00540 {
00541   char line[MAX_LINE];
00542   char status[8], xstatus[8];
00543   QCString subjStr, dateStr, fromStr, toStr, xmarkStr, *lastStr=0;
00544   QCString replyToIdStr, replyToAuxIdStr, referencesStr, msgIdStr;
00545   QCString sizeServerStr, uidStr;
00546   QCString contentTypeStr, charset;
00547   bool atEof = false;
00548   bool inHeader = true;
00549   KMMsgInfo* mi;
00550   QString msgStr;
00551   QRegExp regexp(MSG_SEPERATOR_REGEX);
00552   int i, num, numStatus;
00553   short needStatus;
00554 
00555   assert(mStream != 0);
00556   rewind(mStream);
00557 
00558   mMsgList.clear();
00559 
00560   num     = -1;
00561   numStatus= 11;
00562   off_t offs = 0;
00563   size_t size = 0;
00564   dateStr = "";
00565   fromStr = "";
00566   toStr = "";
00567   subjStr = "";
00568   *status = '\0';
00569   *xstatus = '\0';
00570   xmarkStr = "";
00571   replyToIdStr = "";
00572   replyToAuxIdStr = "";
00573   referencesStr = "";
00574   msgIdStr = "";
00575   needStatus = 3;
00576   size_t sizeServer = 0;
00577   ulong uid = 0;
00578 
00579 
00580   while (!atEof)
00581   {
00582     off_t pos = ftell(mStream);
00583     if (!fgets(line, MAX_LINE, mStream)) atEof = true;
00584 
00585     if (atEof ||
00586         (memcmp(line, MSG_SEPERATOR_START, MSG_SEPERATOR_START_LEN)==0 &&
00587          regexp.search(line) >= 0))
00588     {
00589       size = pos - offs;
00590       pos = ftell(mStream);
00591 
00592       if (num >= 0)
00593       {
00594         if (numStatus <= 0)
00595         {
00596           msgStr = i18n("Creating index file: one message done", "Creating index file: %n messages done", num);
00597           emit statusMsg(msgStr);
00598           numStatus = 10;
00599         }
00600 
00601         if (size > 0)
00602         {
00603           msgIdStr = msgIdStr.stripWhiteSpace();
00604           if( !msgIdStr.isEmpty() ) {
00605             int rightAngle;
00606             rightAngle = msgIdStr.find( '>' );
00607             if( rightAngle != -1 )
00608               msgIdStr.truncate( rightAngle + 1 );
00609           }
00610 
00611           replyToIdStr = replyToIdStr.stripWhiteSpace();
00612           if( !replyToIdStr.isEmpty() ) {
00613             int rightAngle;
00614             rightAngle = replyToIdStr.find( '>' );
00615             if( rightAngle != -1 )
00616               replyToIdStr.truncate( rightAngle + 1 );
00617           }
00618 
00619           referencesStr = referencesStr.stripWhiteSpace();
00620           if( !referencesStr.isEmpty() ) {
00621             int leftAngle, rightAngle;
00622             leftAngle = referencesStr.findRev( '<' );
00623             if( ( leftAngle != -1 )
00624                 && ( replyToIdStr.isEmpty() || ( replyToIdStr[0] != '<' ) ) ) {
00625               // use the last reference, instead of missing In-Reply-To
00626               replyToIdStr = referencesStr.mid( leftAngle );
00627             }
00628 
00629             // find second last reference
00630             leftAngle = referencesStr.findRev( '<', leftAngle - 1 );
00631             if( leftAngle != -1 )
00632               referencesStr = referencesStr.mid( leftAngle );
00633             rightAngle = referencesStr.findRev( '>' );
00634             if( rightAngle != -1 )
00635               referencesStr.truncate( rightAngle + 1 );
00636 
00637             // Store the second to last reference in the replyToAuxIdStr
00638             // It is a good candidate for threading the message below if the
00639             // message In-Reply-To points to is not kept in this folder,
00640             // but e.g. in an Outbox
00641             replyToAuxIdStr = referencesStr;
00642             rightAngle = referencesStr.find( '>' );
00643             if( rightAngle != -1 )
00644               replyToAuxIdStr.truncate( rightAngle + 1 );
00645           }
00646 
00647           contentTypeStr = contentTypeStr.stripWhiteSpace();
00648           charset = "";
00649           if ( !contentTypeStr.isEmpty() )
00650           {
00651             int cidx = contentTypeStr.find( "charset=" );
00652             if ( cidx != -1 ) {
00653               charset = contentTypeStr.mid( cidx + 8 );
00654               if ( !charset.isEmpty() && ( charset[0] == '"' ) ) {
00655                 charset = charset.mid( 1 );
00656               }
00657               cidx = 0;
00658               while ( (unsigned int) cidx < charset.length() ) {
00659                 if ( charset[cidx] == '"' || ( !isalnum(charset[cidx]) &&
00660                     charset[cidx] != '-' && charset[cidx] != '_' ) )
00661                   break;
00662                 ++cidx;
00663               }
00664               charset.truncate( cidx );
00665               // kdDebug() << "KMFolderMaildir::readFileHeaderIntern() charset found: " <<
00666               //              charset << " from " << contentTypeStr << endl;
00667             }
00668           }
00669 
00670           mi = new KMMsgInfo(folder());
00671           mi->init( subjStr.stripWhiteSpace(),
00672                     fromStr.stripWhiteSpace(),
00673                     toStr.stripWhiteSpace(),
00674                     0, KMMsgStatusNew,
00675                     xmarkStr.stripWhiteSpace(),
00676                     replyToIdStr, replyToAuxIdStr, msgIdStr,
00677                     KMMsgEncryptionStateUnknown, KMMsgSignatureStateUnknown,
00678                     KMMsgMDNStateUnknown, charset, offs, size, sizeServer, uid );
00679           mi->setStatus(status, xstatus);
00680           mi->setDate( dateStr.stripWhiteSpace() );
00681           mi->setDirty(false);
00682           mMsgList.append(mi, mExportsSernums );
00683 
00684           *status = '\0';
00685           *xstatus = '\0';
00686           needStatus = 3;
00687           xmarkStr = "";
00688           replyToIdStr = "";
00689           replyToAuxIdStr = "";
00690           referencesStr = "";
00691           msgIdStr = "";
00692           dateStr = "";
00693           fromStr = "";
00694           subjStr = "";
00695           sizeServer = 0;
00696           uid = 0;
00697         }
00698         else num--,numStatus++;
00699       }
00700 
00701       offs = ftell(mStream);
00702       num++;
00703       numStatus--;
00704       inHeader = true;
00705       continue;
00706     }
00707     // Is this a long header line?
00708     if (inHeader && (line[0]=='\t' || line[0]==' '))
00709     {
00710       i = 0;
00711       while (line [i]=='\t' || line [i]==' ') i++;
00712       if (line [i] < ' ' && line [i]>0) inHeader = false;
00713       else if (lastStr) *lastStr += line + i;
00714     }
00715     else lastStr = 0;
00716 
00717     if (inHeader && (line [0]=='\n' || line [0]=='\r'))
00718       inHeader = false;
00719     if (!inHeader) continue;
00720 
00721     /* -sanders Make all messages read when auto-recreating index */
00722     /* Reverted, as it breaks reading the sent mail status, for example.
00723        -till */
00724     if ((needStatus & 1) && strncasecmp(line, "Status:", 7) == 0)
00725     {
00726       for(i=0; i<4 && line[i+8] > ' '; i++)
00727         status[i] = line[i+8];
00728       status[i] = '\0';
00729       needStatus &= ~1;
00730     }
00731     else if ((needStatus & 2) && strncasecmp(line, "X-Status:", 9)==0)
00732     {
00733       for(i=0; i<4 && line[i+10] > ' '; i++)
00734         xstatus[i] = line[i+10];
00735       xstatus[i] = '\0';
00736       needStatus &= ~2;
00737     }
00738     else if (strncasecmp(line,"X-KMail-Mark:",13)==0)
00739         xmarkStr = QCString(line+13);
00740     else if (strncasecmp(line,"In-Reply-To:",12)==0) {
00741       replyToIdStr = QCString(line+12);
00742       lastStr = &replyToIdStr;
00743     }
00744     else if (strncasecmp(line,"References:",11)==0) {
00745       referencesStr = QCString(line+11);
00746       lastStr = &referencesStr;
00747     }
00748     else if (strncasecmp(line,"Message-Id:",11)==0) {
00749       msgIdStr = QCString(line+11);
00750       lastStr = &msgIdStr;
00751     }
00752     else if (strncasecmp(line,"Date:",5)==0)
00753     {
00754       dateStr = QCString(line+5);
00755       lastStr = &dateStr;
00756     }
00757     else if (strncasecmp(line,"From:", 5)==0)
00758     {
00759       fromStr = QCString(line+5);
00760       lastStr = &fromStr;
00761     }
00762     else if (strncasecmp(line,"To:", 3)==0)
00763     {
00764       toStr = QCString(line+3);
00765       lastStr = &toStr;
00766     }
00767     else if (strncasecmp(line,"Subject:",8)==0)
00768     {
00769       subjStr = QCString(line+8);
00770       lastStr = &subjStr;
00771     }
00772     else if (strncasecmp(line,"X-Length:",9)==0)
00773     {
00774       sizeServerStr = QCString(line+9);
00775       sizeServer = sizeServerStr.toULong();
00776       lastStr = &sizeServerStr;
00777     }
00778     else if (strncasecmp(line,"X-UID:",6)==0)
00779     {
00780       uidStr = QCString(line+6);
00781       uid = uidStr.toULong();
00782       lastStr = &uidStr;
00783     }
00784     else if (strncasecmp(line, "Content-Type:", 13) == 0)
00785     {
00786       contentTypeStr = QCString(line+13);
00787       lastStr = &contentTypeStr;
00788     }
00789   }
00790 
00791   if (mAutoCreateIndex)
00792   {
00793     emit statusMsg(i18n("Writing index file"));
00794     writeIndex();
00795   }
00796   else mHeaderOffset = 0;
00797 
00798   correctUnreadMsgsCount();
00799 
00800   if (kmkernel->outboxFolder() == folder() && count() > 0)
00801     KMessageBox::queuedMessageBox(0, KMessageBox::Information,
00802                                   i18n("Your outbox contains messages which were "
00803     "most-likely not created by KMail;\nplease remove them from there if you "
00804     "do not want KMail to send them."));
00805 
00806   invalidateFolder();
00807   return 0;
00808 }
00809 
00810 
00811 //-----------------------------------------------------------------------------
00812 KMMessage* KMFolderMbox::readMsg(int idx)
00813 {
00814   KMMsgInfo* mi = (KMMsgInfo*)mMsgList[idx];
00815 
00816   assert(mi!=0 && !mi->isMessage());
00817   assert(mStream != 0);
00818 
00819   KMMessage *msg = new KMMessage(*mi); // note that mi is deleted by the line below
00820   mMsgList.set(idx,&msg->toMsgBase()); // done now so that the serial number can be computed
00821   msg->fromDwString(getDwString(idx));
00822   return msg;
00823 }
00824 
00825 
00826 #define STRDIM(x) (sizeof(x)/sizeof(*x)-1)
00827 // performs (\n|^)>{n}From_ -> \1>{n-1}From_ conversion
00828 static size_t unescapeFrom( char* str, size_t strLen ) {
00829   if ( !str )
00830     return 0;
00831   if ( strLen <= STRDIM(">From ") )
00832     return strLen;
00833 
00834   // yes, *d++ = *s++ is a no-op as long as d == s (until after the
00835   // first >From_), but writes are cheap compared to reads and the
00836   // data is already in the cache from the read, so special-casing
00837   // might even be slower...
00838   const char * s = str;
00839   char * d = str;
00840   const char * const e = str + strLen - STRDIM(">From ");
00841 
00842   while ( s < e ) {
00843     if ( *s == '\n' && *(s+1) == '>' ) { // we can do the lookahead, since e is 6 chars from the end!
00844       *d++ = *s++;  // == '\n'
00845       *d++ = *s++;  // == '>'
00846       while ( s < e && *s == '>' )
00847         *d++ = *s++;
00848       if ( qstrncmp( s, "From ", STRDIM("From ") ) == 0 )
00849         --d;
00850     }
00851     *d++ = *s++; // yes, s might be e here, but e is not the end :-)
00852   }
00853   // copy the rest:
00854   while ( s < str + strLen )
00855     *d++ = *s++;
00856   if ( d < s ) // only NUL-terminate if it's shorter
00857     *d = 0;
00858 
00859   return d - str;
00860 }
00861 
00862 //static
00863 QByteArray KMFolderMbox::escapeFrom( const DwString & str ) {
00864   const unsigned int strLen = str.length();
00865   if ( strLen <= STRDIM("From ") )
00866     return KMail::Util::ByteArray( str );
00867   // worst case: \nFrom_\nFrom_\nFrom_... => grows to 7/6
00868   QByteArray result( int( strLen + 5 ) / 6 * 7 + 1 );
00869 
00870   const char * s = str.data();
00871   const char * const e = s + strLen - STRDIM("From ");
00872   char * d = result.data();
00873 
00874   bool onlyAnglesAfterLF = false; // dont' match ^From_
00875   while ( s < e ) {
00876     switch ( *s ) {
00877     case '\n':
00878       onlyAnglesAfterLF = true;
00879       break;
00880     case '>':
00881       break;
00882     case 'F':
00883       if ( onlyAnglesAfterLF && qstrncmp( s+1, "rom ", STRDIM("rom ") ) == 0 )
00884         *d++ = '>';
00885       // fall through
00886     default:
00887       onlyAnglesAfterLF = false;
00888       break;
00889     }
00890     *d++ = *s++;
00891   }
00892   while ( s < str.data() + strLen )
00893     *d++ = *s++;
00894 
00895   result.truncate( d - result.data() );
00896   return result;
00897 }
00898 
00899 #undef STRDIM
00900 
00901 //-----------------------------------------------------------------------------
00902 DwString KMFolderMbox::getDwString(int idx)
00903 {
00904   KMMsgInfo* mi = (KMMsgInfo*)mMsgList[idx];
00905 
00906   assert(mi!=0);
00907   assert(mStream != 0);
00908 
00909   size_t msgSize = mi->msgSize();
00910   char* msgText = new char[ msgSize + 1 ];
00911 
00912   fseek(mStream, mi->folderOffset(), SEEK_SET);
00913   fread(msgText, msgSize, 1, mStream);
00914   msgText[msgSize] = '\0';
00915 
00916   size_t newMsgSize = unescapeFrom( msgText, msgSize );
00917   newMsgSize = KMail::Util::crlf2lf( msgText, newMsgSize );
00918 
00919   DwString msgStr;
00920   // the DwString takes possession of msgText, so we must not delete msgText
00921   msgStr.TakeBuffer( msgText, msgSize + 1, 0, newMsgSize );
00922   return msgStr;
00923 }
00924 
00925 
00926 //-----------------------------------------------------------------------------
00927 int KMFolderMbox::addMsg( KMMessage* aMsg, int* aIndex_ret )
00928 {
00929   if (!canAddMsgNow(aMsg, aIndex_ret)) return 0;
00930   bool opened = false;
00931   QByteArray msgText;
00932   char endStr[3];
00933   int idx = -1, rc;
00934   KMFolder* msgParent;
00935   bool editing = false;
00936   int growth = 0;
00937 
00938   if (!mStream)
00939   {
00940     opened = true;
00941     rc = open("mboxaddMsg");
00942     kdDebug(5006) << "KMFolderMBox::addMsg-open: " << rc << " of folder: " << label() << endl;
00943     if (rc) return rc;
00944   }
00945 
00946   // take message out of the folder it is currently in, if any
00947   msgParent = aMsg->parent();
00948   if (msgParent)
00949   {
00950     if ( msgParent== folder() )
00951     {
00952         if (kmkernel->folderIsDraftOrOutbox( folder() ))
00953           //special case for Edit message.
00954           {
00955             kdDebug(5006) << "Editing message in outbox or drafts" << endl;
00956             editing = true;
00957           }
00958         else
00959           return 0;
00960       }
00961 
00962     idx = msgParent->find(aMsg);
00963     msgParent->getMsg( idx );
00964   }
00965 
00966   if (folderType() != KMFolderTypeImap)
00967   {
00968 /*
00969 QFile fileD0( "testdat_xx-kmfoldermbox-0" );
00970 if( fileD0.open( IO_WriteOnly ) ) {
00971     QDataStream ds( &fileD0 );
00972     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00973     fileD0.close();  // If data is 0 we just create a zero length file.
00974 }
00975 */
00976     aMsg->setStatusFields();
00977 /*
00978 QFile fileD1( "testdat_xx-kmfoldermbox-1" );
00979 if( fileD1.open( IO_WriteOnly ) ) {
00980     QDataStream ds( &fileD1 );
00981     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00982     fileD1.close();  // If data is 0 we just create a zero length file.
00983 }
00984 */
00985     if (aMsg->headerField("Content-Type").isEmpty())  // This might be added by
00986       aMsg->removeHeaderField("Content-Type");        // the line above
00987   }
00988   msgText = escapeFrom( aMsg->asDwString() );
00989   size_t len = msgText.size();
00990 
00991   assert(mStream != 0);
00992   clearerr(mStream);
00993   if (len <= 0)
00994   {
00995     kdDebug(5006) << "Message added to folder `" << name() << "' contains no data. Ignoring it." << endl;
00996     if (opened) close("mboxaddMsg");
00997     return 0;
00998   }
00999 
01000   // Make sure the file is large enough to check for an end
01001   // character
01002   fseek(mStream, 0, SEEK_END);
01003   off_t revert = ftell(mStream);
01004   if (ftell(mStream) >= 2) {
01005       // write message to folder file
01006       fseek(mStream, -2, SEEK_END);
01007       fread(endStr, 1, 2, mStream); // ensure separating empty line
01008       if (ftell(mStream) > 0 && endStr[0]!='\n') {
01009           ++growth;
01010           if (endStr[1]!='\n') {
01011               //printf ("****endStr[1]=%c\n", endStr[1]);
01012               fwrite("\n\n", 1, 2, mStream);
01013               ++growth;
01014           }
01015           else fwrite("\n", 1, 1, mStream);
01016       }
01017   }
01018   fseek(mStream,0,SEEK_END); // this is needed on solaris and others
01019   int error = ferror(mStream);
01020   if (error)
01021   {
01022     if (opened) close("mboxaddMsg");
01023     return error;
01024   }
01025 
01026   QCString messageSeparator( aMsg->mboxMessageSeparator() );
01027   fwrite( messageSeparator.data(), messageSeparator.length(), 1, mStream );
01028   off_t offs = ftell(mStream);
01029   fwrite(msgText.data(), len, 1, mStream);
01030   if (msgText[(int)len-1]!='\n') fwrite("\n\n", 1, 2, mStream);
01031   fflush(mStream);
01032   size_t size = ftell(mStream) - offs;
01033 
01034   error = ferror(mStream);
01035   if (error) {
01036     kdDebug(5006) << "Error: Could not add message to folder: " << strerror(errno) << endl;
01037     if (ftell(mStream) > revert) {
01038       kdDebug(5006) << "Undoing changes" << endl;
01039       truncate( QFile::encodeName(location()), revert );
01040     }
01041     kmkernel->emergencyExit( i18n("Could not add message to folder: ") + QString::fromLocal8Bit(strerror(errno)));
01042 
01043     /* This code is not 100% reliable
01044     bool busy = kmkernel->kbp()->isBusy();
01045     if (busy) kmkernel->kbp()->idle();
01046     KMessageBox::sorry(0,
01047           i18n("Unable to add message to folder.\n"
01048                "(No space left on device or insufficient quota?)\n"
01049                "Free space and sufficient quota are required to continue safely."));
01050     if (busy) kmkernel->kbp()->busy();
01051     if (opened) close();
01052     kmkernel->kbp()->idle();
01053     */
01054     return error;
01055   }
01056 
01057   if (msgParent) {
01058     if (idx >= 0) msgParent->take(idx);
01059   }
01060 //  if (mAccount) aMsg->removeHeaderField("X-UID");
01061 
01062   if (aMsg->isUnread() || aMsg->isNew() ||
01063       (folder() == kmkernel->outboxFolder())) {
01064     if (mUnreadMsgs == -1) mUnreadMsgs = 1;
01065     else ++mUnreadMsgs;
01066     if ( !mQuiet )
01067       emit numUnreadMsgsChanged( folder() );
01068   }
01069   ++mTotalMsgs;
01070 
01071   if ( aMsg->attachmentState() == KMMsgAttachmentUnknown &&
01072        aMsg->readyToShow() )
01073     aMsg->updateAttachmentState();
01074 
01075   // store information about the position in the folder file in the message
01076   aMsg->setParent(folder());
01077   aMsg->setFolderOffset(offs);
01078   aMsg->setMsgSize(size);
01079   idx = mMsgList.append(&aMsg->toMsgBase(), mExportsSernums );
01080   if ( aMsg->getMsgSerNum() <= 0 )
01081     aMsg->setMsgSerNum();
01082   else
01083     replaceMsgSerNum( aMsg->getMsgSerNum(), &aMsg->toMsgBase(), idx );
01084 
01085   // change the length of the previous message to encompass white space added
01086   if ((idx > 0) && (growth > 0)) {
01087     // don't grow if a deleted message claims space at the end of the file
01088     if ((ulong)revert == mMsgList[idx - 1]->folderOffset() + mMsgList[idx - 1]->msgSize() )
01089       mMsgList[idx - 1]->setMsgSize( mMsgList[idx - 1]->msgSize() + growth );
01090   }
01091 
01092   // write index entry if desired
01093   if (mAutoCreateIndex)
01094   {
01095     assert(mIndexStream != 0);
01096     clearerr(mIndexStream);
01097     fseek(mIndexStream, 0, SEEK_END);
01098     revert = ftell(mIndexStream);
01099 
01100     KMMsgBase * mb = &aMsg->toMsgBase();
01101         int len;
01102         const uchar *buffer = mb->asIndexString(len);
01103         fwrite(&len,sizeof(len), 1, mIndexStream);
01104         mb->setIndexOffset( ftell(mIndexStream) );
01105         mb->setIndexLength( len );
01106         if(fwrite(buffer, len, 1, mIndexStream) != 1)
01107             kdDebug(5006) << "Whoa! " << __FILE__ << ":" << __LINE__ << endl;
01108 
01109     fflush(mIndexStream);
01110     error = ferror(mIndexStream);
01111 
01112     if ( mExportsSernums )
01113       error |= appendToFolderIdsFile( idx );
01114 
01115     if (error) {
01116       kdWarning(5006) << "Error: Could not add message to folder (No space left on device?)" << endl;
01117       if (ftell(mIndexStream) > revert) {
01118         kdWarning(5006) << "Undoing changes" << endl;
01119         truncate( QFile::encodeName(indexLocation()), revert );
01120       }
01121       if ( errno )
01122         kmkernel->emergencyExit( i18n("Could not add message to folder:") + QString::fromLocal8Bit(strerror(errno)));
01123       else
01124         kmkernel->emergencyExit( i18n("Could not add message to folder (No space left on device?)") );
01125 
01126       /* This code may not be 100% reliable
01127       bool busy = kmkernel->kbp()->isBusy();
01128       if (busy) kmkernel->kbp()->idle();
01129       KMessageBox::sorry(0,
01130         i18n("Unable to add message to folder.\n"
01131              "(No space left on device or insufficient quota?)\n"
01132              "Free space and sufficient quota are required to continue safely."));
01133       if (busy) kmkernel->kbp()->busy();
01134       if (opened) close();
01135       */
01136       return error;
01137     }
01138   }
01139 
01140   // some "paper work"
01141   if (aIndex_ret) *aIndex_ret = idx;
01142   emitMsgAddedSignals(idx);
01143   if (opened) close("mboxaddMsg");
01144 
01145   // All streams have been flushed without errors if we arrive here
01146   // Return success!
01147   // (Don't return status of stream, it may have been closed already.)
01148   return 0;
01149 }
01150 
01151 int KMFolderMbox::compact( unsigned int startIndex, int nbMessages, FILE* tmpfile, off_t& offs, bool& done )
01152 {
01153   int rc = 0;
01154   QCString mtext;
01155   unsigned int stopIndex = nbMessages == -1 ? mMsgList.count() :
01156                            QMIN( mMsgList.count(), startIndex + nbMessages );
01157   //kdDebug(5006) << "KMFolderMbox: compacting from " << startIndex << " to " << stopIndex << endl;
01158   for(unsigned int idx = startIndex; idx < stopIndex; ++idx) {
01159     KMMsgInfo* mi = (KMMsgInfo*)mMsgList.at(idx);
01160     size_t msize = mi->msgSize();
01161     if (mtext.size() < msize + 2)
01162       mtext.resize(msize+2);
01163     off_t folder_offset = mi->folderOffset();
01164 
01165     //now we need to find the separator! grr...
01166     for(off_t i = folder_offset-25; true; i -= 20) {
01167       off_t chunk_offset = i <= 0 ? 0 : i;
01168       if(fseek(mStream, chunk_offset, SEEK_SET) == -1) {
01169         rc = errno;
01170         break;
01171       }
01172       if (mtext.size() < 20)
01173         mtext.resize(20);
01174       fread(mtext.data(), 20, 1, mStream);
01175       if(i <= 0) { //woops we've reached the top of the file, last try..
01176         if ( mtext.contains( "from ", false ) ) {
01177           if (mtext.size() < (size_t)folder_offset)
01178               mtext.resize(folder_offset);
01179           if(fseek(mStream, chunk_offset, SEEK_SET) == -1 ||
01180              !fread(mtext.data(), folder_offset, 1, mStream) ||
01181              !fwrite(mtext.data(), folder_offset, 1, tmpfile)) {
01182               rc = errno;
01183               break;
01184           }
01185           offs += folder_offset;
01186         } else {
01187           rc = 666;
01188         }
01189         break;
01190       } else {
01191         int last_crlf = -1;
01192         for(int i2 = 0; i2 < 20; i2++) {
01193           if(*(mtext.data()+i2) == '\n')
01194             last_crlf = i2;
01195         }
01196         if(last_crlf != -1) {
01197           int size = folder_offset - (i + last_crlf+1);
01198           if ((int)mtext.size() < size)
01199               mtext.resize(size);
01200           if(fseek(mStream, i + last_crlf+1, SEEK_SET) == -1 ||
01201              !fread(mtext.data(), size, 1, mStream) ||
01202              !fwrite(mtext.data(), size, 1, tmpfile)) {
01203               rc = errno;
01204               break;
01205           }
01206           offs += size;
01207           break;
01208         }
01209       }
01210     }
01211     if (rc)
01212       break;
01213 
01214     //now actually write the message
01215     if(fseek(mStream, folder_offset, SEEK_SET) == -1 ||
01216        !fread(mtext.data(), msize, 1, mStream) || !fwrite(mtext.data(), msize, 1, tmpfile)) {
01217         rc = errno;
01218         break;
01219     }
01220     mi->setFolderOffset(offs);
01221     offs += msize;
01222   }
01223   done = ( !rc && stopIndex == mMsgList.count() ); // finished without errors
01224   return rc;
01225 }
01226 
01227 //-----------------------------------------------------------------------------
01228 int KMFolderMbox::compact( bool silent )
01229 {
01230   // This is called only when the user explicitely requests compaction,
01231   // so we don't check needsCompact.
01232 
01233   KMail::MboxCompactionJob* job = new KMail::MboxCompactionJob( folder(), true /*immediate*/ );
01234   int rc = job->executeNow( silent );
01235   // Note that job autodeletes itself.
01236 
01237   // If this is the current folder, the changed signal will ultimately call
01238   // KMHeaders::setFolderInfoStatus which will override the message, so save/restore it
01239   QString statusMsg = BroadcastStatus::instance()->statusMsg();
01240   emit changed();
01241   BroadcastStatus::instance()->setStatusMsg( statusMsg );
01242   return rc;
01243 }
01244 
01245 
01246 //-----------------------------------------------------------------------------
01247 void KMFolderMbox::setLockType( LockType ltype )
01248 {
01249   mLockType = ltype;
01250 }
01251 
01252 //-----------------------------------------------------------------------------
01253 void KMFolderMbox::setProcmailLockFileName( const QString &fname )
01254 {
01255   mProcmailLockFileName = fname;
01256 }
01257 
01258 //-----------------------------------------------------------------------------
01259 int KMFolderMbox::removeContents()
01260 {
01261   int rc = 0;
01262   rc = unlink(QFile::encodeName(location()));
01263   return rc;
01264 }
01265 
01266 //-----------------------------------------------------------------------------
01267 int KMFolderMbox::expungeContents()
01268 {
01269   int rc = 0;
01270   if (truncate(QFile::encodeName(location()), 0))
01271     rc = errno;
01272   return rc;
01273 }
01274 
01275 //-----------------------------------------------------------------------------
01276 #include "kmfoldermbox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys