d1daf5a7acd7ca3628f23c18638ec4f33e12e8ab
[citadel.git] / citadel / journaling.c
1 /*
2  * Message journaling functions.
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
13 # include <time.h>
14 #else
15 # if HAVE_SYS_TIME_H
16 #  include <sys/time.h>
17 # else
18 #  include <time.h>
19 # endif
20 #endif
21
22
23 #include <ctype.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <sys/stat.h>
29 #include <libcitadel.h>
30 #include "citadel.h"
31 #include "server.h"
32 #include "database.h"
33 #include "msgbase.h"
34 #include "support.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "room_ops.h"
38 #include "user_ops.h"
39 #include "file_ops.h"
40 #include "config.h"
41 #include "control.h"
42 #include "genstamp.h"
43 #include "internet_addressing.h"
44 #include "serv_vcard.h"                 /* Needed for vcard_getuser and extract_inet_email_addrs */
45 #include "journaling.h"
46
47 #include "ctdl_module.h"
48 #include "threads.h"
49
50 struct jnlq *jnlq = NULL;       /* journal queue */
51
52 /*
53  * Hand off a copy of a message to be journalized.
54  */
55 void JournalBackgroundSubmit(struct CtdlMessage *msg,
56                         StrBuf *saved_rfc822_version,
57                         struct recptypes *recps) {
58
59         struct jnlq *jptr = NULL;
60
61         /* Avoid double journaling! */
62         if (msg->cm_fields[eJournal] != NULL) {
63                 FreeStrBuf(&saved_rfc822_version);
64                 return;
65         }
66
67         jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
68         if (jptr == NULL) {
69                 FreeStrBuf(&saved_rfc822_version);
70                 return;
71         }
72         memset(jptr, 0, sizeof(struct jnlq));
73         if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
74         if (msg->cm_fields[eAuthor] != NULL) jptr->from = strdup(msg->cm_fields[eAuthor]);
75         if (msg->cm_fields[eNodeName] != NULL) jptr->node = strdup(msg->cm_fields[eNodeName]);
76         if (msg->cm_fields[erFc822Addr] != NULL) jptr->rfca = strdup(msg->cm_fields[erFc822Addr]);
77         if (msg->cm_fields[eMsgSubject] != NULL) jptr->subj = strdup(msg->cm_fields[eMsgSubject]);
78         if (msg->cm_fields[emessageId] != NULL) jptr->msgn = strdup(msg->cm_fields[emessageId]);
79         jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
80
81         /* Add to the queue */
82         begin_critical_section(S_JOURNAL_QUEUE);
83         jptr->next = jnlq;
84         jnlq = jptr;
85         end_critical_section(S_JOURNAL_QUEUE);
86 }
87
88
89 /*
90  * Convert a local user name to an internet email address for the journal
91  */
92  
93 /*
94  * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
95  * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
96  * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
97  * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
98  * delimiter? Or would it be better to return a linked list of some kind?
99  */
100 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
101         struct ctdluser us;
102         struct vCard *v;
103
104         strcpy(inetemail, "");
105         if (CtdlGetUser(&us, localuser) != 0) {
106                 return;
107         }
108
109         v = vcard_get_user(&us);
110         if (v == NULL) {
111                 return;
112         }
113
114         extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
115         vcard_free(v);
116 }
117
118
119 /*
120  * Called by JournalRunQueue() to send an individual message.
121  */
122 void JournalRunQueueMsg(struct jnlq *jmsg) {
123
124         struct CtdlMessage *journal_msg = NULL;
125         struct recptypes *journal_recps = NULL;
126         StrBuf *message_text = NULL;
127         char mime_boundary[256];
128         long mblen;
129         long rfc822len;
130         char recipient[256];
131         char inetemail[256];
132         static int seq = 0;
133         int i;
134
135         if (jmsg == NULL)
136                 return;
137         journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
138         if (journal_recps != NULL) {
139
140                 if (  (journal_recps->num_local > 0)
141                    || (journal_recps->num_internet > 0)
142                    || (journal_recps->num_ignet > 0)
143                    || (journal_recps->num_room > 0)
144                 ) {
145
146                         /*
147                          * Construct journal message.
148                          * Note that we are transferring ownership of some of the memory here.
149                          */
150                         journal_msg = malloc(sizeof(struct CtdlMessage));
151                         memset(journal_msg, 0, sizeof(struct CtdlMessage));
152                         journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
153                         journal_msg->cm_anon_type = MES_NORMAL;
154                         journal_msg->cm_format_type = FMT_RFC822;
155                         CM_SetField(journal_msg, eJournal, HKEY("is journal"));
156                         CM_SetField(journal_msg, eAuthor, jmsg->from, strlen(jmsg->from));
157                         CM_SetField(journal_msg, eNodeName, jmsg->node, strlen(jmsg->node));
158                         CM_SetField(journal_msg, erFc822Addr, jmsg->rfca, strlen(jmsg->rfca));
159                         CM_SetField(journal_msg, eMsgSubject, jmsg->subj, strlen(jmsg->subj));
160
161                         mblen = snprintf(mime_boundary, sizeof(mime_boundary),
162                                          "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
163                         rfc822len = strlen(jmsg->rfc822);
164                        
165                         message_text = NewStrBufPlain(NULL, rfc822len + sizeof(struct recptypes) + 1024);
166
167                         /*
168                          * Here is where we begin to compose the journalized message.
169                          * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
170                          *       requested by a paying customer, and yes, it is intentionally
171                          *       spelled wrong.  Do NOT remove or change it.
172                          */
173                         StrBufAppendBufPlain(
174                                 message_text, 
175                                 HKEY("Content-type: multipart/mixed; boundary=\""), 0);
176
177                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
178
179                         StrBufAppendBufPlain(
180                                 message_text, 
181                                 HKEY("\"\r\n"
182                                      "Content-Identifer: ExJournalReport\r\n"
183                                      "MIME-Version: 1.0\r\n"
184                                      "\n"
185                                      "--"), 0);
186
187                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
188
189                         StrBufAppendBufPlain(
190                                 message_text, 
191                                 HKEY("\r\n"
192                                      "Content-type: text/plain\r\n"
193                                      "\r\n"
194                                      "Sender: "), 0);
195
196                         if (journal_msg->cm_fields[eAuthor])
197                                 StrBufAppendBufPlain(
198                                         message_text, 
199                                         journal_msg->cm_fields[eAuthor], -1, 0);
200                         else
201                                 StrBufAppendBufPlain(
202                                         message_text, 
203                                         HKEY("(null)"), 0);
204
205                         if (journal_msg->cm_fields[erFc822Addr]) {
206                                 StrBufAppendPrintf(message_text, " <%s>",
207                                                    journal_msg->cm_fields[erFc822Addr]);
208                         }
209                         else if (journal_msg->cm_fields[eNodeName]) {
210                                 StrBufAppendPrintf(message_text, " @ %s",
211                                                    journal_msg->cm_fields[eNodeName]);
212                         }
213                         else
214                                 StrBufAppendBufPlain(
215                                         message_text, 
216                                         HKEY(" "), 0);
217
218                         StrBufAppendBufPlain(
219                                 message_text, 
220                                 HKEY("\r\n"
221                                      "Message-ID: <"), 0);
222
223                         StrBufAppendBufPlain(message_text, jmsg->msgn, -1, 0);
224                         StrBufAppendBufPlain(
225                                 message_text, 
226                                 HKEY(">\r\n"
227                                      "Recipients:\r\n"), 0);
228
229                         if (jmsg->recps.num_local > 0) {
230                                 for (i=0; i<jmsg->recps.num_local; ++i) {
231                                         extract_token(recipient, jmsg->recps.recp_local,
232                                                         i, '|', sizeof recipient);
233                                         local_to_inetemail(inetemail, recipient, sizeof inetemail);
234                                         StrBufAppendPrintf(message_text, 
235                                                            "    %s <%s>\r\n", recipient, inetemail);
236                                 }
237                         }
238
239                         if (jmsg->recps.num_ignet > 0) {
240                                 for (i=0; i<jmsg->recps.num_ignet; ++i) {
241                                         extract_token(recipient, jmsg->recps.recp_ignet,
242                                                         i, '|', sizeof recipient);
243                                         StrBufAppendPrintf(message_text, 
244                                                            "    %s\r\n", recipient);
245                                 }
246                         }
247
248                         if (jmsg->recps.num_internet > 0) {
249                                 for (i=0; i<jmsg->recps.num_internet; ++i) {
250                                         extract_token(recipient, jmsg->recps.recp_internet,
251                                                         i, '|', sizeof recipient);
252                                         StrBufAppendPrintf(message_text, 
253                                                 "       %s\r\n", recipient);
254                                 }
255                         }
256
257                         StrBufAppendBufPlain(
258                                 message_text, 
259                                 HKEY("\r\n"
260                                      "--"), 0);
261
262                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
263
264                         StrBufAppendBufPlain(
265                                 message_text, 
266                                 HKEY("\r\n"
267                                      "Content-type: message/rfc822\r\n"
268                                      "\r\n"), 0);
269
270                         StrBufAppendBufPlain(message_text, jmsg->rfc822, rfc822len, 0);
271
272                         StrBufAppendBufPlain(
273                                 message_text, 
274                                 HKEY("--"), 0);
275
276                         StrBufAppendBufPlain(message_text, mime_boundary, mblen, 0);
277
278                         StrBufAppendBufPlain(
279                                 message_text, 
280                                 HKEY("--\r\n"), 0);
281
282                         CM_SetAsFieldSB(journal_msg, eMesageText, &message_text);
283                         free(jmsg->rfc822);
284                         free(jmsg->msgn);
285                         jmsg->rfc822 = NULL;
286                         jmsg->msgn = NULL;
287                         
288                         /* Submit journal message */
289                         CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
290                         CtdlFreeMessage(journal_msg);
291                 }
292
293                 free_recipients(journal_recps);
294         }
295
296         /* We are responsible for freeing this memory. */
297         free(jmsg);
298 }
299
300
301 /*
302  * Run the queue.
303  */
304 void JournalRunQueue(void) {
305         struct jnlq *jptr = NULL;
306
307         while (jnlq != NULL) {
308                 begin_critical_section(S_JOURNAL_QUEUE);
309                 if (jnlq != NULL) {
310                         jptr = jnlq;
311                         jnlq = jnlq->next;
312                 }
313                 end_critical_section(S_JOURNAL_QUEUE);
314                 JournalRunQueueMsg(jptr);
315         }
316 }
317
318