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