* Fixed an error in the template that was causing the 'c_creataide' field to load...
[citadel.git] / citadel / journaling.c
blob6f5e69644378160c181c0901c53afb31bf9d040a
1 /*
2 * $Id$
4 * Message journaling functions.
6 */
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
16 # include <time.h>
17 #else
18 # if HAVE_SYS_TIME_H
19 # include <sys/time.h>
20 # else
21 # include <time.h>
22 # endif
23 #endif
26 #include <ctype.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <sys/stat.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "database.h"
36 #include "msgbase.h"
37 #include "support.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "file_ops.h"
43 #include "config.h"
44 #include "control.h"
45 #include "genstamp.h"
46 #include "internet_addressing.h"
47 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
48 #include "journaling.h"
50 #include "ctdl_module.h"
51 #include "threads.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);
67 return;
70 jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
71 if (jptr == NULL) {
72 free(saved_rfc822_version);
73 return;
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);
86 jptr->next = jnlq;
87 jnlq = jptr;
88 end_critical_section(S_JOURNAL_QUEUE);
93 * Convert a local user name to an internet email address for the journal
97 * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
98 * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
99 * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
100 * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
101 * delimiter? Or would it be better to return a linked list of some kind?
103 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
104 struct ctdluser us;
105 struct vCard *v;
107 strcpy(inetemail, "");
108 if (getuser(&us, localuser) != 0) {
109 return;
112 v = vcard_get_user(&us);
113 if (v == NULL) {
114 return;
117 extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
118 vcard_free(v);
123 * Called by JournalRunQueue() to send an individual message.
125 void JournalRunQueueMsg(struct jnlq *jmsg) {
127 struct CtdlMessage *journal_msg = NULL;
128 struct recptypes *journal_recps = NULL;
129 char *message_text = NULL;
130 char mime_boundary[256];
131 char recipient[256];
132 char inetemail[256];
133 static int seq = 0;
134 int i;
136 journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
137 if (journal_recps != NULL) {
139 if ( (journal_recps->num_local > 0)
140 || (journal_recps->num_internet > 0)
141 || (journal_recps->num_ignet > 0)
142 || (journal_recps->num_room > 0)
146 * Construct journal message.
147 * Note that we are transferring ownership of some of the memory here.
149 journal_msg = malloc(sizeof(struct CtdlMessage));
150 memset(journal_msg, 0, sizeof(struct CtdlMessage));
151 journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
152 journal_msg->cm_anon_type = MES_NORMAL;
153 journal_msg->cm_format_type = FMT_RFC822;
154 journal_msg->cm_fields['J'] = strdup("is journal");
155 journal_msg->cm_fields['A'] = jmsg->from;
156 journal_msg->cm_fields['N'] = jmsg->node;
157 journal_msg->cm_fields['F'] = jmsg->rfca;
158 journal_msg->cm_fields['U'] = jmsg->subj;
160 sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
161 message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
164 * Here is where we begin to compose the journalized message.
165 * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
166 * requested by a paying customer, and yes, it is intentionally
167 * spelled wrong. Do NOT remove or change it.
169 sprintf(message_text,
170 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
171 "Content-Identifer: ExJournalReport\r\n"
172 "MIME-Version: 1.0\r\n"
173 "\n"
174 "--%s\r\n"
175 "Content-type: text/plain\r\n"
176 "\r\n"
177 "Sender: %s "
179 mime_boundary,
180 mime_boundary,
181 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
184 if (journal_msg->cm_fields['F']) {
185 sprintf(&message_text[strlen(message_text)], "<%s>",
186 journal_msg->cm_fields['F']);
188 else if (journal_msg->cm_fields['N']) {
189 sprintf(&message_text[strlen(message_text)], "@ %s",
190 journal_msg->cm_fields['N']);
193 sprintf(&message_text[strlen(message_text)],
194 "\r\n"
195 "Message-ID: <%s>\r\n"
196 "Recipients:\r\n"
198 jmsg->msgn
201 if (jmsg->recps.num_local > 0) {
202 for (i=0; i<jmsg->recps.num_local; ++i) {
203 extract_token(recipient, jmsg->recps.recp_local,
204 i, '|', sizeof recipient);
205 local_to_inetemail(inetemail, recipient, sizeof inetemail);
206 sprintf(&message_text[strlen(message_text)],
207 " %s <%s>\r\n", recipient, inetemail);
211 if (jmsg->recps.num_ignet > 0) {
212 for (i=0; i<jmsg->recps.num_ignet; ++i) {
213 extract_token(recipient, jmsg->recps.recp_ignet,
214 i, '|', sizeof recipient);
215 sprintf(&message_text[strlen(message_text)],
216 " %s\r\n", recipient);
220 if (jmsg->recps.num_internet > 0) {
221 for (i=0; i<jmsg->recps.num_internet; ++i) {
222 extract_token(recipient, jmsg->recps.recp_internet,
223 i, '|', sizeof recipient);
224 sprintf(&message_text[strlen(message_text)],
225 " %s\r\n", recipient);
229 sprintf(&message_text[strlen(message_text)],
230 "\r\n"
231 "--%s\r\n"
232 "Content-type: message/rfc822\r\n"
233 "\r\n"
234 "%s"
235 "--%s--\r\n"
237 mime_boundary,
238 jmsg->rfc822,
239 mime_boundary
242 journal_msg->cm_fields['M'] = message_text;
243 free(jmsg->rfc822);
244 free(jmsg->msgn);
246 /* Submit journal message */
247 CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
248 CtdlFreeMessage(journal_msg);
251 free_recipients(journal_recps);
254 /* We are responsible for freeing this memory. */
255 free(jmsg);
260 * Run the queue.
262 void JournalRunQueue(void) {
263 struct jnlq *jptr = NULL;
265 while (jnlq != NULL) {
266 begin_critical_section(S_JOURNAL_QUEUE);
267 if (jnlq != NULL) {
268 jptr = jnlq;
269 jnlq = jnlq->next;
271 end_critical_section(S_JOURNAL_QUEUE);
272 JournalRunQueueMsg(jptr);