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