QObject(parent),
_initialized(false)
{
- qDebug() << "OidentdConfigGenerator() checking for being initialized";
if (!_initialized)
init();
}
configTag = " stanza created by Quassel";
_configFile = new QFile(configPath);
- qDebug() << "1: _configFile" << _configFile->fileName();
- quasselStanza = QRegExp(QString("^lport .* { .* } #%1$").arg(configTag));
+ // 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.
- if (parseConfig(true) && writeConfig())
- _initialized = true;
+ quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
+ quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(configTag));
- qDebug() << "konichi wa °-°";
+ _mutex.lock();
+ // initially remove all Quassel stanzas that might be present
+ if (parseConfig(false) && writeConfig())
+ _initialized = true;
+ _mutex.unlock();
return _initialized;
}
bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
- qDebug() << "localAddress" << localAddress;
- qDebug() << "localPort" << localPort;
- qDebug() << "peerAddress" << peerAddress;
- qDebug() << "peerPort" << peerPort;
- qDebug() << "ident" << identity->ident();
-
+ Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
QString ident = identity->ident();
- _config.append(QString("lport %1 { reply \"%2\" } #%3\n").arg(localPort).arg(ident).arg(configTag));
+ _config.append(quasselStanzaTemplate.arg(localPort).arg(ident).arg(configTag));
+
+ _mutex.lock();
+ bool ret = writeConfig();
+ _mutex.unlock();
- return writeConfig();
+ return ret;
}
-bool OidentdConfigGenerator::parseConfig(bool stripQuasselStanzas) {
- qDebug() << "_configFile name" << _configFile->fileName();
- qDebug() << "open?" << _configFile->isOpen();
+// 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;
+}
+
+bool OidentdConfigGenerator::parseConfig(bool keepQuasselStanzas) {
if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
return false;
while (!_configFile->atEnd()) {
QByteArray line = _configFile->readLine();
- if (!stripQuasselStanzas || checkLine(line))
+ if (keepQuasselStanzas || !lineByUs(line))
parsedConfig.append(line);
}
}
bool OidentdConfigGenerator::writeConfig() {
- if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
+ if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text))
return false;
- //FIXME: thread safety
- QTextStream out(_configFile);
- out << _config;
+ _configFile->seek(0);
+ _configFile->resize(0);
+ _configFile->write(_config);
return _configFile->flush();
}
-bool OidentdConfigGenerator::checkLine(const QByteArray &line) {
- return !quasselStanza.exactMatch(line);
+bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {
+ return quasselStanzaRx.exactMatch(line);
}