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