* Renamed the "Extended message ID" field to "Exclusive message ID"
[citadel.git] / citadel / serv_notes.c
1 /*
2  * $Id$
3  *
4  * Handles functions related to yellow sticky notes.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "sysdep_decls.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "serv_extensions.h"
39 #include "room_ops.h"
40 #include "user_ops.h"
41 #include "policy.h"
42 #include "database.h"
43 #include "msgbase.h"
44 #include "tools.h"
45
46
47 /*
48  * If we are in a "notes" view room, and the client has sent an RFC822
49  * message containing an X-KOrg-Note-Id: field (Aethera does this, as
50  * do some Kolab clients) then set both the Subject and the Exclusive ID
51  * of the message to that.  It's going to be a UUID so we want to replace
52  * any existing message containing that UUID.
53  */
54 int serv_notes_beforesave(struct CtdlMessage *msg)
55 {
56         char *p;
57         int a, i;
58         char uuid[SIZ];
59
60         /* First determine if this room has the "notes" view set */
61
62         if (CC->curr_view != VIEW_NOTES) {
63                 return(0);                      /* not notes; do nothing */
64         }
65
66         /* It must be an RFC822 message! */
67         if (msg->cm_format_type != 4) {
68                 return(0);      /* You tried to save a non-RFC822 message! */
69         }
70         
71         /* Find the X-KOrg-Note-Id: header */
72         strcpy(uuid, "");
73         p = msg->cm_fields['M'];
74         a = strlen(p);
75         while (--a > 0) {
76                 if (!strncasecmp(p, "X-KOrg-Note-Id: ", 16)) {  /* Found it */
77                         safestrncpy(uuid, p + 16, sizeof(uuid));
78                         for (i = 0; i<strlen(uuid); ++i) {
79                                 if ( (uuid[i] == '\r') || (uuid[i] == '\n') ) {
80                                         uuid[i] = 0;
81                                 }
82                         }
83
84                         lprintf(9, "UUID of note is: %s\n", uuid);
85                         if (strlen(uuid) > 0) {
86
87                                 if (msg->cm_fields['E'] != NULL) {
88                                         free(msg->cm_fields['E']);
89                                 }
90                                 msg->cm_fields['E'] = strdup(uuid);
91
92                                 if (msg->cm_fields['U'] != NULL) {
93                                         free(msg->cm_fields['U']);
94                                 }
95                                 msg->cm_fields['U'] = strdup(uuid);
96                         }
97                 }
98                 p++;
99         }
100         
101         return(0);
102 }
103
104
105 char *serv_notes_init(void)
106 {
107         CtdlRegisterMessageHook(serv_notes_beforesave, EVT_BEFORESAVE);
108         return "$Id$";
109 }