/*
 * quick test whipped up to create SAMPLE.3KT in archive from my message
 * base.  this is NOT a good example of how to access XBBS msg bases --
 * I took many potentially lethal shortcuts.  freq XBBSMSG.LZH for a
 * decent API if you're curious.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <time.h>
#include "xmsg.h"
#include "3mail.h"

extern int stripjnk3 (char *a,int len);
extern char * fido_2_type3a (char *a);  /* from 3supp.c */



int main (void) {

  int    handlet,handled,phndl = -1,x,error;
  long   sized;
  XMSG   xmsg;
  char  *tbuf,*p,*pp;
  PHDR3  pkt3;
  MHDR3  msg3;
  char   from[80],to[80],date[20],id[133];
  time_t seed;

  pkt3.head = NULL;
  pkt3.from = "Fidonet#1:380/20";
  pkt3.to = NULL;
  pkt3.creator = "3TESTOUT";
  pkt3.pword = NULL;
  pkt3.area = "NET_DEV";
  msg3.head = NULL;
  msg3.area = NULL;
  msg3.ref = NULL;
  msg3.from = from;
  msg3.to = to;
  msg3.date = date;
  msg3.id = id;
  seed = time(NULL);

  handled = sopen("E:/XBBS2/MSG/XDATA.01b",O_RDONLY | O_BINARY,SH_DENYNO);
  if(handled != -1) {
    lseek(handled,0L,SEEK_END);
    sized = tell(handled);
    lseek(handled,0L,SEEK_SET);
    sized /= (long)sizeof(XMSG);  /* how many msgs, but we won't use it */
    tbuf = (char *)malloc(32767);
    if(tbuf) {
      handlet = sopen("E:/XBBS2/MSG/XTEXT.01b",O_RDONLY | O_BINARY,SH_DENYNO); /* happens to be NET_DEV >;-) */
      if(handlet != -1) {
        for(x = 0;x < 15;x++) {
          if(read(handled,&xmsg,sizeof(XMSG)) != sizeof(XMSG)) {
            printf("\nMessage data read error\n");
            break;
          }
          if(xmsg.m_attr & MSGPACKED) continue; /* ignore compressed msgs -- too much code to include */
          if(lseek(handlet,xmsg.start,SEEK_SET) == -1L) {
            printf("\nMessage text seek error\n");
            break;
          }
          if(read(handlet,tbuf,xmsg.length) >= (int)xmsg.length - 1) {

            /* create packet, write header */

            phndl = wopen_3pkt(phndl,"SAMPLE.3KT",&pkt3,&error);
            if(error) {
              printf("\nPacket creation error #%d\n",error);
              break;
            }

            if(!xmsg.o_zone) xmsg.o_zone = 1;
            sprintf(from,"%s@Fidonet#%u:%u/%u",xmsg.from,xmsg.o_zone,
                    xmsg.orig_net,xmsg.orig);
            if(xmsg.o_point) sprintf(&from[strlen(from)],".%u",xmsg.o_point);
            sprintf(id," %lx",seed + (long)x);
            strcpy(to,xmsg.to);
            strcat(to,"@");
            msg3.subj = xmsg.subj;
            p = fido_2_type3a(xmsg.date);
            if(p) strcpy(date,p);
            else {
              printf("\nBad date \"%s\" -- msg skipped.\n",xmsg.date);
              continue;
            }
            p = tbuf;
            while((p = strchr(p,'\01')) != NULL) {  /* strip kludges */
              if((pp = strchr(p,'\r')) != NULL) {
                memmove(p,pp + 1,strlen(pp));
              }
              else p++;
            }

            /* write msg to packet */

            write_3msghdr(phndl,&msg3,&error);
            if(error) {
              printf("\nMsg hdr write error #%d\n",error);
              break;
            }
            stripjnk3(tbuf,strlen(tbuf));
            if(write(phndl,tbuf,strlen(tbuf)) < 0) {
              printf("\nMsg text write error\n");
              break;
            }
            if(write(phndl,"\0",2) < 2) {  /* note wopen_pkt() backs up 1 */
              printf("\nMsg text termination error\n");
              break;
            }

            printf("%d\r",x + 1);
          }
          else {
            printf("\nMessage text read error\n");
            break;
          }
        }
        phndl = wclose_3pkt(phndl);
        close(handlet);
        free(tbuf);
        close(handled);
        printf("\nComplete\n");
      }
      else {
        free(tbuf);
        close(handled);
        printf("\nCouldn't open text file\n");
      }
    }
    else {
      close(handled);
      printf("\nOut of memory\n");
    }
  }
  else printf("\nCouldn't open data file\n");

  return 0;
}
