2 * Message journaling functions.
4 * Copyright (c) 1987-2018 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,
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(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);
56 /* Add to the queue */
57 begin_critical_section(S_JOURNAL_QUEUE);
60 end_critical_section(S_JOURNAL_QUEUE);
65 * Convert a local user name to an internet email address for the journal
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?
75 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
79 strcpy(inetemail, "");
80 if (CtdlGetUser(&us, localuser) != 0) {
84 v = vcard_get_user(&us);
89 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
95 * Called by JournalRunQueue() to send an individual message.
97 void JournalRunQueueMsg(struct jnlq *jmsg) {
99 struct CtdlMessage *journal_msg = NULL;
100 recptypes *journal_recps = NULL;
101 StrBuf *message_text = NULL;
102 char mime_boundary[256];
112 journal_recps = validate_recipients(CtdlGetConfigStr("c_journal_dest"), NULL, 0);
113 if (journal_recps != NULL) {
115 if ( (journal_recps->num_local > 0)
116 || (journal_recps->num_internet > 0)
117 || (journal_recps->num_room > 0)
121 * Construct journal message.
122 * Note that we are transferring ownership of some of the memory here.
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"));
131 if (!IsEmptyStr(jmsg->from)) {
132 CM_SetField(journal_msg, eAuthor, jmsg->from, strlen(jmsg->from));
135 if (!IsEmptyStr(jmsg->node)) {
136 CM_SetField(journal_msg, eNodeName, jmsg->node, strlen(jmsg->node));
139 if (!IsEmptyStr(jmsg->rfca)) {
140 CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, strlen(jmsg->rfca));
143 if (!IsEmptyStr(jmsg->subj)) {
144 CM_SetField(journal_msg, eMsgSubject, jmsg->subj, strlen(jmsg->subj));
147 mblen = snprintf(mime_boundary, sizeof(mime_boundary),
148 "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
150 if (!IsEmptyStr(jmsg->rfc822)) {
151 rfc822len = strlen(jmsg->rfc822);
157 message_text = NewStrBufPlain(NULL, rfc822len + sizeof(recptypes) + 1024);
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.
165 StrBufAppendBufPlain(
167 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
169 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
171 StrBufAppendBufPlain(
174 "Content-Identifer: ExJournalReport\r\n"
175 "MIME-Version: 1.0\r\n"
179 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
181 StrBufAppendBufPlain(
184 "Content-type: text/plain\r\n"
188 if (CM_IsEmpty(journal_msg, eAuthor))
189 StrBufAppendBufPlain(
191 journal_msg->cm_fields[eAuthor], -1, 0);
193 StrBufAppendBufPlain(
197 if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
198 StrBufAppendPrintf(message_text, " <%s>",
199 journal_msg->cm_fields[erFc822Addr]);
201 else if (!CM_IsEmpty(journal_msg, eNodeName)) {
202 StrBufAppendPrintf(message_text, " @ %s", journal_msg->cm_fields[eNodeName]);
205 StrBufAppendBufPlain( message_text, HKEY(" "), 0);
208 StrBufAppendBufPlain(message_text, HKEY("\r\nMessage-ID: <"), 0);
210 StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
211 StrBufAppendBufPlain( message_text, HKEY(">\r\nRecipients:\r\n"), 0);
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);
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);
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);
236 CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
242 /* Submit journal message */
243 CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
244 CM_Free(journal_msg);
247 free_recipients(journal_recps);
250 /* We are responsible for freeing this memory. */
258 void JournalRunQueue(void) {
259 struct jnlq *jptr = NULL;
261 while (jnlq != NULL) {
262 begin_critical_section(S_JOURNAL_QUEUE);
267 end_critical_section(S_JOURNAL_QUEUE);
268 JournalRunQueueMsg(jptr);