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