4 * Message journaling functions.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
37 #include "sysdep_decls.h"
38 #include "citserver.h"
45 #include "mime_parser.h"
48 #include "internet_addressing.h"
50 #include "serv_vcard.h"
51 #include "journaling.h"
53 struct jnlq *jnlq = NULL; /* journal queue */
56 * Hand off a copy of a message to be journalized.
58 void JournalBackgroundSubmit(struct CtdlMessage *msg,
59 char *saved_rfc822_version,
60 struct recptypes *recps) {
62 struct jnlq *jptr = NULL;
64 /* Avoid double journaling! */
65 if (msg->cm_fields['J'] != NULL) {
66 free(saved_rfc822_version);
70 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
72 free(saved_rfc822_version);
75 memset(jptr, 0, sizeof(struct jnlq));
76 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
77 if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
78 if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
79 if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
80 if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
81 if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
82 jptr->rfc822 = saved_rfc822_version;
84 /* Add to the queue */
85 begin_critical_section(S_JOURNAL_QUEUE);
88 end_critical_section(S_JOURNAL_QUEUE);
93 * Convert a local user name to an internet email address for the journal
95 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
99 strcpy(inetemail, "");
100 if (getuser(&us, localuser) != 0) {
104 v = vcard_get_user(&us);
109 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
115 * Called by JournalRunQueue() to send an individual message.
117 void JournalRunQueueMsg(struct jnlq *jmsg) {
119 struct CtdlMessage *journal_msg = NULL;
120 struct recptypes *journal_recps = NULL;
121 char *message_text = NULL;
122 char mime_boundary[256];
128 journal_recps = validate_recipients(config.c_journal_dest);
129 if (journal_recps != NULL) {
131 if ( (journal_recps->num_local > 0)
132 || (journal_recps->num_internet > 0)
133 || (journal_recps->num_ignet > 0)
134 || (journal_recps->num_room > 0)
138 * Construct journal message.
139 * Note that we are transferring ownership of some of the memory here.
141 journal_msg = malloc(sizeof(struct CtdlMessage));
142 memset(journal_msg, 0, sizeof(struct CtdlMessage));
143 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
144 journal_msg->cm_anon_type = MES_NORMAL;
145 journal_msg->cm_format_type = FMT_RFC822;
146 journal_msg->cm_fields['J'] = strdup("is journal");
147 journal_msg->cm_fields['A'] = jmsg->from;
148 journal_msg->cm_fields['N'] = jmsg->node;
149 journal_msg->cm_fields['F'] = jmsg->rfca;
150 journal_msg->cm_fields['U'] = jmsg->subj;
152 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
153 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
156 * Here is where we begin to compose the journalized message.
157 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
158 * requested by a paying customer, and yes, it is intentionally
159 * spelled wrong. Do NOT remove or change it.
161 sprintf(message_text,
162 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
163 "Content-Identifer: ExJournalReport\r\n"
164 "MIME-Version: 1.0\r\n"
167 "Content-type: text/plain\r\n"
173 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
176 if (journal_msg->cm_fields['F']) {
177 sprintf(&message_text[strlen(message_text)], "<%s>",
178 journal_msg->cm_fields['F']);
180 else if (journal_msg->cm_fields['N']) {
181 sprintf(&message_text[strlen(message_text)], "@ %s",
182 journal_msg->cm_fields['N']);
185 sprintf(&message_text[strlen(message_text)],
187 "Message-ID: <%s>\r\n"
193 if (jmsg->recps.num_local > 0) {
194 for (i=0; i<jmsg->recps.num_local; ++i) {
195 extract_token(recipient, jmsg->recps.recp_local,
196 i, '|', sizeof recipient);
197 local_to_inetemail(inetemail, recipient, sizeof inetemail);
198 sprintf(&message_text[strlen(message_text)],
199 " %s <%s>\r\n", recipient, inetemail);
203 if (jmsg->recps.num_ignet > 0) {
204 for (i=0; i<jmsg->recps.num_ignet; ++i) {
205 extract_token(recipient, jmsg->recps.recp_ignet,
206 i, '|', sizeof recipient);
207 sprintf(&message_text[strlen(message_text)],
208 " %s\r\n", recipient);
212 if (jmsg->recps.num_internet > 0) {
213 for (i=0; i<jmsg->recps.num_internet; ++i) {
214 extract_token(recipient, jmsg->recps.recp_internet,
215 i, '|', sizeof recipient);
216 sprintf(&message_text[strlen(message_text)],
217 " %s\r\n", recipient);
221 sprintf(&message_text[strlen(message_text)],
224 "Content-type: message/rfc822\r\n"
234 journal_msg->cm_fields['M'] = message_text;
238 /* Submit journal message */
239 CtdlSubmitMsg(journal_msg, journal_recps, "");
240 CtdlFreeMessage(journal_msg);
243 free_recipients(journal_recps);
246 /* We are responsible for freeing this memory. */
254 void JournalRunQueue(void) {
255 struct jnlq *jptr = NULL;
257 while (jnlq != NULL) {
258 begin_critical_section(S_JOURNAL_QUEUE);
263 end_critical_section(S_JOURNAL_QUEUE);
264 JournalRunQueueMsg(jptr);