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