~ubuntu-branches/ubuntu/lucid/kdepim-runtime/lucid-updates

« back to all changes in this revision

Viewing changes to resources/mbox/libmbox/mbox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2010-01-06 18:57:08 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20100106185708-njxieh19nqhom3nd
Tags: 4:4.3.90-0ubuntu1
* New upstream beta release:
  - Bump build-depend versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <fcntl.h>
30
30
 
 
31
#include <QtCore/QBuffer>
31
32
#include <QtCore/QProcess>
32
33
 
33
34
#include <kdebug.h>
36
37
#include <kstandarddirs.h>
37
38
#include <kurl.h>
38
39
 
39
 
static QString sMBoxSeperatorRegExp( "^From .*[0-9][0-9]:[0-9][0-9]" );
 
40
static QString sMBoxSeperatorRegExp( QLatin1String( "^From .*[0-9][0-9]:[0-9][0-9]" ) );
40
41
 
41
42
/// private static methods.
42
 
 
 
43
static bool isMBoxSeparator( QRegExp &matcher, const QByteArray &line )
 
44
{
 
45
  if ( !line.startsWith( "From " ) ) {
 
46
    return false;
 
47
  }
 
48
  return matcher.indexIn( QString::fromLatin1( line ) ) >= 0;
 
49
}
43
50
 
44
51
/// public methods.
45
52
 
162
169
 
163
170
    // if atEnd, use mail only if there was a separator line at all,
164
171
    // otherwise it's not a valid mbox
165
 
    if ( regexp.indexIn( line ) >= 0 ||
 
172
    if ( isMBoxSeparator( regexp, line ) ||
166
173
         ( d->mMboxFile.atEnd() && ( prevSeparator.size() != 0 ) ) ) {
167
174
 
168
175
      // Found the separator or at end of file, the message starts at offs
190
197
        d->mEntries << info;
191
198
      }
192
199
 
193
 
      if ( regexp.indexIn( line ) >= 0 )
 
200
      if ( isMBoxSeparator( regexp, line ) )
194
201
        prevSeparator = line;
195
202
 
196
203
      offs += msgSize; // Mark the beginning of the next message.
229
236
  switch(d->mLockType)
230
237
  {
231
238
    case ProcmailLockfile:
232
 
      args << "-l20" << "-r5";
 
239
      args << QLatin1String( "-l20" ) << QLatin1String( "-r5" );
233
240
      if ( !d->mLockFileName.isEmpty() )
234
 
        args << QFile::encodeName( d->mLockFileName );
 
241
        args << QString::fromLocal8Bit( QFile::encodeName( d->mLockFileName ) );
235
242
      else
236
 
        args << QFile::encodeName( d->mMboxFile.fileName() + ".lock" );
 
243
        args << QString::fromLocal8Bit( QFile::encodeName( d->mMboxFile.fileName() + QLatin1String( ".lock" ) ) );
237
244
 
238
 
      rc = QProcess::execute("lockfile", args);
 
245
      rc = QProcess::execute( QLatin1String( "lockfile" ), args);
239
246
      if( rc != 0 ) {
240
247
        kDebug() << "lockfile -l20 -r5 " << d->mMboxFile.fileName()
241
248
                 << ": Failed ("<< rc << ") switching to read only mode";
247
254
      break;
248
255
 
249
256
    case MuttDotlock:
250
 
      args << QFile::encodeName( d->mMboxFile.fileName() );
251
 
      rc = QProcess::execute( "mutt_dotlock", args );
 
257
      args << QString::fromLocal8Bit( QFile::encodeName( d->mMboxFile.fileName() ) );
 
258
      rc = QProcess::execute( QLatin1String( "mutt_dotlock" ), args );
252
259
 
253
260
      if( rc != 0 ) {
254
261
        kDebug() << "mutt_dotlock " << d->mMboxFile.fileName()
261
268
      break;
262
269
 
263
270
    case MuttDotlockPrivileged:
264
 
      args << "-p" << QFile::encodeName( d->mMboxFile.fileName() );
265
 
      rc = QProcess::execute( "mutt_dotlock", args );
 
271
      args << QLatin1String( "-p" )
 
272
           << QString::fromLocal8Bit( QFile::encodeName( d->mMboxFile.fileName() ) );
 
273
      rc = QProcess::execute( QLatin1String( "mutt_dotlock" ), args );
266
274
 
267
275
      if( rc != 0 ) {
268
276
        kDebug() << "mutt_dotlock -p " << d->mMboxFile.fileName() << ":"
318
326
    QByteArray line = d->mMboxFile.readLine();
319
327
    QRegExp regexp( sMBoxSeperatorRegExp );
320
328
 
321
 
    if ( regexp.indexIn(line) < 0 ) {
 
329
    if ( !isMBoxSeparator( regexp, line ) ) {
322
330
      qDebug() << "Found invalid separator at:" << offset;
323
331
      unlock();
324
332
      return false; // The file is messed up or the index is incorrect.
408
416
 
409
417
  Q_ASSERT( d->mFileLocked );
410
418
  Q_ASSERT( d->mMboxFile.isOpen() );
411
 
  Q_ASSERT( d->mMboxFile.size() > 0 );
412
 
 
413
 
  if ( offset > static_cast<quint64>( d->mMboxFile.size() ) ) {
414
 
    if ( !wasLocked )
415
 
      unlock();
416
 
    return 0;
417
 
  }
418
 
 
419
 
  d->mMboxFile.seek(offset);
420
 
 
421
 
  QByteArray line = d->mMboxFile.readLine();
422
 
  QRegExp regexp( sMBoxSeperatorRegExp );
423
 
 
424
 
  if ( regexp.indexIn( line ) < 0) {
425
 
    kDebug() << "[MBox::readEntry] Invalid entry at:" << offset;
426
 
    if ( !wasLocked )
427
 
      unlock();
428
 
    return 0; // The file is messed up or the index is incorrect.
429
 
  }
 
419
  Q_ASSERT( ( d->mInitialMboxFileSize + d->mAppendedEntries.size() ) > offset );
430
420
 
431
421
  QByteArray message;
432
 
  line = d->mMboxFile.readLine();
433
 
  while ( regexp.indexIn( line ) < 0 && !d->mMboxFile.atEnd() ) {
434
 
    message += line;
 
422
 
 
423
  if ( offset < d->mInitialMboxFileSize ) {
 
424
    d->mMboxFile.seek(offset);
 
425
 
 
426
    QByteArray line = d->mMboxFile.readLine();
 
427
    QRegExp regexp( sMBoxSeperatorRegExp );
 
428
 
 
429
    if ( !isMBoxSeparator( regexp, line ) ) {
 
430
      kDebug() << "[MBox::readEntry] Invalid entry at:" << offset;
 
431
      if ( !wasLocked )
 
432
        unlock();
 
433
      return 0; // The file is messed up or the index is incorrect.
 
434
    }
 
435
 
435
436
    line = d->mMboxFile.readLine();
 
437
    while ( !isMBoxSeparator( regexp, line ) && !d->mMboxFile.atEnd() ) {
 
438
      message += line;
 
439
      line = d->mMboxFile.readLine();
 
440
    }
 
441
  } else {
 
442
    offset -= d->mInitialMboxFileSize;
 
443
    if ( offset > static_cast<quint64>( d->mAppendedEntries.size() ) ) {
 
444
      if ( !wasLocked )
 
445
        unlock();
 
446
      return 0;
 
447
    }
 
448
 
 
449
    QBuffer buffer( &(d->mAppendedEntries) );
 
450
    buffer.open( QIODevice::ReadOnly );
 
451
    buffer.seek( offset );
 
452
 
 
453
    QByteArray line = buffer.readLine();
 
454
    QRegExp regexp( sMBoxSeperatorRegExp );
 
455
 
 
456
    if ( !isMBoxSeparator( regexp, line ) ) {
 
457
      kDebug() << "[MBox::readEntry] Invalid appended entry at:" << offset;
 
458
      if ( !wasLocked )
 
459
        unlock();
 
460
      return 0; // The file is messed up or the index is incorrect.
 
461
    }
 
462
 
 
463
    line = buffer.readLine();
 
464
    while ( !isMBoxSeparator( regexp, line ) && !buffer.atEnd() ) {
 
465
      message += line;
 
466
      line = buffer.readLine();
 
467
    }
436
468
  }
437
469
 
438
470
  // Remove te last '\n' added by writeEntry.
464
496
 
465
497
  Q_ASSERT( d->mFileLocked );
466
498
  Q_ASSERT( d->mMboxFile.isOpen() );
467
 
  Q_ASSERT( static_cast<quint64>(d->mMboxFile.size()) > offset );
 
499
  Q_ASSERT( ( d->mInitialMboxFileSize + d->mAppendedEntries.size() ) > offset );
468
500
 
469
 
  d->mMboxFile.seek( offset );
470
501
  QByteArray headers;
471
 
  QByteArray line = d->mMboxFile.readLine();
472
 
 
473
 
  while ( !line[0] == '\n' ) {
474
 
    headers += line;
475
 
    line = d->mMboxFile.readLine();
 
502
  if ( offset < d->mInitialMboxFileSize ) {
 
503
    d->mMboxFile.seek( offset );
 
504
    QByteArray line = d->mMboxFile.readLine();
 
505
 
 
506
    while ( line[0] != '\n' ) {
 
507
        headers += line;
 
508
        line = d->mMboxFile.readLine();
 
509
    }
 
510
  } else {
 
511
    QBuffer buffer( &(d->mAppendedEntries) );
 
512
    buffer.open( QIODevice::ReadOnly );
 
513
    buffer.seek( offset - d->mInitialMboxFileSize );
 
514
    QByteArray line = buffer.readLine();
 
515
 
 
516
    while ( line[0] != '\n' ) {
 
517
        headers += line;
 
518
        line = buffer.readLine();
 
519
    }
476
520
  }
477
521
 
478
522
  if ( ! wasLocked )
528
572
 
529
573
  switch ( ltype ) {
530
574
    case ProcmailLockfile:
531
 
      if ( KStandardDirs::findExe( "lockfile" ).isEmpty() ) {
 
575
      if ( KStandardDirs::findExe( QLatin1String( "lockfile" ) ).isEmpty() ) {
532
576
        kDebug() << "Could not find the lockfile executable";
533
577
        return false;
534
578
      }
535
579
      break;
536
580
    case MuttDotlock: // fall through
537
581
    case MuttDotlockPrivileged:
538
 
      if ( KStandardDirs::findExe("mutt_dotlock").isEmpty() ) {
 
582
      if ( KStandardDirs::findExe( QLatin1String( "mutt_dotlock" ) ).isEmpty() ) {
539
583
        kDebug() << "Could not find the mutt_dotlock executable";
540
584
        return false;
541
585
      }
576
620
      if ( !d->mLockFileName.isEmpty() )
577
621
        rc = !QFile( d->mLockFileName ).remove();
578
622
      else
579
 
        rc = !QFile( d->mMboxFile.fileName() + ".lock" ).remove();
 
623
        rc = !QFile( d->mMboxFile.fileName() + QLatin1String( ".lock" ) ).remove();
580
624
      break;
581
625
 
582
626
    case MuttDotlock:
583
 
      args << "-u" << QFile::encodeName( d->mMboxFile.fileName() );
584
 
      rc = QProcess::execute( "mutt_dotlock", args );
 
627
      args << QLatin1String( "-u" )
 
628
           << QString::fromLocal8Bit( QFile::encodeName( d->mMboxFile.fileName() ) );
 
629
      rc = QProcess::execute( QLatin1String( "mutt_dotlock" ), args );
585
630
      break;
586
631
 
587
632
    case MuttDotlockPrivileged:
588
 
      args << "-u" << "-p" << QFile::encodeName( d->mMboxFile.fileName() );
589
 
      rc = QProcess::execute( "mutt_dotlock", args );
 
633
      args << QLatin1String( "-u" ) << QLatin1String( "-p" )
 
634
           << QString::fromLocal8Bit( QFile::encodeName( d->mMboxFile.fileName() ) );
 
635
      rc = QProcess::execute( QLatin1String( "mutt_dotlock" ), args );
590
636
      break;
591
637
 
592
638
    case None: // Fall through.