22b41b38fa4a23765563289885ab45858cafa316
[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);
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
1087 /*
1088  * Load a message from disk into memory.
1089  * This is used by CtdlOutputMsg() and other fetch functions.
1090  *
1091  * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1092  *       using the CtdlMessageFree() function.
1093  */
1094 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1095 {
1096         struct CitContext *CCC = CC;
1097         struct cdbdata *dmsgtext;
1098         struct CtdlMessage *ret = NULL;
1099         char *mptr;
1100         char *upper_bound;
1101         cit_uint8_t ch;
1102         cit_uint8_t field_header;
1103         eMsgField which;
1104
1105         MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1106         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1107         if (dmsgtext == NULL) {
1108                 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1109                 return NULL;
1110         }
1111         mptr = dmsgtext->ptr;
1112         upper_bound = mptr + dmsgtext->len;
1113
1114         /* Parse the three bytes that begin EVERY message on disk.
1115          * The first is always 0xFF, the on-disk magic number.
1116          * The second is the anonymous/public type byte.
1117          * The third is the format type byte (vari, fixed, or MIME).
1118          */
1119         ch = *mptr++;
1120         if (ch != 255) {
1121                 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1122                 cdb_free(dmsgtext);
1123                 return NULL;
1124         }
1125         ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1126         memset(ret, 0, sizeof(struct CtdlMessage));
1127
1128         ret->cm_magic = CTDLMESSAGE_MAGIC;
1129         ret->cm_anon_type = *mptr++;    /* Anon type byte */
1130         ret->cm_format_type = *mptr++;  /* Format type byte */
1131
1132
1133         if (dmsgtext->ptr[dmsgtext->len - 1] != '\0')
1134         {
1135                 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Forcefully terminating message!!\n", msgnum, with_body);
1136                 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1137         }
1138
1139         /*
1140          * The rest is zero or more arbitrary fields.  Load them in.
1141          * We're done when we encounter either a zero-length field or
1142          * have just processed the 'M' (message text) field.
1143          */
1144         do {
1145                 field_header = '\0';
1146                 long len;
1147
1148                 /* work around possibly buggy messages: */
1149                 while (field_header == '\0')
1150                 {
1151                         if (mptr >= upper_bound) {
1152                                 break;
1153                         }
1154                         field_header = *mptr++;
1155                 }
1156                 if (mptr >= upper_bound) {
1157                         break;
1158                 }
1159                 which = field_header;
1160                 len = strlen(mptr);
1161
1162                 CM_SetField(ret, which, mptr, len);
1163
1164                 mptr += len + 1;        /* advance to next field */
1165
1166         } while ((mptr < upper_bound) && (field_header != 'M'));
1167
1168         cdb_free(dmsgtext);
1169
1170         /* Always make sure there's something in the msg text field.  If
1171          * it's NULL, the message text is most likely stored separately,
1172          * so go ahead and fetch that.  Failing that, just set a dummy
1173          * body so other code doesn't barf.
1174          */
1175         if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1176                 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1177                 if (dmsgtext != NULL) {
1178                         CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1179                         cdb_free(dmsgtext);
1180                 }
1181         }
1182         if (CM_IsEmpty(ret, eMesageText)) {
1183                 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1184         }
1185
1186         /* Perform "before read" hooks (aborting if any return nonzero) */
1187         if (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0) {
1188                 CM_Free(ret);
1189                 return NULL;
1190         }
1191
1192         return (ret);
1193 }
1194
1195
1196
1197 /*
1198  * Pre callback function for multipart/alternative
1199  *
1200  * NOTE: this differs from the standard behavior for a reason.  Normally when
1201  *       displaying multipart/alternative you want to show the _last_ usable
1202  *       format in the message.  Here we show the _first_ one, because it's
1203  *       usually text/plain.  Since this set of functions is designed for text
1204  *       output to non-MIME-aware clients, this is the desired behavior.
1205  *
1206  */
1207 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1208                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1209                 char *cbid, void *cbuserdata)
1210 {
1211         struct CitContext *CCC = CC;
1212         struct ma_info *ma;
1213         
1214         ma = (struct ma_info *)cbuserdata;
1215         MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);        
1216         if (!strcasecmp(cbtype, "multipart/alternative")) {
1217                 ++ma->is_ma;
1218                 ma->did_print = 0;
1219         }
1220         if (!strcasecmp(cbtype, "message/rfc822")) {
1221                 ++ma->freeze;
1222         }
1223 }
1224
1225 /*
1226  * Post callback function for multipart/alternative
1227  */
1228 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1229                 void *content, char *cbtype, char *cbcharset, size_t length,
1230                 char *encoding, char *cbid, void *cbuserdata)
1231 {
1232         struct CitContext *CCC = CC;
1233         struct ma_info *ma;
1234         
1235         ma = (struct ma_info *)cbuserdata;
1236         MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);       
1237         if (!strcasecmp(cbtype, "multipart/alternative")) {
1238                 --ma->is_ma;
1239                 ma->did_print = 0;
1240         }
1241         if (!strcasecmp(cbtype, "message/rfc822")) {
1242                 --ma->freeze;
1243         }
1244 }
1245
1246 /*
1247  * Inline callback function for mime parser that wants to display text
1248  */
1249 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1250                 void *content, char *cbtype, char *cbcharset, size_t length,
1251                 char *encoding, char *cbid, void *cbuserdata)
1252 {
1253         struct CitContext *CCC = CC;
1254         char *ptr;
1255         char *wptr;
1256         size_t wlen;
1257         struct ma_info *ma;
1258
1259         ma = (struct ma_info *)cbuserdata;
1260
1261         MSG_syslog(LOG_DEBUG,
1262                 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1263                 partnum, filename, cbtype, (long)length);
1264
1265         /*
1266          * If we're in the middle of a multipart/alternative scope and
1267          * we've already printed another section, skip this one.
1268          */     
1269         if ( (ma->is_ma) && (ma->did_print) ) {
1270                 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1271                 return;
1272         }
1273         ma->did_print = 1;
1274
1275         if ( (!strcasecmp(cbtype, "text/plain")) 
1276            || (IsEmptyStr(cbtype)) ) {
1277                 wptr = content;
1278                 if (length > 0) {
1279                         client_write(wptr, length);
1280                         if (wptr[length-1] != '\n') {
1281                                 cprintf("\n");
1282                         }
1283                 }
1284                 return;
1285         }
1286
1287         if (!strcasecmp(cbtype, "text/html")) {
1288                 ptr = html_to_ascii(content, length, 80, 0);
1289                 wlen = strlen(ptr);
1290                 client_write(ptr, wlen);
1291                 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1292                         cprintf("\n");
1293                 }
1294                 free(ptr);
1295                 return;
1296         }
1297
1298         if (ma->use_fo_hooks) {
1299                 if (PerformFixedOutputHooks(cbtype, content, length)) {
1300                 /* above function returns nonzero if it handled the part */
1301                         return;
1302                 }
1303         }
1304
1305         if (strncasecmp(cbtype, "multipart/", 10)) {
1306                 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1307                         partnum, filename, cbtype, (long)length);
1308                 return;
1309         }
1310 }
1311
1312 /*
1313  * The client is elegant and sophisticated and wants to be choosy about
1314  * MIME content types, so figure out which multipart/alternative part
1315  * we're going to send.
1316  *
1317  * We use a system of weights.  When we find a part that matches one of the
1318  * MIME types we've declared as preferential, we can store it in ma->chosen_part
1319  * and then set ma->chosen_pref to that MIME type's position in our preference
1320  * list.  If we then hit another match, we only replace the first match if
1321  * the preference value is lower.
1322  */
1323 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1324                 void *content, char *cbtype, char *cbcharset, size_t length,
1325                 char *encoding, char *cbid, void *cbuserdata)
1326 {
1327         struct CitContext *CCC = CC;
1328         char buf[1024];
1329         int i;
1330         struct ma_info *ma;
1331         
1332         ma = (struct ma_info *)cbuserdata;
1333
1334         // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1335         //       http://bugzilla.citadel.org/show_bug.cgi?id=220
1336         // I don't know if there are any side effects!  Please TEST TEST TEST
1337         //if (ma->is_ma > 0) {
1338
1339         for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1340                 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1341                 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1342                         if (i < ma->chosen_pref) {
1343                                 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1344                                 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1345                                 ma->chosen_pref = i;
1346                         }
1347                 }
1348         }
1349 }
1350
1351 /*
1352  * Now that we've chosen our preferred part, output it.
1353  */
1354 void output_preferred(char *name, 
1355                       char *filename, 
1356                       char *partnum, 
1357                       char *disp,
1358                       void *content, 
1359                       char *cbtype, 
1360                       char *cbcharset, 
1361                       size_t length,
1362                       char *encoding, 
1363                       char *cbid, 
1364                       void *cbuserdata)
1365 {
1366         struct CitContext *CCC = CC;
1367         int i;
1368         char buf[128];
1369         int add_newline = 0;
1370         char *text_content;
1371         struct ma_info *ma;
1372         char *decoded = NULL;
1373         size_t bytes_decoded;
1374         int rc = 0;
1375
1376         ma = (struct ma_info *)cbuserdata;
1377
1378         /* This is not the MIME part you're looking for... */
1379         if (strcasecmp(partnum, ma->chosen_part)) return;
1380
1381         /* If the content-type of this part is in our preferred formats
1382          * list, we can simply output it verbatim.
1383          */
1384         for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1385                 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1386                 if (!strcasecmp(buf, cbtype)) {
1387                         /* Yeah!  Go!  W00t!! */
1388                         if (ma->dont_decode == 0) 
1389                                 rc = mime_decode_now (content, 
1390                                                       length,
1391                                                       encoding,
1392                                                       &decoded,
1393                                                       &bytes_decoded);
1394                         if (rc < 0)
1395                                 break; /* Give us the chance, maybe theres another one. */
1396
1397                         if (rc == 0) text_content = (char *)content;
1398                         else {
1399                                 text_content = decoded;
1400                                 length = bytes_decoded;
1401                         }
1402
1403                         if (text_content[length-1] != '\n') {
1404                                 ++add_newline;
1405                         }
1406                         cprintf("Content-type: %s", cbtype);
1407                         if (!IsEmptyStr(cbcharset)) {
1408                                 cprintf("; charset=%s", cbcharset);
1409                         }
1410                         cprintf("\nContent-length: %d\n",
1411                                 (int)(length + add_newline) );
1412                         if (!IsEmptyStr(encoding)) {
1413                                 cprintf("Content-transfer-encoding: %s\n", encoding);
1414                         }
1415                         else {
1416                                 cprintf("Content-transfer-encoding: 7bit\n");
1417                         }
1418                         cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1419                         cprintf("\n");
1420                         if (client_write(text_content, length) == -1)
1421                         {
1422                                 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1423                                 return;
1424                         }
1425                         if (add_newline) cprintf("\n");
1426                         if (decoded != NULL) free(decoded);
1427                         return;
1428                 }
1429         }
1430
1431         /* No translations required or possible: output as text/plain */
1432         cprintf("Content-type: text/plain\n\n");
1433         rc = 0;
1434         if (ma->dont_decode == 0)
1435                 rc = mime_decode_now (content, 
1436                                       length,
1437                                       encoding,
1438                                       &decoded,
1439                                       &bytes_decoded);
1440         if (rc < 0)
1441                 return; /* Give us the chance, maybe theres another one. */
1442         
1443         if (rc == 0) text_content = (char *)content;
1444         else {
1445                 text_content = decoded;
1446                 length = bytes_decoded;
1447         }
1448
1449         fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1450                         length, encoding, cbid, cbuserdata);
1451         if (decoded != NULL) free(decoded);
1452 }
1453
1454
1455 struct encapmsg {
1456         char desired_section[64];
1457         char *msg;
1458         size_t msglen;
1459 };
1460
1461
1462 /*
1463  * Callback function for
1464  */
1465 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1466                    void *content, char *cbtype, char *cbcharset, size_t length,
1467                    char *encoding, char *cbid, void *cbuserdata)
1468 {
1469         struct encapmsg *encap;
1470
1471         encap = (struct encapmsg *)cbuserdata;
1472
1473         /* Only proceed if this is the desired section... */
1474         if (!strcasecmp(encap->desired_section, partnum)) {
1475                 encap->msglen = length;
1476                 encap->msg = malloc(length + 2);
1477                 memcpy(encap->msg, content, length);
1478                 return;
1479         }
1480 }
1481
1482
1483 /*
1484  * Determine whether the specified message exists in the cached_msglist
1485  * (This is a security check)
1486  */
1487 int check_cached_msglist(long msgnum) {
1488         struct CitContext *CCC = CC;
1489
1490         /* cases in which we skip the check */
1491         if (!CCC) return om_ok;                                         /* not a session */
1492         if (CCC->client_socket <= 0) return om_ok;                      /* not a client session */
1493         if (CCC->cached_msglist == NULL) return om_access_denied;       /* no msglist fetched */
1494         if (CCC->cached_num_msgs == 0) return om_access_denied;         /* nothing to check */
1495
1496
1497         /* Do a binary search within the cached_msglist for the requested msgnum */
1498         int min = 0;
1499         int max = (CC->cached_num_msgs - 1);
1500
1501         while (max >= min) {
1502                 int middle = min + (max-min) / 2 ;
1503                 if (msgnum == CCC->cached_msglist[middle]) {
1504                         return om_ok;
1505                 }
1506                 if (msgnum > CC->cached_msglist[middle]) {
1507                         min = middle + 1;
1508                 }
1509                 else {
1510                         max = middle - 1;
1511                 }
1512         }
1513
1514         return om_access_denied;
1515 }
1516
1517
1518
1519 /*
1520  * Get a message off disk.  (returns om_* values found in msgbase.h)
1521  * 
1522  */
1523 int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
1524                   int mode,             /* how would you like that message? */
1525                   int headers_only,     /* eschew the message body? */
1526                   int do_proto,         /* do Citadel protocol responses? */
1527                   int crlf,             /* Use CRLF newlines instead of LF? */
1528                   char *section,        /* NULL or a message/rfc822 section */
1529                   int flags,            /* various flags; see msgbase.h */
1530                   char **Author,
1531                   char **Address
1532 ) {
1533         struct CitContext *CCC = CC;
1534         struct CtdlMessage *TheMessage = NULL;
1535         int retcode = CIT_OK;
1536         struct encapmsg encap;
1537         int r;
1538
1539         MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n", 
1540                 msg_num, mode,
1541                 (section ? section : "<>")
1542         );
1543
1544         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1545         if (r != om_ok) {
1546                 if (do_proto) {
1547                         if (r == om_not_logged_in) {
1548                                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1549                         }
1550                         else {
1551                                 cprintf("%d An unknown error has occurred.\n", ERROR);
1552                         }
1553                 }
1554                 return(r);
1555         }
1556
1557         /*
1558          * Check to make sure the message is actually IN this room
1559          */
1560         r = check_cached_msglist(msg_num);
1561         if (r == om_access_denied) {
1562                 /* Not in the cache?  We get ONE shot to check it again. */
1563                 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1564                 r = check_cached_msglist(msg_num);
1565         }
1566         if (r != om_ok) {
1567                 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1568                            msg_num, CCC->room.QRname
1569                 );
1570                 if (do_proto) {
1571                         if (r == om_access_denied) {
1572                                 cprintf("%d message %ld was not found in this room\n",
1573                                         ERROR + HIGHER_ACCESS_REQUIRED,
1574                                         msg_num
1575                                 );
1576                         }
1577                 }
1578                 return(r);
1579         }
1580
1581         /*
1582          * Fetch the message from disk.  If we're in HEADERS_FAST mode,
1583          * request that we don't even bother loading the body into memory.
1584          */
1585         if (headers_only == HEADERS_FAST) {
1586                 TheMessage = CtdlFetchMessage(msg_num, 0);
1587         }
1588         else {
1589                 TheMessage = CtdlFetchMessage(msg_num, 1);
1590         }
1591
1592         if (TheMessage == NULL) {
1593                 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1594                         ERROR + MESSAGE_NOT_FOUND, msg_num);
1595                 return(om_no_such_msg);
1596         }
1597
1598         /* Here is the weird form of this command, to process only an
1599          * encapsulated message/rfc822 section.
1600          */
1601         if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1602                 memset(&encap, 0, sizeof encap);
1603                 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1604                 mime_parser(CM_RANGE(TheMessage, eMesageText),
1605                             *extract_encapsulated_message,
1606                             NULL, NULL, (void *)&encap, 0
1607                         );
1608
1609                 if ((Author != NULL) && (*Author == NULL))
1610                 {
1611                         long len;
1612                         CM_GetAsField(TheMessage, eAuthor, Author, &len);
1613                 }
1614                 if ((Address != NULL) && (*Address == NULL))
1615                 {       
1616                         long len;
1617                         CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1618                 }
1619                 CM_Free(TheMessage);
1620                 TheMessage = NULL;
1621
1622                 if (encap.msg) {
1623                         encap.msg[encap.msglen] = 0;
1624                         TheMessage = convert_internet_message(encap.msg);
1625                         encap.msg = NULL;       /* no free() here, TheMessage owns it now */
1626
1627                         /* Now we let it fall through to the bottom of this
1628                          * function, because TheMessage now contains the
1629                          * encapsulated message instead of the top-level
1630                          * message.  Isn't that neat?
1631                          */
1632                 }
1633                 else {
1634                         if (do_proto) {
1635                                 cprintf("%d msg %ld has no part %s\n",
1636                                         ERROR + MESSAGE_NOT_FOUND,
1637                                         msg_num,
1638                                         section);
1639                         }
1640                         retcode = om_no_such_msg;
1641                 }
1642
1643         }
1644
1645         /* Ok, output the message now */
1646         if (retcode == CIT_OK)
1647                 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1648         if ((Author != NULL) && (*Author == NULL))
1649         {
1650                 long len;
1651                 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1652         }
1653         if ((Address != NULL) && (*Address == NULL))
1654         {       
1655                 long len;
1656                 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1657         }
1658
1659         CM_Free(TheMessage);
1660
1661         return(retcode);
1662 }
1663
1664
1665
1666 void OutputCtdlMsgHeaders(
1667         struct CtdlMessage *TheMessage,
1668         int do_proto)           /* do Citadel protocol responses? */
1669 {
1670         int i;
1671         int suppress_f = 0;
1672         char buf[SIZ];
1673         char display_name[256];
1674
1675         /* begin header processing loop for Citadel message format */
1676         safestrncpy(display_name, "<unknown>", sizeof display_name);
1677         if (!CM_IsEmpty(TheMessage, eAuthor)) {
1678                 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1679                 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1680                         safestrncpy(display_name, "****", sizeof display_name);
1681                 }
1682                 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1683                         safestrncpy(display_name, "anonymous", sizeof display_name);
1684                 }
1685                 else {
1686                         safestrncpy(display_name, buf, sizeof display_name);
1687                 }
1688                 if ((is_room_aide())
1689                     && ((TheMessage->cm_anon_type == MES_ANONONLY)
1690                         || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1691                         size_t tmp = strlen(display_name);
1692                         snprintf(&display_name[tmp],
1693                                  sizeof display_name - tmp,
1694                                  " [%s]", buf);
1695                 }
1696         }
1697
1698         /* Don't show Internet address for users on the
1699          * local Citadel network.
1700          */
1701         suppress_f = 0;
1702         if (!CM_IsEmpty(TheMessage, eNodeName) &&
1703             (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1704         {
1705                 suppress_f = 1;
1706         }
1707
1708         /* Now spew the header fields in the order we like them. */
1709         for (i=0; i< NDiskFields; ++i) {
1710                 eMsgField Field;
1711                 Field = FieldOrder[i];
1712                 if (Field != eMesageText) {
1713                         if ( (!CM_IsEmpty(TheMessage, Field))
1714                              && (msgkeys[Field] != NULL) ) {
1715                                 if ((Field == eenVelopeTo) ||
1716                                     (Field == eRecipient) ||
1717                                     (Field == eCarbonCopY)) {
1718                                         sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1719                                 }
1720                                 if (Field == eAuthor) {
1721                                         if (do_proto) cprintf("%s=%s\n",
1722                                                               msgkeys[Field],
1723                                                               display_name);
1724                                 }
1725                                 else if ((Field == erFc822Addr) && (suppress_f)) {
1726                                         /* do nothing */
1727                                 }
1728                                 /* Masquerade display name if needed */
1729                                 else {
1730                                         if (do_proto) cprintf("%s=%s\n",
1731                                                               msgkeys[Field],
1732                                                               TheMessage->cm_fields[Field]
1733                                                 );
1734                                 }
1735                         }
1736                 }
1737         }
1738
1739 }
1740
1741 void OutputRFC822MsgHeaders(
1742         struct CtdlMessage *TheMessage,
1743         int flags,              /* should the bessage be exported clean */
1744         const char *nl,
1745         char *mid, long sizeof_mid,
1746         char *suser, long sizeof_suser,
1747         char *luser, long sizeof_luser,
1748         char *fuser, long sizeof_fuser,
1749         char *snode, long sizeof_snode)
1750 {
1751         char datestamp[100];
1752         int subject_found = 0;
1753         char buf[SIZ];
1754         int i, j, k;
1755         char *mptr = NULL;
1756         char *mpptr = NULL;
1757         char *hptr;
1758
1759         for (i = 0; i < NDiskFields; ++i) {
1760                 if (TheMessage->cm_fields[FieldOrder[i]]) {
1761                         mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1762                         switch (FieldOrder[i]) {
1763                         case eAuthor:
1764                                 safestrncpy(luser, mptr, sizeof_luser);
1765                                 safestrncpy(suser, mptr, sizeof_suser);
1766                                 break;
1767                         case eCarbonCopY:
1768                                 if ((flags & QP_EADDR) != 0) {
1769                                         mptr = qp_encode_email_addrs(mptr);
1770                                 }
1771                                 sanitize_truncated_recipient(mptr);
1772                                 cprintf("CC: %s%s", mptr, nl);
1773                                 break;
1774                         case eMessagePath:
1775                                 cprintf("Return-Path: %s%s", mptr, nl);
1776                                 break;
1777                         case eListID:
1778                                 cprintf("List-ID: %s%s", mptr, nl);
1779                                 break;
1780                         case eenVelopeTo:
1781                                 if ((flags & QP_EADDR) != 0) 
1782                                         mptr = qp_encode_email_addrs(mptr);
1783                                 hptr = mptr;
1784                                 while ((*hptr != '\0') && isspace(*hptr))
1785                                         hptr ++;
1786                                 if (!IsEmptyStr(hptr))
1787                                         cprintf("Envelope-To: %s%s", hptr, nl);
1788                                 break;
1789                         case eMsgSubject:
1790                                 cprintf("Subject: %s%s", mptr, nl);
1791                                 subject_found = 1;
1792                                 break;
1793                         case emessageId:
1794                                 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1795                                 break;
1796                         case erFc822Addr:
1797                                 safestrncpy(fuser, mptr, sizeof_fuser);
1798                         /* case eOriginalRoom:
1799                            cprintf("X-Citadel-Room: %s%s",
1800                            mptr, nl)
1801                            break;
1802                            ; */
1803                         case eNodeName:
1804                                 safestrncpy(snode, mptr, sizeof_snode);
1805                                 break;
1806                         case eRecipient:
1807                                 if (haschar(mptr, '@') == 0)
1808                                 {
1809                                         sanitize_truncated_recipient(mptr);
1810                                         cprintf("To: %s@%s", mptr, config.c_fqdn);
1811                                         cprintf("%s", nl);
1812                                 }
1813                                 else
1814                                 {
1815                                         if ((flags & QP_EADDR) != 0) {
1816                                                 mptr = qp_encode_email_addrs(mptr);
1817                                         }
1818                                         sanitize_truncated_recipient(mptr);
1819                                         cprintf("To: %s", mptr);
1820                                         cprintf("%s", nl);
1821                                 }
1822                                 break;
1823                         case eTimestamp:
1824                                 datestring(datestamp, sizeof datestamp,
1825                                            atol(mptr), DATESTRING_RFC822);
1826                                 cprintf("Date: %s%s", datestamp, nl);
1827                                 break;
1828                         case eWeferences:
1829                                 cprintf("References: ");
1830                                 k = num_tokens(mptr, '|');
1831                                 for (j=0; j<k; ++j) {
1832                                         extract_token(buf, mptr, j, '|', sizeof buf);
1833                                         cprintf("<%s>", buf);
1834                                         if (j == (k-1)) {
1835                                                 cprintf("%s", nl);
1836                                         }
1837                                         else {
1838                                                 cprintf(" ");
1839                                         }
1840                                 }
1841                                 break;
1842                         case eReplyTo:
1843                                 hptr = mptr;
1844                                 while ((*hptr != '\0') && isspace(*hptr))
1845                                         hptr ++;
1846                                 if (!IsEmptyStr(hptr))
1847                                         cprintf("Reply-To: %s%s", mptr, nl);
1848                                 break;
1849
1850                         case eRemoteRoom:
1851                         case eDestination:
1852                         case eExclusiveID:
1853                         case eHumanNode:
1854                         case eJournal:
1855                         case eMesageText:
1856                         case eBig_message:
1857                         case eOriginalRoom:
1858                         case eSpecialField:
1859                         case eErrorMsg:
1860                         case eSuppressIdx:
1861                         case eExtnotify:
1862                         case eVltMsgNum:
1863                                 /* these don't map to mime message headers. */
1864                                 break;
1865
1866                         }
1867                         if (mptr != mpptr)
1868                                 free (mptr);
1869                 }
1870         }
1871         if (subject_found == 0) {
1872                 cprintf("Subject: (no subject)%s", nl);
1873         }
1874 }
1875
1876
1877 void Dump_RFC822HeadersBody(
1878         struct CtdlMessage *TheMessage,
1879         int headers_only,       /* eschew the message body? */
1880         int flags,              /* should the bessage be exported clean? */
1881
1882         const char *nl)
1883 {
1884         cit_uint8_t prev_ch;
1885         int eoh = 0;
1886         const char *StartOfText = StrBufNOTNULL;
1887         char outbuf[1024];
1888         int outlen = 0;
1889         int nllen = strlen(nl);
1890         char *mptr;
1891
1892         mptr = TheMessage->cm_fields[eMesageText];
1893
1894
1895         prev_ch = '\0';
1896         while (*mptr != '\0') {
1897                 if (*mptr == '\r') {
1898                         /* do nothing */
1899                 }
1900                 else {
1901                         if ((!eoh) &&
1902                             (*mptr == '\n'))
1903                         {
1904                                 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1905                                 if (!eoh)
1906                                         eoh = *(mptr+1) == '\n';
1907                                 if (eoh)
1908                                 {
1909                                         StartOfText = mptr;
1910                                         StartOfText = strchr(StartOfText, '\n');
1911                                         StartOfText = strchr(StartOfText, '\n');
1912                                 }
1913                         }
1914                         if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1915                             ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1916                             ((headers_only != HEADERS_NONE) && 
1917                              (headers_only != HEADERS_ONLY))
1918                                 ) {
1919                                 if (*mptr == '\n') {
1920                                         memcpy(&outbuf[outlen], nl, nllen);
1921                                         outlen += nllen;
1922                                         outbuf[outlen] = '\0';
1923                                 }
1924                                 else {
1925                                         outbuf[outlen++] = *mptr;
1926                                 }
1927                         }
1928                 }
1929                 if (flags & ESC_DOT)
1930                 {
1931                         if ((prev_ch == '\n') && 
1932                             (*mptr == '.') && 
1933                             ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1934                         {
1935                                 outbuf[outlen++] = '.';
1936                         }
1937                         prev_ch = *mptr;
1938                 }
1939                 ++mptr;
1940                 if (outlen > 1000) {
1941                         if (client_write(outbuf, outlen) == -1)
1942                         {
1943                                 struct CitContext *CCC = CC;
1944                                 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
1945                                 return;
1946                         }
1947                         outlen = 0;
1948                 }
1949         }
1950         if (outlen > 0) {
1951                 client_write(outbuf, outlen);
1952         }
1953 }
1954
1955
1956
1957 /* If the format type on disk is 1 (fixed-format), then we want
1958  * everything to be output completely literally ... regardless of
1959  * what message transfer format is in use.
1960  */
1961 void DumpFormatFixed(
1962         struct CtdlMessage *TheMessage,
1963         int mode,               /* how would you like that message? */
1964         const char *nl)
1965 {
1966         cit_uint8_t ch;
1967         char buf[SIZ];
1968         int buflen;
1969         int xlline = 0;
1970         int nllen = strlen (nl);
1971         char *mptr;
1972
1973         mptr = TheMessage->cm_fields[eMesageText];
1974         
1975         if (mode == MT_MIME) {
1976                 cprintf("Content-type: text/plain\n\n");
1977         }
1978         *buf = '\0';
1979         buflen = 0;
1980         while (ch = *mptr++, ch > 0) {
1981                 if (ch == '\n')
1982                         ch = '\r';
1983
1984                 if ((buflen > 250) && (!xlline)){
1985                         int tbuflen;
1986                         tbuflen = buflen;
1987
1988                         while ((buflen > 0) && 
1989                                (!isspace(buf[buflen])))
1990                                 buflen --;
1991                         if (buflen == 0) {
1992                                 xlline = 1;
1993                         }
1994                         else {
1995                                 mptr -= tbuflen - buflen;
1996                                 buf[buflen] = '\0';
1997                                 ch = '\r';
1998                         }
1999                 }
2000                 /* if we reach the outer bounds of our buffer, 
2001                    abort without respect what whe purge. */
2002                 if (xlline && 
2003                     ((isspace(ch)) || 
2004                      (buflen > SIZ - nllen - 2)))
2005                         ch = '\r';
2006
2007                 if (ch == '\r') {
2008                         memcpy (&buf[buflen], nl, nllen);
2009                         buflen += nllen;
2010                         buf[buflen] = '\0';
2011
2012                         if (client_write(buf, buflen) == -1)
2013                         {
2014                                 struct CitContext *CCC = CC;
2015                                 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
2016                                 return;
2017                         }
2018                         *buf = '\0';
2019                         buflen = 0;
2020                         xlline = 0;
2021                 } else {
2022                         buf[buflen] = ch;
2023                         buflen++;
2024                 }
2025         }
2026         buf[buflen] = '\0';
2027         if (!IsEmptyStr(buf))
2028                 cprintf("%s%s", buf, nl);
2029 }
2030
2031 /*
2032  * Get a message off disk.  (returns om_* values found in msgbase.h)
2033  */
2034 int CtdlOutputPreLoadedMsg(
2035                 struct CtdlMessage *TheMessage,
2036                 int mode,               /* how would you like that message? */
2037                 int headers_only,       /* eschew the message body? */
2038                 int do_proto,           /* do Citadel protocol responses? */
2039                 int crlf,               /* Use CRLF newlines instead of LF? */
2040                 int flags               /* should the bessage be exported clean? */
2041 ) {
2042         struct CitContext *CCC = CC;
2043         int i;
2044         const char *nl; /* newline string */
2045         struct ma_info ma;
2046
2047         /* Buffers needed for RFC822 translation.  These are all filled
2048          * using functions that are bounds-checked, and therefore we can
2049          * make them substantially smaller than SIZ.
2050          */
2051         char suser[100];
2052         char luser[100];
2053         char fuser[100];
2054         char snode[100];
2055         char mid[100];
2056
2057         MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2058                    ((TheMessage == NULL) ? "NULL" : "not null"),
2059                    mode, headers_only, do_proto, crlf);
2060
2061         strcpy(mid, "unknown");
2062         nl = (crlf ? "\r\n" : "\n");
2063
2064         if (!CM_IsValidMsg(TheMessage)) {
2065                 MSGM_syslog(LOG_ERR,
2066                             "ERROR: invalid preloaded message for output\n");
2067                 cit_backtrace ();
2068                 return(om_no_such_msg);
2069         }
2070
2071         /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2072          * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2073          */
2074         if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2075                 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2076         }
2077                 
2078         /* Are we downloading a MIME component? */
2079         if (mode == MT_DOWNLOAD) {
2080                 if (TheMessage->cm_format_type != FMT_RFC822) {
2081                         if (do_proto)
2082                                 cprintf("%d This is not a MIME message.\n",
2083                                 ERROR + ILLEGAL_VALUE);
2084                 } else if (CCC->download_fp != NULL) {
2085                         if (do_proto) cprintf(
2086                                 "%d You already have a download open.\n",
2087                                 ERROR + RESOURCE_BUSY);
2088                 } else {
2089                         /* Parse the message text component */
2090                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2091                                     *mime_download, NULL, NULL, NULL, 0);
2092                         /* If there's no file open by this time, the requested
2093                          * section wasn't found, so print an error
2094                          */
2095                         if (CCC->download_fp == NULL) {
2096                                 if (do_proto) cprintf(
2097                                         "%d Section %s not found.\n",
2098                                         ERROR + FILE_NOT_FOUND,
2099                                         CCC->download_desired_section);
2100                         }
2101                 }
2102                 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2103         }
2104
2105         /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2106          * in a single server operation instead of opening a download file.
2107          */
2108         if (mode == MT_SPEW_SECTION) {
2109                 if (TheMessage->cm_format_type != FMT_RFC822) {
2110                         if (do_proto)
2111                                 cprintf("%d This is not a MIME message.\n",
2112                                 ERROR + ILLEGAL_VALUE);
2113                 } else {
2114                         /* Parse the message text component */
2115                         int found_it = 0;
2116
2117                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2118                                     *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2119                         /* If section wasn't found, print an error
2120                          */
2121                         if (!found_it) {
2122                                 if (do_proto) cprintf(
2123                                         "%d Section %s not found.\n",
2124                                         ERROR + FILE_NOT_FOUND,
2125                                         CCC->download_desired_section);
2126                         }
2127                 }
2128                 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2129         }
2130
2131         /* now for the user-mode message reading loops */
2132         if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2133
2134         /* Does the caller want to skip the headers? */
2135         if (headers_only == HEADERS_NONE) goto START_TEXT;
2136
2137         /* Tell the client which format type we're using. */
2138         if ( (mode == MT_CITADEL) && (do_proto) ) {
2139                 cprintf("type=%d\n", TheMessage->cm_format_type);
2140         }
2141
2142         /* nhdr=yes means that we're only displaying headers, no body */
2143         if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2144            && ((mode == MT_CITADEL) || (mode == MT_MIME))
2145            && (do_proto)
2146            ) {
2147                 cprintf("nhdr=yes\n");
2148         }
2149
2150         if ((mode == MT_CITADEL) || (mode == MT_MIME)) 
2151                 OutputCtdlMsgHeaders(TheMessage, do_proto);
2152
2153
2154         /* begin header processing loop for RFC822 transfer format */
2155         strcpy(suser, "");
2156         strcpy(luser, "");
2157         strcpy(fuser, "");
2158         memcpy(snode, CFG_KEY(c_nodename) + 1);
2159         if (mode == MT_RFC822) 
2160                 OutputRFC822MsgHeaders(
2161                         TheMessage,
2162                         flags,
2163                         nl,
2164                         mid, sizeof(mid),
2165                         suser, sizeof(suser),
2166                         luser, sizeof(luser),
2167                         fuser, sizeof(fuser),
2168                         snode, sizeof(snode)
2169                         );
2170
2171
2172         for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2173                 suser[i] = tolower(suser[i]);
2174                 if (!isalnum(suser[i])) suser[i]='_';
2175         }
2176
2177         if (mode == MT_RFC822) {
2178                 if (!strcasecmp(snode, NODENAME)) {
2179                         safestrncpy(snode, FQDN, sizeof snode);
2180                 }
2181
2182                 /* Construct a fun message id */
2183                 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2184                 if (strchr(mid, '@')==NULL) {
2185                         cprintf("@%s", snode);
2186                 }
2187                 cprintf(">%s", nl);
2188
2189                 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2190                         cprintf("From: \"----\" <x@x.org>%s", nl);
2191                 }
2192                 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2193                         cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2194                 }
2195                 else if (!IsEmptyStr(fuser)) {
2196                         cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2197                 }
2198                 else {
2199                         cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2200                 }
2201
2202                 /* Blank line signifying RFC822 end-of-headers */
2203                 if (TheMessage->cm_format_type != FMT_RFC822) {
2204                         cprintf("%s", nl);
2205                 }
2206         }
2207
2208         /* end header processing loop ... at this point, we're in the text */
2209 START_TEXT:
2210         if (headers_only == HEADERS_FAST) goto DONE;
2211
2212         /* Tell the client about the MIME parts in this message */
2213         if (TheMessage->cm_format_type == FMT_RFC822) {
2214                 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2215                         memset(&ma, 0, sizeof(struct ma_info));
2216                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2217                                 (do_proto ? *list_this_part : NULL),
2218                                 (do_proto ? *list_this_pref : NULL),
2219                                 (do_proto ? *list_this_suff : NULL),
2220                                 (void *)&ma, 1);
2221                 }
2222                 else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
2223                         Dump_RFC822HeadersBody(
2224                                 TheMessage,
2225                                 headers_only,
2226                                 flags,
2227                                 nl);
2228                         goto DONE;
2229                 }
2230         }
2231
2232         if (headers_only == HEADERS_ONLY) {
2233                 goto DONE;
2234         }
2235
2236         /* signify start of msg text */
2237         if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2238                 if (do_proto) cprintf("text\n");
2239         }
2240
2241         if (TheMessage->cm_format_type == FMT_FIXED) 
2242                 DumpFormatFixed(
2243                         TheMessage,
2244                         mode,           /* how would you like that message? */
2245                         nl);
2246
2247         /* If the message on disk is format 0 (Citadel vari-format), we
2248          * output using the formatter at 80 columns.  This is the final output
2249          * form if the transfer format is RFC822, but if the transfer format
2250          * is Citadel proprietary, it'll still work, because the indentation
2251          * for new paragraphs is correct and the client will reformat the
2252          * message to the reader's screen width.
2253          */
2254         if (TheMessage->cm_format_type == FMT_CITADEL) {
2255                 if (mode == MT_MIME) {
2256                         cprintf("Content-type: text/x-citadel-variformat\n\n");
2257                 }
2258                 memfmout(TheMessage->cm_fields[eMesageText], nl);
2259         }
2260
2261         /* If the message on disk is format 4 (MIME), we've gotta hand it
2262          * off to the MIME parser.  The client has already been told that
2263          * this message is format 1 (fixed format), so the callback function
2264          * we use will display those parts as-is.
2265          */
2266         if (TheMessage->cm_format_type == FMT_RFC822) {
2267                 memset(&ma, 0, sizeof(struct ma_info));
2268
2269                 if (mode == MT_MIME) {
2270                         ma.use_fo_hooks = 0;
2271                         strcpy(ma.chosen_part, "1");
2272                         ma.chosen_pref = 9999;
2273                         ma.dont_decode = CCC->msg4_dont_decode;
2274                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2275                                     *choose_preferred, *fixed_output_pre,
2276                                     *fixed_output_post, (void *)&ma, 1);
2277                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2278                                     *output_preferred, NULL, NULL, (void *)&ma, 1);
2279                 }
2280                 else {
2281                         ma.use_fo_hooks = 1;
2282                         mime_parser(CM_RANGE(TheMessage, eMesageText),
2283                                     *fixed_output, *fixed_output_pre,
2284                                     *fixed_output_post, (void *)&ma, 0);
2285                 }
2286
2287         }
2288
2289 DONE:   /* now we're done */
2290         if (do_proto) cprintf("000\n");
2291         return(om_ok);
2292 }
2293
2294 /*
2295  * Save one or more message pointers into a specified room
2296  * (Returns 0 for success, nonzero for failure)
2297  * roomname may be NULL to use the current room
2298  *
2299  * Note that the 'supplied_msg' field may be set to NULL, in which case
2300  * the message will be fetched from disk, by number, if we need to perform
2301  * replication checks.  This adds an additional database read, so if the
2302  * caller already has the message in memory then it should be supplied.  (Obviously
2303  * this mode of operation only works if we're saving a single message.)
2304  */
2305 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2306                         int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2307 ) {
2308         struct CitContext *CCC = CC;
2309         int i, j, unique;
2310         char hold_rm[ROOMNAMELEN];
2311         struct cdbdata *cdbfr;
2312         int num_msgs;
2313         long *msglist;
2314         long highest_msg = 0L;
2315
2316         long msgid = 0;
2317         struct CtdlMessage *msg = NULL;
2318
2319         long *msgs_to_be_merged = NULL;
2320         int num_msgs_to_be_merged = 0;
2321
2322         MSG_syslog(LOG_DEBUG,
2323                    "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2324                    roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2325         );
2326
2327         strcpy(hold_rm, CCC->room.QRname);
2328
2329         /* Sanity checks */
2330         if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2331         if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2332         if (num_newmsgs > 1) supplied_msg = NULL;
2333
2334         /* Now the regular stuff */
2335         if (CtdlGetRoomLock(&CCC->room,
2336            ((roomname != NULL) ? roomname : CCC->room.QRname) )
2337            != 0) {
2338                 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2339                 return(ERROR + ROOM_NOT_FOUND);
2340         }
2341
2342
2343         msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2344         num_msgs_to_be_merged = 0;
2345
2346
2347         cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2348         if (cdbfr == NULL) {
2349                 msglist = NULL;
2350                 num_msgs = 0;
2351         } else {
2352                 msglist = (long *) cdbfr->ptr;
2353                 cdbfr->ptr = NULL;      /* CtdlSaveMsgPointerInRoom() now owns this memory */
2354                 num_msgs = cdbfr->len / sizeof(long);
2355                 cdb_free(cdbfr);
2356         }
2357
2358
2359         /* Create a list of msgid's which were supplied by the caller, but do
2360          * not already exist in the target room.  It is absolutely taboo to
2361          * have more than one reference to the same message in a room.
2362          */
2363         for (i=0; i<num_newmsgs; ++i) {
2364                 unique = 1;
2365                 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2366                         if (msglist[j] == newmsgidlist[i]) {
2367                                 unique = 0;
2368                         }
2369                 }
2370                 if (unique) {
2371                         msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2372                 }
2373         }
2374
2375         MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2376
2377         /*
2378          * Now merge the new messages
2379          */
2380         msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2381         if (msglist == NULL) {
2382                 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2383                 free(msgs_to_be_merged);
2384                 return (ERROR + INTERNAL_ERROR);
2385         }
2386         memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2387         num_msgs += num_msgs_to_be_merged;
2388
2389         /* Sort the message list, so all the msgid's are in order */
2390         num_msgs = sort_msglist(msglist, num_msgs);
2391
2392         /* Determine the highest message number */
2393         highest_msg = msglist[num_msgs - 1];
2394
2395         /* Write it back to disk. */
2396         cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2397                   msglist, (int)(num_msgs * sizeof(long)));
2398
2399         /* Free up the memory we used. */
2400         free(msglist);
2401
2402         /* Update the highest-message pointer and unlock the room. */
2403         CCC->room.QRhighest = highest_msg;
2404         CtdlPutRoomLock(&CCC->room);
2405
2406         /* Perform replication checks if necessary */
2407         if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2408                 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2409
2410                 for (i=0; i<num_msgs_to_be_merged; ++i) {
2411                         msgid = msgs_to_be_merged[i];
2412         
2413                         if (supplied_msg != NULL) {
2414                                 msg = supplied_msg;
2415                         }
2416                         else {
2417                                 msg = CtdlFetchMessage(msgid, 0);
2418                         }
2419         
2420                         if (msg != NULL) {
2421                                 ReplicationChecks(msg);
2422                 
2423                                 /* If the message has an Exclusive ID, index that... */
2424                                 if (!CM_IsEmpty(msg, eExclusiveID)) {
2425                                         index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2426                                 }
2427
2428                                 /* Free up the memory we may have allocated */
2429                                 if (msg != supplied_msg) {
2430                                         CM_Free(msg);
2431                                 }
2432                         }
2433         
2434                 }
2435         }
2436
2437         else {
2438                 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2439         }
2440
2441         /* Submit this room for processing by hooks */
2442         PerformRoomHooks(&CCC->room);
2443
2444         /* Go back to the room we were in before we wandered here... */
2445         CtdlGetRoom(&CCC->room, hold_rm);
2446
2447         /* Bump the reference count for all messages which were merged */
2448         if (!suppress_refcount_adj) {
2449                 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2450         }
2451
2452         /* Free up memory... */
2453         if (msgs_to_be_merged != NULL) {
2454                 free(msgs_to_be_merged);
2455         }
2456
2457         /* Return success. */
2458         return (0);
2459 }
2460
2461
2462 /*
2463  * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2464  * a single message.
2465  */
2466 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2467                              int do_repl_check, struct CtdlMessage *supplied_msg)
2468 {
2469         return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2470 }
2471
2472
2473
2474
2475 /*
2476  * Message base operation to save a new message to the message store
2477  * (returns new message number)
2478  *
2479  * This is the back end for CtdlSubmitMsg() and should not be directly
2480  * called by server-side modules.
2481  *
2482  */
2483 long send_message(struct CtdlMessage *msg) {
2484         struct CitContext *CCC = CC;
2485         long newmsgid;
2486         long retval;
2487         char msgidbuf[256];
2488         long msgidbuflen;
2489         struct ser_ret smr;
2490         int is_bigmsg = 0;
2491         char *holdM = NULL;
2492         long holdMLen = 0;
2493
2494         /* Get a new message number */
2495         newmsgid = get_new_message_number();
2496         msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2497                                (long unsigned int) time(NULL),
2498                                (long unsigned int) newmsgid,
2499                                config.c_fqdn
2500                 );
2501
2502         /* Generate an ID if we don't have one already */
2503         if (CM_IsEmpty(msg, emessageId)) {
2504                 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2505         }
2506
2507         /* If the message is big, set its body aside for storage elsewhere */
2508         if (!CM_IsEmpty(msg, eMesageText)) {
2509                 if (msg->cm_lengths[eMesageText] > BIGMSG) {
2510                         is_bigmsg = 1;
2511                         holdM = msg->cm_fields[eMesageText];
2512                         msg->cm_fields[eMesageText] = NULL;
2513                         holdMLen = msg->cm_lengths[eMesageText];
2514                         msg->cm_lengths[eMesageText] = 0;
2515                 }
2516         }
2517
2518         /* Serialize our data structure for storage in the database */  
2519         CtdlSerializeMessage(&smr, msg);
2520
2521         if (is_bigmsg) {
2522                 msg->cm_fields[eMesageText] = holdM;
2523                 msg->cm_lengths[eMesageText] = holdMLen;
2524         }
2525
2526         if (smr.len == 0) {
2527                 cprintf("%d Unable to serialize message\n",
2528                         ERROR + INTERNAL_ERROR);
2529                 return (-1L);
2530         }
2531
2532         /* Write our little bundle of joy into the message base */
2533         if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2534                       smr.ser, smr.len) < 0) {
2535                 MSGM_syslog(LOG_ERR, "Can't store message\n");
2536                 retval = 0L;
2537         } else {
2538                 if (is_bigmsg) {
2539                         cdb_store(CDB_BIGMSGS,
2540                                   &newmsgid,
2541                                   (int)sizeof(long),
2542                                   holdM,
2543                                   (holdMLen + 1)
2544                                 );
2545                 }
2546                 retval = newmsgid;
2547         }
2548
2549         /* Free the memory we used for the serialized message */
2550         free(smr.ser);
2551
2552         /* Return the *local* message ID to the caller
2553          * (even if we're storing an incoming network message)
2554          */
2555         return(retval);
2556 }
2557
2558
2559
2560 /*
2561  * Serialize a struct CtdlMessage into the format used on disk and network.
2562  * 
2563  * This function loads up a "struct ser_ret" (defined in server.h) which
2564  * contains the length of the serialized message and a pointer to the
2565  * serialized message in memory.  THE LATTER MUST BE FREED BY THE CALLER.
2566  */
2567 void CtdlSerializeMessage(struct ser_ret *ret,          /* return values */
2568                        struct CtdlMessage *msg) /* unserialized msg */
2569 {
2570         struct CitContext *CCC = CC;
2571         size_t wlen;
2572         int i;
2573
2574         /*
2575          * Check for valid message format
2576          */
2577         if (CM_IsValidMsg(msg) == 0) {
2578                 MSGM_syslog(LOG_ERR, "CtdlSerializeMessage() aborting due to invalid message\n");
2579                 ret->len = 0;
2580                 ret->ser = NULL;
2581                 return;
2582         }
2583
2584         ret->len = 3;
2585         for (i=0; i < NDiskFields; ++i)
2586                 if (msg->cm_fields[FieldOrder[i]] != NULL)
2587                         ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2588
2589         ret->ser = malloc(ret->len);
2590         if (ret->ser == NULL) {
2591                 MSG_syslog(LOG_ERR, "CtdlSerializeMessage() malloc(%ld) failed: %s\n",
2592                            (long)ret->len, strerror(errno));
2593                 ret->len = 0;
2594                 ret->ser = NULL;
2595                 return;
2596         }
2597
2598         ret->ser[0] = 0xFF;
2599         ret->ser[1] = msg->cm_anon_type;
2600         ret->ser[2] = msg->cm_format_type;
2601         wlen = 3;
2602
2603         for (i=0; i < NDiskFields; ++i)
2604                 if (msg->cm_fields[FieldOrder[i]] != NULL)
2605                 {
2606                         ret->ser[wlen++] = (char)FieldOrder[i];
2607
2608                         memcpy(&ret->ser[wlen],
2609                                msg->cm_fields[FieldOrder[i]],
2610                                msg->cm_lengths[FieldOrder[i]] + 1);
2611
2612                         wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2613                 }
2614
2615         if (ret->len != wlen) {
2616                 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2617                            (long)ret->len, (long)wlen);
2618         }
2619
2620         return;
2621 }
2622
2623
2624 /*
2625  * Check to see if any messages already exist in the current room which
2626  * carry the same Exclusive ID as this one.  If any are found, delete them.
2627  */
2628 void ReplicationChecks(struct CtdlMessage *msg) {
2629         struct CitContext *CCC = CC;
2630         long old_msgnum = (-1L);
2631
2632         if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2633
2634         MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2635                    CCC->room.QRname);
2636
2637         /* No exclusive id?  Don't do anything. */
2638         if (msg == NULL) return;
2639         if (CM_IsEmpty(msg, eExclusiveID)) return;
2640
2641         /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2642           msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2643
2644         old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2645         if (old_msgnum > 0L) {
2646                 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2647                 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2648         }
2649 }
2650
2651
2652
2653 /*
2654  * Save a message to disk and submit it into the delivery system.
2655  */
2656 long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
2657                    recptypes *recps,            /* recipients (if mail) */
2658                    const char *force,           /* force a particular room? */
2659                    int flags                    /* should the message be exported clean? */
2660         )
2661 {
2662         char hold_rm[ROOMNAMELEN];
2663         char actual_rm[ROOMNAMELEN];
2664         char force_room[ROOMNAMELEN];
2665         char content_type[SIZ];                 /* We have to learn this */
2666         char recipient[SIZ];
2667         char bounce_to[1024];
2668         const char *room;
2669         long newmsgid;
2670         const char *mptr = NULL;
2671         struct ctdluser userbuf;
2672         int a, i;
2673         struct MetaData smi;
2674         char *collected_addresses = NULL;
2675         struct addresses_to_be_filed *aptr = NULL;
2676         StrBuf *saved_rfc822_version = NULL;
2677         int qualified_for_journaling = 0;
2678         CitContext *CCC = MyContext();
2679
2680         MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2681         if (CM_IsValidMsg(msg) == 0) return(-1);        /* self check */
2682
2683         /* If this message has no timestamp, we take the liberty of
2684          * giving it one, right now.
2685          */
2686         if (CM_IsEmpty(msg, eTimestamp)) {
2687                 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2688         }
2689
2690         /* If this message has no path, we generate one.
2691          */
2692         if (CM_IsEmpty(msg, eMessagePath)) {
2693                 if (!CM_IsEmpty(msg, eAuthor)) {
2694                         CM_CopyField(msg, eMessagePath, eAuthor);
2695                         for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2696                                 if (isspace(msg->cm_fields[eMessagePath][a])) {
2697                                         msg->cm_fields[eMessagePath][a] = ' ';
2698                                 }
2699                         }
2700                 }
2701                 else {
2702                         CM_SetField(msg, eMessagePath, HKEY("unknown"));
2703                 }
2704         }
2705
2706         if (force == NULL) {
2707                 force_room[0] = '\0';
2708         }
2709         else {
2710                 strcpy(force_room, force);
2711         }
2712
2713         /* Learn about what's inside, because it's what's inside that counts */
2714         if (CM_IsEmpty(msg, eMesageText)) {
2715                 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2716                 return(-2);
2717         }
2718
2719         switch (msg->cm_format_type) {
2720         case 0:
2721                 strcpy(content_type, "text/x-citadel-variformat");
2722                 break;
2723         case 1:
2724                 strcpy(content_type, "text/plain");
2725                 break;
2726         case 4:
2727                 strcpy(content_type, "text/plain");
2728                 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2729                 if (mptr != NULL) {
2730                         char *aptr;
2731                         safestrncpy(content_type, &mptr[13], sizeof content_type);
2732                         striplt(content_type);
2733                         aptr = content_type;
2734                         while (!IsEmptyStr(aptr)) {
2735                                 if ((*aptr == ';')
2736                                     || (*aptr == ' ')
2737                                     || (*aptr == 13)
2738                                     || (*aptr == 10)) {
2739                                         *aptr = 0;
2740                                 }
2741                                 else aptr++;
2742                         }
2743                 }
2744         }
2745
2746         /* Goto the correct room */
2747         room = (recps) ? CCC->room.QRname : SENTITEMS;
2748         MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
2749         strcpy(hold_rm, CCC->room.QRname);
2750         strcpy(actual_rm, CCC->room.QRname);
2751         if (recps != NULL) {
2752                 strcpy(actual_rm, SENTITEMS);
2753         }
2754
2755         /* If the user is a twit, move to the twit room for posting */
2756         if (TWITDETECT) {
2757                 if (CCC->user.axlevel == AxProbU) {
2758                         strcpy(hold_rm, actual_rm);
2759                         strcpy(actual_rm, config.c_twitroom);
2760                         MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
2761                 }
2762         }
2763
2764         /* ...or if this message is destined for Aide> then go there. */
2765         if (!IsEmptyStr(force_room)) {
2766                 strcpy(actual_rm, force_room);
2767         }
2768
2769         MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
2770         if (strcasecmp(actual_rm, CCC->room.QRname)) {
2771                 /* CtdlGetRoom(&CCC->room, actual_rm); */
2772                 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2773         }
2774
2775         /*
2776          * If this message has no O (room) field, generate one.
2777          */
2778         if (CM_IsEmpty(msg, eOriginalRoom)) {
2779                 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
2780         }
2781
2782         /* Perform "before save" hooks (aborting if any return nonzero) */
2783         MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
2784         if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2785
2786         /*
2787          * If this message has an Exclusive ID, and the room is replication
2788          * checking enabled, then do replication checks.
2789          */
2790         if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
2791                 ReplicationChecks(msg);
2792         }
2793
2794         /* Save it to disk */
2795         MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
2796         newmsgid = send_message(msg);
2797         if (newmsgid <= 0L) return(-5);
2798
2799         /* Write a supplemental message info record.  This doesn't have to
2800          * be a critical section because nobody else knows about this message
2801          * yet.
2802          */
2803         MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
2804         memset(&smi, 0, sizeof(struct MetaData));
2805         smi.meta_msgnum = newmsgid;
2806         smi.meta_refcount = 0;
2807         safestrncpy(smi.meta_content_type, content_type,
2808                     sizeof smi.meta_content_type);
2809
2810         /*
2811          * Measure how big this message will be when rendered as RFC822.
2812          * We do this for two reasons:
2813          * 1. We need the RFC822 length for the new metadata record, so the
2814          *    POP and IMAP services don't have to calculate message lengths
2815          *    while the user is waiting (multiplied by potentially hundreds
2816          *    or thousands of messages).
2817          * 2. If journaling is enabled, we will need an RFC822 version of the
2818          *    message to attach to the journalized copy.
2819          */
2820         if (CCC->redirect_buffer != NULL) {
2821                 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
2822                 abort();
2823         }
2824         CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2825         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2826         smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
2827         saved_rfc822_version = CCC->redirect_buffer;
2828         CCC->redirect_buffer = NULL;
2829
2830         PutMetaData(&smi);
2831
2832         /* Now figure out where to store the pointers */
2833         MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
2834
2835         /* If this is being done by the networker delivering a private
2836          * message, we want to BYPASS saving the sender's copy (because there
2837          * is no local sender; it would otherwise go to the Trashcan).
2838          */
2839         if ((!CCC->internal_pgm) || (recps == NULL)) {
2840                 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2841                         MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
2842                         CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2843                 }
2844         }
2845
2846         /* For internet mail, drop a copy in the outbound queue room */
2847         if ((recps != NULL) && (recps->num_internet > 0)) {
2848                 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2849         }
2850
2851         /* If other rooms are specified, drop them there too. */
2852         if ((recps != NULL) && (recps->num_room > 0))
2853                 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2854                         extract_token(recipient, recps->recp_room, i,
2855                                       '|', sizeof recipient);
2856                         MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
2857                         CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2858                 }
2859
2860         /* Bump this user's messages posted counter. */
2861         MSGM_syslog(LOG_DEBUG, "Updating user\n");
2862         CtdlGetUserLock(&CCC->user, CCC->curr_user);
2863         CCC->user.posted = CCC->user.posted + 1;
2864         CtdlPutUserLock(&CCC->user);
2865
2866         /* Decide where bounces need to be delivered */
2867         if ((recps != NULL) && (recps->bounce_to == NULL))
2868         {
2869                 if (CCC->logged_in) 
2870                         snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2871                                  CCC->user.fullname, config.c_nodename);
2872                 else 
2873                         snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2874                                  msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2875                 recps->bounce_to = bounce_to;
2876         }
2877                 
2878         CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2879
2880
2881         /* If this is private, local mail, make a copy in the
2882          * recipient's mailbox and bump the reference count.
2883          */
2884         if ((recps != NULL) && (recps->num_local > 0))
2885         {
2886                 char *pch;
2887                 int ntokens;
2888
2889                 pch = recps->recp_local;
2890                 recps->recp_local = recipient;
2891                 ntokens = num_tokens(pch, '|');
2892                 for (i=0; i<ntokens; ++i)
2893                 {
2894                         extract_token(recipient, pch, i, '|', sizeof recipient);
2895                         MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n", recipient);
2896                         if (CtdlGetUser(&userbuf, recipient) == 0) {
2897                                 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2898                                 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2899                                 CtdlBumpNewMailCounter(userbuf.usernum);
2900                                 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2901                         }
2902                         else {
2903                                 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
2904                                 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2905                         }
2906                 }
2907                 recps->recp_local = pch;
2908         }
2909
2910         /* Perform "after save" hooks */
2911         MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
2912
2913         PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2914         CM_FlushField(msg, eVltMsgNum);
2915
2916         /* Go back to the room we started from */
2917         MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
2918         if (strcasecmp(hold_rm, CCC->room.QRname))
2919                 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2920
2921         /*
2922          * Any addresses to harvest for someone's address book?
2923          */
2924         if ( (CCC->logged_in) && (recps != NULL) ) {
2925                 collected_addresses = harvest_collected_addresses(msg);
2926         }
2927
2928         if (collected_addresses != NULL) {
2929                 aptr = (struct addresses_to_be_filed *)
2930                         malloc(sizeof(struct addresses_to_be_filed));
2931                 CtdlMailboxName(actual_rm, sizeof actual_rm,
2932                                 &CCC->user, USERCONTACTSROOM);
2933                 aptr->roomname = strdup(actual_rm);
2934                 aptr->collected_addresses = collected_addresses;
2935                 begin_critical_section(S_ATBF);
2936                 aptr->next = atbf;
2937                 atbf = aptr;
2938                 end_critical_section(S_ATBF);
2939         }
2940
2941         /*
2942          * Determine whether this message qualifies for journaling.
2943          */
2944         if (!CM_IsEmpty(msg, eJournal)) {
2945                 qualified_for_journaling = 0;
2946         }
2947         else {
2948                 if (recps == NULL) {
2949                         qualified_for_journaling = config.c_journal_pubmsgs;
2950                 }
2951                 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2952                         qualified_for_journaling = config.c_journal_email;
2953                 }
2954                 else {
2955                         qualified_for_journaling = config.c_journal_pubmsgs;
2956                 }
2957         }
2958
2959         /*
2960          * Do we have to perform journaling?  If so, hand off the saved
2961          * RFC822 version will be handed off to the journaler for background
2962          * submit.  Otherwise, we have to free the memory ourselves.
2963          */
2964         if (saved_rfc822_version != NULL) {
2965                 if (qualified_for_journaling) {
2966                         JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2967                 }
2968                 else {
2969                         FreeStrBuf(&saved_rfc822_version);
2970                 }
2971         }
2972
2973         if ((recps != NULL) && (recps->bounce_to == bounce_to))
2974                 recps->bounce_to = NULL;
2975
2976         /* Done. */
2977         return(newmsgid);
2978 }
2979
2980
2981 /*
2982  * Convenience function for generating small administrative messages.
2983  */
2984 void quickie_message(const char *from,
2985                      const char *fromaddr,
2986                      const char *to,
2987                      char *room,
2988                      const char *text, 
2989                      int format_type,
2990                      const char *subject)
2991 {
2992         struct CtdlMessage *msg;
2993         recptypes *recp = NULL;
2994
2995         msg = malloc(sizeof(struct CtdlMessage));
2996         memset(msg, 0, sizeof(struct CtdlMessage));
2997         msg->cm_magic = CTDLMESSAGE_MAGIC;
2998         msg->cm_anon_type = MES_NORMAL;
2999         msg->cm_format_type = format_type;
3000
3001         if (from != NULL) {
3002                 CM_SetField(msg, eAuthor, from, strlen(from));
3003         }
3004         else if (fromaddr != NULL) {
3005                 char *pAt;
3006                 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3007                 pAt = strchr(msg->cm_fields[eAuthor], '@');
3008                 if (pAt != NULL) {
3009                         CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3010                 }
3011         }
3012         else {
3013                 msg->cm_fields[eAuthor] = strdup("Citadel");
3014         }
3015
3016         if (fromaddr != NULL) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3017         if (room != NULL) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3018         CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
3019         if (to != NULL) {
3020                 CM_SetField(msg, eRecipient, to, strlen(to));
3021                 recp = validate_recipients(to, NULL, 0);
3022         }
3023         if (subject != NULL) {
3024                 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3025         }
3026         CM_SetField(msg, eMesageText, text, strlen(text));
3027
3028         CtdlSubmitMsg(msg, recp, room, 0);
3029         CM_Free(msg);
3030         if (recp != NULL) free_recipients(recp);
3031 }
3032
3033 void flood_protect_quickie_message(const char *from,
3034                                    const char *fromaddr,
3035                                    const char *to,
3036                                    char *room,
3037                                    const char *text, 
3038                                    int format_type,
3039                                    const char *subject,
3040                                    int nCriterions,
3041                                    const char **CritStr,
3042                                    long *CritStrLen,
3043                                    long ccid,
3044                                    long ioid,
3045                                    time_t NOW)
3046 {
3047         int i;
3048         u_char rawdigest[MD5_DIGEST_LEN];
3049         struct MD5Context md5context;
3050         StrBuf *guid;
3051         char timestamp[64];
3052         long tslen;
3053         time_t tsday = NOW / (8*60*60); /* just care for a day... */
3054
3055         tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3056         MD5Init(&md5context);
3057
3058         for (i = 0; i < nCriterions; i++)
3059                 MD5Update(&md5context,
3060                           (const unsigned char*)CritStr[i], CritStrLen[i]);
3061         MD5Update(&md5context,
3062                   (const unsigned char*)timestamp, tslen);
3063         MD5Final(rawdigest, &md5context);
3064
3065         guid = NewStrBufPlain(NULL,
3066                               MD5_DIGEST_LEN * 2 + 12);
3067         StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3068         StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3069         if (StrLength(guid) > 40)
3070                 StrBufCutAt(guid, 40, NULL);
3071
3072         if (CheckIfAlreadySeen("FPAideMessage",
3073                                guid,
3074                                NOW,
3075                                tsday,
3076                                eUpdate,
3077                                ccid,
3078                                ioid)!= 0)
3079         {
3080                 FreeStrBuf(&guid);
3081                 /* yes, we did. flood protection kicks in. */
3082                 syslog(LOG_DEBUG,
3083                        "not sending message again\n");
3084                 return;
3085         }
3086         FreeStrBuf(&guid);
3087         /* no, this message isn't sent recently; go ahead. */
3088         quickie_message(from,
3089                         fromaddr,
3090                         to,
3091                         room,
3092                         text, 
3093                         format_type,
3094                         subject);
3095 }
3096
3097
3098 /*
3099  * Back end function used by CtdlMakeMessage() and similar functions
3100  */
3101 StrBuf *CtdlReadMessageBodyBuf(char *terminator,        /* token signalling EOT */
3102                                long tlen,
3103                                size_t maxlen,           /* maximum message length */
3104                                StrBuf *exist,           /* if non-null, append to it;
3105                                                            exist is ALWAYS freed  */
3106                                int crlf,                /* CRLF newlines instead of LF */
3107                                int *sock                /* socket handle or 0 for this session's client socket */
3108         ) 
3109 {
3110         StrBuf *Message;
3111         StrBuf *LineBuf;
3112         int flushing = 0;
3113         int finished = 0;
3114         int dotdot = 0;
3115
3116         LineBuf = NewStrBufPlain(NULL, SIZ);
3117         if (exist == NULL) {
3118                 Message = NewStrBufPlain(NULL, 4 * SIZ);
3119         }
3120         else {
3121                 Message = NewStrBufDup(exist);
3122         }
3123
3124         /* Do we need to change leading ".." to "." for SMTP escaping? */
3125         if ((tlen == 1) && (*terminator == '.')) {
3126                 dotdot = 1;
3127         }
3128
3129         /* read in the lines of message text one by one */
3130         do {
3131                 if (sock != NULL) {
3132                         if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3133                             (*sock == -1))
3134                                 finished = 1;
3135                 }
3136                 else {
3137                         if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3138                 }
3139                 if ((StrLength(LineBuf) == tlen) && 
3140                     (!strcmp(ChrPtr(LineBuf), terminator)))
3141                         finished = 1;
3142
3143                 if ( (!flushing) && (!finished) ) {
3144                         if (crlf) {
3145                                 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3146                         }
3147                         else {
3148                                 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3149                         }
3150                         
3151                         /* Unescape SMTP-style input of two dots at the beginning of the line */
3152                         if ((dotdot) &&
3153                             (StrLength(LineBuf) == 2) && 
3154                             (!strcmp(ChrPtr(LineBuf), "..")))
3155                         {
3156                                 StrBufCutLeft(LineBuf, 1);
3157                         }
3158                         
3159                         StrBufAppendBuf(Message, LineBuf, 0);
3160                 }
3161
3162                 /* if we've hit the max msg length, flush the rest */
3163                 if (StrLength(Message) >= maxlen) flushing = 1;
3164
3165         } while (!finished);
3166         FreeStrBuf(&LineBuf);
3167         return Message;
3168 }
3169
3170 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3171 {
3172         if (*Msg == NULL)
3173                 return;
3174         FreeStrBuf(&(*Msg)->MsgBuf);
3175
3176         free(*Msg);
3177         *Msg = NULL;
3178 }
3179
3180 ReadAsyncMsg *NewAsyncMsg(const char *terminator,       /* token signalling EOT */
3181                           long tlen,
3182                           size_t maxlen,                /* maximum message length */
3183                           size_t expectlen,             /* if we expect a message, how long should it be? */
3184                           StrBuf *exist,                /* if non-null, append to it;
3185                                                            exist is ALWAYS freed  */
3186                           long eLen,                    /* length of exist */
3187                           int crlf                      /* CRLF newlines instead of LF */
3188         )
3189 {
3190         ReadAsyncMsg *NewMsg;
3191
3192         NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3193         memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3194
3195         if (exist == NULL) {
3196                 long len;
3197
3198                 if (expectlen == 0) {
3199                         len = 4 * SIZ;
3200                 }
3201                 else {
3202                         len = expectlen + 10;
3203                 }
3204                 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3205         }
3206         else {
3207                 NewMsg->MsgBuf = NewStrBufDup(exist);
3208         }
3209         /* Do we need to change leading ".." to "." for SMTP escaping? */
3210         if ((tlen == 1) && (*terminator == '.')) {
3211                 NewMsg->dodot = 1;
3212         }
3213
3214         NewMsg->terminator = terminator;
3215         NewMsg->tlen = tlen;
3216
3217         NewMsg->maxlen = maxlen;
3218
3219         NewMsg->crlf = crlf;
3220
3221         return NewMsg;
3222 }
3223
3224 /*
3225  * Back end function used by CtdlMakeMessage() and similar functions
3226  */
3227 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3228 {
3229         ReadAsyncMsg *ReadMsg;
3230         int MsgFinished = 0;
3231         eReadState Finished = eMustReadMore;
3232
3233 #ifdef BIGBAD_IODBG
3234         char fn [SIZ];
3235         FILE *fd;
3236         const char *pch = ChrPtr(IO->SendBuf.Buf);
3237         const char *pchh = IO->SendBuf.ReadWritePointer;
3238         long nbytes;
3239         
3240         if (pchh == NULL)
3241                 pchh = pch;
3242         
3243         nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3244         snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3245                  ((CitContext*)(IO->CitContext))->ServiceName,
3246                  IO->SendBuf.fd);
3247         
3248         fd = fopen(fn, "a+");
3249 #endif
3250
3251         ReadMsg = IO->ReadMsg;
3252
3253         /* read in the lines of message text one by one */
3254         do {
3255                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3256                 
3257                 switch (Finished) {
3258                 case eMustReadMore: /// read new from socket... 
3259 #ifdef BIGBAD_IODBG
3260                         if (IO->RecvBuf.ReadWritePointer != NULL) {
3261                                 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3262                                 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3263                                 
3264                                 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3265                         
3266                                 fprintf(fd, "]\n");
3267                         } else {
3268                                 fprintf(fd, "BufferEmpty! \n");
3269                         }
3270                         fclose(fd);
3271 #endif
3272                         return Finished;
3273                     break;
3274                 case eBufferNotEmpty: /* shouldn't happen... */
3275                 case eReadSuccess: /// done for now...
3276                     break;
3277                 case eReadFail: /// WHUT?
3278                     ///todo: shut down! 
3279                         break;
3280                 }
3281             
3282
3283                 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) && 
3284                     (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3285                         MsgFinished = 1;
3286 #ifdef BIGBAD_IODBG
3287                         fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3288 #endif
3289                 }
3290                 else if (!ReadMsg->flushing) {
3291
3292 #ifdef BIGBAD_IODBG
3293                         fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3294 #endif
3295
3296                         /* Unescape SMTP-style input of two dots at the beginning of the line */
3297                         if ((ReadMsg->dodot) &&
3298                             (StrLength(IO->IOBuf) == 2) &&  /* TODO: do we just unescape lines with two dots or any line? */
3299                             (!strcmp(ChrPtr(IO->IOBuf), "..")))
3300                         {
3301 #ifdef BIGBAD_IODBG
3302                                 fprintf(fd, "UnEscaped!\n");
3303 #endif
3304                                 StrBufCutLeft(IO->IOBuf, 1);
3305                         }
3306
3307                         if (ReadMsg->crlf) {
3308                                 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3309                         }
3310                         else {
3311                                 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3312                         }
3313
3314                         StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3315                 }
3316
3317                 /* if we've hit the max msg length, flush the rest */
3318                 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3319
3320         } while (!MsgFinished);
3321
3322 #ifdef BIGBAD_IODBG
3323         fprintf(fd, "Done with reading; %s.\n, ",
3324                 (MsgFinished)?"Message Finished": "FAILED");
3325         fclose(fd);
3326 #endif
3327         if (MsgFinished)
3328                 return eReadSuccess;
3329         else 
3330                 return eAbort;
3331 }
3332
3333
3334 /*
3335  * Back end function used by CtdlMakeMessage() and similar functions
3336  */
3337 char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
3338                           long tlen,
3339                           size_t maxlen,                /* maximum message length */
3340                           StrBuf *exist,                /* if non-null, append to it;
3341                                                    exist is ALWAYS freed  */
3342                           int crlf,             /* CRLF newlines instead of LF */
3343                           int *sock             /* socket handle or 0 for this session's client socket */
3344         ) 
3345 {
3346         StrBuf *Message;
3347
3348         Message = CtdlReadMessageBodyBuf(terminator,
3349                                          tlen,
3350                                          maxlen,
3351                                          exist,
3352                                          crlf,
3353                                          sock);
3354         if (Message == NULL)
3355                 return NULL;
3356         else
3357                 return SmashStrBuf(&Message);
3358 }
3359
3360 struct CtdlMessage *CtdlMakeMessage(
3361         struct ctdluser *author,        /* author's user structure */
3362         char *recipient,                /* NULL if it's not mail */
3363         char *recp_cc,                  /* NULL if it's not mail */
3364         char *room,                     /* room where it's going */
3365         int type,                       /* see MES_ types in header file */
3366         int format_type,                /* variformat, plain text, MIME... */
3367         char *fake_name,                /* who we're masquerading as */
3368         char *my_email,                 /* which of my email addresses to use (empty is ok) */
3369         char *subject,                  /* Subject (optional) */
3370         char *supplied_euid,            /* ...or NULL if this is irrelevant */
3371         char *preformatted_text,        /* ...or NULL to read text from client */
3372         char *references                /* Thread references */
3373 )
3374 {
3375         return CtdlMakeMessageLen(
3376                 author, /* author's user structure */
3377                 recipient,              /* NULL if it's not mail */
3378                 (recipient)?strlen(recipient) : 0,
3379                 recp_cc,                        /* NULL if it's not mail */
3380                 (recp_cc)?strlen(recp_cc): 0,
3381                 room,                   /* room where it's going */
3382                 (room)?strlen(room): 0,
3383                 type,                   /* see MES_ types in header file */
3384                 format_type,            /* variformat, plain text, MIME... */
3385                 fake_name,              /* who we're masquerading as */
3386                 (fake_name)?strlen(fake_name): 0,
3387                 my_email,                       /* which of my email addresses to use (empty is ok) */
3388                 (my_email)?strlen(my_email): 0,
3389                 subject,                        /* Subject (optional) */
3390                 (subject)?strlen(subject): 0,
3391                 supplied_euid,          /* ...or NULL if this is irrelevant */
3392                 (supplied_euid)?strlen(supplied_euid):0,
3393                 preformatted_text,      /* ...or NULL to read text from client */
3394                 (preformatted_text)?strlen(preformatted_text) : 0,
3395                 references,             /* Thread references */
3396                 (references)?strlen(references):0);
3397
3398 }
3399
3400 /*
3401  * Build a binary message to be saved on disk.
3402  * (NOTE: if you supply 'preformatted_text', the buffer you give it
3403  * will become part of the message.  This means you are no longer
3404  * responsible for managing that memory -- it will be freed along with
3405  * the rest of the fields when CM_Free() is called.)
3406  */
3407
3408 struct CtdlMessage *CtdlMakeMessageLen(
3409         struct ctdluser *author,        /* author's user structure */
3410         char *recipient,                /* NULL if it's not mail */
3411         long rcplen,
3412         char *recp_cc,                  /* NULL if it's not mail */
3413         long cclen,
3414         char *room,                     /* room where it's going */
3415         long roomlen,
3416         int type,                       /* see MES_ types in header file */
3417         int format_type,                /* variformat, plain text, MIME... */
3418         char *fake_name,                /* who we're masquerading as */
3419         long fnlen,
3420         char *my_email,                 /* which of my email addresses to use (empty is ok) */
3421         long myelen,
3422         char *subject,                  /* Subject (optional) */
3423         long subjlen,
3424         char *supplied_euid,            /* ...or NULL if this is irrelevant */
3425         long euidlen,
3426         char *preformatted_text,        /* ...or NULL to read text from client */
3427         long textlen,
3428         char *references,               /* Thread references */
3429         long reflen
3430         )
3431 {
3432         struct CitContext *CCC = CC;
3433         /* Don't confuse the poor folks if it's not routed mail. * /
3434            char dest_node[256] = "";*/
3435         long blen;
3436         char buf[1024];
3437         struct CtdlMessage *msg;
3438         StrBuf *FakeAuthor;
3439         StrBuf *FakeEncAuthor = NULL;
3440
3441         msg = malloc(sizeof(struct CtdlMessage));
3442         memset(msg, 0, sizeof(struct CtdlMessage));
3443         msg->cm_magic = CTDLMESSAGE_MAGIC;
3444         msg->cm_anon_type = type;
3445         msg->cm_format_type = format_type;
3446
3447         if (recipient != NULL) rcplen = striplt(recipient);
3448         if (recp_cc != NULL) cclen = striplt(recp_cc);
3449
3450         /* Path or Return-Path */
3451         if (myelen > 0) {
3452                 CM_SetField(msg, eMessagePath, my_email, myelen);
3453         }
3454         else {
3455                 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3456         }
3457         convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3458
3459         blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3460         CM_SetField(msg, eTimestamp, buf, blen);
3461
3462         if (fnlen > 0) {
3463                 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3464         }
3465         else {
3466                 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3467         }
3468         StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3469         CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3470         FreeStrBuf(&FakeAuthor);
3471
3472         if (CCC->room.QRflags & QR_MAILBOX) {           /* room */
3473                 CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
3474         }
3475         else {
3476                 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
3477         }
3478
3479         CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
3480         CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
3481
3482         if (rcplen > 0) {
3483                 CM_SetField(msg, eRecipient, recipient, rcplen);
3484         }
3485         if (cclen > 0) {
3486                 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3487         }
3488
3489         if (myelen > 0) {
3490                 CM_SetField(msg, erFc822Addr, my_email, myelen);
3491         }
3492         else if ( (author == &CCC->user) && (!IsEmptyStr(CCC->cs_inet_email)) ) {
3493                 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
3494         }
3495
3496         if (subject != NULL) {
3497                 long length;
3498                 length = striplt(subject);
3499                 if (length > 0) {
3500                         long i;
3501                         long IsAscii;
3502                         IsAscii = -1;
3503                         i = 0;
3504                         while ((subject[i] != '\0') &&
3505                                (IsAscii = isascii(subject[i]) != 0 ))
3506                                 i++;
3507                         if (IsAscii != 0)
3508                                 CM_SetField(msg, eMsgSubject, subject, subjlen);
3509                         else /* ok, we've got utf8 in the string. */
3510                         {
3511                                 char *rfc2047Subj;
3512                                 rfc2047Subj = rfc2047encode(subject, length);
3513                                 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3514                         }
3515
3516                 }
3517         }
3518
3519         if (euidlen > 0) {
3520                 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3521         }
3522
3523         if (reflen > 0) {
3524                 CM_SetField(msg, eWeferences, references, reflen);
3525         }
3526
3527         if (preformatted_text != NULL) {
3528                 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3529         }
3530         else {
3531                 StrBuf *MsgBody;
3532                 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
3533                 if (MsgBody != NULL) {
3534                         CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3535                 }
3536         }
3537
3538         return(msg);
3539 }
3540
3541
3542
3543
3544 /*
3545  * API function to delete messages which match a set of criteria
3546  * (returns the actual number of messages deleted)
3547  */
3548 int CtdlDeleteMessages(char *room_name,         /* which room */
3549                        long *dmsgnums,          /* array of msg numbers to be deleted */
3550                        int num_dmsgnums,        /* number of msgs to be deleted, or 0 for "any" */
3551                        char *content_type       /* or "" for any.  regular expressions expected. */
3552         )
3553 {
3554         struct CitContext *CCC = CC;
3555         struct ctdlroom qrbuf;
3556         struct cdbdata *cdbfr;
3557         long *msglist = NULL;
3558         long *dellist = NULL;
3559         int num_msgs = 0;
3560         int i, j;
3561         int num_deleted = 0;
3562         int delete_this;
3563         struct MetaData smi;
3564         regex_t re;
3565         regmatch_t pm;
3566         int need_to_free_re = 0;
3567
3568         if (content_type) if (!IsEmptyStr(content_type)) {
3569                         regcomp(&re, content_type, 0);
3570                         need_to_free_re = 1;
3571                 }
3572         MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
3573                    room_name, num_dmsgnums, content_type);
3574
3575         /* get room record, obtaining a lock... */
3576         if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3577                 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
3578                            room_name);
3579                 if (need_to_free_re) regfree(&re);
3580                 return (0);     /* room not found */
3581         }
3582         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3583
3584         if (cdbfr != NULL) {
3585                 dellist = malloc(cdbfr->len);
3586                 msglist = (long *) cdbfr->ptr;
3587                 cdbfr->ptr = NULL;      /* CtdlDeleteMessages() now owns this memory */
3588                 num_msgs = cdbfr->len / sizeof(long);
3589                 cdb_free(cdbfr);
3590         }
3591         if (num_msgs > 0) {
3592                 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3593                 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3594                 int have_more_del = 1;
3595
3596                 num_msgs = sort_msglist(msglist, num_msgs);
3597                 if (num_dmsgnums > 1)
3598                         num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3599 /*
3600                 {
3601                         StrBuf *dbg = NewStrBuf();
3602                         for (i = 0; i < num_dmsgnums; i++)
3603                                 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3604                         MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
3605                         FreeStrBuf(&dbg);
3606                 }
3607 */
3608                 i = 0; j = 0;
3609                 while ((i < num_msgs) && (have_more_del)) {
3610                         delete_this = 0x00;
3611
3612                         /* Set/clear a bit for each criterion */
3613
3614                         /* 0 messages in the list or a null list means that we are
3615                          * interested in deleting any messages which meet the other criteria.
3616                          */
3617                         if (have_delmsgs) {
3618                                 delete_this |= 0x01;
3619                         }
3620                         else {
3621                                 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3622
3623                                 if (i >= num_msgs)
3624                                         continue;
3625
3626                                 if (msglist[i] == dmsgnums[j]) {
3627                                         delete_this |= 0x01;
3628                                 }
3629                                 j++;
3630                                 have_more_del = (j < num_dmsgnums);
3631                         }
3632
3633                         if (have_contenttype) {
3634                                 GetMetaData(&smi, msglist[i]);
3635                                 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3636                                         delete_this |= 0x02;
3637                                 }
3638                         } else {
3639                                 delete_this |= 0x02;
3640                         }
3641
3642                         /* Delete message only if all bits are set */
3643                         if (delete_this == 0x03) {
3644                                 dellist[num_deleted++] = msglist[i];
3645                                 msglist[i] = 0L;
3646                         }
3647                         i++;
3648                 }
3649 /*
3650                 {
3651                         StrBuf *dbg = NewStrBuf();
3652                         for (i = 0; i < num_deleted; i++)
3653                                 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3654                         MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
3655                         FreeStrBuf(&dbg);
3656                 }
3657 */
3658                 num_msgs = sort_msglist(msglist, num_msgs);
3659                 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3660                           msglist, (int)(num_msgs * sizeof(long)));
3661
3662                 if (num_msgs > 0)
3663                         qrbuf.QRhighest = msglist[num_msgs - 1];
3664                 else
3665                         qrbuf.QRhighest = 0;
3666         }
3667         CtdlPutRoomLock(&qrbuf);
3668
3669         /* Go through the messages we pulled out of the index, and decrement
3670          * their reference counts by 1.  If this is the only room the message
3671          * was in, the reference count will reach zero and the message will
3672          * automatically be deleted from the database.  We do this in a
3673          * separate pass because there might be plug-in hooks getting called,
3674          * and we don't want that happening during an S_ROOMS critical
3675          * section.
3676          */
3677         if (num_deleted) {
3678                 for (i=0; i<num_deleted; ++i) {
3679                         PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3680                 }
3681                 AdjRefCountList(dellist, num_deleted, -1);
3682         }
3683         /* Now free the memory we used, and go away. */
3684         if (msglist != NULL) free(msglist);
3685         if (dellist != NULL) free(dellist);
3686         MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
3687         if (need_to_free_re) regfree(&re);
3688         return (num_deleted);
3689 }
3690
3691
3692
3693
3694 /*
3695  * GetMetaData()  -  Get the supplementary record for a message
3696  */
3697 void GetMetaData(struct MetaData *smibuf, long msgnum)
3698 {
3699
3700         struct cdbdata *cdbsmi;
3701         long TheIndex;
3702
3703         memset(smibuf, 0, sizeof(struct MetaData));
3704         smibuf->meta_msgnum = msgnum;
3705         smibuf->meta_refcount = 1;      /* Default reference count is 1 */
3706
3707         /* Use the negative of the message number for its supp record index */
3708         TheIndex = (0L - msgnum);
3709
3710         cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3711         if (cdbsmi == NULL) {
3712                 return;         /* record not found; go with defaults */
3713         }
3714         memcpy(smibuf, cdbsmi->ptr,
3715                ((cdbsmi->len > sizeof(struct MetaData)) ?
3716                 sizeof(struct MetaData) : cdbsmi->len));
3717         cdb_free(cdbsmi);
3718         return;
3719 }
3720
3721
3722 /*
3723  * PutMetaData()  -  (re)write supplementary record for a message
3724  */
3725 void PutMetaData(struct MetaData *smibuf)
3726 {
3727         long TheIndex;
3728
3729         /* Use the negative of the message number for the metadata db index */
3730         TheIndex = (0L - smibuf->meta_msgnum);
3731
3732         cdb_store(CDB_MSGMAIN,
3733                   &TheIndex, (int)sizeof(long),
3734                   smibuf, (int)sizeof(struct MetaData));
3735
3736 }
3737
3738 /*
3739  * AdjRefCount  -  submit an adjustment to the reference count for a message.
3740  *                 (These are just queued -- we actually process them later.)
3741  */
3742 void AdjRefCount(long msgnum, int incr)
3743 {
3744         struct CitContext *CCC = CC;
3745         struct arcq new_arcq;
3746         int rv = 0;
3747
3748         MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
3749
3750         begin_critical_section(S_SUPPMSGMAIN);
3751         if (arcfp == NULL) {
3752                 arcfp = fopen(file_arcq, "ab+");
3753                 chown(file_arcq, CTDLUID, (-1));
3754                 chmod(file_arcq, 0600);
3755         }
3756         end_critical_section(S_SUPPMSGMAIN);
3757
3758         /* msgnum < 0 means that we're trying to close the file */
3759         if (msgnum < 0) {
3760                 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
3761                 begin_critical_section(S_SUPPMSGMAIN);
3762                 if (arcfp != NULL) {
3763                         fclose(arcfp);
3764                         arcfp = NULL;
3765                 }
3766                 end_critical_section(S_SUPPMSGMAIN);
3767                 return;
3768         }
3769
3770         /*
3771          * If we can't open the queue, perform the operation synchronously.
3772          */
3773         if (arcfp == NULL) {
3774                 TDAP_AdjRefCount(msgnum, incr);
3775                 return;
3776         }
3777
3778         new_arcq.arcq_msgnum = msgnum;
3779         new_arcq.arcq_delta = incr;
3780         rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3781         if (rv == -1) {
3782                 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3783                            file_arcq,
3784                            strerror(errno));
3785         }
3786         fflush(arcfp);
3787
3788         return;
3789 }
3790
3791 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3792 {
3793         struct CitContext *CCC = CC;
3794         long i, the_size, offset;
3795         struct arcq *new_arcq;
3796         int rv = 0;
3797
3798         MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
3799
3800         begin_critical_section(S_SUPPMSGMAIN);
3801         if (arcfp == NULL) {
3802                 arcfp = fopen(file_arcq, "ab+");
3803                 chown(file_arcq, CTDLUID, (-1));
3804                 chmod(file_arcq, 0600);
3805         }
3806         end_critical_section(S_SUPPMSGMAIN);
3807
3808         /*
3809          * If we can't open the queue, perform the operation synchronously.
3810          */
3811         if (arcfp == NULL) {
3812                 for (i = 0; i < nmsg; i++)
3813                         TDAP_AdjRefCount(msgnum[i], incr);
3814                 return;
3815         }
3816
3817         the_size = sizeof(struct arcq) * nmsg;
3818         new_arcq = malloc(the_size);
3819         for (i = 0; i < nmsg; i++) {
3820                 new_arcq[i].arcq_msgnum = msgnum[i];
3821                 new_arcq[i].arcq_delta = incr;
3822         }
3823         rv = 0;
3824         offset = 0;
3825         while ((rv >= 0) && (offset < the_size))
3826         {
3827                 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3828                 if (rv == -1) {
3829                         MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3830                                    file_arcq,
3831                                    strerror(errno));
3832                 }
3833                 else {
3834                         offset += rv;
3835                 }
3836         }
3837         free(new_arcq);
3838         fflush(arcfp);
3839
3840         return;
3841 }
3842
3843
3844 /*
3845  * TDAP_ProcessAdjRefCountQueue()
3846  *
3847  * Process the queue of message count adjustments that was created by calls
3848  * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3849  * for each one.  This should be an "off hours" operation.
3850  */
3851 int TDAP_ProcessAdjRefCountQueue(void)
3852 {
3853         struct CitContext *CCC = CC;
3854         char file_arcq_temp[PATH_MAX];
3855         int r;
3856         FILE *fp;
3857         struct arcq arcq_rec;
3858         int num_records_processed = 0;
3859
3860         snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3861
3862         begin_critical_section(S_SUPPMSGMAIN);
3863         if (arcfp != NULL) {
3864                 fclose(arcfp);
3865                 arcfp = NULL;
3866         }
3867
3868         r = link(file_arcq, file_arcq_temp);
3869         if (r != 0) {
3870                 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3871                 end_critical_section(S_SUPPMSGMAIN);
3872                 return(num_records_processed);
3873         }
3874
3875         unlink(file_arcq);
3876         end_critical_section(S_SUPPMSGMAIN);
3877
3878         fp = fopen(file_arcq_temp, "rb");
3879         if (fp == NULL) {
3880                 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3881                 return(num_records_processed);
3882         }
3883
3884         while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3885                 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3886                 ++num_records_processed;
3887         }
3888
3889         fclose(fp);
3890         r = unlink(file_arcq_temp);
3891         if (r != 0) {
3892                 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3893         }
3894
3895         return(num_records_processed);
3896 }
3897
3898
3899
3900 /*
3901  * TDAP_AdjRefCount  -  adjust the reference count for a message.
3902  *                      This one does it "for real" because it's called by
3903  *                      the autopurger function that processes the queue
3904  *                      created by AdjRefCount().   If a message's reference
3905  *                      count becomes zero, we also delete the message from
3906  *                      disk and de-index it.
3907  */
3908 void TDAP_AdjRefCount(long msgnum, int incr)
3909 {
3910         struct CitContext *CCC = CC;
3911
3912         struct MetaData smi;
3913         long delnum;
3914
3915         /* This is a *tight* critical section; please keep it that way, as
3916          * it may get called while nested in other critical sections.  
3917          * Complicating this any further will surely cause deadlock!
3918          */
3919         begin_critical_section(S_SUPPMSGMAIN);
3920         GetMetaData(&smi, msgnum);
3921         smi.meta_refcount += incr;
3922         PutMetaData(&smi);
3923         end_critical_section(S_SUPPMSGMAIN);
3924         MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
3925                    msgnum, incr, smi.meta_refcount
3926                 );
3927
3928         /* If the reference count is now zero, delete the message
3929          * (and its supplementary record as well).
3930          */
3931         if (smi.meta_refcount == 0) {
3932                 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
3933                 
3934                 /* Call delete hooks with NULL room to show it has gone altogether */
3935                 PerformDeleteHooks(NULL, msgnum);
3936
3937                 /* Remove from message base */
3938                 delnum = msgnum;
3939                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3940                 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3941
3942                 /* Remove metadata record */
3943                 delnum = (0L - msgnum);
3944                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3945         }
3946
3947 }
3948
3949 /*
3950  * Write a generic object to this room
3951  *
3952  * Note: this could be much more efficient.  Right now we use two temporary
3953  * files, and still pull the message into memory as with all others.
3954  */
3955 void CtdlWriteObject(char *req_room,                    /* Room to stuff it in */
3956                      char *content_type,                /* MIME type of this object */
3957                      char *raw_message,         /* Data to be written */
3958                      off_t raw_length,          /* Size of raw_message */
3959                      struct ctdluser *is_mailbox,       /* Mailbox room? */
3960                      int is_binary,                     /* Is encoding necessary? */
3961                      int is_unique,                     /* Del others of this type? */
3962                      unsigned int flags         /* Internal save flags */
3963         )
3964 {
3965         struct CitContext *CCC = CC;
3966         struct ctdlroom qrbuf;
3967         char roomname[ROOMNAMELEN];
3968         struct CtdlMessage *msg;
3969         StrBuf *encoded_message = NULL;
3970
3971         if (is_mailbox != NULL) {
3972                 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3973         }
3974         else {
3975                 safestrncpy(roomname, req_room, sizeof(roomname));
3976         }
3977
3978         MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
3979
3980         if (is_binary) {
3981                 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3982         }
3983         else {
3984                 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3985         }
3986
3987         StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3988         StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3989         StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3990
3991         if (is_binary) {
3992                 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3993         }
3994         else {
3995                 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3996         }
3997
3998         if (is_binary) {
3999                 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
4000         }
4001         else {
4002                 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
4003         }
4004
4005         MSGM_syslog(LOG_DEBUG, "Allocating\n");
4006         msg = malloc(sizeof(struct CtdlMessage));
4007         memset(msg, 0, sizeof(struct CtdlMessage));
4008         msg->cm_magic = CTDLMESSAGE_MAGIC;
4009         msg->cm_anon_type = MES_NORMAL;
4010         msg->cm_format_type = 4;
4011         CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
4012         CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
4013         CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
4014         CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
4015         msg->cm_flags = flags;
4016         
4017         CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
4018
4019         /* Create the requested room if we have to. */
4020         if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4021                 CtdlCreateRoom(roomname, 
4022                                ( (is_mailbox != NULL) ? 5 : 3 ),
4023                                "", 0, 1, 0, VIEW_BBS);
4024         }
4025         /* If the caller specified this object as unique, delete all
4026          * other objects of this type that are currently in the room.
4027          */
4028         if (is_unique) {
4029                 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4030                            CtdlDeleteMessages(roomname, NULL, 0, content_type)
4031                         );
4032         }
4033         /* Now write the data */
4034         CtdlSubmitMsg(msg, NULL, roomname, 0);
4035         CM_Free(msg);
4036 }
4037
4038
4039
4040 /*****************************************************************************/
4041 /*                      MODULE INITIALIZATION STUFF                          */
4042 /*****************************************************************************/
4043 void SetMessageDebugEnabled(const int n)
4044 {
4045         MessageDebugEnabled = n;
4046 }
4047 CTDL_MODULE_INIT(msgbase)
4048 {
4049         if (!threading) {
4050                 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4051         }
4052
4053         /* return our Subversion id for the Log */
4054         return "msgbase";
4055 }