X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcore%2Foidentdconfiggenerator.cpp;h=06ff272f0c99858399de00db86fc8481b24562fc;hb=694f9bfbf7f1af19108461c7e00d133e55082bce;hp=779a50c9b33342616b84dc55f78b8df05a79f6b7;hpb=03eb5a574ec55546c62336428c7a9caa63b45a7a;p=quassel.git diff --git a/src/core/oidentdconfiggenerator.cpp b/src/core/oidentdconfiggenerator.cpp index 779a50c9..06ff272f 100644 --- a/src/core/oidentdconfiggenerator.cpp +++ b/src/core/oidentdconfiggenerator.cpp @@ -21,64 +21,125 @@ #include "oidentdconfiggenerator.h" OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) : - QObject(parent), - _initialized(false) + QObject(parent), + _initialized(false) { - qDebug() << "OidentdConfigGenerator() checking for being initialized"; - if (!_initialized) - init(); + if (!_initialized) + init(); } -bool OidentdConfigGenerator::init() { - configDir = QDir::homePath(); - configFileName = ".oidentd.conf"; - configTag = " stanza created by Quassel"; - _configFile = new QFile(configDir.absoluteFilePath(configFileName)); - qDebug() << "1: _configFile" << _configFile->fileName(); +OidentdConfigGenerator::~OidentdConfigGenerator() +{ + _quasselConfig.clear(); + writeConfig(); + _configFile->deleteLater(); +} - quasselStanza = QRegExp(QString("^lport .* { .* } #%1$").arg(configTag)); - if (update()) - _initialized = true; +bool OidentdConfigGenerator::init() +{ + _configDir = QDir::homePath(); + _configFileName = ".oidentd.conf"; - qDebug() << "konichi wa °-°"; + if (Quassel::isOptionSet("oidentd-conffile")) + _configPath = Quassel::optionValue("oidentd-conffile"); + else + _configPath = _configDir.absoluteFilePath(_configFileName); - return _initialized; -} + _configTag = " stanza created by Quassel"; + + _configFile = new QFile(_configPath); -bool OidentdConfigGenerator::update() { - if (parseConfig()) - qDebug() << "oidentd config parsed successfully"; - else - qDebug() << QString("parsing oidentd config failed (%1 [%2])").arg(_configFile->errorString()).arg(_configFile->error()); + // Rx has to match Template in order for cleanup to work. + // Template should be enhanced with the "from" parameter as soon as Quassel gains + // the ability to bind to an IP on client sockets. - return writeConfig(); + _quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n"); + _quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(_configTag)); + + // initially remove all Quassel stanzas that might be present + if (parseConfig(false) && writeConfig()) + _initialized = true; + + return _initialized; } -bool OidentdConfigGenerator::parseConfig() { - qDebug() << "_configFile name" << _configFile->fileName(); - qDebug() << "open?" << _configFile->isOpen(); - if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite)) - return false; - QByteArray parsedConfig; - while (!_configFile->atEnd()) { - QByteArray line = _configFile->readLine(); +bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) +{ + Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort) + QString ident = identity->ident(); + + _quasselConfig.append(_quasselStanzaTemplate.arg(localPort).arg(ident).arg(_configTag).toAscii()); + + bool ret = writeConfig(); - if (checkLine(line)) - parsedConfig.append(line); - } + return ret; +} + + +//! not yet implemented +bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) +{ + Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort) + return true; +} - _config = parsedConfig; - return true; +bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas) +{ + if (!_configFile->exists()) + return true; + + if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly)) + return false; + _mutex.lock(); + + _parsedConfig.clear(); + _configFile->seek(0); + while (!_configFile->atEnd()) { + QByteArray line = _configFile->readLine(); + + if (!lineByUs(line)) + _parsedConfig.append(line); + else if (readQuasselStanzas) + _quasselConfig.append(line); + } + + _configFile->close(); + _mutex.unlock(); + return true; } -bool OidentdConfigGenerator::writeConfig() { - return true; + +bool OidentdConfigGenerator::writeConfig() +{ +#ifdef HAVE_UMASK + mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r-- +#endif + bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text)); +#ifdef HAVE_UMASK + umask(prev_umask); +#endif + + if (not_open) + return false; + + _mutex.lock(); + + _configFile->seek(0); + _configFile->resize(0); + _configFile->write(_parsedConfig); + _configFile->write(_quasselConfig); + + _configFile->close(); + _mutex.unlock(); + return true; } -bool OidentdConfigGenerator::checkLine(const QByteArray &line) { - return !quasselStanza.exactMatch(line); + +bool OidentdConfigGenerator::lineByUs(const QByteArray &line) +{ + return _quasselStanzaRx.exactMatch(line); }