1// krazy:excludeall=license (it's a program, not a library)
2/*
3 SPDX-FileCopyrightText: 2001 Malte Starostik <malte@kde.org>
4 based on kmailservice.cpp,
5 SPDX-FileCopyrightText: 2000 Simon Hausmann <hausmann@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include <KAuthorized>
11#include <KConfig>
12#include <KConfigGroup>
13#include <KIO/CommandLauncherJob>
14#include <KLocalizedString>
15#include <QApplication>
16#include <QDebug>
17#include <QMessageBox>
18#include <QUrl>
19
20int main(int argc, char **argv)
21{
22 QApplication a(argc, argv);
23
24 if (argc != 2) {
25 fprintf(stderr, format: "Usage: ktelnetservice6 <url>\n");
26 return 1;
27 }
28
29 KConfig config(QStringLiteral("kdeglobals"));
30 KConfigGroup cg(&config, QStringLiteral("General"));
31 QString terminal = cg.readPathEntry(key: "TerminalApplication", QStringLiteral("konsole"));
32
33 QUrl url(QString::fromLocal8Bit(ba: argv[1]));
34 QStringList cmd;
35 if (terminal == QLatin1String("konsole")) {
36 cmd << QStringLiteral("--noclose");
37 }
38
39 cmd << QStringLiteral("-e");
40 if (url.scheme() == QLatin1String("telnet")) {
41 cmd << QStringLiteral("telnet");
42 } else if (url.scheme() == QLatin1String("ssh")) {
43 cmd << QStringLiteral("ssh");
44 } else if (url.scheme() == QLatin1String("rlogin")) {
45 cmd << QStringLiteral("rlogin");
46 } else {
47 qCritical() << "Invalid protocol " << url.scheme();
48 return 2;
49 }
50
51 if (!KAuthorized::authorize(action: KAuthorized::SHELL_ACCESS)) {
52 QMessageBox::critical(parent: nullptr, i18n("Access denied"), i18n("You do not have permission to access the %1 protocol.", url.scheme()));
53 return 3;
54 }
55
56 if (!url.userName().isEmpty()) {
57 cmd << QStringLiteral("-l");
58 cmd << url.userName();
59 }
60
61 QString host;
62 if (!url.host().isEmpty()) {
63 host = url.host(); // telnet://host
64 } else if (!url.path().isEmpty()) {
65 host = url.path(); // telnet:host
66 }
67
68 if (host.isEmpty() || host.startsWith(c: QLatin1Char('-'))) {
69 qCritical() << "Invalid hostname " << host;
70 return 2;
71 }
72
73 cmd << host;
74
75 if (url.port() > 0) {
76 if (url.scheme() == QLatin1String("ssh")) {
77 cmd << QStringLiteral("-p") << QString::number(url.port());
78 } else {
79 cmd << QString::number(url.port());
80 }
81 }
82
83 auto job = new KIO::CommandLauncherJob(terminal, cmd);
84 job->start();
85
86 return 0;
87}
88

source code of kio/src/schemehandlers/telnet/ktelnetservice.cpp