+
+void Quassel::setupBuildInfo()
+{
+ BuildInfo buildInfo;
+ buildInfo.applicationName = "quassel";
+ buildInfo.coreApplicationName = "quasselcore";
+ buildInfo.clientApplicationName = "quasselclient";
+ buildInfo.organizationName = "Quassel Project";
+ buildInfo.organizationDomain = "quassel-irc.org";
+
+ buildInfo.protocolVersion = 10; // FIXME: deprecated, will be removed
+
+ buildInfo.baseVersion = QUASSEL_VERSION_STRING;
+ buildInfo.generatedVersion = GIT_DESCRIBE;
+
+ // Check if we got a commit hash
+ if (!QString(GIT_HEAD).isEmpty()) {
+ buildInfo.commitHash = GIT_HEAD;
+ // Set to Unix epoch, wrapped as a string for backwards-compatibility
+ buildInfo.commitDate = QString::number(GIT_COMMIT_DATE);
+ }
+ else if (!QString(DIST_HASH).contains("Format")) {
+ buildInfo.commitHash = DIST_HASH;
+ // Leave as Unix epoch if set as Unix epoch, but don't force this for
+ // backwards-compatibility with existing packaging/release tools that might set strings.
+ buildInfo.commitDate = QString(DIST_DATE);
+ }
+
+ // create a nice version string
+ if (buildInfo.generatedVersion.isEmpty()) {
+ if (!buildInfo.commitHash.isEmpty()) {
+ // dist version
+ buildInfo.plainVersionString = QString{"v%1 (dist-%2)"}
+ .arg(buildInfo.baseVersion)
+ .arg(buildInfo.commitHash.left(7));
+ buildInfo.fancyVersionString = QString{"v%1 (dist-<a href=\"https://github.com/quassel/quassel/commit/%3\">%2</a>)"}
+ .arg(buildInfo.baseVersion)
+ .arg(buildInfo.commitHash.left(7))
+ .arg(buildInfo.commitHash);
+ }
+ else {
+ // we only have a base version :(
+ buildInfo.plainVersionString = QString{"v%1 (unknown revision)"}.arg(buildInfo.baseVersion);
+ }
+ }
+ else {
+ // analyze what we got from git-describe
+ static const QRegExp rx{"(.*)-(\\d+)-g([0-9a-f]+)(-dirty)?$"};
+ if (rx.exactMatch(buildInfo.generatedVersion)) {
+ QString distance = rx.cap(2) == "0" ? QString{} : QString{"%1+%2 "}.arg(rx.cap(1), rx.cap(2));
+ buildInfo.plainVersionString = QString{"v%1 (%2git-%3%4)"}.arg(buildInfo.baseVersion, distance, rx.cap(3), rx.cap(4));
+ if (!buildInfo.commitHash.isEmpty()) {
+ buildInfo.fancyVersionString = QString{"v%1 (%2git-<a href=\"https://github.com/quassel/quassel/commit/%5\">%3</a>%4)"}
+ .arg(buildInfo.baseVersion, distance, rx.cap(3), rx.cap(4), buildInfo.commitHash);
+ }
+ }
+ else {
+ buildInfo.plainVersionString = QString{"v%1 (invalid revision)"}.arg(buildInfo.baseVersion);
+ }
+ }
+ if (buildInfo.fancyVersionString.isEmpty()) {
+ buildInfo.fancyVersionString = buildInfo.plainVersionString;
+ }
+
+ instance()->_buildInfo = std::move(buildInfo);
+}
+
+
+const Quassel::BuildInfo &Quassel::buildInfo()
+{
+ return instance()->_buildInfo;
+}
+
+
+void Quassel::setupSignalHandling()
+{
+#ifndef Q_OS_WIN
+ _signalWatcher = new PosixSignalWatcher(this);
+#else
+ _signalWatcher = new WindowsSignalWatcher(this);
+#endif
+ connect(_signalWatcher, SIGNAL(handleSignal(AbstractSignalWatcher::Action)), this, SLOT(handleSignal(AbstractSignalWatcher::Action)));
+}
+
+
+void Quassel::handleSignal(AbstractSignalWatcher::Action action)
+{
+ switch (action) {
+ case AbstractSignalWatcher::Action::Reload:
+ // Most applications use this as the 'configuration reload' command, e.g. nginx uses it for graceful reloading of processes.
+ if (!_reloadHandlers.empty()) {
+ quInfo() << "Reloading configuration";
+ if (reloadConfig()) {
+ quInfo() << "Successfully reloaded configuration";
+ }
+ }
+ break;
+ case AbstractSignalWatcher::Action::Terminate:
+ if (!_quitting) {
+ quit();
+ }
+ else {
+ quInfo() << "Already shutting down, ignoring signal";
+ }
+ break;
+ case AbstractSignalWatcher::Action::HandleCrash:
+ logBacktrace(instance()->coreDumpFileName());
+ exit(EXIT_FAILURE);
+ }
+}
+
+
+Quassel::RunMode Quassel::runMode() {
+ return instance()->_runMode;
+}
+
+
+void Quassel::setupCliParser()
+{
+ QList<QCommandLineOption> options;
+
+ // General options
+ /// @todo Bring back --datadir to specify the database location independent of config
+ if (runMode() == RunMode::ClientOnly) {
+ options += {{"c", "configdir"}, tr("Specify the directory holding the client configuration."), tr("path")};
+ }
+ else {
+ options += {{"c", "configdir"}, tr("Specify the directory holding configuration files, the SQlite database and the SSL certificate."), tr("path")};
+ }
+
+ // Client options
+ if (runMode() != RunMode::CoreOnly) {
+ options += {
+ {"icontheme", tr("Override the system icon theme ('breeze' is recommended)."), tr("theme")},
+ {"qss", tr("Load a custom application stylesheet."), tr("file.qss")},
+ {"hidewindow", tr("Start the client minimized to the system tray.")},
+ };
+ }
+
+ // Core options
+ if (runMode() != RunMode::ClientOnly) {
+ options += {
+ {"listen", tr("The address(es) quasselcore will listen on."), tr("<address>[,<address>[,...]]"), "::,0.0.0.0"},
+ {{"p", "port"}, tr("The port quasselcore will listen at."), tr("port"), "4242"},
+ {{"n", "norestore"}, tr("Don't restore last core's state.")},
+ {"config-from-environment", tr("Load configuration from environment variables.")},
+ {"select-backend", tr("Switch storage backend (migrating data if possible)."), tr("backendidentifier")},
+ {"select-authenticator", tr("Select authentication backend."), tr("authidentifier")},
+ {"add-user", tr("Starts an interactive session to add a new core user.")},
+ {"change-userpass", tr("Starts an interactive session to change the password of the user identified by <username>."), tr("username")},
+ {"strict-ident", tr("Use users' quasselcore username as ident reply. Ignores each user's configured ident setting.")},
+ {"ident-daemon", tr("Enable internal ident daemon.")},
+ {"ident-port", tr("The port quasselcore will listen at for ident requests. Only meaningful with --ident-daemon."), tr("port"), "10113"},
+ {"oidentd", tr("Enable oidentd integration. In most cases you should also enable --strict-ident.")},
+ {"oidentd-conffile", tr("Set path to oidentd configuration file."), tr("file")},
+#ifdef HAVE_SSL
+ {"require-ssl", tr("Require SSL for remote (non-loopback) client connections.")},
+ {"ssl-cert", tr("Specify the path to the SSL certificate."), tr("path"), "configdir/quasselCert.pem"},
+ {"ssl-key", tr("Specify the path to the SSL key."), tr("path"), "ssl-cert-path"},
+#endif
+ };
+ }
+
+ // Logging options
+ options += {
+ {{"L", "loglevel"}, tr("Supports one of Debug|Info|Warning|Error; default is Info."), tr("level"), "Info"},
+ {{"l", "logfile"}, tr("Log to a file."), "path"},
+#ifdef HAVE_SYSLOG
+ {"syslog", tr("Log to syslog.")},
+#endif
+ };
+
+ // Debug options
+ options += {{"d", "debug"}, tr("Enable debug output.")};
+ if (runMode() != RunMode::CoreOnly) {
+ options += {
+ {"debugbufferswitches", tr("Enables debugging for bufferswitches.")},
+ {"debugmodel", tr("Enables debugging for models.")},
+ };
+ }
+ if (runMode() != RunMode::ClientOnly) {
+ options += {
+ {"debug-irc", tr("Enable logging of all raw IRC messages to debug log, including passwords! In most cases you should also set --loglevel Debug")},
+ {"debug-irc-id", tr("Limit raw IRC logging to this network ID. Implies --debug-irc"), tr("database network ID"), "-1"},
+ };
+ }
+
+ _cliParser.addOptions(options);
+ _cliParser.addHelpOption();
+ _cliParser.addVersionOption();
+ _cliParser.setApplicationDescription(tr("Quassel IRC is a modern, distributed IRC client."));
+
+ // This will call ::exit() for --help, --version and in case of errors
+ _cliParser.process(*QCoreApplication::instance());
+}
+
+
+QString Quassel::optionValue(const QString &key)
+{
+ return instance()->_cliParser.value(key);
+}
+
+
+bool Quassel::isOptionSet(const QString &key)
+{
+ return instance()->_cliParser.isSet(key);
+}
+
+
+const QString &Quassel::coreDumpFileName()
+{
+ if (_coreDumpFileName.isEmpty()) {
+ QDir configDir(configDirPath());
+ _coreDumpFileName = configDir.absoluteFilePath(QString("Quassel-Crash-%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMdd-hhmm")));
+ QFile dumpFile(_coreDumpFileName);
+ dumpFile.open(QIODevice::Append);
+ QTextStream dumpStream(&dumpFile);
+ dumpStream << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash << '\n';
+ qDebug() << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash;
+ dumpStream.flush();
+ dumpFile.close();
+ }
+ return _coreDumpFileName;
+}
+
+
+QString Quassel::configDirPath()
+{
+ if (!instance()->_configDirPath.isEmpty())
+ return instance()->_configDirPath;
+
+ QString path;
+ if (isOptionSet("configdir")) {
+ path = Quassel::optionValue("configdir");
+ }
+ else {