aboutsummaryrefslogtreecommitdiffstats
path: root/src/ical.c
blob: 93952eced56fd574364400965085a55fa4a233ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2015 calcurse Development Team <misc@calcurse.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

#include <strings.h>
#include <sys/types.h>

#include "calcurse.h"

#define ICALDATEFMT      "%Y%m%d"
#define ICALDATETIMEFMT  "%Y%m%dT%H%M%S"

typedef enum {
	ICAL_VEVENT,
	ICAL_VTODO,
	ICAL_TYPES
} ical_types_e;

typedef enum {
	UNDEFINED,
	APPOINTMENT,
	EVENT
} ical_vevent_e;

typedef struct {
	enum recur_type type;
	int freq;
	long until;
	unsigned count;
} ical_rpt_t;

static void ical_export_header(FILE *);
static void ical_export_recur_events(FILE *, int);
static void ical_export_events(FILE *, int);
static void ical_export_recur_apoints(FILE *, int);
static void ical_export_apoints(FILE *, int);
static void ical_export_todo(FILE *, int);
static void ical_export_footer(FILE *);

static const char *ical_recur_type[RECUR_TYPES] =
    { "", "DAILY", "WEEKLY", "MONTHLY", "YEARLY" };

/* iCal alarm notification. */
static void ical_export_valarm(FILE * stream)
{
	fputs("BEGIN:VALARM\n", stream);
	pthread_mutex_lock(&nbar.mutex);
	fprintf(stream, "TRIGGER:-P%dS\n", nbar.cntdwn);
	pthread_mutex_unlock(&nbar.mutex);
	fputs("ACTION:DISPLAY\n", stream);
	fputs("END:VALARM\n", stream);
}

/* Export header. */
static void ical_export_header(FILE * stream)
{
	fputs("BEGIN:VCALENDAR\n", stream);
	fprintf(stream, "PRODID:-//calcurse//NONSGML v%s//EN\n", VERSION);
	fputs("VERSION:2.0\n", stream);
}

/* Export footer. */
static void ical_export_footer(FILE * stream)
{
	fputs("END:VCALENDAR\n", stream);
}

/* Export recurrent events. */
static void ical_export_recur_events(FILE * stream, int export_uid)
{
	llist_item_t *i, *j;
	char ical_date[BUFSIZ];

	LLIST_FOREACH(&recur_elist, i) {
		struct recur_event *rev = LLIST_GET_DATA(i);
		date_sec2date_fmt(rev->day, ICALDATEFMT, ical_date);
		fputs("BEGIN:VEVENT\n", stream);
		fprintf(stream, "DTSTART:%s\n", ical_date);
		fprintf(stream, "RRULE:FREQ=%s;INTERVAL=%d",
			ical_recur_type[rev->rpt->type], rev->rpt->freq);

		if (rev->rpt->until != 0) {
			date_sec2date_fmt(rev->rpt->until, ICALDATEFMT,
					  ical_date);
			fprintf(stream, ";UNTIL=%s\n", ical_date);
		} else {
			fputc('\n', stream);
		}

		if (LLIST_FIRST(&rev->exc)) {
			fputs("EXDATE:", stream);
			LLIST_FOREACH(&rev->exc, j) {
				struct excp *exc = LLIST_GET_DATA(j);
				date_sec2date_fmt(exc->st, ICALDATEFMT,
						  ical_date);
				fprintf(stream, "%s", ical_date);
				fputc(LLIST_NEXT(j) ? ',' : '\n', stream);
			}
		}

		fprintf(stream, "SUMMARY:%s\n", rev->mesg);

		if (export_uid) {
			char *hash = recur_event_hash(rev);
			fprintf(stream, "UID:%s\n", hash);
			mem_free(hash);
		}

		fputs("END:VEVENT\n", stream);
	}
}

/* Export events. */
static void ical_export_events(FILE * stream, int export_uid)
{
	llist_item_t *i;
	char ical_date[BUFSIZ];

	LLIST_FOREACH(&eventlist, i) {
		struct event *ev = LLIST_TS_GET_DATA(i);
		date_sec2date_fmt(ev->day, ICALDATEFMT, ical_date);
		fputs("BEGIN:VEVENT\n", stream);
		fprintf(stream, "DTSTART:%s\n", ical_date);
		fprintf(stream, "SUMMARY:%s\n", ev->mesg);

		if (export_uid) {
			char *hash = event_hash(ev);
			fprintf(stream, "UID:%s\n", hash);
			mem_free(hash);
		}

		fputs("END:VEVENT\n", stream);
	}
}

/* Export recurrent appointments. */
static void ical_export_recur_apoints(FILE * stream, int export_uid)
{
	llist_item_t *i, *j;
	char ical_datetime[BUFSIZ];
	char ical_date[BUFSIZ];

	LLIST_TS_LOCK(&recur_alist_p);
	LLIST_TS_FOREACH(&recur_alist_p, i) {
		struct recur_apoint *rapt = LLIST_TS_GET_DATA(i);

		date_sec2date_fmt(rapt->start, ICALDATETIMEFMT,
				  ical_datetime);
		fputs("BEGIN:VEVENT\n", stream);
		fprintf(stream, "DTSTART:%s\n", ical_datetime);
		if (rapt->dur > 0) {
			fprintf(stream, "DURATION:P%ldDT%ldH%ldM%ldS\n",
				rapt->dur / DAYINSEC,
				(rapt->dur / HOURINSEC) % DAYINHOURS,
				(rapt->dur / MININSEC) % HOURINMIN,
				rapt->dur % MININSEC);
		}
		fprintf(stream, "RRULE:FREQ=%s;INTERVAL=%d",
			ical_recur_type[rapt->rpt->type], rapt->rpt->freq);

		if (rapt->rpt->until != 0) {
			date_sec2date_fmt(rapt->rpt->until + HOURINSEC,
					  ICALDATEFMT, ical_date);
			fprintf(stream, ";UNTIL=%s\n", ical_date);
		} else {
			fputc('\n', stream);
		}

		if (LLIST_FIRST(&rapt->exc)) {
			fputs("EXDATE:", stream);
			LLIST_FOREACH(&rapt->exc, j) {
				struct excp *exc = LLIST_GET_DATA(j);
				date_sec2date_fmt(exc->st, ICALDATEFMT,
						  ical_date);
				fprintf(stream, "%s", ical_date);
				fputc(LLIST_NEXT(j) ? ',' : '\n', stream);
			}
		}

		fprintf(stream, "SUMMARY:%s\n", rapt->mesg);
		if (rapt->state & APOINT_NOTIFY)
			ical_export_valarm(stream);

		if (export_uid) {
			char *hash = recur_apoint_hash(rapt);
			fprintf(stream, "UID:%s\n", hash);
			mem_free(hash);
		}

		fputs("END:VEVENT\n", stream);
	}
	LLIST_TS_UNLOCK(&recur_alist_p);
}

/* Export appointments. */
static void ical_export_apoints(FILE * stream, int export_uid)
{
	llist_item_t *i;
	char ical_datetime[BUFSIZ];

	LLIST_TS_LOCK(&alist_p);
	LLIST_TS_FOREACH(&alist_p, i) {
		struct apoint *apt = LLIST_TS_GET_DATA(i);
		date_sec2date_fmt(apt->start, ICALDATETIMEFMT,
				  ical_datetime);
		fputs("BEGIN:VEVENT\n", stream);
		fprintf(stream, "DTSTART:%s\n", ical_datetime);
		if (apt->dur > 0) {
			fprintf(stream, "DURATION:P%ldDT%ldH%ldM%ldS\n",
				apt->dur / DAYINSEC,
				(apt->dur / HOURINSEC) % DAYINHOURS,
				(apt->dur / MININSEC) % HOURINMIN,
				apt->dur % MININSEC);
		}
		fprintf(stream, "SUMMARY:%s\n", apt->mesg);
		if (apt->state & APOINT_NOTIFY)
			ical_export_valarm(stream);

		if (export_uid) {
			char *hash = apoint_hash(apt);
			fprintf(stream, "UID:%s\n", hash);
			mem_free(hash);
		}

		fputs("END:VEVENT\n", stream);
	}
	LLIST_TS_UNLOCK(&alist_p);
}

/* Export todo items. */
static void ical_export_todo(FILE * stream, int export_uid)
{
	llist_item_t *i;

	LLIST_FOREACH(&todolist, i) {
		struct todo *todo = LLIST_TS_GET_DATA(i);
		int priority = todo->id;

		fputs("BEGIN:VTODO\n", stream);
		if (todo->id < 0) {
			fprintf(stream, "STATUS:COMPLETED\n");
			priority = -priority;
		}
		fprintf(stream, "PRIORITY:%d\n", priority);
		fprintf(stream, "SUMMARY:%s\n", todo->mesg);

		if (export_uid) {
			char *hash = todo_hash(todo);
			fprintf(stream, "UID:%s\n", hash);
			mem_free(hash);
		}

		fputs("END:VTODO\n", stream);
	}
}

/* Print a header to describe import log report format. */
static void ical_log_init(FILE * log, int major, int minor)
{
	const char *header =
	    "+-------------------------------------------------------------------+\n"
	    "| Calcurse icalendar import log.                                    |\n"
	    "|                                                                   |\n"
	    "| Items imported from icalendar file, version %d.%d                   |\n"
	    "| Some items could not be imported, they are described hereafter.   |\n"
	    "| The log line format is as follows:                                |\n"
	    "|                                                                   |\n"
	    "|       TYPE [LINE]: DESCRIPTION                                    |\n"
	    "|                                                                   |\n"
	    "| where:                                                            |\n"
	    "|  * TYPE represents the item type ('VEVENT' or 'VTODO')            |\n"
	    "|  * LINE is the line in the input stream at which this item begins |\n"
	    "|  * DESCRIPTION indicates why the item could not be imported       |\n"
	    "+-------------------------------------------------------------------+\n\n";

	if (log)
		fprintf(log, header, major, minor);
}

/*
 * Used to build a report of the import process.
 * The icalendar item for which a problem occurs is mentioned (by giving its
 * first line inside the icalendar file), together with a message describing the
 * problem.
 */
static void ical_log(FILE * log, ical_types_e type, unsigned lineno,
		     char *msg)
{
	const char *typestr[ICAL_TYPES] = { "VEVENT", "VTODO" };

	RETURN_IF(type < 0 || type >= ICAL_TYPES, _("unknown ical type"));
	if (!log)
		return;

	fprintf(log, "%s [%d]: %s\n", typestr[type], lineno, msg);
}

static void ical_store_todo(int priority, char *mesg, char *note, int list)
{
	struct todo *todo = todo_add(mesg, priority, note);
	if (list) {
		char *hash = todo_hash(todo);
		printf("%s\n", hash);
		mem_free(hash);
	}
	mem_free(mesg);
	erase_note(&note);
}

static void
ical_store_event(char *mesg, char *note, long day, long end,
		 ical_rpt_t * rpt, llist_t * exc, int list)
{
	const int EVENTID = 1;
	struct event *ev;
	struct recur_event *rev;

	if (rpt) {
		rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
				      rpt->freq, rpt->until, exc);
		mem_free(rpt);
		if (list) {
			char *hash = recur_event_hash(rev);
			printf("%s\n", hash);
			mem_free(hash);
		}
		goto cleanup;
	}

	if (end == 0 || end - day <= DAYINSEC) {
		ev = event_new(mesg, note, day, EVENTID);
		if (list) {
			char *hash = event_hash(ev);
			printf("%s\n", hash);
			mem_free(hash);
		}
		goto cleanup;
	}

	/*
	 * Here we have an event that spans over several days.
	 *
	 * In iCal, the end specifies when the event is supposed to end, in
	 * calcurse, the end specifies the time that the last occurrence of the
	 * event starts, so we need to do some conversion here.
	 */
	end = day + ((end - day - 1) / DAYINSEC) * DAYINSEC;
	rpt = mem_malloc(sizeof(ical_rpt_t));
	rpt->type = RECUR_DAILY;
	rpt->freq = 1;
	rpt->count = 0;
	rpt->until = end;
	rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
			      rpt->freq, rpt->until, exc);
	mem_free(rpt);
	if (list) {
		char *hash = recur_event_hash(rev);
		printf("%s\n", hash);
		mem_free(hash);
	}

cleanup:
	mem_free(mesg);
	erase_note(&note);
}

static void
ical_store_apoint(char *mesg, char *note, long start, long dur,
		  ical_rpt_t * rpt, llist_t * exc, int has_alarm, int list)
{
	char state = 0L;
	struct apoint *apt;
	struct recur_apoint *rapt;

	if (has_alarm)
		state |= APOINT_NOTIFY;
	if (rpt) {
		rapt = recur_apoint_new(mesg, note, start, dur, state,
					rpt->type, rpt->freq, rpt->until, exc);
		mem_free(rpt);
		if (list) {
			char *hash = recur_apoint_hash(rapt);
			printf("%s\n", hash);
			mem_free(hash);
		}
	} else {
		apt = apoint_new(mesg, note, start, dur, state);
		if (list) {
			char *hash = apoint_hash(apt);
			printf("%s\n", hash);
			mem_free(hash);
		}
	}
	mem_free(mesg);
	erase_note(&note);
}

/*
 * Returns an allocated string representing the string given in argument once
 * unformatted.
 */
static char *ical_unformat_line(char *line)
{
	struct string s;
	char *p;

	string_init(&s);
	for (p = line; *p; p++) {
		switch (*p) {
		case '\\':
			switch (*(p + 1)) {
			case 'n':
				string_catf(&s, "%c", '\n');
				p++;
				break;
			case 't':
				string_catf(&s, "%c", '\t');
				p++;
				break;
			case ';':
			case ':':
			case ',':
				string_catf(&s, "%c", *(p + 1));
				p++;
				break;
			default:
				string_catf(&s, "%c", *p);
				break;
			}
			break;
		default:
			string_catf(&s, "%c", *p);
			break;
		}
	}

	return string_buf(&s);
}

static void
ical_readline_init(FILE * fdi, char *buf, char *lstore, unsigned *ln)
{
	char *eol;

	*buf = *lstore = '\0';
	if (fgets(lstore, BUFSIZ, fdi)) {
		if ((eol = strchr(lstore, '\n')) != NULL) {
			if (*(eol - 1) == '\r')
				*(eol - 1) = '\0';
			else
				*eol = '\0';
		}
		(*ln)++;
	}
}

static int ical_readline(FILE * fdi, char *buf, char *lstore, unsigned *ln)
{
	char *eol;

	strncpy(buf, lstore, BUFSIZ);
	(*ln)++;

	while (fgets(lstore, BUFSIZ, fdi) != NULL) {
		if ((eol = strchr(lstore, '\n')) != NULL) {
			if (*(eol - 1) == '\r')
				*(eol - 1) = '\0';
			else
				*eol = '\0';
		}
		if (*lstore != SPACE && *lstore != TAB)
			break;
		strncat(buf, lstore + 1, BUFSIZ - strlen(buf) - 1);
		(*ln)++;
	}

	if (feof(fdi)) {
		*lstore = '\0';
		if (*buf == '\0')
			return 0;
	}

	return 1;
}

static int
ical_chk_header(FILE * fd, char *buf, char *lstore, unsigned *lineno,
		int *major, int *minor)
{
	if (!ical_readline(fd, buf, lstore, lineno))
		return 0;

	if (!starts_with_ci(buf, "BEGIN:VCALENDAR"))
		return 0;

	while (!sscanf(buf, "VERSION:%d.%d", major, minor)) {
		if (!ical_readline(fd, buf, lstore, lineno))
			return 0;
	}

	return 1;
}

/*
 * iCalendar date-time format is based on the ISO 8601 complete
 * representation. It should be something like : DATE 'T' TIME
 * where DATE is 'YYYYMMDD' and TIME is 'HHMMSS'.
 * The time and 'T' separator are optional (in the case of an day-long event).
 *
 * Optionally, if the type pointer is given, specify if it is an event
 * (no time is given, meaning it is an all-day event), or an appointment
 * (time is given).
 *
 * The timezone is not yet handled by calcurse.
 */
static time_t ical_datetime2time_t(char *datestr, ical_vevent_e * type)
{
	const int FORMAT_DATE = 3, FORMAT_DATETIME = 6, FORMAT_DATETIMEZ = 7;
	struct date date;
	unsigned hour, min, sec;
	char c;
	int format;

	format = sscanf(datestr, "%04u%02u%02uT%02u%02u%02u%c",
			&date.yyyy, &date.mm, &date.dd, &hour, &min, &sec, &c);
	if (format == FORMAT_DATE) {
		if (type)
			*type = EVENT;
		return date2sec(date, 0, 0);
	} else if (format == FORMAT_DATETIME || format == FORMAT_DATETIMEZ) {
		if (type)
			*type = APPOINTMENT;
		if (format == FORMAT_DATETIMEZ && c == 'Z')
			return utcdate2sec(date, hour, min);
		else
			return date2sec(date, hour, min);
	}
	return 0;
}

static long ical_durtime2long(char *timestr)
{
	char *p = timestr;
	int bytes_read;
	unsigned hour = 0, min = 0, sec = 0;

	if (*p != 'T')
		return 0;
	p++;

	if (strchr(p, 'H')) {
		if (sscanf(p, "%uH%n", &hour, &bytes_read) != 1)
			return 0;
		p += bytes_read;
	}
	if (strchr(p, 'M')) {
		if (sscanf(p, "%uM%n", &min, &bytes_read) != 1)
			return 0;
		p += bytes_read;
	}
	if (strchr(p, 'S')) {
		if (sscanf(p, "%uS%n", &sec, &bytes_read) != 1)
			return 0;
		p += bytes_read;
	}

	return hour * HOURINSEC + min * MININSEC + sec;
}

/*
 * Extract from RFC2445:
 *
 * Value Name: DURATION
 *
 * Purpose: This value type is used to identify properties that contain
 * duration of time.
 *
 * Formal Definition: The value type is defined by the following
 * notation:
 *
 * dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
 * dur-date   = dur-day [dur-time]
 * dur-time   = "T" (dur-hour / dur-minute / dur-second)
 * dur-week   = 1*DIGIT "W"
 * dur-hour   = 1*DIGIT "H" [dur-minute]
 * dur-minute = 1*DIGIT "M" [dur-second]
 * dur-second = 1*DIGIT "S"
 * dur-day    = 1*DIGIT "D"
 *
 * Example: A duration of 15 days, 5 hours and 20 seconds would be:
 * P15DT5H0M20S
 * A duration of 7 weeks would be:
 * P7W
 */
static long ical_dur2long(char *durstr)
{
	char *p;
	int bytes_read;
	struct {
		unsigned week, day;
	} date;

	memset(&date, 0, sizeof date);

	p = strchr(durstr, 'P');
	if (!p)
		return -1;
	p++;

	if (*p == '-')
		return -1;
	if (*p == '+')
		p++;

	if (*p == 'T') {
		/* dur-time */
		return ical_durtime2long(p);
	} else if (strchr(p, 'W')) {
		/* dur-week */
		if (sscanf(p, "%u", &date.week) == 1)
			return date.week * WEEKINDAYS * DAYINSEC;
	} else if (strchr(p, 'D')) {
		/* dur-date */
		if (sscanf(p, "%uD%n", &date.day, &bytes_read) == 1) {
			p += bytes_read;
			return date.day * DAYINSEC + ical_durtime2long(p);
		}
	}

	return -1;
}

/*
 * Compute the vevent repetition end date from the repetition count.
 *
 * Extract from RFC2445:
 * The COUNT rule part defines the number of occurrences at which to
 * range-bound the recurrence. The "DTSTART" property value, if specified,
 * counts as the first occurrence.
 */
static long ical_compute_rpt_until(long start, ical_rpt_t * rpt)
{
	switch (rpt->type) {
	case RECUR_DAILY:
		return date_sec_change(start, 0, rpt->freq * (rpt->count - 1));
	case RECUR_WEEKLY:
		return date_sec_change(start, 0,
				rpt->freq * WEEKINDAYS * (rpt->count - 1));
	case RECUR_MONTHLY:
		return date_sec_change(start, rpt->freq * (rpt->count - 1), 0);
	case RECUR_YEARLY:
		return date_sec_change(start,
				    rpt->freq * 12 * (rpt->count - 1), 0);
	default:
		return 0;
	}
}

/*
 * Read a recurrence rule from an iCalendar RRULE string.
 *
 * Value Name: RECUR
 *
 * Purpose: This value type is used to identify properties that contain
 * a recurrence rule specification.
 *
 * Formal Definition: The value type is defined by the following
 * notation:
 *
 * recur      = "FREQ"=freq *(
 *
 * ; either UNTIL or COUNT may appear in a 'recur',
 * ; but UNTIL and COUNT MUST NOT occur in the same 'recur'
 *
 * ( ";" "UNTIL" "=" enddate ) /
 * ( ";" "COUNT" "=" 1*DIGIT ) /
 *
 * ; the rest of these keywords are optional,
 * ; but MUST NOT occur more than
 * ; once
 *
 * ( ";" "INTERVAL" "=" 1*DIGIT )          /
 * ( ";" "BYSECOND" "=" byseclist )        /
 * ( ";" "BYMINUTE" "=" byminlist )        /
 * ( ";" "BYHOUR" "=" byhrlist )           /
 * ( ";" "BYDAY" "=" bywdaylist )          /
 * ( ";" "BYMONTHDAY" "=" bymodaylist )    /
 * ( ";" "BYYEARDAY" "=" byyrdaylist )     /
 * ( ";" "BYWEEKNO" "=" bywknolist )       /
 * ( ";" "BYMONTH" "=" bymolist )          /
 * ( ";" "BYSETPOS" "=" bysplist )         /
 * ( ";" "WKST" "=" weekday )              /
 * ( ";" x-name "=" text )
 * )
*/
static ical_rpt_t *ical_read_rrule(FILE * log, char *rrulestr,
				   unsigned *noskipped, const int itemline)
{
	const char count[] = "COUNT=";
	const char interv[] = "INTERVAL=";
	char freqstr[BUFSIZ];
	unsigned interval;
	ical_rpt_t *rpt;
	char *p;

	p = strchr(rrulestr, ':');
	if (!p) {
		ical_log(log, ICAL_VEVENT, itemline,
			 _("recurrence rule malformed."));
		(*noskipped)++;
		return NULL;
	}
	p++;

	rpt = mem_malloc(sizeof(ical_rpt_t));
	memset(rpt, 0, sizeof(ical_rpt_t));
	if (sscanf(p, "FREQ=%s", freqstr) != 1) {
		ical_log(log, ICAL_VEVENT, itemline,
			 _("recurrence frequence not found."));
		(*noskipped)++;
		mem_free(rpt);
		return NULL;
	}

	if (starts_with(freqstr, "DAILY")) {
		rpt->type = RECUR_DAILY;
	} else if (starts_with(freqstr, "WEEKLY")) {
		rpt->type = RECUR_WEEKLY;
	} else if (starts_with(freqstr, "MONTHLY")) {
		rpt->type = RECUR_MONTHLY;
	} else if (starts_with(freqstr, "YEARLY")) {
		rpt->type = RECUR_YEARLY;
	} else {
		ical_log(log, ICAL_VEVENT, itemline,
			 _("recurrence frequence not recognized."));
		(*noskipped)++;
		mem_free(rpt);
		return NULL;
	}

	/*
	 * The UNTIL rule part defines a date-time value which bounds the
	 * recurrence rule in an inclusive manner.  If not present, and the
	 * COUNT rule part is also not present, the RRULE is considered to
	 * repeat forever.

	 * The COUNT rule part defines the number of occurrences at which to
	 * range-bound the recurrence.  The "DTSTART" property value, if
	 * specified, counts as the first occurrence.
	 */
	if ((p = strstr(rrulestr, "UNTIL")) != NULL) {
		rpt->until = ical_datetime2time_t(strchr(p, '=') + 1, NULL);
	} else {
		unsigned cnt;
		char *countstr;

		rpt->until = 0;
		if ((countstr = strstr(rrulestr, count))) {
			countstr += sizeof(count) - 1;
			if (sscanf(countstr, "%u", &cnt) == 1)
				rpt->count = cnt;
		}
	}

	rpt->freq = 1;
	if ((p = strstr(rrulestr, interv))) {
		p += sizeof(interv) - 1;
		if (sscanf(p, "%u", &interval) == 1)
			rpt->freq = interval;
	}

	return rpt;
}

static void ical_add_exc(llist_t * exc_head, long date)
{
	if (date == 0)
		return;

	struct excp *exc = mem_malloc(sizeof(struct excp));
	exc->st = date;

	LLIST_ADD(exc_head, exc);
}

/*
 * This property defines the list of date/time exceptions for a
 * recurring calendar component.
 */
static void
ical_read_exdate(llist_t * exc, FILE * log, char *exstr,
		 unsigned *noskipped, const int itemline)
{
	char *p, *q;

	p = strchr(exstr, ':');
	if (!p) {
		ical_log(log, ICAL_VEVENT, itemline,
			 _("recurrence exception dates malformed."));
		(*noskipped)++;
		return;
	}
	p++;

	while ((q = strchr(p, ',')) != NULL) {
		char buf[BUFSIZ];
		const int buflen = q - p;

		strncpy(buf, p, buflen);
		buf[buflen] = '\0';
		ical_add_exc(exc, ical_datetime2time_t(buf, NULL));
		p = ++q;
	}
	ical_add_exc(exc, ical_datetime2time_t(p, NULL));
}

/* Return an allocated string containing the name of the newly created note. */
static char *ical_read_note(char *line, unsigned *noskipped,
			    ical_types_e item_type, const int itemline,
			    FILE * log)
{
	char *p, *notestr, *note;

	p = strchr(line, ':');
	if (!p) {
		ical_log(log, item_type, itemline,
			 _("description malformed."));
		(*noskipped)++;
		return NULL;
	}
	p++;

	notestr = ical_unformat_line(p);
	if (notestr == NULL) {
		ical_log(log, item_type, itemline,
			 _("could not get entire item description."));
		(*noskipped)++;
		return NULL;
	} else if (strlen(notestr) == 0) {
		mem_free(notestr);
		return NULL;
	} else {
		note = generate_note(notestr);
		mem_free(notestr);
		return note;
	}
}

/* Returns an allocated string containing the ical item summary. */
static char *ical_read_summary(char *line)
{
	char *p, *summary;

	p = strchr(line, ':');
	if (!p)
		return NULL;

	summary = ical_unformat_line(p + 1);
	if (!summary)
		return NULL;

	/* Event summaries must not contain newlines. */
	for (p = strchr(summary, '\n'); p; p = strchr(p, '\n'))
		*p = ' ';

	return summary;
}

static void
ical_read_event(FILE * fdi, FILE * log, int list, unsigned *noevents,
		unsigned *noapoints, unsigned *noskipped, char *buf,
		char *lstore, unsigned *lineno)
{
	const int ITEMLINE = *lineno;
	ical_vevent_e vevent_type;
	char *p;
	struct {
		llist_t exc;
		ical_rpt_t *rpt;
		char *mesg, *note;
		long start, end, dur;
		int has_alarm;
	} vevent;
	int skip_alarm;

	vevent_type = UNDEFINED;
	memset(&vevent, 0, sizeof vevent);
	LLIST_INIT(&vevent.exc);
	skip_alarm = 0;
	while (ical_readline(fdi, buf, lstore, lineno)) {
		if (skip_alarm) {
			/*
			 * Need to skip VALARM properties because some keywords
			 * could interfere, such as DURATION, SUMMARY,..
			 */
			if (starts_with_ci(buf, "END:VALARM"))
				skip_alarm = 0;
			continue;
		}

		if (starts_with_ci(buf, "END:VEVENT")) {
			if (!vevent.mesg) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("could not retrieve item summary."));
				goto cleanup;
			}
			if (vevent.start == 0) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("item start date is not defined."));
				goto cleanup;
			}

			if (vevent_type == APPOINTMENT && vevent.dur == 0) {
				if (vevent.end != 0) {
					vevent.dur = vevent.end - vevent.start;
				}

				if (vevent.dur < 0) {
					ical_log(log, ICAL_VEVENT, ITEMLINE,
						_("item has a negative duration."));
					goto cleanup;
				}
			}

			if (vevent.rpt && vevent.rpt->count) {
				vevent.rpt->until =
					ical_compute_rpt_until(vevent.start,
							vevent.rpt);
			}

			switch (vevent_type) {
			case APPOINTMENT:
				ical_store_apoint(vevent.mesg, vevent.note,
						vevent.start, vevent.dur,
						vevent.rpt, &vevent.exc,
						vevent.has_alarm, list);
				(*noapoints)++;
				break;
			case EVENT:
				ical_store_event(vevent.mesg, vevent.note,
						vevent.start, vevent.end,
						vevent.rpt, &vevent.exc, list);
				(*noevents)++;
				break;
			case UNDEFINED:
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("item could not be identified."));
				goto cleanup;
				break;
			}

			return;
		}

		if (starts_with_ci(buf, "DTSTART")) {
			p = strchr(buf, ':');
			if (!p) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("event start time malformed."));
				goto cleanup;
			}

			vevent.start = ical_datetime2time_t(++p, &vevent_type);
			if (!vevent.start) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("could not retrieve event start time."));
				goto cleanup;
			}
		} else if (starts_with_ci(buf, "DTEND")) {
			p = strchr(buf, ':');
			if (!p) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("event end time malformed."));
				goto cleanup;
			}

			vevent.end = ical_datetime2time_t(++p, &vevent_type);
			if (!vevent.end) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("could not retrieve event end time."));
				goto cleanup;
			}
		} else if (starts_with_ci(buf, "DURATION")) {
			vevent.dur = ical_dur2long(buf);
			if (vevent.dur <= 0) {
				ical_log(log, ICAL_VEVENT, ITEMLINE,
					 _("item duration malformed."));
				goto cleanup;
			}
		} else if (starts_with_ci(buf, "RRULE")) {
			vevent.rpt = ical_read_rrule(log, buf, noskipped,
					ITEMLINE);
		} else if (starts_with_ci(buf, "EXDATE")) {
			ical_read_exdate(&vevent.exc, log, buf, noskipped,
					ITEMLINE);
		} else if (starts_with_ci(buf, "SUMMARY")) {
			vevent.mesg = ical_read_summary(buf);
		} else if (starts_with_ci(buf, "BEGIN:VALARM")) {
			skip_alarm = vevent.has_alarm = 1;
		} else if (starts_with_ci(buf, "DESCRIPTION")) {
			vevent.note = ical_read_note(buf, noskipped,
					ICAL_VEVENT, ITEMLINE, log);
		}
	}

	ical_log(log, ICAL_VEVENT, ITEMLINE,
		 _("The ical file seems to be malformed. "
		   "The end of item was not found."));

cleanup:

	if (vevent.note)
		mem_free(vevent.note);
	if (vevent.mesg)
		mem_free(vevent.mesg);
	if (vevent.rpt)
		mem_free(vevent.rpt);
	LLIST_FREE(&vevent.exc);
	(*noskipped)++;
}

static void
ical_read_todo(FILE * fdi, FILE * log, int list, unsigned *notodos,
	       unsigned *noskipped, char *buf, char *lstore,
	       unsigned *lineno)
{
	const int LOWEST = 9;
	const int ITEMLINE = *lineno;
	struct {
		char *mesg, *note;
		int has_priority, priority;
		int completed;
	} vtodo;
	int skip_alarm;

	memset(&vtodo, 0, sizeof vtodo);
	skip_alarm = 0;
	while (ical_readline(fdi, buf, lstore, lineno)) {
		if (skip_alarm) {
			/*
			 * Need to skip VALARM properties because some keywords
			 * could interfere, such as DURATION, SUMMARY,..
			 */
			if (starts_with_ci(buf, "END:VALARM"))
				skip_alarm = 0;
			continue;
		}

		if (starts_with_ci(buf, "END:VTODO")) {
			if (!vtodo.has_priority)
				vtodo.priority = LOWEST;
			if (vtodo.completed)
				vtodo.priority = -vtodo.priority;
			if (!vtodo.mesg) {
				ical_log(log, ICAL_VTODO, ITEMLINE,
					 _("could not retrieve item summary."));
				goto cleanup;
			}

			ical_store_todo(vtodo.priority, vtodo.mesg,
					vtodo.note, list);
			(*notodos)++;
			return;
		}

		if (starts_with_ci(buf, "PRIORITY:")) {
			sscanf(buf, "%d", &vtodo.priority);
			if (vtodo.priority >= 1 && vtodo.priority <= 9) {
				vtodo.has_priority = 1;
			} else {
				ical_log(log, ICAL_VTODO, ITEMLINE,
					 _("item priority is not acceptable "
					  "(must be between 1 and 9)."));
			}
		} else if (starts_with_ci(buf, "STATUS:COMPLETED")) {
			vtodo.completed = 1;
		} else if (starts_with_ci(buf, "SUMMARY")) {
			vtodo.mesg = ical_read_summary(buf);
		} else if (starts_with_ci(buf, "BEGIN:VALARM")) {
			skip_alarm = 1;
		} else if (starts_with_ci(buf, "DESCRIPTION")) {
			vtodo.note = ical_read_note(buf, noskipped, ICAL_VTODO,
					ITEMLINE, log);
		}
	}

	ical_log(log, ICAL_VTODO, ITEMLINE,
		 _("The ical file seems to be malformed. "
		   "The end of item was not found."));

cleanup:
	if (vtodo.note)
		mem_free(vtodo.note);
	if (vtodo.mesg)
		mem_free(vtodo.mesg);
	(*noskipped)++;
}

/* Import calcurse data. */
void
ical_import_data(FILE * stream, FILE * log, int list, unsigned *events,
		 unsigned *apoints, unsigned *todos, unsigned *lines,
		 unsigned *skipped)
{
	char buf[BUFSIZ], lstore[BUFSIZ];
	int major, minor;

	ical_readline_init(stream, buf, lstore, lines);
	RETURN_IF(!ical_chk_header
		  (stream, buf, lstore, lines, &major, &minor),
		  _("Warning: ical header malformed or wrong version number. "
		   "Aborting..."));

	ical_log_init(log, major, minor);

	while (ical_readline(stream, buf, lstore, lines)) {
		(*lines)++;
		if (starts_with_ci(buf, "BEGIN:VEVENT")) {
			ical_read_event(stream, log, list, events, apoints,
					skipped, buf, lstore, lines);
		} else if (starts_with_ci(buf, "BEGIN:VTODO")) {
			ical_read_todo(stream, log, list, todos, skipped, buf,
				       lstore, lines);
		}
	}
}

/* Export calcurse data. */
void ical_export_data(FILE * stream, int export_uid)
{
	ical_export_header(stream);
	ical_export_recur_events(stream, export_uid);
	ical_export_events(stream, export_uid);
	ical_export_recur_apoints(stream, export_uid);
	ical_export_apoints(stream, export_uid);
	ical_export_todo(stream, export_uid);
	ical_export_footer(stream);
}