/*------------------------------------------------------------------*/
/* A small program to change the date string stored in *.MSG files  */
/* from Opus format to Qmail format. Use it in good health.         */
/*                                                                  */
/* With Turbo C: tcc -mt -lt -f- -v- -r -G qm_conv.c                */
/* With MSC:     cl /AT /Gs /Ozax qm_conv.c;                        */
/*                                                                  */
/*  Thanks to Dean Lachan and George Peace for testing and          */
/*  suggestions                                                     */
/*                                                                  */
/* Doug Boone, FidoNet 1:119/5                                      */
/*------------------------------------------------------------------*/

#include  <stdio.h>
#include  <dos.h>
#include  <io.h>
#include  <fcntl.h>
#include  <string.h>
#ifdef __TURBOC__
#include  <dir.h>
#endif

#define   EOS   '\0'
#define   MAXPATH 80

void  usage(void);
void  pascal do_msgs(char *);
int   pascal findhigh(char *);

typedef unsigned int  word;
typedef unsigned char byte;

/*-----------------------------------------------------------------*/
/* TIME STAMP                                                      */
/*-----------------------------------------------------------------*/
struct _stamp
{
      unsigned int   date;
      unsigned int   time;
};

struct _msg
{   
   byte from[36];
   byte to[36];
   byte subj[72];
   byte date[20];    /* Obsolete/unused ASCII date information   */
   word times;       /* FIDO<tm>: Number of times read           */
   word dest;        /* Destination node                         */
   word orig;        /* Origination node number                  */
   word cost;        /* Unit cost charged to send the message    */
   word orig_net;    /* Origination network number               */
   word dest_net;    /* Destination network number               */
   struct _stamp date_written;   /* When user wrote the msg      */
   struct _stamp date_arrived;   /* When msg arrived on-line     */
   word reply;       /* Current msg is a reply to this msg number*/
   word attr;        /* Attribute (behavior) of the message      */
   word up;          /* Next message in the thread               */
};



void cdecl main(int argc,char *argv[])
{
  FILE  *areas;
  FILE  *log;
  char  msgpath[MAXPATH];
  char  aline[120];
  char  *ptr;
  char  *ptr2;
  char  *pathptr;
  char  areatag[32];
  char  path[MAXPATH];


  if (argc < 3)
    usage();

  if ((log = fopen(argv[1],"rt")) == NULL)
    usage();

  if ((areas = fopen(argv[2],"rt")) == NULL) {
    fclose(log);
    usage();
    }

  while ((ptr = fgets(aline,118,log)) != NULL) {  /* Read ECHOTOSS */
    memset(&areatag,EOS,32);
    while (*ptr && *ptr > ' ')
      ptr++;
    *ptr = EOS;
    if (!aline[0])
      continue;
    strncpy(areatag,strupr(aline),31);
    areatag[31] = EOS;
    rewind(areas);    /* Then start looking for a math in AREAS.BBS */
    memset(&path,EOS,MAXPATH);
    while (!path[0] && (ptr = fgets(aline,118,areas)) != NULL) {
      if (strstr(strupr(aline),areatag)) {    /* Smells good */
		if (strchr(aline,'#')) {   /* passthru? */
            printf("Skipping PASSTHROUGH area %s\n",areatag);
            break;
            }
        while (*ptr && isspace(*ptr))    /* Skip leading whitespace */
          ptr++;
        pathptr = ptr;
        while (*ptr && !isspace(*ptr))    /* Get the msg path */
          ptr++;
        *ptr = EOS;
        ptr++;
        while (*ptr &&  isspace(*ptr))    /* Skip whitespace bet path & tag */
          ptr++;
        ptr2 = ptr;
        while (*ptr2 && !isspace(*ptr2))
          ptr2++;
        *ptr2 = EOS;
        if (!strcmp(ptr,areatag)) {    /* Make sure the tags match */
          strcpy(path,pathptr);
          if (path[strlen(path)] != '\\')
            strcat(path,"\\");
          do_msgs(path);    /* Go get messages */
          }
        }
      }
    }
  fclose(areas);
  fclose(log);
  exit(0);
}

void usage(void)
{
  printf("\n\nConverts Opus *.MSG dates to FTSC *.PKT dates.\n\n");
  printf("Use as:\n\tQM_CONV echotoss.log areas.bbs\n\n");
  printf("Needs the FULL path and file name for both files\n\n");
  printf("Run just before using the Qmail SCAN option on\n");
  printf("OPUS boards or boards that generate OPUS *.MSG files\n");
  printf("for messages that have just been entered.\n\n");
  exit(1);
}

void pascal do_msgs(char *start)
{
  char  path[MAXPATH];
  struct  _msg  msg;
  char  date[20];
  int   number;
  int   high;
  int   fh;

  strcpy(path,start);
  high = findhigh(path);  /* Get the highest message number here */
  strcat(path,"1.MSG");    /* Read 1.msg for high water mark */
  if ((fh = open(path,O_BINARY|O_RDONLY)) < 0)
    number = 1;
  else {
    read(fh,(char *)&msg,sizeof(struct _msg));
    close(fh);
    number = msg.reply;
    }
  if (number >= high) {    /* Nothing new here */
    printf("Nothing new in %s\n",start);
    return;
    }
  while (number <= high) {    /* Start checking the *.MSGs */
    sprintf(path,"%s%d.MSG",start,number);
    if ((fh = open(path,O_BINARY|O_RDWR)) > 0) {
      read(fh,(char *)&msg,sizeof(struct _msg));
      if ((msg.date[18] == 0x00) && (msg.date[19] == 0xff)) {/* Got one */
        memset(&date,EOS,20);
        strncpy(date,msg.date,10);
        strncpy(date+10,msg.date+9,10);
        printf("Changing %s\t%s to %s\n",path,msg.date,date);
        strncpy(msg.date,date,20);
        lseek(fh,0L,SEEK_SET);
        write(fh,(char *)&msg,sizeof(struct _msg));
        }    /* Got a message to change */
      else
        printf("Skipping %s\t%s\n",path,msg.date);
      close(fh);
      }    /* End of openning *.MSG file properly */
    number++;
    };    /* End of messages */
}

#ifdef __TURBOC__
int pascal findhigh(char *path)
{
  char  temp[MAXPATH];
  int   highest;
  struct  ffblk ffblk;
  register int now;

  highest = 0;

  strcpy(temp,path);
  strcat(temp,"*.MSG");

  if (findfirst(temp,&ffblk,0))
    return(0);
  do {
    now = atoi(ffblk.ff_name);
    if (now > highest)
      highest = now;
    } while (!(findnext(&ffblk)));
  return(highest);
}

#else

int pascal findhigh(char *path)
{
  char  temp[MAXPATH];
  int   highest;
  struct  find_t ffblk;
  register int now;

  highest = 0;

  strcpy(temp,path);
  strcat(temp,"*.MSG");

  if (_dos_findfirst(temp,_A_NORMAL,&ffblk))
    return(0);
  do {
    now = atoi(ffblk.name);
    if (now > highest)
      highest = now;
    } while (!(_dos_findnext(&ffblk)));
  return(highest);
}
#endif

