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