* --pedantic cleanup.
[citadel.git] / webcit / availability.c
1 /*
2  * $Id$
3  *
4  */
5
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10 /*
11  * Utility function to fetch a VFREEBUSY type of thing for any specified user.
12  */
13 icalcomponent *get_freebusy_for_user(char *who) {
14         long nLines;
15         char buf[SIZ];
16         StrBuf *serialized_fb = NewStrBuf();
17         icalcomponent *fb = NULL;
18
19         serv_printf("ICAL freebusy|%s", who);
20         serv_getln(buf, sizeof buf);
21         if (buf[0] == '1') {
22                 read_server_text(serialized_fb, &nLines);
23         }
24
25         if (serialized_fb == NULL) {
26                 return NULL;
27         }
28         
29         fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
30         FreeStrBuf(&serialized_fb);
31         if (fb == NULL) {
32                 return NULL;
33         }
34
35         return(fb);
36 }
37
38
39 /*
40  * Check to see if two events overlap.  
41  * (This function is used in both Citadel and WebCit.  If you change it in
42  * one place, change it in the other.  We should seriously consider moving
43  * this function upstream into libical.)
44  *
45  * Returns nonzero if they do overlap.
46  */
47 int ical_ctdl_is_overlap(
48                         struct icaltimetype t1start,
49                         struct icaltimetype t1end,
50                         struct icaltimetype t2start,
51                         struct icaltimetype t2end
52 ) {
53
54         if (icaltime_is_null_time(t1start)) return(0);
55         if (icaltime_is_null_time(t2start)) return(0);
56
57         /* First, check for all-day events */
58         if (t1start.is_date) {
59                 if (!icaltime_compare_date_only(t1start, t2start)) {
60                         return(1);
61                 }
62                 if (!icaltime_is_null_time(t2end)) {
63                         if (!icaltime_compare_date_only(t1start, t2end)) {
64                                 return(1);
65                         }
66                 }
67         }
68
69         if (t2start.is_date) {
70                 if (!icaltime_compare_date_only(t2start, t1start)) {
71                         return(1);
72                 }
73                 if (!icaltime_is_null_time(t1end)) {
74                         if (!icaltime_compare_date_only(t2start, t1end)) {
75                                 return(1);
76                         }
77                 }
78         }
79
80         /* lprintf (9, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d \n",
81                 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
82                 t2start.hour, t2start.minute, t2end.hour, t2end.minute);
83         */
84
85         /* Now check for overlaps using date *and* time. */
86
87         /* First, bail out if either event 1 or event 2 is missing end time. */
88         if (icaltime_is_null_time(t1end)) return(0);
89         if (icaltime_is_null_time(t2end)) return(0);
90
91         /* If event 1 ends before event 2 starts, we're in the clear. */
92         if (icaltime_compare(t1end, t2start) <= 0) return(0);
93         /* lprintf(9, "first passed\n"); */
94
95         /* If event 2 ends before event 1 starts, we're also ok. */
96         if (icaltime_compare(t2end, t1start) <= 0) return(0);
97         /* lprintf(9, "second passed\n"); */
98
99         /* Otherwise, they overlap. */
100         return(1);
101 }
102
103
104
105 /*
106  * \brief dig availability on citserver
107  * Back end function for check_attendee_availability()
108  * This one checks an individual attendee against a supplied
109  * event start and end time.  All these fields have already been
110  * broken out.  
111  * \param attendee_string name of the attendee
112  * \param event_start starttime of the event to check
113  * \param event_end endtime of the event to check
114  * \return The result is placed in 'annotation'.
115  */
116 void check_individual_attendee(char *attendee_string,
117                                 struct icaltimetype event_start,
118                                 struct icaltimetype event_end,
119                                 char *annotation) {
120
121         icalcomponent *fbc = NULL;
122         icalcomponent *fb = NULL;
123         icalproperty *thisfb = NULL;
124         struct icalperiodtype period;
125
126         /**
127          * Set to 'unknown' right from the beginning.  Unless we learn
128          * something else, that's what we'll go with.
129          */
130         strcpy(annotation, _("availability unknown"));
131
132         fbc = get_freebusy_for_user(attendee_string);
133         if (fbc == NULL) {
134                 return;
135         }
136
137         /**
138          * Make sure we're looking at a VFREEBUSY by itself.  What we're probably
139          * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
140          */
141         if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
142                 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
143         }
144         else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
145                 fb = fbc;
146         }
147
148         /** Iterate through all FREEBUSY's looking for conflicts. */
149         if (fb != NULL) {
150
151                 strcpy(annotation, _("free"));
152
153                 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
154                     thisfb != NULL;
155                     thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
156
157                         /** Do the check */
158                         period = icalproperty_get_freebusy(thisfb);
159                         if (ical_ctdl_is_overlap(period.start, period.end,
160                            event_start, event_end)) {
161                                 strcpy(annotation, _("BUSY"));
162                         }
163
164                 }
165         }
166
167         icalcomponent_free(fbc);
168 }
169
170
171
172
173 /**
174  * \brief check attendees availability
175  * Check the availability of all attendees for an event (when possible)
176  * and annotate accordingly.
177  * \param vevent the event which should be compared with attendees calendar
178  */
179 void check_attendee_availability(icalcomponent *vevent) {
180         icalproperty *attendee = NULL;
181         icalproperty *dtstart_p = NULL;
182         icalproperty *dtend_p = NULL;
183         struct icaltimetype dtstart_t;
184         struct icaltimetype dtend_t;
185         char attendee_string[SIZ];
186         char annotated_attendee_string[SIZ];
187         char annotation[SIZ];
188
189         if (vevent == NULL) {
190                 return;
191         }
192
193         /**
194          * If we're looking at a fully encapsulated VCALENDAR
195          * rather than a VEVENT component, attempt to use the first
196          * relevant VEVENT subcomponent.  If there is none, the
197          * NULL returned by icalcomponent_get_first_component() will
198          * tell the next iteration of this function to create a
199          * new one.
200          */
201         if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
202                 check_attendee_availability(
203                         icalcomponent_get_first_component(
204                                 vevent, ICAL_VEVENT_COMPONENT
205                         )
206                 );
207                 return;
208         }
209
210         ical_dezonify(vevent);          /**< Convert everything to UTC */
211
212         /**
213          * Learn the start and end times.
214          */
215         dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
216         if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
217
218         dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
219         if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
220
221         /**
222          * Iterate through attendees.
223          */
224         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
225             attendee != NULL;
226             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
227
228                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
229                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
230
231                         /** screen name or email address */
232                         strcpy(attendee_string, &attendee_string[7]);
233                         striplt(attendee_string);
234
235                         check_individual_attendee(attendee_string,
236                                                 dtstart_t, dtend_t,
237                                                 annotation);
238
239                         /** Replace the attendee name with an annotated one. */
240                         snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
241                                 "MAILTO:%s (%s)", attendee_string, annotation);
242                         icalproperty_set_attendee(attendee, annotated_attendee_string);
243
244                 }
245         }
246
247 }
248