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