00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "smtpclient.h"
00021 #include "emailhandler.h"
00022
00023 SmtpClient::SmtpClient()
00024 {
00025 socket = new QSocket(this, "smtpClient");
00026 stream = new QTextStream(socket);
00027 mailList.setAutoDelete(TRUE);
00028
00029 connect(socket, SIGNAL(error(int)), this, SLOT(errorHandling(int)));
00030 connect(socket, SIGNAL(connected()), this, SLOT(connectionEstablished()));
00031 connect(socket, SIGNAL(readyRead()), this, SLOT(incomingData()));
00032
00033 sending = FALSE;
00034 }
00035
00036 SmtpClient::~SmtpClient()
00037 {
00038 delete socket;
00039 delete stream;
00040 }
00041
00042 void SmtpClient::newConnection(const QString &target, int port)
00043 {
00044 if (sending) {
00045 qWarning("socket in use, connection refused");
00046 return;
00047 }
00048
00049 status = Init;
00050 sending = TRUE;
00051 socket->connectToHost(target, port);
00052
00053 emit updateStatus(tr("DNS lookup"));
00054 }
00055
00056 void SmtpClient::addMail(const QString &from, const QString &subject, const QStringList &to, const QString &body)
00057 {
00058 RawEmail *mail = new RawEmail;
00059
00060 mail->from = from;
00061 mail->subject = subject;
00062 mail->to = to;
00063 mail->body = body;
00064
00065 mailList.append(mail);
00066 }
00067
00068 void SmtpClient::connectionEstablished()
00069 {
00070 emit updateStatus(tr("Connection established"));
00071
00072 }
00073
00074 void SmtpClient::errorHandling(int status)
00075 {
00076 errorHandlingWithMsg( status, QString::null );
00077 }
00078
00079 void SmtpClient::errorHandlingWithMsg(int status, const QString & EMsg )
00080 {
00081 emit errorOccurred(status, EMsg );
00082 socket->close();
00083 mailList.clear();
00084 sending = FALSE;
00085 }
00086
00087 void SmtpClient::incomingData()
00088 {
00089 QString response;
00090
00091 if (!socket->canReadLine())
00092 return;
00093
00094 response = socket->readLine();
00095 switch(status) {
00096 case Init: {
00097 if (response[0] == '2') {
00098 status = From;
00099 mailPtr = mailList.first();
00100 *stream << "HELO there\r\n";
00101 } else errorHandlingWithMsg(ErrUnknownResponse,response);
00102 break;
00103 }
00104 case From: {
00105 if (response[0] == '2') {
00106 qDebug(mailPtr->from);
00107 *stream << "MAIL FROM: " << mailPtr->from << "\r\n";
00108 status = Recv;
00109 } else errorHandlingWithMsg(ErrUnknownResponse, response );
00110 break;
00111 }
00112 case Recv: {
00113 if (response[0] == '2') {
00114 it = mailPtr->to.begin();
00115 if (it == NULL) {
00116 errorHandlingWithMsg(ErrUnknownResponse,response);
00117 }
00118 *stream << "RCPT TO: " << *it << "\r\n";
00119 status = MRcv;
00120 } else errorHandlingWithMsg(ErrUnknownResponse,response);
00121 break;
00122 }
00123 case MRcv: {
00124 if (response[0] == '2') {
00125 it++;
00126 if ( it != mailPtr->to.end() ) {
00127 *stream << "RCPT TO: " << *it << "\r\n";
00128 break;
00129 } else {
00130 status = Data;
00131 }
00132 } else errorHandlingWithMsg(ErrUnknownResponse,response);
00133 }
00134 case Data: {
00135 if (response[0] == '2') {
00136 *stream << "DATA\r\n";
00137 status = Body;
00138 emit updateStatus(tr("Sending: ") + mailPtr->subject);
00139
00140 } else errorHandlingWithMsg(ErrUnknownResponse,response);
00141 break;
00142 }
00143 case Body: {
00144 if (response[0] == '3') {
00145 *stream << mailPtr->body << "\r\n.\r\n";
00146 mailPtr = mailList.next();
00147 if (mailPtr != NULL) {
00148 status = From;
00149 } else {
00150 status = Quit;
00151 }
00152 } else errorHandlingWithMsg(ErrUnknownResponse,response);
00153 break;
00154 }
00155 case Quit: {
00156 if (response[0] == '2') {
00157 *stream << "QUIT\r\n";
00158 status = Done;
00159 QString temp;
00160 temp.setNum(mailList.count());
00161 emit updateStatus(tr("Sent ") + temp + tr(" messages"));
00162 emit mailSent();
00163 mailList.clear();
00164 sending = FALSE;
00165 socket->close();
00166 } else errorHandlingWithMsg(ErrUnknownResponse,response);
00167 break;
00168 }
00169 }
00170 }