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