kio Library: main.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# misc/ksendbugmail/main.cpp

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

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:06 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003