2
* The code here is taken from accountdialog.cpp, which is:
3
* Copyright (C) 2000 Espen Sand, espe
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include "procmailparser.h"
24
#include <QTextStream>
27
#include <paths.h> /* defines _PATH_MAILDIR */
31
#define _PATH_MAILDIR "/var/spool/mail"
34
using namespace KMail;
36
ProcmailRCParser::ProcmailRCParser(const QString &filename)
37
: mProcmailrc(filename),
38
mStream(new QTextStream(&mProcmailrc))
41
mVars.insert( "HOME", QDir::homePath() );
43
if( filename.isEmpty() ) {
44
mProcmailrc.setFileName(QDir::homePath() + "/.procmailrc");
47
QRegExp lockFileGlobal("^LOCKFILE=", Qt::CaseSensitive);
48
QRegExp lockFileLocal("^:0", Qt::CaseSensitive);
50
if( mProcmailrc.open(QIODevice::ReadOnly) ) {
54
while( !mStream->atEnd() ) {
56
s = mStream->readLine().trimmed();
58
if( s[0] == '#' ) continue; // skip comments
62
if( (commentPos = s.indexOf('#')) > -1 ) {
63
// get rid of trailing comment
64
s.truncate(commentPos);
68
if( lockFileGlobal.indexIn(s) != -1 ) {
70
} else if( lockFileLocal.indexIn(s) != -1 ) {
72
} else if( int i = s.indexOf('=') ) {
73
processVariableSetting(s,i);
78
QString default_Location = qgetenv("MAIL");
80
if (default_Location.isNull()) {
81
default_Location = _PATH_MAILDIR;
82
default_Location += '/';
83
default_Location += qgetenv("USER");
85
if ( !mSpoolFiles.contains(default_Location) )
86
mSpoolFiles << default_Location;
88
default_Location = default_Location + ".lock";
89
if ( !mLockFiles.contains(default_Location) )
90
mLockFiles << default_Location;
93
ProcmailRCParser::~ProcmailRCParser()
99
ProcmailRCParser::processGlobalLock(const QString &s)
101
QString val = expandVars(s.mid(s.indexOf('=') + 1).trimmed());
102
if ( !mLockFiles.contains(val) )
107
ProcmailRCParser::processLocalLock(const QString &s)
110
int colonPos = s.lastIndexOf(':');
112
if (colonPos > 0) { // we don't care about the leading one
113
val = s.mid(colonPos + 1).trimmed();
115
if ( val.length() ) {
116
// user specified a lockfile, so process it
118
val = expandVars(val);
119
if ( val[0] != '/' && mVars.contains("MAILDIR") )
120
val.insert(0, mVars["MAILDIR"] + '/');
121
} // else we'll deduce the lockfile name one we
122
// get the spoolfile name
125
// parse until we find the spoolfile
126
QString line, prevLine;
129
line = mStream->readLine().trimmed();
130
} while ( !mStream->atEnd() &&
132
( prevLine.length() > 0 && prevLine[prevLine.length() - 1] == '\\' ) ) );
134
if( line[0] != '!' && line[0] != '|' && line[0] != '{' ) {
135
// this is a filename, expand it
137
line = line.trimmed();
138
line = expandVars(line);
140
// prepend default MAILDIR if needed
141
if( line[0] != '/' && mVars.contains("MAILDIR") )
142
line.insert(0, mVars["MAILDIR"] + '/');
144
// now we have the spoolfile name
145
if ( !mSpoolFiles.contains(line) )
148
if( colonPos > 0 && val.isEmpty() ) {
149
// there is a local lockfile, but the user didn't
150
// specify the name so compute it from the spoolfile's name
153
// append lock extension
154
if( mVars.contains("LOCKEXT") )
155
val += mVars["LOCKEXT"];
160
if ( !val.isNull() && !mLockFiles.contains(val) ) {
168
ProcmailRCParser::processVariableSetting(const QString &s, int eqPos)
170
if( eqPos == -1) return;
172
QString varName = s.left(eqPos),
173
varValue = expandVars(s.mid(eqPos + 1).trimmed());
175
mVars.insert( varName.toLatin1(), varValue );
179
ProcmailRCParser::expandVars(const QString &s)
181
if( s.isEmpty()) return s;
185
for ( QHash<QByteArray, QString>::const_iterator it = mVars.constBegin(); it != mVars.constEnd(); ++it ) {
186
expS.replace( QString::fromLatin1("$") + it.key(), it.value() );