OidentdConfigGen: cleaning up & comments
[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   // Rx has to match Template in order for cleanup to work.
47   // Template should be enhanced with the "from" parameter as soon as Quassel gains
48   // the ability to bind to an IP on client sockets.
49
50   quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
51   quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(configTag));
52
53   _mutex.lock();
54   // initially remove all Quassel stanzas that might be present
55   if (parseConfig(false) && writeConfig())
56     _initialized = true;
57   _mutex.unlock();
58
59   return _initialized;
60 }
61
62 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
63   Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
64   QString ident = identity->ident();
65
66   _config.append(quasselStanzaTemplate.arg(localPort).arg(ident).arg(configTag));
67
68   _mutex.lock();
69   bool ret = writeConfig();
70   _mutex.unlock();
71
72   return ret;
73 }
74
75 // not yet implemented
76 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
77   Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
78   return true;
79 }
80
81 bool OidentdConfigGenerator::parseConfig(bool keepQuasselStanzas) {
82   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
83     return false;
84
85   QByteArray parsedConfig;
86   while (!_configFile->atEnd()) {
87     QByteArray line = _configFile->readLine();
88
89     if (keepQuasselStanzas || !lineByUs(line))
90       parsedConfig.append(line);
91   }
92
93   _config = parsedConfig;
94
95   return true;
96 }
97
98 bool OidentdConfigGenerator::writeConfig() {
99   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text))
100     return false;
101
102   _configFile->seek(0);
103   _configFile->resize(0);
104   _configFile->write(_config);
105
106   return _configFile->flush();
107 }
108
109 bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {
110   return quasselStanzaRx.exactMatch(line);
111 }