init();
}
+OidentdConfigGenerator::~OidentdConfigGenerator() {
+ _quasselConfig.clear();
+ writeConfig();
+}
+
bool OidentdConfigGenerator::init() {
configDir = QDir::homePath();
configFileName = ".oidentd.conf";
quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(configTag));
- _mutex.lock();
// initially remove all Quassel stanzas that might be present
if (parseConfig(false) && writeConfig())
_initialized = true;
- _mutex.unlock();
return _initialized;
}
Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
QString ident = identity->ident();
- _config.append(quasselStanzaTemplate.arg(localPort).arg(ident).arg(configTag));
+ _quasselConfig.append(quasselStanzaTemplate.arg(localPort).arg(ident).arg(configTag));
- _mutex.lock();
bool ret = writeConfig();
- _mutex.unlock();
return ret;
}
-// not yet implemented
+//! 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))
+bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas) {
+ if (!_configFile->exists())
+ return true;
+
+ if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly))
return false;
+ _mutex.lock();
- QByteArray parsedConfig;
+ _parsedConfig.clear();
+ _configFile->seek(0);
while (!_configFile->atEnd()) {
QByteArray line = _configFile->readLine();
- if (keepQuasselStanzas || !lineByUs(line))
- parsedConfig.append(line);
+ if (!lineByUs(line))
+ _parsedConfig.append(line);
+ else if (readQuasselStanzas)
+ _quasselConfig.append(line);
}
- _config = parsedConfig;
-
+ _configFile->close();
+ _mutex.unlock();
return true;
}
bool OidentdConfigGenerator::writeConfig() {
- if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text))
+ mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r--
+ bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text));
+ umask(prev_umask);
+
+ if (not_open)
return false;
+ _mutex.lock();
+
_configFile->seek(0);
_configFile->resize(0);
- _configFile->write(_config);
+ _configFile->write(_parsedConfig);
+ _configFile->write(_quasselConfig);
- return _configFile->flush();
+ _configFile->close();
+ _mutex.unlock();
+ return true;
}
bool OidentdConfigGenerator::lineByUs(const QByteArray &line) {