4 * Message journaling functions.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
34 #include "serv_extensions.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
46 #include "mime_parser.h"
49 #include "internet_addressing.h"
50 #include "journaling.h"
52 struct jnlq *jnlq = NULL; /* journal queue */
55 * Hand off a copy of a message to be journalized.
57 void JournalBackgroundSubmit(struct CtdlMessage *msg,
58 char *saved_rfc822_version,
59 struct recptypes *recps) {
61 struct jnlq *jptr = NULL;
63 /* Avoid double journaling! */
64 if (msg->cm_fields['J'] != NULL) {
65 free(saved_rfc822_version);
69 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
71 free(saved_rfc822_version);
74 memset(jptr, 0, sizeof(struct jnlq));
75 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
76 if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
77 if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
78 if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
79 if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
80 if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
81 jptr->rfc822 = saved_rfc822_version;
83 /* Add to the queue */
84 begin_critical_section(S_JOURNAL_QUEUE);
87 end_critical_section(S_JOURNAL_QUEUE);
92 * Called by JournalRunQueue() to send an individual message.
94 void JournalRunQueueMsg(struct jnlq *jmsg) {
96 struct CtdlMessage *journal_msg = NULL;
97 struct recptypes *journal_recps = NULL;
98 char *message_text = NULL;
99 char mime_boundary[256];
104 journal_recps = validate_recipients(config.c_journal_dest);
105 if (journal_recps != NULL) {
107 if ( (journal_recps->num_local > 0)
108 || (journal_recps->num_internet > 0)
109 || (journal_recps->num_ignet > 0)
110 || (journal_recps->num_room > 0)
114 * Construct journal message.
115 * Note that we are transferring ownership of some of the memory here.
117 journal_msg = malloc(sizeof(struct CtdlMessage));
118 memset(journal_msg, 0, sizeof(struct CtdlMessage));
119 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
120 journal_msg->cm_anon_type = MES_NORMAL;
121 journal_msg->cm_format_type = FMT_RFC822;
122 journal_msg->cm_fields['J'] = strdup("is journal");
123 journal_msg->cm_fields['A'] = jmsg->from;
124 journal_msg->cm_fields['N'] = jmsg->node;
125 journal_msg->cm_fields['F'] = jmsg->rfca;
126 journal_msg->cm_fields['U'] = jmsg->subj;
128 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
129 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
132 * Here is where we begin to compose the journalized message.
133 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
134 * requested by a paying customer, and yes, it is intentionally
135 * spelled wrong. Do NOT remove or change it.
137 sprintf(message_text,
138 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
139 "Content-Identifer: ExJournalReport\r\n"
140 "MIME-Version: 1.0\r\n"
143 "Content-type: text/plain\r\n"
149 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
152 if (journal_msg->cm_fields['F']) {
153 sprintf(&message_text[strlen(message_text)], "<%s>",
154 journal_msg->cm_fields['F']);
156 else if (journal_msg->cm_fields['N']) {
157 sprintf(&message_text[strlen(message_text)], "@ %s",
158 journal_msg->cm_fields['N']);
161 sprintf(&message_text[strlen(message_text)],
163 "Message-ID: <%s>\r\n"
169 if (jmsg->recps.num_local > 0) {
170 for (i=0; i<jmsg->recps.num_local; ++i) {
171 extract_token(recipient, jmsg->recps.recp_local,
172 i, '|', sizeof recipient);
173 sprintf(&message_text[strlen(message_text)],
174 " %s\r\n", recipient);
178 if (jmsg->recps.num_ignet > 0) {
179 for (i=0; i<jmsg->recps.num_ignet; ++i) {
180 extract_token(recipient, jmsg->recps.recp_ignet,
181 i, '|', sizeof recipient);
182 sprintf(&message_text[strlen(message_text)],
183 " %s\r\n", recipient);
187 if (jmsg->recps.num_internet > 0) {
188 for (i=0; i<jmsg->recps.num_internet; ++i) {
189 extract_token(recipient, jmsg->recps.recp_internet,
190 i, '|', sizeof recipient);
191 sprintf(&message_text[strlen(message_text)],
192 " %s\r\n", recipient);
196 sprintf(&message_text[strlen(message_text)],
199 "Content-type: message/rfc822\r\n"
209 journal_msg->cm_fields['M'] = message_text;
213 /* Submit journal message */
214 CtdlSubmitMsg(journal_msg, journal_recps, "");
215 CtdlFreeMessage(journal_msg);
221 /* We are responsible for freeing this memory. */
229 void JournalRunQueue(void) {
230 struct jnlq *jptr = NULL;
232 while (jnlq != NULL) {
233 begin_critical_section(S_JOURNAL_QUEUE);
238 end_critical_section(S_JOURNAL_QUEUE);
239 JournalRunQueueMsg(jptr);