072b15956b283b1f2dd68ab70a1b06568a8a24d0
[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$").arg(configTag));
47
48   if (parseConfig(false) && writeConfig())
49     _initialized = true;
50
51   qDebug() << "OidentdConfigGenerator" << (!_initialized ? "not" : "") << "initialized";
52
53   return _initialized;
54 }
55
56 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
57   qDebug() << "localAddress" << localAddress;
58   qDebug() << "localPort" << localPort;
59   qDebug() << "peerAddress" << peerAddress;
60   qDebug() << "peerPort" << peerPort;
61   qDebug() << "ident" << identity->ident();
62
63   QString ident = identity->ident();
64
65   _config.append(QString("lport %1 { reply \"%2\" } #%3\n").arg(localPort).arg(ident).arg(configTag));
66
67   bool ret = writeConfig();
68   qDebug() << "config written" << ret;
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   qDebug() << "_configFile name" << _configFile->fileName();
81   qDebug() << "open?" << _configFile->isOpen();
82   qDebug() << "keeping our stanzas?" << keepQuasselStanzas;
83   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
84     return false;
85
86   QByteArray parsedConfig;
87   while (!_configFile->atEnd()) {
88     QByteArray line = _configFile->readLine();
89
90     qDebug() << "line" << line;
91     qDebug() << "line by us?" << lineByUs(line);
92     if (keepQuasselStanzas || !lineByUs(line))
93       parsedConfig.append(line);
94   }
95
96   _config = parsedConfig;
97
98   return true;
99 }
100
101 bool OidentdConfigGenerator::writeConfig() {
102   if (!_configFile->isOpen() && !_configFile->open(QFile::WriteOnly | QFile::Truncate))
103     return false;
104
105   //FIXME: thread safety
106   QTextStream out(_configFile);
107   out << _config;
108
109   return _configFile->flush();
110 }
111
112 bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {
113   return !quasselStanza.exactMatch(line);
114 }