File indexing completed on 2024-05-12 03:56:51

0001 // krazy:excludeall=license (it's a program, not a library)
0002 /*
0003     SPDX-FileCopyrightText: 2001 Malte Starostik <malte@kde.org>
0004     based on kmailservice.cpp,
0005     SPDX-FileCopyrightText: 2000 Simon Hausmann <hausmann@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <KAuthorized>
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 #include <KIO/CommandLauncherJob>
0014 #include <KLocalizedString>
0015 #include <QApplication>
0016 #include <QDebug>
0017 #include <QMessageBox>
0018 #include <QUrl>
0019 
0020 int main(int argc, char **argv)
0021 {
0022     QApplication a(argc, argv);
0023 
0024     if (argc != 2) {
0025         fprintf(stderr, "Usage: ktelnetservice6 <url>\n");
0026         return 1;
0027     }
0028 
0029     KConfig config(QStringLiteral("kdeglobals"));
0030     KConfigGroup cg(&config, QStringLiteral("General"));
0031     QString terminal = cg.readPathEntry("TerminalApplication", QStringLiteral("konsole"));
0032 
0033     QUrl url(QString::fromLocal8Bit(argv[1]));
0034     QStringList cmd;
0035     if (terminal == QLatin1String("konsole")) {
0036         cmd << QStringLiteral("--noclose");
0037     }
0038 
0039     cmd << QStringLiteral("-e");
0040     if (url.scheme() == QLatin1String("telnet")) {
0041         cmd << QStringLiteral("telnet");
0042     } else if (url.scheme() == QLatin1String("ssh")) {
0043         cmd << QStringLiteral("ssh");
0044     } else if (url.scheme() == QLatin1String("rlogin")) {
0045         cmd << QStringLiteral("rlogin");
0046     } else {
0047         qCritical() << "Invalid protocol " << url.scheme();
0048         return 2;
0049     }
0050 
0051     if (!KAuthorized::authorize(KAuthorized::SHELL_ACCESS)) {
0052         QMessageBox::critical(nullptr, i18n("Access denied"), i18n("You do not have permission to access the %1 protocol.", url.scheme()));
0053         return 3;
0054     }
0055 
0056     if (!url.userName().isEmpty()) {
0057         cmd << QStringLiteral("-l");
0058         cmd << url.userName();
0059     }
0060 
0061     QString host;
0062     if (!url.host().isEmpty()) {
0063         host = url.host(); // telnet://host
0064     } else if (!url.path().isEmpty()) {
0065         host = url.path(); // telnet:host
0066     }
0067 
0068     if (host.isEmpty() || host.startsWith(QLatin1Char('-'))) {
0069         qCritical() << "Invalid hostname " << host;
0070         return 2;
0071     }
0072 
0073     cmd << host;
0074 
0075     if (url.port() > 0) {
0076         if (url.scheme() == QLatin1String("ssh")) {
0077             cmd << QStringLiteral("-p") << QString::number(url.port());
0078         } else {
0079             cmd << QString::number(url.port());
0080         }
0081     }
0082 
0083     auto job = new KIO::CommandLauncherJob(terminal, cmd);
0084     job->start();
0085 
0086     return 0;
0087 }