home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC15B17.ZIP / EXP.C next >
Text File  |  1997-03-02  |  24KB  |  837 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];
  35. unsigned short net_sysnum, defuser, num_users, use_alias, instance, curuser;
  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. int num_to_name(char *where, int whichuser, int alias);
  155.  
  156. void parse_net_ini(void)
  157. {
  158.  
  159.   char line[121], *ss;
  160.   FILE *fp;
  161.  
  162.   defuser=1;
  163.   use_alias = 1;
  164.   fp = fsh_open("NET.INI", "rt");
  165.   if (!fp)
  166.     return;
  167.   while (fgets(line, 80, fp)) {
  168.     ss = NULL;
  169.     stripspace(line);
  170.     /* fprintf(stderr, "\nCurrent : %s\n", line); */
  171.     if ((line[0] == ';') || (line[0] == '\n') || (line[0] == '['))
  172.       continue;
  173.     if (strlen(line) == 0)
  174.       continue;
  175.     if (strnicmp(line, "POSTMASTER", 10) == 0) {
  176.       ss = strtok(line, "=");
  177.       if (ss) {
  178.         ss = strtok(NULL, "\n");
  179.         if (ss)
  180.           defuser = atoi(ss);
  181.       }
  182.     }
  183.     if (strnicmp(line, "REALNAME", 8) == 0) {
  184.       ss = strtok(line, "=");
  185.       if (ss) {
  186.         ss = strtok(NULL, "\n");
  187.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  188.           use_alias=0;
  189.       }
  190.     }
  191.   }
  192.   num_to_name(postmaster, defuser, 1);
  193.   fclose(fp);
  194.   return;
  195. }
  196.  
  197. static unsigned char *trimstr1(unsigned char *s)
  198. {
  199.   int i;
  200.   static char *whitespace = " \r\n\t";
  201.  
  202.   i = (int) strlen(s);
  203.   while ((i > 0) && ((char *) _fstrchr(whitespace, s[i-1]) != NULL))
  204.     --i;
  205.   while ((i > 0) && ((char *) _fstrchr(whitespace, *s) != NULL)) {
  206.     memmove(s, s+1, --i);
  207.   }
  208.   s[i] = 0;
  209.   return(s);
  210. }
  211.  
  212. int num_to_name(char *where, int whichuser, int alias)
  213. {
  214.   int userfile, num_users, found;
  215.   userrec ur;
  216.   long pos;
  217.   char fn[MAXPATH];
  218.  
  219.   found = 0;
  220.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  221.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  222.   if (userfile < 0) {
  223.     fprintf(stderr, "\n ■ Cannot open %s.", fn);
  224.     return(found);
  225.   }
  226.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  227.  
  228.   if (whichuser > num_users) {
  229.     fprintf(stderr, "\n ■ User #%d out of range.", whichuser);
  230.     return(found);
  231.   }
  232.   pos = ((long) sizeof(userrec) * ((long) whichuser));
  233.   lseek(userfile, pos, SEEK_SET);
  234.   read(userfile, &ur, sizeof(userrec));
  235.   if (ur.realname[0] == 0)
  236.     fprintf(stderr, "\n ■ User #%d has blank real name field!", whichuser);
  237.   else {
  238.     if (ur.inact == inact_deleted)
  239.       fprintf(stderr, "\n ■ User #%d is marked as deleted!", whichuser);
  240.     else {
  241.       if (!alias)
  242.         strcpy(where, ur.realname);
  243.       else {
  244.         strlwr(ur.name);
  245.         strcpy(where, ur.name);
  246.       }
  247.       found = 1;
  248.     }
  249.   }
  250.   close(userfile);
  251.   return(found);
  252. }
  253.  
  254. int name_to_num(char *name)
  255. {
  256.   int userfile, usernum;
  257.   userrec ur;
  258.   long pos;
  259.   char fn[MAXPATH];
  260.  
  261.   fprintf(stderr, "\n ■ Searching for user %s", strupr(name));
  262.   stripspace(name);
  263.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  264.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  265.   if (userfile < 0) {
  266.     printf("\n ■ Cannot open %s", fn);
  267.     return(0);
  268.   }
  269.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  270.  
  271.   for (usernum = 1; usernum < num_users; usernum++) {
  272.     pos = ((long) sizeof(userrec) * ((long) usernum));
  273.     lseek(userfile, pos, SEEK_SET);
  274.     read(userfile, &ur, sizeof(userrec));
  275.     stripspace(ur.realname);
  276.     stripspace(ur.name);
  277.     if ((strcmpi(ur.realname, name) == 0) || (strcmpi(ur.name, name) == 0)) {
  278.       if (ur.inact == inact_deleted) {
  279.         fprintf(stderr, "... user #%d is deleted account.", usernum);
  280.         usernum = 0;
  281.         break;
  282.       } else {
  283.         fprintf(stderr, "... matched user #%d.", usernum);
  284.         break;
  285.       }
  286.     }
  287.   }
  288.   userfile = close(userfile);
  289.  
  290.   if (usernum >= num_users) {
  291.     fprintf(stderr, "... no match found.");
  292.     return(0);
  293.   }
  294.   return(usernum);
  295. }
  296.  
  297. char *find_name(char *name)
  298. {
  299.   char *ss;
  300.  
  301.   curuser = 0;
  302.   if (strcspn(name, "(") != strlen(name)) {
  303.     ss = strtok(name, "(");
  304.     ss = strtok(NULL, ")");
  305.     strcpy(name, ss);
  306.     curuser = name_to_num(name);
  307.   } else
  308.     if (strcspn(name, "\"") != strlen(name)) {
  309.     ss = strtok(name, "\"");
  310.     ss = strtok(NULL, "\"");
  311.     strcpy(name, ss);
  312.     curuser = name_to_num(name);
  313.   } else
  314.     if (strcspn(name, "<") != strlen(name)) {
  315.       if ((strcspn(name, " ")) < (strcspn(name, "<"))) {
  316.         ss = strtok(name, "<");
  317.         trimstr1(ss);
  318.         strcpy(name, ss);
  319.         curuser = name_to_num(name);
  320.       } else {
  321.         fprintf(stderr, "\nName is *after* host in \"%s\"", name);
  322.       }
  323.     }
  324.   if (curuser == 0)
  325.     return('\0');
  326.   else
  327.     return(ss);
  328. }
  329.  
  330. void name_packet(char *pktname)
  331. {
  332.   int ok;
  333.   struct stat info;
  334.   unsigned i;
  335.  
  336.   ok = 0;
  337.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  338.     sprintf(pktname, "%sP0-%u.%3.3hu", net_data, i, instance);
  339.     if (stat(pktname, &info) == -1)
  340.       ok = 1;
  341.   }
  342. }
  343.  
  344.  
  345. int import(char *fn)
  346. {
  347.   char s[513], pktname[MAXPATH], msgdate[31], *ss, *p;
  348.   int f, from, subj, intext, done;
  349.   long textlen;
  350.   struct msghdr mh;
  351.   net_header_rec nh;
  352.   struct date dt;
  353.   struct time tm;
  354.   FILE *fp;
  355.  
  356.   intext = 0;
  357.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  358.   if (f < 0)
  359.     return(1);
  360.   textlen = filelength(f);
  361.   if (textlen > 32767L) {
  362.     fprintf(stderr, "\n ■ Skipping %s - greater than 32K.", fn);
  363.     return(1);
  364.   }
  365.   p = (char *) malloc(textlen + 1);
  366.   if (p==NULL) {
  367.     fprintf(stderr, "\n ■ Unable to allocate %ld bytes.", textlen);
  368.     return(1);
  369.   }
  370.   read(f, (void *) p, textlen);
  371.   close(f);
  372.  
  373.   nh.tosys = net_sysnum;
  374.   nh.fromsys = 32767L;
  375.   nh.fromuser = 0;
  376.   nh.touser = defuser;
  377.   nh.main_type = main_type_email;
  378.   nh.minor_type = 0;
  379.   nh.list_len = 0;
  380.   gettime(&tm);
  381.   getdate(&dt);
  382.   nh.daten = dostounix(&dt, &tm);
  383.   nh.method = 0;
  384.  
  385.   fp = fsh_open(fn, "rb");
  386.   if (!fp) {
  387.     free(p);
  388.     return(1);
  389.   }
  390.   from = subj = done = 0;
  391.   while ((fgets(s, 254, fp)) && !done) {
  392.     if ((s[0] == '\r') || (s[0] == '\n') || (s[0] == 0))
  393.       intext = 1;
  394.     if (!intext) {
  395.       if ((strncmpi(s, "reply-to:", 9) == 0)) {
  396.         from = 1;
  397.         ss = strtok(s, ": ");
  398.         ss = strtok(NULL, "\r\n");
  399.         trimstr1(ss);
  400.         /* strncpy(mh.fromUserName, ss, 46); */
  401.         strcpy(mh.fromUserName, ss);
  402.         if (strlen(mh.fromUserName) > 190)
  403.           mh.fromUserName[190] = '\0';
  404.         strcat(mh.fromUserName, "\r\n");
  405.       } else if ((strncmpi(s, "from:", 5) == 0) && (!from)) {
  406.         from = 1;
  407.         ss = strtok(s, ": ");
  408.         ss = strtok(NULL, "\r\n");
  409.         trimstr1(ss);
  410.         /* strncpy(mh.fromUserName, ss, 46); */
  411.         strcpy(mh.fromUserName, ss);
  412.         if (strlen(mh.fromUserName) > 190);
  413.         mh.fromUserName[190] = '\0';
  414.         strcat(mh.fromUserName, "\r\n");
  415.       } else if ((strncmpi(s, "return-path:", 12) == 0) && (!from)) {
  416.         ss = strtok(s, ": ");
  417.         ss = strtok(NULL, "\r\n");
  418.         trimstr1(ss);
  419.         /* strncpy(mh.fromUserName, ss, 46); */
  420.         strcpy(mh.fromUserName, ss);
  421.         if (strlen(mh.fromUserName) > 190)
  422.           mh.fromUserName[190] = '\0';
  423.         strcat(mh.fromUserName, "\r\n");
  424.       } else if ((strncmpi(s, "subject:", 8) == 0) && (!subj)) {
  425.         ss = strtok(s, ": ");
  426.         ss = strtok(NULL, "\r\n");
  427.         trimstr1(ss);
  428.         strcpy(mh.subject, ss);
  429.         if (strlen(mh.subject) > 72)
  430.           mh.subject[72] = '\0';
  431.         else
  432.           mh.subject[strlen(mh.subject)] = '\0';
  433.       } else if ((strncmpi(s, "to:", 3) == 0) || (strncmpi(s, "cc:", 3) == 0)) {
  434.         ss = strtok(s, ": ");
  435.         ss = strtok(NULL, "\r\n");
  436.         strcpy(mh.toUserName, ss);
  437.         trimstr1(mh.toUserName);
  438.         if (_fstrstr(mh.toUserName, " "))
  439.           find_name(mh.toUserName);
  440.         else
  441.           mh.toUserName[0] = '\0';
  442.         if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  443.           nh.touser = defuser;
  444.           strcpy(mh.toUserName, postmaster);
  445.         } else
  446.           nh.touser = curuser;
  447.        }
  448.     } else
  449.       done = 1;
  450.   }
  451.   fclose(fp);
  452.   fprintf(stderr, "\n ■ From    : %s", mh.fromUserName);
  453.   fprintf(stderr, " ■ Sent to : %s #%hd", mh.toUserName, nh.touser);
  454.   fprintf(stderr, "\n ■ Subject : %s", mh.subject);
  455.   name_packet(pktname);
  456.   fp = fsh_open(pktname, "wb");
  457.   if (fp < 0) {
  458.     fprintf(stderr, "\n ■ Unable to create packet %s", pktname);
  459.     free(p);
  460.     return(1);
  461.   }
  462.   strncpy(msgdate, ctime(&(time_t) nh.daten), 24);
  463.   msgdate[24] = '\0';
  464.   strcat(msgdate, "\r\n");
  465.   nh.length = textlen + strlen(mh.fromUserName) + strlen(mh.subject) + strlen(msgdate) + 1;
  466.   fwrite(&nh, sizeof(net_header_rec), 1, fp);
  467.   fwrite(mh.subject, sizeof(char), strlen(mh.subject) +1, fp);
  468.   fwrite(mh.fromUserName, sizeof(char), strlen(mh.fromUserName), fp);
  469.   fwrite(msgdate, sizeof(char), strlen(msgdate), fp);
  470.   fwrite(p, sizeof(char), textlen, fp);
  471.   fclose(fp);
  472.   free(p);
  473.   return(0);
  474. }
  475.  
  476. int export(char *fn)
  477. {
  478.   char fn1[121], groupname[81], outfn[121], _temp_buffer[256];
  479.   char *ss, *buffer, *text;
  480.   unsigned stype, ttype;
  481.   int infile, outfile, inloc, outloc, term, ok, i, j;
  482.   net_header_rec nhr;
  483.   struct msghdr mh;
  484.   struct tm *time_msg;
  485.   FILE *fp;
  486.   time_t some;
  487.   char *main_type[] =
  488.   {
  489.     "Network Update", "email by usernum", "post from sub host", "file",
  490.     "post to sub host", "external message", "email by name",
  491.     "NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
  492.     "CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
  493.     "Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info"
  494.   };
  495.  
  496.   if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
  497.     return(1);
  498.   else
  499.     fprintf(stderr, "\n ■ Parsing %s (%ld bytes)", fn, filelength(infile));
  500.  
  501.   if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
  502.     fprintf(stderr, "\n ■ Out of memory allocating input buffer!");
  503.     return(1);
  504.   }
  505.   if ((text = (char *) malloc(32 * 1024)) == NULL) {
  506.     fprintf(stderr, "\n ■ Out of memory allocating output buffer!");
  507.     if (buffer)
  508.       free(buffer);
  509.     buffer = NULL;
  510.     return(1);
  511.   }
  512.   while (read(infile, &nhr, sizeof(nhr))) {
  513.     read(infile, buffer, nhr.length);
  514.     if (nhr.tosys != 32767) {
  515.       fprintf(stderr, "\n ■ Non-Internet system routing via @32767... aborting export.");
  516.       return(1);
  517.     }
  518.     if ((nhr.main_type == main_type_post) ||
  519.         (nhr.main_type == main_type_new_post) ||
  520.         (nhr.main_type == main_type_email_name)) {
  521.       inloc = 0;
  522.       if (nhr.main_type == main_type_email_name) {
  523.         stype = nhr.minor_type;
  524.         inloc = strlen(buffer) + 1;
  525.         strcpy(mh.toUserName, buffer);
  526.         if (nhr.fromsys != net_sysnum) {
  527.           fprintf(stderr, "\n ■ Gate from #%hd@%hd to %s not yet supported",
  528.                   nhr.fromuser, nhr.fromsys, mh.toUserName);
  529.           continue;
  530.         }
  531.       } else
  532.         if (nhr.main_type == main_type_post)
  533.         stype = nhr.minor_type;
  534.       else {
  535.         stype = atoi(&buffer[inloc]);
  536.         sprintf(_temp_buffer, "%u", stype);
  537.         inloc += strlen(_temp_buffer) + 1;
  538.       }
  539.       strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  540.       inloc += strlen(&buffer[inloc]) + 1;
  541.  
  542.       for (term = inloc; buffer[term] != '\r'; term++);
  543.       buffer[term] = '\0';
  544.  
  545.       if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
  546.         if (nhr.main_type != main_type_post) {
  547.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  548.             fprintf(stderr, "\n ■ No match for user #%hd... skipping message!",
  549.                     nhr.fromuser);
  550.             continue;
  551.           }
  552.         } else {
  553.           if (!num_to_name(mh.fromUserName, nhr.fromuser, 1)) {
  554.             fprintf(stderr, "\n ■ No match for user #%hd... skipping message!",
  555.                     nhr.fromuser);
  556.             continue;
  557.           }
  558.         }
  559.       } else {
  560.         strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  561.         strtok(mh.fromUserName, "#");
  562.         if ((nhr.main_type == main_type_post) &&
  563.             (nhr.fromsys != net_sysnum)) {
  564.           sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
  565.           strcat(mh.fromUserName, _temp_buffer);
  566.         }
  567.       }
  568.  
  569.       inloc = term + 2;
  570.  
  571.       while (buffer[inloc] != '\n')
  572.         inloc++;
  573.       inloc++;
  574.  
  575.       if (strncmp(&buffer[inloc], "0R", 3) == 0) {
  576.         while (buffer[inloc] != '\n')
  577.           ++inloc;
  578.         inloc++;
  579.       }
  580.       if (strnicmp(&buffer[inloc], "re:", 3) == 0) {
  581.         for (term = inloc; buffer[term] != '\r'; term++);
  582.         buffer[term] = '\0';
  583.         strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  584.         inloc = term + 2;
  585.       }
  586.       if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
  587.           (strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
  588.         for (term = inloc; buffer[term] != '\r'; term++);
  589.         buffer[term] = '\0';
  590.         strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
  591.         inloc = term + 2;
  592.       } else {
  593.         if (nhr.main_type != main_type_email_name) {
  594.           strcpy(mh.toUserName, "ALL\n");
  595.         }
  596.       }
  597.  
  598.       outloc = 0;
  599.       do {
  600.         if (buffer[inloc] == 2) {
  601.           i = inloc + 1;
  602.           for (j = 1; j < 80; j++) {
  603.             if ((buffer[i] == '\r') ||
  604.                 (i > nhr.length))
  605.               break;
  606.             i++;
  607.           }
  608.           if (j < 80) {
  609.             i = (80 - j) / 2;
  610.             for (j = 1; j <= i; j++)
  611.               text[outloc++] = ' ';
  612.           }
  613.           inloc++;
  614.         } else
  615.           if (buffer[inloc] == 3)
  616.           inloc += 2;
  617.         else
  618.           if ((buffer[inloc] == 4) && (buffer[inloc + 1] == '0')) {
  619.           i = inloc;
  620.           for (j = 1; j < 80; j++) {
  621.             if ((buffer[i] == '\r') ||
  622.                 (i > nhr.length))
  623.               break;
  624.             i++;
  625.             inloc++;
  626.           }
  627.           inloc++;
  628.           }
  629.         else
  630.           if (buffer[inloc] >= 127) {
  631.           inloc++;
  632.         } else
  633.           if (buffer[inloc] == 1)
  634.           inloc++;
  635.         else
  636.           text[outloc++] = buffer[inloc++];
  637.       } while (inloc < nhr.length);
  638.  
  639.       text[outloc] = '\0';
  640.  
  641.       switch (nhr.main_type) {
  642.       case main_type_email:
  643.       case main_type_email_name:
  644.           i = 1;
  645.           sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
  646.           while (exist(outfn))
  647.             sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  648.           break;
  649.       case main_type_post:
  650.       case main_type_pre_post:
  651.           i = 1;
  652.           sprintf(outfn, "%sOUTBOUND\\%d.%d", net_data, stype, i);
  653.           while (exist(outfn))
  654.             sprintf(outfn, "%sOUTBOUND\\%d.%d", net_data, stype, ++i);
  655.           break;
  656.       default:
  657.           continue;
  658.       }
  659.  
  660.       fprintf(stderr, "\n ■ Creating: %s", outfn);
  661.       fprintf(stderr, "\n ■ From    : %s", mh.fromUserName);
  662.       if ((nhr.main_type == main_type_post) ||
  663.           (nhr.main_type == main_type_pre_post)) {
  664.         sprintf(fn1, "%sNEWS.RC", net_data);
  665.         fp = fsh_open(fn1, "rt");
  666.         if (!fp)
  667.           fprintf(stderr, "\n ■ %s not found!", fn1);
  668.         else {
  669.           ok = 0;
  670.           while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
  671.             groupname[0] = 0;
  672.             ss = strtok(_temp_buffer, " ");
  673.             if (ss) {
  674.               strcpy(groupname, ss);
  675.               ss = strtok(NULL, " ");
  676.               ss = strtok(NULL, "\r");
  677.               ttype = atoi(ss);
  678.               if (ttype == stype)
  679.                 ok = 1;
  680.             }
  681.           }
  682.           *ss = NULL;
  683.           fclose(fp);
  684.         }
  685.       }
  686.       if ((nhr.main_type == main_type_email) ||
  687.           (nhr.main_type == main_type_email_name)) {
  688.         fprintf(stderr, "\n ■ To      : %s", strlwr(mh.toUserName));
  689.       } else {
  690.         if (groupname[0] == 0)
  691.           fprintf(stderr, "\n ■ No match for subtype %u in NEWS.RC", stype);
  692.         else
  693.           fprintf(stderr, "\n ■ Post to : %s", groupname);
  694.       }
  695.  
  696.       fprintf(stderr, "\n ■ Subject : %s", mh.subject);
  697.  
  698.       outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
  699.       if (outfile == -1) {
  700.         if (buffer)
  701.           free(buffer);
  702.         if (text)
  703.           free(text);
  704.         fprintf(stderr, "\n ■ Unable to open output file %s", outfn);
  705.         return(1);
  706.       }
  707.       time(&some);
  708.       time_msg = localtime(&some);
  709.       strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
  710.       sprintf(_temp_buffer, "From: %s@%s (%s)\n",
  711.               POPNAME, DOMAIN, mh.fromUserName);
  712.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  713.       /* delay(2000); */
  714.       sprintf(_temp_buffer, "Message-ID: <%lx-%s@%s>\n",
  715.               time(NULL), POPNAME, DOMAIN);
  716.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  717.       if ((nhr.main_type == main_type_email) ||
  718.           (nhr.main_type == main_type_email_name)) {
  719.         sprintf(_temp_buffer, "To: %s\n",
  720.                 mh.toUserName);
  721.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  722.       } else {
  723.         sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
  724.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  725.       }
  726.       sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
  727.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  728.       sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
  729.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  730.       sprintf(_temp_buffer, "Organization: %s * @%hu.%s * %s\n",
  731.               syscfg.systemname, net_sysnum, net_name, syscfg.systemphone);
  732.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  733.       sprintf(_temp_buffer, "Reply-To: %s@%s (%s)\n",
  734.               POPNAME, DOMAIN, mh.fromUserName);
  735.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  736.       sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n",VERSION);
  737.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  738.  
  739.       if ((nhr.main_type != main_type_email) &&
  740.           (nhr.main_type != main_type_email_name)) {
  741.         sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
  742.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  743.       }
  744.       write(outfile, text, strlen(text));
  745.  
  746.       sprintf(_temp_buffer, "\n\nOrigin: %s * %s * @%hu.FILEnet\n\n",
  747.               syscfg.systemname, syscfg.systemphone, net_sysnum);
  748.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  749.       close(outfile);
  750.     } else {
  751.       if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x14))
  752.         fprintf(stderr, "\n ■ %s message skipped",
  753.                 main_type[nhr.main_type - 1]);
  754.       else
  755.         fprintf(stderr, "\n ■ Unknown Main_type %hd skipped", nhr.main_type);
  756.     }
  757.   }
  758.   close(infile);
  759.   unlink(fn);
  760.   if (text)
  761.     free(text);
  762.   if (buffer)
  763.     free(buffer);
  764.   return(0);
  765. }
  766.  
  767. int main(int argc, char *argv[])
  768. {
  769.   char s[MAXPATH], fn[MAXPATH], *ss;
  770.   int f, f1, maxmail,i;
  771.   struct ffblk ff;
  772.  
  773.   /* 0      1               2          3          4       5        6       */
  774.   /* exp s32767.net,      net_data, net_sysnum, popname, domain, net_name  */
  775.   /* exp netlog/contact,  net_data,   sysnum,     sent    recd   totaltime */
  776.  
  777.   if (argc != 7) {
  778.     fprintf(stderr, "Version: WWIV PPP Project %s\n\n",VERSION);
  779.     fprintf(stderr, "\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
  780.     if (argc > 1) {
  781.       fprintf(stderr, "Command line was: ");
  782.       for (i=0; i<argc; i++)
  783.         fprintf(stderr, "%s ",argv[i]);
  784.       fprintf(stderr, "\n\n");
  785.     }
  786.     return(1);
  787.   }
  788.   strcpy(net_data, argv[2]);
  789.   sprintf(fn, "%s%s", net_data, argv[1]);
  790.   net_name = argv[6];
  791.   strcpy(POPNAME, argv[4]);
  792.   strcpy(DOMAIN, argv[5]);
  793.   net_sysnum = atoi(argv[3]);
  794.  
  795.   f=sh_open1("CONFIG.DAT",O_RDONLY | O_BINARY);
  796.   if (f < 0) {
  797.     fprintf(stderr, "Could not open CONFIG.DAT!\n\n");
  798.     return(1);
  799.   }
  800.   read(f, (void *) &syscfg, sizeof(configrec));
  801.   close(f);
  802.  
  803.   parse_net_ini();
  804.   export(fn);
  805.  
  806.   ss = getenv("WWIV_INSTANCE");
  807.   if (ss) {
  808.     instance = atoi(ss);
  809.     if (instance >= 1000) {
  810.       fprintf(stderr, "\n ■ WWIV_INSTANCE set to %hd.  Can only be 1..999!",
  811.               instance);
  812.       instance = 1;
  813.     }
  814.   } else
  815.     instance = 1;
  816.  
  817.   sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
  818.   f1 = findfirst(fn, &ff, 0);
  819.   if (f1 == 0) {
  820.     fprintf(stderr, "\n ■ Importing mail from %sSPOOL.", net_data);
  821.     fprintf(stderr, "\n ■ Postmaster set to %s #%hd.",
  822.             postmaster, defuser);
  823.     fprintf(stderr, "\n ■ Using %s on outbound Internet email.",
  824.             use_alias ? "aliases" : "real names");
  825.   }
  826.   maxmail = 14;
  827.   while ((f1 == 0) && maxmail) {
  828.     sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  829.     if (!import(fn)) {
  830.       unlink(fn);
  831.       --maxmail;
  832.     }
  833.     f1 = findnext(&ff);
  834.   }
  835.   return 0;
  836. }
  837.