~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kmail/procmailparser.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *   The code here is taken from accountdialog.cpp, which is:
3
 
 *   Copyright (C) 2000 Espen Sand, espe
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
18
 
 *
19
 
 */
20
 
 
21
 
#include "procmailparser.h"
22
 
 
23
 
#include <QDir>
24
 
#include <QTextStream>
25
 
 
26
 
#ifdef HAVE_PATHS_H
27
 
#include <paths.h>  /* defines _PATH_MAILDIR */
28
 
#endif
29
 
 
30
 
#ifndef _PATH_MAILDIR
31
 
#define _PATH_MAILDIR "/var/spool/mail"
32
 
#endif
33
 
 
34
 
using namespace KMail;
35
 
 
36
 
ProcmailRCParser::ProcmailRCParser(const QString &filename)
37
 
  : mProcmailrc(filename),
38
 
    mStream(new QTextStream(&mProcmailrc))
39
 
{
40
 
  // predefined
41
 
  mVars.insert( "HOME", QDir::homePath() );
42
 
 
43
 
  if( filename.isEmpty() ) {
44
 
    mProcmailrc.setFileName(QDir::homePath() + "/.procmailrc");
45
 
  }
46
 
 
47
 
  QRegExp lockFileGlobal("^LOCKFILE=", Qt::CaseSensitive);
48
 
  QRegExp lockFileLocal("^:0", Qt::CaseSensitive);
49
 
 
50
 
  if(  mProcmailrc.open(QIODevice::ReadOnly) ) {
51
 
 
52
 
    QString s;
53
 
 
54
 
    while( !mStream->atEnd() ) {
55
 
 
56
 
      s = mStream->readLine().trimmed();
57
 
 
58
 
      if(  s[0] == '#' ) continue; // skip comments
59
 
 
60
 
      int commentPos = -1;
61
 
 
62
 
      if( (commentPos = s.indexOf('#')) > -1 ) {
63
 
        // get rid of trailing comment
64
 
        s.truncate(commentPos);
65
 
        s = s.trimmed();
66
 
      }
67
 
 
68
 
      if(  lockFileGlobal.indexIn(s) != -1 ) {
69
 
        processGlobalLock(s);
70
 
      } else if( lockFileLocal.indexIn(s) != -1 ) {
71
 
        processLocalLock(s);
72
 
      } else if( int i = s.indexOf('=') ) {
73
 
        processVariableSetting(s,i);
74
 
      }
75
 
    }
76
 
 
77
 
  }
78
 
  QString default_Location = qgetenv("MAIL");
79
 
 
80
 
  if (default_Location.isNull()) {
81
 
    default_Location = _PATH_MAILDIR;
82
 
    default_Location += '/';
83
 
    default_Location += qgetenv("USER");
84
 
  }
85
 
  if ( !mSpoolFiles.contains(default_Location) )
86
 
    mSpoolFiles << default_Location;
87
 
 
88
 
  default_Location = default_Location + ".lock";
89
 
  if ( !mLockFiles.contains(default_Location) )
90
 
    mLockFiles << default_Location;
91
 
}
92
 
 
93
 
ProcmailRCParser::~ProcmailRCParser()
94
 
{
95
 
  delete mStream;
96
 
}
97
 
 
98
 
void
99
 
ProcmailRCParser::processGlobalLock(const QString &s)
100
 
{
101
 
  QString val = expandVars(s.mid(s.indexOf('=') + 1).trimmed());
102
 
  if ( !mLockFiles.contains(val) )
103
 
    mLockFiles << val;
104
 
}
105
 
 
106
 
void
107
 
ProcmailRCParser::processLocalLock(const QString &s)
108
 
{
109
 
  QString val;
110
 
  int colonPos = s.lastIndexOf(':');
111
 
 
112
 
  if (colonPos > 0) { // we don't care about the leading one
113
 
    val = s.mid(colonPos + 1).trimmed();
114
 
 
115
 
    if ( val.length() ) {
116
 
      // user specified a lockfile, so process it
117
 
      //
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
123
 
  }
124
 
 
125
 
  // parse until we find the spoolfile
126
 
  QString line, prevLine;
127
 
  do {
128
 
    prevLine = line;
129
 
    line = mStream->readLine().trimmed();
130
 
  } while ( !mStream->atEnd() &&
131
 
             ( line[0] == '*' ||
132
 
               ( prevLine.length() > 0 && prevLine[prevLine.length() - 1] == '\\' ) ) );
133
 
 
134
 
  if( line[0] != '!' && line[0] != '|' &&  line[0] != '{' ) {
135
 
    // this is a filename, expand it
136
 
    //
137
 
    line =  line.trimmed();
138
 
    line = expandVars(line);
139
 
 
140
 
    // prepend default MAILDIR if needed
141
 
    if( line[0] != '/' && mVars.contains("MAILDIR") )
142
 
      line.insert(0, mVars["MAILDIR"] + '/');
143
 
 
144
 
    // now we have the spoolfile name
145
 
    if ( !mSpoolFiles.contains(line) )
146
 
      mSpoolFiles << line;
147
 
 
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
151
 
      val = line;
152
 
 
153
 
      // append lock extension
154
 
      if( mVars.contains("LOCKEXT") )
155
 
        val += mVars["LOCKEXT"];
156
 
      else
157
 
        val += ".lock";
158
 
    }
159
 
 
160
 
    if ( !val.isNull() && !mLockFiles.contains(val) ) {
161
 
      mLockFiles << val;
162
 
    }
163
 
  }
164
 
 
165
 
}
166
 
 
167
 
void
168
 
ProcmailRCParser::processVariableSetting(const QString &s, int eqPos)
169
 
{
170
 
  if( eqPos == -1) return;
171
 
 
172
 
  QString varName = s.left(eqPos),
173
 
    varValue = expandVars(s.mid(eqPos + 1).trimmed());
174
 
 
175
 
  mVars.insert( varName.toLatin1(), varValue );
176
 
}
177
 
 
178
 
QString
179
 
ProcmailRCParser::expandVars(const QString &s)
180
 
{
181
 
  if( s.isEmpty()) return s;
182
 
 
183
 
  QString expS = s;
184
 
 
185
 
  for ( QHash<QByteArray, QString>::const_iterator it = mVars.constBegin(); it != mVars.constEnd(); ++it ) {
186
 
    expS.replace( QString::fromLatin1("$") + it.key(), it.value() );
187
 
  }
188
 
 
189
 
  return expS;
190
 
}