/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */

#include "am.h"
#include "server.h"

/* read in the mailbox of an user */
int ReadContents(char *folder,struct List *Mailbox,struct Strings *Strings)
{
	BPTR contents;
	char s[MAIL_FIELDLENGTH];
	struct ContentsItem Actual;
	struct Mail *New;

	strcpy(s,folder);
	strcat(s,"/contents");
	
	if ((contents = Open((UBYTE *)s,MODE_OLDFILE)))
	{
		if (!ReadStrings(contents,Strings))
		{
			writelog("readcontents: couldn't read strings");
			Close(contents);
			return(FALSE);
		}

		while (FRead(contents,(APTR)&Actual,1,sizeof(struct ContentsItem))
			==sizeof(struct ContentsItem))
		{
			/* allocate space */
			if (!(New = (struct Mail *)
				AllocMem(sizeof(struct Mail),MEMF_CLEAR)))
			{
				writelog("readcontents: out of memory");
				Close(contents);
				return(FALSE);
			}

			New->Number = Actual.Number;
			New->Date = Actual.Date;
			New->read = Actual.read;
			New->DisplayText[0]=0;
			New->m_Node.ln_Type = AMNT_MAILBOX;
			New->m_SelectNode.ln_Name = New->DisplayText;
			New->From = GetString(Actual.FromNumber,Strings);
			New->To = GetString(Actual.ToNumber,Strings);
			New->Cc = GetString(Actual.CcNumber,Strings);
			New->ReplyTo = GetString(Actual.ReplyToNumber,Strings);
			New->Subject = GetString(Actual.SubjectNumber,Strings);
			New->InReplyTo = GetString(Actual.InReplyToNumber,Strings);
			New->MsgId = GetString(Actual.MsgIdNumber,Strings);

			/* put into list */
			AddHead(Mailbox,&(New->m_Node));
		}
	
		Close(contents);
	}
	return(TRUE);
	
}
