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