779a50c9b33342616b84dc55f78b8df05a79f6b7
[quassel.git] / src / core / oidentdconfiggenerator.cpp
1 /***************************************************************************
2  *   Copyright (C) 2012 by the Quassel Project                             *
3  *   devel@quassel-irc.org                                                 *
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) version 3.                                           *
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     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "oidentdconfiggenerator.h"
22
23 OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
24   QObject(parent),
25   _initialized(false)
26 {
27   qDebug() << "OidentdConfigGenerator() checking for being initialized";
28   if (!_initialized)
29     init();
30 }
31
32 bool OidentdConfigGenerator::init() {
33   configDir = QDir::homePath();
34   configFileName = ".oidentd.conf";
35   configTag = " stanza created by Quassel";
36
37   _configFile = new QFile(configDir.absoluteFilePath(configFileName));
38   qDebug() << "1: _configFile" << _configFile->fileName();
39
40   quasselStanza = QRegExp(QString("^lport .* { .* } #%1$").arg(configTag));
41
42   if (update())
43     _initialized = true;
44
45   qDebug() << "konichi wa °-°";
46
47   return _initialized;
48 }
49
50 bool OidentdConfigGenerator::update() {
51   if (parseConfig())
52     qDebug() << "oidentd config parsed successfully";
53   else
54     qDebug() << QString("parsing oidentd config failed (%1 [%2])").arg(_configFile->errorString()).arg(_configFile->error());
55
56   return writeConfig();
57 }
58
59 bool OidentdConfigGenerator::parseConfig() {
60   qDebug() << "_configFile name" << _configFile->fileName();
61   qDebug() << "open?" << _configFile->isOpen();
62   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
63     return false;
64
65   QByteArray parsedConfig;
66   while (!_configFile->atEnd()) {
67     QByteArray line = _configFile->readLine();
68
69     if (checkLine(line))
70       parsedConfig.append(line);
71   }
72
73   _config = parsedConfig;
74
75   return true;
76 }
77
78 bool OidentdConfigGenerator::writeConfig() {
79   return true;
80 }
81
82 bool OidentdConfigGenerator::checkLine(const QByteArray &line) {
83   return !quasselStanza.exactMatch(line);
84 }