
/*
MIME Message formatting SAM for smtp sending (or IMAP upload) - designed to be generally used 
	in conjuction with prcc.c and smail.c 

(used to be part of surgeweb level code, now also used by surgealert)

Example of use:
*/

#include "tcplib.h"
#include "defines.h"
#include "lib.h"

typedef struct SFORMAT {
	int r;
	char sep_mix[BFSZ],sep_mpa[BFSZ],sep_mpr[BFSZ];
	FILE *f_txt,*f_html;
	int n_txt, flowed_txt, enc_txt;	// Encodings of the text part: charset(ascii/utf8), format=flowed (y/n), transfer encoding(none/qp/base64)
	int n_html;
	char *attach_ids;
	int attach, text_only, related;
	int generate_subject;
	char generated_subject[BFSZ];
	void *data;									// user custom data pointer
	int cal;
	int cloudfile_top;
	// Access to various parameters (used to be part of Sw*)
	Pair *param;
	Pair *rcpts_to, *rcpts_cc, *rcpts_bcc, *cid_index, *cloudfiles;		// readonly pointers only DO NOT FREE!!
	Wordlist *cids_found;												// readonly pointers only DO NOT FREE!!
} SFormat;

enum {SF_ATTACH,SF_MPA,SF_MPR};

void sformat_set_imsg(int (*fn)(const char *, ...));
void sformat_set_tmpfile(char* (*fn)(void*,char *));
void sformat_set_decode_filename(char* (*fn)(void*,char*));
void sformat_set_wash_fordisplay_html(FILE* (*fn)(void *, FILE *, int, int, Pair *));


int sformat_new(SFormat **sf);
int sformat_init(SFormat *sf, char *type);
int sformat_free(SFormat **sf);

int sformat_recipient_header(FILE *f,char *hdr, Pair* rcpts);
char *sformat_encode_rfc1522(char *src, char *dst);
char *sformat_get_content_type(SFormat *sf, char *bf, int level);

int sformat_message_header(SFormat *sf, FILE *f2,char *mime_header, int *fstart);
int sformat_message_textpart(SFormat *sf, FILE *f2, char *separator, int do_headers, char *result);
int sformat_prepare_message_textpart(SFormat *sf, char *body, char *result);
int sformat_prepare_message_textparts_mpa(SFormat *sf, char *body, char *result);

int sformat_whole_message(SFormat *sf, FILE **f_out, char *result);
int sformat_message_attachments(SFormat *sf, FILE *f2, char *part_separator, char *attach_ids, int *fail_count, char *result);
int sformat_message_attachments_files(SFormat *sf, FILE *f2, char *part_separator, char *attach_files, int *fail_count);

int sformat_message_relateds(SFormat *sf, FILE *f2, char *part_separator, char *attach_ids, int *fail_count, char *result);
int sformat_message_htmlpart(SFormat *sf, FILE *f2, char *separator, char *result);

