b5e52db19445ca112cad3962a49999264decff60
[citadel.git] / citadel / server / modules / calendar / calendar_report.c
1 // This is a handler for calendar "report" operations.
2 // It is expected that CALDAV REPORT operations should be able to use this directly.
3 //
4 // Copyright (c) 1987-2024 by the citadel.org team
5 //
6 // This program is open source software.  Use, duplication, or disclosure
7 // are subject to the terms of the GNU General Public License version 3.
8
9 #include <libical/ical.h>
10 #include "../../ctdl_module.h"
11 #include "../../msgbase.h"
12 #include "../../internet_addressing.h"
13 #include "../../room_ops.h"
14 #include "../../euidindex.h"
15 #include "../../default_timezone.h"
16 #include "../../config.h"
17 #include "serv_calendar.h"
18
19
20 // CtdlForEachMessage callback for calendar_query()
21 void calendar_query_backend(long msgnum, void *data) {
22         struct CtdlMessage *msg = NULL;
23         struct ical_respond_data ird;
24
25         syslog(LOG_DEBUG, "calendar_query: calendar_query_backend(%ld)", msgnum);
26
27         // Look for the calendar event...
28         msg = CtdlFetchMessage(msgnum, 1);
29         if (msg == NULL) return;
30         memset(&ird, 0, sizeof ird);
31         strcpy(ird.desired_partnum, "_HUNT_");
32         mime_parser(
33                 CM_RANGE(msg, eMessageText),
34                 *ical_locate_part,              // This callback function extracts a vcalendar item from the message.
35                 NULL,
36                 NULL,
37                 (void *) &ird,                  // Give it this place to put the vcalendar object.
38                 0
39         );
40         CM_Free(msg);
41         if (ird.cal == NULL) return;            // If there was no calendar item in this message, do nothing else.
42
43
44         // This is where we need to perform our search reduction.
45
46
47         char *ser = icalcomponent_as_ical_string_r(ird.cal);
48         if (ser) {
49                 size_t len = strlen(ser);       // Output the object, ensuring it terminates with a newline.
50                 client_write(ser, len);
51                 if ( (len>0) && (ser[len-1] != '\n') ) {
52                         client_write(HKEY("\n"));
53                 }
54                 free(ser);
55         }
56
57         icalcomponent_free(ird.cal);            // Return the memory we got from the callback.
58 }
59
60
61 // Go through a calendar room and output calendar objects after applying caller specified filters.
62 // It is intended as a data source for WebCit (both the UI and CalDAV)
63 void calendar_query(void) {
64         void *filter_rules;     // Don't know yet what form this will take
65
66         // Only allow this operation if we're in a room containing a calendar or tasks view
67         if (    (CC->room.QRdefaultview != VIEW_CALENDAR)
68                 && (CC->room.QRdefaultview != VIEW_TASKS)
69         ) {
70                 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
71                 return;         // This room does not contain a calendar.
72         }
73
74         cprintf("%d Filtered calendar listing:\n", LISTING_FOLLOWS);
75
76         // Now go through the room encapsulating all calendar items.
77         CtdlForEachMessage(MSGS_ALL, 0, NULL,
78                 NULL,
79                 NULL,
80                 calendar_query_backend,
81                 (void *) filter_rules
82         );
83
84         cprintf("000\n");
85 }