/* ======================================================================== */
/* ========================  MAILBOX  ROUTINES  =========================== */
/* ======================================================================== */

struct hdrstruc {
       unsigned char msg_no[8];
       unsigned char msg_ndx[8];
       unsigned char msg_len[8];
       unsigned char date[20];
       unsigned char flags[4];
       unsigned char from[20];
       unsigned char to[20];
       unsigned char title[30];
};
struct hdrstruc msghdr;

int cur_msgno;
long cur_ndx;

int hdrf,msgf;

char itoastr[20];

char cr_lf[2]={13,10};


mbox()
{
       unsigned char mstr[81];
       int i,c,exists,youhavemail;

       textattr(MIDBARCOLOR);
       gotoxy(3,20);
       cprintf("MAILBOX MODE  Sysop Cmds: B,K,L,P,R,S");
       textattr(RCVCOLOR);

       exists=0;

       if((hdrf = _open("MSGS.HDR",O_RDWR|O_TEXT)) == -1)
               _creat("MSGS.HDR",0);
       else {
               exists=1;
               _close(hdrf);
       }
       if((msgf = _open("MSGS.TXT",O_RDWR|O_TEXT)) == -1)
               _creat("MSGS.TXT",0);
       else
               _close(msgf);

       hdrf = _open("MSGS.HDR",O_RDWR|O_TEXT);
       msgf = _open("MSGS.TXT",O_RDWR|O_TEXT);

       if(exists) {
               cur_ndx = lseek(msgf,0l,SEEK_END);

               if(filelength(hdrf) != 0l) {
                       lseek(hdrf,-118l,SEEK_END);
                       _read(hdrf,&msghdr,118);
                       cur_msgno = atoi(msghdr.msg_no)-1;
                       cur_msgno++;
               }
               else
                       cur_msgno = 0;
       }
       else {
               cur_ndx = 0l;
               cur_msgno=0;
       }

       while(1)
       {
               strcpy(scanstr,"                              ");
               while(! connected()) {
                       chk_time();
                       if(kbhit()) {
                               c = getch();
                               if(c == 0) {
                                       c = getch();
                                       switch(c){
                                               case 0x47:      scrsav();
                                                                       break;
                                       }
                               }
                               else {
                                       switch(toupper(c)){
                                               case 'S':       sysop_enter_msg();
                                                                       break;
                                               case 'L':       sysop_list_msgs();
                                                                       break;
                                               case 'R':       sysop_read_msg();
                                                                       break;
                                               case 'K':   sysop_kill_msg();
                                                                       break;
                                               case 'P':   purge_msgs();
                                                                       break;
                                               case 'B' :      goto exit_mbox;
                                       }
                               }
                       }
               }

               youhavemail = FALSE;
               lseek(hdrf,0l,SEEK_SET);
               for(i=0;i<cur_msgno;i++) {
                       _read(hdrf,&msghdr,118);
                       if((strstr(msghdr.to,mbox_cs)) &&
                               (! strchr(msghdr.flags,'Y')) )
                       {
                               youhavemail = TRUE;
                               break;
                       }
               }

               comm_send('');
               send_string("<<< Welcome to SM5SXL Mats in Norrkoping JO88CO >>>",1);
               send_string("",1);
               send_string("I'm not here at the moment, so Pse leave a message.",1);
               send_string("",1);
               send_string("Available Commands:",1);
               send_string("B    Bye (Logout)",1);
               send_string("K #  Kill msg number #",1);
               send_string("L    List msgs",1);
               send_string("R #  Read msg number #",1);
               send_string("S    Send msg to SM5SXL",1);

               if(youhavemail) {
                       send_string("",1);
                       send_string("You have Mail!",1);
               }

               while(1) {
                       send_string("",1);
                       send_string("Enter Command (B,K,L,R,S) >",0);
                       waitfor_string("S) >");
                       comm_send('');
                       if(! rcv_string(mstr)) goto abort_user;
                       strcpy(mstr,strupr(mstr));
                       i=0;
                       while(mstr[i] <= 0x20)
                               i++;
                       switch(mstr[i]) {
                               case 'S':       if(! enter_msg()) goto abort_user;
                                                       break;
                               case 'L':       list_msgs();
                                                       break;
                               case 'R':   read_msg(mstr,i);
                                                       break;
                               case 'K':   kill_msg(mstr,i);
                                                       break;
                               case 'B':       goto bye;
                       }
               }
bye:
               comm_send('');
               send_string("",1);
               send_string("Bye bye...",1);
               waitfor_string("..\015\012");
               comm_send(0x03);
               delay(200);
               send_string("D",1);
abort_user:
               ;
       }
exit_mbox:
       _close(hdrf);
       _close(msgf);
       textattr(MIDBARCOLOR);
       gotoxy(3,20);
       cprintf("                                     ");
       dirwr(' ',50,1,MAILCOLOR);
       textattr(XMITCOLOR);
}

connected()
{
       if((comm_in = comm_rcv()) != 0) {
               in = comm_in & 0xff;
               out_aux_char();
               strcpy(scanstr, &scanstr[1]);
               scanstr[29] = in;

               if(strstr(scanstr,"*** CONNECTED to ")) {
                       rcv_string(mbox_cs);
                       return 1;
               }
       }
       return 0;
}

enter_msg()
{
       int msglen;
       unsigned char msgstr[81];

       strcpy(msghdr.msg_no,itoa(cur_msgno+1,itoastr,10));
       strcpy(msghdr.msg_ndx,ltoa(cur_ndx,itoastr,10));
       strcpy(msghdr.date,utcstr);
       strcpy(msghdr.flags,"N");
       strcpy(msghdr.from,mbox_cs);
       strcpy(msghdr.to,"SM5SXL");

       comm_send('');
       send_string("",1);
       send_string("Title: ",0);
       waitfor_string("le: ");
       comm_send('');
       if(! rcv_string(msghdr.title)) goto error;
       comm_send('');
       send_string("Enter message text. End with /EX on a new line.",1);
       waitfor_string("e.\015\012");
       comm_send('');

       lseek(msgf,cur_ndx,SEEK_SET);
       msglen=0;

       while(1){
               if(! rcv_string(msgstr))
                       goto error;
               _write(msgf,msgstr,strlen(msgstr));
               _write(msgf,cr_lf,2);
               cur_ndx += (long)(strlen(msgstr)+2);
               msglen += (strlen(msgstr)+2);
               if(strstr(strupr(msgstr),"/EX")) break;
       }

       strcpy(msghdr.msg_len,itoa(msglen,itoastr,10));

       lseek(hdrf,(long)(cur_msgno*118),SEEK_SET);
       _write(hdrf,&msghdr,118);
       cur_msgno++;

       comm_send('');
       send_string("Message stored OK.",1);
       dirwr('M',50,1,MAILCOLOR);
       return 1;
error:
       return 0;
}


list_msgs()
{
       int i;
       unsigned char tmp[81];

       comm_send('');
       lseek(hdrf,0l,SEEK_SET);
       send_string("",1);
       send_string(   "Msg No  Date   Time       Flags   From      To        Title",1);
       send_string("",1);

       for(i=0;i<cur_msgno;i++) {
               _read(hdrf,&msghdr,118);
               if(! strchr(msghdr.flags,'K')) {
                       sprintf(tmp,"%-8.8s%-18.18s%-8.8s%-10.10s%-10.10s%-15.15s",
                                                               msghdr.msg_no,msghdr.date,msghdr.flags,
                                                               msghdr.from,msghdr.to,msghdr.title);
                       send_string(tmp,1);
               }
       }
}


read_msg(char *s,int p)
{
       int readno;
       int i,len,out;

       comm_send('');

       if(!(readno = atoi(&s[p+1])))
               goto read_err;
       lseek(hdrf,(long)((readno-1)*118),SEEK_SET);
       _read(hdrf,&msghdr,118);

       send_string("",1);
       send_string("Msg # : ",0);
       send_string(msghdr.msg_no,1);
       send_string("From  : ",0);
       send_string(msghdr.from,1);
       send_string("To    : ",0);
       send_string(msghdr.to,1);
       send_string("Title : ",0);
       send_string(msghdr.title,1);
       send_string("",1);

       lseek(msgf,atol(msghdr.msg_ndx),SEEK_SET);
       len = atoi(msghdr.msg_len);

       for(i=0;i<len;i++) {
               _read(msgf,&out,1);
               if(out != 0x0A) {
                       chk_flow();
                       comm_send(out);
               }
               out_con_char(out);

               if((comm_in = comm_rcv()) != 0)
               {
                       in = comm_in & 0xff;
                       out_aux_char();
               }
       }

       if( (strstr(msghdr.to,mbox_cs)) &&
               (strchr(msghdr.flags,'N')))
       {
               strcpy(msghdr.flags,"Y");
               lseek(hdrf,-118l,SEEK_CUR);
               _write(hdrf,&msghdr,118);
       }
       return 1;
read_err:
       send_string("",1);
       send_string("Invalid message number!",1);
       return 0;
}

kill_msg(char *s,int p)
{
       int killno;

       comm_send('');

       if(!(killno = atoi(&s[p+1])))
               goto kill_err;
       lseek(hdrf,(long)((killno-1)*118),SEEK_SET);
       _read(hdrf,&msghdr,118);

       strcpy(msghdr.flags,"K");
       lseek(hdrf,-118l,SEEK_CUR);
       _write(hdrf,&msghdr,118);

       send_string("",1);
       send_string("Message number ",0);
       send_string(msghdr.msg_no,0);
       send_string(" Killed.",1);
       return 1;
kill_err:
       send_string("",1);
       send_string("Invalid message number!",1);
       return 0;
}

sysop_enter_msg()
{
       unsigned char msgstr[81];
       int msglen;

       strcpy(msghdr.msg_no,itoa(cur_msgno+1,itoastr,10));
       strcpy(msghdr.msg_ndx,ltoa(cur_ndx,itoastr,10));
       strcpy(msghdr.date,utcstr);
       strcpy(msghdr.flags,"N");
       strcpy(msghdr.from,"SM5SXL");
       print_string("",1);
       print_string("To   : ",0);
       get_string(msghdr.to);
       strcpy(msghdr.to,strupr(msghdr.to));
       print_string("",1);
       print_string("Title: ",0);
       get_string(msghdr.title);
       print_string("",1);
       print_string("Enter message text. End with /ex on a new line.\n",1);

       lseek(msgf,cur_ndx,SEEK_SET);
       msglen=0;

       while(1)
       {
               get_string(msgstr);
               _write(msgf,msgstr,strlen(msgstr));
               _write(msgf,cr_lf,2);
               cur_ndx += (long)(strlen(msgstr)+2);
               msglen += (strlen(msgstr)+2);
               if(strstr(strupr(msgstr),"/EX")) break;
       }

       strcpy(msghdr.msg_len,itoa(msglen,itoastr,10));

       lseek(hdrf,(long)(cur_msgno*118),SEEK_SET);
       _write(hdrf,&msghdr,118);
       cur_msgno++;

       print_string("Message stored OK.",1);
       print_string("",1);
       return 1;
}


sysop_list_msgs()
{
       int i;
       unsigned char tmp[81];

       lseek(hdrf,0l,SEEK_SET);
       print_string("",1);
       print_string(   "Msg No  Date   Time       Flags   From      To        Title",1);
       print_string("",1);

       for(i=0;i<cur_msgno;i++)
       {
               _read(hdrf,&msghdr,118);
               if(! strchr(msghdr.flags,'K')) {
                       sprintf(tmp,"%-8.8s%-18.18s%-8.8s%-10.10s%-10.10s%-15.15s",
                                                                       msghdr.msg_no,msghdr.date,msghdr.flags,
                                                                       msghdr.from,msghdr.to,msghdr.title);
                       print_string(tmp,1);
               }
       }
}

sysop_read_msg()
{
       char readno[4];
       int i,len;

       print_string("",1);
       print_string("Number of message to read: ",0);
       get_string(readno);
       if(! atoi(readno))
               goto sysread_err;

       lseek(hdrf,(long)((atoi(readno)-1)*118),SEEK_SET);
       _read(hdrf,&msghdr,118);

       print_string("",1);
       print_string("Msg # : ",0);
       print_string(msghdr.msg_no,1);
       print_string("From  : ",0);
       print_string(msghdr.from,1);
       print_string("To    : ",0);
       print_string(msghdr.to,1);
       print_string("Title : ",0);
       print_string(msghdr.title,1);
       print_string("",1);

       lseek(msgf,atol(msghdr.msg_ndx),SEEK_SET);
       len = atoi(msghdr.msg_len);

       for(i=0;i<len;i++)
       {
               _read(msgf,&in,1);
               out_aux_char();
       }

       if( (strstr(msghdr.to,"SM5SXL")) &&
               (strchr(msghdr.flags,'N')))
       {
               strcpy(msghdr.flags,"Y");
               lseek(hdrf,-118l,SEEK_CUR);
               _write(hdrf,&msghdr,118);
       }
       return 1;
sysread_err:
       print_string("",1);
       print_string("Invalid message number!",1);
       return 0;
}

sysop_kill_msg()
{
       unsigned char killno[10];

       print_string("",1);
       print_string("Number of message to Kill: ",0);
       get_string(killno);
       if(! atoi(killno))
               goto syskill_err;

       lseek(hdrf,(long)((atoi(killno)-1)*118),SEEK_SET);
       _read(hdrf,&msghdr,118);

       strcpy(msghdr.flags,"K");
       lseek(hdrf,-118l,SEEK_CUR);
       _write(hdrf,&msghdr,118);

       print_string("",1);
       print_string("Message number ",0);
       print_string(msghdr.msg_no,0);
       print_string(" Killed.",1);
       print_string("",1);
       return 1;
syskill_err:
       print_string("",1);
       print_string("Invalid message number!",1);
       return 0;
}

purge_msgs()
{
       int newmsgf,newhdrf;
       unsigned int i,j,newmsgno,len;
       long newndx;

       _creat("_MSGS.HDR",0);
       newhdrf = _open("_MSGS.HDR",O_WRONLY|O_TEXT);
       _creat("_MSGS.TXT",0);
       newmsgf = _open("_MSGS.TXT",O_WRONLY|O_TEXT);

       lseek(hdrf,0l,SEEK_SET);
       newmsgno=0;
       newndx=0l;

       for(i=0;i<cur_msgno;i++){
               _read(hdrf,&msghdr,118);

               if( (! strchr(msghdr.flags,'K'))  &&
                       (! strchr(msghdr.flags,'Y')) )
               {
                       itoa(newmsgno+1,msghdr.msg_no,10);
                       newmsgno++;

                       lseek(msgf,atol(msghdr.msg_ndx),SEEK_SET);
                       len = atoi(msghdr.msg_len);

                       for(j = 0;j < len;j++) {
                               _read(msgf,&in,1);
                               _write(newmsgf,&in,1);
                       }

                       ltoa(newndx,msghdr.msg_ndx,10);
                       newndx+=(long)len;
                       _write(newhdrf,&msghdr,118);
               }
       }

       _close(hdrf);
       _close(msgf);
       _close(newmsgf);
       _close(newhdrf);
       remove("MSGS.HDR");
       remove("MSGS.TXT");
       rename("_MSGS.HDR","MSGS.HDR");
       rename("_MSGS.TXT","MSGS.TXT");

       hdrf = _open("MSGS.HDR",O_RDWR|O_TEXT);
       msgf = _open("MSGS.TXT",O_RDWR|O_TEXT);

       if(filelength(msgf) != 0l)
               cur_ndx = lseek(msgf,0l,SEEK_END);
       else
               cur_ndx = 0l;

       if(filelength(hdrf) != 0l)
       {
               lseek(hdrf,-118l,SEEK_END);
               _read(hdrf,&msghdr,118);
               cur_msgno = atoi(msghdr.msg_no)-1;
               cur_msgno++;
       }
       else
               cur_msgno = 0;
}

/* ==================================================================== */
/* ===================  END OF MAILBOX ROUTINES  ====================== */
/* ==================================================================== */