
typedef struct IPAIR {
	char *var;
	char *attrib;
	char *val;
	struct IPAIR *next;
} Ipair;
#define MAX_ALERTS 2
typedef struct ICAL {
	char *name; // vevent, vtodo, vjournal, vfreebusy, valarm
	char *name_extra;
	char *cal_id;

	// Flags for use during processing
	int  alert_count;
	time_t start, alerts[MAX_ALERTS];
	int repeats, repeat;
	int readonly, locked, del;
	char *rid;

	Ipair *fields;
	int guard; // guard byte that should not ever be changed.
	int fields_locked;
	struct ICAL *sub; // object within this object (events within calender)
	struct ICAL *next; // List of objects, if this is a 'vevent'  then 
	char *created_file;	// source line where this object is created...
	int created_line; 
	char *locked_file;
	int locked_line;
} Ical;
#define MAXREPEAT 2000 // Once a week 
typedef struct DATELIST {time_t event[MAXREPEAT]; int nevents; } Datelist;
// flags = +-N  or 0 for a normal rule... 
typedef struct BYLIST {int event[MAXREPEAT]; int prefix[MAXREPEAT]; int nevents; } Bylist;


Ical *ical_read_object(FILE *f, char *objname, char*nextline, char *file, int line);
int ical_readline(FILE *f, char *var, char *val, char *attrib, char *nextline, int valsz);
#define ical_read(a) ical_read_real(a,__FILE__,__LINE__)
#define ical_read2(a) ical_read2_real(a,__FILE__,__LINE__)
Ical *ical_newx(char *src, int line);
#define ical_new() ical_newx(__FILE__,__LINE__)

Ical *ical_read_real(char *fname, char *file, int line);
Ical *ical_read2_real(FILE *f, char *file, int line);



void ical_add_sub(Ical *ic, Ical *sub);
//int ical_free(Ical *x);
#define ical_ffree(a) ical_ffree_real(a,__FILE__,__LINE__)
int ical_ffree_real(Ical **x, char *source, int line);

int ical_during(Ical *x, time_t dtstart, time_t dtend);
time_t ical_time(char *s, int timezone_offset);
int ical_datetime(Datelist *dl, Ical *ic, char *var, time_t start, time_t end);
int days_in_year(time_t t);
int days_in_month(time_t t);
char *ical_find_value(Ical *ic, char *var);
int ical_find_both(Ical *ic, char *var, char **val, char **attrib);


char *ical_field_get(Ical *ic, char *var);
#define ical_field_set(a,b,c,d) ical_field_setx(a,b,c,d,__FILE__,__LINE__);
int ical_field_setx(Ical *ic, char *var, char *val, char*attrib, char *file, int line);
int ical_field_addmatch(Ical *ic, char *var, char *val, char*attrib);
int ical_field_remove(Ical *ic, char *var);
int ical_count_events(Ical *ic);
int ical_event_index(Ical *ic, Ical**index, int np, int clean, int offset);
int ical_clean_one_entry(Ical *icp2, Ical*icp, int *repeat, Ical**icp_main, int offset);
int ical_timestamp(char *txt, int offset);
int ical_timestamp_local(char *txt);
char *ical_timestamp_txt(time_t t,int utc,char *bf);
Dstr *ical_generate_ics(Ical *ic, Dstr *dstr);
Ical *ical_default_ics(Ical **ic_out, int offset, int is_todo);

Ical *ical_confirmation(Ical *src, char *email, char *action);
int ical_update_event(Ical *ev, Ical *cal_request, char *action, int ev_is_new);
Ical *cal_event_sub(Ical*cal);
void cal_update_string(char *src, char *var, char *val, char *dst);
void cal_update_str(char *str, char *var, char *val);

char *ical_mfind_attrib(Ical *ic, char *var, char *val);
int ical_mfield_remove(Ical *ic, char *var, char *val);

int ical_mfield_set(Ical *ic, char *var, char *val, char*attrib);	// cf ical_field_set
char *ical_partstat_status(char *attrib);

char *ical_encode(char *in,char *out);
int ical_timestamp_report(char *txt, int offset);