4 * Message journaling functions.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
32 #include <libcitadel.h>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
47 #include "internet_addressing.h"
48 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
49 #include "journaling.h"
51 #include "ctdl_module.h"
54 struct jnlq *jnlq = NULL; /* journal queue */
57 * Hand off a copy of a message to be journalized.
59 void JournalBackgroundSubmit(struct CtdlMessage *msg,
60 char *saved_rfc822_version,
61 struct recptypes *recps) {
63 struct jnlq *jptr = NULL;
65 /* Avoid double journaling! */
66 if (msg->cm_fields['J'] != NULL) {
67 free(saved_rfc822_version);
71 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
73 free(saved_rfc822_version);
76 memset(jptr, 0, sizeof(struct jnlq));
77 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
78 if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
79 if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
80 if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
81 if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
82 if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
83 jptr->rfc822 = saved_rfc822_version;
85 /* Add to the queue */
86 begin_critical_section(S_JOURNAL_QUEUE);
89 end_critical_section(S_JOURNAL_QUEUE);
94 * Convert a local user name to an internet email address for the journal
98 * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
99 * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
100 * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
101 * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
102 * delimiter? Or would it be better to return a linked list of some kind?
104 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
108 strcpy(inetemail, "");
109 if (getuser(&us, localuser) != 0) {
113 v = vcard_get_user(&us);
118 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
124 * Called by JournalRunQueue() to send an individual message.
126 void JournalRunQueueMsg(struct jnlq *jmsg) {
128 struct CtdlMessage *journal_msg = NULL;
129 struct recptypes *journal_recps = NULL;
130 char *message_text = NULL;
131 char mime_boundary[256];
137 journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
138 if (journal_recps != NULL) {
140 if ( (journal_recps->num_local > 0)
141 || (journal_recps->num_internet > 0)
142 || (journal_recps->num_ignet > 0)
143 || (journal_recps->num_room > 0)
147 * Construct journal message.
148 * Note that we are transferring ownership of some of the memory here.
150 journal_msg = malloc(sizeof(struct CtdlMessage));
151 memset(journal_msg, 0, sizeof(struct CtdlMessage));
152 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
153 journal_msg->cm_anon_type = MES_NORMAL;
154 journal_msg->cm_format_type = FMT_RFC822;
155 journal_msg->cm_fields['J'] = strdup("is journal");
156 journal_msg->cm_fields['A'] = jmsg->from;
157 journal_msg->cm_fields['N'] = jmsg->node;
158 journal_msg->cm_fields['F'] = jmsg->rfca;
159 journal_msg->cm_fields['U'] = jmsg->subj;
161 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
162 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
165 * Here is where we begin to compose the journalized message.
166 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
167 * requested by a paying customer, and yes, it is intentionally
168 * spelled wrong. Do NOT remove or change it.
170 sprintf(message_text,
171 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
172 "Content-Identifer: ExJournalReport\r\n"
173 "MIME-Version: 1.0\r\n"
176 "Content-type: text/plain\r\n"
182 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
185 if (journal_msg->cm_fields['F']) {
186 sprintf(&message_text[strlen(message_text)], "<%s>",
187 journal_msg->cm_fields['F']);
189 else if (journal_msg->cm_fields['N']) {
190 sprintf(&message_text[strlen(message_text)], "@ %s",
191 journal_msg->cm_fields['N']);
194 sprintf(&message_text[strlen(message_text)],
196 "Message-ID: <%s>\r\n"
202 if (jmsg->recps.num_local > 0) {
203 for (i=0; i<jmsg->recps.num_local; ++i) {
204 extract_token(recipient, jmsg->recps.recp_local,
205 i, '|', sizeof recipient);
206 local_to_inetemail(inetemail, recipient, sizeof inetemail);
207 sprintf(&message_text[strlen(message_text)],
208 " %s <%s>\r\n", recipient, inetemail);
212 if (jmsg->recps.num_ignet > 0) {
213 for (i=0; i<jmsg->recps.num_ignet; ++i) {
214 extract_token(recipient, jmsg->recps.recp_ignet,
215 i, '|', sizeof recipient);
216 sprintf(&message_text[strlen(message_text)],
217 " %s\r\n", recipient);
221 if (jmsg->recps.num_internet > 0) {
222 for (i=0; i<jmsg->recps.num_internet; ++i) {
223 extract_token(recipient, jmsg->recps.recp_internet,
224 i, '|', sizeof recipient);
225 sprintf(&message_text[strlen(message_text)],
226 " %s\r\n", recipient);
230 sprintf(&message_text[strlen(message_text)],
233 "Content-type: message/rfc822\r\n"
243 journal_msg->cm_fields['M'] = message_text;
247 /* Submit journal message */
248 CtdlSubmitMsg(journal_msg, journal_recps, "");
249 CtdlFreeMessage(journal_msg);
252 free_recipients(journal_recps);
255 /* We are responsible for freeing this memory. */
263 void JournalRunQueue(void) {
264 struct jnlq *jptr = NULL;
266 while (jnlq != NULL) {
267 begin_critical_section(S_JOURNAL_QUEUE);
272 end_critical_section(S_JOURNAL_QUEUE);
273 JournalRunQueueMsg(jptr);