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