1 /***************************************************************************
2 * Copyright (C) 2012 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "oidentdconfiggenerator.h"
23 OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
27 qDebug() << "OidentdConfigGenerator() checking for being initialized";
32 bool OidentdConfigGenerator::init() {
33 configDir = QDir::homePath();
34 configFileName = ".oidentd.conf";
36 if(Quassel::isOptionSet("oidentd-conffile"))
37 configPath = Quassel::optionValue("oidentd-conffile");
39 configPath = configDir.absoluteFilePath(configFileName);
41 configTag = " stanza created by Quassel";
43 _configFile = new QFile(configPath);
44 qDebug() << "1: _configFile" << _configFile->fileName();
46 quasselStanza = QRegExp(QString("^lport .* { .* } #%1$").arg(configTag));
48 if (parseConfig(false) && writeConfig())
51 qDebug() << "OidentdConfigGenerator" << (!_initialized ? "not" : "") << "initialized";
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();
63 QString ident = identity->ident();
65 _config.append(QString("lport %1 { reply \"%2\" } #%3\n").arg(localPort).arg(ident).arg(configTag));
67 bool ret = writeConfig();
68 qDebug() << "config written" << ret;
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)
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))
86 QByteArray parsedConfig;
87 while (!_configFile->atEnd()) {
88 QByteArray line = _configFile->readLine();
90 qDebug() << "line" << line;
91 qDebug() << "line by us?" << lineByUs(line);
92 if (keepQuasselStanzas || !lineByUs(line))
93 parsedConfig.append(line);
96 _config = parsedConfig;
101 bool OidentdConfigGenerator::writeConfig() {
102 if (!_configFile->isOpen() && !_configFile->open(QFile::WriteOnly | QFile::Truncate))
105 //FIXME: thread safety
106 QTextStream out(_configFile);
109 return _configFile->flush();
112 bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {
113 return !quasselStanza.exactMatch(line);