QssParser: Interpret "oblique" as italic
[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(QObject *parent) :
27     QObject(parent),
28     _initialized(false)
29 {
30     if (!_initialized)
31         init();
32 }
33
34
35 OidentdConfigGenerator::~OidentdConfigGenerator()
36 {
37     _quasselConfig.clear();
38     writeConfig();
39     _configFile->deleteLater();
40 }
41
42
43 bool OidentdConfigGenerator::init()
44 {
45     _configDir = QDir::homePath();
46     _configFileName = ".oidentd.conf";
47
48     if (Quassel::isOptionSet("oidentd-conffile"))
49         _configPath = Quassel::optionValue("oidentd-conffile");
50     else
51         _configPath = _configDir.absoluteFilePath(_configFileName);
52
53     _configTag = " stanza created by Quassel";
54
55     _configFile = new QFile(_configPath);
56
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.
60
61     _quasselStanzaTemplate = QString("lport %1 { reply \"%2\" } #%3\n");
62     _quasselStanzaRx = QRegExp(QString("^lport .* \\{ .* \\} #%1\\r?\\n").arg(_configTag));
63
64     // initially remove all Quassel stanzas that might be present
65     if (parseConfig(false) && writeConfig())
66         _initialized = true;
67
68     return _initialized;
69 }
70
71
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);
76 }
77
78
79 bool OidentdConfigGenerator::addSocket(const CoreIdentity *identity,
80                                        const QHostAddress &localAddress, quint16 localPort,
81                                        const QHostAddress &peerAddress, quint16 peerPort,
82                                        qint64 socketId)
83 {
84     Q_UNUSED(localAddress)
85     Q_UNUSED(peerAddress)
86     Q_UNUSED(peerPort)
87     Q_UNUSED(socketId)
88
89     const QString ident = sysIdentForIdentity(identity);
90
91     _quasselConfig.append(_quasselStanzaTemplate.arg(localPort).arg(ident).arg(_configTag).toLatin1());
92
93     bool ret = writeConfig();
94
95     return ret;
96 }
97
98
99 //! not yet implemented
100 bool OidentdConfigGenerator::removeSocket(const CoreIdentity *identity,
101                                           const QHostAddress &localAddress, quint16 localPort,
102                                           const QHostAddress &peerAddress, quint16 peerPort,
103                                           qint64 socketId)
104 {
105     Q_UNUSED(identity)
106     Q_UNUSED(localAddress)
107     Q_UNUSED(localPort)
108     Q_UNUSED(peerAddress)
109     Q_UNUSED(peerPort)
110     Q_UNUSED(socketId)
111
112     return true;
113 }
114
115
116 bool OidentdConfigGenerator::parseConfig(bool readQuasselStanzas)
117 {
118     if (!_configFile->exists())
119         return true;
120
121     if (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadOnly))
122         return false;
123     _mutex.lock();
124
125     _parsedConfig.clear();
126     _configFile->seek(0);
127     while (!_configFile->atEnd()) {
128         QByteArray line = _configFile->readLine();
129
130         if (!lineByUs(line))
131             _parsedConfig.append(line);
132         else if (readQuasselStanzas)
133             _quasselConfig.append(line);
134     }
135
136     _configFile->close();
137     _mutex.unlock();
138     return true;
139 }
140
141
142 bool OidentdConfigGenerator::writeConfig()
143 {
144 #ifdef HAVE_UMASK
145     mode_t prev_umask = umask(S_IXUSR | S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH); // == 0133, rw-r--r--
146 #endif
147     bool not_open = (!_configFile->isOpen() && !_configFile->open(QIODevice::ReadWrite | QIODevice::Text));
148 #ifdef HAVE_UMASK
149     umask(prev_umask);
150 #endif
151
152     if (not_open)
153         return false;
154
155     _mutex.lock();
156
157     _configFile->seek(0);
158     _configFile->resize(0);
159     _configFile->write(_parsedConfig);
160     _configFile->write(_quasselConfig);
161
162     _configFile->close();
163     _mutex.unlock();
164     return true;
165 }
166
167
168 bool OidentdConfigGenerator::lineByUs(const QByteArray &line)
169 {
170     return _quasselStanzaRx.exactMatch(line);
171 }