2 * Message journaling functions.
4 * Copyright (c) 1987-2015 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>
18 #include "ctdl_module.h"
20 #include "citserver.h"
23 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
24 #include "internet_addressing.h"
25 #include "journaling.h"
27 struct jnlq *jnlq = NULL; /* journal queue */
30 * Hand off a copy of a message to be journalized.
32 void JournalBackgroundSubmit(struct CtdlMessage *msg,
33 StrBuf *saved_rfc822_version,
36 struct jnlq *jptr = NULL;
38 /* Avoid double journaling! */
39 if (!CM_IsEmpty(msg, eJournal)) {
40 FreeStrBuf(&saved_rfc822_version);
44 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
46 FreeStrBuf(&saved_rfc822_version);
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);
58 /* Add to the queue */
59 begin_critical_section(S_JOURNAL_QUEUE);
62 end_critical_section(S_JOURNAL_QUEUE);
67 * Convert a local user name to an internet email address for the journal
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?
77 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
81 strcpy(inetemail, "");
82 if (CtdlGetUser(&us, localuser) != 0) {
86 v = vcard_get_user(&us);
91 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
97 * Called by JournalRunQueue() to send an individual message.
99 void JournalRunQueueMsg(struct jnlq *jmsg) {
101 struct CtdlMessage *journal_msg = NULL;
102 recptypes *journal_recps = NULL;
103 StrBuf *message_text = NULL;
104 char mime_boundary[256];
114 journal_recps = validate_recipients(CtdlGetConfigStr("c_journal_dest"), NULL, 0);
115 if (journal_recps != NULL) {
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)
124 * Construct journal message.
125 * Note that we are transferring ownership of some of the memory here.
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));
138 mblen = snprintf(mime_boundary, sizeof(mime_boundary),
139 "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
140 rfc822len = strlen(jmsg->rfc822);
142 message_text = NewStrBufPlain(NULL, rfc822len + sizeof(recptypes) + 1024);
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.
150 StrBufAppendBufPlain(
152 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
154 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
156 StrBufAppendBufPlain(
159 "Content-Identifer: ExJournalReport\r\n"
160 "MIME-Version: 1.0\r\n"
164 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
166 StrBufAppendBufPlain(
169 "Content-type: text/plain\r\n"
173 if (CM_IsEmpty(journal_msg, eAuthor))
174 StrBufAppendBufPlain(
176 journal_msg->cm_fields[eAuthor], -1, 0);
178 StrBufAppendBufPlain(
182 if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
183 StrBufAppendPrintf(message_text, " <%s>",
184 journal_msg->cm_fields[erFc822Addr]);
186 else if (!CM_IsEmpty(journal_msg, eNodeName)) {
187 StrBufAppendPrintf(message_text, " @ %s",
188 journal_msg->cm_fields[eNodeName]);
191 StrBufAppendBufPlain(
195 StrBufAppendBufPlain(
198 "Message-ID: <"), 0);
200 StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
201 StrBufAppendBufPlain(
204 "Recipients:\r\n"), 0);
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);
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);
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);
234 StrBufAppendBufPlain(
239 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
241 StrBufAppendBufPlain(
244 "Content-type: message/rfc822\r\n"
247 StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
249 StrBufAppendBufPlain(
253 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
255 StrBufAppendBufPlain(
259 CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
265 /* Submit journal message */
266 CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
267 CM_Free(journal_msg);
270 free_recipients(journal_recps);
273 /* We are responsible for freeing this memory. */
281 void JournalRunQueue(void) {
282 struct jnlq *jptr = NULL;
284 while (jnlq != NULL) {
285 begin_critical_section(S_JOURNAL_QUEUE);
290 end_critical_section(S_JOURNAL_QUEUE);
291 JournalRunQueueMsg(jptr);