36
37
#include <kstandarddirs.h>
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]" ) );
41
42
/// private static methods.
43
static bool isMBoxSeparator( QRegExp &matcher, const QByteArray &line )
45
if ( !line.startsWith( "From " ) ) {
48
return matcher.indexIn( QString::fromLatin1( line ) ) >= 0;
44
51
/// public methods.
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 ) ) ) {
168
175
// Found the separator or at end of file, the message starts at offs
229
236
switch(d->mLockType)
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 ) );
236
args << QFile::encodeName( d->mMboxFile.fileName() + ".lock" );
243
args << QString::fromLocal8Bit( QFile::encodeName( d->mMboxFile.fileName() + QLatin1String( ".lock" ) ) );
238
rc = QProcess::execute("lockfile", args);
245
rc = QProcess::execute( QLatin1String( "lockfile" ), args);
240
247
kDebug() << "lockfile -l20 -r5 " << d->mMboxFile.fileName()
241
248
<< ": Failed ("<< rc << ") switching to read only mode";
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 );
254
261
kDebug() << "mutt_dotlock " << d->mMboxFile.fileName()
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 );
268
276
kDebug() << "mutt_dotlock -p " << d->mMboxFile.fileName() << ":"
318
326
QByteArray line = d->mMboxFile.readLine();
319
327
QRegExp regexp( sMBoxSeperatorRegExp );
321
if ( regexp.indexIn(line) < 0 ) {
329
if ( !isMBoxSeparator( regexp, line ) ) {
322
330
qDebug() << "Found invalid separator at:" << offset;
324
332
return false; // The file is messed up or the index is incorrect.
409
417
Q_ASSERT( d->mFileLocked );
410
418
Q_ASSERT( d->mMboxFile.isOpen() );
411
Q_ASSERT( d->mMboxFile.size() > 0 );
413
if ( offset > static_cast<quint64>( d->mMboxFile.size() ) ) {
419
d->mMboxFile.seek(offset);
421
QByteArray line = d->mMboxFile.readLine();
422
QRegExp regexp( sMBoxSeperatorRegExp );
424
if ( regexp.indexIn( line ) < 0) {
425
kDebug() << "[MBox::readEntry] Invalid entry at:" << offset;
428
return 0; // The file is messed up or the index is incorrect.
419
Q_ASSERT( ( d->mInitialMboxFileSize + d->mAppendedEntries.size() ) > offset );
431
421
QByteArray message;
432
line = d->mMboxFile.readLine();
433
while ( regexp.indexIn( line ) < 0 && !d->mMboxFile.atEnd() ) {
423
if ( offset < d->mInitialMboxFileSize ) {
424
d->mMboxFile.seek(offset);
426
QByteArray line = d->mMboxFile.readLine();
427
QRegExp regexp( sMBoxSeperatorRegExp );
429
if ( !isMBoxSeparator( regexp, line ) ) {
430
kDebug() << "[MBox::readEntry] Invalid entry at:" << offset;
433
return 0; // The file is messed up or the index is incorrect.
435
436
line = d->mMboxFile.readLine();
437
while ( !isMBoxSeparator( regexp, line ) && !d->mMboxFile.atEnd() ) {
439
line = d->mMboxFile.readLine();
442
offset -= d->mInitialMboxFileSize;
443
if ( offset > static_cast<quint64>( d->mAppendedEntries.size() ) ) {
449
QBuffer buffer( &(d->mAppendedEntries) );
450
buffer.open( QIODevice::ReadOnly );
451
buffer.seek( offset );
453
QByteArray line = buffer.readLine();
454
QRegExp regexp( sMBoxSeperatorRegExp );
456
if ( !isMBoxSeparator( regexp, line ) ) {
457
kDebug() << "[MBox::readEntry] Invalid appended entry at:" << offset;
460
return 0; // The file is messed up or the index is incorrect.
463
line = buffer.readLine();
464
while ( !isMBoxSeparator( regexp, line ) && !buffer.atEnd() ) {
466
line = buffer.readLine();
438
470
// Remove te last '\n' added by writeEntry.
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 );
469
d->mMboxFile.seek( offset );
470
501
QByteArray headers;
471
QByteArray line = d->mMboxFile.readLine();
473
while ( !line[0] == '\n' ) {
475
line = d->mMboxFile.readLine();
502
if ( offset < d->mInitialMboxFileSize ) {
503
d->mMboxFile.seek( offset );
504
QByteArray line = d->mMboxFile.readLine();
506
while ( line[0] != '\n' ) {
508
line = d->mMboxFile.readLine();
511
QBuffer buffer( &(d->mAppendedEntries) );
512
buffer.open( QIODevice::ReadOnly );
513
buffer.seek( offset - d->mInitialMboxFileSize );
514
QByteArray line = buffer.readLine();
516
while ( line[0] != '\n' ) {
518
line = buffer.readLine();
478
522
if ( ! wasLocked )
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";
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";
576
620
if ( !d->mLockFileName.isEmpty() )
577
621
rc = !QFile( d->mLockFileName ).remove();
579
rc = !QFile( d->mMboxFile.fileName() + ".lock" ).remove();
623
rc = !QFile( d->mMboxFile.fileName() + QLatin1String( ".lock" ) ).remove();
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 );
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 );
592
638
case None: // Fall through.