You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
337 lines
8.9 KiB
337 lines
8.9 KiB
15 years ago
|
/* $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;
|
||
|
|
||
15 years ago
|
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()));
|
||
15 years ago
|
|
||
|
// some sendmail will give 'duplicate helo' error, quick fix for now
|
||
15 years ago
|
connect(this, TQT_SIGNAL(messageSent()), TQT_SLOT(closeConnection()));
|
||
15 years ago
|
}
|
||
|
|
||
|
SMTP::~SMTP()
|
||
|
{
|
||
|
if(sock){
|
||
|
delete sock;
|
||
|
sock = 0L;
|
||
|
}
|
||
|
connectTimer.stop();
|
||
|
timeOutTimer.stop();
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::setServerHost(const TQString& serverhost)
|
||
15 years ago
|
{
|
||
|
serverHost = serverhost;
|
||
|
}
|
||
|
|
||
|
void SMTP::setPort(unsigned short int port)
|
||
|
{
|
||
|
hostPort = port;
|
||
|
}
|
||
|
|
||
|
void SMTP::setTimeOut(int timeout)
|
||
|
{
|
||
|
timeOut = timeout;
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::setSenderAddress(const TQString& sender)
|
||
15 years ago
|
{
|
||
|
senderAddress = sender;
|
||
14 years ago
|
int index = senderAddress.find('<');
|
||
15 years ago
|
if (index == -1)
|
||
|
return;
|
||
|
senderAddress = senderAddress.mid(index + 1);
|
||
14 years ago
|
index = senderAddress.find('>');
|
||
15 years ago
|
if (index != -1)
|
||
|
senderAddress = senderAddress.left(index);
|
||
|
senderAddress = senderAddress.simplifyWhiteSpace();
|
||
|
while (1) {
|
||
14 years ago
|
index = senderAddress.find(' ');
|
||
15 years ago
|
if (index != -1)
|
||
|
senderAddress = senderAddress.mid(index + 1); // take one side
|
||
|
else
|
||
|
break;
|
||
|
}
|
||
14 years ago
|
index = senderAddress.find('@');
|
||
15 years ago
|
if (index == -1)
|
||
|
senderAddress.append("@localhost"); // won't go through without a local mail system
|
||
|
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::setRecipientAddress(const TQString& recipient)
|
||
15 years ago
|
{
|
||
|
recipientAddress = recipient;
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::setMessageSubject(const TQString& subject)
|
||
15 years ago
|
{
|
||
|
messageSubject = subject;
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::setMessageBody(const TQString& message)
|
||
15 years ago
|
{
|
||
|
messageBody = message;
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::setMessageHeader(const TQString &header)
|
||
15 years ago
|
{
|
||
|
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;
|
||
13 years ago
|
writeString = TQString::fromLatin1("helo %1\r\n").arg(domainName);
|
||
15 years ago
|
write(sock->socket(), writeString.ascii(), writeString.length());
|
||
|
}
|
||
|
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;
|
||
12 years ago
|
sock = new TDESocket(serverHost.ascii(), hostPort);
|
||
15 years ago
|
|
||
|
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;
|
||
|
|
||
12 years ago
|
connect(sock, TQT_SIGNAL(readEvent(TDESocket *)), this, TQT_SLOT(socketRead(TDESocket *)));
|
||
|
connect(sock, TQT_SIGNAL(closeEvent(TDESocket *)), this, TQT_SLOT(socketClose(TDESocket *)));
|
||
15 years ago
|
// 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);
|
||
|
}
|
||
|
|
||
12 years ago
|
void SMTP::socketRead(TDESocket *socket)
|
||
15 years ago
|
{
|
||
|
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;
|
||
14 years ago
|
nl = lineBuffer.find('\n');
|
||
15 years ago
|
if(nl == -1)
|
||
|
return;
|
||
|
lastLine = lineBuffer.left(nl);
|
||
|
lineBuffer = lineBuffer.right(lineBuffer.length() - nl - 1);
|
||
|
processLine(&lastLine);
|
||
|
if(connected)
|
||
|
interactTimer.start(timeOut, true);
|
||
|
}
|
||
|
|
||
12 years ago
|
void SMTP::socketClose(TDESocket *socket)
|
||
15 years ago
|
{
|
||
|
timeOutTimer.stop();
|
||
12 years ago
|
disconnect(sock, TQT_SIGNAL(readEvent(TDESocket *)), this, TQT_SLOT(socketRead(TDESocket *)));
|
||
|
disconnect(sock, TQT_SIGNAL(closeEvent(TDESocket *)), this, TQT_SLOT(socketClose(TDESocket *)));
|
||
15 years ago
|
socket->enableRead(false);
|
||
|
kdDebug() << "connection terminated" << endl;
|
||
|
connected = false;
|
||
|
if(socket){
|
||
|
delete socket;
|
||
|
socket = 0L;
|
||
|
sock = 0L;
|
||
|
}
|
||
|
emit connectionClosed();
|
||
|
}
|
||
|
|
||
15 years ago
|
void SMTP::processLine(TQString *line)
|
||
15 years ago
|
{
|
||
|
int i, stat;
|
||
15 years ago
|
TQString tmpstr;
|
||
15 years ago
|
|
||
14 years ago
|
i = line->find(' ');
|
||
15 years ago
|
tmpstr = line->left(i);
|
||
|
if(i > 3)
|
||
|
kdDebug() << "warning: SMTP status code longer then 3 digits: " << tmpstr << endl;
|
||
|
stat = tmpstr.toInt();
|
||
14 years ago
|
serverState = (SMTPServerStatus)stat;
|
||
15 years ago
|
lastState = state;
|
||
|
|
||
|
kdDebug() << "smtp state: [" << stat << "][" << *line << "]" << endl;
|
||
|
|
||
|
switch(stat){
|
||
|
case GREET: //220
|
||
|
state = IN;
|
||
13 years ago
|
writeString = TQString::fromLatin1("helo %1\r\n").arg(domainName);
|
||
15 years ago
|
kdDebug() << "out: " << writeString << endl;
|
||
|
write(sock->socket(), writeString.ascii(), writeString.length());
|
||
|
break;
|
||
|
case GOODBYE: //221
|
||
|
state = QUIT;
|
||
|
break;
|
||
|
case SUCCESSFUL://250
|
||
|
switch(state){
|
||
|
case IN:
|
||
|
state = READY;
|
||
13 years ago
|
writeString = TQString::fromLatin1("mail from: %1\r\n").arg(senderAddress);
|
||
15 years ago
|
kdDebug() << "out: " << writeString << endl;
|
||
|
write(sock->socket(), writeString.ascii(), writeString.length());
|
||
|
break;
|
||
|
case READY:
|
||
|
state = SENTFROM;
|
||
13 years ago
|
writeString = TQString::fromLatin1("rcpt to: %1\r\n").arg(recipientAddress);
|
||
15 years ago
|
kdDebug() << "out: " << writeString << endl;
|
||
|
write(sock->socket(), writeString.ascii(), writeString.length());
|
||
|
break;
|
||
|
case SENTFROM:
|
||
|
state = SENTTO;
|
||
13 years ago
|
writeString = TQString::fromLatin1("data\r\n");
|
||
15 years ago
|
kdDebug() << "out: " << writeString << endl;
|
||
|
write(sock->socket(), writeString.ascii(), writeString.length());
|
||
|
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;
|
||
13 years ago
|
writeString = TQString::fromLatin1("Subject: %1\r\n").arg(messageSubject);
|
||
15 years ago
|
writeString += messageHeader;
|
||
|
writeString += "\r\n";
|
||
|
writeString += messageBody;
|
||
13 years ago
|
writeString += TQString::fromLatin1(".\r\n");
|
||
15 years ago
|
kdDebug() << "out: " << writeString;
|
||
|
write(sock->socket(), writeString.ascii(), writeString.length());
|
||
|
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"
|