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