// ----------------------------------------------------
// surge addressbook sam - for surgealert & surgeonline
// author: M Weehuizen, April 2011
// ----------------------------------------------------

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "lib.h"
#include "debug.h"
#include "sabook.h"
#include "sformat.h"




//static char* (*sab_tmpfile)(void*,char *);
//void sab_set_tmpfile(char* (*fn)(void*,char *)){
//	sab_tmpfile = fn;
//}

// Functionality

/*
From:
To:
Date:
Subject:
MessageID:
X-Surge-Contact:
MIME Version
multipart-mixed

-- text part

This is a contact definition file saved by SurgeAlert.

-- attachment part
vcard file content

-- end parts

*/

/*
// write file to be IMAP appended as a contact message
FILE *sab_message_file(Kpair *fields)
{
	FILE *f_out, *f;
	SFormat *sf;
	int ret;

	// save parameters as a vcard file
	f=fopen(sab_tmpfile(sw, fname),"w+b");
	if(f==NULL) return NULL;
	abk_vcard_ouput(f, cid, fields, outname);

	sformat_new(&sf);
	sformat_init(sf,"SA");
	sab_prepare_sformat(sw,sf);
	ret = sformat_whole_message(sf, &f_out, result);
	sformat_free(&sf);

	return f_out;
}

int sab_prepare_sformat(Sw *sw, SFormat *sf)
{
	char bf[BFSZ],*s,*upto;
	int att=0;

	sf->attach_ids=web_input(sw->w,"attach_ids");	// <<< now includes both std + mpr attachments
	sf->attach = TRUE;
	sf->text_only = TRUE;

	// pointers to data sources - these do not get freed by sformat!!
	sf->rcpts_to = sw->rcpts_to;
	sf->data = sw;

	// Addressing options:
	pair_add(&sf->param,"domain",sw->ses->domain);							
	pair_add(&sf->param,"from_address",web_var_find(sw->w,"from_address"));	
	pair_add(&sf->param,"from_rfc1522",web_var_find(sw->w,"from_rfc1522")); 
	pair_add(&sf->param,"subject","Marijn Weehuizen contact information (marijn@netwinsite.com)");			
	pair_add(&sf->param,"xcontact","123|marijn@netwinsite.com|Me|Marijn|Weehuizen");
	pair_add(&sf->param,"mailer","SurgeAlert - Mail and Messaging Client");

	// Behaviour / formatting options:
	pair_add(&sf->param,"msg_body",web_input(sw->w,"msg_body"));
	pair_add(&sf->param,"attach_path",sw_attach_path(sw,bf));

	return TRUE;
}

int addressContactFile(MAddress *mAddress)
{
	
}


// Output a vcard. Minimum field set required by RFC is : BEGIN + VERSION + N + FN + END
// RFC2426: http://tools.ietf.org/html/rfc2426

int abk_vcard_ouput(FILE *f, char *cid, Kpair *fields, char *outname)
{
	char bf[BFSZ], *field;
	char p1[BFSZ],p2[BFSZ],p3[BFSZ],p4[BFSZ];

	if(f==NULL) return FALSE;

	fprintf(f,"BEGIN:VCARD\r\n");
	fprintf(f,"VERSION:3.0\r\n");
	field=kpair_find(fields,"name");
	json_basicvalue(field,"first",p1);
	json_basicvalue(field,"last",p2);
	json_basicvalue(field,"prefix",p3);
	json_basicvalue(field,"suffix",p4);
	fprintf(f,"N:%s;%s;;%s;%s\r\n",p1,p2,p3,p4);
	// N:Taylor;Gary;A;Mr;CNA

	// Have the file sensibly named
	ncpy(outname,p1,BFSZ-1);
	ncat_sep(outname," ",p2,BFSZ-1);
	if(strlen(outname)==0) ncpy(outname,"Contact",BFSZ-1);
	lib_clean_fname(outname);
	ncat(outname,".vcf",BFSZ-1);

	
	ncpy(bf,p3,BFSZ-1);
	ncat_sep(bf," ",p1,BFSZ-1);
	ncat_sep(bf," ",p2,BFSZ-1);
	ncat_sep(bf," ",p4,BFSZ-1);
	fprintf(f,"FN:%s\r\n",bf);
	// FN:Mr Gary A Taylor CNA

	// NICKNAME:Gary

	abk_vcard_ouput_arr(f, fields, "email", "EMAIL");
	// EMAIL:gtaylor@nexicomgroup.net

	abk_vcard_ouput_arr(f, fields, "phone", "TEL");
	// TEL;TYPE=WORK,PREF:705-932-4191

//	fprintf(f,"ADR:;;;;;;\r\n");
//	fprintf(f,"LABEL;TYPE=dom,home,postal,parcel:Mr.John Q. Public\\, Esq.\\n\r\n");
//	fprintf(f," Mail Drop: TNE QB\\n123 Main Street\\nAny Town\\, CA  91921-1234\r\n");
//	fprintf(f," \\nU.S.A.\r\n");
//	Does not seem to work with most mail clients!?!?
	abk_vcard_ouput_arr(f, fields, "address", "ADR");
	// ADR:;;5 King St. E.;Millbrook;ON;L0A1G0;Canada
	// *structured* or *unstructured* address info
	// LABEL;ENCODING=B:NSBLaW5nIFN0LiBFLgpNaWxsYnJvb2ssIE9OCiAgICAgICAgIEwwQTFHMC
	//  BDYW5hZGE=
	// LABEL;TYPE=dom,home,postal,parcel:Mr.John Q. Public\, Esq.\n
	//  Mail Drop: TNE QB\n123 Main Street\nAny Town\, CA  91921-1234
	//  \nU.S.A.

	field=kpair_find(fields,"company");
	json_basicvalue(field,"value",p1);
	json_basicvalue(field,"department",p2);
	if(strlen(p1)>0 || strlen(p2)>0) fprintf(f,"ORG:%s;%s\r\n",p1,p2);
	// ORG:Nexicom;
	json_basicvalue(field,"title",p1);
	if(strlen(p1)>0) fprintf(f,"TITLE:%s;%s\r\n",p1);
	// TITLE:Technician

	// BDAY:1958-02-21
	// TZ:-05:00

	abk_vcard_ouput_arr(f, fields, "url", "URL");
	// URL:www.nexicom.net

	// CLASS:PUBLIC
	// REV:2009-11-05T19:50:19Z
	// X-OWM-CHARSET:iso-8859-1
	
	fprintf(f,"X-SW-UID:%s\r\n",cid);
	// X-OWM-UID:20091105-195019-JJYJMCBCWCWC-79ER
	fprintf(f,"END:VCARD\r\n");

	return TRUE;
}


*/
// decode message 
