char *MAILDIR = "H:\\MAIL\\";
char *ROOTDIR = "R:\\";
char *MAILFILES = "H:\\MAIL\\*.MSG";
char *INFOFILE = "R:\\MAILSERV\\INFO.DAT";
char *BADFILE  = "R:\\MAILSERV\\BAD.DAT";

char MAXMES = 50;
char WARNDIF = 10;
char *SUPERUSER = "MUD";
#define MAILVER "@05@Ha@0C@pp@0F@y @07@Mail@02@ ver 2.04b!"
#define MAILVER2 " @0F@Compil@07@ed on @0A@"
#define ALLPASS "\1\1\1\1"
#define Toff 0x2FF000000L
typedef struct {
        char sender[9];
        char reciever[9];
        struct date msgdate;
        struct time msgtime;
        char replied;
        char carbon;
        char forward;
        char forwarder[9];
        char subject[50];
    } mhdr;
mhdr msghdr;

char exist(char *filen) {
    return(access(filen,0) == 0);
    }


int sorter(const void *a, const void *b) {
    return(stricmp((char *) a , (char *) b) * -1);
    }
int sorter2(const void *a, const void *b) {
    return(stricmp((char *) a , (char *) b));
    }
void snewln(char *str) {
    char x = 0;
    while(str[x]) x++;
    if(str[x-1] == '\n' || str[x-1] == '\r') str[x-1] = 0;
    if(str[x-2] == '\n' || str[x-2] == '\r') str[x-2] = 0;
    }
void copy_file(char *infile, char *outfile) {
    char c;
    FILE *i, *o;
    copy_file_top:
    i = fopen(infile,"rb");
    o = fopen(outfile,"wb");
    while((c = getc(i)) != EOF)
                        putc(c,o);
    fclose(i);
    fclose(o);
    if(!exist(outfile)) {
        disp("File Not Copied, Retrying...");
        if(kbhit()) if(getch() == 27) return;
        goto copy_file_top;
        }
    }
void move_file(char *infile,char *outfile) {
    copy_file(infile,outfile);
    remove(infile);
    }
char baduser(char *usrname) {
    FILE *p;
    char bad = 0;
    char buffer[15];
    p = fopen(BADFILE,"rb");
    while(fgets(buffer,11,p)) {
        snewln(buffer);
        if(stricmp(buffer,usrname) == 0) bad = 1;
        if(bad) break;
        }
    fclose(p);
    return(bad);
    }

char getusr(char *usrname) {
     FILE *fp;
     char str[100],str2[100];
     int s;
     sprintf(str,"%susr.dat",MAILDIR);
     if (!exist(str)) return -1;
     fp = fopen(str,"r");
     fgets(str,10,fp);
     fclose(fp);
     snewln(str);
     s = 0;
     while(str[s] != ' ' && str[s] != 0) s++;
     str[s] = 0;
     sprintf(str2,"%s%s\\tempmchk.mda",ROOTDIR,str);
     fp = fopen(str2,"w");
     fprintf(fp,"delme");
     fclose(fp);
     if (!exist(str2)) return 0;
     remove(str2);
     strcpy(usrname,str);
     if(baduser(usrname)) {
                clrscr();
                textcolor(4);
                cprintf("Sorry, %s, your mail privileges have been temporarily revoked...",usrname);
                getch();
                exit(1);
                }
     return 1;
}
char chkusr(char *usrname) {
   char str[100];
   if(baduser(usrname)) {
                textcolor(15);
                cprintf("\n\rSorry, user %s\'s mail privileges have been revoked.\n\r",usrname);
                getch();
                return(0);
               }
   sprintf(str,"%s%s\\MAIL\\USR.DAT",ROOTDIR,usrname);
   return(exist(str));
}


void distr() { /* NEEDS DOS 5.0+ */
     FILE *o;
     char x;
     char chk[100];
     char fnam[100];
     char fle[150];
     int count = 0;
     disp("\r\n@08@Find@07@ing U@0F@sers...");
     sprintf(fle,"dir /b /ad %s*. > mailusrs.tmp",ROOTDIR);
     system(fle);
     o = fopen("mailusrs.tmp","r");
     disp("\r\n@04@Distributing@0R@");
     for(;;) {
         x = 0;
         if(count++ % 10 == 0) disp(".");
         if (fgets(fle,10,o)== 0) {
                fclose(o);
                remove("mailusrs.tmp");
                goto en;
                }
         while(fle[x])x++;
         fle[x-1] = 0;
         do {
          sprintf(chk,"%s%s\\MAIL\\%s.msg",ROOTDIR,fle,ultoa(time(NULL) - Toff,fnam,16));
           } while(exist(chk));
        copy_file("MAILTEMP.$$$",chk);
    }
 en:
 disp("...\r\n@03@\7Done...\r\n");
 }


char dfile(char *fname) {
       FILE *fil;
       char obuf[500];
       int point = 0;
       char line = 0;
       char x;
       if(!exist(fname)) return -1;
       fil = fopen(fname,"r");
       if(!fil) return -1;
       while((x=getc(fil)) != EOF) {
		     obuf[point++] = x;
		     if (x == '\n') {
			       obuf[point] = '\r';
			       obuf[point+1] =0;
			       disp(obuf);
                   line++;
                   if(line > 47) {
                        disp("@07@Hit any key to @03@Continue@03@...");
                        getch();
                        for(line=0;line<strlen("Hit any key to Continue...");line++) disp("\b \b");
                        line = 0;
                        }
                   obuf[0] =0;
			       point = 0;
                   } //NewLn
             } //File
     fclose(fil);
     return 0;
     }

char dmsg(char *fname) {
       FILE *fil;
       char obuf[200];
       int point = 0;
       char line = 0;
       char x;
       if(!exist(fname)) return -1;
       fil = fopen(fname,"rb");
       if(!fil) return -1;
       fread(&msghdr,sizeof(mhdr),1,fil);
       sprintf(obuf,"@02@From     :@0A@ %s\r\n", msghdr.sender);
       disp(obuf);
       sprintf(obuf,"@02@To       :@0A@ %s\r\n", msghdr.reciever);
       disp(obuf);
       disp("@02@Subject  :@0A@ ");
       if(msghdr.replied) disp("Re: ");
       sprintf(obuf,"%s\r\n", msghdr.subject);
       disp(obuf);
       sprintf(obuf,"@02@Date     :@0A@ %i/%i/%i @ %i:%i\r\n",msghdr.msgdate.da_mon,msghdr.msgdate.da_day,msghdr.msgdate.da_year,
                                                              msghdr.msgtime.ti_hour, msghdr.msgtime.ti_min);
       disp(obuf);
       if(msghdr.carbon) disp("@02@Carbon Copy\r\n");
       if(msghdr.forward) {
                sprintf(obuf,"@02@Forwarded by: @0A@ %s\r\n",msghdr.forwarder);
                disp(obuf);
                }
       disp("@07@ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\r\n");
      textcolor(9);
       while((x=getc(fil)) != EOF) {
		     obuf[point++] = x;
		     if (x == '\n') {
			       obuf[point] = '\r';
			       obuf[point+1] =0;
			       disp(obuf);
                   line++;
                   if(line > 47) {
                        disp("@07@Hit any key to @03@Continue@03@...");
                        if(getch() == 27) goto break_from_read;
                        for(line=0;line<strlen("Hit any key to Continue...");line++) disp("\b \b");
                        line = 0;
                        }
                   obuf[0] =0;
			       point = 0;
                   } //NewLn
             } //File
     break_from_read:
     fclose(fil);
     return 0;
     }

char sendm(char *to,char *from,char *sub,char *quote, char reply) {
    FILE *fp;
    char bufbak[100];
    char filechk[100],filename[100];
    char buffermisc[100];
    char fnum;
    char buf[200];
    msghdr.forward = 0;
    msghdr.replied = 0;
    msghdr.carbon = 0;
    if(!sub[0]) {
        disp("\r\n\r\n@02@Subject (@0A@ENTER to @04@abort@02@): ");
        sgets(48,msghdr.subject,0);
        if (msghdr.subject[0] == 0) return 1;
        }
    if ((fp = fopen("MAILTEMP.$$$","wb")) == 0) return -1;
    strupr(&from);
    strupr(&to);
    strcpy(msghdr.sender, from);
    strcpy(msghdr.reciever, to);
    //Time Getting
     gettime(&msghdr.msgtime);
     getdate(&msghdr.msgdate);
  //File Header
     if(reply) msghdr.replied = 1;
     fwrite(&msghdr,sizeof(mhdr),1,fp);
  //FileRecordBegin
    disp("@0F@\r\nEnter your message, hit Esc when done...\r\n");
    buf[0] = 0;
/* Quoting */
  quotef:
    if(quote[0]) {
        char init;
        char tt;
        char buffer[400];
        char outbuf[430];
        FILE *fi = fopen(quote,"r");
        msghdr.replied = 1;
        disp("\n");
        fgets(buffer,399,fi);
        init = toupper(to[0]);
        fseek(fi,sizeof(mhdr),SEEK_SET);
        for(;;) {
                if(fgets(buffer,399,fi) == 0) {
                        fclose(fi);
                        fputs("@09@",fp);
                        goto mesbeg;
                        }
                 if(buffer[0] == '@' &&
                   buffer[1] == '0' &&
                   buffer[2] == 'E' &&
                   buffer[3] == '@' &&
                   buffer[5] == '@' &&
                   buffer[6] == '0' &&
                   buffer[7] == '7' &&
                   buffer[8] == '@' &&
                   buffer[9] == '>') sprintf(outbuf,"%s",buffer);
                   else sprintf(outbuf,"@0E@%c@07@> %s\r",init,buffer);
                fputs(outbuf,fp);
                tt=0;
                while(buffer[tt]) tt++;
                buffer[tt-1] = 0;
                if(buffer[0] == '@' &&
                   buffer[1] == '0' &&
                   buffer[2] == 'E' &&
                   buffer[3] == '@' &&
                   buffer[5] == '@' &&
                   buffer[6] == '0' &&
                   buffer[7] == '7' &&
                   buffer[8] == '@' &&
                   buffer[9] == '>') sprintf(outbuf,"\n\r@0F@>> %s",buffer);
                   else sprintf(outbuf,"\n\r@0F@>> @0E@%c@07@>%s",init,buffer);
                disp(outbuf);
           }
    } /*Quoting END */
    mesbeg:
     for(;;) {
          char t = 0;
          disp("@0F@\n\r>>");
          textcolor(LIGHTBLUE);
          t = sgets(73,buf,1);
          strcat(buf,"\n");
          fprintf(fp,"%s",buf);
          if (t) goto endmess;
          }
          endmess:
  //EndRecordFile
  disp("@0C@\r\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n");
  disp("@0F@\r\n@03@[@0b@S@03@]end [@0b@A@03@]bort [@0b@C@03@]arbon Copy: ");
  textcolor(LIGHTBLUE);
  fclose(fp);
smes:
  fnum = getch();
     if(!fnum) fnum = getch() * 0x100;
    if (toupper(fnum) == 'A') {
        disp(" @03@Abort...\r\n");
        disp("@0F@\r\n Message NOT sent.");
        delay(200);
        remove("MAILTEMP.$$$");
        return 3;
        }
    else if(toupper(fnum) == 'S' || toupper(fnum) == 'C') {
        char xxxxx = 0;
        if(stricmp(to,"ALL") == 0 && toupper(fnum) == 'C')
             disp("\r\n\7@0F@Cannot @0A@Carbon Copy@0B@ Distributed Message, Sending");
        else if(toupper(fnum) == 'C') xxxxx=1;
        if(xxxxx) disp("@03@ Carbon Copy....\r\n");
        else disp("@03@ Send...\r\n");
        if(stricmp(to,"all") == 0) {
                        distr();
                        remove("mailtemp.$$$");
                        getch();
                        return 0;
                        }
        notsent:
        do {
             sprintf(filechk,"%s%s\\MAIL\\%s.msg",ROOTDIR,to,ultoa(time(NULL) - Toff,buffermisc,16));
        } while(exist(filechk));
        copy_file("MAILTEMP.$$$",filechk);
        if(exist(filechk)) disp("\r\n@0F@Message Sent...");
                 else {
                    disp("\r\n\r\nError Writing File, Retrying... (Hit ESC to quit)\r\n ");
                    disp(filename);
                    disp("\r\n");
                    delay(1000);
                    if(kbhit()) if(getch() == 27) {
                                 copy_file("MAILTEMP.$$$","deadmail.dat");
                                 goto abort_send;
                                 }
                    goto notsent;
                    } //Not Exists
        abort_send:
        if(xxxxx) {
           fp = fopen("mailtemp.$$$","r+");
           fread(&msghdr,sizeof(mhdr),1,fp);
           gettime(&msghdr.msgtime);
           getdate(&msghdr.msgdate);
           msghdr.carbon = 1;
           fseek(fp,0L,SEEK_SET);
           fwrite(&msghdr,sizeof(mhdr),1,fp);
           fclose(fp);
           notcar:
           do {
                   sprintf(filechk,"%s%s.msg",MAILDIR,ultoa(time(NULL) - Toff,buffermisc,16));
              }while(exist(filechk));
              move_file("MAILTEMP.$$$",filechk);
              if(exist(filechk)) disp("\r\n@0F@Message copied to you...");
              else {
                    disp("\r\n\r\nError Writing File, Retrying...");
                    disp("\r\n Debug: ");
                    disp(filename);
                    disp("\r\n");
                    delay(1000);
                    if(kbhit()) if(getch() == 27) goto abort_carbon;
                    goto notcar;
                    }
             abort_carbon:
             delay(500);
             }
        remove("mailtemp.$$$");
        return 0;
	}
   else goto smes;
}

