4 * This module implements iCalendar object processing and the Calendar>
5 * room on a Citadel/UX server. It handles iCalendar objects using the
6 * iTIP protocol. See RFCs 2445 and 2446.
12 #include <sys/types.h>
19 #include "serv_calendar.h"
22 #include "citserver.h"
23 #include "sysdep_decls.h"
26 #include "dynloader.h"
31 #include "mime_parser.h"
39 struct ical_respond_data {
40 char desired_partnum[SIZ];
49 * Write our config to disk
51 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
56 strcpy(temp, tmpnam(NULL));
57 ser = icalcomponent_as_ical_string(cal);
58 if (ser == NULL) return;
60 /* Make a temp file out of it */
61 fp = fopen(temp, "w");
62 if (fp == NULL) return;
63 fwrite(ser, strlen(ser), 1, fp);
66 /* This handy API function does all the work for us.
67 * NOTE: normally we would want to set that last argument to 1, to
68 * force the system to delete the user's old vCard. But it doesn't
69 * have to, because the vcard_upload_beforesave() hook above
70 * is going to notice what we're trying to do, and delete the old vCard.
72 CtdlWriteObject(USERCALENDARROOM, /* which room */
73 "text/calendar", /* MIME type */
77 0, /* don't delete others of this type */
85 * Add a calendar object to the user's calendar
87 void ical_add(icalcomponent *cal, int recursion_level) {
91 * The VEVENT subcomponent is the one we're interested in saving.
93 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
95 ical_write_to_cal(&CC->usersupp, cal);
99 /* If the component has subcomponents, recurse through them. */
100 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
102 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
103 /* Recursively process subcomponent */
104 ical_add(c, recursion_level+1);
112 * Callback function for mime parser that hunts for calendar content types
113 * and turns them into calendar objects
115 void ical_process_request(char *name, char *filename, char *partnum, char *disp,
116 void *content, char *cbtype, size_t length, char *encoding,
119 struct ical_respond_data *ird;
121 ird = (struct ical_respond_data *) cbuserdata;
122 if (strcasecmp(partnum, ird->desired_partnum)) return;
123 ird->cal = icalcomponent_new_from_string(content);
128 * Respond to a meeting request.
130 void ical_respond(long msgnum, char *partnum, char *action) {
131 struct CtdlMessage *msg;
132 struct ical_respond_data ird;
135 (strcasecmp(action, "accept"))
136 && (strcasecmp(action, "decline"))
138 cprintf("%d Action must be 'accept' or 'decline'\n",
139 ERROR + ILLEGAL_VALUE
144 msg = CtdlFetchMessage(msgnum);
146 cprintf("%d Message %ld not found.\n",
153 memset(&ird, 0, sizeof ird);
154 strcpy(ird.desired_partnum, partnum);
155 mime_parser(msg->cm_fields['M'],
157 *ical_process_request, /* callback function */
159 (void *) &ird, /* user data */
163 CtdlFreeMessage(msg);
165 if (ird.cal != NULL) {
166 /* FIXME do something with it */
168 /* Save this in the user's calendar if necessary */
169 if (!strcasecmp(action, "accept")) {
170 ical_add(ird.cal, 0);
173 /* Send a reply if necessary */
174 /* FIXME ... do this */
176 /* Delete the message from the inbox */
177 /* FIXME ... do this */
179 icalcomponent_free(ird.cal);
180 cprintf("%d ok\n", CIT_OK);
184 cprintf("%d No calendar object found\n", ERROR);
188 /* should never get here */
195 * All Citadel calendar commands from the client come through here.
197 void cmd_ical(char *argbuf)
204 if (CtdlAccessCheck(ac_logged_in)) return;
206 extract(subcmd, argbuf, 0);
208 if (!strcmp(subcmd, "test")) {
209 cprintf("%d This server supports calendaring\n", CIT_OK);
213 if (!strcmp(subcmd, "respond")) {
214 msgnum = extract_long(argbuf, 1);
215 extract(partnum, argbuf, 2);
216 extract(action, argbuf, 3);
217 ical_respond(msgnum, partnum, action);
221 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
225 /* should never get here */
228 #endif /* HAVE_ICAL_H */
232 * We don't know if the calendar room exists so we just create it at login
234 void ical_create_room(void)
239 /* Create the calendar room if it doesn't already exist */
240 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
242 /* Set expiration policy to manual; otherwise objects will be lost! */
243 if (lgetroom(&qr, USERCALENDARROOM)) {
244 lprintf(3, "Couldn't get the user calendar room!\n");
247 qr.QRep.expire_mode = EXPIRE_MANUAL;
250 /* Set the view to a calendar view */
251 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
252 vbuf.v_view = 3; /* 3 = calendar */
253 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
255 /* Create the tasks list room if it doesn't already exist */
256 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
258 /* Set expiration policy to manual; otherwise objects will be lost! */
259 if (lgetroom(&qr, USERTASKSROOM)) {
260 lprintf(3, "Couldn't get the user calendar room!\n");
263 qr.QRep.expire_mode = EXPIRE_MANUAL;
266 /* Set the view to a task list view */
267 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
268 vbuf.v_view = 4; /* 4 = tasks */
269 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
275 /* See if we need to prevent the object from being saved */
276 int ical_obj_beforesave(struct CtdlMessage *msg)
278 char roomname[ROOMNAMELEN];
283 * Only messages with content-type text/calendar or text/x-calendar
284 * may be saved to Calendar>. If the message is bound for
285 * Calendar> but doesn't have this content-type, throw an error
286 * so that the message may not be posted.
289 /* First determine if this is our room */
290 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
291 if (strncmp(roomname, msg->cm_fields['O'], ROOMNAMELEN))
292 return 0; /* It's not us... */
294 /* Then determine content-type of the message */
296 /* It must be an RFC822 message! */
297 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
298 if (msg->cm_format_type != 4)
299 return 1; /* You tried to save a non-RFC822 message! */
301 /* Find the Content-Type: header */
302 p = msg->cm_fields['M'];
305 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
306 if (!strncasecmp(p + 14, "text/x-calendar", 15) ||
307 !strncasecmp(p + 14, "text/calendar", 13))
315 /* Oops! No Content-Type in this message! How'd that happen? */
316 lprintf(7, "RFC822 message with no Content-Type header!\n");
322 /* Register this module with the Citadel server. */
323 char *Dynamic_Module_Init(void)
325 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
327 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
328 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");