OidentdConfigGen: remove qDebug()s
[quassel.git] / src / core / oidentdconfiggenerator.cpp
index 779a50c..877981c 100644 (file)
@@ -24,7 +24,6 @@ OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
   QObject(parent),
   _initialized(false)
 {
-  qDebug() << "OidentdConfigGenerator() checking for being initialized";
   if (!_initialized)
     init();
 }
@@ -32,33 +31,52 @@ OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
 bool OidentdConfigGenerator::init() {
   configDir = QDir::homePath();
   configFileName = ".oidentd.conf";
+
+  if(Quassel::isOptionSet("oidentd-conffile"))
+    configPath = Quassel::optionValue("oidentd-conffile");
+  else
+    configPath = configDir.absoluteFilePath(configFileName);
+
   configTag = " stanza created by Quassel";
 
-  _configFile = new QFile(configDir.absoluteFilePath(configFileName));
-  qDebug() << "1: _configFile" << _configFile->fileName();
+  _configFile = new QFile(configPath);
 
-  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 (update())
-    _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::update() {
-  if (parseConfig())
-    qDebug() << "oidentd config parsed successfully";
-  else
-    qDebug() << QString("parsing oidentd config failed (%1 [%2])").arg(_configFile->errorString()).arg(_configFile->error());
+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();
+
+  _config.append(quasselStanzaTemplate.arg(localPort).arg(ident).arg(configTag));
+
+  _mutex.lock();
+  bool ret = writeConfig();
+  _mutex.unlock();
 
-  return writeConfig();
+  return ret;
 }
 
-bool OidentdConfigGenerator::parseConfig() {
-  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;
 
@@ -66,7 +84,7 @@ bool OidentdConfigGenerator::parseConfig() {
   while (!_configFile->atEnd()) {
     QByteArray line = _configFile->readLine();
 
-    if (checkLine(line))
+    if (keepQuasselStanzas || !lineByUs(line))
       parsedConfig.append(line);
   }
 
@@ -76,9 +94,16 @@ bool OidentdConfigGenerator::parseConfig() {
 }
 
 bool OidentdConfigGenerator::writeConfig() {
-  return true;
+  if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text))
+    return false;
+
+  _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);
 }