properly rewind oidentd config file
[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
36   if(Quassel::isOptionSet("oidentd-conffile"))
37     configPath = Quassel::optionValue("oidentd-conffile");
38   else
39     configPath = configDir.absoluteFilePath(configFileName);
40
41   configTag = " stanza created by Quassel";
42
43   _configFile = new QFile(configPath);
44   qDebug() << "1: _configFile" << _configFile->fileName();
45
46   quasselStanza = QRegExp(QString("^lport .* \\{ .* \\} #%1\\n").arg(configTag));
47
48   _mutex.lock();
49   if (parseConfig(false) && writeConfig())
50     _initialized = true;
51   _mutex.unlock();
52
53   qDebug() << "OidentdConfigGenerator" << (!_initialized ? "not" : "") << "initialized";
54
55   return _initialized;
56 }
57
58 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
59   qDebug() << "localAddress" << localAddress;
60   qDebug() << "localPort" << localPort;
61   qDebug() << "peerAddress" << peerAddress;
62   qDebug() << "peerPort" << peerPort;
63   qDebug() << "ident" << identity->ident();
64
65   QString ident = identity->ident();
66
67   _config.append(QString("lport %1 { reply \"%2\" } #%3\n").arg(localPort).arg(ident).arg(configTag));
68
69   _mutex.lock();
70   bool ret = writeConfig();
71   _mutex.unlock();
72   qDebug() << "config written" << ret;
73
74   return ret;
75 }
76
77 // not yet implemented
78 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
79   Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
80   return true;
81 }
82
83 bool OidentdConfigGenerator::parseConfig(bool keepQuasselStanzas) {
84   qDebug() << "_configFile name" << _configFile->fileName();
85   qDebug() << "open?" << _configFile->isOpen();
86   qDebug() << "keeping our stanzas?" << keepQuasselStanzas;
87   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
88     return false;
89
90   QByteArray parsedConfig;
91   while (!_configFile->atEnd()) {
92     QByteArray line = _configFile->readLine();
93
94     qDebug() << "line" << line;
95     qDebug() << "line by us?" << lineByUs(line);
96     if (keepQuasselStanzas || !lineByUs(line))
97       parsedConfig.append(line);
98   }
99
100   _config = parsedConfig;
101
102   return true;
103 }
104
105 bool OidentdConfigGenerator::writeConfig() {
106   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text | QFile::Truncate))
107     return false;
108
109   _configFile->seek(0);
110   _configFile->resize(0);
111   _configFile->write(_config);
112
113   return _configFile->flush();
114 }
115
116 bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {
117   return quasselStanza.exactMatch(line);
118 }