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