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 struct jnlq *jnlq = NULL; /* journal queue */
54 * Hand off a copy of a message to be journalized.
56 void JournalBackgroundSubmit(struct CtdlMessage *msg,
57 char *saved_rfc822_version,
58 struct recptypes *recps) {
60 struct jnlq *jptr = NULL;
62 /* Avoid double journaling! */
63 if (msg->cm_fields['J'] != NULL) {
64 free(saved_rfc822_version);
68 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
70 free(saved_rfc822_version);
73 memset(jptr, 0, sizeof(struct jnlq));
74 if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
75 if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
76 if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
77 if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
78 if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
79 if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
80 jptr->rfc822 = saved_rfc822_version;
82 /* Add to the queue */
83 begin_critical_section(S_JOURNAL_QUEUE);
86 end_critical_section(S_JOURNAL_QUEUE);
91 * Convert a local user name to an internet email address for the journal
95 * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
96 * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
97 * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
98 * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
99 * delimiter? Or would it be better to return a linked list of some kind?
101 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
105 strcpy(inetemail, "");
106 if (getuser(&us, localuser) != 0) {
110 v = vcard_get_user(&us);
115 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
121 * Called by JournalRunQueue() to send an individual message.
123 void JournalRunQueueMsg(struct jnlq *jmsg) {
125 struct CtdlMessage *journal_msg = NULL;
126 struct recptypes *journal_recps = NULL;
127 char *message_text = NULL;
128 char mime_boundary[256];
134 journal_recps = validate_recipients(config.c_journal_dest);
135 if (journal_recps != NULL) {
137 if ( (journal_recps->num_local > 0)
138 || (journal_recps->num_internet > 0)
139 || (journal_recps->num_ignet > 0)
140 || (journal_recps->num_room > 0)
144 * Construct journal message.
145 * Note that we are transferring ownership of some of the memory here.
147 journal_msg = malloc(sizeof(struct CtdlMessage));
148 memset(journal_msg, 0, sizeof(struct CtdlMessage));
149 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
150 journal_msg->cm_anon_type = MES_NORMAL;
151 journal_msg->cm_format_type = FMT_RFC822;
152 journal_msg->cm_fields['J'] = strdup("is journal");
153 journal_msg->cm_fields['A'] = jmsg->from;
154 journal_msg->cm_fields['N'] = jmsg->node;
155 journal_msg->cm_fields['F'] = jmsg->rfca;
156 journal_msg->cm_fields['U'] = jmsg->subj;
158 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
159 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
162 * Here is where we begin to compose the journalized message.
163 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
164 * requested by a paying customer, and yes, it is intentionally
165 * spelled wrong. Do NOT remove or change it.
167 sprintf(message_text,
168 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
169 "Content-Identifer: ExJournalReport\r\n"
170 "MIME-Version: 1.0\r\n"
173 "Content-type: text/plain\r\n"
179 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
182 if (journal_msg->cm_fields['F']) {
183 sprintf(&message_text[strlen(message_text)], "<%s>",
184 journal_msg->cm_fields['F']);
186 else if (journal_msg->cm_fields['N']) {
187 sprintf(&message_text[strlen(message_text)], "@ %s",
188 journal_msg->cm_fields['N']);
191 sprintf(&message_text[strlen(message_text)],
193 "Message-ID: <%s>\r\n"
199 if (jmsg->recps.num_local > 0) {
200 for (i=0; i<jmsg->recps.num_local; ++i) {
201 extract_token(recipient, jmsg->recps.recp_local,
202 i, '|', sizeof recipient);
203 local_to_inetemail(inetemail, recipient, sizeof inetemail);
204 sprintf(&message_text[strlen(message_text)],
205 " %s <%s>\r\n", recipient, inetemail);
209 if (jmsg->recps.num_ignet > 0) {
210 for (i=0; i<jmsg->recps.num_ignet; ++i) {
211 extract_token(recipient, jmsg->recps.recp_ignet,
212 i, '|', sizeof recipient);
213 sprintf(&message_text[strlen(message_text)],
214 " %s\r\n", recipient);
218 if (jmsg->recps.num_internet > 0) {
219 for (i=0; i<jmsg->recps.num_internet; ++i) {
220 extract_token(recipient, jmsg->recps.recp_internet,
221 i, '|', sizeof recipient);
222 sprintf(&message_text[strlen(message_text)],
223 " %s\r\n", recipient);
227 sprintf(&message_text[strlen(message_text)],
230 "Content-type: message/rfc822\r\n"
240 journal_msg->cm_fields['M'] = message_text;
244 /* Submit journal message */
245 CtdlSubmitMsg(journal_msg, journal_recps, "");
246 CtdlFreeMessage(journal_msg);
249 free_recipients(journal_recps);
252 /* We are responsible for freeing this memory. */
260 void JournalRunQueue(void) {
261 struct jnlq *jptr = NULL;
263 while (jnlq != NULL) {
264 begin_critical_section(S_JOURNAL_QUEUE);
269 end_critical_section(S_JOURNAL_QUEUE);
270 JournalRunQueueMsg(jptr);