1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "corenetwork.h"
24 #include "oidentdconfiggenerator.h"
26 OidentdConfigGenerator::OidentdConfigGenerator(QObject *parent) :
35 OidentdConfigGenerator::~OidentdConfigGenerator()
37 _quasselConfig.clear();
39 _configFile->deleteLater();
43 bool OidentdConfigGenerator::init()
45 _configDir = QDir::homePath();
46 _configFileName = ".oidentd.conf";
48 if (Quassel::isOptionSet("oidentd-conffile"))
49 _configPath = Quassel::optionValue("oidentd-conffile");
51 _configPath = _configDir.absoluteFilePath(_configFileName);
53 _configTag = " stanza created by Quassel";
55 _configFile = new QFile(_configPath);
57 // Rx has to match Template in order for cleanup to work.
58 // Template should be enhanced with the "from" parameter as soon as Quassel gains
59 // the ability to bind to an IP on client sockets.
61 _quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
62 _quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(_configTag));
64 // initially remove all Quassel stanzas that might be present
65 if (parseConfig(false) && writeConfig())
72 QString OidentdConfigGenerator::sysIdentForIdentity(const CoreIdentity *identity) const {
73 // Make sure the identity's ident complies with strict mode if enabled
74 const CoreNetwork *network = qobject_cast<CoreNetwork *>(sender());
75 return network->coreSession()->strictCompliantIdent(identity);
79 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort)
81 Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
82 const QString ident = sysIdentForIdentity(identity);
84 _quasselConfig.append(_quasselStanzaTemplate.arg(localPort).arg(ident).arg(_configTag).toLatin1());
86 bool ret = writeConfig();
92 //! not yet implemented
93 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort)
95 Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
100 bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas)
102 if (!_configFile->exists())
105 if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly))
109 _parsedConfig.clear();
110 _configFile->seek(0);
111 while (!_configFile->atEnd()) {
112 QByteArray line = _configFile->readLine();
115 _parsedConfig.append(line);
116 else if (readQuasselStanzas)
117 _quasselConfig.append(line);
120 _configFile->close();
126 bool OidentdConfigGenerator::writeConfig()
129 mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r--
131 bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text));
141 _configFile->seek(0);
142 _configFile->resize(0);
143 _configFile->write(_parsedConfig);
144 _configFile->write(_quasselConfig);
146 _configFile->close();
152 bool OidentdConfigGenerator::lineByUs(const QByteArray &line)
154 return _quasselStanzaRx.exactMatch(line);