Integrated the DKIM signer into serv_smtpclient, but disabled it
[citadel.git] / webcit / availability.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
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.
6  *
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.
11  */
12
13
14 #include "webcit.h"
15
16 #include "calendar.h"
17
18 /*
19  * Utility function to fetch a VFREEBUSY type of thing for any specified user.
20  */
21 icalcomponent *get_freebusy_for_user(char *who) {
22         long nLines;
23         char buf[SIZ];
24         StrBuf *serialized_fb = NewStrBuf();
25         icalcomponent *fb = NULL;
26
27         serv_printf("ICAL freebusy|%s", who);
28         serv_getln(buf, sizeof buf);
29         if (buf[0] == '1') {
30                 read_server_text(serialized_fb, &nLines);
31         }
32
33         if (serialized_fb == NULL) {
34                 return NULL;
35         }
36         
37         fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
38         FreeStrBuf(&serialized_fb);
39         if (fb == NULL) {
40                 return NULL;
41         }
42
43         return(fb);
44 }
45
46
47 /*
48  * Back end function for check_attendee_availability()
49  * This one checks an individual attendee against a supplied
50  * event start and end time.  All these fields have already been
51  * broken out.  
52  *
53  * attendee_string      name of the attendee
54  * event_start          start time of the event to check
55  * event_end            end time of the event to check
56  *
57  * The result is placed in 'annotation'.
58  */
59 void check_individual_attendee(char *attendee_string,
60                                 struct icaltimetype event_start,
61                                 struct icaltimetype event_end,
62                                 char *annotation) {
63
64         icalcomponent *fbc = NULL;
65         icalcomponent *fb = NULL;
66         icalproperty *thisfb = NULL;
67         struct icalperiodtype period;
68
69         /*
70          * Set to 'unknown' right from the beginning.  Unless we learn
71          * something else, that's what we'll go with.
72          */
73         strcpy(annotation, _("availability unknown"));
74
75         fbc = get_freebusy_for_user(attendee_string);
76         if (fbc == NULL) {
77                 return;
78         }
79
80         /*
81          * Make sure we're looking at a VFREEBUSY by itself.  What we're probably
82          * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
83          */
84         if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
85                 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
86         }
87         else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
88                 fb = fbc;
89         }
90
91         /* Iterate through all FREEBUSY's looking for conflicts. */
92         if (fb != NULL) {
93
94                 strcpy(annotation, _("free"));
95
96                 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
97                     thisfb != NULL;
98                     thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
99
100                         /** Do the check */
101                         period = icalproperty_get_freebusy(thisfb);
102                         if (ical_ctdl_is_overlap(period.start, period.end,
103                            event_start, event_end)) {
104                                 strcpy(annotation, _("BUSY"));
105                         }
106
107                 }
108         }
109
110         icalcomponent_free(fbc);
111 }
112
113
114
115
116 /*
117  * Check the availability of all attendees for an event (when possible)
118  * and annotate accordingly.
119  *
120  * vevent       the event which should be compared with attendees calendar
121  */
122 void check_attendee_availability(icalcomponent *vevent) {
123         icalproperty *attendee = NULL;
124         icalproperty *dtstart_p = NULL;
125         icalproperty *dtend_p = NULL;
126         struct icaltimetype dtstart_t;
127         struct icaltimetype dtend_t;
128         char attendee_string[SIZ];
129         char annotated_attendee_string[SIZ];
130         char annotation[SIZ];
131         const char *ch;
132
133         if (vevent == NULL) {
134                 return;
135         }
136
137         /*
138          * If we're looking at a fully encapsulated VCALENDAR
139          * rather than a VEVENT component, attempt to use the first
140          * relevant VEVENT subcomponent.  If there is none, the
141          * NULL returned by icalcomponent_get_first_component() will
142          * tell the next iteration of this function to create a
143          * new one.
144          */
145         if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
146                 check_attendee_availability(
147                         icalcomponent_get_first_component(
148                                 vevent, ICAL_VEVENT_COMPONENT
149                         )
150                 );
151                 return;
152         }
153
154         ical_dezonify(vevent);          /**< Convert everything to UTC */
155
156         /*
157          * Learn the start and end times.
158          */
159         dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
160         if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
161
162         dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
163         if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
164
165         /*
166          * Iterate through attendees.
167          */
168         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
169             attendee != NULL;
170             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
171                 ch = icalproperty_get_attendee(attendee);
172                 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
173
174                         /** screen name or email address */
175                         safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
176                         string_trim(attendee_string);
177
178                         check_individual_attendee(attendee_string,
179                                                 dtstart_t, dtend_t,
180                                                 annotation);
181
182                         /** Replace the attendee name with an annotated one. */
183                         snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
184                                 "MAILTO:%s (%s)", attendee_string, annotation);
185                         icalproperty_set_attendee(attendee, annotated_attendee_string);
186
187                 }
188         }
189
190 }
191