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