home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC15B21.ZIP / EXP.C < prev    next >
Text File  |  1997-03-29  |  27KB  |  928 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <ctype.h>
  8. #include <mem.h>
  9. #include <conio.h>
  10. #include <io.h>
  11. #include <share.h>
  12. #include <errno.h>
  13. #include <dir.h>
  14. #include <time.h>
  15. #include <malloc.h>
  16. #include <process.h>
  17. #include <direct.h>
  18. #include "vardec.h"
  19. #include "net.h"
  20.  
  21. #include "version.h"
  22.  
  23. #define WAIT_TIME 10
  24. #define TRIES 100
  25.  
  26. struct msghdr {
  27.   char fromUserName[205];
  28.   char toUserName[81];
  29.   char subject[81];
  30.   char dateTime[81];
  31. };
  32.  
  33. configrec syscfg;
  34. char *net_name, postmaster[31], net_data[MAXPATH], POPNAME[21], DOMAIN[81], tagfile[MAXPATH], maindir[MAXPATH];
  35. unsigned short net_sysnum, defuser, num_users, use_alias, instance, curuser, spam;
  36.  
  37. int exist(char *s)
  38. {
  39.   int i;
  40.   struct ffblk ff;
  41.  
  42.   i = findfirst(s, &ff, 0);
  43.   if (i)
  44.     return (0);
  45.   else
  46.     return (1);
  47. }
  48.  
  49. char *stripspace(char *str)
  50. {
  51.   char *obuf, *nbuf;
  52.  
  53.   if (str) {
  54.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  55.       if (!isspace(*obuf))
  56.         *nbuf++ = *obuf;
  57.     }
  58.     *nbuf = NULL;
  59.   }
  60.   return (str);
  61. }
  62.  
  63. int sh_open(char *path, int file_access, unsigned mode)
  64. {
  65.   int handle, count, share;
  66.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  67.  
  68.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (mode & S_IWRITE))
  69.     share = SH_DENYRW;
  70.   else
  71.     share = SH_DENYWR;
  72.   handle = open(path, file_access | share, mode);
  73.   if (handle < 0) {
  74.     count = 1;
  75.     fnsplit(path, drive, dir, file, ext);
  76.     if (access(path, 0) != -1) {
  77.       delay(WAIT_TIME);
  78.       handle = open(path, file_access | share, mode);
  79.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  80.         delay(WAIT_TIME);
  81.         count++;
  82.         handle = open(path, file_access | share, mode);
  83.       }
  84.     }
  85.   }
  86.   return (handle);
  87. }
  88.  
  89. int sh_open1(char *path, int access)
  90. {
  91.   unsigned mode;
  92.  
  93.   mode = 0;
  94.   if ((access & O_RDWR) || (access & O_WRONLY))
  95.     mode |= S_IWRITE;
  96.   if ((access & O_RDWR) || (access & O_RDONLY))
  97.     mode |= S_IREAD;
  98.   return (sh_open(path, access, mode));
  99. }
  100.  
  101. FILE *fsh_open(char *path, char *fmode)
  102. {
  103.   FILE *f;
  104.   int count, share, md, fd;
  105.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  106.  
  107.   share = SH_DENYWR;
  108.   md = 0;
  109.   if ((_fstrchr(fmode, 'w')) != NULL) {
  110.     share = SH_DENYRD;
  111.     md = O_RDWR | O_CREAT | O_TRUNC;
  112.   } else
  113.     if ((_fstrchr(fmode, 'a')) != NULL) {
  114.     share = SH_DENYRD;
  115.     md = O_RDWR | O_CREAT;
  116.   } else {
  117.     md = O_RDONLY;
  118.   }
  119.   if ((_fstrchr(fmode, 'b')) != NULL) {
  120.     md |= O_BINARY;
  121.   }
  122.   if ((_fstrchr(fmode, '+')) != NULL) {
  123.     md &= ~O_RDONLY;
  124.     md |= O_RDWR;
  125.     share = SH_DENYRD;
  126.   }
  127.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  128.   if (fd < 0) {
  129.     count = 1;
  130.     fnsplit(path, drive, dir, file, ext);
  131.     if ((access(path, 0)) != -1) {
  132.       delay(WAIT_TIME);
  133.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  134.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  135.         delay(WAIT_TIME);
  136.         count++;
  137.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  138.       }
  139.     }
  140.   }
  141.   if (fd > 0) {
  142.     if ((_fstrchr(fmode, 'a')) != NULL)
  143.       lseek(fd, 0L, SEEK_END);
  144.  
  145.     f = fdopen(fd, fmode);
  146.     if (!f) {
  147.       close(fd);
  148.     }
  149.   } else
  150.     f = 0;
  151.   return (f);
  152. }
  153.  
  154. static unsigned char *trimstr1(unsigned char *s)
  155. {
  156.   int i;
  157.   static char *whitespace = " \r\n\t";
  158.  
  159.   i = (int) strlen(s);
  160.   while ((i > 0) && ((char *) _fstrchr(whitespace, s[i - 1]) != NULL))
  161.     --i;
  162.   while ((i > 0) && ((char *) _fstrchr(whitespace, *s) != NULL)) {
  163.     memmove(s, s + 1, --i);
  164.   }
  165.   s[i] = 0;
  166.   return (s);
  167. }
  168.  
  169.  
  170. int num_to_name(char *where, int whichuser, int alias);
  171.  
  172. void parse_net_ini(void)
  173. {
  174.  
  175.   char s[MAXPATH], line[121], *ss;
  176.   FILE *fp;
  177.  
  178.   defuser = 1;
  179.   use_alias = 1;
  180.   sprintf(s, "%sNET.INI", maindir);
  181.   fp = fsh_open(s, "rt");
  182.   if (!fp) {
  183.     fprintf(stderr, "\n ■ Unable to open %s.", s);
  184.     return;
  185.   }
  186.   while (fgets(line, 80, fp)) {
  187.     ss = NULL;
  188.     stripspace(line);
  189.     /* fprintf(stderr, "\nCurrent : %s\n", line); */
  190.     if ((line[0] == ';') || (line[0] == '\n') || (line[0] == '['))
  191.       continue;
  192.     if (strlen(line) == 0)
  193.       continue;
  194.     if (strnicmp(line, "POSTMASTER", 10) == 0) {
  195.       ss = strtok(line, "=");
  196.       if (ss) {
  197.         ss = strtok(NULL, "\n");
  198.         if (ss)
  199.           defuser = atoi(ss);
  200.       }
  201.     }
  202.     if (strnicmp(line, "SPAMCONTROL", 11) == 0) {
  203.       ss = strtok(line, "=");
  204.       if (ss) {
  205.         ss = strtok(NULL, "\n");
  206.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  207.           spam = 1;
  208.       }
  209.     }
  210.     if (strnicmp(line, "SIGNATURE", 9) == 0) {
  211.       ss = strtok(line, "=");
  212.       if (ss) {
  213.         ss = strtok(NULL, "\n");
  214.         trimstr1(ss);
  215.         strcpy(tagfile, ss);
  216.         if (!exist(tagfile)) {
  217.           fprintf(stderr, "\n ■ Signature file %s not found!", tagfile);
  218.           tagfile[0] = 0;
  219.         }
  220.       }
  221.     }
  222.     if (strnicmp(line, "REALNAME", 8) == 0) {
  223.       ss = strtok(line, "=");
  224.       if (ss) {
  225.         ss = strtok(NULL, "\n");
  226.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  227.           use_alias = 0;
  228.       }
  229.     }
  230.   }
  231.   num_to_name(postmaster, defuser, 1);
  232.   fclose(fp);
  233.   return;
  234. }
  235.  
  236. int num_to_name(char *where, int whichuser, int alias)
  237. {
  238.   int userfile, num_users, found;
  239.   userrec ur;
  240.   long pos;
  241.   char fn[MAXPATH];
  242.  
  243.   found = 0;
  244.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  245.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  246.   if (userfile < 0) {
  247.     fprintf(stderr, "\n ■ Cannot open %s.", fn);
  248.     return (found);
  249.   }
  250.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  251.  
  252.   if (whichuser > num_users) {
  253.     fprintf(stderr, "\n ■ User #%d out of range.", whichuser);
  254.     return (found);
  255.   }
  256.   pos = ((long) sizeof(userrec) * ((long) whichuser));
  257.   lseek(userfile, pos, SEEK_SET);
  258.   read(userfile, &ur, sizeof(userrec));
  259.   if (ur.realname[0] == 0)
  260.     fprintf(stderr, "\n ■ User #%d has blank real name field!", whichuser);
  261.   else {
  262.     if (ur.inact == inact_deleted)
  263.       fprintf(stderr, "\n ■ User #%d is marked as deleted!", whichuser);
  264.     else {
  265.       if (!alias)
  266.         strcpy(where, ur.realname);
  267.       else {
  268.         strlwr(ur.name);
  269.         strcpy(where, ur.name);
  270.       }
  271.       found = 1;
  272.     }
  273.   }
  274.   close(userfile);
  275.   return (found);
  276. }
  277.  
  278. int name_to_num(char *name)
  279. {
  280.   int i, userfile, usernum;
  281.   userrec ur;
  282.   long pos;
  283.   char fn[MAXPATH], othername[60];
  284.  
  285.   fprintf(stderr, "\n ■ Searching for user %s", strupr(name));
  286.   stripspace(name);
  287.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  288.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  289.   if (userfile < 0) {
  290.     printf("\n ■ Cannot open %s", fn);
  291.     return (0);
  292.   }
  293.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  294.  
  295.   for (i = 0; i < strlen(name); i++)
  296.     if (name[i] == ' ')
  297.       othername[i] = '_';
  298.     else
  299.       othername[i] = name[i];
  300.  
  301.   for (usernum = 1; usernum < num_users; usernum++) {
  302.     pos = ((long) sizeof(userrec) * ((long) usernum));
  303.     lseek(userfile, pos, SEEK_SET);
  304.     read(userfile, &ur, sizeof(userrec));
  305.     stripspace(ur.realname);
  306.     stripspace(ur.name);
  307.     if ((strcmpi(ur.realname, name) == 0) || (strcmpi(ur.name, name) == 0) ||
  308.         (strcmpi(ur.realname, othername) == 0) || (strcmpi(ur.name, othername) == 0)) {
  309.       if (ur.inact == inact_deleted) {
  310.         fprintf(stderr, "... user #%d is deleted account.", usernum);
  311.         usernum = 0;
  312.         break;
  313.       } else {
  314.         fprintf(stderr, "... matched user #%d.", usernum);
  315.         break;
  316.       }
  317.     }
  318.   }
  319.   userfile = close(userfile);
  320.  
  321.   if (usernum >= num_users) {
  322.     fprintf(stderr, "... no match found.");
  323.     return (0);
  324.   }
  325.   return (usernum);
  326. }
  327.  
  328. char *find_name(char *name)
  329. {
  330.   char *ss;
  331.  
  332.   curuser = 0;
  333.   if (strcspn(name, "(") != strlen(name)) {
  334.     ss = strtok(name, "(");
  335.     ss = strtok(NULL, ")");
  336.     strcpy(name, ss);
  337.     curuser = name_to_num(name);
  338.   } else
  339.     if (strcspn(name, "\"") != strlen(name)) {
  340.     ss = strtok(name, "\"");
  341.     ss = strtok(NULL, "\"");
  342.     strcpy(name, ss);
  343.     curuser = name_to_num(name);
  344.   } else
  345.     if (strcspn(name, "<") != strlen(name)) {
  346.     if ((strcspn(name, " ")) < (strcspn(name, "<"))) {
  347.       ss = strtok(name, "<");
  348.       trimstr1(ss);
  349.       strcpy(name, ss);
  350.       curuser = name_to_num(name);
  351.     } else {
  352.       fprintf(stderr, "\nName is *after* host in \"%s\"", name);
  353.     }
  354.     }
  355.   if (curuser == 0)
  356.     return ('\0');
  357.   else
  358.     return (ss);
  359. }
  360.  
  361. void name_packet(char *pktname)
  362. {
  363.   int ok;
  364.   struct stat info;
  365.   unsigned i;
  366.  
  367.   ok = 0;
  368.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  369.     sprintf(pktname, "%sP0-%u.%3.3hu", net_data, i, instance);
  370.     if (stat(pktname, &info) == -1)
  371.       ok = 1;
  372.   }
  373. }
  374.  
  375.  
  376. int import(char *fn)
  377. {
  378.   char s[513], s1[513], pktname[MAXPATH], msgdate[31], *ss, *p;
  379.   int f, from, subj, intext, done;
  380.   long textlen;
  381.   struct msghdr mh;
  382.   net_header_rec nh;
  383.   struct date dt;
  384.   struct time tm;
  385.   FILE *fp;
  386.  
  387.   intext = 0;
  388.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  389.   if (f < 0)
  390.     return (1);
  391.   textlen = filelength(f);
  392.   if (textlen > 32767L) {
  393.     fprintf(stderr, "\n ■ Skipping %s - greater than 32K.", fn);
  394.     return (1);
  395.   }
  396.   p = (char *) malloc(textlen + 1);
  397.   if (p == NULL) {
  398.     fprintf(stderr, "\n ■ Unable to allocate %ld bytes.", textlen);
  399.     return (1);
  400.   }
  401.   read(f, (void *) p, textlen);
  402.   close(f);
  403.  
  404.   nh.tosys = net_sysnum;
  405.   nh.fromsys = 32767L;
  406.   nh.fromuser = 0;
  407.   nh.touser = defuser;
  408.   nh.main_type = main_type_email;
  409.   nh.minor_type = 0;
  410.   nh.list_len = 0;
  411.   gettime(&tm);
  412.   getdate(&dt);
  413.   nh.daten = dostounix(&dt, &tm);
  414.   nh.method = 0;
  415.  
  416.   fp = fsh_open(fn, "rb");
  417.   if (!fp) {
  418.     free(p);
  419.     return (1);
  420.   }
  421.   from = subj = done = 0;
  422.   while ((fgets(s, 254, fp)) && !done) {
  423.     if ((s[0] == '\r') || (s[0] == '\n') || (s[0] == 0))
  424.       intext = 1;
  425.     if (!intext) {
  426.       if (s[0] == 4) {
  427.         strcpy(s1, s);
  428.         strcpy(s, &s1[3]);
  429.       }
  430.       if ((strncmpi(s, "reply-to:", 9) == 0)) {
  431.         from = 1;
  432.         ss = strtok(s, ": ");
  433.         ss = strtok(NULL, "\r\n");
  434.         trimstr1(ss);
  435.         /* strncpy(mh.fromUserName, ss, 46); */
  436.         strcpy(mh.fromUserName, ss);
  437.         if (strlen(mh.fromUserName) > 190)
  438.           mh.fromUserName[190] = '\0';
  439.         strcat(mh.fromUserName, "\r\n");
  440.       } else
  441.         if ((strncmpi(s, "from:", 5) == 0) && (!from)) {
  442.         from = 1;
  443.         ss = strtok(s, ": ");
  444.         ss = strtok(NULL, "\r\n");
  445.         trimstr1(ss);
  446.         /* strncpy(mh.fromUserName, ss, 46); */
  447.         strcpy(mh.fromUserName, ss);
  448.         if (strlen(mh.fromUserName) > 190)
  449.           mh.fromUserName[190] = '\0';
  450.         strcat(mh.fromUserName, "\r\n");
  451.       } else
  452.         if ((strncmpi(s, "return-path:", 12) == 0) && (!from)) {
  453.         ss = strtok(s, ": ");
  454.         ss = strtok(NULL, "\r\n");
  455.         trimstr1(ss);
  456.         /* strncpy(mh.fromUserName, ss, 46); */
  457.         strcpy(mh.fromUserName, ss);
  458.         if (strlen(mh.fromUserName) > 190)
  459.           mh.fromUserName[190] = '\0';
  460.         strcat(mh.fromUserName, "\r\n");
  461.       } else
  462.         if ((strncmpi(s, "subject:", 8) == 0) && (!subj)) {
  463.         ss = strtok(s, ": ");
  464.         ss = strtok(NULL, "\r\n");
  465.         trimstr1(ss);
  466.         strcpy(mh.subject, ss);
  467.         if (strlen(mh.subject) > 72)
  468.           mh.subject[72] = '\0';
  469.         else
  470.           mh.subject[strlen(mh.subject)] = '\0';
  471.       } else
  472.         if ((strncmpi(s, "to:", 3) == 0) || (strncmpi(s, "cc:", 3) == 0)) {
  473.         ss = strtok(s, ": ");
  474.         ss = strtok(NULL, "\r\n");
  475.         strcpy(mh.toUserName, ss);
  476.         trimstr1(mh.toUserName);
  477.         if (_fstrstr(mh.toUserName, " "))
  478.           find_name(mh.toUserName);
  479.         else
  480.           mh.toUserName[0] = '\0';
  481.         if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  482.           nh.touser = defuser;
  483.           strcpy(mh.toUserName, postmaster);
  484.         } else
  485.           nh.touser = curuser;
  486.         }
  487.     } else
  488.       done = 1;
  489.   }
  490.   fclose(fp);
  491.   fprintf(stderr, "\n ■ From    : %s", mh.fromUserName);
  492.   fprintf(stderr, " ■ Sent to : %s #%hd", mh.toUserName, nh.touser);
  493.   fprintf(stderr, "\n ■ Subject : %s", mh.subject);
  494.   name_packet(pktname);
  495.   fp = fsh_open(pktname, "wb");
  496.   if (fp < 0) {
  497.     fprintf(stderr, "\n ■ Unable to create packet %s", pktname);
  498.     free(p);
  499.     return (1);
  500.   }
  501.   strncpy(msgdate, ctime(&(time_t) nh.daten), 24);
  502.   msgdate[24] = '\0';
  503.   strcat(msgdate, "\r\n");
  504.   nh.length = textlen + strlen(mh.fromUserName) + strlen(mh.subject) + strlen(msgdate) + 1;
  505.   fwrite(&nh, sizeof(net_header_rec), 1, fp);
  506.   fwrite(mh.subject, sizeof(char), strlen(mh.subject) +1, fp);
  507.   fwrite(mh.fromUserName, sizeof(char), strlen(mh.fromUserName), fp);
  508.   fwrite(msgdate, sizeof(char), strlen(msgdate), fp);
  509.   fwrite(p, sizeof(char), textlen, fp);
  510.   fclose(fp);
  511.   free(p);
  512.   return (0);
  513. }
  514.  
  515. int export(char *fn)
  516. {
  517.   char fn1[121], groupname[81], outfn[121], _temp_buffer[256];
  518.   char *ss, *buffer, *text;
  519.   unsigned stype, ttype;
  520.   int infile, outfile, inloc, outloc, term, ok, i, j;
  521.   net_header_rec nhr;
  522.   struct msghdr mh;
  523.   struct tm *time_msg;
  524.   FILE *fp;
  525.   time_t some;
  526.   char *main_type[] =
  527.   {
  528.     "Network Update", "email by usernum", "post from sub host", "file",
  529.     "post to sub host", "external message", "email by name",
  530.     "NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
  531.     "CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
  532.     "Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info"
  533.   };
  534.  
  535.   if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
  536.     return (1);
  537.   else
  538.     fprintf(stderr, "\n ■ Parsing %s (%ld bytes)", fn, filelength(infile));
  539.  
  540.   if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
  541.     fprintf(stderr, "\n ■ Out of memory allocating input buffer!");
  542.     return (1);
  543.   }
  544.   if ((text = (char *) malloc(32 * 1024)) == NULL) {
  545.     fprintf(stderr, "\n ■ Out of memory allocating output buffer!");
  546.     if (buffer)
  547.       free(buffer);
  548.     buffer = NULL;
  549.     return (1);
  550.   }
  551.   while (read(infile, &nhr, sizeof(nhr))) {
  552.     read(infile, buffer, nhr.length);
  553.     if (nhr.tosys != 32767) {
  554.       fprintf(stderr, "\n ■ Non-Internet system routing via @32767... aborting export.");
  555.       return (1);
  556.     }
  557.     if ((nhr.main_type == main_type_post) ||
  558.         (nhr.main_type == main_type_new_post) ||
  559.         (nhr.main_type == main_type_email_name)) {
  560.       inloc = 0;
  561.       if (nhr.main_type == main_type_email_name) {
  562.         stype = nhr.minor_type;
  563.         inloc = strlen(buffer) + 1;
  564.         strcpy(mh.toUserName, buffer);
  565.         if (nhr.fromsys != net_sysnum) {
  566.           fprintf(stderr, "\n ■ Gate from #%hd@%hd to %s not yet supported",
  567.                   nhr.fromuser, nhr.fromsys, mh.toUserName);
  568.           continue;
  569.         }
  570.       } else
  571.         if (nhr.main_type == main_type_post)
  572.         stype = nhr.minor_type;
  573.       else {
  574.         stype = atoi(&buffer[inloc]);
  575.         sprintf(_temp_buffer, "%u", stype);
  576.         inloc += strlen(_temp_buffer) + 1;
  577.       }
  578.       strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  579.       inloc += strlen(&buffer[inloc]) + 1;
  580.  
  581.       for (term = inloc; buffer[term] != '\r'; term++);
  582.       buffer[term] = '\0';
  583.  
  584.       if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
  585.         if (nhr.main_type != main_type_post) {
  586.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  587.             fprintf(stderr, "\n ■ No match for user #%hd... skipping message!",
  588.                     nhr.fromuser);
  589.             continue;
  590.           }
  591.         } else {
  592.           if (!num_to_name(mh.fromUserName, nhr.fromuser, 1)) {
  593.             fprintf(stderr, "\n ■ No match for user #%hd... skipping message!",
  594.                     nhr.fromuser);
  595.             continue;
  596.           }
  597.         }
  598.       } else {
  599.         strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  600.         strtok(mh.fromUserName, "#");
  601.         if ((nhr.main_type == main_type_post) &&
  602.             (nhr.fromsys != net_sysnum)) {
  603.           sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
  604.           strcat(mh.fromUserName, _temp_buffer);
  605.         }
  606.       }
  607.  
  608.       inloc = term + 2;
  609.  
  610.       while (buffer[inloc] != '\n')
  611.         inloc++;
  612.       inloc++;
  613.  
  614.       if (strncmp(&buffer[inloc], "0R", 3) == 0) {
  615.         while (buffer[inloc] != '\n')
  616.           ++inloc;
  617.         inloc++;
  618.       }
  619.       if (strnicmp(&buffer[inloc], "RE: ", 4) == 0) {
  620.         for (term = inloc; buffer[term] != '\r'; term++);
  621.         buffer[term] = '\0';
  622.         strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  623.         inloc = term + 2;
  624.       }
  625.       if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
  626.           (strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
  627.         for (term = inloc; buffer[term] != '\r'; term++);
  628.         buffer[term] = '\0';
  629.         strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
  630.         inloc = term + 2;
  631.       } else {
  632.         if (nhr.main_type != main_type_email_name) {
  633.           strcpy(mh.toUserName, "ALL\n");
  634.         }
  635.       }
  636.  
  637.       outloc = 0;
  638.       do {
  639.         if (buffer[inloc] == 2) {
  640.           i = inloc + 1;
  641.           for (j = 1; j < 80; j++) {
  642.             if ((buffer[i] == '\r') ||
  643.                 (i > nhr.length))
  644.               break;
  645.             i++;
  646.           }
  647.           if (j < 80) {
  648.             i = (80 - j) / 2;
  649.             for (j = 1; j <= i; j++)
  650.               text[outloc++] = ' ';
  651.           }
  652.           inloc++;
  653.         } else
  654.           if (buffer[inloc] == 3)
  655.           inloc += 2;
  656.         else
  657.           if ((buffer[inloc] == 4) && (buffer[inloc + 1] == '0')) {
  658.           i = inloc;
  659.           for (j = 1; j < 80; j++) {
  660.             if ((buffer[i] == '\r') ||
  661.                 (i > nhr.length))
  662.               break;
  663.             i++;
  664.             inloc++;
  665.           }
  666.           inloc++;
  667.           }
  668.         else
  669.           if (buffer[inloc] >= 127) {
  670.           inloc++;
  671.         } else
  672.           if (buffer[inloc] == 1)
  673.           inloc++;
  674.         else
  675.           text[outloc++] = buffer[inloc++];
  676.       } while (inloc < nhr.length);
  677.  
  678.       text[outloc] = '\0';
  679.  
  680.       switch (nhr.main_type) {
  681.         case main_type_email:
  682.         case main_type_email_name:
  683.           i = 1;
  684.           sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
  685.           while (exist(outfn))
  686.             sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  687.           break;
  688.         case main_type_post:
  689.         case main_type_pre_post:
  690.           i = 1;
  691.           sprintf(outfn, "%sOUTBOUND\\%d.%d", net_data, stype, i);
  692.           while (exist(outfn))
  693.             sprintf(outfn, "%sOUTBOUND\\%d.%d", net_data, stype, ++i);
  694.           break;
  695.         default:
  696.           continue;
  697.       }
  698.  
  699.       fprintf(stderr, "\n ■ Creating: %s", outfn);
  700.       fprintf(stderr, "\n ■ From    : %s", mh.fromUserName);
  701.       if ((nhr.main_type == main_type_post) ||
  702.           (nhr.main_type == main_type_pre_post)) {
  703.         sprintf(fn1, "%sNEWS.RC", net_data);
  704.         fp = fsh_open(fn1, "rt");
  705.         if (!fp)
  706.           fprintf(stderr, "\n ■ %s not found!", fn1);
  707.         else {
  708.           ok = 0;
  709.           while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
  710.             groupname[0] = 0;
  711.             ss = strtok(_temp_buffer, " ");
  712.             if (ss) {
  713.               strcpy(groupname, ss);
  714.               ss = strtok(NULL, " ");
  715.               ss = strtok(NULL, "\r");
  716.               ttype = atoi(ss);
  717.               if (ttype == stype)
  718.                 ok = 1;
  719.             }
  720.           }
  721.           *ss = NULL;
  722.           fclose(fp);
  723.         }
  724.       }
  725.       if ((nhr.main_type == main_type_email) ||
  726.           (nhr.main_type == main_type_email_name)) {
  727.         fprintf(stderr, "\n ■ To      : %s", strlwr(mh.toUserName));
  728.       } else {
  729.         if (groupname[0] == 0)
  730.           fprintf(stderr, "\n ■ No match for subtype %u in NEWS.RC", stype);
  731.         else
  732.           fprintf(stderr, "\n ■ Post to : %s", groupname);
  733.       }
  734.  
  735.       fprintf(stderr, "\n ■ Subject : %s", mh.subject);
  736.  
  737.       outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
  738.       if (outfile == -1) {
  739.         if (buffer)
  740.           free(buffer);
  741.         if (text)
  742.           free(text);
  743.         fprintf(stderr, "\n ■ Unable to open output file %s", outfn);
  744.         return (1);
  745.       }
  746.       time(&some);
  747.       time_msg = localtime(&some);
  748.       strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
  749.       for (j = 0; j < strlen(mh.fromUserName); j++)
  750.         if (mh.fromUserName[j] == ' ')
  751.           mh.fromUserName[j] = '_';
  752.       if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)))
  753.         sprintf(_temp_buffer, "From: nowhere@no.net (%s)\n",
  754.                 mh.fromUserName);
  755.       else
  756.         sprintf(_temp_buffer, "From: %s@%s (%s)\n",
  757.                 POPNAME, DOMAIN, mh.fromUserName);
  758.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  759.       /* delay(2000); */
  760.       sprintf(_temp_buffer, "Message-ID: <%lx-%s@%s>\n",
  761.               time(NULL), POPNAME, DOMAIN);
  762.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  763.       if ((nhr.main_type == main_type_email) ||
  764.           (nhr.main_type == main_type_email_name)) {
  765.         sprintf(_temp_buffer, "To: %s\n",
  766.                 mh.toUserName);
  767.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  768.       } else {
  769.         sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
  770.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  771.       }
  772.       sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
  773.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  774.       sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
  775.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  776.       sprintf(_temp_buffer, "Organization: %s * @%hu.%s * %s\n",
  777.               syscfg.systemname, net_sysnum, net_name, syscfg.systemphone);
  778.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  779.       if ((!spam) && ((nhr.main_type != main_type_post) || (nhr.main_type != main_type_new_post))) {
  780.         sprintf(_temp_buffer, "Reply-To: %s@%s (%s)\n",
  781.                 POPNAME, DOMAIN, mh.fromUserName);
  782.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  783.       }
  784.       sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n", VERSION);
  785.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  786.  
  787.       if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post))) {
  788.         sprintf(_temp_buffer, "Reply to: %s@%s (%s)\n\n",
  789.                 POPNAME, DOMAIN, mh.fromUserName);
  790.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  791.       }
  792.       if ((nhr.main_type != main_type_email) &&
  793.           (nhr.main_type != main_type_email_name) &&
  794.           (strncmp(mh.toUserName, "ALL", 3) != 0)) {
  795.         sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
  796.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  797.       }
  798.       write(outfile, text, strlen(text));
  799.  
  800.       if (tagfile[0] == 0) {
  801.         sprintf(_temp_buffer, "\n\nOrigin: %s * %s * @%hu.FILEnet\n\n",
  802.                 syscfg.systemname, syscfg.systemphone, net_sysnum);
  803.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  804.       } else {
  805.         fp = fsh_open(tagfile, "rt");
  806.         if (!fp)
  807.           fprintf(stderr, "\n ■ Error reading %s.", tagfile);
  808.         else {
  809.           sprintf(_temp_buffer, "\n\n");
  810.           write(outfile, _temp_buffer, strlen(_temp_buffer));
  811.           while (fgets(_temp_buffer, 120, fp)) {
  812.             for (i = 0; ((i < strlen(_temp_buffer)) &&
  813.                 (_temp_buffer[i] != '\r') && (_temp_buffer[i] != '\n')); i++)
  814.               if ((_temp_buffer[i] < 32) || (_temp_buffer[i] > 126))
  815.                   _temp_buffer[i] = 32;
  816.             write(outfile, _temp_buffer, strlen(_temp_buffer));
  817.           }
  818.           fclose(fp);
  819.           sprintf(_temp_buffer, "\n\n");
  820.           write(outfile, _temp_buffer, strlen(_temp_buffer));
  821.         }
  822.       }
  823.       close(outfile);
  824.     } else {
  825.       if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x14))
  826.         fprintf(stderr, "\n ■ %s message skipped",
  827.                 main_type[nhr.main_type - 1]);
  828.       else
  829.         fprintf(stderr, "\n ■ Unknown Main_type %hd skipped", nhr.main_type);
  830.     }
  831.   }
  832.   close(infile);
  833.   unlink(fn);
  834.   if (text)
  835.     free(text);
  836.   if (buffer)
  837.     free(buffer);
  838.   return (0);
  839. }
  840.  
  841. void get_dir(char *s, int be)
  842. {
  843.   strcpy(s, "X:\\");
  844.   s[0] = 'A' + getdisk();
  845.   getcurdir(0, s + 3);
  846.   if (be) {
  847.     if (s[strlen(s) - 1] != '\\')
  848.       strcat(s, "\\");
  849.   }
  850. }
  851.  
  852. int main(int argc, char *argv[])
  853. {
  854.   char fn[MAXPATH], *ss;
  855.   int f, f1, maxmail, i;
  856.   struct ffblk ff;
  857.  
  858.   /* 0      1               2          3          4       5        6       */
  859.   /* exp s32767.net,      net_data, net_sysnum, popname, domain, net_name  */
  860.   /* exp netlog/contact,  net_data,   sysnum,     sent    recd   totaltime */
  861.  
  862.   fprintf(stderr, "\n ■ PPP Import/Export %s", VERSION);
  863.   if (argc != 7) {
  864.     fprintf(stderr, "\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
  865.     if (argc > 1) {
  866.       fprintf(stderr, "Command line was: ");
  867.       for (i = 0; i < argc; i++)
  868.         fprintf(stderr, "%s ", argv[i]);
  869.       fprintf(stderr, "\n\n");
  870.     }
  871.     return (1);
  872.   }
  873.   get_dir(maindir, 1);
  874.   strcpy(net_data, argv[2]);
  875.   sprintf(fn, "%s%s", net_data, argv[1]);
  876.   net_name = argv[6];
  877.   strcpy(POPNAME, argv[4]);
  878.   strcpy(DOMAIN, argv[5]);
  879.   net_sysnum = atoi(argv[3]);
  880.   tagfile[0] = 0;
  881.   spam = 0;
  882.  
  883.   f = sh_open1("CONFIG.DAT", O_RDONLY | O_BINARY);
  884.   if (f < 0) {
  885.     fprintf(stderr, "Could not open CONFIG.DAT!\n\n");
  886.     return (1);
  887.   }
  888.   read(f, (void *) &syscfg, sizeof(configrec));
  889.   close(f);
  890.  
  891.   ss = getenv("WWIV_INSTANCE");
  892.   if (ss) {
  893.     instance = atoi(ss);
  894.     if (instance >= 1000) {
  895.       fprintf(stderr, "\n ■ WWIV_INSTANCE set to %hd.  Can only be 1..999!",
  896.               instance);
  897.       instance = 1;
  898.     }
  899.   } else
  900.     instance = 1;
  901.  
  902.   parse_net_ini();
  903.  
  904.   fprintf(stderr, "\n ■ Postmaster default account set to %s #%hd.",
  905.           strupr(postmaster), defuser);
  906.   fprintf(stderr, "\n ■ Using user %s on outbound Internet mail.",
  907.           use_alias ? "aliases" : "real names");
  908.   if (spam)
  909.     fprintf(stderr, "\n ■ Using bogus originating address on newsgroup posts.");
  910.   if (tagfile[0] != 0)
  911.     fprintf(stderr, "\n ■ Using signature file : %s", tagfile);
  912.  
  913.   export(fn);
  914.  
  915.   sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
  916.   f1 = findfirst(fn, &ff, 0);
  917.   maxmail = 14;
  918.   while ((f1 == 0) && maxmail) {
  919.     sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  920.     if (!import(fn)) {
  921.       unlink(fn);
  922.       --maxmail;
  923.     }
  924.     f1 = findnext(&ff);
  925.   }
  926.   return 0;
  927. }
  928.