Some cleanups
[quassel.git] / src / core / oidentdconfiggenerator.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <QString>
22
23 #include "corenetwork.h"
24 #include "oidentdconfiggenerator.h"
25
26 OidentdConfigGenerator::OidentdConfigGenerator(bool strict, QObject *parent) :
27     QObject(parent),
28     _initialized(false),
29     _strict(strict)
30 {
31     if (!_initialized)
32         init();
33 }
34
35
36 OidentdConfigGenerator::~OidentdConfigGenerator()
37 {
38     _quasselConfig.clear();
39     writeConfig();
40     _configFile->deleteLater();
41 }
42
43
44 bool OidentdConfigGenerator::init()
45 {
46     _configDir = QDir::homePath();
47     _configFileName = ".oidentd.conf";
48
49     if (Quassel::isOptionSet("oidentd-conffile"))
50         _configPath = Quassel::optionValue("oidentd-conffile");
51     else
52         _configPath = _configDir.absoluteFilePath(_configFileName);
53
54     _configTag = " stanza created by Quassel";
55
56     _configFile = new QFile(_configPath);
57
58     // Rx has to match Template in order for cleanup to work.
59     // Template should be enhanced with the "from" parameter as soon as Quassel gains
60     // the ability to bind to an IP on client sockets.
61
62     _quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
63     _quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(_configTag));
64
65     // initially remove all Quassel stanzas that might be present
66     if (parseConfig(false) && writeConfig())
67         _initialized = true;
68
69     return _initialized;
70 }
71
72
73 QString OidentdConfigGenerator::sysIdentForIdentity(const CoreIdentity *identity) const {
74     if (!_strict) {
75         return identity->ident();
76     }
77     const CoreNetwork *network = qobject_cast<CoreNetwork *>(sender());
78     return network->coreSession()->strictSysident();
79 }
80
81
82 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort)
83 {
84     Q_UNUSED(localAddress) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
85     const QString ident = sysIdentForIdentity(identity);
86
87     _quasselConfig.append(_quasselStanzaTemplate.arg(localPort).arg(ident).arg(_configTag).toLatin1());
88
89     bool ret = writeConfig();
90
91     return ret;
92 }
93
94
95 //! not yet implemented
96 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity, const QHostAddress &localAddress, quint16 localPort, const QHostAddress &peerAddress, quint16 peerPort)
97 {
98     Q_UNUSED(identity) Q_UNUSED(localAddress) Q_UNUSED(localPort) Q_UNUSED(peerAddress) Q_UNUSED(peerPort)
99     return true;
100 }
101
102
103 bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas)
104 {
105     if (!_configFile->exists())
106         return true;
107
108     if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly))
109         return false;
110     _mutex.lock();
111
112     _parsedConfig.clear();
113     _configFile->seek(0);
114     while (!_configFile->atEnd()) {
115         QByteArray line = _configFile->readLine();
116
117         if (!lineByUs(line))
118             _parsedConfig.append(line);
119         else if (readQuasselStanzas)
120             _quasselConfig.append(line);
121     }
122
123     _configFile->close();
124     _mutex.unlock();
125     return true;
126 }
127
128
129 bool OidentdConfigGenerator::writeConfig()
130 {
131 #ifdef HAVE_UMASK
132     mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r--
133 #endif
134     bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text));
135 #ifdef HAVE_UMASK
136     umask(prev_umask);
137 #endif
138
139     if (not_open)
140         return false;
141
142     _mutex.lock();
143
144     _configFile->seek(0);
145     _configFile->resize(0);
146     _configFile->write(_parsedConfig);
147     _configFile->write(_quasselConfig);
148
149     _configFile->close();
150     _mutex.unlock();
151     return true;
152 }
153
154
155 bool OidentdConfigGenerator::lineByUs(const QByteArray &line)
156 {
157     return _quasselStanzaRx.exactMatch(line);
158 }