KIO
main.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (c) 2000 Bernd Johannes Wuebben <wuebben@math.cornell.edu> 00003 Copyright (c) 2000 Stephan Kulow <coolo@kde.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2, or (at your option) 00008 any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 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 QString lstr; 00040 00041 switch(errornum) { 00042 case SMTP::ConnectError: 00043 lstr = i18n("Error connecting to server."); 00044 break; 00045 case SMTP::NotConnected: 00046 lstr = i18n("Not connected."); 00047 break; 00048 case SMTP::ConnectTimeout: 00049 lstr = i18n("Connection timed out."); 00050 break; 00051 case SMTP::InteractTimeout: 00052 lstr = i18n("Time out waiting for server interaction."); 00053 break; 00054 default: 00055 lstr = sm->getLastLine().trimmed(); 00056 lstr = i18n("Server said: \"%1\"", lstr); 00057 } 00058 kDebug() << lstr; 00059 00060 fputs(lstr.toUtf8().data(), stdout); 00061 fflush(stdout); 00062 00063 qApp->exit(1); 00064 } 00065 00066 void BugMailer::slotSend() { 00067 kDebug(); 00068 qApp->exit(0); 00069 } 00070 00071 int main(int argc, char **argv) { 00072 00073 KAboutData d("ksendbugmail", "kdelibs4", ki18n("KSendBugMail"), "1.0", 00074 ki18n("Sends a bug report by email"), 00075 KAboutData::License_GPL, ki18n("(c) 2000 Stephan Kulow")); 00076 d.addAuthor(ki18n("Stephan Kulow"), ki18n("Author"), "coolo@kde.org"); 00077 00078 KCmdLineOptions options; 00079 options.add("subject <argument>", ki18n("Subject line")); 00080 options.add("recipient <argument>", ki18n("Recipient"), "submit@bugs.kde.org"); 00081 00082 KCmdLineArgs::init(argc, argv, &d); 00083 KCmdLineArgs::addCmdLineOptions(options); 00084 KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); 00085 00086 KApplication a(false); 00087 00088 QString recipient = args->getOption("recipient"); 00089 if (recipient.isEmpty()) 00090 recipient = "submit@bugs.kde.org"; 00091 else { 00092 if (recipient.at(0) == '\'') { 00093 recipient = recipient.mid(1).left(recipient.length() - 2); 00094 } 00095 } 00096 kDebug() << "recp" << recipient; 00097 00098 QString subject = args->getOption("subject"); 00099 if (subject.isEmpty()) 00100 subject = "(no subject)"; 00101 else { 00102 if (subject.at(0) == '\'') 00103 subject = subject.mid(1).left(subject.length() - 2); 00104 } 00105 QTextStream input(stdin, QIODevice::ReadOnly); 00106 input.setCodec("UTF-8"); 00107 QString text, line; 00108 while (!input.atEnd()) { 00109 line = input.readLine(); 00110 text += line + "\r\n"; 00111 } 00112 kDebug() << text; 00113 00114 KEMailSettings emailConfig; 00115 emailConfig.setProfile(emailConfig.defaultProfileName()); 00116 QString fromaddr = emailConfig.getSetting(KEMailSettings::EmailAddress); 00117 if (!fromaddr.isEmpty()) { 00118 QString name = emailConfig.getSetting(KEMailSettings::RealName); 00119 if (!name.isEmpty()) 00120 fromaddr = name + QLatin1String(" <") + fromaddr + QString::fromLatin1(">"); 00121 } else { 00122 struct passwd *p; 00123 p = getpwuid(getuid()); 00124 fromaddr = QLatin1String(p->pw_name); 00125 fromaddr += '@'; 00126 char buffer[256]; 00127 buffer[0] = '\0'; 00128 if(!gethostname(buffer, sizeof(buffer))) 00129 buffer[sizeof(buffer)-1] = '\0'; 00130 fromaddr += buffer; 00131 } 00132 kDebug() << "fromaddr \"" << fromaddr << "\""; 00133 00134 QString server = emailConfig.getSetting(KEMailSettings::OutServer); 00135 if (server.isEmpty()) 00136 server=QLatin1String("bugs.kde.org"); 00137 00138 SMTP *sm = new SMTP; 00139 BugMailer bm(sm); 00140 00141 QObject::connect(sm, SIGNAL(messageSent()), &bm, SLOT(slotSend())); 00142 QObject::connect(sm, SIGNAL(error(int)), &bm, SLOT(slotError(int))); 00143 sm->setServerHost(server); 00144 sm->setPort(25); 00145 sm->setSenderAddress(fromaddr); 00146 sm->setRecipientAddress(recipient); 00147 sm->setMessageSubject(subject); 00148 sm->setMessageHeader(QString::fromLatin1("From: %1\r\nTo: %2\r\n").arg(fromaddr).arg(QString(recipient))); 00149 sm->setMessageBody(text); 00150 sm->sendMessage(); 00151 00152 int r = a.exec(); 00153 kDebug() << "execing " << r; 00154 delete sm; 00155 return r; 00156 } 00157 00158 #include "main.moc"
KDE 4.6 API Reference