2 * Message journaling functions.
4 * Copyright (c) 1987-2020 by the citadel.org team
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.
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.
16 #include <libcitadel.h>
17 #include "ctdl_module.h"
18 #include "citserver.h"
21 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
22 #include "internet_addressing.h"
23 #include "journaling.h"
25 struct jnlq *jnlq = NULL; /* journal queue */
28 * Hand off a copy of a message to be journalized.
30 void JournalBackgroundSubmit(struct CtdlMessage *msg,
31 StrBuf *saved_rfc822_version,
32 struct recptypes *recps) {
34 struct jnlq *jptr = NULL;
36 /* Avoid double journaling! */
37 if (!CM_IsEmpty(msg, eJournal)) {
38 FreeStrBuf(&saved_rfc822_version);
42 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
44 FreeStrBuf(&saved_rfc822_version);
47 memset(jptr, 0, sizeof(struct jnlq));
48 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
49 if (!CM_IsEmpty(msg, eAuthor)) jptr->from = strdup(msg->cm_fields[eAuthor]);
50 if (!CM_IsEmpty(msg, erFc822Addr)) jptr->rfca = strdup(msg->cm_fields[erFc822Addr]);
51 if (!CM_IsEmpty(msg, eMsgSubject)) jptr->subj = strdup(msg->cm_fields[eMsgSubject]);
52 if (!CM_IsEmpty(msg, emessageId)) jptr->msgn = strdup(msg->cm_fields[emessageId]);
53 jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
55 /* Add to the queue */
56 begin_critical_section(S_JOURNAL_QUEUE);
59 end_critical_section(S_JOURNAL_QUEUE);
64 * Convert a local user name to an internet email address for the journal
65 * FIXME - grab the user's Internet email address from the user record, not from vCard !!!!
67 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
71 strcpy(inetemail, "");
72 if (CtdlGetUser(&us, localuser) != 0) {
76 v = vcard_get_user(&us);
81 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
87 * Called by JournalRunQueue() to send an individual message.
89 void JournalRunQueueMsg(struct jnlq *jmsg) {
91 struct CtdlMessage *journal_msg = NULL;
92 struct recptypes *journal_recps = NULL;
93 StrBuf *message_text = NULL;
94 char mime_boundary[256];
104 journal_recps = validate_recipients(CtdlGetConfigStr("c_journal_dest"), NULL, 0);
105 if (journal_recps != NULL) {
107 if ( (journal_recps->num_local > 0)
108 || (journal_recps->num_internet > 0)
109 || (journal_recps->num_room > 0)
113 * Construct journal message.
114 * Note that we are transferring ownership of some of the memory here.
116 journal_msg = malloc(sizeof(struct CtdlMessage));
117 memset(journal_msg, 0, sizeof(struct CtdlMessage));
118 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
119 journal_msg->cm_anon_type = MES_NORMAL;
120 journal_msg->cm_format_type = FMT_RFC822;
121 CM_SetField(journal_msg, eJournal, HKEY("is journal"));
123 if (!IsEmptyStr(jmsg->from)) {
124 CM_SetField(journal_msg, eAuthor, jmsg->from, -1);
127 if (!IsEmptyStr(jmsg->rfca)) {
128 CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, -1);
131 if (!IsEmptyStr(jmsg->subj)) {
132 CM_SetField(journal_msg, eMsgSubject, jmsg->subj, -1);
135 mblen = snprintf(mime_boundary, sizeof(mime_boundary),
136 "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
138 if (!IsEmptyStr(jmsg->rfc822)) {
139 rfc822len = strlen(jmsg->rfc822);
145 message_text = NewStrBufPlain(NULL, rfc822len + sizeof(struct recptypes) + 1024);
148 * Here is where we begin to compose the journalized message.
149 * (The "ExJournalReport" header is consumed by some email retention services which assume the journaling agent is Exchange.)
151 StrBufAppendBufPlain(
153 HKEY("Content-type: multipart/mixed; boundary=\""),
157 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
159 StrBufAppendBufPlain(
162 "Content-Identifier: ExJournalReport\r\n"
163 "MIME-Version: 1.0\r\n"
169 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
171 StrBufAppendBufPlain(
174 "Content-type: text/plain\r\n"
178 if (CM_IsEmpty(journal_msg, eAuthor))
179 StrBufAppendBufPlain(
181 journal_msg->cm_fields[eAuthor], -1, 0);
183 StrBufAppendBufPlain(
187 if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
188 StrBufAppendPrintf(message_text, " <%s>",
189 journal_msg->cm_fields[erFc822Addr]);
192 StrBufAppendBufPlain(message_text, HKEY("\r\nMessage-ID: <"), 0);
193 StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
194 StrBufAppendBufPlain(message_text, HKEY(">\r\nRecipients:\r\n"), 0);
196 if (jmsg->recps.num_local > 0) {
197 for (i=0; i<jmsg->recps.num_local; ++i) {
198 extract_token(recipient, jmsg->recps.recp_local, i, '|', sizeof recipient);
199 local_to_inetemail(inetemail, recipient, sizeof inetemail);
200 StrBufAppendPrintf(message_text, " %s <%s>\r\n", recipient, inetemail);
204 if (jmsg->recps.num_internet > 0) {
205 for (i=0; i<jmsg->recps.num_internet; ++i) {
206 extract_token(recipient, jmsg->recps.recp_internet, i, '|', sizeof recipient);
207 StrBufAppendPrintf(message_text, " %s\r\n", recipient);
211 StrBufAppendBufPlain(message_text, HKEY("\r\n" "--"), 0);
212 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
213 StrBufAppendBufPlain(message_text, HKEY("\r\nContent-type: message/rfc822\r\n\r\n"), 0);
214 StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
215 StrBufAppendBufPlain(message_text, HKEY("--"), 0);
216 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
217 StrBufAppendBufPlain(message_text, HKEY("--\r\n"), 0);
219 CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
225 /* Submit journal message */
226 CtdlSubmitMsg(journal_msg, journal_recps, "");
227 CM_Free(journal_msg);
230 free_recipients(journal_recps);
233 /* We are responsible for freeing this memory. */
241 void JournalRunQueue(void) {
242 struct jnlq *jptr = NULL;
244 while (jnlq != NULL) {
245 begin_critical_section(S_JOURNAL_QUEUE);
250 end_critical_section(S_JOURNAL_QUEUE);
251 JournalRunQueueMsg(jptr);