8d3f888224e7c763e88a66ffd4feea799ea953b4
[citadel.git] / citadel / modules / notes / 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 <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "room_ops.h"
39 #include "user_ops.h"
40 #include "policy.h"
41 #include "database.h"
42 #include "msgbase.h"
43
44 #include "ctdl_module.h"
45
46
47 /*
48  * Callback function for serv_notes_beforesave() hunts for a vNote in the MIME structure
49  */
50 void notes_extract_vnote(char *name, char *filename, char *partnum, char *disp,
51                    void *content, char *cbtype, char *cbcharset, size_t length,
52                    char *encoding, char *cbid, void *cbuserdata)
53 {
54         struct vnote **v = (struct vnote **) cbuserdata;
55
56         if (!strcasecmp(cbtype, "text/vnote")) {
57
58                 CtdlLogPrintf(CTDL_DEBUG, "Part %s contains a vNote!  Loading...\n", partnum);
59                 if (*v != NULL) {
60                         vnote_free(*v);
61                 }
62                 *v = vnote_new_from_str(content);
63         }
64 }
65
66
67 /*
68  * Before-save hook searches for two different types of notes (legacy Kolab/Aethera notes
69  * and modern vNote format notes) and does its best to learn the subject (summary)
70  * and EUID (uid) of the note for Citadel's own nefarious purposes.
71  */
72 int serv_notes_beforesave(struct CtdlMessage *msg)
73 {
74         char *p;
75         int a, i;
76         char uuid[512];
77         struct vnote *v = NULL;
78
79         /* First determine if this room has the "notes" view set */
80
81         if (CC->room.QRdefaultview != VIEW_NOTES) {
82                 return(0);                      /* not notes; do nothing */
83         }
84
85         /* It must be an RFC822 message! */
86         if (msg->cm_format_type != 4) {
87                 return(0);      /* You tried to save a non-RFC822 message! */
88         }
89         
90         /*
91          * If we are in a "notes" view room, and the client has sent an RFC822
92          * message containing an X-KOrg-Note-Id: field (Aethera does this, as
93          * do some Kolab clients) then set both the Subject and the Exclusive ID
94          * of the message to that.  It's going to be a UUID so we want to replace
95          * any existing message containing that UUID.
96          */
97         strcpy(uuid, "");
98         p = msg->cm_fields['M'];
99         a = strlen(p);
100         while (--a > 0) {
101                 if (!strncasecmp(p, "X-KOrg-Note-Id: ", 16)) {  /* Found it */
102                         safestrncpy(uuid, p + 16, sizeof(uuid));
103                         for (i = 0; uuid[i]; ++i) {
104                                 if ( (uuid[i] == '\r') || (uuid[i] == '\n') ) {
105                                         uuid[i] = 0;
106                                         break;
107                                 }
108                         }
109
110                         CtdlLogPrintf(9, "UUID of note is: %s\n", uuid);
111                         if (!IsEmptyStr(uuid)) {
112
113                                 if (msg->cm_fields['E'] != NULL) {
114                                         free(msg->cm_fields['E']);
115                                 }
116                                 msg->cm_fields['E'] = strdup(uuid);
117
118                                 if (msg->cm_fields['U'] != NULL) {
119                                         free(msg->cm_fields['U']);
120                                 }
121                                 msg->cm_fields['U'] = strdup(uuid);
122                         }
123                 }
124                 p++;
125         }
126
127         /* Modern clients are using vNote format.  Check for one... */
128
129         mime_parser(msg->cm_fields['M'],
130                 NULL,
131                 *notes_extract_vnote,
132                 NULL, NULL,
133                 &v,             /* user data ptr - put the vnote here */
134                 0
135         );
136
137         if (v == NULL) return(0);       /* no vNotes were found in this message */
138
139         /* Set the message EUID to the vNote UID */
140
141         if (v->uid) if (!IsEmptyStr(v->uid)) {
142                 CtdlLogPrintf(9, "UID of vNote is: %s\n", v->uid);
143                 if (msg->cm_fields['E'] != NULL) {
144                         free(msg->cm_fields['E']);
145                 }
146                 msg->cm_fields['E'] = strdup(v->uid);
147         }
148
149         /* Set the message Subject to the vNote Summary */
150
151         if (v->summary) if (!IsEmptyStr(v->summary)) {
152                 if (msg->cm_fields['U'] != NULL) {
153                         free(msg->cm_fields['U']);
154                 }
155                 msg->cm_fields['U'] = strdup(v->summary);
156                 if (strlen(msg->cm_fields['U']) > 72) {
157                         strcpy(&msg->cm_fields['U'][68], "...");
158                 }
159         }
160
161         vnote_free(v);
162         
163         return(0);
164 }
165
166
167 CTDL_MODULE_INIT(notes)
168 {
169         if (!threading)
170         {
171                 CtdlRegisterMessageHook(serv_notes_beforesave, EVT_BEFORESAVE);
172         }
173         
174         /* return our Subversion id for the Log */
175         return "$Id$";
176 }