Continue phase 2 of modules stuff.
[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 "database.h"
35 #include "msgbase.h"
36 #include "support.h"
37 #include "sysdep_decls.h"
38 #include "citserver.h"
39 #include "room_ops.h"
40 #include "user_ops.h"
41 #include "file_ops.h"
42 #include "config.h"
43 #include "control.h"
44 #include "tools.h"
45 #include "mime_parser.h"
46 #include "html.h"
47 #include "genstamp.h"
48 #include "internet_addressing.h"
49 #include "vcard.h"
50 #include "serv_vcard.h" /* Needed for vcard_getuser and extract_inet_email_addrs */
51 #include "journaling.h"
52
53 struct jnlq *jnlq = NULL;       /* journal queue */
54
55 /*
56  * Hand off a copy of a message to be journalized.
57  */
58 void JournalBackgroundSubmit(struct CtdlMessage *msg,
59                         char *saved_rfc822_version,
60                         struct recptypes *recps) {
61
62         struct jnlq *jptr = NULL;
63
64         /* Avoid double journaling! */
65         if (msg->cm_fields['J'] != NULL) {
66                 free(saved_rfc822_version);
67                 return;
68         }
69
70         jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
71         if (jptr == NULL) {
72                 free(saved_rfc822_version);
73                 return;
74         }
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;
83
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);
89 }
90
91
92 /*
93  * Convert a local user name to an internet email address for the journal
94  */
95 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
96         struct ctdluser us;
97         struct vCard *v;
98
99         strcpy(inetemail, "");
100         if (getuser(&us, localuser) != 0) {
101                 return;
102         }
103
104         v = vcard_get_user(&us);
105         if (v == NULL) {
106                 return;
107         }
108
109         extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
110         vcard_free(v);
111 }
112
113
114 /*
115  * Called by JournalRunQueue() to send an individual message.
116  */
117 void JournalRunQueueMsg(struct jnlq *jmsg) {
118
119         struct CtdlMessage *journal_msg = NULL;
120         struct recptypes *journal_recps = NULL;
121         char *message_text = NULL;
122         char mime_boundary[256];
123         char recipient[256];
124         char inetemail[256];
125         static int seq = 0;
126         int i;
127
128         journal_recps = validate_recipients(config.c_journal_dest);
129         if (journal_recps != NULL) {
130
131                 if (  (journal_recps->num_local > 0)
132                    || (journal_recps->num_internet > 0)
133                    || (journal_recps->num_ignet > 0)
134                    || (journal_recps->num_room > 0)
135                 ) {
136
137                         /*
138                          * Construct journal message.
139                          * Note that we are transferring ownership of some of the memory here.
140                          */
141                         journal_msg = malloc(sizeof(struct CtdlMessage));
142                         memset(journal_msg, 0, sizeof(struct CtdlMessage));
143                         journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
144                         journal_msg->cm_anon_type = MES_NORMAL;
145                         journal_msg->cm_format_type = FMT_RFC822;
146                         journal_msg->cm_fields['J'] = strdup("is journal");
147                         journal_msg->cm_fields['A'] = jmsg->from;
148                         journal_msg->cm_fields['N'] = jmsg->node;
149                         journal_msg->cm_fields['F'] = jmsg->rfca;
150                         journal_msg->cm_fields['U'] = jmsg->subj;
151
152                         sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
153                         message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
154
155                         /*
156                          * Here is where we begin to compose the journalized message.
157                          * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
158                          *       requested by a paying customer, and yes, it is intentionally
159                          *       spelled wrong.  Do NOT remove or change it.
160                          */
161                         sprintf(message_text,
162                                 "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
163                                 "Content-Identifer: ExJournalReport\r\n"
164                                 "MIME-Version: 1.0\r\n"
165                                 "\n"
166                                 "--%s\r\n"
167                                 "Content-type: text/plain\r\n"
168                                 "\r\n"
169                                 "Sender: %s "
170                         ,
171                                 mime_boundary,
172                                 mime_boundary,
173                                 ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
174                         );
175
176                         if (journal_msg->cm_fields['F']) {
177                                 sprintf(&message_text[strlen(message_text)], "<%s>",
178                                         journal_msg->cm_fields['F']);
179                         }
180                         else if (journal_msg->cm_fields['N']) {
181                                 sprintf(&message_text[strlen(message_text)], "@ %s",
182                                         journal_msg->cm_fields['N']);
183                         }
184
185                         sprintf(&message_text[strlen(message_text)],
186                                 "\r\n"
187                                 "Message-ID: <%s>\r\n"
188                                 "Recipients:\r\n"
189                         ,
190                                 jmsg->msgn
191                         );
192
193                         if (jmsg->recps.num_local > 0) {
194                                 for (i=0; i<jmsg->recps.num_local; ++i) {
195                                         extract_token(recipient, jmsg->recps.recp_local,
196                                                         i, '|', sizeof recipient);
197                                         local_to_inetemail(inetemail, recipient, sizeof inetemail);
198                                         sprintf(&message_text[strlen(message_text)],
199                                                 "       %s <%s>\r\n", recipient, inetemail);
200                                 }
201                         }
202
203                         if (jmsg->recps.num_ignet > 0) {
204                                 for (i=0; i<jmsg->recps.num_ignet; ++i) {
205                                         extract_token(recipient, jmsg->recps.recp_ignet,
206                                                         i, '|', sizeof recipient);
207                                         sprintf(&message_text[strlen(message_text)],
208                                                 "       %s\r\n", recipient);
209                                 }
210                         }
211
212                         if (jmsg->recps.num_internet > 0) {
213                                 for (i=0; i<jmsg->recps.num_internet; ++i) {
214                                         extract_token(recipient, jmsg->recps.recp_internet,
215                                                         i, '|', sizeof recipient);
216                                         sprintf(&message_text[strlen(message_text)],
217                                                 "       %s\r\n", recipient);
218                                 }
219                         }
220
221                         sprintf(&message_text[strlen(message_text)],
222                                 "\r\n"
223                                 "--%s\r\n"
224                                 "Content-type: message/rfc822\r\n"
225                                 "\r\n"
226                                 "%s"
227                                 "--%s--\r\n"
228                         ,
229                                 mime_boundary,
230                                 jmsg->rfc822,
231                                 mime_boundary
232                         );
233
234                         journal_msg->cm_fields['M'] = message_text;
235                         free(jmsg->rfc822);
236                         free(jmsg->msgn);
237                         
238                         /* Submit journal message */
239                         CtdlSubmitMsg(journal_msg, journal_recps, "");
240                         CtdlFreeMessage(journal_msg);
241                 }
242
243                 free_recipients(journal_recps);
244         }
245
246         /* We are responsible for freeing this memory. */
247         free(jmsg);
248 }
249
250
251 /*
252  * Run the queue.
253  */
254 void JournalRunQueue(void) {
255         struct jnlq *jptr = NULL;
256
257         while (jnlq != NULL) {
258                 begin_critical_section(S_JOURNAL_QUEUE);
259                 if (jnlq != NULL) {
260                         jptr = jnlq;
261                         jnlq = jnlq->next;
262                 }
263                 end_critical_section(S_JOURNAL_QUEUE);
264                 JournalRunQueueMsg(jptr);
265         }
266 }
267
268