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