1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/****************************************************************************
**
** This file is a modified version of part of an example program for Qt.
** This file may be used, distributed and modified without limitation.
**
** Don Sanders <sanders@kde.org>
**
*****************************************************************************/
#include "smtp.h"
#include <qtextstream.h>
#include <qsocket.h>
#include <qtimer.h>
#include <kapplication.h>
#include <kmessagebox.h>
#include <klocale.h>
Smtp::Smtp( const QString &from, const QStringList &to,
const QString &aMessage,
const QString &server,
unsigned short int port )
{
skipReadResponse = false;
mSocket = new QSocket( this );
connect ( mSocket, SIGNAL( readyRead() ),
this, SLOT( readyRead() ) );
connect ( mSocket, SIGNAL( connected() ),
this, SLOT( connected() ) );
connect ( mSocket, SIGNAL( error(int) ),
this, SLOT( socketError(int) ) );
message = aMessage;
this->from = from;
rcpt = to;
state = smtpInit;
command = "";
emit status( i18n( "Connecting to %1" ).arg( server ) );
mSocket->connectToHost( server, port );
t = new QTextStream( mSocket );
t->setEncoding(QTextStream::Latin1);
}
Smtp::~Smtp()
{
if (t)
delete t;
if (mSocket)
delete mSocket;
}
void Smtp::send( const QString &from, const QStringList &to,
const QString &aMessage )
{
skipReadResponse = true;
message = aMessage;
this->from = from;
rcpt = to;
state = smtpMail;
command = "";
readyRead();
}
void Smtp::quit()
{
skipReadResponse = true;
state = smtpQuit;
command = "";
readyRead();
}
void Smtp::connected()
{
emit status( i18n( "Connected to %1" ).arg( mSocket->peerName() ) );
}
void Smtp::socketError(int errorCode)
{
command = "CONNECT";
switch ( errorCode ) {
case QSocket::ErrConnectionRefused:
responseLine = i18n( "Connection refused." );
break;
case QSocket::ErrHostNotFound:
responseLine = i18n( "Host Not Found." );
break;
case QSocket::ErrSocketRead:
responseLine = i18n( "Error reading socket." );
break;
default:
responseLine = i18n( "Internal error, unrecognized error." );
}
QTimer::singleShot( 0, this, SLOT(emitError()) );
}
void Smtp::emitError() {
error( command, responseLine );
}
void Smtp::readyRead()
{
if (!skipReadResponse) {
// SMTP is line-oriented
if ( !mSocket->canReadLine() )
return;
do {
responseLine = mSocket->readLine();
response += responseLine;
} while( mSocket->canReadLine() && responseLine[3] != ' ' );
}
skipReadResponse = false;
if ( state == smtpInit && responseLine[0] == '2' ) {
// banner was okay, let's go on
command = "HELO there";
*t << "HELO there\r\n";
state = smtpMail;
} else if ( state == smtpMail && responseLine[0] == '2' ) {
// HELO response was okay (well, it has to be)
command = "MAIL";
*t << "MAIL FROM: <" << from << ">\r\n";
state = smtpRcpt;
} else if ( state == smtpRcpt && responseLine[0] == '2' && (rcpt.begin() != rcpt.end())) {
command = "RCPT";
*t << "RCPT TO: <" << *(rcpt.begin()) << ">\r\n";
rcpt.remove( rcpt.begin() );
if (rcpt.begin() == rcpt.end())
state = smtpData;
} else if ( state == smtpData && responseLine[0] == '2' ) {
command = "DATA";
*t << "DATA\r\n";
state = smtpBody;
} else if ( state == smtpBody && responseLine[0] == '3' ) {
command = "DATA";
QString seperator = "";
if (message[message.length() - 1] != '\n')
seperator = "\r\n";
*t << message << seperator << ".\r\n";
state = smtpSuccess;
} else if ( state == smtpSuccess && responseLine[0] == '2' ) {
QTimer::singleShot( 0, this, SIGNAL(success()) );
} else if ( state == smtpQuit && responseLine[0] == '2' ) {
command = "QUIT";
*t << "QUIT\r\n";
// here, we just close.
state = smtpClose;
emit status( i18n( "Message sent" ) );
} else if ( state == smtpClose ) {
// we ignore it
} else { // error occurred
QTimer::singleShot( 0, this, SLOT(emitError()) );
state = smtpClose;
}
response = "";
if ( state == smtpClose ) {
delete t;
t = 0;
delete mSocket;
mSocket = 0;
QTimer::singleShot( 0, this, SLOT(deleteMe()) );
}
}
void Smtp::deleteMe()
{
delete this;
}
#include "smtp.moc"
|