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;
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);
330 /*/ cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum)); */
332 /* Free the memory we obtained from libical's constructor */
333 StrBufPlain(Mime->ContentType, HKEY("text/html"));
334 StrBufAppendPrintf(WC->trailing_javascript,
335 "eventEditAllDay(); \n"
336 "RecurrenceShowHide(); \n"
337 "EnableOrDisableCheckButton(); \n"
340 UnStackContext(&SuperTP);
341 UnStackContext(&SubTP);
342 icalcomponent_free(cal);
344 void CreateIcalComponendKindLookup(void)
348 IcalComponentMap = NewHash (1, NULL);
349 while (icalproperty_kind_map[i].NameLen != 0) {
350 RegisterNS(icalproperty_kind_map[i].Name,
351 icalproperty_kind_map[i].NameLen,
357 Put(IcalComponentMap,
358 icalproperty_kind_map[i].Name,
359 icalproperty_kind_map[i].NameLen,
360 &icalproperty_kind_map[i],
361 reference_free_handler);
371 int cond_ICalIsMethod(StrBuf *Target, WCTemplputParams *TP)
373 icalproperty_method *the_method = (icalproperty_method *) CTX(CTX_ICALMETHOD);
374 icalproperty_method which_method;
376 which_method = GetTemplateTokenNumber(Target, TP, 2, ICAL_METHOD_X);
377 return *the_method == which_method;
381 typedef struct CalendarConflict
384 long existing_msgnum;
385 StrBuf *conflict_event_uid;
386 StrBuf *conflict_event_summary;
388 void DeleteConflict(void *vConflict)
390 CalendarConflict *c = (CalendarConflict *) vConflict;
392 FreeStrBuf(&c->conflict_event_uid);
393 FreeStrBuf(&c->conflict_event_summary);
396 HashList *iterate_FindConflict(StrBuf *Target, WCTemplputParams *TP)
399 HashList *Conflicts = NULL;
400 CalendarConflict *Conflict;
401 wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
403 serv_printf("ICAL conflicts|%ld|%s|", Mime->msgnum, ChrPtr(Mime->PartNum));
406 StrBuf_ServGetln(Line);
407 if (GetServerStatus(Line, NULL) == 1)
409 const char *Pos = NULL;
412 Conflicts = NewHash(1, Flathash);
413 while(!Done && (StrBuf_ServGetln(Line) >= 0) )
414 if ( (StrLength(Line)==3) &&
415 !strcmp(ChrPtr(Line), "000"))
420 Conflict = (CalendarConflict *) malloc(sizeof(CalendarConflict));
421 Conflict->conflict_event_uid = NewStrBufPlain(NULL, StrLength(Line));
422 Conflict->conflict_event_summary = NewStrBufPlain(NULL, StrLength(Line));
424 Conflict->existing_msgnum = StrBufExtractNext_long(Line, &Pos, '|');
425 StrBufSkip_NTokenS(Line, &Pos, '|', 1);
426 StrBufExtract_NextToken(Conflict->conflict_event_uid, Line, &Pos, '|');
427 StrBufExtract_NextToken(Conflict->conflict_event_summary, Line, &Pos, '|');
428 Conflict->is_update = StrBufExtractNext_long(Line, &Pos, '|');
430 Put(Conflicts, IKEY(n), Conflict, DeleteConflict);
436 syslog(9, "...done.\n");
442 void tmplput_ConflictEventMsgID(StrBuf *Target, WCTemplputParams *TP)
444 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
445 char buf[sizeof(long) * 16];
447 snprintf(buf, sizeof(buf), "%ld", C->existing_msgnum);
448 StrBufAppendTemplateStr(Target, TP, buf, 0);
450 void tmplput_ConflictEUID(StrBuf *Target, WCTemplputParams *TP)
452 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
454 StrBufAppendTemplate(Target, TP, C->conflict_event_uid, 0);
456 void tmplput_ConflictSummary(StrBuf *Target, WCTemplputParams *TP)
458 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
460 StrBufAppendTemplate(Target, TP, C->conflict_event_summary, 0);
462 int cond_ConflictIsUpdate(StrBuf *Target, WCTemplputParams *TP)
464 CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
468 typedef struct CalAttendee
471 icalparameter_partstat partstat;
474 void DeleteAtt(void *vAtt)
476 CalAttendee *att = (CalAttendee*) vAtt;
477 FreeStrBuf(&att->AttendeeStr);
481 HashList *iterate_get_ical_attendees(StrBuf *Target, WCTemplputParams *TP)
483 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
484 icalparameter *partstat_param;
487 HashList *Attendees = NULL;
491 /* If the component has attendees, iterate through them. */
492 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY);
494 p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
495 ch = icalproperty_get_attendee(p);
496 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
497 Att = (CalAttendee*) malloc(sizeof(CalAttendee));
499 /** screen name or email address */
500 Att->AttendeeStr = NewStrBufPlain(ch + 7, -1);
501 StrBufTrim(Att->AttendeeStr);
503 /** participant status */
504 partstat_param = icalproperty_get_first_parameter(
506 ICAL_PARTSTAT_PARAMETER
508 if (partstat_param == NULL) {
509 Att->partstat = ICAL_PARTSTAT_X;
512 Att->partstat = icalparameter_get_partstat(partstat_param);
514 if (Attendees == NULL)
515 Attendees = NewHash(1, Flathash);
516 Put(Attendees, IKEY(n), Att, DeleteAtt);
523 void tmplput_ICalAttendee(StrBuf *Target, WCTemplputParams *TP)
525 CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
526 StrBufAppendTemplate(Target, TP, Att->AttendeeStr, 0);
528 int cond_ICalAttendeeState(StrBuf *Target, WCTemplputParams *TP)
530 CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
531 icalparameter_partstat which_partstat;
533 which_partstat = GetTemplateTokenNumber(Target, TP, 2, ICAL_PARTSTAT_X);
534 return Att->partstat == which_partstat;
536 /* If the component has subcomponents, recurse through them. * /
537 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
539 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
540 /* Recursively process subcomponent * /
541 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
547 InitModule_ICAL_SUBST
550 RegisterCTX(CTX_ICAL);
552 RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS_TPL, 1, 501);
553 RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS_TPL, 1, 500);
556 CreateIcalComponendKindLookup ();
557 RegisterConditional("COND:ICAL:PROPERTY", 1, cond_ICalHaveItem, CTX_ICAL);
558 RegisterConditional("COND:ICAL:IS:A", 1, cond_ICalIsA, CTX_ICAL);
561 RegisterIterator("ICAL:CONFLICT", 0, NULL, iterate_FindConflict,
562 NULL, DeleteHash, CTX_MIME_ATACH, CTX_ICALCONFLICT, IT_NOFLAG);
563 RegisterNamespace("ICAL:CONFLICT:MSGID", 0, 1, tmplput_ConflictEventMsgID, NULL, CTX_ICALCONFLICT);
564 RegisterNamespace("ICAL:CONFLICT:EUID", 0, 1, tmplput_ConflictEUID, NULL, CTX_ICALCONFLICT);
565 RegisterNamespace("ICAL:CONFLICT:SUMMARY", 0, 1, tmplput_ConflictSummary, NULL, CTX_ICALCONFLICT);
566 RegisterConditional("ICAL:CONFLICT:IS:UPDATE", 0, cond_ConflictIsUpdate, CTX_ICALCONFLICT);
569 RegisterCTX(CTX_ICALATTENDEE);
570 RegisterIterator("ICAL:ATTENDEES", 0, NULL, iterate_get_ical_attendees,
571 NULL, DeleteHash, CTX_ICALATTENDEE, CTX_ICAL, IT_NOFLAG);
572 RegisterNamespace("ICAL:ATTENDEE", 1, 2, tmplput_ICalAttendee, NULL, CTX_ICALATTENDEE);
573 RegisterConditional("COND:ICAL:ATTENDEE", 1, cond_ICalAttendeeState, CTX_ICALATTENDEE);
575 RegisterCTX(CTX_ICALPROPERTY);
576 RegisterNamespace("ICAL:ITEM", 1, 2, tmplput_ICalItem, NULL, CTX_ICAL);
577 RegisterNamespace("ICAL:PROPERTY:STR", 0, 1, tmplput_CtxICalProperty, NULL, CTX_ICALPROPERTY);
578 RegisterNamespace("ICAL:PROPERTY:DATE", 0, 1, tmplput_CtxICalPropertyDate, NULL, CTX_ICALPROPERTY);
580 RegisterCTX(CTX_ICALMETHOD);
581 RegisterConditional("COND:ICAL:METHOD", 1, cond_ICalIsMethod, CTX_ICALMETHOD);
584 RegisterCTX(CTX_ICALTIME);
585 RegisterConditional("COND:ICAL:DT:PROPERTY", 1, cond_ICalHaveTimeItem, CTX_ICAL);
586 RegisterConditional("COND:ICAL:DT:ISDATE", 0, cond_ICalTimeIsDate, CTX_ICALTIME);
587 RegisterNamespace("ICAL:DT:DATE", 0, 1, tmplput_ICalTime_Date, NULL, CTX_ICALTIME);
588 RegisterNamespace("ICAL:DT:DATETIME", 0, 1, tmplput_ICalTime_Time, NULL, CTX_ICALTIME);
592 ServerShutdownModule_ICAL
595 DeleteHash(&IcalComponentMap);