4b134049fd9b4b3d7358ff0fd75a2e6d08235219
[quassel.git] / src / common / main.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 <cstdlib>
22 #include <memory>
23
24 #ifdef HAVE_UMASK
25 #  include <sys/types.h>
26 #  include <sys/stat.h>
27 #endif /* HAVE_UMASK */
28
29 #include <QCoreApplication>
30 #include <QTextCodec>
31
32 #ifdef BUILD_CORE
33 #  include "coreapplication.h"
34 #elif defined BUILD_QTUI
35 #  include "aboutdata.h"
36 #  include "qtuiapplication.h"
37 #elif defined BUILD_MONO
38 #  include "aboutdata.h"
39 #  include "monoapplication.h"
40
41 #else
42 #error "Something is wrong - you need to #define a build mode!"
43 #endif
44
45 // We don't want quasselcore to depend on KDE
46 #if defined HAVE_KF5 && defined BUILD_CORE
47 #  undef HAVE_KF5
48 #endif
49
50 #if defined HAVE_KF5
51 #  include <KCoreAddons/KAboutData>
52 #  include <KCoreAddons/Kdelibs4ConfigMigrator>
53 #endif
54
55 #if !defined(BUILD_CORE) && defined(STATIC)
56 #include <QtPlugin>
57 Q_IMPORT_PLUGIN(qjpeg)
58 Q_IMPORT_PLUGIN(qgif)
59 #endif
60
61 #include "qt5cliparser.h"
62 #include "quassel.h"
63 #include "types.h"
64
65 int main(int argc, char **argv)
66 {
67     // We need to explicitly initialize the required resources when linking statically
68 #ifndef BUILD_QTUI
69     Q_INIT_RESOURCE(sql);
70 #endif
71 #ifndef BUILD_CORE
72     Q_INIT_RESOURCE(pics);
73     Q_INIT_RESOURCE(hicolor_icons);
74 #endif
75
76 #ifdef EMBED_DATA
77     Q_INIT_RESOURCE(i18n);
78 # ifndef BUILD_CORE
79     Q_INIT_RESOURCE(data);
80     Q_INIT_RESOURCE(breeze_icons);
81     Q_INIT_RESOURCE(breeze_dark_icons);
82 #  ifdef WITH_OXYGEN_ICONS
83       Q_INIT_RESOURCE(oxygen_icons);
84 #  endif
85 #  ifdef WITH_BUNDLED_ICONS
86       Q_INIT_RESOURCE(breeze_icon_theme);
87       Q_INIT_RESOURCE(breeze_dark_icon_theme);
88 #   ifdef WITH_OXYGEN_ICONS
89       Q_INIT_RESOURCE(oxygen_icon_theme);
90 #   endif
91 #  endif
92 # endif
93 #endif
94
95     // Set umask so files are created with restricted permissions
96 #ifdef HAVE_UMASK
97     umask(S_IRWXG | S_IRWXO);
98 #endif
99
100     // Instantiate early, so log messages are handled
101     Quassel quassel;
102
103     Quassel::setupBuildInfo();
104     QCoreApplication::setApplicationName(Quassel::buildInfo().applicationName);
105     QCoreApplication::setApplicationVersion(Quassel::buildInfo().plainVersionString);
106     QCoreApplication::setOrganizationName(Quassel::buildInfo().organizationName);
107     QCoreApplication::setOrganizationDomain(Quassel::buildInfo().organizationDomain);
108
109     // Migrate settings from KDE4 to KF5 if appropriate
110 #ifdef HAVE_KF5
111     Kdelibs4ConfigMigrator migrator(QCoreApplication::applicationName());
112     migrator.setConfigFiles(QStringList() << "quasselrc" << "quassel.notifyrc");
113     migrator.migrate();
114 #endif
115
116     //Setup the High-DPI settings
117 # if QT_VERSION >= 0x050600 && defined(Q_OS_WIN)
118     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); //Added in Qt 5.6
119 #endif
120     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
121
122     // Instantiate application
123 #if defined BUILD_CORE
124     CoreApplication app(argc, argv);
125     const auto runMode = Quassel::RunMode::CoreOnly;
126 #elif defined BUILD_QTUI
127     QtUiApplication app(argc, argv);
128     const auto runMode = Quassel::RunMode::ClientOnly;
129 #elif defined BUILD_MONO
130     MonolithicApplication app(argc, argv);
131     const auto runMode = Quassel::RunMode::Monolithic;
132 #endif
133
134     // Initialize CLI arguments
135     // TODO: Move CLI option handling into Quassel::init(), after initializing the translation catalogue
136
137     std::shared_ptr<AbstractCliParser> cliParser = std::make_shared<Qt5CliParser>();
138     Quassel::setCliParser(cliParser);
139
140     // put shared client&core arguments here
141     cliParser->addSwitch("debug", 'd', "Enable debug output");
142     cliParser->addSwitch("help", 'h', "Display this help and exit");
143     cliParser->addSwitch("version", 'v', "Display version information");
144 #ifdef BUILD_QTUI
145     cliParser->addOption("configdir", 'c', "Specify the directory holding the client configuration", "path");
146 #else
147     cliParser->addOption("configdir", 'c', "Specify the directory holding configuration files, the SQlite database and the SSL certificate", "path");
148 #endif
149     cliParser->addOption("datadir", 0, "DEPRECATED - Use --configdir instead", "path");
150     cliParser->addOption("loglevel", 'L', "Loglevel Debug|Info|Warning|Error", "level", "Info");
151 #ifdef HAVE_SYSLOG
152     cliParser->addSwitch("syslog", 0, "Log to syslog");
153 #endif
154     cliParser->addOption("logfile", 'l', "Log to a file", "path");
155
156 #ifndef BUILD_CORE
157     // put client-only arguments here
158     cliParser->addOption("icontheme", 0, "Override the system icon theme ('breeze' is recommended)", "theme");
159     cliParser->addOption("qss", 0, "Load a custom application stylesheet", "file.qss");
160     cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches");
161     cliParser->addSwitch("debugmodel", 0, "Enables debugging for models");
162     cliParser->addSwitch("hidewindow", 0, "Start the client minimized to the system tray");
163 #endif
164 #ifndef BUILD_QTUI
165     // put core-only arguments here
166     cliParser->addOption("listen", 0, "The address(es) quasselcore will listen on", "<address>[,<address>[,...]]", "::,0.0.0.0");
167     cliParser->addOption("port", 'p', "The port quasselcore will listen at", "port", "4242");
168     cliParser->addSwitch("norestore", 'n', "Don't restore last core's state");
169     cliParser->addSwitch("config-from-environment", 0, "Load configuration from environment variables");
170     cliParser->addOption("select-backend", 0, "Switch storage backend (migrating data if possible)", "backendidentifier");
171     cliParser->addOption("select-authenticator", 0, "Select authentication backend", "authidentifier");
172     cliParser->addSwitch("add-user", 0, "Starts an interactive session to add a new core user");
173     cliParser->addOption("change-userpass", 0, "Starts an interactive session to change the password of the user identified by <username>", "username");
174     cliParser->addSwitch("oidentd", 0, "Enable oidentd integration.  In most cases you should also enable --strict-ident");
175     cliParser->addOption("oidentd-conffile", 0, "Set path to oidentd configuration file", "file");
176     cliParser->addSwitch("strict-ident", 0, "Use users' quasselcore username as ident reply. Ignores each user's configured ident setting.");
177     cliParser->addSwitch("ident-daemon", 0, "Enable internal ident daemon");
178     cliParser->addOption("ident-port", 0, "The port quasselcore will listen at for ident requests. Only meaningful with --ident-daemon", "port", "10113");
179 #ifdef HAVE_SSL
180     cliParser->addSwitch("require-ssl", 0, "Require SSL for remote (non-loopback) client connections");
181     cliParser->addOption("ssl-cert", 0, "Specify the path to the SSL Certificate", "path", "configdir/quasselCert.pem");
182     cliParser->addOption("ssl-key", 0, "Specify the path to the SSL key", "path", "ssl-cert-path");
183 #endif
184     cliParser->addSwitch("debug-irc", 0,
185                          "Enable logging of all raw IRC messages to debug log, including "
186                          "passwords!  In most cases you should also set --loglevel Debug");
187     cliParser->addOption("debug-irc-id", 0,
188                          "Limit raw IRC logging to this network ID.  Implies --debug-irc",
189                          "database network ID", "-1");
190     cliParser->addSwitch("enable-experimental-dcc", 0, "Enable highly experimental and unfinished support for CTCP DCC (DANGEROUS)");
191 #endif
192
193     if (!cliParser->init(app.arguments())) {
194         cliParser->usage();
195         return EXIT_FAILURE;
196     }
197
198     try {
199         // Note: This method requires CLI options to be available
200         Quassel::instance()->init(runMode);
201
202 #ifdef HAVE_KF5
203         AboutData aboutData;
204         AboutData::setQuasselPersons(&aboutData);
205         KAboutData::setApplicationData(aboutData.kAboutData());
206 #endif
207
208         // Initialize the application
209         app.init();
210     }
211     catch (ExitException e) {
212         if (!e.errorString.isEmpty()) {
213             qCritical() << e.errorString;
214         }
215         return e.exitCode;
216     }
217
218     return app.exec();
219 }