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