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