  uses dos,crt,openfiles;

  type

  MessageRec = Record
                Author   : String[30];
                Status   : Byte; { Bit 1 - Private } { Bit 2 - Anonymous }
                Position : LongInt;
                Numlines : integer;
                ToField  : String[30];
                UserNote : String[50];
                DateTime : string[8];
                Subject  : string[30];
               End;

               type msgbod  = record
                       text : string[80];
                    end;

               type msgbuff = array[1..101] of msgbod;

procedure post_msg(fn:String;idx:messagerec;mb:msgbuff);
var idxf : file of messagerec; { message index }
    msgf : file of msgbod;  { message body file }
    ctr  : integer;
    str  : string[80];
begin
assign(idxf,fn+'.NDX');
{$i-} reset(idxf); {$i+}
if ioresult<>0 then rewrite(idxf);
seek(idxf,filesize(idxf));

assign(msgf,fn+'.BOD');
{$i-} reset(msgf); {$i+}
if ioresult<>0 then rewrite(msgf);
idx.position:=filesize(msgf);

write(idxf,idx);
close(idxf);
seek(msgf,filesize(msgf));
ctr:=0;
repeat
inc(ctr);
write(msgf,mb[ctr]);
until ctr=idx.numlines;
close(msgf);
end;

procedure read_msg(fn:string; i:longint);
type str80=string[80];
var idxf : file of messagerec; { message index }
    msgf : file {of str80};     { message body file }
    idx  : messagerec;
    lc   : integer;
    liner: str80;
begin
assign(idxf,fn+'.NDX');
{$i-} reset(idxf); {$i+}
if ioresult<>0 then rewrite(idxf);

if i = 0 then begin
                   close(idxf);
                   exit;
                   end;

if filesize(idxf) =0 then begin
                         close(idxf);
                         exit;
                         end;

if i > filesize(idxf) then begin
                             close(idxf);
                             exit;
                             end;
seek(idxf,i-1);
read(idxf,idx);

assign(msgf,fn+'.BOD');
{$i-} reset(msgf,1); {$i+}

if ioresult<>0 then begin;
                    rewrite(msgf,1);
                    close(msgf);
                    exit;
                    end;
seek(msgf,idx.position);
writeln('message from ',idx.author);
writeln('message to   ',idx.tofield);
writeln('position     ',idx.position);
for lc:=1 to idx.numlines do begin
                             fillchar(liner,sizeof(liner),#0);
                             blockread(msgf,liner,sizeof(liner));
                             write(lc);
                             writeln(liner);
                             end;
close(msgf);
close(idxf);
writeln('---- > ');
readkey;
end;

procedure local_upload_msg(fn:string; ndx:messagerec; ul:string);
var idxf : file of messagerec; { message index }
    msgf : file of msgbod;     { message body file }
    ulf  : text;
    maxl : integer;
    line : msgbod;
    ctr  : integer;
begin
assign(idxf,fn+'.NDX');
{$i-} reset(idxf); {$i+}
if ioresult<>0 then rewrite(idxf);

assign(ulf,ul);
{$i-} reset(ulf); {$i+}
if ioresult<>0 then begin
                    close(idxf);
                    exit;
                    end;

assign(msgf,fn+'.BOD');
{$i-} reset(msgf); {$i+}
if ioresult<>0 then rewrite(msgf);

seek(msgf,filesize(msgf));

ctr:=0;
ndx.position:=filesize(msgf);

repeat
fillchar(line,80,#0);
readln(ulf,line.text);
textattr:=7;
write(msgf,line);
inc(ctr);
until eof(ulf);

ndx.numlines:=ctr;
seek(idxf,filesize(idxf));
write(idxf,ndx);
close(idxf);
close(ulf);
close(msgf);
end;


var ndx:messagerec;
    mb :msgbuff;
    f  :file;
begin
fillchar(ndx,sizeof(ndx),#0);
ndx.author:='Sudden Death';
ndx.status:=1;
ndx.position:=0;
ndx.numlines:=0;
ndx.tofield:='Judge Dredd';
ndx.Usernote:='Duh';
ndx.datetime:='03/20/80';
ndx.subject:='New Message Shit';
ndx.numlines:=3;
fillchar(mb,sizeof(mb),#0);
MB[1].TEXT:='d';
MB[2].TEXT:='e';
mb[3].text:='f';
{post_msg(fn:String;idx:messagerec;mb:msgbuff);}
{POST_MSG('AREA1',NDX,MB);
{read_msg('AREA1',1);}
read_msg('AREA1',1);{
read_msg('AREA1',3);}
readkey;

end.