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"
51 #include "serv_vcard.h"
52 #include "journaling.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
96 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
100 strcpy(inetemail, "");
101 if (getuser(&us, localuser) != 0) {
105 v = vcard_get_user(&us);
110 extract_primary_inet_email(inetemail, inetemail_len, v);
116 * Called by JournalRunQueue() to send an individual message.
118 void JournalRunQueueMsg(struct jnlq *jmsg) {
120 struct CtdlMessage *journal_msg = NULL;
121 struct recptypes *journal_recps = NULL;
122 char *message_text = NULL;
123 char mime_boundary[256];
129 journal_recps = validate_recipients(config.c_journal_dest);
130 if (journal_recps != NULL) {
132 if ( (journal_recps->num_local > 0)
133 || (journal_recps->num_internet > 0)
134 || (journal_recps->num_ignet > 0)
135 || (journal_recps->num_room > 0)
139 * Construct journal message.
140 * Note that we are transferring ownership of some of the memory here.
142 journal_msg = malloc(sizeof(struct CtdlMessage));
143 memset(journal_msg, 0, sizeof(struct CtdlMessage));
144 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
145 journal_msg->cm_anon_type = MES_NORMAL;
146 journal_msg->cm_format_type = FMT_RFC822;
147 journal_msg->cm_fields['J'] = strdup("is journal");
148 journal_msg->cm_fields['A'] = jmsg->from;
149 journal_msg->cm_fields['N'] = jmsg->node;
150 journal_msg->cm_fields['F'] = jmsg->rfca;
151 journal_msg->cm_fields['U'] = jmsg->subj;
153 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
154 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
157 * Here is where we begin to compose the journalized message.
158 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
159 * requested by a paying customer, and yes, it is intentionally
160 * spelled wrong. Do NOT remove or change it.
162 sprintf(message_text,
163 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
164 "Content-Identifer: ExJournalReport\r\n"
165 "MIME-Version: 1.0\r\n"
168 "Content-type: text/plain\r\n"
174 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
177 if (journal_msg->cm_fields['F']) {
178 sprintf(&message_text[strlen(message_text)], "<%s>",
179 journal_msg->cm_fields['F']);
181 else if (journal_msg->cm_fields['N']) {
182 sprintf(&message_text[strlen(message_text)], "@ %s",
183 journal_msg->cm_fields['N']);
186 sprintf(&message_text[strlen(message_text)],
188 "Message-ID: <%s>\r\n"
194 if (jmsg->recps.num_local > 0) {
195 for (i=0; i<jmsg->recps.num_local; ++i) {
196 extract_token(recipient, jmsg->recps.recp_local,
197 i, '|', sizeof recipient);
198 local_to_inetemail(inetemail, recipient, sizeof inetemail);
199 sprintf(&message_text[strlen(message_text)],
200 " %s <%s>\r\n", recipient, inetemail);
204 if (jmsg->recps.num_ignet > 0) {
205 for (i=0; i<jmsg->recps.num_ignet; ++i) {
206 extract_token(recipient, jmsg->recps.recp_ignet,
207 i, '|', sizeof recipient);
208 sprintf(&message_text[strlen(message_text)],
209 " %s\r\n", recipient);
213 if (jmsg->recps.num_internet > 0) {
214 for (i=0; i<jmsg->recps.num_internet; ++i) {
215 extract_token(recipient, jmsg->recps.recp_internet,
216 i, '|', sizeof recipient);
217 sprintf(&message_text[strlen(message_text)],
218 " %s\r\n", recipient);
222 sprintf(&message_text[strlen(message_text)],
225 "Content-type: message/rfc822\r\n"
235 journal_msg->cm_fields['M'] = message_text;
239 /* Submit journal message */
240 CtdlSubmitMsg(journal_msg, journal_recps, "");
241 CtdlFreeMessage(journal_msg);
247 /* We are responsible for freeing this memory. */
255 void JournalRunQueue(void) {
256 struct jnlq *jptr = NULL;
258 while (jnlq != NULL) {
259 begin_critical_section(S_JOURNAL_QUEUE);
264 end_critical_section(S_JOURNAL_QUEUE);
265 JournalRunQueueMsg(jptr);