oidentd config parsing + implicit mutexes
[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   if (!_initialized)
28     init();
29 }
30
31 OidentdConfigGenerator::~OidentdConfigGenerator() {
32   _quasselConfig.clear();
33   writeConfig();
34 }
35
36 bool OidentdConfigGenerator::init() {
37   configDir = QDir::homePath();
38   configFileName = ".oidentd.conf";
39
40   if(Quassel::isOptionSet("oidentd-conffile"))
41     configPath = Quassel::optionValue("oidentd-conffile");
42   else
43     configPath = configDir.absoluteFilePath(configFileName);
44
45   configTag = " stanza created by Quassel";
46
47   _configFile = new QFile(configPath);
48
49   // Rx has to match Template in order for cleanup to work.
50   // Template should be enhanced with the "from" parameter as soon as Quassel gains
51   // the ability to bind to an IP on client sockets.
52
53   quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
54   quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(configTag));
55
56   // initially remove all Quassel stanzas that might be present
57   if (parseConfig(false) && writeConfig())
58     _initialized = true;
59
60   return _initialized;
61 }
62
63 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
64   Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
65   QString ident = identity->ident();
66
67   _quasselConfig.append(quasselStanzaTemplate.arg(localPort).arg(ident).arg(configTag));
68
69   bool ret = writeConfig();
70
71   return ret;
72 }
73
74 // not yet implemented
75 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
76   Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
77   return true;
78 }
79
80 bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas) {
81   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
82     return false;
83   _mutex.lock();
84
85   _parsedConfig.clear();
86   _configFile->seek(0);
87   while (!_configFile->atEnd()) {
88     QByteArray line = _configFile->readLine();
89
90     if (!lineByUs(line))
91       _parsedConfig.append(line);
92     else if (readQuasselStanzas)
93       _quasselConfig.append(line);
94   }
95
96   _configFile->close();
97   _mutex.unlock();
98   return true;
99 }
100
101 bool OidentdConfigGenerator::writeConfig() {
102   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text))
103     return false;
104   _mutex.lock();
105
106   _configFile->seek(0);
107   _configFile->resize(0);
108   _configFile->write(_parsedConfig);
109   _configFile->write(_quasselConfig);
110
111   _configFile->close();
112   _mutex.unlock();
113   return true;
114 }
115
116 bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {
117   return quasselStanzaRx.exactMatch(line);
118 }