KIO
main.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "main.h"
00021 #include <sys/types.h>
00022 #include <pwd.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025
00026 #include <QtCore/QTextStream>
00027
00028 #include <kapplication.h>
00029 #include <kemailsettings.h>
00030 #include <klocale.h>
00031 #include <kcmdlineargs.h>
00032 #include <kaboutdata.h>
00033 #include <kdebug.h>
00034 #include <kconfig.h>
00035
00036 #include "smtp.h"
00037
00038 void BugMailer::slotError(int errornum) {
00039 kDebug() << "slotError\n";
00040 QString str, lstr;
00041
00042 switch(errornum) {
00043 case SMTP::ConnectError:
00044 lstr = i18n("Error connecting to server.");
00045 break;
00046 case SMTP::NotConnected:
00047 lstr = i18n("Not connected.");
00048 break;
00049 case SMTP::ConnectTimeout:
00050 lstr = i18n("Connection timed out.");
00051 break;
00052 case SMTP::InteractTimeout:
00053 lstr = i18n("Time out waiting for server interaction.");
00054 break;
00055 default:
00056 lstr = sm->getLastLine().trimmed();
00057 lstr = i18n("Server said: \"%1\"", lstr);
00058 }
00059 fputs(lstr.toUtf8().data(), stdout);
00060 fflush(stdout);
00061
00062 ::exit(1);
00063 }
00064
00065 void BugMailer::slotSend() {
00066 kDebug() << "slotSend\n";
00067 ::exit(0);
00068 }
00069
00070 int main(int argc, char **argv) {
00071
00072 KAboutData d("ksendbugmail", "kdelibs4", ki18n("KSendBugMail"), "1.0",
00073 ki18n("Sends a short bug report to submit@bugs.kde.org"),
00074 KAboutData::License_GPL, ki18n("(c) 2000 Stephan Kulow"));
00075 d.addAuthor(ki18n("Stephan Kulow"), ki18n("Author"), "coolo@kde.org");
00076
00077 KCmdLineOptions options;
00078 options.add("subject <argument>", ki18n("Subject line"));
00079 options.add("recipient <argument>", ki18n("Recipient"), "submit@bugs.kde.org");
00080
00081 KCmdLineArgs::init(argc, argv, &d);
00082 KCmdLineArgs::addCmdLineOptions(options);
00083 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00084
00085 KApplication a(false);
00086
00087 QString recipient = args->getOption("recipient");
00088 if (recipient.isEmpty())
00089 recipient = "submit@bugs.kde.org";
00090 else {
00091 if (recipient.at(0) == '\'') {
00092 recipient = recipient.mid(1).left(recipient.length() - 2);
00093 }
00094 }
00095 kDebug() << "recp \"" << recipient << "\"\n";
00096
00097 QString subject = args->getOption("subject");
00098 if (subject.isEmpty())
00099 subject = "(no subject)";
00100 else {
00101 if (subject.at(0) == '\'')
00102 subject = subject.mid(1).left(subject.length() - 2);
00103 }
00104 QTextStream input(stdin, QIODevice::ReadOnly);
00105 QString text, line;
00106 while (!input.atEnd()) {
00107 line = input.readLine();
00108 text += line + "\r\n";
00109 }
00110 kDebug() << text;
00111
00112 KEMailSettings emailConfig;
00113 emailConfig.setProfile(emailConfig.defaultProfileName());
00114 QString fromaddr = emailConfig.getSetting(KEMailSettings::EmailAddress);
00115 if (!fromaddr.isEmpty()) {
00116 QString name = emailConfig.getSetting(KEMailSettings::RealName);
00117 if (!name.isEmpty())
00118 fromaddr = name + QLatin1String(" <") + fromaddr + QString::fromLatin1(">");
00119 } else {
00120 struct passwd *p;
00121 p = getpwuid(getuid());
00122 fromaddr = QLatin1String(p->pw_name);
00123 fromaddr += '@';
00124 char buffer[256];
00125 buffer[0] = '\0';
00126 if(!gethostname(buffer, sizeof(buffer)))
00127 buffer[sizeof(buffer)-1] = '\0';
00128 fromaddr += buffer;
00129 }
00130 kDebug() << "fromaddr \"" << fromaddr << "\"";
00131
00132 QString server = emailConfig.getSetting(KEMailSettings::OutServer);
00133 if (server.isEmpty())
00134 server=QLatin1String("bugs.kde.org");
00135
00136 SMTP *sm = new SMTP;
00137 BugMailer bm(sm);
00138
00139 QObject::connect(sm, SIGNAL(messageSent()), &bm, SLOT(slotSend()));
00140 QObject::connect(sm, SIGNAL(error(int)), &bm, SLOT(slotError(int)));
00141 sm->setServerHost(server);
00142 sm->setPort(25);
00143 sm->setSenderAddress(fromaddr);
00144 sm->setRecipientAddress(recipient);
00145 sm->setMessageSubject(subject);
00146 sm->setMessageHeader(QString::fromLatin1("From: %1\r\nTo: %2\r\n").arg(fromaddr).arg(QString(recipient)));
00147 sm->setMessageBody(text);
00148 sm->sendMessage();
00149
00150 int r = a.exec();
00151 kDebug() << "execing " << r;
00152 delete sm;
00153 return r;
00154 }
00155
00156 #include "main.moc"