void readmail()
{
  int i,i1,i2,i3,f,mw,mloc[200],mfl,curmail,done,abort,next,okmail;
  unsigned short x,xx;
  char s[81],s1[81],s2[81],fn[81],*b;
  mailrec m;
  slrec ss;
  userrec u;
  char ch;
  long len,num_mail,num_mail1;

  ss=syscfg.sl[thisuser.sl];
  sprintf(fn,"%sEMAIL.DAT",syscfg.datadir);
  f=open(fn,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  if (f<0) {
    nl();
    nl();
    pl("No mail file exists!");
    nl();
    return;
  }
  mfl=filelength(f)/sizeof(mailrec);
  mw=0;
  for (i=0; (i<mfl) && (mw<200); i++) {
    lseek(f,((long) (i)) * (sizeof(mailrec)), SEEK_SET);
    read(f,(void *)(&m),sizeof(mailrec));
    if ((m.tosys==0) && (m.touser==usernum))
      mloc[mw++]=i;
  }
  thisuser.waiting=mw;
  if (usernum==1)
    fwaiting=mw;
  if (mw==0) {
    nl();
    nl();
    pl("You have no mail.");
    nl();
    return;
  }
  if (mw==1)
    curmail=0;
  else {
    nl();
    nl();
    pl("You have mail from:");
    nl();
    for (i=0; i<mw; i++) {
      lseek(f,((long) mloc[i]) * sizeof(mailrec),SEEK_SET);
      read(f,(void *) (&m),sizeof(mailrec));
      itoa(i+1,s,10);
      strcat(s,". ");
      if ((m.anony & anony_sender) && ((ss.ability & ability_read_email_anony)==0)) {
        strcat(s,">UNKNOWN<");
      } else {
        if (m.fromsys==0) {
	  if (m.fromuser==65535)
	    strcat(s,"WWIVnet");
	  else {
            read_user(m.fromuser,&u);
            strcat(s,nam(&u,m.fromuser));
	  }
        } else {
          sprintf(s1,"User %u @%u",m.fromuser,m.fromsys);
          strcat(s,s1);
        }
      }
      pl(s);
    }
    nl();
    helpl=10;
    pl("Hit return, or enter number");
    outstr(":");
    input(s,3);
    if (strcmp(s,"Q")==0) {
      close(f);
      return;
    }
    i=atoi(s);
    if (i)
      if (i<=mw)
        curmail=i-1;
      else
        curmail=0;
    else
      curmail=0;
  }
  done=0;
  do {
    sprintf(s,"(%u/%u): ",curmail+1,mw);
    abort=0;
    nl();
    nl();
    osan(s,&abort,&next);
    next=0;
    ansic(MSG_COLOR);
    s[0]=0;
    if (mloc[curmail]<0) {
      strcat(s,">>> MAIL DELETED <<<");
      okmail=0;
      pl(s);
      nl();
      nl();
    } else {
      lseek(f,((long) (mloc[curmail])) * (sizeof(mailrec)), SEEK_SET);
      read(f,(void *)&m,sizeof(mailrec));
      if ((m.tosys!=0) || (m.touser!=usernum)) {
	mloc[curmail]=-1;
        strcat(s,">>> MAIL DELETED <<<");
        okmail=0;
        pl(s);
        nl();
        nl();
      } else {
        strcat(s,m.title);
        strcpy(irt,m.title);
        abort=0;
        i=((ability_read_email_anony & ss.ability)!=0);
        okmail=1;
        pla(s,&abort);
        if ((m.fromsys) && (!m.fromuser))
          grab_user_name(&(m.msg),"EMAIL");
        else
          net_email_name[0]=0;
        setorigin(m.fromsys);
        if (!abort)
          read_message1(&m.msg, (m.anony & 0x0f), i, &next, "EMAIL");
      }
    }
    do {
      i2=1;
      prt(2,"Mail {?} : ");
      helpl=34;
      if (!okmail)
	ch=onek("QI?-+G");
      else
        if (so())
          ch=onek("QSRIDAF?-+GZVUEO");
        else
          if (cs())
            ch=onek("QSRIDAF?-+GZVUO");
          else
            ch=onek("QSRIDAF?+-G");
      switch (ch) {
	case 'E':
          if ((so()) && (okmail)) {
            b=readfile(&(m.msg),"EMAIL",&len);
            extract_out(b,len);
          }
          i2=0;
	  break;
        case 'Q':
          done=1;
          break;
	case 'O':
	  if ((cs()) && (okmail) && (m.fromuser!=65535)) {
	    nl();
	    prt(2,"Which form letter? ");
            input(s,4);
            sprintf(s1,"%sFORM%s.MSG",syscfg.gfilesdir,s);
	    if (exist(s1)) {
              strcpy(s,nam(&thisuser,usernum));
	      if (m.anony & anony_receiver)
	        strcpy(s,">UNKNOWN<");
              strcat(s," read your mail on ");
              strcat(s,date());
	      if (m.fromsys==0)
                ssm(m.fromuser,m.fromsys,s);
              delmail(f,mloc[curmail]);
              mloc[curmail]=-1;
              ++curmail;
              if (curmail>=mw)
                done=1;
              if (!wfc)
                topscreen();
              close(f);
              load_workspace(s1,1);
              email(m.fromuser,m.fromsys,0,m.anony);
              f=open(fn,O_RDWR | O_BINARY);
	    } else {
	      nl();
	      pl("File not found.");
	      nl();
              i2=0;
	    }
	  }
	  break;
	case 'G':
          sprintf(s,"Go to which (1-%u) ? ",mw);
	  prt(2,s);
	  input(s,3);
	  i2=atoi(s);
	  if ((i2>0) && (i2<=mw)) {
	    curmail=i2-1;
	    i2=1;
	  } else
	    i2=0;
	  break;
        case 'I':
	case '+':
          ++curmail;
          if (curmail>=mw)
            done=1;
          break;
	case '-':
	  if (curmail)
            --curmail;
	  break;
        case 'R':
          break;
        case '?':
          printmenu(4);
          i2=0;
          break;
        case 'D':
	  if (!okmail)
	    break;
          strcpy(s,nam(&thisuser,usernum));
	  if (m.anony & anony_receiver)
	    strcpy(s,">UNKNOWN<");
          strcat(s," read your mail on ");
          strcat(s,date());
	  if ((m.fromsys==0) && (m.fromuser!=65535))
            ssm(m.fromuser,m.fromsys,s);
        case 'Z':
	  if (!okmail)
	    break;
          delmail(f,mloc[curmail]);
          mloc[curmail]=-1;
          ++curmail;
          if (curmail>=mw)
            done=1;
          if (!wfc)
            topscreen();
          break;
        case 'F':
	  if (m.status & status_multimail) {
	    nl();
	    pl("Can't forward multimail.");
	    nl();
	    break;
	  }
	  if (!okmail)
	    break;
          nl();
          nl();
          prt(2,"Forward to: ");
          input(s,30);
          x=finduser(s);
          if ((x==usernum) && (!cs())) {
            x=0;
            nl();
            pl("Can't forward to yourself.");
            nl();
          }
          xx=0;
          if ((x) && (forwardm(&x,&xx))) {
            nl();
            if (x)
              pl("Forwarded from there.");
            else
              pl("Can't forward to him.");
          }
	  if (x>0) {
	    read_user(x,&u);
            sprintf(s,"Forward to %s? ",nam(&u,x));
	    prt(5,s);
	    if (!yn())
	      x=0;
	  }
          if (x>0) {
            --thisuser.waiting;
            if (usernum==1)
              --fwaiting;
            m.touser=x;
            lseek(f,((long) (mloc[curmail])) * (sizeof(mailrec)), SEEK_SET);
            write(f,(void *)&m,sizeof(mailrec));
            read_user(x,&u);
            ++u.waiting;
            write_user(x,&u);
            if (x==1)
              ++fwaiting;
            strcpy(s,"\r\nForwarded from: ");
            strcat(s,nam(&thisuser,usernum));
            lineadd(&m.msg,s,"EMAIL");
            strcpy(s,nam(&thisuser,usernum));
            strcat(s," forwarded your mail to ");
            strcat(s,nam(&u,x));
	    if (m.fromsys==0)
              ssm(m.fromuser,m.fromsys,s);
            mloc[curmail]=-1;
            ++curmail;
            if (curmail>=mw)
              done=1;
            if (!wfc)
              topscreen();
            print("Forwarded to ",nam(&u,x),"");
          } else {
            nl();
            pl("Unknown user.");
            nl();
          }
          break;
        case 'A':
        case 'S':
	  if (!okmail)
	    break;
          close(f);
          num_mail=((long) thisuser.feedbacksent) +
                   ((long) thisuser.emailsent) +
                   ((long) thisuser.emailnet);
	  if (m.fromuser!=65535)
            email(m.fromuser,m.fromsys,0,m.anony);
          f=open(fn,O_RDWR | O_BINARY);
          num_mail1=((long) thisuser.feedbacksent) +
                    ((long) thisuser.emailsent) +
                    ((long) thisuser.emailnet);
          if (ch=='A') {
            if (num_mail!=num_mail1) {
              strcpy(s,nam(&thisuser,usernum));
              if (m.anony & anony_receiver)
                strcpy(s,">UNKNOWN<");
              strcat(s," read your mail on ");
              strcat(s,date());
              if ((m.fromsys==0) && (m.fromuser!=65535))
                ssm(m.fromuser,m.fromsys,s);
              delmail(f,mloc[curmail]);
              mloc[curmail]=-1;
              ++curmail;
              if (curmail>=mw)
                done=1;
              if (!wfc)
                topscreen();
            } else {
              nl();
              pl("No mail sent.");
              nl();
              i2=0;
            }
          } else {
            ++curmail;
            if (curmail>=mw)
              done=1;
            if (!wfc)
              topscreen();
          }
          break;
        case 'U':
        case 'V':
	  if (!okmail)
	    break;
          if ((m.fromsys==0) && (cs()) && (m.fromuser!=65535))
            if (ch=='V')
              valuser(m.fromuser);
            else
              uedit(m.fromuser,0);
	  else
	    if (cs()) {
	      nl();
	      pl("Mail from another system.");
	      nl();
	    }
          i2=0;
          break;
      }
    } while ((!i2) && (!hangup));
  } while ((!hangup) && (!done));
  close(f);
}
