/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec 
<cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

// QtSmtp.h: interface for the QtSmtp class.
//
//////////////////////////////////////////////////////////////////////

#ifndef QTSMTP_H
#define QTSMTP_H

#include "QtTelnet.h"	// Added by ClassView
#include "MessageStructs.h"

//////////////////////////////////////////////////////////////////////
// structures
//////////////////////////////////////////////////////////////////////

struct ServerOptions
{
	bool authRequired;
	bool authLogin;

	ServerOptions()
	{
		clear();
	}

	void clear()
	{
		authRequired = false;
		authLogin = false;
	}
};


//////////////////////////////////////////////////////////////////////
// class QtSmtp
//////////////////////////////////////////////////////////////////////

class QtSmtp : public QObject  
{
	Q_OBJECT

protected:
	enum SmtpState
	{
		DISCONNECTED,	// proper server responses
		CONNECTED,		// 220
		EHLO,			// 250
		AUTHLOGIN,		// 334
		USER,			// 334
		PASSWORD,		// 335
		MAILFROM,		// 250
		RCPT,			// 250
		CC,				// 250
		DATA,			// 354
		END,			// 250
	};

public:
	void beginNewMail();
	void setUserPassword(const QString user, const QString password);
	void setSubject(const QString subject);
	void setMsgId(const QString msgId);
	void setMessage(const QString message);
	void sendMail();
	void addAttachment(const QByteArray &content, const QString filename);
	void addFileAttachment(const QString filename);
	void addRecipient(const QString recipient, const QString name = "");
	void addCC(const QString recipient, const QString name = "");
	void setSender(const QString sender, const QString name = "");
	QtSmtp(QObject *parent = 0);
	virtual ~QtSmtp();
	void setServer(const QString host);

signals:
	void errorOccured(const QString &error);
	void messageSend();
	void progressStep();

protected slots:
	void telnetMessage(const QStringList &lines);
	void telnetConnectionError(const QString &error);
	void telnetConnected();

protected:
	void getStoredIDs();
	void sendMultipart();
	void sendToRecipients();
	void sendToCC();
	void checkOptions(const QStringList &lines);
	bool checkResponse(const QStringList &lines, int okValue);
	void processSending(const QStringList &lines);
	SmtpState mState;
	ServerOptions mServerOptions;
	QString mUser;
	QString mPassword;
	QString mHost;
	UserItem mSender;
	QList<UserItem> mRcpt;
	int mRcptIndex;
	QList<UserItem> mCC;
	int mCCIndex;
	QString mMessage;
	QString mSubject;
	QString mMsgID;
	QList<AttachmentItem> mAttachments;
	QtTelnet *mTelnet;
};

#endif
