#ifndef _SmsSmtp_Header
#define _SmsSmtp_Header
#include "smslib.h"
#include "tcplib.h"
#include "lib.h"

struct sms_smtp;
typedef struct sms_smtp SmsSmtp;

typedef struct smtp_message {
	char message[BFSZ],*from,*ip,*name,*file;
	int upto,gotheaders,needreply;
	Pair *headers;
	Wordlist *to;
	Chan *sock;
	FILE *f;
} SmtpMessage;

typedef struct smtp_init {
	char *sendhost,*greeting,*smtplog,*workpath,*todom,*fromdom;
	int listenport,sendport;
} SmtpInit;

typedef struct smtp_message_extra {
	Pair *headers;
} MessageExtra;

SmsSmtp *sms_smtp_init(Sms *sms, SmtpInit *init, int *errcode);
void sms_smtp_free(SmsSmtp **smtp);

SmtpMessage *sms_smtp_wait(SmsSmtp *smtp, long timeout_msec, int *errcode);

int sms_smtp_send(SmsSmtp *smtp, MessageExtra *extra, char *from, char *to, char *message);

/* will read message up to just after MAIL FROM: */
int sms_smtp_recv(SmsSmtp *smtp, SmtpMessage *message);

/* will read rest of message after MAIL FROM: */
/* code should be 2xx i.e. 250 */
int sms_smtp_accept(SmsSmtp *smtp, SmtpMessage *message, int code, char *reason);

/* will reply with an error to message */
/* code should be 5xx i.e. 500 */
int sms_smtp_reject(SmsSmtp *smtp, SmtpMessage *message, int code, char *reason);

char *sms_smtp_error(SmsSmtp *smtp, int errcode, char *bf, int bfsz);
char *sms_smtp_fromip(SmsSmtp *smtp, SmtpMessage *msg);

/* will read message if not already done so up to just after MAIL FROM: */
char *sms_smtp_from(SmsSmtp *smtp, SmtpMessage *msg);
char *sms_smtp_to(SmsSmtp *smtp, SmtpMessage *msg);
char *sms_smtp_body(SmsSmtp *smtp, SmtpMessage *msg);

void sms_smtp_msgfree(SmsSmtp *smtp, SmtpMessage **msg);
void sms_smtp_remove_to(SmsSmtp *smtp, SmtpMessage *msg);

MessageExtra *sms_smtp_msgextra(SmtpMessage *parse);
void sms_smtp_msgextra_free(MessageExtra **e);


int qp_header_encode(char *header, char *text, char *bf, int bfsz);
int qp_header_decode(char *text, char *bf, int bfsz);

#define SMS_SMTP_OPEN		SMS_NO_ERROR+1
#define SMS_SMTP_LISTEN		SMS_NO_ERROR+2
#define SMS_SMTP_READ		SMS_NO_ERROR+3
#define SMS_SMTP_WRITE		SMS_NO_ERROR+4
#define SMS_SMTP_CLOSED		SMS_NO_ERROR+5
#define SMS_SMTP_PROTOCOL	SMS_NO_ERROR+6
#define SMS_SMTP_FILE		SMS_NO_ERROR+7
#endif
