ec158b43c80999e120a1d02669002900aa6d3acb
[citadel.git] / citadel / journaling.c
1 /*
2  * $Id:  $
3  *
4  * Message journaling functions.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13
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
24
25
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 "citadel.h"
33 #include "server.h"
34 #include "serv_extensions.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 "tools.h"
46 #include "mime_parser.h"
47 #include "html.h"
48 #include "genstamp.h"
49 #include "internet_addressing.h"
50 #include "vcard.h"
51 #include "serv_vcard.h"
52 #include "journaling.h"
53
54 struct jnlq *jnlq = NULL;       /* journal queue */
55
56 /*
57  * Hand off a copy of a message to be journalized.
58  */
59 void JournalBackgroundSubmit(struct CtdlMessage *msg,
60                         char *saved_rfc822_version,
61                         struct recptypes *recps) {
62
63         struct jnlq *jptr = NULL;
64
65         /* Avoid double journaling! */
66         if (msg->cm_fields['J'] != NULL) {
67                 free(saved_rfc822_version);
68                 return;
69         }
70
71         jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
72         if (jptr == NULL) {
73                 free(saved_rfc822_version);
74                 return;
75         }
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;
84
85         /* Add to the queue */
86         begin_critical_section(S_JOURNAL_QUEUE);
87         jptr->next = jnlq;
88         jnlq = jptr;
89         end_critical_section(S_JOURNAL_QUEUE);
90 }
91
92
93 /*
94  * Convert a local user name to an internet email address for the journal
95  */
96 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
97         struct ctdluser us;
98         struct vCard *v;
99
100         strcpy(inetemail, "");
101         if (getuser(&us, localuser) != 0) {
102                 return;
103         }
104
105         v = vcard_get_user(&us);
106         if (v == NULL) {
107                 return;
108         }
109
110         extract_primary_inet_email(inetemail, inetemail_len, v);
111         vcard_free(v);
112 }
113
114
115 /*
116  * Called by JournalRunQueue() to send an individual message.
117  */
118 void JournalRunQueueMsg(struct jnlq *jmsg) {
119
120         struct CtdlMessage *journal_msg = NULL;
121         struct recptypes *journal_recps = NULL;
122         char *message_text = NULL;
123         char mime_boundary[256];
124         char recipient[256];
125         char inetemail[256];
126         static int seq = 0;
127         int i;
128
129         journal_recps = validate_recipients(config.c_journal_dest);
130         if (journal_recps != NULL) {
131
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)
136                 ) {
137
138                         /*
139                          * Construct journal message.
140                          * Note that we are transferring ownership of some of the memory here.
141                          */
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;
152
153                         sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
154                         message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
155
156                         /*
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.
161                          */
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"
166                                 "\n"
167                                 "--%s\r\n"
168                                 "Content-type: text/plain\r\n"
169                                 "\r\n"
170                                 "Sender: %s "
171                         ,
172                                 mime_boundary,
173                                 mime_boundary,
174                                 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
175                         );
176
177                         if (journal_msg->cm_fields['F']) {
178                                 sprintf(&message_text[strlen(message_text)], "<%s>",
179                                         journal_msg->cm_fields['F']);
180                         }
181                         else if (journal_msg->cm_fields['N']) {
182                                 sprintf(&message_text[strlen(message_text)], "@ %s",
183                                         journal_msg->cm_fields['N']);
184                         }
185
186                         sprintf(&message_text[strlen(message_text)],
187                                 "\r\n"
188                                 "Message-ID: <%s>\r\n"
189                                 "Recipients:\r\n"
190                         ,
191                                 jmsg->msgn
192                         );
193
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);
201                                 }
202                         }
203
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);
210                                 }
211                         }
212
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);
219                                 }
220                         }
221
222                         sprintf(&message_text[strlen(message_text)],
223                                 "\r\n"
224                                 "--%s\r\n"
225                                 "Content-type: message/rfc822\r\n"
226                                 "\r\n"
227                                 "%s"
228                                 "--%s--\r\n"
229                         ,
230                                 mime_boundary,
231                                 jmsg->rfc822,
232                                 mime_boundary
233                         );
234
235                         journal_msg->cm_fields['M'] = message_text;
236                         free(jmsg->rfc822);
237                         free(jmsg->msgn);
238                         
239                         /* Submit journal message */
240                         CtdlSubmitMsg(journal_msg, journal_recps, "");
241                         CtdlFreeMessage(journal_msg);
242                 }
243
244                 free(journal_recps);
245         }
246
247         /* We are responsible for freeing this memory. */
248         free(jmsg);
249 }
250
251
252 /*
253  * Run the queue.
254  */
255 void JournalRunQueue(void) {
256         struct jnlq *jptr = NULL;
257
258         while (jnlq != NULL) {
259                 begin_critical_section(S_JOURNAL_QUEUE);
260                 if (jnlq != NULL) {
261                         jptr = jnlq;
262                         jnlq = jnlq->next;
263                 }
264                 end_critical_section(S_JOURNAL_QUEUE);
265                 JournalRunQueueMsg(jptr);
266         }
267 }
268
269