ICAL: implement server backend for conflict handling.
[citadel.git] / webcit / ical_subst.c
index e48b2f9902b0c9aa5a55d7435f1ab0b21fe2e136..bd44e42b96fea2efc126a8c9038b99876c894398 100644 (file)
@@ -20,6 +20,8 @@ CtxType CTX_ICAL = CTX_NONE;
 CtxType CTX_ICALPROPERTY = CTX_NONE;
 CtxType CTX_ICALMETHOD = CTX_NONE;
 CtxType CTX_ICALTIME = CTX_NONE;
+CtxType CTX_ICALATTENDEE = CTX_NONE;
+CtxType CTX_ICALCONFLICT = CTX_NONE;
 #if 0
 void SortPregetMatter(HashList *Cals)
 {
@@ -167,7 +169,7 @@ int cond_ICalHaveTimeItem(StrBuf *Target, WCTemplputParams *TP)
 
                DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams) + 
                                                       sizeof(struct icaltimetype));
-               t = (struct icaltimetype *) ((char*)DynamicTP) + sizeof(WCTemplputParams);
+               t = (struct icaltimetype *) &DynamicTP[1];
                memset(&tt, 0, sizeof(struct icaltimetype));
                switch (Kind)
                {
@@ -265,14 +267,16 @@ void tmplput_CtxICalPropertyDate(StrBuf *Target, WCTemplputParams *TP)
 
 
 
-void render_MIME_ICS_TPL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
+void render_MIME_ICS_TPL(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
 {
+       wc_mime_attachment *Mime = CTX(CTX_MIME_ATACH);
        icalproperty_method the_method = ICAL_METHOD_NONE;
        icalproperty *method = NULL;
        icalcomponent *cal;
        icalcomponent *c;
         WCTemplputParams SubTP;
         WCTemplputParams SuperTP;
+
        static int divcount = 0;
 
        if (StrLength(Mime->Data) == 0) {
@@ -288,6 +292,8 @@ void render_MIME_ICS_TPL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *Foun
        }
 
        putlbstr("divname",  ++divcount);
+
+
        putbstr("cal_partnum", NewStrBufDup(Mime->PartNum));
        putlbstr("msgnum", Mime->msgnum);
 
@@ -305,8 +311,12 @@ void render_MIME_ICS_TPL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *Foun
                the_method = icalproperty_get_method(method);
        }
 
-       SuperTP.Context = &the_method;
-       SuperTP.Filter.ContextType = CTX_ICALMETHOD,
+       StackContext (TP,
+                     &SuperTP,
+                     &the_method,
+                     CTX_ICALMETHOD,
+                     0,
+                     TP->Tokens);
 
        StackContext (&SuperTP, 
                      &SubTP, 
@@ -327,6 +337,7 @@ void render_MIME_ICS_TPL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *Foun
                "EnableOrDisableCheckButton();  \n"
        );
 
+       UnStackContext(&SuperTP);
        UnStackContext(&SubTP);
        icalcomponent_free(cal);
 }
@@ -367,41 +378,160 @@ int cond_ICalIsMethod(StrBuf *Target, WCTemplputParams *TP)
 }
 
 
+typedef struct CalendarConflict
+{
+       long is_update;
+       long existing_msgnum;
+       StrBuf *conflict_event_uid;
+       StrBuf *conflict_event_summary;
+}CalendarConflict;
+void DeleteConflict(void *vConflict)
+{
+       CalendarConflict *c = (CalendarConflict *) vConflict;
 
+       FreeStrBuf(&c->conflict_event_uid);
+       FreeStrBuf(&c->conflict_event_summary);
+       free(c);
+}
+HashList *iterate_FindConflict(StrBuf *Target, WCTemplputParams *TP)
+{
+       StrBuf *Line;
+       HashList *Conflicts = NULL;
+       CalendarConflict *Conflict;
+       wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
+
+       serv_printf("ICAL conflicts|%ld|%s|", Mime->msgnum, ChrPtr(Mime->PartNum));
+
+       Line = NewStrBuf();
+       StrBuf_ServGetln(Line);
+       if (GetServerStatus(Line, NULL) == 1)
+       {
+               const char *Pos = NULL;
+               int Done = 0;
+               int n = 0;
+               Conflicts = NewHash(1, Flathash);
+               while(!Done && (StrBuf_ServGetln(Line) >= 0) )
+                       if ( (StrLength(Line)==3) && 
+                            !strcmp(ChrPtr(Line), "000")) 
+                       {
+                               Done = 1;
+                       }
+                       else {
+                               Conflict = (CalendarConflict *) malloc(sizeof(CalendarConflict *));
+                               Conflict->conflict_event_uid = NewStrBufPlain(NULL, StrLength(Line));
+                               Conflict->conflict_event_summary = NewStrBufPlain(NULL, StrLength(Line));
+
+                               Conflict->existing_msgnum = StrBufExtractNext_long(Line, &Pos, '|');
+                               StrBufSkip_NTokenS(Line, &Pos, '|', 1);
+                               StrBufExtract_NextToken(Conflict->conflict_event_uid, Line, &Pos, '|');
+                               StrBufExtract_NextToken(Conflict->conflict_event_summary, Line, &Pos, '|');
+                               Conflict->is_update = StrBufExtractNext_long(Line, &Pos, '|');
+
+                               Put(Conflicts, IKEY(n), Conflict, DeleteConflict);
+                               n++;
+                               Pos = NULL;
+                       }
+       }
+       syslog(9, "...done.\n");
+       return Conflicts;
+}
 
 
 
+void tmplput_ConflictEventMsgID(StrBuf *Target, WCTemplputParams *TP)
+{
+       CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
+       char buf[sizeof(long) * 16];
 
-void tmplput_Conflict(StrBuf *Target, WCTemplputParams *TP)
-{}
+       snprintf(buf, sizeof(buf), "%ld", C->existing_msgnum);
+       StrBufAppendTemplateStr(Target, TP, buf, 0);
+}
+void tmplput_ConflictEUID(StrBuf *Target, WCTemplputParams *TP)
+{
+       CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
+       
+       StrBufAppendTemplate(Target, TP, C->conflict_event_uid, 0);
+}
+void tmplput_ConflictSummary(StrBuf *Target, WCTemplputParams *TP)
+{
+       CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
 
-HashList* IterateGetAttendees()
+       StrBufAppendTemplate(Target, TP, C->conflict_event_summary, 0);
+}
+int cond_ConflictIsUpdate(StrBuf *Target, WCTemplputParams *TP)
 {
-/*
-       /* If the component has attendees, iterate through them. * /
+       CalendarConflict *C = (CalendarConflict *) CTX(CTX_ICALCONFLICT);
+       return C->is_update;
+}
+
+typedef struct CalAttendee
+{
+       StrBuf *AttendeeStr;
+       icalparameter_partstat partstat;
+} CalAttendee;
+
+void DeleteAtt(void *vAtt)
+{
+       CalAttendee *att = (CalAttendee*) vAtt;
+       FreeStrBuf(&att->AttendeeStr);
+       free(vAtt);
+}
+
+HashList *iterate_get_ical_attendees(StrBuf *Target, WCTemplputParams *TP)
+{
+       icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
+       icalparameter *partstat_param;
+       icalproperty *p;
+       CalAttendee *Att;
+       HashList *Attendees = NULL;
+       const char *ch;
+       int n = 0;
+
+       /* If the component has attendees, iterate through them. */
        for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); 
             (p != NULL); 
             p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
-               StrBufAppendPrintf(Target, "<dt>");
-               StrBufAppendPrintf(Target, _("Attendee:"));
-               StrBufAppendPrintf(Target, "</dt><dd>");
                ch = icalproperty_get_attendee(p);
-               if ((ch != NULL) && !strncasecmp(buf, "MAILTO:", 7)) {
-
-                       /** screen name or email address * /
-                       safestrncpy(buf, ch + 7, sizeof(buf));
-                       striplt(buf);
-                       StrEscAppend(Target, NULL, buf, 0, 0);
-                       StrBufAppendPrintf(Target, " ");
-
-                       /** participant status * /
-                       partstat_as_string(buf, p);
-                       StrEscAppend(Target, NULL, buf, 0, 0);
+               if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
+                       Att = (CalAttendee*) malloc(sizeof(CalAttendee));
+
+                       /** screen name or email address */
+                       Att->AttendeeStr = NewStrBufPlain(ch + 7, -1);
+                       StrBufTrim(Att->AttendeeStr);
+
+                       /** participant status */
+                       partstat_param = icalproperty_get_first_parameter(
+                               p,
+                               ICAL_PARTSTAT_PARAMETER
+                               );
+                       if (partstat_param == NULL) {
+                               Att->partstat = ICAL_PARTSTAT_X;
+                       }
+                       else {
+                               Att->partstat = icalparameter_get_partstat(partstat_param);
+                       }
+                       if (Attendees == NULL)
+                               Attendees = NewHash(1, Flathash);
+                       Put(Attendees, IKEY(n), Att, DeleteAtt);
+                       n++;
                }
-               StrBufAppendPrintf(Target, "</dd>\n");
        }
-*/
-       return NULL;
+       return Attendees;
+}
+
+void tmplput_ICalAttendee(StrBuf *Target, WCTemplputParams *TP)
+{
+       CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
+       StrBufAppendTemplate(Target, TP, Att->AttendeeStr, 0);
+}
+int cond_ICalAttendeeState(StrBuf *Target, WCTemplputParams *TP)
+{
+       CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
+       icalparameter_partstat which_partstat;
+
+       which_partstat = GetTemplateTokenNumber(Target, TP, 2, ICAL_PARTSTAT_X);
+       return Att->partstat == which_partstat;
+}
        /* If the component has subcomponents, recurse through them. * /
        for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
             (c != 0);
@@ -410,8 +540,6 @@ HashList* IterateGetAttendees()
                cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
        }
        */
-}
-
 
 
 void 
@@ -428,7 +556,20 @@ InitModule_ICAL_SUBST
        RegisterConditional("COND:ICAL:PROPERTY", 1, cond_ICalHaveItem, CTX_ICAL);
        RegisterConditional("COND:ICAL:IS:A", 1, cond_ICalIsA, CTX_ICAL);
 
-       RegisterNamespace("ICAL:SERV:CHECK:CONFLICT", 0, 0, tmplput_Conflict, NULL, CTX_ICAL);
+
+        RegisterIterator("ICAL:CONFLICT", 0, NULL, iterate_FindConflict, 
+                         NULL, NULL, CTX_MIME_ATACH, CTX_ICALCONFLICT, IT_NOFLAG);
+       RegisterNamespace("ICAL:CONFLICT:MSGID", 0, 1, tmplput_ConflictEventMsgID, NULL, CTX_ICALCONFLICT);
+       RegisterNamespace("ICAL:CONFLICT:EUID", 0, 1, tmplput_ConflictEUID, NULL, CTX_ICALCONFLICT);
+       RegisterNamespace("ICAL:CONFLICT:SUMMARY", 0, 1, tmplput_ConflictSummary, NULL, CTX_ICALCONFLICT);
+       RegisterConditional("ICAL:CONFLICT:IS:UPDATE", 0, cond_ConflictIsUpdate, CTX_ICALCONFLICT);
+
+
+       RegisterCTX(CTX_ICALATTENDEE);
+        RegisterIterator("ICAL:ATTENDEES", 0, NULL, iterate_get_ical_attendees, 
+                         NULL, NULL, CTX_ICALATTENDEE, CTX_ICAL, IT_NOFLAG);
+       RegisterNamespace("ICAL:ATTENDEE", 1, 2, tmplput_ICalAttendee, NULL, CTX_ICALATTENDEE);
+       RegisterConditional("COND:ICAL:ATTENDEE", 1, cond_ICalAttendeeState, CTX_ICALATTENDEE);
 
        RegisterCTX(CTX_ICALPROPERTY);
        RegisterNamespace("ICAL:ITEM", 1, 2, tmplput_ICalItem, NULL, CTX_ICAL);
@@ -455,4 +596,25 @@ ServerShutdownModule_ICAL
 
 
 
-
+/*
+                       if (is_update) {
+                               snprintf(conflict_message, sizeof conflict_message,
+                                        _("This is an update of '%s' which is already in your calendar."), conflict_name);
+                       }
+                       else {
+                               snprintf(conflict_message, sizeof conflict_message,
+                                        _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
+                       }
+
+                       StrBufAppendPrintf(Target, "<dt>%s",
+                                          (is_update ?
+                                           _("Update:") :
+                                           _("CONFLICT:")
+                                                  )
+                               );
+                       StrBufAppendPrintf(Target, "</dt><dd>");
+                       StrEscAppend(Target, NULL, conflict_message, 0, 0);
+                       StrBufAppendPrintf(Target, "</dd>\n");
+                       }
+               }
+/*/