8af9afdca80668f96c3ddf8bea509c9d485fe9da
[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                         CM_SetField(journal_msg, eAuthor, jmsg->from, strlen(jmsg->from));
134                         CM_SetField(journal_msg, eNodeName, jmsg->node, strlen(jmsg->node));
135                         CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, strlen(jmsg->rfca));
136                         CM_SetField(journal_msg, eMsgSubject, jmsg->subj, strlen(jmsg->subj));
137
138                         mblen = snprintf(mime_boundary, sizeof(mime_boundary),
139                                          "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
140                         rfc822len = strlen(jmsg->rfc822);
141                        
142                         message_text = NewStrBufPlain(NULL, rfc822len + sizeof(recptypes) + 1024);
143
144                         /*
145                          * Here is where we begin to compose the journalized message.
146                          * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
147                          *       requested by a paying customer, and yes, it is intentionally
148                          *       spelled wrong.  Do NOT remove or change it.
149                          */
150                         StrBufAppendBufPlain(
151                                 message_text, 
152                                 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
153
154                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
155
156                         StrBufAppendBufPlain(
157                                 message_text, 
158                                 HKEY("\"\r\n"
159                                      "Content-Identifer: ExJournalReport\r\n"
160                                      "MIME-Version: 1.0\r\n"
161                                      "\n"
162                                      "--"), 0);
163
164                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
165
166                         StrBufAppendBufPlain(
167                                 message_text, 
168                                 HKEY("\r\n"
169                                      "Content-type: text/plain\r\n"
170                                      "\r\n"
171                                      "Sender: "), 0);
172
173                         if (CM_IsEmpty(journal_msg, eAuthor))
174                                 StrBufAppendBufPlain(
175                                         message_text, 
176                                         journal_msg->cm_fields[eAuthor], -1, 0);
177                         else
178                                 StrBufAppendBufPlain(
179                                         message_text, 
180                                         HKEY("(null)"), 0);
181
182                         if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
183                                 StrBufAppendPrintf(message_text, " <%s>",
184                                                    journal_msg->cm_fields[erFc822Addr]);
185                         }
186                         else if (!CM_IsEmpty(journal_msg, eNodeName)) {
187                                 StrBufAppendPrintf(message_text, " @ %s",
188                                                    journal_msg->cm_fields[eNodeName]);
189                         }
190                         else
191                                 StrBufAppendBufPlain(
192                                         message_text, 
193                                         HKEY(" "), 0);
194
195                         StrBufAppendBufPlain(
196                                 message_text, 
197                                 HKEY("\r\n"
198                                      "Message-ID: <"), 0);
199
200                         StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
201                         StrBufAppendBufPlain(
202                                 message_text, 
203                                 HKEY(">\r\n"
204                                      "Recipients:\r\n"), 0);
205
206                         if (jmsg->recps.num_local > 0) {
207                                 for (i=0; i<jmsg->recps.num_local; ++i) {
208                                         extract_token(recipient, jmsg->recps.recp_local,
209                                                         i, '|', sizeof recipient);
210                                         local_to_inetemail(inetemail, recipient, sizeof inetemail);
211                                         StrBufAppendPrintf(message_text, 
212                                                            "    %s <%s>\r\n", recipient, inetemail);
213                                 }
214                         }
215
216                         if (jmsg->recps.num_ignet > 0) {
217                                 for (i=0; i<jmsg->recps.num_ignet; ++i) {
218                                         extract_token(recipient, jmsg->recps.recp_ignet,
219                                                         i, '|', sizeof recipient);
220                                         StrBufAppendPrintf(message_text, 
221                                                            "    %s\r\n", recipient);
222                                 }
223                         }
224
225                         if (jmsg->recps.num_internet > 0) {
226                                 for (i=0; i<jmsg->recps.num_internet; ++i) {
227                                         extract_token(recipient, jmsg->recps.recp_internet,
228                                                         i, '|', sizeof recipient);
229                                         StrBufAppendPrintf(message_text, 
230                                                 "       %s\r\n", recipient);
231                                 }
232                         }
233
234                         StrBufAppendBufPlain(
235                                 message_text, 
236                                 HKEY("\r\n"
237                                      "--"), 0);
238
239                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
240
241                         StrBufAppendBufPlain(
242                                 message_text, 
243                                 HKEY("\r\n"
244                                      "Content-type: message/rfc822\r\n"
245                                      "\r\n"), 0);
246
247                         StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
248
249                         StrBufAppendBufPlain(
250                                 message_text, 
251                                 HKEY("--"), 0);
252
253                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
254
255                         StrBufAppendBufPlain(
256                                 message_text, 
257                                 HKEY("--\r\n"), 0);
258
259                         CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
260                         free(jmsg->rfc822);
261                         free(jmsg->msgn);
262                         jmsg->rfc822 = NULL;
263                         jmsg->msgn = NULL;
264                         
265                         /* Submit journal message */
266                         CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
267                         CM_Free(journal_msg);
268                 }
269
270                 free_recipients(journal_recps);
271         }
272
273         /* We are responsible for freeing this memory. */
274         free(jmsg);
275 }
276
277
278 /*
279  * Run the queue.
280  */
281 void JournalRunQueue(void) {
282         struct jnlq *jptr = NULL;
283
284         while (jnlq != NULL) {
285                 begin_critical_section(S_JOURNAL_QUEUE);
286                 if (jnlq != NULL) {
287                         jptr = jnlq;
288                         jnlq = jnlq->next;
289                 }
290                 end_critical_section(S_JOURNAL_QUEUE);
291                 JournalRunQueueMsg(jptr);
292         }
293 }
294
295