2 * Message journaling functions.
6 #include <libcitadel.h>
8 #include "ctdl_module.h"
10 #include "citserver.h"
12 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
13 #include "internet_addressing.h"
14 #include "journaling.h"
16 struct jnlq *jnlq = NULL; /* journal queue */
19 * Hand off a copy of a message to be journalized.
21 void JournalBackgroundSubmit(struct CtdlMessage *msg,
22 StrBuf *saved_rfc822_version,
25 struct jnlq *jptr = NULL;
27 /* Avoid double journaling! */
28 if (!CM_IsEmpty(msg, eJournal)) {
29 FreeStrBuf(&saved_rfc822_version);
33 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
35 FreeStrBuf(&saved_rfc822_version);
38 memset(jptr, 0, sizeof(struct jnlq));
39 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(recptypes));
40 if (!CM_IsEmpty(msg, eAuthor)) jptr->from = strdup(msg->cm_fields[eAuthor]);
41 if (!CM_IsEmpty(msg, eNodeName)) jptr->node = strdup(msg->cm_fields[eNodeName]);
42 if (!CM_IsEmpty(msg, erFc822Addr)) jptr->rfca = strdup(msg->cm_fields[erFc822Addr]);
43 if (!CM_IsEmpty(msg, eMsgSubject)) jptr->subj = strdup(msg->cm_fields[eMsgSubject]);
44 if (!CM_IsEmpty(msg, emessageId)) jptr->msgn = strdup(msg->cm_fields[emessageId]);
45 jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
47 /* Add to the queue */
48 begin_critical_section(S_JOURNAL_QUEUE);
51 end_critical_section(S_JOURNAL_QUEUE);
56 * Convert a local user name to an internet email address for the journal
60 * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
61 * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
62 * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
63 * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
64 * delimiter? Or would it be better to return a linked list of some kind?
66 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
70 strcpy(inetemail, "");
71 if (CtdlGetUser(&us, localuser) != 0) {
75 v = vcard_get_user(&us);
80 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
86 * Called by JournalRunQueue() to send an individual message.
88 void JournalRunQueueMsg(struct jnlq *jmsg) {
90 struct CtdlMessage *journal_msg = NULL;
91 recptypes *journal_recps = NULL;
92 StrBuf *message_text = NULL;
93 char mime_boundary[256];
103 journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
104 if (journal_recps != NULL) {
106 if ( (journal_recps->num_local > 0)
107 || (journal_recps->num_internet > 0)
108 || (journal_recps->num_ignet > 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"));
122 CM_SetField(journal_msg, eAuthor, jmsg->from, strlen(jmsg->from));
123 CM_SetField(journal_msg, eNodeName, jmsg->node, strlen(jmsg->node));
124 CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, strlen(jmsg->rfca));
125 CM_SetField(journal_msg, eMsgSubject, jmsg->subj, strlen(jmsg->subj));
127 mblen = snprintf(mime_boundary, sizeof(mime_boundary),
128 "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
129 rfc822len = strlen(jmsg->rfc822);
131 message_text = NewStrBufPlain(NULL, rfc822len + sizeof(recptypes) + 1024);
134 * Here is where we begin to compose the journalized message.
135 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
136 * requested by a paying customer, and yes, it is intentionally
137 * spelled wrong. Do NOT remove or change it.
139 StrBufAppendBufPlain(
141 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
143 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
145 StrBufAppendBufPlain(
148 "Content-Identifer: ExJournalReport\r\n"
149 "MIME-Version: 1.0\r\n"
153 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
155 StrBufAppendBufPlain(
158 "Content-type: text/plain\r\n"
162 if (CM_IsEmpty(journal_msg, eAuthor))
163 StrBufAppendBufPlain(
165 journal_msg->cm_fields[eAuthor], -1, 0);
167 StrBufAppendBufPlain(
171 if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
172 StrBufAppendPrintf(message_text, " <%s>",
173 journal_msg->cm_fields[erFc822Addr]);
175 else if (!CM_IsEmpty(journal_msg, eNodeName)) {
176 StrBufAppendPrintf(message_text, " @ %s",
177 journal_msg->cm_fields[eNodeName]);
180 StrBufAppendBufPlain(
184 StrBufAppendBufPlain(
187 "Message-ID: <"), 0);
189 StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
190 StrBufAppendBufPlain(
193 "Recipients:\r\n"), 0);
195 if (jmsg->recps.num_local > 0) {
196 for (i=0; i<jmsg->recps.num_local; ++i) {
197 extract_token(recipient, jmsg->recps.recp_local,
198 i, '|', sizeof recipient);
199 local_to_inetemail(inetemail, recipient, sizeof inetemail);
200 StrBufAppendPrintf(message_text,
201 " %s <%s>\r\n", recipient, inetemail);
205 if (jmsg->recps.num_ignet > 0) {
206 for (i=0; i<jmsg->recps.num_ignet; ++i) {
207 extract_token(recipient, jmsg->recps.recp_ignet,
208 i, '|', sizeof recipient);
209 StrBufAppendPrintf(message_text,
210 " %s\r\n", recipient);
214 if (jmsg->recps.num_internet > 0) {
215 for (i=0; i<jmsg->recps.num_internet; ++i) {
216 extract_token(recipient, jmsg->recps.recp_internet,
217 i, '|', sizeof recipient);
218 StrBufAppendPrintf(message_text,
219 " %s\r\n", recipient);
223 StrBufAppendBufPlain(
228 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
230 StrBufAppendBufPlain(
233 "Content-type: message/rfc822\r\n"
236 StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
238 StrBufAppendBufPlain(
242 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
244 StrBufAppendBufPlain(
248 CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
254 /* Submit journal message */
255 CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
256 CM_Free(journal_msg);
259 free_recipients(journal_recps);
262 /* We are responsible for freeing this memory. */
270 void JournalRunQueue(void) {
271 struct jnlq *jptr = NULL;
273 while (jnlq != NULL) {
274 begin_critical_section(S_JOURNAL_QUEUE);
279 end_critical_section(S_JOURNAL_QUEUE);
280 JournalRunQueueMsg(jptr);