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