Sort cli options in --help output
[quassel.git] / src / core / oidentdconfiggenerator.cpp
1 /***************************************************************************
2  *   Copyright (C) 2012 by the Quassel Project                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "oidentdconfiggenerator.h"
22
23 OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
24   QObject(parent),
25   _initialized(false)
26 {
27   qDebug() << "OidentdConfigGenerator() checking for being initialized";
28   if (!_initialized)
29     init();
30 }
31
32 bool OidentdConfigGenerator::init() {
33   configDir = QDir::homePath();
34   configFileName = ".oidentd.conf";
35   configTag = " stanza created by Quassel";
36
37   _configFile = new QFile(configDir.absoluteFilePath(configFileName));
38   qDebug() << "1: _configFile" << _configFile->fileName();
39
40   quasselStanza = QRegExp(QString("^lport .* { .* } #%1$").arg(configTag));
41
42   if (parseConfig(true) && writeConfig())
43     _initialized = true;
44
45   qDebug() << "konichi wa °-°";
46
47   return _initialized;
48 }
49
50 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort) {
51   qDebug() << "localAddress" << localAddress;
52   qDebug() << "localPort" << localPort;
53   qDebug() << "peerAddress" << peerAddress;
54   qDebug() << "peerPort" << peerPort;
55   qDebug() << "ident" << identity->ident();
56
57   QString ident = identity->ident();
58
59   _config.append(QString("lport %1 { reply \"%2\" } #%3\n").arg(localPort).arg(ident).arg(configTag));
60
61   return writeConfig();
62 }
63
64 bool OidentdConfigGenerator::parseConfig(bool stripQuasselStanzas) {
65   qDebug() << "_configFile name" << _configFile->fileName();
66   qDebug() << "open?" << _configFile->isOpen();
67   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
68     return false;
69
70   QByteArray parsedConfig;
71   while (!_configFile->atEnd()) {
72     QByteArray line = _configFile->readLine();
73
74     if (!stripQuasselStanzas || checkLine(line))
75       parsedConfig.append(line);
76   }
77
78   _config = parsedConfig;
79
80   return true;
81 }
82
83 bool OidentdConfigGenerator::writeConfig() {
84   if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite))
85     return false;
86
87   //FIXME: thread safety
88   QTextStream out(_configFile);
89   out << _config;
90
91   return _configFile->flush();
92 }
93
94 bool OidentdConfigGenerator::checkLine(const QByteArray &line) {
95   return !quasselStanza.exactMatch(line);
96 }