* move policy.c into modules/expire/expire_policy.c, since it just controls this.
[citadel.git] / citadel / modules / notes / serv_notes.c
1 /*
2  * $Id$
3  *
4  * Handles functions related to yellow sticky notes.
5  *
6  * Copyright (c) 2007-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <sys/wait.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "user_ops.h"
54 #include "database.h"
55 #include "msgbase.h"
56
57 #include "ctdl_module.h"
58
59
60 /*
61  * Callback function for serv_notes_beforesave() hunts for a vNote in the MIME structure
62  */
63 void notes_extract_vnote(char *name, char *filename, char *partnum, char *disp,
64                    void *content, char *cbtype, char *cbcharset, size_t length,
65                    char *encoding, char *cbid, void *cbuserdata)
66 {
67         struct vnote **v = (struct vnote **) cbuserdata;
68
69         if (!strcasecmp(cbtype, "text/vnote")) {
70
71                 CtdlLogPrintf(CTDL_DEBUG, "Part %s contains a vNote!  Loading...\n", partnum);
72                 if (*v != NULL) {
73                         vnote_free(*v);
74                 }
75                 *v = vnote_new_from_str(content);
76         }
77 }
78
79
80 /*
81  * Before-save hook searches for two different types of notes (legacy Kolab/Aethera notes
82  * and modern vNote format notes) and does its best to learn the subject (summary)
83  * and EUID (uid) of the note for Citadel's own nefarious purposes.
84  */
85 int serv_notes_beforesave(struct CtdlMessage *msg)
86 {
87         char *p;
88         int a, i;
89         char uuid[512];
90         struct vnote *v = NULL;
91
92         /* First determine if this room has the "notes" view set */
93
94         if (CC->room.QRdefaultview != VIEW_NOTES) {
95                 return(0);                      /* not notes; do nothing */
96         }
97
98         /* It must be an RFC822 message! */
99         if (msg->cm_format_type != 4) {
100                 return(0);      /* You tried to save a non-RFC822 message! */
101         }
102         
103         /*
104          * If we are in a "notes" view room, and the client has sent an RFC822
105          * message containing an X-KOrg-Note-Id: field (Aethera does this, as
106          * do some Kolab clients) then set both the Subject and the Exclusive ID
107          * of the message to that.  It's going to be a UUID so we want to replace
108          * any existing message containing that UUID.
109          */
110         strcpy(uuid, "");
111         p = msg->cm_fields['M'];
112         a = strlen(p);
113         while (--a > 0) {
114                 if (!strncasecmp(p, "X-KOrg-Note-Id: ", 16)) {  /* Found it */
115                         safestrncpy(uuid, p + 16, sizeof(uuid));
116                         for (i = 0; uuid[i]; ++i) {
117                                 if ( (uuid[i] == '\r') || (uuid[i] == '\n') ) {
118                                         uuid[i] = 0;
119                                         break;
120                                 }
121                         }
122
123                         CtdlLogPrintf(9, "UUID of note is: %s\n", uuid);
124                         if (!IsEmptyStr(uuid)) {
125
126                                 if (msg->cm_fields['E'] != NULL) {
127                                         free(msg->cm_fields['E']);
128                                 }
129                                 msg->cm_fields['E'] = strdup(uuid);
130
131                                 if (msg->cm_fields['U'] != NULL) {
132                                         free(msg->cm_fields['U']);
133                                 }
134                                 msg->cm_fields['U'] = strdup(uuid);
135                         }
136                 }
137                 p++;
138         }
139
140         /* Modern clients are using vNote format.  Check for one... */
141
142         mime_parser(msg->cm_fields['M'],
143                 NULL,
144                 *notes_extract_vnote,
145                 NULL, NULL,
146                 &v,             /* user data ptr - put the vnote here */
147                 0
148         );
149
150         if (v == NULL) return(0);       /* no vNotes were found in this message */
151
152         /* Set the message EUID to the vNote UID */
153
154         if (v->uid) if (!IsEmptyStr(v->uid)) {
155                 CtdlLogPrintf(9, "UID of vNote is: %s\n", v->uid);
156                 if (msg->cm_fields['E'] != NULL) {
157                         free(msg->cm_fields['E']);
158                 }
159                 msg->cm_fields['E'] = strdup(v->uid);
160         }
161
162         /* Set the message Subject to the vNote Summary */
163
164         if (v->summary) if (!IsEmptyStr(v->summary)) {
165                 if (msg->cm_fields['U'] != NULL) {
166                         free(msg->cm_fields['U']);
167                 }
168                 msg->cm_fields['U'] = strdup(v->summary);
169                 if (strlen(msg->cm_fields['U']) > 72) {
170                         strcpy(&msg->cm_fields['U'][68], "...");
171                 }
172         }
173
174         vnote_free(v);
175         
176         return(0);
177 }
178
179
180 CTDL_MODULE_INIT(notes)
181 {
182         if (!threading)
183         {
184                 CtdlRegisterMessageHook(serv_notes_beforesave, EVT_BEFORESAVE);
185         }
186         
187         /* return our Subversion id for the Log */
188         return "$Id$";
189 }