summaryrefslogtreecommitdiffstats
path: root/tdeio/misc/tdesendbugmail/smtp.cpp
blob: 470f24050534b4557ff36ba950f3ec250fddae35 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* $Id$ */

#include <sys/utsname.h>
#include <unistd.h>
#include <stdio.h>

#include <kdebug.h>

#include "smtp.h"

SMTP::SMTP(char *serverhost, unsigned short int port, int timeout)
{
    struct utsname uts;

    serverHost = serverhost;
    hostPort = port;
    timeOut = timeout * 1000;

    senderAddress = "user@example.net";
    recipientAddress = "user@example.net";
    messageSubject = "(no subject)";
    messageBody = "empty";
    messageHeader = "";

    connected = false;
    finished = false;

    sock = 0L;
    state = INIT;
    serverState = NONE;

    uname(&uts);
    domainName = uts.nodename;


    if(domainName.isEmpty())
        domainName = "somemachine.example.net";

    kdDebug() << "SMTP object created" << endl;

    connect(&connectTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(connectTimerTick()));
    connect(&timeOutTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(connectTimedOut()));
    connect(&interactTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(interactTimedOut()));

    // some sendmail will give 'duplicate helo' error, quick fix for now
    connect(this, TQT_SIGNAL(messageSent()), TQT_SLOT(closeConnection()));
}

SMTP::~SMTP()
{
    if(sock){
        delete sock;
        sock = 0L;
    }
    connectTimer.stop();
    timeOutTimer.stop();
}

void SMTP::setServerHost(const TQString& serverhost)
{
    serverHost = serverhost;
}

void SMTP::setPort(unsigned short int port)
{
    hostPort = port;
}

void SMTP::setTimeOut(int timeout)
{
    timeOut = timeout;
}

void SMTP::setSenderAddress(const TQString& sender)
{
    senderAddress = sender;
    int index = senderAddress.find('<');
    if (index == -1)
        return;
    senderAddress = senderAddress.mid(index + 1);
    index =  senderAddress.find('>');
    if (index != -1)
        senderAddress = senderAddress.left(index);
    senderAddress = senderAddress.simplifyWhiteSpace();
    while (1) {
        index =  senderAddress.find(' ');
        if (index != -1)
            senderAddress = senderAddress.mid(index + 1); // take one side
        else
            break;
    }
    index = senderAddress.find('@');
    if (index == -1)
        senderAddress.append("@localhost"); // won't go through without a local mail system

}

void SMTP::setRecipientAddress(const TQString& recipient)
{
    recipientAddress = recipient;
}

void SMTP::setMessageSubject(const TQString& subject)
{
    messageSubject = subject;
}

void SMTP::setMessageBody(const TQString& message)
{
    messageBody = message;
}

void SMTP::setMessageHeader(const TQString &header)
{
    messageHeader = header;
}

void SMTP::openConnection(void)
{
    kdDebug() << "started connect timer" << endl;
    connectTimer.start(100, true);
}

void SMTP::closeConnection(void)
{
    socketClose(sock);
}

void SMTP::sendMessage(void)
{
	if(!connected)
		connectTimerTick();
	if(state == FINISHED && connected){
		kdDebug() << "state was == FINISHED\n" << endl;
		finished = false;
		state = IN;
		writeString = TQString::fromLatin1("helo %1\r\n").arg(domainName);
		if (write(sock->socket(), writeString.ascii(), writeString.length()) < 0) {
			// Error
		}
	}
	if(connected){
		kdDebug() << "enabling read on sock...\n" << endl;
		interactTimer.start(timeOut, true);
		sock->enableRead(true);
	}
}
#include <stdio.h>

void SMTP::connectTimerTick(void)
{
    connectTimer.stop();
//    timeOutTimer.start(timeOut, true);

    kdDebug() << "connectTimerTick called..." << endl;

    if(sock){
        delete sock;
        sock = 0L;
    }

    kdDebug() << "connecting to " << serverHost << ":" << hostPort << " ..... " << endl;
    sock = new TDESocket(serverHost.ascii(), hostPort);

    if(sock == 0L || sock->socket() < 0) {
        timeOutTimer.stop();
        kdDebug() << "connection failed!" << endl;
        socketClose(sock);
        emit error(CONNECTERROR);
        connected = false;
        return;
    }
    connected = true;
    finished = false;
    state = INIT;
    serverState = NONE;

    connect(sock, TQT_SIGNAL(readEvent(TDESocket *)), this, TQT_SLOT(socketRead(TDESocket *)));
    connect(sock, TQT_SIGNAL(closeEvent(TDESocket *)), this, TQT_SLOT(socketClose(TDESocket *)));
    //    sock->enableRead(true);
    timeOutTimer.stop();
    kdDebug() << "connected" << endl;
}

void SMTP::connectTimedOut(void)
{
    timeOutTimer.stop();

    if(sock)
	sock->enableRead(false);
    kdDebug() << "socket connection timed out" << endl;
    socketClose(sock);
    emit error(CONNECTTIMEOUT);
}

void SMTP::interactTimedOut(void)
{
    interactTimer.stop();

    if(sock)
        sock->enableRead(false);
    kdDebug() << "time out waiting for server interaction" << endl;
    socketClose(sock);
    emit error(INTERACTTIMEOUT);
}

void SMTP::socketRead(TDESocket *socket)
{
    int n, nl;

    kdDebug() << "socketRead() called..." << endl;
    interactTimer.stop();

    if(socket == 0L || socket->socket() < 0)
        return;
    n = read(socket->socket(), readBuffer, SMTP_READ_BUFFER_SIZE-1 );

    if(n < 0)
        return;

    readBuffer[n] = '\0';
    lineBuffer += readBuffer;
    nl = lineBuffer.find('\n');
    if(nl == -1)
        return;
    lastLine = lineBuffer.left(nl);
    lineBuffer = lineBuffer.right(lineBuffer.length() - nl - 1);
    processLine(&lastLine);
    if(connected)
        interactTimer.start(timeOut, true);
}

void SMTP::socketClose(TDESocket *socket)
{
    timeOutTimer.stop();
    disconnect(sock, TQT_SIGNAL(readEvent(TDESocket *)), this, TQT_SLOT(socketRead(TDESocket *)));
    disconnect(sock, TQT_SIGNAL(closeEvent(TDESocket *)), this, TQT_SLOT(socketClose(TDESocket *)));
    socket->enableRead(false);
    kdDebug() << "connection terminated" << endl;
    connected = false;
    if(socket){
        delete socket;
        socket = 0L;
        sock = 0L;
    }
    emit connectionClosed();
}

void SMTP::processLine(TQString *line)
{
	int i, stat;
	TQString tmpstr;

	i = line->find(' ');
	tmpstr = line->left(i);
	if(i > 3)
		kdDebug() << "warning: SMTP status code longer then 3 digits: " << tmpstr << endl;
	stat = tmpstr.toInt();
	serverState = (SMTPServerStatus)stat;
	lastState = state;

	kdDebug() << "smtp state: [" << stat << "][" << *line << "]" << endl;

	switch(stat){
	case GREET:     //220
		state = IN;
		writeString = TQString::fromLatin1("helo %1\r\n").arg(domainName);
		kdDebug() << "out: " << writeString << endl;
		if (write(sock->socket(), writeString.ascii(), writeString.length()) < 0) {
			// Error
			state = CERROR;
			kdDebug() << "smtp error (write failed)" << endl;
			socketClose(sock);
			emit error(COMMAND);
		}
		break;
	case GOODBYE:   //221
		state = QUIT;
		break;
	case SUCCESSFUL://250
		switch(state) {
			case IN:
				state = READY;
				writeString = TQString::fromLatin1("mail from: %1\r\n").arg(senderAddress);
				kdDebug() << "out: " << writeString << endl;
				if (write(sock->socket(), writeString.ascii(), writeString.length()) < 0) {
					// Error
					state = CERROR;
					kdDebug() << "smtp error (write failed)" << endl;
					socketClose(sock);
					emit error(COMMAND);
				}
				break;
			case READY:
				state = SENTFROM;
				writeString = TQString::fromLatin1("rcpt to: %1\r\n").arg(recipientAddress);
				kdDebug() << "out: " << writeString << endl;
				if (write(sock->socket(), writeString.ascii(), writeString.length()) < 0) {
					// Error
					state = CERROR;
					kdDebug() << "smtp error (write failed)" << endl;
					socketClose(sock);
					emit error(COMMAND);
				}
				break;
			case SENTFROM:
				state = SENTTO;
				writeString = TQString::fromLatin1("data\r\n");
				kdDebug() << "out: " << writeString << endl;
				if (write(sock->socket(), writeString.ascii(), writeString.length()) < 0) {
					// Error
					state = CERROR;
					kdDebug() << "smtp error (write failed)" << endl;
					socketClose(sock);
					emit error(COMMAND);
				}
				break;
			case DATA:
				state = FINISHED;
				finished = true;
				sock->enableRead(false);
				emit messageSent();
				break;
			default:
				state = CERROR;
				kdDebug() << "smtp error (state error): [" << lastState << "]:[" << stat << "][" << *line << "]" << endl;
				socketClose(sock);
				emit error(COMMAND);
				break;
		}
		break;
	case READYDATA: //354
		state = DATA;
		writeString = TQString::fromLatin1("Subject: %1\r\n").arg(messageSubject);
		writeString += messageHeader;
		writeString += "\r\n";
		writeString += messageBody;
		writeString += TQString::fromLatin1(".\r\n");
		kdDebug() << "out: " << writeString;
		if (write(sock->socket(), writeString.ascii(), writeString.length()) < 0) {
			// Error
			state = CERROR;
			kdDebug() << "smtp error (write failed)" << endl;
			socketClose(sock);
			emit error(COMMAND);
		}
		break;
	case ERROR:     //501
		state = CERROR;
		kdDebug() << "smtp error (command error): [" << lastState << "]:[" << stat << "][" << *line << "]\n" << endl;
		socketClose(sock);
		emit error(COMMAND);
		break;
	case UNKNOWN:   //550
		state = CERROR;
		kdDebug() << "smtp error (unknown user): [" << lastState << "]:[" << stat << "][" << *line << "]" << endl;
		socketClose(sock);
		emit error(UNKNOWNUSER);
		break;
	default:
		state = CERROR;
		kdDebug() << "unknown response: [" << lastState << "]:[" << stat << "][" << *line << "]" << endl;
		socketClose(sock);
		emit error(UNKNOWNRESPONSE);
	}
}

#include "smtp.moc"