0d2e74f4182d2218a042b7a9ea9167de77abbf06
[citadel.git] / citadel / journaling.c
1 /*
2  * Message journaling functions.
3  *
4  * Copyright (c) 1987-2015 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 <stdio.h>
16 #include <libcitadel.h>
17
18 #include "ctdl_module.h"
19
20 #include "citserver.h"
21 #include "config.h"
22 #include "user_ops.h"
23 #include "serv_vcard.h"                 /* Needed for vcard_getuser and extract_inet_email_addrs */
24 #include "internet_addressing.h"
25 #include "journaling.h"
26
27 struct jnlq *jnlq = NULL;       /* journal queue */
28
29 /*
30  * Hand off a copy of a message to be journalized.
31  */
32 void JournalBackgroundSubmit(struct CtdlMessage *msg,
33                         StrBuf *saved_rfc822_version,
34                         recptypes *recps) {
35
36         struct jnlq *jptr = NULL;
37
38         /* Avoid double journaling! */
39         if (!CM_IsEmpty(msg, eJournal)) {
40                 FreeStrBuf(&saved_rfc822_version);
41                 return;
42         }
43
44         jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
45         if (jptr == NULL) {
46                 FreeStrBuf(&saved_rfc822_version);
47                 return;
48         }
49         memset(jptr, 0, sizeof(struct jnlq));
50         if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(recptypes));
51         if (!CM_IsEmpty(msg, eAuthor)) jptr->from = strdup(msg->cm_fields[eAuthor]);
52         if (!CM_IsEmpty(msg, eNodeName)) jptr->node = strdup(msg->cm_fields[eNodeName]);
53         if (!CM_IsEmpty(msg, erFc822Addr)) jptr->rfca = strdup(msg->cm_fields[erFc822Addr]);
54         if (!CM_IsEmpty(msg, eMsgSubject)) jptr->subj = strdup(msg->cm_fields[eMsgSubject]);
55         if (!CM_IsEmpty(msg, emessageId)) jptr->msgn = strdup(msg->cm_fields[emessageId]);
56         jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
57
58         /* Add to the queue */
59         begin_critical_section(S_JOURNAL_QUEUE);
60         jptr->next = jnlq;
61         jnlq = jptr;
62         end_critical_section(S_JOURNAL_QUEUE);
63 }
64
65
66 /*
67  * Convert a local user name to an internet email address for the journal
68  */
69  
70 /*
71  * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
72  * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
73  * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
74  * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
75  * delimiter? Or would it be better to return a linked list of some kind?
76  */
77 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
78         struct ctdluser us;
79         struct vCard *v;
80
81         strcpy(inetemail, "");
82         if (CtdlGetUser(&us, localuser) != 0) {
83                 return;
84         }
85
86         v = vcard_get_user(&us);
87         if (v == NULL) {
88                 return;
89         }
90
91         extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
92         vcard_free(v);
93 }
94
95
96 /*
97  * Called by JournalRunQueue() to send an individual message.
98  */
99 void JournalRunQueueMsg(struct jnlq *jmsg) {
100
101         struct CtdlMessage *journal_msg = NULL;
102         recptypes *journal_recps = NULL;
103         StrBuf *message_text = NULL;
104         char mime_boundary[256];
105         long mblen;
106         long rfc822len;
107         char recipient[256];
108         char inetemail[256];
109         static int seq = 0;
110         int i;
111
112         if (jmsg == NULL)
113                 return;
114         journal_recps = validate_recipients(CtdlGetConfigStr("c_journal_dest"), NULL, 0);
115         if (journal_recps != NULL) {
116
117                 if (  (journal_recps->num_local > 0)
118                    || (journal_recps->num_internet > 0)
119                    || (journal_recps->num_ignet > 0)
120                    || (journal_recps->num_room > 0)
121                 ) {
122
123                         /*
124                          * Construct journal message.
125                          * Note that we are transferring ownership of some of the memory here.
126                          */
127                         journal_msg = malloc(sizeof(struct CtdlMessage));
128                         memset(journal_msg, 0, sizeof(struct CtdlMessage));
129                         journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
130                         journal_msg->cm_anon_type = MES_NORMAL;
131                         journal_msg->cm_format_type = FMT_RFC822;
132                         CM_SetField(journal_msg, eJournal, HKEY("is journal"));
133
134                         if (!IsEmptyStr(jmsg->from)) {
135                                 CM_SetField(journal_msg, eAuthor, jmsg->from, strlen(jmsg->from));
136                         }
137
138                         if (!IsEmptyStr(jmsg->node)) {
139                                 CM_SetField(journal_msg, eNodeName, jmsg->node, strlen(jmsg->node));
140                         }
141
142                         if (!IsEmptyStr(jmsg->rfca)) {
143                                 CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, strlen(jmsg->rfca));
144                         }
145
146                         if (!IsEmptyStr(jmsg->subj)) {
147                                 CM_SetField(journal_msg, eMsgSubject, jmsg->subj, strlen(jmsg->subj));
148                         }
149
150                         mblen = snprintf(mime_boundary, sizeof(mime_boundary),
151                                          "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
152
153                         if (!IsEmptyStr(jmsg->rfc822)) {
154                                 rfc822len = strlen(jmsg->rfc822);
155                         }
156                         else {
157                                 rfc822len = 0;
158                         }
159
160                         message_text = NewStrBufPlain(NULL, rfc822len + sizeof(recptypes) + 1024);
161
162                         /*
163                          * Here is where we begin to compose the journalized message.
164                          * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
165                          *       requested by a paying customer, and yes, it is intentionally
166                          *       spelled wrong.  Do NOT remove or change it.
167                          */
168                         StrBufAppendBufPlain(
169                                 message_text, 
170                                 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
171
172                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
173
174                         StrBufAppendBufPlain(
175                                 message_text, 
176                                 HKEY("\"\r\n"
177                                      "Content-Identifer: ExJournalReport\r\n"
178                                      "MIME-Version: 1.0\r\n"
179                                      "\n"
180                                      "--"), 0);
181
182                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
183
184                         StrBufAppendBufPlain(
185                                 message_text, 
186                                 HKEY("\r\n"
187                                      "Content-type: text/plain\r\n"
188                                      "\r\n"
189                                      "Sender: "), 0);
190
191                         if (CM_IsEmpty(journal_msg, eAuthor))
192                                 StrBufAppendBufPlain(
193                                         message_text, 
194                                         journal_msg->cm_fields[eAuthor], -1, 0);
195                         else
196                                 StrBufAppendBufPlain(
197                                         message_text, 
198                                         HKEY("(null)"), 0);
199
200                         if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
201                                 StrBufAppendPrintf(message_text, " <%s>",
202                                                    journal_msg->cm_fields[erFc822Addr]);
203                         }
204                         else if (!CM_IsEmpty(journal_msg, eNodeName)) {
205                                 StrBufAppendPrintf(message_text, " @ %s",
206                                                    journal_msg->cm_fields[eNodeName]);
207                         }
208                         else
209                                 StrBufAppendBufPlain(
210                                         message_text, 
211                                         HKEY(" "), 0);
212
213                         StrBufAppendBufPlain(
214                                 message_text, 
215                                 HKEY("\r\n"
216                                      "Message-ID: <"), 0);
217
218                         StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
219                         StrBufAppendBufPlain(
220                                 message_text, 
221                                 HKEY(">\r\n"
222                                      "Recipients:\r\n"), 0);
223
224                         if (jmsg->recps.num_local > 0) {
225                                 for (i=0; i<jmsg->recps.num_local; ++i) {
226                                         extract_token(recipient, jmsg->recps.recp_local,
227                                                         i, '|', sizeof recipient);
228                                         local_to_inetemail(inetemail, recipient, sizeof inetemail);
229                                         StrBufAppendPrintf(message_text, 
230                                                            "    %s <%s>\r\n", recipient, inetemail);
231                                 }
232                         }
233
234                         if (jmsg->recps.num_ignet > 0) {
235                                 for (i=0; i<jmsg->recps.num_ignet; ++i) {
236                                         extract_token(recipient, jmsg->recps.recp_ignet,
237                                                         i, '|', sizeof recipient);
238                                         StrBufAppendPrintf(message_text, 
239                                                            "    %s\r\n", recipient);
240                                 }
241                         }
242
243                         if (jmsg->recps.num_internet > 0) {
244                                 for (i=0; i<jmsg->recps.num_internet; ++i) {
245                                         extract_token(recipient, jmsg->recps.recp_internet,
246                                                         i, '|', sizeof recipient);
247                                         StrBufAppendPrintf(message_text, 
248                                                 "       %s\r\n", recipient);
249                                 }
250                         }
251
252                         StrBufAppendBufPlain(
253                                 message_text, 
254                                 HKEY("\r\n"
255                                      "--"), 0);
256
257                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
258
259                         StrBufAppendBufPlain(
260                                 message_text, 
261                                 HKEY("\r\n"
262                                      "Content-type: message/rfc822\r\n"
263                                      "\r\n"), 0);
264
265                         StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
266
267                         StrBufAppendBufPlain(
268                                 message_text, 
269                                 HKEY("--"), 0);
270
271                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
272
273                         StrBufAppendBufPlain(
274                                 message_text, 
275                                 HKEY("--\r\n"), 0);
276
277                         CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
278                         free(jmsg->rfc822);
279                         free(jmsg->msgn);
280                         jmsg->rfc822 = NULL;
281                         jmsg->msgn = NULL;
282                         
283                         /* Submit journal message */
284                         CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
285                         CM_Free(journal_msg);
286                 }
287
288                 free_recipients(journal_recps);
289         }
290
291         /* We are responsible for freeing this memory. */
292         free(jmsg);
293 }
294
295
296 /*
297  * Run the queue.
298  */
299 void JournalRunQueue(void) {
300         struct jnlq *jptr = NULL;
301
302         while (jnlq != NULL) {
303                 begin_critical_section(S_JOURNAL_QUEUE);
304                 if (jnlq != NULL) {
305                         jptr = jnlq;
306                         jnlq = jnlq->next;
307                 }
308                 end_critical_section(S_JOURNAL_QUEUE);
309                 JournalRunQueueMsg(jptr);
310         }
311 }
312
313