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"
43 #include "internet_addressing.h"
44 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
45 #include "journaling.h"
47 #include "ctdl_module.h"
50 struct jnlq *jnlq = NULL; /* journal queue */
53 * Hand off a copy of a message to be journalized.
55 void JournalBackgroundSubmit(struct CtdlMessage *msg,
56 StrBuf *saved_rfc822_version,
57 struct recptypes *recps) {
59 struct jnlq *jptr = NULL;
61 /* Avoid double journaling! */
62 if (msg->cm_fields['J'] != NULL) {
63 FreeStrBuf(&saved_rfc822_version);
67 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
69 FreeStrBuf(&saved_rfc822_version);
72 memset(jptr, 0, sizeof(struct jnlq));
73 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
74 if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
75 if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
76 if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
77 if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
78 if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
79 jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
81 /* Add to the queue */
82 begin_critical_section(S_JOURNAL_QUEUE);
85 end_critical_section(S_JOURNAL_QUEUE);
90 * Convert a local user name to an internet email address for the journal
94 * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
95 * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
96 * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
97 * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
98 * delimiter? Or would it be better to return a linked list of some kind?
100 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
104 strcpy(inetemail, "");
105 if (CtdlGetUser(&us, localuser) != 0) {
109 v = vcard_get_user(&us);
114 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
120 * Called by JournalRunQueue() to send an individual message.
122 void JournalRunQueueMsg(struct jnlq *jmsg) {
124 struct CtdlMessage *journal_msg = NULL;
125 struct recptypes *journal_recps = NULL;
126 char *message_text = NULL;
127 char mime_boundary[256];
133 journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
134 if (journal_recps != NULL) {
136 if ( (journal_recps->num_local > 0)
137 || (journal_recps->num_internet > 0)
138 || (journal_recps->num_ignet > 0)
139 || (journal_recps->num_room > 0)
143 * Construct journal message.
144 * Note that we are transferring ownership of some of the memory here.
146 journal_msg = malloc(sizeof(struct CtdlMessage));
147 memset(journal_msg, 0, sizeof(struct CtdlMessage));
148 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
149 journal_msg->cm_anon_type = MES_NORMAL;
150 journal_msg->cm_format_type = FMT_RFC822;
151 journal_msg->cm_fields['J'] = strdup("is journal");
152 journal_msg->cm_fields['A'] = jmsg->from;
153 journal_msg->cm_fields['N'] = jmsg->node;
154 journal_msg->cm_fields['F'] = jmsg->rfca;
155 journal_msg->cm_fields['U'] = jmsg->subj;
157 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
158 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
161 * Here is where we begin to compose the journalized message.
162 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
163 * requested by a paying customer, and yes, it is intentionally
164 * spelled wrong. Do NOT remove or change it.
166 sprintf(message_text,
167 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
168 "Content-Identifer: ExJournalReport\r\n"
169 "MIME-Version: 1.0\r\n"
172 "Content-type: text/plain\r\n"
178 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
181 if (journal_msg->cm_fields['F']) {
182 sprintf(&message_text[strlen(message_text)], "<%s>",
183 journal_msg->cm_fields['F']);
185 else if (journal_msg->cm_fields['N']) {
186 sprintf(&message_text[strlen(message_text)], "@ %s",
187 journal_msg->cm_fields['N']);
190 sprintf(&message_text[strlen(message_text)],
192 "Message-ID: <%s>\r\n"
198 if (jmsg->recps.num_local > 0) {
199 for (i=0; i<jmsg->recps.num_local; ++i) {
200 extract_token(recipient, jmsg->recps.recp_local,
201 i, '|', sizeof recipient);
202 local_to_inetemail(inetemail, recipient, sizeof inetemail);
203 sprintf(&message_text[strlen(message_text)],
204 " %s <%s>\r\n", recipient, inetemail);
208 if (jmsg->recps.num_ignet > 0) {
209 for (i=0; i<jmsg->recps.num_ignet; ++i) {
210 extract_token(recipient, jmsg->recps.recp_ignet,
211 i, '|', sizeof recipient);
212 sprintf(&message_text[strlen(message_text)],
213 " %s\r\n", recipient);
217 if (jmsg->recps.num_internet > 0) {
218 for (i=0; i<jmsg->recps.num_internet; ++i) {
219 extract_token(recipient, jmsg->recps.recp_internet,
220 i, '|', sizeof recipient);
221 sprintf(&message_text[strlen(message_text)],
222 " %s\r\n", recipient);
226 sprintf(&message_text[strlen(message_text)],
229 "Content-type: message/rfc822\r\n"
239 journal_msg->cm_fields['M'] = message_text;
243 /* Submit journal message */
244 CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
245 CtdlFreeMessage(journal_msg);
248 free_recipients(journal_recps);
251 /* We are responsible for freeing this memory. */
259 void JournalRunQueue(void) {
260 struct jnlq *jptr = NULL;
262 while (jnlq != NULL) {
263 begin_critical_section(S_JOURNAL_QUEUE);
268 end_critical_section(S_JOURNAL_QUEUE);
269 JournalRunQueueMsg(jptr);