2 * Message journaling functions.
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
16 # include <sys/time.h>
29 #include <libcitadel.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
42 #include "internet_addressing.h"
43 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
44 #include "journaling.h"
46 #include "ctdl_module.h"
49 struct jnlq *jnlq = NULL; /* journal queue */
52 * Hand off a copy of a message to be journalized.
54 void JournalBackgroundSubmit(struct CtdlMessage *msg,
55 StrBuf *saved_rfc822_version,
56 struct recptypes *recps) {
58 struct jnlq *jptr = NULL;
60 /* Avoid double journaling! */
61 if (!CM_IsEmpty(msg, eJournal)) {
62 FreeStrBuf(&saved_rfc822_version);
66 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
68 FreeStrBuf(&saved_rfc822_version);
71 memset(jptr, 0, sizeof(struct jnlq));
72 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
73 if (!CM_IsEmpty(msg, eAuthor)) jptr->from = strdup(msg->cm_fields[eAuthor]);
74 if (!CM_IsEmpty(msg, eNodeName)) jptr->node = strdup(msg->cm_fields[eNodeName]);
75 if (!CM_IsEmpty(msg, erFc822Addr)) jptr->rfca = strdup(msg->cm_fields[erFc822Addr]);
76 if (!CM_IsEmpty(msg, eMsgSubject)) jptr->subj = strdup(msg->cm_fields[eMsgSubject]);
77 if (!CM_IsEmpty(msg, emessageId)) jptr->msgn = strdup(msg->cm_fields[emessageId]);
78 jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
80 /* Add to the queue */
81 begin_critical_section(S_JOURNAL_QUEUE);
84 end_critical_section(S_JOURNAL_QUEUE);
89 * Convert a local user name to an internet email address for the journal
93 * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
94 * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
95 * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
96 * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
97 * delimiter? Or would it be better to return a linked list of some kind?
99 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
103 strcpy(inetemail, "");
104 if (CtdlGetUser(&us, localuser) != 0) {
108 v = vcard_get_user(&us);
113 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
119 * Called by JournalRunQueue() to send an individual message.
121 void JournalRunQueueMsg(struct jnlq *jmsg) {
123 struct CtdlMessage *journal_msg = NULL;
124 struct recptypes *journal_recps = NULL;
125 StrBuf *message_text = NULL;
126 char mime_boundary[256];
136 journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
137 if (journal_recps != NULL) {
139 if ( (journal_recps->num_local > 0)
140 || (journal_recps->num_internet > 0)
141 || (journal_recps->num_ignet > 0)
142 || (journal_recps->num_room > 0)
146 * Construct journal message.
147 * Note that we are transferring ownership of some of the memory here.
149 journal_msg = malloc(sizeof(struct CtdlMessage));
150 memset(journal_msg, 0, sizeof(struct CtdlMessage));
151 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
152 journal_msg->cm_anon_type = MES_NORMAL;
153 journal_msg->cm_format_type = FMT_RFC822;
154 CM_SetField(journal_msg, eJournal, HKEY("is journal"));
155 CM_SetField(journal_msg, eAuthor, jmsg->from, strlen(jmsg->from));
156 CM_SetField(journal_msg, eNodeName, jmsg->node, strlen(jmsg->node));
157 CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, strlen(jmsg->rfca));
158 CM_SetField(journal_msg, eMsgSubject, jmsg->subj, strlen(jmsg->subj));
160 mblen = snprintf(mime_boundary, sizeof(mime_boundary),
161 "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
162 rfc822len = strlen(jmsg->rfc822);
164 message_text = NewStrBufPlain(NULL, rfc822len + sizeof(struct recptypes) + 1024);
167 * Here is where we begin to compose the journalized message.
168 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
169 * requested by a paying customer, and yes, it is intentionally
170 * spelled wrong. Do NOT remove or change it.
172 StrBufAppendBufPlain(
174 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
176 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
178 StrBufAppendBufPlain(
181 "Content-Identifer: ExJournalReport\r\n"
182 "MIME-Version: 1.0\r\n"
186 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
188 StrBufAppendBufPlain(
191 "Content-type: text/plain\r\n"
195 if (CM_IsEmpty(journal_msg, eAuthor))
196 StrBufAppendBufPlain(
198 journal_msg->cm_fields[eAuthor], -1, 0);
200 StrBufAppendBufPlain(
204 if (!CM_IsEmpty(journal_msg, erFc822Addr)) {
205 StrBufAppendPrintf(message_text, " <%s>",
206 journal_msg->cm_fields[erFc822Addr]);
208 else if (!CM_IsEmpty(journal_msg, eNodeName)) {
209 StrBufAppendPrintf(message_text, " @ %s",
210 journal_msg->cm_fields[eNodeName]);
213 StrBufAppendBufPlain(
217 StrBufAppendBufPlain(
220 "Message-ID: <"), 0);
222 StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
223 StrBufAppendBufPlain(
226 "Recipients:\r\n"), 0);
228 if (jmsg->recps.num_local > 0) {
229 for (i=0; i<jmsg->recps.num_local; ++i) {
230 extract_token(recipient, jmsg->recps.recp_local,
231 i, '|', sizeof recipient);
232 local_to_inetemail(inetemail, recipient, sizeof inetemail);
233 StrBufAppendPrintf(message_text,
234 " %s <%s>\r\n", recipient, inetemail);
238 if (jmsg->recps.num_ignet > 0) {
239 for (i=0; i<jmsg->recps.num_ignet; ++i) {
240 extract_token(recipient, jmsg->recps.recp_ignet,
241 i, '|', sizeof recipient);
242 StrBufAppendPrintf(message_text,
243 " %s\r\n", recipient);
247 if (jmsg->recps.num_internet > 0) {
248 for (i=0; i<jmsg->recps.num_internet; ++i) {
249 extract_token(recipient, jmsg->recps.recp_internet,
250 i, '|', sizeof recipient);
251 StrBufAppendPrintf(message_text,
252 " %s\r\n", recipient);
256 StrBufAppendBufPlain(
261 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
263 StrBufAppendBufPlain(
266 "Content-type: message/rfc822\r\n"
269 StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
271 StrBufAppendBufPlain(
275 StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
277 StrBufAppendBufPlain(
281 CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
287 /* Submit journal message */
288 CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
289 CM_Free(journal_msg);
292 free_recipients(journal_recps);
295 /* We are responsible for freeing this memory. */
303 void JournalRunQueue(void) {
304 struct jnlq *jptr = NULL;
306 while (jnlq != NULL) {
307 begin_critical_section(S_JOURNAL_QUEUE);
312 end_critical_section(S_JOURNAL_QUEUE);
313 JournalRunQueueMsg(jptr);