/*    SMTP Reqdir/Reqfil Server for TCP/IP    */
/*    by Mats Petersson                       */
/*    v1.0 930127                             */


#include <stdio.h>
#include <errno.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include <dir.h>
#include <string.h>
#include <process.h>
#include <cursor.c>

FILE* tempfile;
FILE* outfile;
FILE* reqdatf;
char fstr[80];
char commandstr[80];
char tempstr[80];
char tempdir[80];
char roepnaam[80];
char subdir[80],aan[80],dir[80];
char bidstr[5];
char outfname[80];
int bid;

struct tm *tbl;
time_t t;

struct ffblk ffblk;


extract(char *s, int p)
{
char *i;
    strcpy(s,&fstr[p]);
    while(s[0] == ' ') strcpy(s,&s[1]);

    while(i = strrchr(s,'\x20')) i[0] = 0;

    if( (i != 0)  || (i = strrchr(s,'\n')) )
    {
         i[0] = 0;
         i[1] = 0;
    }
}

getdata(FILE* fp)
{
         strcpy(aan,"");
         strcpy(dir,"");
         while(fgets(fstr,80,fp))
         {
              if( (! strnicmp(fstr,"From ",5)) && (aan[0] != 0)) return;
              if(! strnicmp(fstr,"From: ",6)) extract(aan,5);
              if(! strnicmp(fstr,"Subject: ",9)) extract(dir,8);
              if(! strnicmp(fstr,"Reply-To: ",10)) extract(aan,9);
         }
}

makeheader(char *ftype, char *reqtype)
{
char datestr[80];
static char *wday[] = {"Mon", "Tue", "Wed", "Thu", "Fri",
                "Sat", "Sun"};
static char *mon[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

         reqdatf = fopen("request.dat","rt");
         if(fscanf(reqdatf,"%s",bidstr) > 0) bid = atoi(bidstr);
         else
         {
             bid = 0;
             strcpy(bidstr,"0");
         }
         fclose(reqdatf);

         time(&t);
         tbl = localtime(&t);
         sprintf(datestr,"%s, %d %s %d %02d:%02d:%02d CET",
            wday[tbl->tm_wday],tbl->tm_mday,mon[tbl->tm_mon],tbl->tm_year,
            tbl->tm_hour,tbl->tm_min,tbl->tm_sec);

         sprintf(outfname,"spool\\mqueue\\%s.txt",bidstr);
         outfile = fopen(outfname,"wt");

         fprintf(outfile,"Date: %s\n",datestr);
         fprintf(outfile,"Message-Id: <%s-%s@%s>\n",bidstr,reqtype,roepnaam);
         fprintf(outfile,"From: %s@%s\n",reqtype,roepnaam);
         fprintf(outfile,"To: %s\n",aan);
         fprintf(outfile,"Subject: %s %s\n",ftype,dir);
         fprintf(outfile,"X-Mailer: SMTP reqdir/reqfil server\n");
         fprintf(outfile,"\n");
}


makewrkfile(char *reqtype)
{
    sprintf(outfname,"spool\\mqueue\\%s.wrk",bidstr);
    outfile = fopen(outfname,"wt");

    fprintf(outfile,"%s\n",roepnaam);
    fprintf(outfile,"%s@%s\n",reqtype,roepnaam);
    fprintf(outfile,"%s\n",aan);
    fclose(outfile);

    bid += 2;
    sprintf(bidstr,"%d\n",bid);
    reqdatf = fopen("request.dat","wt");
    fputs(bidstr,reqdatf);
    fclose(reqdatf);
}

reqdir(FILE* rdfile)
{
int nosuchdir;

    while(1)
    {
         nosuchdir = 0;
         getdata(rdfile);

         if((feof(rdfile)) && (aan[0] == 0))
         {
              fclose(rdfile);
              remove("spool\\mail\\reqdir.txt");
              remove("temp.$$$");
              return;
         }

         sprintf(tempstr,"%s%s",subdir,dir);
         strcpy(dir,tempstr);
         makeheader("Directory","reqdir");

         if(findfirst(dir,&ffblk,FA_DIREC))
              nosuchdir = 1;

         if (! nosuchdir)
         {
              sprintf(commandstr,"dir %s > temp.$$$",dir);
              system(commandstr);
              tempfile = fopen("temp.$$$","rt");

              while(fgets(fstr,80,tempfile))
                 if((fstr[0] != ' ') && (fstr[0] != '.')) fputs(fstr,outfile);

              fclose(tempfile);
         }
         else
         {
              fprintf(outfile,"Sorry OM\n");
              fprintf(outfile,"I can't find %s\n\n",dir);
              fprintf(outfile,"73's de SMTP reqdir/reqfil server");
         }
         fclose(outfile);
         makewrkfile("reqdir");
    }
}


splitfile()
{
    tempfile = fopen("temp.$$$","rt");

    while(! feof(tempfile))
    {
         fgets(fstr,80,tempfile);
         if(strstr(fstr," go_7+"))
         {
              makeheader("File","reqfil");
              fputs(fstr,outfile);
              do{
                   fgets(fstr,80,tempfile);
                   fputs(fstr,outfile);
              } while(! strstr(fstr," stop_7+"));
              fclose(outfile);
              makewrkfile("reqfil");
         }
    }
    fclose(tempfile);
}

reqfil(FILE* rffile)
{
FILE* reqfile;
unsigned char val1,val2;
int result, i;
int nosuchfile;
char shortfname[8];

    while(1)
    {
         getdata(rffile);
         nosuchfile = 0;

         if((feof(rffile)) && (aan[0] == 0))
         {
              fclose(rffile);
              remove("spool\\mail\\reqfil.txt");
              remove("temp.$$$");
              return;
         }

         sprintf(tempstr,"%s%s",subdir,dir);
         strcpy(dir,tempstr);

         if(findfirst(dir,&ffblk,0))
              nosuchfile = 1;

         if (! nosuchfile)
         {
              reqfile = fopen(dir,"rt");           /* BINARY FILE check */
              val1 = 0; val2 = 0;
              for(i = 0;i<300;i++){
                   fread(&val1,1,1,reqfile);
                   if(val1 > val2) val2 = val1;
              }
              fclose(reqfile);

              if(val2 > 127)
              {
                    result = spawnl(P_WAIT, searchpath("7plus.exe"), "",dir, NULL);

                    strcpy(shortfname,strrchr(dir,'\\')+1);
                    shortfname[strrchr(shortfname,'.') - shortfname] = 0;
                    sprintf(tempstr,"%s.p??",shortfname);

                    if(! findfirst(tempstr,&ffblk,0))
                    {
                        sprintf(commandstr,"copy %s temp.$$$ >NUL:",tempstr);
                        system(commandstr);
                        sprintf(commandstr,"del %s",tempstr);
                        system(commandstr);
                    }
                    else
                    {
                        strcat(shortfname,".7pl");
                        sprintf(commandstr,"copy %s temp.$$$ >NUL:",shortfname);
                        system(commandstr);
                        sprintf(commandstr,"del %s",shortfname);
                        system(commandstr);
                    }
                    splitfile();
                    clrscr();
              }
              else
              {
                   makeheader("File","reqfil");
                   sprintf(commandstr,"copy %s temp.$$$ >NUL:",dir);
                   system(commandstr);
                   tempfile = fopen("temp.$$$","rt");

                   while(fgets(fstr,80,tempfile))
                        fputs(fstr,outfile);

                   fclose(tempfile);
                   fclose(outfile);
                   makewrkfile("reqfil");
              }
         }
         else
         {
              makeheader("File","reqfil");
              fprintf(outfile,"Sorry OM\n");
              fprintf(outfile,"I can't find %s\n\n",dir);
              fprintf(outfile,"73's de SMTP reqdir/reqfil server");
              fclose(outfile);
              makewrkfile("reqfil");
         }
    }
}

check()
{
FILE* reqdirf;
FILE* reqfilf;

    if(reqdirf = fopen("spool\\mail\\reqdir.txt","rt")) reqdir(reqdirf);
    if(reqfilf = fopen("spool\\mail\\reqfil.txt","rt")) reqfil(reqfilf);
}



howtouse()
{
    clrscr();
    printf("               Request Server for TCP/IP version 1.0\n");
    printf("\n");
    printf("For correct operation the file 7PLUS.EXE must be in the PATH\n");
    printf("\n");
    printf("How to use the REQDIR server:\n");
    printf("Send a message to REQDIR @ <call> to get a directory.\n");
    printf("The Subject line must be the name of the directory.\n");
    printf("So 'Subject: upload' will check '<base_dir>\\upload\\*.*'\n");
    printf("\n");
    printf("How to use the REQFIL server:\n");
    printf("Send a message to REQFIL @ <call> to get a 7PLUS encoded file.\n");
    printf("The subject must be the name of the file.\n");
    printf("So 'Subject: upload\\file.ext' sends '<base_dir>\\upload\\file.ext' as\n");
    printf("7PLUS encoded file(s).\n");
    printf("\n");
    printf("ASCII files will be sent as 'normal' messages.\n");
    printf("\n");
    printf("For all answering there must be an entry in your ALIAS file.\n");
    printf("\n");
    printf("Options : 0   run the program one time (with the 'AT' command),\n");
    printf("          n   read mailfile every 'n' seconds (in DV / DD).\n");
    printf("\n");
    printf("Read the REQUEST.CFG file, made by this server for installation!\n");
    getch();
    clrscr();
    quit();
}

init()
{
FILE* cfgfile;

       clrscr();
       HideCur();

       printf("Mail Server v1.0\n");

       if(! (cfgfile = fopen("request.cfg","rt")))
       {
         cfgfile = fopen("request.cfg","wt");
         fprintf(cfgfile,"# These are the default values for the REQUEST.CFG file.\n");
         fprintf(cfgfile,"call sm5sxl         # This must be Your Callsign\n");
         fprintf(cfgfile,"subdir \\public\\     # This is your base directory\n");
         fclose(cfgfile);
         printf("# These are the default values for the REQUEST.CFG file.\n");
         printf("call sm5sxl         # This must be Your Callsign\n");
         printf("subdir \\public\\     # This is your base directory\n\n");
         printf("You must now edit the file REQUEST.CFG for Your configuration!\n");
         quit();
       }

       while(fgets(fstr,80,cfgfile))
       {
               if(fstr[0] == 'c') extract(roepnaam,4);
               if(fstr[0] == 'C') extract(roepnaam,4);

               if(fstr[0] == 's') extract(subdir,6);
               if(fstr[0] == 'S') extract(subdir,6);
       }

       if(!(reqdatf = fopen("request.dat","rt")))
       {
              reqdatf = fopen("request.dat","wt");
              fclose(reqdatf);
       }
       else fclose(reqdatf);
}


quit()
{
    ShowCur();
    exit(0);
}


main(int argc,char **argv)
{
char ch;
int time_int;
time_t t;

       if(argc < 2) howtouse();

       init();

       time_int = atoi(argv[1]);

       check();
       if(time_int == 0) quit();

       t = time(NULL);

       while ( (ch = bioskey(1) & 0xff) != 27)
       {
              gotoxy(1,2);
              cprintf("Next Action after %02d seconds       Timer set to %d Seconds\n",
                         time_int - (int)difftime(time(NULL),t),time_int);

              if ( (int)difftime(time(NULL),t) == time_int)
              {
                   check();
                   t = time(NULL);
              }
       }
       getch();
       quit();
}