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