msgbase.c: upgrade CtdlSetSeen() to the new CtdlGetMsgList() API
[citadel.git] / citadel / server / msgbase.c
1 // Implements the message store.
2 //
3 // Copyright (c) 1987-2023 by the citadel.org team
4 //
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <regex.h>
12 #include <sys/stat.h>
13 #include <assert.h>
14 #include <libcitadel.h>
15 #include "ctdl_module.h"
16 #include "citserver.h"
17 #include "control.h"
18 #include "config.h"
19 #include "clientsocket.h"
20 #include "genstamp.h"
21 #include "room_ops.h"
22 #include "user_ops.h"
23 #include "internet_addressing.h"
24 #include "euidindex.h"
25 #include "msgbase.h"
26 #include "journaling.h"
27
28 struct addresses_to_be_filed *atbf = NULL;
29
30 // These are the four-character field headers we use when outputting
31 // messages in Citadel format (as opposed to RFC822 format).
32 char *msgkeys[] = {
33         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
34         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
35         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
36         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
37         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
38         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
39         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
40         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
41         NULL, 
42         "from", // A -> eAuthor
43         NULL,   // B -> eBig_message
44         NULL,   // C (formerly used as eRemoteRoom)
45         NULL,   // D (formerly used as eDestination)
46         "exti", // E -> eXclusivID
47         "rfca", // F -> erFc822Addr
48         NULL,   // G
49         "hnod", // H (formerly used as eHumanNode)
50         "msgn", // I -> emessageId
51         "jrnl", // J -> eJournal
52         "rep2", // K -> eReplyTo
53         "list", // L -> eListID
54         "text", // M -> eMesageText
55         NULL,   // N (formerly used as eNodename)
56         "room", // O -> eOriginalRoom
57         "path", // P -> eMessagePath
58         NULL,   // Q
59         "rcpt", // R -> eRecipient
60         NULL,   // S (formerly used as eSpecialField)
61         "time", // T -> eTimestamp
62         "subj", // U -> eMsgSubject
63         "nvto", // V -> eenVelopeTo
64         "wefw", // W -> eWeferences
65         NULL,   // X
66         "cccc", // Y -> eCarbonCopY
67         NULL    // Z
68 };
69
70
71 HashList *msgKeyLookup = NULL;
72
73 int GetFieldFromMnemonic(eMsgField *f, const char* c) {
74         void *v = NULL;
75         if (GetHash(msgKeyLookup, c, 4, &v)) {
76                 *f = (eMsgField) v;
77                 return 1;
78         }
79         return 0;
80 }
81
82 void FillMsgKeyLookupTable(void) {
83         long i;
84
85         msgKeyLookup = NewHash (1, FourHash);
86
87         for (i=0; i < 91; i++) {
88                 if (msgkeys[i] != NULL) {
89                         Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
90                 }
91         }
92 }
93
94
95 eMsgField FieldOrder[]  = {
96 /* Important fields */
97         emessageId   ,
98         eMessagePath ,
99         eTimestamp   ,
100         eAuthor      ,
101         erFc822Addr  ,
102         eOriginalRoom,
103         eRecipient   ,
104 /* Semi-important fields */
105         eBig_message ,
106         eExclusiveID ,
107         eWeferences  ,
108         eJournal     ,
109 /* G is not used yet */
110         eReplyTo     ,
111         eListID      ,
112 /* Q is not used yet */
113         eenVelopeTo  ,
114 /* X is not used yet */
115 /* Z is not used yet */
116         eCarbonCopY  ,
117         eMsgSubject  ,
118 /* internal only */
119         eErrorMsg    ,
120         eSuppressIdx ,
121         eExtnotify   ,
122 /* Message text (MUST be last) */
123         eMesageText 
124 /* Not saved to disk: 
125         eVltMsgNum
126 */
127 };
128
129 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
130
131
132 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which) {
133         return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
134 }
135
136
137 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length) {
138         if (Msg->cm_fields[which] != NULL) {
139                 free (Msg->cm_fields[which]);
140         }
141         if (length < 0) {                       // You can set the length to -1 to have CM_SetField measure it for you
142                 length = strlen(buf);
143         }
144         Msg->cm_fields[which] = malloc(length + 1);
145         memcpy(Msg->cm_fields[which], buf, length);
146         Msg->cm_fields[which][length] = '\0';
147         Msg->cm_lengths[which] = length;
148 }
149
150
151 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue) {
152         char buf[128];
153         long len;
154         len = snprintf(buf, sizeof(buf), "%ld", lvalue);
155         CM_SetField(Msg, which, buf, len);
156 }
157
158
159 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen) {
160         if (Msg->cm_fields[WhichToCut] == NULL)
161                 return;
162
163         if (Msg->cm_lengths[WhichToCut] > maxlen)
164         {
165                 Msg->cm_fields[WhichToCut][maxlen] = '\0';
166                 Msg->cm_lengths[WhichToCut] = maxlen;
167         }
168 }
169
170
171 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which) {
172         if (Msg->cm_fields[which] != NULL)
173                 free (Msg->cm_fields[which]);
174         Msg->cm_fields[which] = NULL;
175         Msg->cm_lengths[which] = 0;
176 }
177
178
179 void CM_Flush(struct CtdlMessage *Msg) {
180         int i;
181
182         if (CM_IsValidMsg(Msg) == 0) {
183                 return;
184         }
185
186         for (i = 0; i < 256; ++i) {
187                 CM_FlushField(Msg, i);
188         }
189 }
190
191
192 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy) {
193         long len;
194         if (Msg->cm_fields[WhichToPutTo] != NULL) {
195                 free (Msg->cm_fields[WhichToPutTo]);
196         }
197
198         if (Msg->cm_fields[WhichtToCopy] != NULL) {
199                 len = Msg->cm_lengths[WhichtToCopy];
200                 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
201                 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
202                 Msg->cm_fields[WhichToPutTo][len] = '\0';
203                 Msg->cm_lengths[WhichToPutTo] = len;
204         }
205         else {
206                 Msg->cm_fields[WhichToPutTo] = NULL;
207                 Msg->cm_lengths[WhichToPutTo] = 0;
208         }
209 }
210
211
212 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length) {
213         if (Msg->cm_fields[which] != NULL) {
214                 long oldmsgsize;
215                 long newmsgsize;
216                 char *new;
217
218                 oldmsgsize = Msg->cm_lengths[which] + 1;
219                 newmsgsize = length + oldmsgsize;
220
221                 new = malloc(newmsgsize);
222                 memcpy(new, buf, length);
223                 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
224                 free(Msg->cm_fields[which]);
225                 Msg->cm_fields[which] = new;
226                 Msg->cm_lengths[which] = newmsgsize - 1;
227         }
228         else {
229                 Msg->cm_fields[which] = malloc(length + 1);
230                 memcpy(Msg->cm_fields[which], buf, length);
231                 Msg->cm_fields[which][length] = '\0';
232                 Msg->cm_lengths[which] = length;
233         }
234 }
235
236
237 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length) {
238         if (Msg->cm_fields[which] != NULL) {
239                 free (Msg->cm_fields[which]);
240         }
241
242         Msg->cm_fields[which] = *buf;
243         *buf = NULL;
244         if (length < 0) {                       // You can set the length to -1 to have CM_SetField measure it for you
245                 Msg->cm_lengths[which] = strlen(Msg->cm_fields[which]);
246         }
247         else {
248                 Msg->cm_lengths[which] = length;
249         }
250 }
251
252
253 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf) {
254         if (Msg->cm_fields[which] != NULL) {
255                 free (Msg->cm_fields[which]);
256         }
257
258         Msg->cm_lengths[which] = StrLength(*buf);
259         Msg->cm_fields[which] = SmashStrBuf(buf);
260 }
261
262
263 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen) {
264         if (Msg->cm_fields[which] != NULL) {
265                 *retlen = Msg->cm_lengths[which];
266                 *ret = Msg->cm_fields[which];
267                 Msg->cm_fields[which] = NULL;
268                 Msg->cm_lengths[which] = 0;
269         }
270         else {
271                 *ret = NULL;
272                 *retlen = 0;
273         }
274 }
275
276
277 // Returns 1 if the supplied pointer points to a valid Citadel message.
278 // If the pointer is NULL or the magic number check fails, returns 0.
279 int CM_IsValidMsg(struct CtdlMessage *msg) {
280         if (msg == NULL) {
281                 return 0;
282         }
283         if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
284                 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
285                 return 0;
286         }
287         return 1;
288 }
289
290
291 void CM_FreeContents(struct CtdlMessage *msg) {
292         int i;
293
294         for (i = 0; i < 256; ++i)
295                 if (msg->cm_fields[i] != NULL) {
296                         free(msg->cm_fields[i]);
297                         msg->cm_lengths[i] = 0;
298                 }
299
300         msg->cm_magic = 0;      // just in case
301 }
302
303
304 // 'Destructor' for struct CtdlMessage
305 void CM_Free(struct CtdlMessage *msg) {
306         if (CM_IsValidMsg(msg) == 0) {
307                 if (msg != NULL) free (msg);
308                 return;
309         }
310         CM_FreeContents(msg);
311         free(msg);
312 }
313
314
315 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg) {
316         long len;
317         len = OrgMsg->cm_lengths[i];
318         NewMsg->cm_fields[i] = malloc(len + 1);
319         if (NewMsg->cm_fields[i] == NULL) {
320                 return 0;
321         }
322         memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
323         NewMsg->cm_fields[i][len] = '\0';
324         NewMsg->cm_lengths[i] = len;
325         return 1;
326 }
327
328
329 struct CtdlMessage *CM_Duplicate(struct CtdlMessage *OrgMsg) {
330         int i;
331         struct CtdlMessage *NewMsg;
332
333         if (CM_IsValidMsg(OrgMsg) == 0) {
334                 return NULL;
335         }
336         NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
337         if (NewMsg == NULL) {
338                 return NULL;
339         }
340
341         memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
342
343         memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
344         
345         for (i = 0; i < 256; ++i) {
346                 if (OrgMsg->cm_fields[i] != NULL) {
347                         if (!CM_DupField(i, OrgMsg, NewMsg)) {
348                                 CM_Free(NewMsg);
349                                 return NULL;
350                         }
351                 }
352         }
353
354         return NewMsg;
355 }
356
357
358 // Determine if a given message matches the fields in a message template.
359 // Return 0 for a successful match.
360 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
361         int i;
362
363         // If there aren't any fields in the template, all messages will match.
364         if (template == NULL) return(0);
365
366         // Null messages are bogus.
367         if (msg == NULL) return(1);
368
369         for (i='A'; i<='Z'; ++i) {
370                 if (template->cm_fields[i] != NULL) {
371                         if (msg->cm_fields[i] == NULL) {
372                                 // Considered equal if temmplate is empty string
373                                 if (IsEmptyStr(template->cm_fields[i])) continue;
374                                 return 1;
375                         }
376                         if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
377                             (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
378                                 return 1;
379                 }
380         }
381
382         // All compares succeeded: we have a match!
383         return 0;
384 }
385
386
387 // Retrieve the "seen" message list for the current room.
388 void CtdlGetSeen(char *buf, int which_set) {
389         struct visit vbuf;
390
391         // Learn about the user and room in question
392         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
393
394         if (which_set == ctdlsetseen_seen) {
395                 safestrncpy(buf, vbuf.v_seen, SIZ);
396         }
397         if (which_set == ctdlsetseen_answered) {
398                 safestrncpy(buf, vbuf.v_answered, SIZ);
399         }
400 }
401
402
403 // Manipulate the "seen msgs" string (or other message set strings)
404 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
405                 int target_setting, int which_set,
406                 struct ctdluser *which_user, struct ctdlroom *which_room) {
407         int i, k;
408         int is_seen = 0;
409         int was_seen = 0;
410         long lo = (-1L);
411         long hi = (-1L);
412         struct visit vbuf;
413         long *msglist;
414         int num_msgs = 0;
415         StrBuf *vset;
416         StrBuf *setstr;
417         StrBuf *lostr;
418         StrBuf *histr;
419         const char *pvset;
420         char *is_set;   // actually an array of booleans
421
422         // Don't bother doing *anything* if we were passed a list of zero messages
423         if (num_target_msgnums < 1) {
424                 return;
425         }
426
427         // If no room was specified, we go with the current room.
428         if (!which_room) {
429                 which_room = &CC->room;
430         }
431
432         // If no user was specified, we go with the current user.
433         if (!which_user) {
434                 which_user = &CC->user;
435         }
436
437         syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
438                    num_target_msgnums, target_msgnums[0],
439                    (target_setting ? "SET" : "CLEAR"),
440                    which_set,
441                    which_room->QRname);
442
443         // Learn about the user and room in question
444         CtdlGetRelationship(&vbuf, which_user, which_room);
445
446         // Load the message list
447         num_msgs = CtdlFetchMsgList(which_room->QRnumber, &msglist);
448         if (num_msgs <= 0) {
449                 if (msglist != NULL) {
450                         free(msglist);
451                 }
452                 return;
453         }
454         
455         is_set = malloc(num_msgs * sizeof(char));
456         memset(is_set, 0, (num_msgs * sizeof(char)) );
457
458         // Decide which message set we're manipulating
459         switch(which_set) {
460         case ctdlsetseen_seen:
461                 vset = NewStrBufPlain(vbuf.v_seen, -1);
462                 break;
463         case ctdlsetseen_answered:
464                 vset = NewStrBufPlain(vbuf.v_answered, -1);
465                 break;
466         default:
467                 vset = NewStrBuf();
468         }
469
470         // Translate the existing sequence set into an array of booleans
471         setstr = NewStrBuf();
472         lostr = NewStrBuf();
473         histr = NewStrBuf();
474         pvset = NULL;
475         while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
476
477                 StrBufExtract_token(lostr, setstr, 0, ':');
478                 if (StrBufNum_tokens(setstr, ':') >= 2) {
479                         StrBufExtract_token(histr, setstr, 1, ':');
480                 }
481                 else {
482                         FlushStrBuf(histr);
483                         StrBufAppendBuf(histr, lostr, 0);
484                 }
485                 lo = StrTol(lostr);
486                 if (!strcmp(ChrPtr(histr), "*")) {
487                         hi = LONG_MAX;
488                 }
489                 else {
490                         hi = StrTol(histr);
491                 }
492
493                 for (i = 0; i < num_msgs; ++i) {
494                         if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
495                                 is_set[i] = 1;
496                         }
497                 }
498         }
499         FreeStrBuf(&setstr);
500         FreeStrBuf(&lostr);
501         FreeStrBuf(&histr);
502
503         // Now translate the array of booleans back into a sequence set
504         FlushStrBuf(vset);
505         was_seen = 0;
506         lo = (-1);
507         hi = (-1);
508
509         for (i=0; i<num_msgs; ++i) {
510                 is_seen = is_set[i];
511
512                 // Apply changes
513                 for (k=0; k<num_target_msgnums; ++k) {
514                         if (msglist[i] == target_msgnums[k]) {
515                                 is_seen = target_setting;
516                         }
517                 }
518
519                 if ((was_seen == 0) && (is_seen == 1)) {
520                         lo = msglist[i];
521                 }
522                 else if ((was_seen == 1) && (is_seen == 0)) {
523                         hi = msglist[i-1];
524
525                         if (StrLength(vset) > 0) {
526                                 StrBufAppendBufPlain(vset, HKEY(","), 0);
527                         }
528                         if (lo == hi) {
529                                 StrBufAppendPrintf(vset, "%ld", hi);
530                         }
531                         else {
532                                 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
533                         }
534                 }
535
536                 if ((is_seen) && (i == num_msgs - 1)) {
537                         if (StrLength(vset) > 0) {
538                                 StrBufAppendBufPlain(vset, HKEY(","), 0);
539                         }
540                         if ((i==0) || (was_seen == 0)) {
541                                 StrBufAppendPrintf(vset, "%ld", msglist[i]);
542                         }
543                         else {
544                                 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
545                         }
546                 }
547
548                 was_seen = is_seen;
549         }
550
551         // We will have to stuff this string back into a 4096 byte buffer, so if it's
552         // larger than that now, truncate it by removing tokens from the beginning.
553         // The limit of 100 iterations is there to prevent an infinite loop in case
554         // something unexpected happens.
555         int number_of_truncations = 0;
556         while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
557                 StrBufRemove_token(vset, 0, ',');
558                 ++number_of_truncations;
559         }
560
561         // If we're truncating the sequence set of messages marked with the 'seen' flag,
562         // we want the earliest messages (the truncated ones) to be marked, not unmarked.
563         // Otherwise messages at the beginning will suddenly appear to be 'unseen'.
564         if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
565                 StrBuf *first_tok;
566                 first_tok = NewStrBuf();
567                 StrBufExtract_token(first_tok, vset, 0, ',');
568                 StrBufRemove_token(vset, 0, ',');
569
570                 if (StrBufNum_tokens(first_tok, ':') > 1) {
571                         StrBufRemove_token(first_tok, 0, ':');
572                 }
573                 
574                 StrBuf *new_set;
575                 new_set = NewStrBuf();
576                 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
577                 StrBufAppendBuf(new_set, first_tok, 0);
578                 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
579                 StrBufAppendBuf(new_set, vset, 0);
580
581                 FreeStrBuf(&vset);
582                 FreeStrBuf(&first_tok);
583                 vset = new_set;
584         }
585
586         // Decide which message set we're manipulating.  Zero the buffers so they compress well.
587         switch (which_set) {
588                 case ctdlsetseen_seen:
589                         memset(vbuf.v_seen, 0, sizeof vbuf.v_seen);
590                         safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
591                         break;
592                 case ctdlsetseen_answered:
593                         memset(vbuf.v_answered, 0, sizeof vbuf.v_seen);
594                         safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
595                         break;
596         }
597
598         free(is_set);
599         free(msglist);
600         CtdlSetRelationship(&vbuf, which_user, which_room);
601         FreeStrBuf(&vset);
602 }
603
604
605 // API function to perform an operation for each qualifying message in the
606 // current room.  (Returns the number of messages processed.)
607 int CtdlForEachMessage(int mode, long ref, char *search_string,
608                         char *content_type,
609                         struct CtdlMessage *compare,
610                         ForEachMsgCallback CallBack,
611                         void *userdata)
612 {
613         int a, i, j;
614         struct visit vbuf;
615         struct cdbdata *cdbfr;
616         long *msglist = NULL;
617         int num_msgs = 0;
618         int num_processed = 0;
619         long thismsg;
620         struct MetaData smi;
621         struct CtdlMessage *msg = NULL;
622         int is_seen = 0;
623         long lastold = 0L;
624         int printed_lastold = 0;
625         int num_search_msgs = 0;
626         long *search_msgs = NULL;
627         regex_t re;
628         int need_to_free_re = 0;
629         regmatch_t pm;
630
631         if ((content_type) && (!IsEmptyStr(content_type))) {
632                 regcomp(&re, content_type, 0);
633                 need_to_free_re = 1;
634         }
635
636         // Learn about the user and room in question
637         if (server_shutting_down) {
638                 if (need_to_free_re) regfree(&re);
639                 return -1;
640         }
641         CtdlGetUser(&CC->user, CC->curr_user);
642
643         if (server_shutting_down) {
644                 if (need_to_free_re) regfree(&re);
645                 return -1;
646         }
647         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
648
649         if (server_shutting_down) {
650                 if (need_to_free_re) regfree(&re);
651                 return -1;
652         }
653
654         // Load the message list
655         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
656         if (cdbfr == NULL) {
657                 if (need_to_free_re) regfree(&re);
658                 return 0;       // No messages at all?  No further action.
659         }
660
661         msglist = (long *) cdbfr->ptr;
662         num_msgs = cdbfr->len / sizeof(long);
663
664         cdbfr->ptr = NULL;      // clear this so that cdb_free() doesn't free it
665         cdb_free(cdbfr);        // we own this memory now
666
667         /*
668          * Now begin the traversal.
669          */
670         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
671
672                 /* If the caller is looking for a specific MIME type, filter
673                  * out all messages which are not of the type requested.
674                  */
675                 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
676
677                         /* This call to GetMetaData() sits inside this loop
678                          * so that we only do the extra database read per msg
679                          * if we need to.  Doing the extra read all the time
680                          * really kills the server.  If we ever need to use
681                          * metadata for another search criterion, we need to
682                          * move the read somewhere else -- but still be smart
683                          * enough to only do the read if the caller has
684                          * specified something that will need it.
685                          */
686                         if (server_shutting_down) {
687                                 if (need_to_free_re) regfree(&re);
688                                 free(msglist);
689                                 return -1;
690                         }
691                         GetMetaData(&smi, msglist[a]);
692
693                         /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
694                         if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
695                                 msglist[a] = 0L;
696                         }
697                 }
698         }
699
700         num_msgs = sort_msglist(msglist, num_msgs);
701
702         /* If a template was supplied, filter out the messages which
703          * don't match.  (This could induce some delays!)
704          */
705         if (num_msgs > 0) {
706                 if (compare != NULL) {
707                         for (a = 0; a < num_msgs; ++a) {
708                                 if (server_shutting_down) {
709                                         if (need_to_free_re) regfree(&re);
710                                         free(msglist);
711                                         return -1;
712                                 }
713                                 msg = CtdlFetchMessage(msglist[a], 1);
714                                 if (msg != NULL) {
715                                         if (CtdlMsgCmp(msg, compare)) {
716                                                 msglist[a] = 0L;
717                                         }
718                                         CM_Free(msg);
719                                 }
720                         }
721                 }
722         }
723
724         /* If a search string was specified, get a message list from
725          * the full text index and remove messages which aren't on both
726          * lists.
727          *
728          * How this works:
729          * Since the lists are sorted and strictly ascending, and the
730          * output list is guaranteed to be shorter than or equal to the
731          * input list, we overwrite the bottom of the input list.  This
732          * eliminates the need to memmove big chunks of the list over and
733          * over again.
734          */
735         if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
736
737                 /* Call search module via hook mechanism.
738                  * NULL means use any search function available.
739                  * otherwise replace with a char * to name of search routine
740                  */
741                 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
742
743                 if (num_search_msgs > 0) {
744         
745                         int orig_num_msgs;
746
747                         orig_num_msgs = num_msgs;
748                         num_msgs = 0;
749                         for (i=0; i<orig_num_msgs; ++i) {
750                                 for (j=0; j<num_search_msgs; ++j) {
751                                         if (msglist[i] == search_msgs[j]) {
752                                                 msglist[num_msgs++] = msglist[i];
753                                         }
754                                 }
755                         }
756                 }
757                 else {
758                         num_msgs = 0;   /* No messages qualify */
759                 }
760                 if (search_msgs != NULL) free(search_msgs);
761
762                 /* Now that we've purged messages which don't contain the search
763                  * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
764                  * point on.
765                  */
766                 mode = MSGS_ALL;
767         }
768
769         /*
770          * Now iterate through the message list, according to the
771          * criteria supplied by the caller.
772          */
773         if (num_msgs > 0)
774                 for (a = 0; a < num_msgs; ++a) {
775                         if (server_shutting_down) {
776                                 if (need_to_free_re) regfree(&re);
777                                 free(msglist);
778                                 return num_processed;
779                         }
780                         thismsg = msglist[a];
781                         if (mode == MSGS_ALL) {
782                                 is_seen = 0;
783                         }
784                         else {
785                                 is_seen = is_msg_in_sequence_set(vbuf.v_seen, thismsg);
786                                 if (is_seen) lastold = thismsg;
787                         }
788                         if (
789                                 (thismsg > 0L)
790                         && (
791                                 (mode == MSGS_ALL)
792                                 || ((mode == MSGS_OLD) && (is_seen))
793                                 || ((mode == MSGS_NEW) && (!is_seen))
794                                 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
795                                 || ((mode == MSGS_FIRST) && (a < ref))
796                                 || ((mode == MSGS_GT) && (thismsg > ref))
797                                 || ((mode == MSGS_LT) && (thismsg < ref))
798                                 || ((mode == MSGS_EQ) && (thismsg == ref))
799                             )
800                             ) {
801                                 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
802                                         if (CallBack) {
803                                                 CallBack(lastold, userdata);
804                                         }
805                                         printed_lastold = 1;
806                                         ++num_processed;
807                                 }
808                                 if (CallBack) {
809                                         CallBack(thismsg, userdata);
810                                 }
811                                 ++num_processed;
812                         }
813                 }
814         if (need_to_free_re) regfree(&re);
815
816         /*
817          * We cache the most recent msglist in order to do security checks later
818          */
819         if (CC->client_socket > 0) {
820                 if (CC->cached_msglist != NULL) {
821                         free(CC->cached_msglist);
822                 }
823                 CC->cached_msglist = msglist;
824                 CC->cached_num_msgs = num_msgs;
825         }
826         else {
827                 free(msglist);
828         }
829
830         return num_processed;
831 }
832
833
834 /*
835  * memfmout()  -  Citadel text formatter and paginator.
836  *           Although the original purpose of this routine was to format
837  *           text to the reader's screen width, all we're really using it
838  *           for here is to format text out to 80 columns before sending it
839  *           to the client.  The client software may reformat it again.
840  */
841 void memfmout(
842         char *mptr,             /* where are we going to get our text from? */
843         const char *nl          /* string to terminate lines with */
844 ) {
845         int column = 0;
846         unsigned char ch = 0;
847         char outbuf[1024];
848         int len = 0;
849         int nllen = 0;
850
851         if (!mptr) return;
852         nllen = strlen(nl);
853         while (ch=*(mptr++), ch != 0) {
854
855                 if (ch == '\n') {
856                         if (client_write(outbuf, len) == -1) {
857                                 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
858                                 return;
859                         }
860                         len = 0;
861                         if (client_write(nl, nllen) == -1) {
862                                 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
863                                 return;
864                         }
865                         column = 0;
866                 }
867                 else if (ch == '\r') {
868                         /* Ignore carriage returns.  Newlines are always LF or CRLF but never CR. */
869                 }
870                 else if (isspace(ch)) {
871                         if (column > 72) {              /* Beyond 72 columns, break on the next space */
872                                 if (client_write(outbuf, len) == -1) {
873                                         syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
874                                         return;
875                                 }
876                                 len = 0;
877                                 if (client_write(nl, nllen) == -1) {
878                                         syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
879                                         return;
880                                 }
881                                 column = 0;
882                         }
883                         else {
884                                 outbuf[len++] = ch;
885                                 ++column;
886                         }
887                 }
888                 else {
889                         outbuf[len++] = ch;
890                         ++column;
891                         if (column > 1000) {            /* Beyond 1000 columns, break anywhere */
892                                 if (client_write(outbuf, len) == -1) {
893                                         syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
894                                         return;
895                                 }
896                                 len = 0;
897                                 if (client_write(nl, nllen) == -1) {
898                                         syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
899                                         return;
900                                 }
901                                 column = 0;
902                         }
903                 }
904         }
905         if (len) {
906                 if (client_write(outbuf, len) == -1) {
907                         syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
908                         return;
909                 }
910                 client_write(nl, nllen);
911                 column = 0;
912         }
913 }
914
915
916 /*
917  * Callback function for mime parser that simply lists the part
918  */
919 void list_this_part(char *name, char *filename, char *partnum, char *disp,
920                     void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
921                     char *cbid, void *cbuserdata)
922 {
923         struct ma_info *ma;
924         
925         ma = (struct ma_info *)cbuserdata;
926         if (ma->is_ma == 0) {
927                 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
928                         name, 
929                         filename, 
930                         partnum, 
931                         disp, 
932                         cbtype, 
933                         (long)length, 
934                         cbid, 
935                         cbcharset);
936         }
937 }
938
939
940 /* 
941  * Callback function for multipart prefix
942  */
943 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
944                     void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
945                     char *cbid, void *cbuserdata)
946 {
947         struct ma_info *ma;
948         
949         ma = (struct ma_info *)cbuserdata;
950         if (!strcasecmp(cbtype, "multipart/alternative")) {
951                 ++ma->is_ma;
952         }
953
954         if (ma->is_ma == 0) {
955                 cprintf("pref=%s|%s\n", partnum, cbtype);
956         }
957 }
958
959
960 /* 
961  * Callback function for multipart sufffix
962  */
963 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
964                     void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
965                     char *cbid, void *cbuserdata)
966 {
967         struct ma_info *ma;
968         
969         ma = (struct ma_info *)cbuserdata;
970         if (ma->is_ma == 0) {
971                 cprintf("suff=%s|%s\n", partnum, cbtype);
972         }
973         if (!strcasecmp(cbtype, "multipart/alternative")) {
974                 --ma->is_ma;
975         }
976 }
977
978
979 /*
980  * Callback function for mime parser that opens a section for downloading
981  * we use serv_files function here: 
982  */
983 extern void OpenCmdResult(char *filename, const char *mime_type);
984 void mime_download(char *name, char *filename, char *partnum, char *disp,
985                    void *content, char *cbtype, char *cbcharset, size_t length,
986                    char *encoding, char *cbid, void *cbuserdata)
987 {
988         int rv = 0;
989
990         /* Silently go away if there's already a download open. */
991         if (CC->download_fp != NULL)
992                 return;
993
994         if (
995                 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
996         ||      (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
997         ) {
998                 CC->download_fp = tmpfile();
999                 if (CC->download_fp == NULL) {
1000                         syslog(LOG_ERR, "msgbase: mime_download() couldn't write: %m");
1001                         cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1002                         return;
1003                 }
1004         
1005                 rv = fwrite(content, length, 1, CC->download_fp);
1006                 if (rv <= 0) {
1007                         syslog(LOG_ERR, "msgbase: mime_download() Couldn't write: %m");
1008                         cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1009                         fclose(CC->download_fp);
1010                         CC->download_fp = NULL;
1011                         return;
1012                 }
1013                 fflush(CC->download_fp);
1014                 rewind(CC->download_fp);
1015         
1016                 OpenCmdResult(filename, cbtype);
1017         }
1018 }
1019
1020
1021 /*
1022  * Callback function for mime parser that outputs a section all at once.
1023  * We can specify the desired section by part number *or* content-id.
1024  */
1025 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1026                    void *content, char *cbtype, char *cbcharset, size_t length,
1027                    char *encoding, char *cbid, void *cbuserdata)
1028 {
1029         int *found_it = (int *)cbuserdata;
1030
1031         if (
1032                 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1033         ||      (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1034         ) {
1035                 *found_it = 1;
1036                 cprintf("%d %d|-1|%s|%s|%s\n",
1037                         BINARY_FOLLOWS,
1038                         (int)length,
1039                         filename,
1040                         cbtype,
1041                         cbcharset
1042                 );
1043                 client_write(content, length);
1044         }
1045 }
1046
1047
1048 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length) {
1049         struct CtdlMessage *ret = NULL;
1050         const char *mptr;
1051         const char *upper_bound;
1052         cit_uint8_t ch;
1053         cit_uint8_t field_header;
1054         eMsgField which;
1055
1056         mptr = Buffer;
1057         upper_bound = Buffer + Length;
1058         if (msgnum <= 0) {
1059                 return NULL;
1060         }
1061
1062         // Parse the three bytes that begin EVERY message on disk.
1063         // The first is always 0xFF, the on-disk magic number.
1064         // The second is the anonymous/public type byte.
1065         // The third is the format type byte (vari, fixed, or MIME).
1066         //
1067         ch = *mptr++;
1068         if (ch != 255) {
1069                 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1070                 return NULL;
1071         }
1072         ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1073         memset(ret, 0, sizeof(struct CtdlMessage));
1074
1075         ret->cm_magic = CTDLMESSAGE_MAGIC;
1076         ret->cm_anon_type = *mptr++;                            // Anon type byte
1077         ret->cm_format_type = *mptr++;                          // Format type byte
1078
1079         // The rest is zero or more arbitrary fields.  Load them in.
1080         // We're done when we encounter either a zero-length field or
1081         // have just processed the 'M' (message text) field.
1082         //
1083         do {
1084                 field_header = '\0';
1085                 long len;
1086
1087                 while (field_header == '\0') {                  // work around possibly buggy messages
1088                         if (mptr >= upper_bound) {
1089                                 break;
1090                         }
1091                         field_header = *mptr++;
1092                 }
1093                 if (mptr >= upper_bound) {
1094                         break;
1095                 }
1096                 which = field_header;
1097                 len = strlen(mptr);
1098
1099                 CM_SetField(ret, which, mptr, len);
1100
1101                 mptr += len + 1;                                // advance to next field
1102
1103         } while ((mptr < upper_bound) && (field_header != 'M'));
1104         return (ret);
1105 }
1106
1107
1108 // Load a message from disk into memory.
1109 // This is used by CtdlOutputMsg() and other fetch functions.
1110 //
1111 // NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1112 //       using the CM_Free(); function.
1113 //
1114 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body) {
1115         struct cdbdata *dmsgtext;
1116         struct CtdlMessage *ret = NULL;
1117
1118         syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1119         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1120         if (dmsgtext == NULL) {
1121                 syslog(LOG_ERR, "msgbase: message #%ld was not found", msgnum);
1122                 return NULL;
1123         }
1124
1125         if (dmsgtext->ptr[dmsgtext->len - 1] != '\0') {
1126                 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1127                 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1128         }
1129
1130         ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1131
1132         cdb_free(dmsgtext);
1133
1134         if (ret == NULL) {
1135                 return NULL;
1136         }
1137
1138         // Always make sure there's something in the msg text field.  If
1139         // it's NULL, the message text is most likely stored separately,
1140         // so go ahead and fetch that.  Failing that, just set a dummy
1141         // body so other code doesn't barf.
1142         //
1143         if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1144                 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1145                 if (dmsgtext != NULL) {
1146                         CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1147                         cdb_free(dmsgtext);
1148                 }
1149         }
1150         if (CM_IsEmpty(ret, eMesageText)) {
1151                 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1152         }
1153
1154         return (ret);
1155 }
1156
1157
1158 // Pre callback function for multipart/alternative
1159 //
1160 // NOTE: this differs from the standard behavior for a reason.  Normally when
1161 //       displaying multipart/alternative you want to show the _last_ usable
1162 //       format in the message.  Here we show the _first_ one, because it's
1163 //       usually text/plain.  Since this set of functions is designed for text
1164 //       output to non-MIME-aware clients, this is the desired behavior.
1165 //
1166 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1167                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1168                 char *cbid, void *cbuserdata)
1169 {
1170         struct ma_info *ma;
1171         
1172         ma = (struct ma_info *)cbuserdata;
1173         syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);     
1174         if (!strcasecmp(cbtype, "multipart/alternative")) {
1175                 ++ma->is_ma;
1176                 ma->did_print = 0;
1177         }
1178         if (!strcasecmp(cbtype, "message/rfc822")) {
1179                 ++ma->freeze;
1180         }
1181 }
1182
1183
1184 //
1185 // Post callback function for multipart/alternative
1186 //
1187 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1188                 void *content, char *cbtype, char *cbcharset, size_t length,
1189                 char *encoding, char *cbid, void *cbuserdata)
1190 {
1191         struct ma_info *ma;
1192         
1193         ma = (struct ma_info *)cbuserdata;
1194         syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);    
1195         if (!strcasecmp(cbtype, "multipart/alternative")) {
1196                 --ma->is_ma;
1197                 ma->did_print = 0;
1198         }
1199         if (!strcasecmp(cbtype, "message/rfc822")) {
1200                 --ma->freeze;
1201         }
1202 }
1203
1204
1205 // Inline callback function for mime parser that wants to display text
1206 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1207                 void *content, char *cbtype, char *cbcharset, size_t length,
1208                 char *encoding, char *cbid, void *cbuserdata)
1209 {
1210         char *ptr;
1211         char *wptr;
1212         size_t wlen;
1213         struct ma_info *ma;
1214
1215         ma = (struct ma_info *)cbuserdata;
1216
1217         syslog(LOG_DEBUG,
1218                 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1219                 partnum, filename, cbtype, (long)length
1220         );
1221
1222         // If we're in the middle of a multipart/alternative scope and
1223         // we've already printed another section, skip this one.
1224         if ( (ma->is_ma) && (ma->did_print) ) {
1225                 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1226                 return;
1227         }
1228         ma->did_print = 1;
1229
1230         if ( (!strcasecmp(cbtype, "text/plain")) 
1231            || (IsEmptyStr(cbtype)) ) {
1232                 wptr = content;
1233                 if (length > 0) {
1234                         client_write(wptr, length);
1235                         if (wptr[length-1] != '\n') {
1236                                 cprintf("\n");
1237                         }
1238                 }
1239                 return;
1240         }
1241
1242         if (!strcasecmp(cbtype, "text/html")) {
1243                 ptr = html_to_ascii(content, length, 80, 0);
1244                 wlen = strlen(ptr);
1245                 client_write(ptr, wlen);
1246                 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1247                         cprintf("\n");
1248                 }
1249                 free(ptr);
1250                 return;
1251         }
1252
1253         if (ma->use_fo_hooks) {
1254                 if (PerformFixedOutputHooks(cbtype, content, length)) {         // returns nonzero if it handled the part
1255                         return;
1256                 }
1257         }
1258
1259         if (strncasecmp(cbtype, "multipart/", 10)) {
1260                 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1261                         partnum, filename, cbtype, (long)length);
1262                 return;
1263         }
1264 }
1265
1266
1267 // The client is elegant and sophisticated and wants to be choosy about
1268 // MIME content types, so figure out which multipart/alternative part
1269 // we're going to send.
1270 //
1271 // We use a system of weights.  When we find a part that matches one of the
1272 // MIME types we've declared as preferential, we can store it in ma->chosen_part
1273 // and then set ma->chosen_pref to that MIME type's position in our preference
1274 // list.  If we then hit another match, we only replace the first match if
1275 // the preference value is lower.
1276 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1277                 void *content, char *cbtype, char *cbcharset, size_t length,
1278                 char *encoding, char *cbid, void *cbuserdata)
1279 {
1280         char buf[1024];
1281         int i;
1282         struct ma_info *ma;
1283         
1284         ma = (struct ma_info *)cbuserdata;
1285
1286         for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1287                 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1288                 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1289                         if (i < ma->chosen_pref) {
1290                                 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1291                                 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1292                                 ma->chosen_pref = i;
1293                         }
1294                 }
1295         }
1296 }
1297
1298
1299 // Now that we've chosen our preferred part, output it.
1300 void output_preferred(char *name, 
1301                       char *filename, 
1302                       char *partnum, 
1303                       char *disp,
1304                       void *content, 
1305                       char *cbtype, 
1306                       char *cbcharset, 
1307                       size_t length,
1308                       char *encoding, 
1309                       char *cbid, 
1310                       void *cbuserdata)
1311 {
1312         int i;
1313         char buf[128];
1314         int add_newline = 0;
1315         char *text_content;
1316         struct ma_info *ma;
1317         char *decoded = NULL;
1318         size_t bytes_decoded;
1319         int rc = 0;
1320
1321         ma = (struct ma_info *)cbuserdata;
1322
1323         // This is not the MIME part you're looking for...
1324         if (strcasecmp(partnum, ma->chosen_part)) return;
1325
1326         // If the content-type of this part is in our preferred formats
1327         // list, we can simply output it verbatim.
1328         for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1329                 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1330                 if (!strcasecmp(buf, cbtype)) {
1331                         /* Yeah!  Go!  W00t!! */
1332                         if (ma->dont_decode == 0) 
1333                                 rc = mime_decode_now (content, 
1334                                                       length,
1335                                                       encoding,
1336                                                       &decoded,
1337                                                       &bytes_decoded);
1338                         if (rc < 0)
1339                                 break; // Give us the chance, maybe theres another one.
1340
1341                         if (rc == 0) text_content = (char *)content;
1342                         else {
1343                                 text_content = decoded;
1344                                 length = bytes_decoded;
1345                         }
1346
1347                         if (text_content[length-1] != '\n') {
1348                                 ++add_newline;
1349                         }
1350                         cprintf("Content-type: %s", cbtype);
1351                         if (!IsEmptyStr(cbcharset)) {
1352                                 cprintf("; charset=%s", cbcharset);
1353                         }
1354                         cprintf("\nContent-length: %d\n",
1355                                 (int)(length + add_newline) );
1356                         if (!IsEmptyStr(encoding)) {
1357                                 cprintf("Content-transfer-encoding: %s\n", encoding);
1358                         }
1359                         else {
1360                                 cprintf("Content-transfer-encoding: 7bit\n");
1361                         }
1362                         cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1363                         cprintf("\n");
1364                         if (client_write(text_content, length) == -1)
1365                         {
1366                                 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1367                                 return;
1368                         }
1369                         if (add_newline) cprintf("\n");
1370                         if (decoded != NULL) free(decoded);
1371                         return;
1372                 }
1373         }
1374
1375         // No translations required or possible: output as text/plain
1376         cprintf("Content-type: text/plain\n\n");
1377         rc = 0;
1378         if (ma->dont_decode == 0)
1379                 rc = mime_decode_now (content, 
1380                                       length,
1381                                       encoding,
1382                                       &decoded,
1383                                       &bytes_decoded);
1384         if (rc < 0)
1385                 return; // Give us the chance, maybe theres another one.
1386         
1387         if (rc == 0) text_content = (char *)content;
1388         else {
1389                 text_content = decoded;
1390                 length = bytes_decoded;
1391         }
1392
1393         fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset, length, encoding, cbid, cbuserdata);
1394         if (decoded != NULL) free(decoded);
1395 }
1396
1397
1398 struct encapmsg {
1399         char desired_section[64];
1400         char *msg;
1401         size_t msglen;
1402 };
1403
1404
1405 // Callback function
1406 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1407                    void *content, char *cbtype, char *cbcharset, size_t length,
1408                    char *encoding, char *cbid, void *cbuserdata)
1409 {
1410         struct encapmsg *encap;
1411
1412         encap = (struct encapmsg *)cbuserdata;
1413
1414         // Only proceed if this is the desired section...
1415         if (!strcasecmp(encap->desired_section, partnum)) {
1416                 encap->msglen = length;
1417                 encap->msg = malloc(length + 2);
1418                 memcpy(encap->msg, content, length);
1419                 return;
1420         }
1421 }
1422
1423
1424 // Determine whether the specified message exists in the cached_msglist
1425 // (This is a security check)
1426 int check_cached_msglist(long msgnum) {
1427
1428         // cases in which we skip the check
1429         if (!CC) return om_ok;                                          // not a session
1430         if (CC->client_socket <= 0) return om_ok;                       // not a client session
1431         if (CC->cached_msglist == NULL) return om_access_denied;        // no msglist fetched
1432         if (CC->cached_num_msgs == 0) return om_access_denied;          // nothing to check 
1433
1434         // Do a binary search within the cached_msglist for the requested msgnum
1435         int min = 0;
1436         int max = (CC->cached_num_msgs - 1);
1437
1438         while (max >= min) {
1439                 int middle = min + (max-min) / 2 ;
1440                 if (msgnum == CC->cached_msglist[middle]) {
1441                         return om_ok;
1442                 }
1443                 if (msgnum > CC->cached_msglist[middle]) {
1444                         min = middle + 1;
1445                 }
1446                 else {
1447                         max = middle - 1;
1448                 }
1449         }
1450
1451         return om_access_denied;
1452 }
1453
1454
1455 // Get a message off disk.  (returns om_* values found in msgbase.h)
1456 int CtdlOutputMsg(long msg_num,         // message number (local) to fetch
1457                 int mode,               // how would you like that message?
1458                 int headers_only,       // eschew the message body?
1459                 int do_proto,           // do Citadel protocol responses?
1460                 int crlf,               // Use CRLF newlines instead of LF?
1461                 char *section,          // NULL or a message/rfc822 section
1462                 int flags,              // various flags; see msgbase.h
1463                 char **Author,
1464                 char **Address,
1465                 char **MessageID
1466 ) {
1467         struct CtdlMessage *TheMessage = NULL;
1468         int retcode = CIT_OK;
1469         struct encapmsg encap;
1470         int r;
1471
1472         syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)", 
1473                 msg_num, mode,
1474                 (section ? section : "<>")
1475         );
1476
1477         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1478         if (r != om_ok) {
1479                 if (do_proto) {
1480                         if (r == om_not_logged_in) {
1481                                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1482                         }
1483                         else {
1484                                 cprintf("%d An unknown error has occurred.\n", ERROR);
1485                         }
1486                 }
1487                 return(r);
1488         }
1489
1490         /*
1491          * Check to make sure the message is actually IN this room
1492          */
1493         r = check_cached_msglist(msg_num);
1494         if (r == om_access_denied) {
1495                 /* Not in the cache?  We get ONE shot to check it again. */
1496                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1497                 r = check_cached_msglist(msg_num);
1498         }
1499         if (r != om_ok) {
1500                 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1501                            msg_num, CC->room.QRname
1502                 );
1503                 if (do_proto) {
1504                         if (r == om_access_denied) {
1505                                 cprintf("%d message %ld was not found in this room\n",
1506                                         ERROR + HIGHER_ACCESS_REQUIRED,
1507                                         msg_num
1508                                 );
1509                         }
1510                 }
1511                 return(r);
1512         }
1513
1514         /*
1515          * Fetch the message from disk.  If we're in HEADERS_FAST mode,
1516          * request that we don't even bother loading the body into memory.
1517          */
1518         if (headers_only == HEADERS_FAST) {
1519                 TheMessage = CtdlFetchMessage(msg_num, 0);
1520         }
1521         else {
1522                 TheMessage = CtdlFetchMessage(msg_num, 1);
1523         }
1524
1525         if (TheMessage == NULL) {
1526                 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1527                         ERROR + MESSAGE_NOT_FOUND, msg_num);
1528                 return(om_no_such_msg);
1529         }
1530
1531         /* Here is the weird form of this command, to process only an
1532          * encapsulated message/rfc822 section.
1533          */
1534         if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1535                 memset(&encap, 0, sizeof encap);
1536                 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1537                 mime_parser(CM_RANGE(TheMessage, eMesageText),
1538                             *extract_encapsulated_message,
1539                             NULL, NULL, (void *)&encap, 0
1540                         );
1541
1542                 if ((Author != NULL) && (*Author == NULL))
1543                 {
1544                         long len;
1545                         CM_GetAsField(TheMessage, eAuthor, Author, &len);
1546                 }
1547                 if ((Address != NULL) && (*Address == NULL))
1548                 {       
1549                         long len;
1550                         CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1551                 }
1552                 if ((MessageID != NULL) && (*MessageID == NULL))
1553                 {       
1554                         long len;
1555                         CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1556                 }
1557                 CM_Free(TheMessage);
1558                 TheMessage = NULL;
1559
1560                 if (encap.msg) {
1561                         encap.msg[encap.msglen] = 0;
1562                         TheMessage = convert_internet_message(encap.msg);
1563                         encap.msg = NULL;       /* no free() here, TheMessage owns it now */
1564
1565                         /* Now we let it fall through to the bottom of this
1566                          * function, because TheMessage now contains the
1567                          * encapsulated message instead of the top-level
1568                          * message.  Isn't that neat?
1569                          */
1570                 }
1571                 else {
1572                         if (do_proto) {
1573                                 cprintf("%d msg %ld has no part %s\n",
1574                                         ERROR + MESSAGE_NOT_FOUND,
1575                                         msg_num,
1576                                         section);
1577                         }
1578                         retcode = om_no_such_msg;
1579                 }
1580
1581         }
1582
1583         /* Ok, output the message now */
1584         if (retcode == CIT_OK)
1585                 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1586         if ((Author != NULL) && (*Author == NULL))
1587         {
1588                 long len;
1589                 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1590         }
1591         if ((Address != NULL) && (*Address == NULL))
1592         {       
1593                 long len;
1594                 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1595         }
1596         if ((MessageID != NULL) && (*MessageID == NULL))
1597         {       
1598                 long len;
1599                 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1600         }
1601
1602         CM_Free(TheMessage);
1603
1604         return(retcode);
1605 }
1606
1607
1608 void OutputCtdlMsgHeaders(struct CtdlMessage *TheMessage, int do_proto) {
1609         int i;
1610         char buf[SIZ];
1611         char display_name[256];
1612
1613         /* begin header processing loop for Citadel message format */
1614         safestrncpy(display_name, "<unknown>", sizeof display_name);
1615         if (!CM_IsEmpty(TheMessage, eAuthor)) {
1616                 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1617                 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1618                         safestrncpy(display_name, "****", sizeof display_name);
1619                 }
1620                 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1621                         safestrncpy(display_name, "anonymous", sizeof display_name);
1622                 }
1623                 else {
1624                         safestrncpy(display_name, buf, sizeof display_name);
1625                 }
1626                 if ((is_room_aide())
1627                     && ((TheMessage->cm_anon_type == MES_ANONONLY)
1628                         || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1629                         size_t tmp = strlen(display_name);
1630                         snprintf(&display_name[tmp],
1631                                  sizeof display_name - tmp,
1632                                  " [%s]", buf);
1633                 }
1634         }
1635
1636         /* Now spew the header fields in the order we like them. */
1637         for (i=0; i< NDiskFields; ++i) {
1638                 eMsgField Field;
1639                 Field = FieldOrder[i];
1640                 if (Field != eMesageText) {
1641                         if ( (!CM_IsEmpty(TheMessage, Field)) && (msgkeys[Field] != NULL) ) {
1642                                 if ((Field == eenVelopeTo) || (Field == eRecipient) || (Field == eCarbonCopY)) {
1643                                         sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1644                                 }
1645                                 if (Field == eAuthor) {
1646                                         if (do_proto) {
1647                                                 cprintf("%s=%s\n", msgkeys[Field], display_name);
1648                                         }
1649                                 }
1650                                 /* Masquerade display name if needed */
1651                                 else {
1652                                         if (do_proto) {
1653                                                 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1654                                         }
1655                                 }
1656                                 /* Give the client a hint about whether the message originated locally */
1657                                 if (Field == erFc822Addr) {
1658                                         if (IsDirectory(TheMessage->cm_fields[Field] ,0)) {
1659                                                 cprintf("locl=yes\n");                          // message originated locally.
1660                                         }
1661
1662
1663
1664                                 }
1665                         }
1666                 }
1667         }
1668 }
1669
1670
1671 void OutputRFC822MsgHeaders(
1672         struct CtdlMessage *TheMessage,
1673         int flags,              /* should the message be exported clean */
1674         const char *nl, int nlen,
1675         char *mid, long sizeof_mid,
1676         char *suser, long sizeof_suser,
1677         char *luser, long sizeof_luser,
1678         char *fuser, long sizeof_fuser,
1679         char *snode, long sizeof_snode)
1680 {
1681         char datestamp[100];
1682         int subject_found = 0;
1683         char buf[SIZ];
1684         int i, j, k;
1685         char *mptr = NULL;
1686         char *mpptr = NULL;
1687         char *hptr;
1688
1689         for (i = 0; i < NDiskFields; ++i) {
1690                 if (TheMessage->cm_fields[FieldOrder[i]]) {
1691                         mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1692                         switch (FieldOrder[i]) {
1693                         case eAuthor:
1694                                 safestrncpy(luser, mptr, sizeof_luser);
1695                                 safestrncpy(suser, mptr, sizeof_suser);
1696                                 break;
1697                         case eCarbonCopY:
1698                                 if ((flags & QP_EADDR) != 0) {
1699                                         mptr = qp_encode_email_addrs(mptr);
1700                                 }
1701                                 sanitize_truncated_recipient(mptr);
1702                                 cprintf("CC: %s%s", mptr, nl);
1703                                 break;
1704                         case eMessagePath:
1705                                 cprintf("Return-Path: %s%s", mptr, nl);
1706                                 break;
1707                         case eListID:
1708                                 cprintf("List-ID: %s%s", mptr, nl);
1709                                 break;
1710                         case eenVelopeTo:
1711                                 if ((flags & QP_EADDR) != 0) 
1712                                         mptr = qp_encode_email_addrs(mptr);
1713                                 hptr = mptr;
1714                                 while ((*hptr != '\0') && isspace(*hptr))
1715                                         hptr ++;
1716                                 if (!IsEmptyStr(hptr))
1717                                         cprintf("Envelope-To: %s%s", hptr, nl);
1718                                 break;
1719                         case eMsgSubject:
1720                                 cprintf("Subject: %s%s", mptr, nl);
1721                                 subject_found = 1;
1722                                 break;
1723                         case emessageId:
1724                                 safestrncpy(mid, mptr, sizeof_mid);
1725                                 break;
1726                         case erFc822Addr:
1727                                 safestrncpy(fuser, mptr, sizeof_fuser);
1728                                 break;
1729                         case eRecipient:
1730                                 if (haschar(mptr, '@') == 0) {
1731                                         sanitize_truncated_recipient(mptr);
1732                                         cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1733                                         cprintf("%s", nl);
1734                                 }
1735                                 else {
1736                                         if ((flags & QP_EADDR) != 0) {
1737                                                 mptr = qp_encode_email_addrs(mptr);
1738                                         }
1739                                         sanitize_truncated_recipient(mptr);
1740                                         cprintf("To: %s", mptr);
1741                                         cprintf("%s", nl);
1742                                 }
1743                                 break;
1744                         case eTimestamp:
1745                                 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1746                                 cprintf("Date: %s%s", datestamp, nl);
1747                                 break;
1748                         case eWeferences:
1749                                 cprintf("References: ");
1750                                 k = num_tokens(mptr, '|');
1751                                 for (j=0; j<k; ++j) {
1752                                         extract_token(buf, mptr, j, '|', sizeof buf);
1753                                         cprintf("<%s>", buf);
1754                                         if (j == (k-1)) {
1755                                                 cprintf("%s", nl);
1756                                         }
1757                                         else {
1758                                                 cprintf(" ");
1759                                         }
1760                                 }
1761                                 break;
1762                         case eReplyTo:
1763                                 hptr = mptr;
1764                                 while ((*hptr != '\0') && isspace(*hptr))
1765                                         hptr ++;
1766                                 if (!IsEmptyStr(hptr))
1767                                         cprintf("Reply-To: %s%s", mptr, nl);
1768                                 break;
1769
1770                         case eExclusiveID:
1771                         case eJournal:
1772                         case eMesageText:
1773                         case eBig_message:
1774                         case eOriginalRoom:
1775                         case eErrorMsg:
1776                         case eSuppressIdx:
1777                         case eExtnotify:
1778                         case eVltMsgNum:
1779                                 /* these don't map to mime message headers. */
1780                                 break;
1781                         }
1782                         if (mptr != mpptr) {
1783                                 free (mptr);
1784                         }
1785                 }
1786         }
1787         if (subject_found == 0) {
1788                 cprintf("Subject: (no subject)%s", nl);
1789         }
1790 }
1791
1792
1793 void Dump_RFC822HeadersBody(
1794         struct CtdlMessage *TheMessage,
1795         int headers_only,       /* eschew the message body? */
1796         int flags,              /* should the bessage be exported clean? */
1797         const char *nl, int nlen)
1798 {
1799         cit_uint8_t prev_ch;
1800         int eoh = 0;
1801         const char *StartOfText = StrBufNOTNULL;
1802         char outbuf[1024];
1803         int outlen = 0;
1804         int nllen = strlen(nl);
1805         char *mptr;
1806         int lfSent = 0;
1807
1808         mptr = TheMessage->cm_fields[eMesageText];
1809
1810         prev_ch = '\0';
1811         while (*mptr != '\0') {
1812                 if (*mptr == '\r') {
1813                         /* do nothing */
1814                 }
1815                 else {
1816                         if ((!eoh) &&
1817                             (*mptr == '\n'))
1818                         {
1819                                 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1820                                 if (!eoh)
1821                                         eoh = *(mptr+1) == '\n';
1822                                 if (eoh)
1823                                 {
1824                                         StartOfText = mptr;
1825                                         StartOfText = strchr(StartOfText, '\n');
1826                                         StartOfText = strchr(StartOfText, '\n');
1827                                 }
1828                         }
1829                         if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1830                             ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1831                             ((headers_only != HEADERS_NONE) && 
1832                              (headers_only != HEADERS_ONLY))
1833                         ) {
1834                                 if (*mptr == '\n') {
1835                                         memcpy(&outbuf[outlen], nl, nllen);
1836                                         outlen += nllen;
1837                                         outbuf[outlen] = '\0';
1838                                 }
1839                                 else {
1840                                         outbuf[outlen++] = *mptr;
1841                                 }
1842                         }
1843                 }
1844                 if (flags & ESC_DOT) {
1845                         if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1846                                 outbuf[outlen++] = '.';
1847                         }
1848                         prev_ch = *mptr;
1849                 }
1850                 ++mptr;
1851                 if (outlen > 1000) {
1852                         if (client_write(outbuf, outlen) == -1) {
1853                                 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1854                                 return;
1855                         }
1856                         lfSent =  (outbuf[outlen - 1] == '\n');
1857                         outlen = 0;
1858                 }
1859         }
1860         if (outlen > 0) {
1861                 client_write(outbuf, outlen);
1862                 lfSent =  (outbuf[outlen - 1] == '\n');
1863         }
1864         if (!lfSent)
1865                 client_write(nl, nlen);
1866 }
1867
1868
1869 /* If the format type on disk is 1 (fixed-format), then we want
1870  * everything to be output completely literally ... regardless of
1871  * what message transfer format is in use.
1872  */
1873 void DumpFormatFixed(
1874         struct CtdlMessage *TheMessage,
1875         int mode,               /* how would you like that message? */
1876         const char *nl, int nllen)
1877 {
1878         cit_uint8_t ch;
1879         char buf[SIZ];
1880         int buflen;
1881         int xlline = 0;
1882         char *mptr;
1883
1884         mptr = TheMessage->cm_fields[eMesageText];
1885         
1886         if (mode == MT_MIME) {
1887                 cprintf("Content-type: text/plain\n\n");
1888         }
1889         *buf = '\0';
1890         buflen = 0;
1891         while (ch = *mptr++, ch > 0) {
1892                 if (ch == '\n')
1893                         ch = '\r';
1894
1895                 if ((buflen > 250) && (!xlline)){
1896                         int tbuflen;
1897                         tbuflen = buflen;
1898
1899                         while ((buflen > 0) && 
1900                                (!isspace(buf[buflen])))
1901                                 buflen --;
1902                         if (buflen == 0) {
1903                                 xlline = 1;
1904                         }
1905                         else {
1906                                 mptr -= tbuflen - buflen;
1907                                 buf[buflen] = '\0';
1908                                 ch = '\r';
1909                         }
1910                 }
1911
1912                 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
1913                 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
1914                         ch = '\r';
1915                 }
1916
1917                 if (ch == '\r') {
1918                         memcpy (&buf[buflen], nl, nllen);
1919                         buflen += nllen;
1920                         buf[buflen] = '\0';
1921
1922                         if (client_write(buf, buflen) == -1) {
1923                                 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
1924                                 return;
1925                         }
1926                         *buf = '\0';
1927                         buflen = 0;
1928                         xlline = 0;
1929                 } else {
1930                         buf[buflen] = ch;
1931                         buflen++;
1932                 }
1933         }
1934         buf[buflen] = '\0';
1935         if (!IsEmptyStr(buf)) {
1936                 cprintf("%s%s", buf, nl);
1937         }
1938 }
1939
1940
1941 /*
1942  * Get a message off disk.  (returns om_* values found in msgbase.h)
1943  */
1944 int CtdlOutputPreLoadedMsg(
1945                 struct CtdlMessage *TheMessage,
1946                 int mode,               /* how would you like that message? */
1947                 int headers_only,       /* eschew the message body? */
1948                 int do_proto,           /* do Citadel protocol responses? */
1949                 int crlf,               /* Use CRLF newlines instead of LF? */
1950                 int flags               /* should the bessage be exported clean? */
1951 ) {
1952         int i;
1953         const char *nl; /* newline string */
1954         int nlen;
1955         struct ma_info ma;
1956
1957         /* Buffers needed for RFC822 translation.  These are all filled
1958          * using functions that are bounds-checked, and therefore we can
1959          * make them substantially smaller than SIZ.
1960          */
1961         char suser[1024];
1962         char luser[1024];
1963         char fuser[1024];
1964         char snode[1024];
1965         char mid[1024];
1966
1967         syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
1968                    ((TheMessage == NULL) ? "NULL" : "not null"),
1969                    mode, headers_only, do_proto, crlf
1970         );
1971
1972         strcpy(mid, "unknown");
1973         nl = (crlf ? "\r\n" : "\n");
1974         nlen = crlf ? 2 : 1;
1975
1976         if (!CM_IsValidMsg(TheMessage)) {
1977                 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
1978                 return(om_no_such_msg);
1979         }
1980
1981         /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
1982          * Pad it with spaces in order to avoid changing the RFC822 length of the message.
1983          */
1984         if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
1985                 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
1986         }
1987                 
1988         /* Are we downloading a MIME component? */
1989         if (mode == MT_DOWNLOAD) {
1990                 if (TheMessage->cm_format_type != FMT_RFC822) {
1991                         if (do_proto)
1992                                 cprintf("%d This is not a MIME message.\n",
1993                                 ERROR + ILLEGAL_VALUE);
1994                 } else if (CC->download_fp != NULL) {
1995                         if (do_proto) cprintf(
1996                                 "%d You already have a download open.\n",
1997                                 ERROR + RESOURCE_BUSY);
1998                 } else {
1999                         /* Parse the message text component */
2000                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2001                                     *mime_download, NULL, NULL, NULL, 0);
2002                         /* If there's no file open by this time, the requested
2003                          * section wasn't found, so print an error
2004                          */
2005                         if (CC->download_fp == NULL) {
2006                                 if (do_proto) cprintf(
2007                                         "%d Section %s not found.\n",
2008                                         ERROR + FILE_NOT_FOUND,
2009                                         CC->download_desired_section);
2010                         }
2011                 }
2012                 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2013         }
2014
2015         // MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2016         // in a single server operation instead of opening a download file.
2017         if (mode == MT_SPEW_SECTION) {
2018                 if (TheMessage->cm_format_type != FMT_RFC822) {
2019                         if (do_proto)
2020                                 cprintf("%d This is not a MIME message.\n",
2021                                 ERROR + ILLEGAL_VALUE);
2022                 }
2023                 else {
2024                         // Locate and parse the component specified by the caller
2025                         int found_it = 0;
2026                         mime_parser(CM_RANGE(TheMessage, eMesageText), *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2027
2028                         // If section wasn't found, print an error
2029                         if (!found_it) {
2030                                 if (do_proto) {
2031                                         cprintf( "%d Section %s not found.\n", ERROR + FILE_NOT_FOUND, CC->download_desired_section);
2032                                 }
2033                         }
2034                 }
2035                 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2036         }
2037
2038         // now for the user-mode message reading loops
2039         if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2040
2041         // Does the caller want to skip the headers?
2042         if (headers_only == HEADERS_NONE) goto START_TEXT;
2043
2044         // Tell the client which format type we're using.
2045         if ( (mode == MT_CITADEL) && (do_proto) ) {
2046                 cprintf("type=%d\n", TheMessage->cm_format_type);       // Tell the client which format type we're using.
2047         }
2048
2049         // nhdr=yes means that we're only displaying headers, no body
2050         if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2051            && ((mode == MT_CITADEL) || (mode == MT_MIME))
2052            && (do_proto)
2053            ) {
2054                 cprintf("nhdr=yes\n");
2055         }
2056
2057         if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
2058                 OutputCtdlMsgHeaders(TheMessage, do_proto);
2059         }
2060
2061         // begin header processing loop for RFC822 transfer format
2062         strcpy(suser, "");
2063         strcpy(luser, "");
2064         strcpy(fuser, "");
2065         strcpy(snode, "");
2066         if (mode == MT_RFC822) 
2067                 OutputRFC822MsgHeaders(
2068                         TheMessage,
2069                         flags,
2070                         nl, nlen,
2071                         mid, sizeof(mid),
2072                         suser, sizeof(suser),
2073                         luser, sizeof(luser),
2074                         fuser, sizeof(fuser),
2075                         snode, sizeof(snode)
2076                         );
2077
2078
2079         for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2080                 suser[i] = tolower(suser[i]);
2081                 if (!isalnum(suser[i])) suser[i]='_';
2082         }
2083
2084         if (mode == MT_RFC822) {
2085                 /* Construct a fun message id */
2086                 cprintf("Message-ID: <%s", mid);
2087                 if (strchr(mid, '@')==NULL) {
2088                         cprintf("@%s", snode);
2089                 }
2090                 cprintf(">%s", nl);
2091
2092                 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2093                         cprintf("From: \"----\" <x@x.org>%s", nl);
2094                 }
2095                 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2096                         cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2097                 }
2098                 else if (!IsEmptyStr(fuser)) {
2099                         cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2100                 }
2101                 else {
2102                         cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2103                 }
2104
2105                 /* Blank line signifying RFC822 end-of-headers */
2106                 if (TheMessage->cm_format_type != FMT_RFC822) {
2107                         cprintf("%s", nl);
2108                 }
2109         }
2110
2111         // end header processing loop ... at this point, we're in the text
2112 START_TEXT:
2113         if (headers_only == HEADERS_FAST) goto DONE;
2114
2115         // Tell the client about the MIME parts in this message
2116         if (TheMessage->cm_format_type == FMT_RFC822) {
2117                 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2118                         memset(&ma, 0, sizeof(struct ma_info));
2119                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2120                                 (do_proto ? *list_this_part : NULL),
2121                                 (do_proto ? *list_this_pref : NULL),
2122                                 (do_proto ? *list_this_suff : NULL),
2123                                 (void *)&ma, 1);
2124                 }
2125                 else if (mode == MT_RFC822) {   // unparsed RFC822 dump
2126                         Dump_RFC822HeadersBody(
2127                                 TheMessage,
2128                                 headers_only,
2129                                 flags,
2130                                 nl, nlen);
2131                         goto DONE;
2132                 }
2133         }
2134
2135         if (headers_only == HEADERS_ONLY) {
2136                 goto DONE;
2137         }
2138
2139         // signify start of msg text
2140         if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2141                 if (do_proto) cprintf("text\n");
2142         }
2143
2144         if (TheMessage->cm_format_type == FMT_FIXED) 
2145                 DumpFormatFixed(
2146                         TheMessage,
2147                         mode,           // how would you like that message?
2148                         nl, nlen);
2149
2150         // If the message on disk is format 0 (Citadel vari-format), we
2151         // output using the formatter at 80 columns.  This is the final output
2152         // form if the transfer format is RFC822, but if the transfer format
2153         // is Citadel proprietary, it'll still work, because the indentation
2154         // for new paragraphs is correct and the client will reformat the
2155         // message to the reader's screen width.
2156         //
2157         if (TheMessage->cm_format_type == FMT_CITADEL) {
2158                 if (mode == MT_MIME) {
2159                         cprintf("Content-type: text/x-citadel-variformat\n\n");
2160                 }
2161                 memfmout(TheMessage->cm_fields[eMesageText], nl);
2162         }
2163
2164         // If the message on disk is format 4 (MIME), we've gotta hand it
2165         // off to the MIME parser.  The client has already been told that
2166         // this message is format 1 (fixed format), so the callback function
2167         // we use will display those parts as-is.
2168         //
2169         if (TheMessage->cm_format_type == FMT_RFC822) {
2170                 memset(&ma, 0, sizeof(struct ma_info));
2171
2172                 if (mode == MT_MIME) {
2173                         ma.use_fo_hooks = 0;
2174                         strcpy(ma.chosen_part, "1");
2175                         ma.chosen_pref = 9999;
2176                         ma.dont_decode = CC->msg4_dont_decode;
2177                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2178                                     *choose_preferred, *fixed_output_pre,
2179                                     *fixed_output_post, (void *)&ma, 1);
2180                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2181                                     *output_preferred, NULL, NULL, (void *)&ma, 1);
2182                 }
2183                 else {
2184                         ma.use_fo_hooks = 1;
2185                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2186                                     *fixed_output, *fixed_output_pre,
2187                                     *fixed_output_post, (void *)&ma, 0);
2188                 }
2189
2190         }
2191
2192 DONE:   /* now we're done */
2193         if (do_proto) cprintf("000\n");
2194         return(om_ok);
2195 }
2196
2197 // Save one or more message pointers into a specified room
2198 // (Returns 0 for success, nonzero for failure)
2199 // roomname may be NULL to use the current room
2200 //
2201 // Note that the 'supplied_msg' field may be set to NULL, in which case
2202 // the message will be fetched from disk, by number, if we need to perform
2203 // replication checks.  This adds an additional database read, so if the
2204 // caller already has the message in memory then it should be supplied.  (Obviously
2205 // this mode of operation only works if we're saving a single message.)
2206 //
2207 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2208                         int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2209 ) {
2210         int i, j, unique;
2211         char hold_rm[ROOMNAMELEN];
2212         struct cdbdata *cdbfr;
2213         int num_msgs;
2214         long *msglist;
2215         long highest_msg = 0L;
2216
2217         long msgid = 0;
2218         struct CtdlMessage *msg = NULL;
2219
2220         long *msgs_to_be_merged = NULL;
2221         int num_msgs_to_be_merged = 0;
2222
2223         syslog(LOG_DEBUG,
2224                 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2225                 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2226         );
2227
2228         strcpy(hold_rm, CC->room.QRname);
2229
2230         /* Sanity checks */
2231         if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2232         if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2233         if (num_newmsgs > 1) supplied_msg = NULL;
2234
2235         /* Now the regular stuff */
2236         if (CtdlGetRoomLock(&CC->room,
2237            ((roomname != NULL) ? roomname : CC->room.QRname) )
2238            != 0) {
2239                 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2240                 return(ERROR + ROOM_NOT_FOUND);
2241         }
2242
2243
2244         msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2245         num_msgs_to_be_merged = 0;
2246
2247
2248         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
2249         if (cdbfr == NULL) {
2250                 msglist = NULL;
2251                 num_msgs = 0;
2252         }
2253         else {
2254                 msglist = (long *) cdbfr->ptr;
2255                 cdbfr->ptr = NULL;      // CtdlSaveMsgPointerInRoom() now owns this memory (this needs attention if we move to LMDB)
2256                 num_msgs = cdbfr->len / sizeof(long);
2257                 cdb_free(cdbfr);
2258         }
2259
2260
2261         /* Create a list of msgid's which were supplied by the caller, but do
2262          * not already exist in the target room.  It is absolutely taboo to
2263          * have more than one reference to the same message in a room.
2264          */
2265         for (i=0; i<num_newmsgs; ++i) {
2266                 unique = 1;
2267                 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2268                         if (msglist[j] == newmsgidlist[i]) {
2269                                 unique = 0;
2270                         }
2271                 }
2272                 if (unique) {
2273                         msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2274                 }
2275         }
2276
2277         syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2278
2279         /*
2280          * Now merge the new messages
2281          */
2282         msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2283         if (msglist == NULL) {
2284                 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2285                 free(msgs_to_be_merged);
2286                 return (ERROR + INTERNAL_ERROR);
2287         }
2288         memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2289         num_msgs += num_msgs_to_be_merged;
2290
2291         /* Sort the message list, so all the msgid's are in order */
2292         num_msgs = sort_msglist(msglist, num_msgs);
2293
2294         /* Determine the highest message number */
2295         highest_msg = msglist[num_msgs - 1];
2296
2297         /* Write it back to disk. */
2298         cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
2299                   msglist, (int)(num_msgs * sizeof(long)));
2300
2301         /* Free up the memory we used. */
2302         free(msglist);
2303
2304         /* Update the highest-message pointer and unlock the room. */
2305         CC->room.QRhighest = highest_msg;
2306         CtdlPutRoomLock(&CC->room);
2307
2308         /* Perform replication checks if necessary */
2309         if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2310                 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2311
2312                 for (i=0; i<num_msgs_to_be_merged; ++i) {
2313                         msgid = msgs_to_be_merged[i];
2314         
2315                         if (supplied_msg != NULL) {
2316                                 msg = supplied_msg;
2317                         }
2318                         else {
2319                                 msg = CtdlFetchMessage(msgid, 0);
2320                         }
2321         
2322                         if (msg != NULL) {
2323                                 ReplicationChecks(msg);
2324                 
2325                                 /* If the message has an Exclusive ID, index that... */
2326                                 if (!CM_IsEmpty(msg, eExclusiveID)) {
2327                                         index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2328                                 }
2329
2330                                 /* Free up the memory we may have allocated */
2331                                 if (msg != supplied_msg) {
2332                                         CM_Free(msg);
2333                                 }
2334                         }
2335         
2336                 }
2337         }
2338
2339         else {
2340                 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2341         }
2342
2343         /* Submit this room for processing by hooks */
2344         int total_roomhook_errors = PerformRoomHooks(&CC->room);
2345         if (total_roomhook_errors) {
2346                 syslog(LOG_WARNING, "msgbase: room hooks returned %d errors", total_roomhook_errors);
2347         }
2348
2349         /* Go back to the room we were in before we wandered here... */
2350         CtdlGetRoom(&CC->room, hold_rm);
2351
2352         /* Bump the reference count for all messages which were merged */
2353         if (!suppress_refcount_adj) {
2354                 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2355         }
2356
2357         /* Free up memory... */
2358         if (msgs_to_be_merged != NULL) {
2359                 free(msgs_to_be_merged);
2360         }
2361
2362         /* Return success. */
2363         return (0);
2364 }
2365
2366
2367 /*
2368  * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2369  * a single message.
2370  */
2371 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2372                              int do_repl_check, struct CtdlMessage *supplied_msg)
2373 {
2374         return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2375 }
2376
2377
2378 /*
2379  * Message base operation to save a new message to the message store
2380  * (returns new message number)
2381  *
2382  * This is the back end for CtdlSubmitMsg() and should not be directly
2383  * called by server-side modules.
2384  *
2385  */
2386 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2387         long retval;
2388         struct ser_ret smr;
2389         int is_bigmsg = 0;
2390         char *holdM = NULL;
2391         long holdMLen = 0;
2392
2393         /*
2394          * If the message is big, set its body aside for storage elsewhere
2395          * and we hide the message body from the serializer
2396          */
2397         if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG) {
2398                 is_bigmsg = 1;
2399                 holdM = msg->cm_fields[eMesageText];
2400                 msg->cm_fields[eMesageText] = NULL;
2401                 holdMLen = msg->cm_lengths[eMesageText];
2402                 msg->cm_lengths[eMesageText] = 0;
2403         }
2404
2405         /* Serialize our data structure for storage in the database */  
2406         CtdlSerializeMessage(&smr, msg);
2407
2408         if (is_bigmsg) {
2409                 /* put the message body back into the message */
2410                 msg->cm_fields[eMesageText] = holdM;
2411                 msg->cm_lengths[eMesageText] = holdMLen;
2412         }
2413
2414         if (smr.len == 0) {
2415                 if (Reply) {
2416                         cprintf("%d Unable to serialize message\n",
2417                                 ERROR + INTERNAL_ERROR);
2418                 }
2419                 else {
2420                         syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2421
2422                 }
2423                 return (-1L);
2424         }
2425
2426         /* Write our little bundle of joy into the message base */
2427         retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long), smr.ser, smr.len);
2428         if (retval < 0) {
2429                 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2430         }
2431         else {
2432                 if (is_bigmsg) {
2433                         retval = cdb_store(CDB_BIGMSGS,
2434                                            &msgid,
2435                                            (int)sizeof(long),
2436                                            holdM,
2437                                            (holdMLen + 1)
2438                                 );
2439                         if (retval < 0) {
2440                                 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2441                         }
2442                 }
2443         }
2444
2445         /* Free the memory we used for the serialized message */
2446         free(smr.ser);
2447
2448         return(retval);
2449 }
2450
2451
2452 long send_message(struct CtdlMessage *msg) {
2453         long newmsgid;
2454         long retval;
2455         char msgidbuf[256];
2456         long msgidbuflen;
2457
2458         /* Get a new message number */
2459         newmsgid = get_new_message_number();
2460
2461         /* Generate an ID if we don't have one already */
2462         if (CM_IsEmpty(msg, emessageId)) {
2463                 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2464                                        (long unsigned int) time(NULL),
2465                                        (long unsigned int) newmsgid,
2466                                        CtdlGetConfigStr("c_fqdn")
2467                         );
2468
2469                 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2470         }
2471
2472         retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2473
2474         if (retval == 0) {
2475                 retval = newmsgid;
2476         }
2477
2478         /* Return the *local* message ID to the caller
2479          * (even if we're storing an incoming network message)
2480          */
2481         return(retval);
2482 }
2483
2484
2485 /*
2486  * Serialize a struct CtdlMessage into the format used on disk.
2487  * 
2488  * This function loads up a "struct ser_ret" (defined in server.h) which
2489  * contains the length of the serialized message and a pointer to the
2490  * serialized message in memory.  THE LATTER MUST BE FREED BY THE CALLER.
2491  */
2492 void CtdlSerializeMessage(struct ser_ret *ret,          /* return values */
2493                           struct CtdlMessage *msg)      /* unserialized msg */
2494 {
2495         size_t wlen;
2496         int i;
2497
2498         /*
2499          * Check for valid message format
2500          */
2501         if (CM_IsValidMsg(msg) == 0) {
2502                 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2503                 ret->len = 0;
2504                 ret->ser = NULL;
2505                 return;
2506         }
2507
2508         ret->len = 3;
2509         for (i=0; i < NDiskFields; ++i)
2510                 if (msg->cm_fields[FieldOrder[i]] != NULL)
2511                         ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2512
2513         ret->ser = malloc(ret->len);
2514         if (ret->ser == NULL) {
2515                 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2516                 ret->len = 0;
2517                 ret->ser = NULL;
2518                 return;
2519         }
2520
2521         ret->ser[0] = 0xFF;
2522         ret->ser[1] = msg->cm_anon_type;
2523         ret->ser[2] = msg->cm_format_type;
2524         wlen = 3;
2525
2526         for (i=0; i < NDiskFields; ++i) {
2527                 if (msg->cm_fields[FieldOrder[i]] != NULL) {
2528                         ret->ser[wlen++] = (char)FieldOrder[i];
2529
2530                         memcpy(&ret->ser[wlen],
2531                                msg->cm_fields[FieldOrder[i]],
2532                                msg->cm_lengths[FieldOrder[i]] + 1);
2533
2534                         wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2535                 }
2536         }
2537
2538         if (ret->len != wlen) {
2539                 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2540         }
2541
2542         return;
2543 }
2544
2545
2546 /*
2547  * Check to see if any messages already exist in the current room which
2548  * carry the same Exclusive ID as this one.  If any are found, delete them.
2549  */
2550 void ReplicationChecks(struct CtdlMessage *msg) {
2551         long old_msgnum = (-1L);
2552
2553         if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2554
2555         syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2556
2557         /* No exclusive id?  Don't do anything. */
2558         if (msg == NULL) return;
2559         if (CM_IsEmpty(msg, eExclusiveID)) return;
2560
2561         /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2562           msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2563
2564         old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2565         if (old_msgnum > 0L) {
2566                 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2567                 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2568         }
2569 }
2570
2571
2572 /*
2573  * Save a message to disk and submit it into the delivery system.
2574  */
2575 long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
2576                    struct recptypes *recps,     /* recipients (if mail) */
2577                    const char *force            /* force a particular room? */
2578 ) {
2579         char hold_rm[ROOMNAMELEN];
2580         char actual_rm[ROOMNAMELEN];
2581         char force_room[ROOMNAMELEN];
2582         char content_type[SIZ];                 /* We have to learn this */
2583         char recipient[SIZ];
2584         char bounce_to[1024];
2585         const char *room;
2586         long newmsgid;
2587         const char *mptr = NULL;
2588         struct ctdluser userbuf;
2589         int a, i;
2590         struct MetaData smi;
2591         char *collected_addresses = NULL;
2592         struct addresses_to_be_filed *aptr = NULL;
2593         StrBuf *saved_rfc822_version = NULL;
2594         int qualified_for_journaling = 0;
2595
2596         syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2597         if (CM_IsValidMsg(msg) == 0) return(-1);        /* self check */
2598
2599         /* If this message has no timestamp, we take the liberty of
2600          * giving it one, right now.
2601          */
2602         if (CM_IsEmpty(msg, eTimestamp)) {
2603                 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2604         }
2605
2606         /* If this message has no path, we generate one.
2607          */
2608         if (CM_IsEmpty(msg, eMessagePath)) {
2609                 if (!CM_IsEmpty(msg, eAuthor)) {
2610                         CM_CopyField(msg, eMessagePath, eAuthor);
2611                         for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2612                                 if (isspace(msg->cm_fields[eMessagePath][a])) {
2613                                         msg->cm_fields[eMessagePath][a] = ' ';
2614                                 }
2615                         }
2616                 }
2617                 else {
2618                         CM_SetField(msg, eMessagePath, HKEY("unknown"));
2619                 }
2620         }
2621
2622         if (force == NULL) {
2623                 force_room[0] = '\0';
2624         }
2625         else {
2626                 strcpy(force_room, force);
2627         }
2628
2629         /* Learn about what's inside, because it's what's inside that counts */
2630         if (CM_IsEmpty(msg, eMesageText)) {
2631                 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2632                 return(-2);
2633         }
2634
2635         switch (msg->cm_format_type) {
2636         case 0:
2637                 strcpy(content_type, "text/x-citadel-variformat");
2638                 break;
2639         case 1:
2640                 strcpy(content_type, "text/plain");
2641                 break;
2642         case 4:
2643                 strcpy(content_type, "text/plain");
2644                 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2645                 if (mptr != NULL) {
2646                         char *aptr;
2647                         safestrncpy(content_type, &mptr[13], sizeof content_type);
2648                         string_trim(content_type);
2649                         aptr = content_type;
2650                         while (!IsEmptyStr(aptr)) {
2651                                 if ((*aptr == ';')
2652                                     || (*aptr == ' ')
2653                                     || (*aptr == 13)
2654                                     || (*aptr == 10)) {
2655                                         *aptr = 0;
2656                                 }
2657                                 else aptr++;
2658                         }
2659                 }
2660         }
2661
2662         /* Goto the correct room */
2663         room = (recps) ? CC->room.QRname : SENTITEMS;
2664         syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2665         strcpy(hold_rm, CC->room.QRname);
2666         strcpy(actual_rm, CC->room.QRname);
2667         if (recps != NULL) {
2668                 strcpy(actual_rm, SENTITEMS);
2669         }
2670
2671         /* If the user is a twit, move to the twit room for posting */
2672         if (TWITDETECT) {
2673                 if (CC->user.axlevel == AxProbU) {
2674                         strcpy(hold_rm, actual_rm);
2675                         strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2676                         syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2677                 }
2678         }
2679
2680         /* ...or if this message is destined for Aide> then go there. */
2681         if (!IsEmptyStr(force_room)) {
2682                 strcpy(actual_rm, force_room);
2683         }
2684
2685         syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2686         if (strcasecmp(actual_rm, CC->room.QRname)) {
2687                 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2688         }
2689
2690         /*
2691          * If this message has no O (room) field, generate one.
2692          */
2693         if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2694                 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
2695         }
2696
2697         /* Perform "before save" hooks (aborting if any return nonzero) */
2698         syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2699         if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2700
2701         /*
2702          * If this message has an Exclusive ID, and the room is replication
2703          * checking enabled, then do replication checks.
2704          */
2705         if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2706                 ReplicationChecks(msg);
2707         }
2708
2709         /* Save it to disk */
2710         syslog(LOG_DEBUG, "msgbase: saving to disk");
2711         newmsgid = send_message(msg);
2712         if (newmsgid <= 0L) return(-5);
2713
2714         /* Write a supplemental message info record.  This doesn't have to
2715          * be a critical section because nobody else knows about this message
2716          * yet.
2717          */
2718         syslog(LOG_DEBUG, "msgbase: creating metadata record");
2719         memset(&smi, 0, sizeof(struct MetaData));
2720         smi.meta_msgnum = newmsgid;
2721         smi.meta_refcount = 0;
2722         safestrncpy(smi.meta_content_type, content_type, sizeof smi.meta_content_type);
2723
2724         /*
2725          * Measure how big this message will be when rendered as RFC822.
2726          * We do this for two reasons:
2727          * 1. We need the RFC822 length for the new metadata record, so the
2728          *    POP and IMAP services don't have to calculate message lengths
2729          *    while the user is waiting (multiplied by potentially hundreds
2730          *    or thousands of messages).
2731          * 2. If journaling is enabled, we will need an RFC822 version of the
2732          *    message to attach to the journalized copy.
2733          */
2734         if (CC->redirect_buffer != NULL) {
2735                 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2736                 exit(CTDLEXIT_REDIRECT);
2737         }
2738         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2739         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2740         smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2741         saved_rfc822_version = CC->redirect_buffer;
2742         CC->redirect_buffer = NULL;
2743
2744         PutMetaData(&smi);
2745
2746         /* Now figure out where to store the pointers */
2747         syslog(LOG_DEBUG, "msgbase: storing pointers");
2748
2749         /* If this is being done by the networker delivering a private
2750          * message, we want to BYPASS saving the sender's copy (because there
2751          * is no local sender; it would otherwise go to the Trashcan).
2752          */
2753         if ((!CC->internal_pgm) || (recps == NULL)) {
2754                 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2755                         syslog(LOG_ERR, "msgbase: ERROR saving message pointer %ld in %s", newmsgid, actual_rm);
2756                         CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2757                 }
2758         }
2759
2760         /* For internet mail, drop a copy in the outbound queue room */
2761         if ((recps != NULL) && (recps->num_internet > 0)) {
2762                 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2763         }
2764
2765         /* If other rooms are specified, drop them there too. */
2766         if ((recps != NULL) && (recps->num_room > 0)) {
2767                 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2768                         extract_token(recipient, recps->recp_room, i, '|', sizeof recipient);
2769                         syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2770                         CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2771                 }
2772         }
2773
2774         /* Decide where bounces need to be delivered */
2775         if ((recps != NULL) && (recps->bounce_to == NULL)) {
2776                 if (CC->logged_in) {
2777                         strcpy(bounce_to, CC->user.fullname);
2778                 }
2779                 else if (!IsEmptyStr(msg->cm_fields[eAuthor])){
2780                         strcpy(bounce_to, msg->cm_fields[eAuthor]);
2781                 }
2782                 recps->bounce_to = bounce_to;
2783         }
2784                 
2785         CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2786
2787         // If this is private, local mail, make a copy in the recipient's mailbox and bump the reference count.
2788         if ((recps != NULL) && (recps->num_local > 0)) {
2789                 char *pch;
2790                 int ntokens;
2791
2792                 pch = recps->recp_local;
2793                 recps->recp_local = recipient;
2794                 ntokens = num_tokens(pch, '|');
2795                 for (i=0; i<ntokens; ++i) {
2796                         extract_token(recipient, pch, i, '|', sizeof recipient);
2797                         syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2798                         if (CtdlGetUser(&userbuf, recipient) == 0) {
2799                                 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2800                                 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2801                                 CtdlBumpNewMailCounter(userbuf.usernum);        // if this user is logged in, tell them they have new mail.
2802                                 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2803                         }
2804                         else {
2805                                 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2806                                 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2807                         }
2808                 }
2809                 recps->recp_local = pch;
2810         }
2811
2812         /* Perform "after save" hooks */
2813         syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2814
2815         PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2816         CM_FlushField(msg, eVltMsgNum);
2817
2818         /* Go back to the room we started from */
2819         syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2820         if (strcasecmp(hold_rm, CC->room.QRname)) {
2821                 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2822         }
2823
2824         /*
2825          * Any addresses to harvest for someone's address book?
2826          */
2827         if ( (CC->logged_in) && (recps != NULL) ) {
2828                 collected_addresses = harvest_collected_addresses(msg);
2829         }
2830
2831         if (collected_addresses != NULL) {
2832                 aptr = (struct addresses_to_be_filed *) malloc(sizeof(struct addresses_to_be_filed));
2833                 CtdlMailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
2834                 aptr->roomname = strdup(actual_rm);
2835                 aptr->collected_addresses = collected_addresses;
2836                 begin_critical_section(S_ATBF);
2837                 aptr->next = atbf;
2838                 atbf = aptr;
2839                 end_critical_section(S_ATBF);
2840         }
2841
2842         /*
2843          * Determine whether this message qualifies for journaling.
2844          */
2845         if (!CM_IsEmpty(msg, eJournal)) {
2846                 qualified_for_journaling = 0;
2847         }
2848         else {
2849                 if (recps == NULL) {
2850                         qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2851                 }
2852                 else if (recps->num_local + recps->num_internet > 0) {
2853                         qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2854                 }
2855                 else {
2856                         qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2857                 }
2858         }
2859
2860         /*
2861          * Do we have to perform journaling?  If so, hand off the saved
2862          * RFC822 version will be handed off to the journaler for background
2863          * submit.  Otherwise, we have to free the memory ourselves.
2864          */
2865         if (saved_rfc822_version != NULL) {
2866                 if (qualified_for_journaling) {
2867                         JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2868                 }
2869                 else {
2870                         FreeStrBuf(&saved_rfc822_version);
2871                 }
2872         }
2873
2874         if ((recps != NULL) && (recps->bounce_to == bounce_to))
2875                 recps->bounce_to = NULL;
2876
2877         /* Done. */
2878         return(newmsgid);
2879 }
2880
2881
2882 /*
2883  * Convenience function for generating small administrative messages.
2884  */
2885 long quickie_message(char *from,
2886                      char *fromaddr,
2887                      char *to,
2888                      char *room,
2889                      char *text, 
2890                      int format_type,
2891                      char *subject)
2892 {
2893         struct CtdlMessage *msg;
2894         struct recptypes *recp = NULL;
2895
2896         msg = malloc(sizeof(struct CtdlMessage));
2897         memset(msg, 0, sizeof(struct CtdlMessage));
2898         msg->cm_magic = CTDLMESSAGE_MAGIC;
2899         msg->cm_anon_type = MES_NORMAL;
2900         msg->cm_format_type = format_type;
2901
2902         if (!IsEmptyStr(from)) {
2903                 CM_SetField(msg, eAuthor, from, -1);
2904         }
2905         else if (!IsEmptyStr(fromaddr)) {
2906                 char *pAt;
2907                 CM_SetField(msg, eAuthor, fromaddr, -1);
2908                 pAt = strchr(msg->cm_fields[eAuthor], '@');
2909                 if (pAt != NULL) {
2910                         CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
2911                 }
2912         }
2913         else {
2914                 msg->cm_fields[eAuthor] = strdup("Citadel");
2915         }
2916
2917         if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, -1);
2918         if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, -1);
2919         if (!IsEmptyStr(to)) {
2920                 CM_SetField(msg, eRecipient, to, -1);
2921                 recp = validate_recipients(to, NULL, 0);
2922         }
2923         if (!IsEmptyStr(subject)) {
2924                 CM_SetField(msg, eMsgSubject, subject, -1);
2925         }
2926         if (!IsEmptyStr(text)) {
2927                 CM_SetField(msg, eMesageText, text, -1);
2928         }
2929
2930         long msgnum = CtdlSubmitMsg(msg, recp, room);
2931         CM_Free(msg);
2932         if (recp != NULL) free_recipients(recp);
2933         return msgnum;
2934 }
2935
2936
2937 /*
2938  * Back end function used by CtdlMakeMessage() and similar functions
2939  */
2940 StrBuf *CtdlReadMessageBodyBuf(char *terminator,        // token signalling EOT
2941                                long tlen,
2942                                size_t maxlen,           // maximum message length
2943                                StrBuf *exist,           // if non-null, append to it; exist is ALWAYS freed
2944                                int crlf                 // CRLF newlines instead of LF
2945 ) {
2946         StrBuf *Message;
2947         StrBuf *LineBuf;
2948         int flushing = 0;
2949         int finished = 0;
2950         int dotdot = 0;
2951
2952         LineBuf = NewStrBufPlain(NULL, SIZ);
2953         if (exist == NULL) {
2954                 Message = NewStrBufPlain(NULL, 4 * SIZ);
2955         }
2956         else {
2957                 Message = NewStrBufDup(exist);
2958         }
2959
2960         /* Do we need to change leading ".." to "." for SMTP escaping? */
2961         if ((tlen == 1) && (*terminator == '.')) {
2962                 dotdot = 1;
2963         }
2964
2965         /* read in the lines of message text one by one */
2966         do {
2967                 if (CtdlClientGetLine(LineBuf) < 0) {
2968                         finished = 1;
2969                 }
2970                 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
2971                         finished = 1;
2972                 }
2973                 if ( (!flushing) && (!finished) ) {
2974                         if (crlf) {
2975                                 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
2976                         }
2977                         else {
2978                                 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
2979                         }
2980                         
2981                         /* Unescape SMTP-style input of two dots at the beginning of the line */
2982                         if ((dotdot) && (StrLength(LineBuf) > 1) && (ChrPtr(LineBuf)[0] == '.')) {
2983                                 StrBufCutLeft(LineBuf, 1);
2984                         }
2985                         StrBufAppendBuf(Message, LineBuf, 0);
2986                 }
2987
2988                 /* if we've hit the max msg length, flush the rest */
2989                 if (StrLength(Message) >= maxlen) {
2990                         flushing = 1;
2991                 }
2992
2993         } while (!finished);
2994         FreeStrBuf(&LineBuf);
2995
2996         if (flushing) {
2997                 syslog(LOG_ERR, "msgbase: exceeded maximum message length of %ld - message was truncated", maxlen);
2998         }
2999
3000         return Message;
3001 }
3002
3003
3004 // Back end function used by CtdlMakeMessage() and similar functions
3005 char *CtdlReadMessageBody(char *terminator,     // token signalling EOT
3006                           long tlen,
3007                           size_t maxlen,        // maximum message length
3008                           StrBuf *exist,        // if non-null, append to it; exist is ALWAYS freed
3009                           int crlf              // CRLF newlines instead of LF
3010 ) {
3011         StrBuf *Message;
3012
3013         Message = CtdlReadMessageBodyBuf(terminator,
3014                                          tlen,
3015                                          maxlen,
3016                                          exist,
3017                                          crlf
3018         );
3019         if (Message == NULL) {
3020                 return NULL;
3021         }
3022         else {
3023                 return SmashStrBuf(&Message);
3024         }
3025 }
3026
3027
3028 struct CtdlMessage *CtdlMakeMessage(
3029         struct ctdluser *author,        /* author's user structure */
3030         char *recipient,                /* NULL if it's not mail */
3031         char *recp_cc,                  /* NULL if it's not mail */
3032         char *room,                     /* room where it's going */
3033         int type,                       /* see MES_ types in header file */
3034         int format_type,                /* variformat, plain text, MIME... */
3035         char *fake_name,                /* who we're masquerading as */
3036         char *my_email,                 /* which of my email addresses to use (empty is ok) */
3037         char *subject,                  /* Subject (optional) */
3038         char *supplied_euid,            /* ...or NULL if this is irrelevant */
3039         char *preformatted_text,        /* ...or NULL to read text from client */
3040         char *references                /* Thread references */
3041 ) {
3042         return CtdlMakeMessageLen(
3043                 author, /* author's user structure */
3044                 recipient,              /* NULL if it's not mail */
3045                 (recipient)?strlen(recipient) : 0,
3046                 recp_cc,                        /* NULL if it's not mail */
3047                 (recp_cc)?strlen(recp_cc): 0,
3048                 room,                   /* room where it's going */
3049                 (room)?strlen(room): 0,
3050                 type,                   /* see MES_ types in header file */
3051                 format_type,            /* variformat, plain text, MIME... */
3052                 fake_name,              /* who we're masquerading as */
3053                 (fake_name)?strlen(fake_name): 0,
3054                 my_email,                       /* which of my email addresses to use (empty is ok) */
3055                 (my_email)?strlen(my_email): 0,
3056                 subject,                        /* Subject (optional) */
3057                 (subject)?strlen(subject): 0,
3058                 supplied_euid,          /* ...or NULL if this is irrelevant */
3059                 (supplied_euid)?strlen(supplied_euid):0,
3060                 preformatted_text,      /* ...or NULL to read text from client */
3061                 (preformatted_text)?strlen(preformatted_text) : 0,
3062                 references,             /* Thread references */
3063                 (references)?strlen(references):0);
3064
3065 }
3066
3067
3068 /*
3069  * Build a binary message to be saved on disk.
3070  * (NOTE: if you supply 'preformatted_text', the buffer you give it
3071  * will become part of the message.  This means you are no longer
3072  * responsible for managing that memory -- it will be freed along with
3073  * the rest of the fields when CM_Free() is called.)
3074  */
3075 struct CtdlMessage *CtdlMakeMessageLen(
3076         struct ctdluser *author,        /* author's user structure */
3077         char *recipient,                /* NULL if it's not mail */
3078         long rcplen,
3079         char *recp_cc,                  /* NULL if it's not mail */
3080         long cclen,
3081         char *room,                     /* room where it's going */
3082         long roomlen,
3083         int type,                       /* see MES_ types in header file */
3084         int format_type,                /* variformat, plain text, MIME... */
3085         char *fake_name,                /* who we're masquerading as */
3086         long fnlen,
3087         char *my_email,                 /* which of my email addresses to use (empty is ok) */
3088         long myelen,
3089         char *subject,                  /* Subject (optional) */
3090         long subjlen,
3091         char *supplied_euid,            /* ...or NULL if this is irrelevant */
3092         long euidlen,
3093         char *preformatted_text,        /* ...or NULL to read text from client */
3094         long textlen,
3095         char *references,               /* Thread references */
3096         long reflen
3097 ) {
3098         long blen;
3099         char buf[1024];
3100         struct CtdlMessage *msg;
3101         StrBuf *FakeAuthor;
3102         StrBuf *FakeEncAuthor = NULL;
3103
3104         msg = malloc(sizeof(struct CtdlMessage));
3105         memset(msg, 0, sizeof(struct CtdlMessage));
3106         msg->cm_magic = CTDLMESSAGE_MAGIC;
3107         msg->cm_anon_type = type;
3108         msg->cm_format_type = format_type;
3109
3110         if (recipient != NULL) rcplen = string_trim(recipient);
3111         if (recp_cc != NULL) cclen = string_trim(recp_cc);
3112
3113         /* Path or Return-Path */
3114         if (myelen > 0) {
3115                 CM_SetField(msg, eMessagePath, my_email, myelen);
3116         }
3117         else if (!IsEmptyStr(author->fullname)) {
3118                 CM_SetField(msg, eMessagePath, author->fullname, -1);
3119         }
3120         convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3121
3122         blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3123         CM_SetField(msg, eTimestamp, buf, blen);
3124
3125         if (fnlen > 0) {
3126                 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3127         }
3128         else {
3129                 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3130         }
3131         StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3132         CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3133         FreeStrBuf(&FakeAuthor);
3134
3135         if (!!IsEmptyStr(CC->room.QRname)) {
3136                 if (CC->room.QRflags & QR_MAILBOX) {            /* room */
3137                         CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], -1);
3138                 }
3139                 else {
3140                         CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
3141                 }
3142         }
3143
3144         if (rcplen > 0) {
3145                 CM_SetField(msg, eRecipient, recipient, rcplen);
3146         }
3147         if (cclen > 0) {
3148                 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3149         }
3150
3151         if (myelen > 0) {
3152                 CM_SetField(msg, erFc822Addr, my_email, myelen);
3153         }
3154         else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3155                 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, -1);
3156         }
3157
3158         if (subject != NULL) {
3159                 long length;
3160                 length = string_trim(subject);
3161                 if (length > 0) {
3162                         long i;
3163                         long IsAscii;
3164                         IsAscii = -1;
3165                         i = 0;
3166                         while ((subject[i] != '\0') &&
3167                                (IsAscii = isascii(subject[i]) != 0 ))
3168                                 i++;
3169                         if (IsAscii != 0)
3170                                 CM_SetField(msg, eMsgSubject, subject, subjlen);
3171                         else /* ok, we've got utf8 in the string. */
3172                         {
3173                                 char *rfc2047Subj;
3174                                 rfc2047Subj = rfc2047encode(subject, length);
3175                                 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3176                         }
3177
3178                 }
3179         }
3180
3181         if (euidlen > 0) {
3182                 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3183         }
3184
3185         if (reflen > 0) {
3186                 CM_SetField(msg, eWeferences, references, reflen);
3187         }
3188
3189         if (preformatted_text != NULL) {
3190                 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3191         }
3192         else {
3193                 StrBuf *MsgBody;
3194                 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3195                 if (MsgBody != NULL) {
3196                         CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3197                 }
3198         }
3199
3200         return(msg);
3201 }
3202
3203
3204 /*
3205  * API function to delete messages which match a set of criteria
3206  * (returns the actual number of messages deleted)
3207  */
3208 int CtdlDeleteMessages(const char *room_name,   // which room
3209                        long *dmsgnums,          // array of msg numbers to be deleted
3210                        int num_dmsgnums,        // number of msgs to be deleted, or 0 for "any"
3211                        char *content_type       // or "" for any.  regular expressions expected.
3212 ) {
3213         struct ctdlroom qrbuf;
3214         struct cdbdata *cdbfr;
3215         long *msglist = NULL;
3216         long *dellist = NULL;
3217         int num_msgs = 0;
3218         int i, j;
3219         int num_deleted = 0;
3220         int delete_this;
3221         struct MetaData smi;
3222         regex_t re;
3223         regmatch_t pm;
3224         int need_to_free_re = 0;
3225
3226         if (content_type) if (!IsEmptyStr(content_type)) {
3227                         regcomp(&re, content_type, 0);
3228                         need_to_free_re = 1;
3229                 }
3230         syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3231
3232         /* get room record, obtaining a lock... */
3233         if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3234                 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3235                 if (need_to_free_re) regfree(&re);
3236                 return(0);      /* room not found */
3237         }
3238         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3239
3240         if (cdbfr != NULL) {
3241                 dellist = malloc(cdbfr->len);
3242                 msglist = (long *) cdbfr->ptr;
3243                 cdbfr->ptr = NULL;      // CtdlDeleteMessages() now owns this memory (this needs attention if we move to LMDB)
3244                 num_msgs = cdbfr->len / sizeof(long);
3245                 cdb_free(cdbfr);
3246         }
3247         if (num_msgs > 0) {
3248                 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3249                 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3250                 int have_more_del = 1;
3251
3252                 num_msgs = sort_msglist(msglist, num_msgs);
3253                 if (num_dmsgnums > 1) {
3254                         num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3255                 }
3256                 i = 0;
3257                 j = 0;
3258                 while ((i < num_msgs) && (have_more_del)) {
3259                         delete_this = 0x00;
3260
3261                         /* Set/clear a bit for each criterion */
3262
3263                         /* 0 messages in the list or a null list means that we are
3264                          * interested in deleting any messages which meet the other criteria.
3265                          */
3266                         if (have_delmsgs) {
3267                                 delete_this |= 0x01;
3268                         }
3269                         else {
3270                                 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3271
3272                                 if (i >= num_msgs)
3273                                         continue;
3274
3275                                 if (msglist[i] == dmsgnums[j]) {
3276                                         delete_this |= 0x01;
3277                                 }
3278                                 j++;
3279                                 have_more_del = (j < num_dmsgnums);
3280                         }
3281
3282                         if (have_contenttype) {
3283                                 GetMetaData(&smi, msglist[i]);
3284                                 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3285                                         delete_this |= 0x02;
3286                                 }
3287                         } else {
3288                                 delete_this |= 0x02;
3289                         }
3290
3291                         /* Delete message only if all bits are set */
3292                         if (delete_this == 0x03) {
3293                                 dellist[num_deleted++] = msglist[i];
3294                                 msglist[i] = 0L;
3295                         }
3296                         i++;
3297                 }
3298 /*
3299                 {
3300                         StrBuf *dbg = NewStrBuf();
3301                         for (i = 0; i < num_deleted; i++)
3302                                 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3303                         syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3304                         FreeStrBuf(&dbg);
3305                 }
3306 */
3307                 num_msgs = sort_msglist(msglist, num_msgs);
3308                 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3309                           msglist, (int)(num_msgs * sizeof(long)));
3310
3311                 if (num_msgs > 0)
3312                         qrbuf.QRhighest = msglist[num_msgs - 1];
3313                 else
3314                         qrbuf.QRhighest = 0;
3315         }
3316         CtdlPutRoomLock(&qrbuf);
3317
3318         /* Go through the messages we pulled out of the index, and decrement
3319          * their reference counts by 1.  If this is the only room the message
3320          * was in, the reference count will reach zero and the message will
3321          * automatically be deleted from the database.  We do this in a
3322          * separate pass because there might be plug-in hooks getting called,
3323          * and we don't want that happening during an S_ROOMS critical
3324          * section.
3325          */
3326         if (num_deleted) {
3327                 for (i=0; i<num_deleted; ++i) {
3328                         PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3329                 }
3330                 AdjRefCountList(dellist, num_deleted, -1);
3331         }
3332         /* Now free the memory we used, and go away. */
3333         if (msglist != NULL) free(msglist);
3334         if (dellist != NULL) free(dellist);
3335         syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3336         if (need_to_free_re) regfree(&re);
3337         return (num_deleted);
3338 }
3339
3340
3341 /*
3342  * GetMetaData()  -  Get the supplementary record for a message
3343  */
3344 void GetMetaData(struct MetaData *smibuf, long msgnum)
3345 {
3346         struct cdbdata *cdbsmi;
3347         long TheIndex;
3348
3349         memset(smibuf, 0, sizeof(struct MetaData));
3350         smibuf->meta_msgnum = msgnum;
3351         smibuf->meta_refcount = 1;      /* Default reference count is 1 */
3352
3353         /* Use the negative of the message number for its supp record index */
3354         TheIndex = (0L - msgnum);
3355
3356         cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3357         if (cdbsmi == NULL) {
3358                 return;                 /* record not found; leave it alone */
3359         }
3360         memcpy(smibuf, cdbsmi->ptr,
3361                ((cdbsmi->len > sizeof(struct MetaData)) ?
3362                 sizeof(struct MetaData) : cdbsmi->len)
3363         );
3364         cdb_free(cdbsmi);
3365         return;
3366 }
3367
3368
3369 /*
3370  * PutMetaData()  -  (re)write supplementary record for a message
3371  */
3372 void PutMetaData(struct MetaData *smibuf)
3373 {
3374         long TheIndex;
3375
3376         /* Use the negative of the message number for the metadata db index */
3377         TheIndex = (0L - smibuf->meta_msgnum);
3378
3379         cdb_store(CDB_MSGMAIN,
3380                   &TheIndex, (int)sizeof(long),
3381                   smibuf, (int)sizeof(struct MetaData)
3382         );
3383 }
3384
3385
3386 /*
3387  * Convenience function to process a big block of AdjRefCount() operations
3388  */
3389 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3390 {
3391         long i;
3392
3393         for (i = 0; i < nmsg; i++) {
3394                 AdjRefCount(msgnum[i], incr);
3395         }
3396 }
3397
3398
3399 /*
3400  * AdjRefCount - adjust the reference count for a message.  We need to delete from disk any message whose reference count reaches zero.
3401  */
3402 void AdjRefCount(long msgnum, int incr)
3403 {
3404         struct MetaData smi;
3405         long delnum;
3406
3407         /* This is a *tight* critical section; please keep it that way, as
3408          * it may get called while nested in other critical sections.  
3409          * Complicating this any further will surely cause deadlock!
3410          */
3411         begin_critical_section(S_SUPPMSGMAIN);
3412         GetMetaData(&smi, msgnum);
3413         smi.meta_refcount += incr;
3414         PutMetaData(&smi);
3415         end_critical_section(S_SUPPMSGMAIN);
3416         syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3417
3418         /* If the reference count is now zero, delete both the message and its metadata record.
3419          */
3420         if (smi.meta_refcount == 0) {
3421                 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3422                 
3423                 /* Call delete hooks with NULL room to show it has gone altogether */
3424                 PerformDeleteHooks(NULL, msgnum);
3425
3426                 /* Remove from message base */
3427                 delnum = msgnum;
3428                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3429                 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3430
3431                 /* Remove metadata record */
3432                 delnum = (0L - msgnum);
3433                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3434         }
3435 }
3436
3437
3438 /*
3439  * Write a generic object to this room
3440  *
3441  * Returns the message number of the written object, in case you need it.
3442  */
3443 long CtdlWriteObject(char *req_room,                    /* Room to stuff it in */
3444                      char *content_type,                /* MIME type of this object */
3445                      char *raw_message,                 /* Data to be written */
3446                      off_t raw_length,                  /* Size of raw_message */
3447                      struct ctdluser *is_mailbox,       /* Mailbox room? */
3448                      int is_binary,                     /* Is encoding necessary? */
3449                      unsigned int flags                 /* Internal save flags */
3450 ) {
3451         struct ctdlroom qrbuf;
3452         char roomname[ROOMNAMELEN];
3453         struct CtdlMessage *msg;
3454         StrBuf *encoded_message = NULL;
3455
3456         if (is_mailbox != NULL) {
3457                 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3458         }
3459         else {
3460                 safestrncpy(roomname, req_room, sizeof(roomname));
3461         }
3462
3463         syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3464
3465         if (is_binary) {
3466                 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3467         }
3468         else {
3469                 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3470         }
3471
3472         StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3473         StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3474         StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3475
3476         if (is_binary) {
3477                 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3478         }
3479         else {
3480                 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3481         }
3482
3483         if (is_binary) {
3484                 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3485         }
3486         else {
3487                 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3488         }
3489
3490         syslog(LOG_DEBUG, "msgbase: allocating");
3491         msg = malloc(sizeof(struct CtdlMessage));
3492         memset(msg, 0, sizeof(struct CtdlMessage));
3493         msg->cm_magic = CTDLMESSAGE_MAGIC;
3494         msg->cm_anon_type = MES_NORMAL;
3495         msg->cm_format_type = 4;
3496         CM_SetField(msg, eAuthor, CC->user.fullname, -1);
3497         CM_SetField(msg, eOriginalRoom, req_room, -1);
3498         msg->cm_flags = flags;
3499         
3500         CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3501
3502         /* Create the requested room if we have to. */
3503         if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3504                 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3505         }
3506
3507         /* Now write the data */
3508         long new_msgnum = CtdlSubmitMsg(msg, NULL, roomname);
3509         CM_Free(msg);
3510         return new_msgnum;
3511 }
3512
3513
3514 /************************************************************************/
3515 /*                      MODULE INITIALIZATION                           */
3516 /************************************************************************/
3517
3518 char *ctdl_module_init_msgbase(void) {
3519         if (!threading) {
3520                 FillMsgKeyLookupTable();
3521         }
3522
3523         /* return our module id for the log */
3524         return "msgbase";
3525 }