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