2 * Copyright (c) 1996-2012 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
15 extern IcalKindEnumMap icalproperty_kind_map[];
16 extern IcalMethodEnumMap icalproperty_method_map[];
18 HashList *IcalComponentMap = NULL;
19 CtxType CTX_ICAL = CTX_NONE;
20 CtxType CTX_ICALPROPERTY = CTX_NONE;
21 CtxType CTX_ICALMETHOD = CTX_NONE;
22 CtxType CTX_ICALTIME = CTX_NONE;
23 CtxType CTX_ICALATTENDEE = CTX_NONE;
24 CtxType CTX_ICALCONFLICT = CTX_NONE;
26 void SortPregetMatter(HashList *Cals)
32 IcalEnumMap *SortMap[10];
35 const char *Next = NULL;
36 const StrBuf *SortVector;
41 SortVector = SBSTR("ICALSortVec");
42 if (SortVector == NULL)
45 for (i = 0; i < 10; i++) SortMap[i] = NULL;
47 while (StrBufExtract_NextToken(SortBy, SortVector, &Next, ':') > 0) {
48 GetHash(IcalComponentMap, SKEY(SortBy), &vSort);
49 Map = (IcalEnumMap*) vSort;
59 switch (SortMap[i - 1]->map) {
66 It = GetNewHashPos(Cals, 0);
67 while (GetNextHashPos(Cals, It, &KLen, &Key, &vCal)) {
69 Cal = (disp_cal*) vCal;
70 Cal->Status = icalcomponent_get_status(Cal->cal);
71 Cal->SortBy = Cal->cal;
74 while ((SortMap[i] != NULL) &&
75 (Cal->SortBy != NULL))
77 /****Cal->SortBy = icalcomponent_get_first_property(Cal->SortBy, SortMap[i++]->map); */
84 void tmplput_ICalItem(StrBuf *Target, WCTemplputParams *TP)
86 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
88 icalproperty_kind Kind;
91 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
92 p = icalcomponent_get_first_property(cal, Kind);
94 str = icalproperty_get_comment (p);
95 StrBufAppendTemplateStr(Target, TP, str, 1);
99 void tmplput_CtxICalProperty(StrBuf *Target, WCTemplputParams *TP)
101 icalproperty *p = (icalproperty *) CTX(CTX_ICALPROPERTY);
104 str = icalproperty_get_comment (p);
105 StrBufAppendTemplateStr(Target, TP, str, 0);
108 int ReleaseIcalSubCtx(StrBuf *Target, WCTemplputParams *TP)
110 WCTemplputParams *TPP = TP;
115 int cond_ICalIsA(StrBuf *Target, WCTemplputParams *TP)
117 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
118 icalcomponent_kind c = GetTemplateTokenNumber(Target, TP, 2, ICAL_NO_COMPONENT);
119 return icalcomponent_isa(cal) == c;
122 int cond_ICalHaveItem(StrBuf *Target, WCTemplputParams *TP)
124 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
126 icalproperty_kind Kind;
128 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 2, ICAL_ANY_PROPERTY);
129 p = icalcomponent_get_first_property(cal, Kind);
131 WCTemplputParams *DynamicTP;
133 DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams));
134 StackDynamicContext (TP,
141 TP->Tokens->Params[1]->lvalue);
148 int ReleaseIcalTimeCtx(StrBuf *Target, WCTemplputParams *TP)
150 WCTemplputParams *TPP = TP;
157 int cond_ICalHaveTimeItem(StrBuf *Target, WCTemplputParams *TP)
159 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
161 icalproperty_kind Kind;
163 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 2, ICAL_ANY_PROPERTY);
164 p = icalcomponent_get_first_property(cal, Kind);
166 struct icaltimetype *t;
167 struct icaltimetype tt;
168 WCTemplputParams *DynamicTP;
170 DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams) +
171 sizeof(struct icaltimetype));
172 t = (struct icaltimetype *) &DynamicTP[1];
173 memset(&tt, 0, sizeof(struct icaltimetype));
176 case ICAL_DTSTART_PROPERTY:
177 tt = icalproperty_get_dtstart(p);
179 case ICAL_DTEND_PROPERTY:
180 tt = icalproperty_get_dtend(p);
185 memcpy(t, &tt, sizeof(struct icaltimetype));
187 StackDynamicContext (TP,
194 TP->Tokens->Params[1]->lvalue);
202 int cond_ICalTimeIsDate(StrBuf *Target, WCTemplputParams *TP)
204 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
208 void tmplput_ICalTime_Date(StrBuf *Target, WCTemplputParams *TP)
213 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
215 memset(&d_tm, 0, sizeof d_tm);
216 d_tm.tm_year = t->year - 1900;
217 d_tm.tm_mon = t->month - 1;
218 d_tm.tm_mday = t->day;
219 len = wc_strftime(buf, sizeof(buf), "%x", &d_tm);
220 StrBufAppendBufPlain(Target, buf, len, 0);
222 void tmplput_ICalTime_Time(StrBuf *Target, WCTemplputParams *TP)
226 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
229 tt = icaltime_as_timet(*t);
230 len = webcit_fmt_date(buf, sizeof(buf), tt, DATEFMT_FULL);
231 StrBufAppendBufPlain(Target, buf, len, 0);
234 void tmplput_ICalDate(StrBuf *Target, WCTemplputParams *TP)
236 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
238 icalproperty_kind Kind;
239 struct icaltimetype t;
243 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
244 p = icalcomponent_get_first_property(cal, Kind);
247 t = icalproperty_get_dtend(p);
248 tt = icaltime_as_timet(t);
249 len = webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
250 StrBufAppendBufPlain(Target, buf, len, 0);
254 void tmplput_CtxICalPropertyDate(StrBuf *Target, WCTemplputParams *TP)
256 icalproperty *p = (icalproperty *) CTX(CTX_ICALPROPERTY);
257 struct icaltimetype t;
262 t = icalproperty_get_dtend(p);
263 tt = icaltime_as_timet(t);
264 len = webcit_fmt_date(buf, sizeof(buf), tt, DATEFMT_FULL);
265 StrBufAppendBufPlain(Target, buf, len, 0);
270 void render_MIME_ICS_TPL(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
272 wc_mime_attachment *Mime = CTX(CTX_MIME_ATACH);
273 icalproperty_method the_method = ICAL_METHOD_NONE;
274 icalproperty *method = NULL;
275 icalcomponent *cal = NULL;
276 icalcomponent *c = NULL;
277 WCTemplputParams SubTP;
278 WCTemplputParams SuperTP;
280 static int divcount = 0;
282 if (StrLength(Mime->Data) == 0) {
285 if (StrLength(Mime->Data) > 0) {
286 cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
289 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
290 StrBufAppendPrintf(Mime->Data, "<br>\n");
294 putlbstr("divname", ++divcount);
297 putbstr("cal_partnum", NewStrBufDup(Mime->PartNum));
298 putlbstr("msgnum", Mime->msgnum);
300 memset(&SubTP, 0, sizeof(WCTemplputParams));
301 memset(&SuperTP, 0, sizeof(WCTemplputParams));
303 /*//ical_dezonify(cal); */
305 /* If the component has subcomponents, recurse through them. */
306 c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
307 c = (c != NULL) ? c : cal;
309 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
310 if (method != NULL) {
311 the_method = icalproperty_get_method(method);
321 StackContext (&SuperTP,
327 FlushStrBuf(Mime->Data);
328 /// DoTemplate(HKEY("ical_attachment_display"), Mime->Data, &SubTP);
329 DoTemplate(HKEY("ical_edit"), Mime->Data, &SubTP);
331 /*/ cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum)); */
333 /* Free the memory we obtained from libical's constructor */
334 StrBufPlain(Mime->ContentType, HKEY("text/html"));
335 StrBufAppendPrintf(WC->trailing_javascript,
336 "eventEditAllDay(); \n"
337 "RecurrenceShowHide(); \n"
338 "EnableOrDisableCheckButton(); \n"
341 UnStackContext(&SuperTP);
342 UnStackContext(&SubTP);
343 icalcomponent_free(cal);
345 void CreateIcalComponendKindLookup(void)
349 IcalComponentMap = NewHash (1, NULL);
350 while (icalproperty_kind_map[i].NameLen != 0) {
351 RegisterNS(icalproperty_kind_map[i].Name,
352 icalproperty_kind_map[i].NameLen,
358 Put(IcalComponentMap,
359 icalproperty_kind_map[i].Name,
360 icalproperty_kind_map[i].NameLen,
361 &icalproperty_kind_map[i],
362 reference_free_handler);
372 int cond_ICalIsMethod(StrBuf *Target, WCTemplputParams *TP)
374 icalproperty_method *the_method = (icalproperty_method *) CTX(CTX_ICALMETHOD);
375 icalproperty_method which_method;
377 which_method = GetTemplateTokenNumber(Target, TP, 2, ICAL_METHOD_X);
378 return *the_method == which_method;
382 typedef struct CalendarConflict
385 long existing_msgnum;
386 StrBuf *conflict_event_uid;
387 StrBuf *conflict_event_summary;
389 void DeleteConflict(void *vConflict)
391 CalendarConflict *c = (CalendarConflict *) vConflict;
393 FreeStrBuf(&c->conflict_event_uid);
394 FreeStrBuf(&c->conflict_event_summary);
397 HashList *iterate_FindConflict(StrBuf *Target, WCTemplputParams *TP)
400 HashList *Conflicts = NULL;
401 CalendarConflict *Conflict;
402 wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
404 serv_printf("ICAL conflicts|%ld|%s|", Mime->msgnum, ChrPtr(Mime->PartNum));
407 StrBuf_ServGetln(Line);
408 if (GetServerStatus(Line, NULL) == 1)
410 const char *Pos = NULL;
413 Conflicts = NewHash(1, Flathash);
414 while(!Done && (StrBuf_ServGetln(Line) >= 0) )
415 if ( (StrLength(Line)==3) &&
416 !strcmp(ChrPtr(Line), "000"))
421 Conflict = (CalendarConflict *) malloc(sizeof(CalendarConflict));
422 Conflict->conflict_event_uid = NewStrBufPlain(NULL, StrLength(Line));
423 Conflict->conflict_event_summary = NewStrBufPlain(NULL, StrLength(Line));
425 Conflict->existing_msgnum = StrBufExtractNext_long(Line, &Pos, '|');
426 StrBufSkip_NTokenS(Line, &Pos, '|', 1);
427 StrBufExtract_NextToken(Conflict->conflict_event_uid, Line, &Pos, '|');
428 StrBufExtract_NextToken(Conflict->conflict_event_summary, Line, &Pos, '|');
429 Conflict->is_update = StrBufExtractNext_long(Line, &Pos, '|');
431 Put(Conflicts, IKEY(n), Conflict, DeleteConflict);
437 syslog(LOG_DEBUG, "...done.\n");
443 void tmplput_ConflictEventMsgID(StrBuf *Target, WCTemplputParams *TP)
445 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
446 char buf[sizeof(long) * 16];
448 snprintf(buf, sizeof(buf), "%ld", C->existing_msgnum);
449 StrBufAppendTemplateStr(Target, TP, buf, 0);
451 void tmplput_ConflictEUID(StrBuf *Target, WCTemplputParams *TP)
453 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
455 StrBufAppendTemplate(Target, TP, C->conflict_event_uid, 0);
457 void tmplput_ConflictSummary(StrBuf *Target, WCTemplputParams *TP)
459 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
461 StrBufAppendTemplate(Target, TP, C->conflict_event_summary, 0);
463 int cond_ConflictIsUpdate(StrBuf *Target, WCTemplputParams *TP)
465 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
469 typedef struct CalAttendee
472 icalparameter_partstat partstat;
475 void DeleteAtt(void *vAtt)
477 CalAttendee *att = (CalAttendee*) vAtt;
478 FreeStrBuf(&att->AttendeeStr);
482 HashList *iterate_get_ical_attendees(StrBuf *Target, WCTemplputParams *TP)
484 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
485 icalparameter *partstat_param;
488 HashList *Attendees = NULL;
492 /* If the component has attendees, iterate through them. */
493 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY);
495 p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
496 ch = icalproperty_get_attendee(p);
497 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
498 Att = (CalAttendee*) malloc(sizeof(CalAttendee));
500 /** screen name or email address */
501 Att->AttendeeStr = NewStrBufPlain(ch + 7, -1);
502 StrBufTrim(Att->AttendeeStr);
504 /** participant status */
505 partstat_param = icalproperty_get_first_parameter(
507 ICAL_PARTSTAT_PARAMETER
509 if (partstat_param == NULL) {
510 Att->partstat = ICAL_PARTSTAT_X;
513 Att->partstat = icalparameter_get_partstat(partstat_param);
515 if (Attendees == NULL)
516 Attendees = NewHash(1, Flathash);
517 Put(Attendees, IKEY(n), Att, DeleteAtt);
524 void tmplput_ICalAttendee(StrBuf *Target, WCTemplputParams *TP)
526 CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
527 StrBufAppendTemplate(Target, TP, Att->AttendeeStr, 0);
529 int cond_ICalAttendeeState(StrBuf *Target, WCTemplputParams *TP)
531 CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
532 icalparameter_partstat which_partstat;
534 which_partstat = GetTemplateTokenNumber(Target, TP, 2, ICAL_PARTSTAT_X);
535 return Att->partstat == which_partstat;
537 /* If the component has subcomponents, recurse through them. * /
538 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
540 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
541 // Recursively process subcomponent
542 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
548 InitModule_ICAL_SUBST
551 RegisterCTX(CTX_ICAL);
553 RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS_TPL, 1, 501);
554 RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS_TPL, 1, 500);
557 CreateIcalComponendKindLookup ();
558 RegisterConditional("COND:ICAL:PROPERTY", 1, cond_ICalHaveItem, CTX_ICAL);
559 RegisterConditional("COND:ICAL:IS:A", 1, cond_ICalIsA, CTX_ICAL);
562 RegisterIterator("ICAL:CONFLICT", 0, NULL, iterate_FindConflict,
563 NULL, DeleteHash, CTX_MIME_ATACH, CTX_ICALCONFLICT, IT_NOFLAG);
564 RegisterNamespace("ICAL:CONFLICT:MSGID", 0, 1, tmplput_ConflictEventMsgID, NULL, CTX_ICALCONFLICT);
565 RegisterNamespace("ICAL:CONFLICT:EUID", 0, 1, tmplput_ConflictEUID, NULL, CTX_ICALCONFLICT);
566 RegisterNamespace("ICAL:CONFLICT:SUMMARY", 0, 1, tmplput_ConflictSummary, NULL, CTX_ICALCONFLICT);
567 RegisterConditional("ICAL:CONFLICT:IS:UPDATE", 0, cond_ConflictIsUpdate, CTX_ICALCONFLICT);
570 RegisterCTX(CTX_ICALATTENDEE);
571 RegisterIterator("ICAL:ATTENDEES", 0, NULL, iterate_get_ical_attendees,
572 NULL, DeleteHash, CTX_ICALATTENDEE, CTX_ICAL, IT_NOFLAG);
573 RegisterNamespace("ICAL:ATTENDEE", 1, 2, tmplput_ICalAttendee, NULL, CTX_ICALATTENDEE);
574 RegisterConditional("COND:ICAL:ATTENDEE", 1, cond_ICalAttendeeState, CTX_ICALATTENDEE);
576 RegisterCTX(CTX_ICALPROPERTY);
577 RegisterNamespace("ICAL:ITEM", 1, 2, tmplput_ICalItem, NULL, CTX_ICAL);
578 RegisterNamespace("ICAL:PROPERTY:STR", 0, 1, tmplput_CtxICalProperty, NULL, CTX_ICALPROPERTY);
579 RegisterNamespace("ICAL:PROPERTY:DATE", 0, 1, tmplput_CtxICalPropertyDate, NULL, CTX_ICALPROPERTY);
581 RegisterCTX(CTX_ICALMETHOD);
582 RegisterConditional("COND:ICAL:METHOD", 1, cond_ICalIsMethod, CTX_ICALMETHOD);
585 RegisterCTX(CTX_ICALTIME);
586 RegisterConditional("COND:ICAL:DT:PROPERTY", 1, cond_ICalHaveTimeItem, CTX_ICAL);
587 RegisterConditional("COND:ICAL:DT:ISDATE", 0, cond_ICalTimeIsDate, CTX_ICALTIME);
588 RegisterNamespace("ICAL:DT:DATE", 0, 1, tmplput_ICalTime_Date, NULL, CTX_ICALTIME);
589 RegisterNamespace("ICAL:DT:DATETIME", 0, 1, tmplput_ICalTime_Time, NULL, CTX_ICALTIME);
593 ServerShutdownModule_ICAL
596 DeleteHash(&IcalComponentMap);