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