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