Warning, file /frameworks/kxmlgui/src/ksendbugmail/main.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2000 Bernd Johannes Wuebben <wuebben@math.cornell.edu>
0003     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "main.h"
0009 #include <qplatformdefs.h>
0010 
0011 #include <QApplication>
0012 #include <QHostInfo>
0013 #include <QTextStream>
0014 
0015 #include <KEMailSettings>
0016 #include <KLocalizedString>
0017 
0018 #include <qcommandlineoption.h>
0019 #include <qcommandlineparser.h>
0020 
0021 #include "../systeminformation_p.h"
0022 #include "smtp.h"
0023 
0024 void BugMailer::slotError(int errornum)
0025 {
0026     QString lstr;
0027 
0028     switch (errornum) {
0029     case SMTP::ConnectError:
0030         lstr = i18n("Error connecting to server.");
0031         break;
0032     case SMTP::NotConnected:
0033         lstr = i18n("Not connected.");
0034         break;
0035     case SMTP::ConnectTimeout:
0036         lstr = i18n("Connection timed out.");
0037         break;
0038     case SMTP::InteractTimeout:
0039         lstr = i18n("Time out waiting for server interaction.");
0040         break;
0041     default:
0042         lstr = QString::fromLatin1(sm->getLastLine().trimmed());
0043         lstr = i18n("Server said: \"%1\"", lstr);
0044     }
0045     // qCDebug(DEBUG_KXMLGUI) << lstr;
0046 
0047     fputs(lstr.toUtf8().data(), stdout);
0048     fflush(stdout);
0049 
0050     qApp->exit(1);
0051 }
0052 
0053 void BugMailer::slotSend()
0054 {
0055     // qCDebug(DEBUG_KXMLGUI);
0056     qApp->exit(0);
0057 }
0058 
0059 int main(int argc, char **argv)
0060 {
0061     QCoreApplication a(argc, argv);
0062     a.setApplicationName(QStringLiteral("ksendbugmail"));
0063     a.setApplicationVersion(QStringLiteral("1.0"));
0064 
0065     KLocalizedString::setApplicationDomain("kxmlgui5");
0066 
0067     // d.addAuthor(ki18n("Stephan Kulow"), ki18n("Author"), "coolo@kde.org");
0068 
0069     QString subject;
0070     QString recipient;
0071     {
0072         QCommandLineParser parser;
0073         parser.addVersionOption();
0074         parser.setApplicationDescription(i18n("Sends a bug report by email."));
0075         parser.addHelpOption();
0076         parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("subject"), i18n("The subject line of the email."), QStringLiteral("argument")));
0077         parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("recipient"),
0078                                             i18n("The email address to send the bug report to."),
0079                                             QStringLiteral("argument"),
0080                                             QStringLiteral("submit@bugs.kde.org")));
0081         parser.process(a);
0082         recipient = parser.value(QStringLiteral("recipient"));
0083         subject = parser.value(QStringLiteral("subject"));
0084     }
0085     if (recipient.isEmpty()) {
0086         recipient = QStringLiteral("submit@bugs.kde.org");
0087     } else {
0088         if (recipient.at(0) == QLatin1Char('\'')) {
0089             recipient = recipient.mid(1, recipient.length() - 2);
0090         }
0091     }
0092     // qCDebug(DEBUG_KXMLGUI) << "recp" << recipient;
0093 
0094     if (subject.isEmpty()) {
0095         subject = QStringLiteral("(no subject)");
0096     } else {
0097         if (subject.at(0) == QLatin1Char('\'')) {
0098             subject = subject.mid(1, subject.length() - 2);
0099         }
0100     }
0101     QTextStream input(stdin, QIODevice::ReadOnly);
0102     // The default in Qt6 is UTF-8
0103 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0104     input.setCodec("UTF-8");
0105 #endif
0106     QString text;
0107     QString line;
0108     while (!input.atEnd()) {
0109         line = input.readLine();
0110         text += line + QLatin1String("\r\n");
0111     }
0112     // qCDebug(DEBUG_KXMLGUI) << text;
0113 
0114     KEMailSettings emailConfig;
0115     emailConfig.setProfile(emailConfig.defaultProfileName());
0116     QString fromaddr = emailConfig.getSetting(KEMailSettings::EmailAddress);
0117     if (!fromaddr.isEmpty()) {
0118         QString name = emailConfig.getSetting(KEMailSettings::RealName);
0119         if (!name.isEmpty()) {
0120             fromaddr = name + QLatin1String(" <") + fromaddr + QLatin1Char('>');
0121         }
0122     } else {
0123         fromaddr = SystemInformation::userName() + QLatin1Char('@') + QHostInfo::localHostName();
0124     }
0125     // qCDebug(DEBUG_KXMLGUI) << "fromaddr \"" << fromaddr << "\"";
0126 
0127     QString server = emailConfig.getSetting(KEMailSettings::OutServer);
0128     if (server.isEmpty()) {
0129         server = QStringLiteral("bugs.kde.org");
0130     }
0131 
0132     SMTP *sm = new SMTP;
0133     BugMailer bm(sm);
0134 
0135     QObject::connect(sm, &SMTP::messageSent, &bm, &BugMailer::slotSend);
0136     QObject::connect(sm, &SMTP::error, &bm, &BugMailer::slotError);
0137     sm->setServerHost(server);
0138     sm->setPort(25);
0139     sm->setSenderAddress(fromaddr);
0140     sm->setRecipientAddress(recipient);
0141     sm->setMessageSubject(subject);
0142     sm->setMessageHeader(QStringLiteral("From: %1\r\nTo: %2\r\n").arg(fromaddr, recipient));
0143     sm->setMessageBody(text);
0144     sm->sendMessage();
0145 
0146     int r = a.exec();
0147     // qCDebug(DEBUG_KXMLGUI) << "execing " << r;
0148     delete sm;
0149     return r;
0150 }
0151 
0152 #include "moc_main.cpp"