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