/*
** SellaNMS
** $Id: smtp.h,v 1.5 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_COMMON_SMTP_H
#define __DGS_COMMON_SMTP_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include "template_func.h"
#include <stl/string>
#include <stl/queue>

#define DGS_SMTP_SERVER_DEF	"127.0.0.1"
#define DGS_SMTP_PORT_DEF		25
#define DGS_SMTP_TIMEOUT_DEF	2
#define DGS_SMTP_PRIORITY_DEF	0
#define DGS_SMTP_LENGTH_DEF	0
#define DGS_SMTP_XMAILER_DEF	"DGS SMTP++ v0.9.2 <http://www.digitalgenesis.com>"

/* NOTE:
 *
 * Each call to setX(string) commands adds an additional item to the internal
 * queue. It does not overwrite the old data.
 *
 * setLength() causes an email to be slit into seperate emails by char. Useful
 * for pagers that can only support X chars.
 *
 */

typedef pair<unsigned long, pair<string, int> > smtp_pair_t;
typedef priority_queue<smtp_pair_t, vector<smtp_pair_t>, greater_pair_first<unsigned long, pair<string, int> > > smtp_data_t;

class SMTP {
	public:
		SMTP();
		~SMTP();
		RESULT send();
		void setDebug(bool);
		void setSyslog(bool);
		void setSeverity(int severity);
		void setServer(string);
		void setPort(unsigned int);
		void setTimeout(unsigned int);
		void setPriority(unsigned int);
		void setLength(unsigned int characters); /* 0 Unlimited */
		void setSubject(string);
		void setFrom(string);
		void setReplyTo(string);
		void setTo(string);
		void setCc(string);
		void setBcc(string);
		void setBody(string);
		void clear();
		bool isValid();

	private:
		RESULT _send(int s, smtp_data_t *data);
		bool checkAddress();
		void correctBareLF(string&);
		void cleanString(string&);
		int getResponse(int socket);

		bool debug;
		bool useSyslog;
		bool hasContent;
		bool hasRecipient;
		char buf[2048];
		int length_subject;
		unsigned int port, timeout, length;
		string server;
		int severity;

		smtp_pair_t smtp_pair;
		smtp_data_t *data;
};

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/
