home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC15B23.ZIP / EXP.C < prev    next >
Text File  |  1997-03-31  |  28KB  |  960 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. unsigned char *strrep(char *str, char old, char New)
  279. {
  280.   int i;
  281.  
  282.   for (i = 0; str[i]; i++)
  283.     if (str[i] == old)
  284.       str[i] = New;
  285.   return (str);
  286. }
  287.  
  288.  
  289. int name_to_num(char *name)
  290. {
  291.   int userfile, usernum;
  292.   userrec ur;
  293.   long pos;
  294.   char fn[MAXPATH], ur_name[60], ur_realname[60];
  295.  
  296.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  297.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  298.   if (userfile < 0) {
  299.     fprintf(stderr, "\n ■ Cannot open %s", fn);
  300.     return (0);
  301.   } else
  302.     fprintf(stderr, "\n ■ Searching for user \"%s\"...", name);
  303.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  304.  
  305.   for (usernum = 1; usernum < num_users; usernum++) {
  306.     pos = ((long) sizeof(userrec) * ((long) usernum));
  307.     lseek(userfile, pos, SEEK_SET);
  308.     read(userfile, &ur, sizeof(userrec));
  309.     strcpy(ur_realname, ur.realname);
  310.     strrep(ur_realname, ' ', '_');
  311.     strcpy(ur_name, ur.name);
  312.     strrep(ur_name, ' ', '_');
  313.     if ((strcmpi(ur.realname, name) == 0) || (strcmpi(ur_realname, name) == 0) ||
  314.         (strcmpi(ur.name, name) == 0) || (strcmpi(ur_name, name) == 0)) {
  315.       if (ur.inact == inact_deleted) {
  316.         fprintf(stderr, " user #%d is deleted account.", usernum);
  317.         usernum = 0;
  318.         break;
  319.       } else {
  320.         fprintf(stderr, " matched user #%d.", usernum);
  321.         break;
  322.       }
  323.     }
  324.   }
  325.   userfile = close(userfile);
  326.  
  327.   if (usernum >= num_users) {
  328.     fprintf(stderr, "... no match found.");
  329.     return (0);
  330.   }
  331.   return (usernum);
  332. }
  333.  
  334. char *find_name(char *name)
  335. {
  336.   char *ss;
  337.  
  338.   curuser = 0;
  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.     ss = strtok(name, "\"");
  347.     ss = strtok(NULL, "\"");
  348.     strcpy(name, ss);
  349.     curuser = name_to_num(name);
  350.   } else
  351.     if (strcspn(name, "<") != strlen(name)) {
  352.     if ((strcspn(name, " ")) < (strcspn(name, "<"))) {
  353.       ss = strtok(name, "<");
  354.       trimstr1(ss);
  355.       strcpy(name, ss);
  356.       curuser = name_to_num(name);
  357.     } else {
  358.       fprintf(stderr, "\nName is *after* host in \"%s\"", name);
  359.     }
  360.     }
  361.   if (curuser == 0)
  362.     return ('\0');
  363.   else
  364.     return (ss);
  365. }
  366.  
  367. void name_packet(char *pktname)
  368. {
  369.   int ok;
  370.   struct stat info;
  371.   unsigned i;
  372.  
  373.   ok = 0;
  374.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  375.     sprintf(pktname, "%sP0-%u.%3.3hu", net_data, i, instance);
  376.     if (stat(pktname, &info) == -1)
  377.       ok = 1;
  378.   }
  379. }
  380.  
  381.  
  382. int import(char *fn)
  383. {
  384.   char s[513], s1[513], pktname[MAXPATH], msgdate[31], *ss, *p;
  385.   int f, from, subj, intext, done;
  386.   long textlen;
  387.   struct msghdr mh;
  388.   net_header_rec nh;
  389.   struct date dt;
  390.   struct time tm;
  391.   FILE *fp;
  392.  
  393.   intext = 0;
  394.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  395.   if (f < 0)
  396.     return (1);
  397.   textlen = filelength(f);
  398.   if (textlen > 32767L) {
  399.     fprintf(stderr, "\n ■ Skipping %s - greater than 32K.", fn);
  400.     return (1);
  401.   }
  402.   p = (char *) malloc(textlen + 1);
  403.   if (p == NULL) {
  404.     fprintf(stderr, "\n ■ Unable to allocate %ld bytes.", textlen);
  405.     return (1);
  406.   }
  407.   read(f, (void *) p, textlen);
  408.   close(f);
  409.  
  410.   nh.tosys = net_sysnum;
  411.   nh.fromsys = 32767L;
  412.   nh.fromuser = 0;
  413.   nh.touser = defuser;
  414.   nh.main_type = main_type_email;
  415.   nh.minor_type = 0;
  416.   nh.list_len = 0;
  417.   gettime(&tm);
  418.   getdate(&dt);
  419.   nh.daten = dostounix(&dt, &tm);
  420.   nh.method = 0;
  421.  
  422.   fp = fsh_open(fn, "rb");
  423.   if (!fp) {
  424.     free(p);
  425.     return (1);
  426.   }
  427.   from = subj = done = 0;
  428.   while ((fgets(s, 254, fp)) && !done) {
  429.     if ((s[0] == '\r') || (s[0] == '\n') || (s[0] == 0))
  430.       intext = 1;
  431.     if (!intext) {
  432.       if (s[0] == 4) {
  433.         strcpy(s1, s);
  434.         strcpy(s, &s1[3]);
  435.       }
  436.       if ((strncmpi(s, "reply-to:", 9) == 0)) {
  437.         from = 1;
  438.         ss = strtok(s, ": ");
  439.         ss = strtok(NULL, "\r\n");
  440.         trimstr1(ss);
  441.         /* strncpy(mh.fromUserName, ss, 46); */
  442.         strcpy(mh.fromUserName, ss);
  443.         if (strlen(mh.fromUserName) > 190)
  444.           mh.fromUserName[190] = '\0';
  445.         strcat(mh.fromUserName, "\r\n");
  446.       } else
  447.         if ((strncmpi(s, "from:", 5) == 0) && (!from)) {
  448.         from = 1;
  449.         ss = strtok(s, ": ");
  450.         ss = strtok(NULL, "\r\n");
  451.         trimstr1(ss);
  452.         /* strncpy(mh.fromUserName, ss, 46); */
  453.         strcpy(mh.fromUserName, ss);
  454.         if (strlen(mh.fromUserName) > 190)
  455.           mh.fromUserName[190] = '\0';
  456.         strcat(mh.fromUserName, "\r\n");
  457.       } else
  458.         if ((strncmpi(s, "return-path:", 12) == 0) && (!from)) {
  459.         ss = strtok(s, ": ");
  460.         ss = strtok(NULL, "\r\n");
  461.         trimstr1(ss);
  462.         /* strncpy(mh.fromUserName, ss, 46); */
  463.         strcpy(mh.fromUserName, ss);
  464.         if (strlen(mh.fromUserName) > 190)
  465.           mh.fromUserName[190] = '\0';
  466.         strcat(mh.fromUserName, "\r\n");
  467.       } else
  468.         if ((strncmpi(s, "subject:", 8) == 0) && (!subj)) {
  469.         ss = strtok(s, ": ");
  470.         ss = strtok(NULL, "\r\n");
  471.         trimstr1(ss);
  472.         strcpy(mh.subject, ss);
  473.         if (strlen(mh.subject) > 72)
  474.           mh.subject[72] = '\0';
  475.         else
  476.           mh.subject[strlen(mh.subject)] = '\0';
  477.       } else
  478.         if ((strncmpi(s, "to:", 3) == 0) || (strncmpi(s, "cc:", 3) == 0)) {
  479.         ss = strtok(s, ": ");
  480.         ss = strtok(NULL, "\r\n");
  481.         strcpy(mh.toUserName, ss);
  482.         trimstr1(mh.toUserName);
  483.         if (_fstrstr(mh.toUserName, " "))
  484.           find_name(mh.toUserName);
  485.         else
  486.           mh.toUserName[0] = '\0';
  487.         if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  488.           nh.touser = defuser;
  489.           strcpy(mh.toUserName, postmaster);
  490.         } else
  491.           nh.touser = curuser;
  492.         }
  493.     } else
  494.       done = 1;
  495.   }
  496.   fclose(fp);
  497.   fprintf(stderr, "\n ■ From    : %s", mh.fromUserName);
  498.   fprintf(stderr, " ■ Sent to : %s #%hd", mh.toUserName, nh.touser);
  499.   fprintf(stderr, "\n ■ Subject : %s", mh.subject);
  500.   name_packet(pktname);
  501.   fp = fsh_open(pktname, "wb");
  502.   if (fp < 0) {
  503.     fprintf(stderr, "\n ■ Unable to create packet %s", pktname);
  504.     free(p);
  505.     return (1);
  506.   }
  507.   strncpy(msgdate, ctime(&(time_t) nh.daten), 24);
  508.   msgdate[24] = '\0';
  509.   strcat(msgdate, "\r\n");
  510.   nh.length = textlen + strlen(mh.fromUserName) + strlen(mh.subject) + strlen(msgdate) + 1;
  511.   fwrite(&nh, sizeof(net_header_rec), 1, fp);
  512.   fwrite(mh.subject, sizeof(char), strlen(mh.subject) +1, fp);
  513.   fwrite(mh.fromUserName, sizeof(char), strlen(mh.fromUserName), fp);
  514.   fwrite(msgdate, sizeof(char), strlen(msgdate), fp);
  515.   fwrite(p, sizeof(char), textlen, fp);
  516.   fclose(fp);
  517.   free(p);
  518.   return (0);
  519. }
  520.  
  521. unsigned char *stripcolors(unsigned char *text)
  522. {
  523.   static unsigned char s[161];
  524.   int i, i1;
  525.  
  526.   if (strlen(text) == 0)
  527.     return ("");
  528.  
  529.   i = 0;
  530.   i1 = 0;
  531.   do {
  532.     if (text[i] == 3)
  533.       i++;
  534.     else {
  535.       s[i1] = text[i];
  536.       i1++;
  537.     }
  538.     i++;
  539.   } while (i < strlen(text));
  540.   s[i1] = 0;
  541.   return (s);
  542. }
  543.  
  544. int export(char *fn)
  545. {
  546.   char fn1[121], groupname[81], outfn[121], _temp_buffer[256];
  547.   char *ss, *buffer, *text;
  548.   unsigned stype, ttype;
  549.   int infile, outfile, inloc, outloc, term, ok, i, j;
  550.   net_header_rec nhr;
  551.   struct msghdr mh;
  552.   struct tm *time_msg;
  553.   FILE *fp;
  554.   time_t some;
  555.   char *main_type[] =
  556.   {
  557.     "Network Update", "email by usernum", "post from sub host", "file",
  558.     "post to sub host", "external message", "email by name",
  559.     "NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
  560.     "CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
  561.     "Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info"
  562.   };
  563.  
  564.   if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
  565.     return (1);
  566.   else
  567.     fprintf(stderr, "\n ■ Parsing %s (%ld bytes)", fn, filelength(infile));
  568.  
  569.   if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
  570.     fprintf(stderr, "\n ■ Out of memory allocating input buffer!");
  571.     return (1);
  572.   }
  573.   if ((text = (char *) malloc(32 * 1024)) == NULL) {
  574.     fprintf(stderr, "\n ■ Out of memory allocating output buffer!");
  575.     if (buffer)
  576.       free(buffer);
  577.     buffer = NULL;
  578.     return (1);
  579.   }
  580.   while (read(infile, &nhr, sizeof(nhr))) {
  581.     read(infile, buffer, nhr.length);
  582.     if (nhr.tosys != 32767) {
  583.       fprintf(stderr, "\n ■ Non-Internet system routing via @32767... aborting export.");
  584.       return (1);
  585.     }
  586.     if ((nhr.main_type == main_type_post) ||
  587.         (nhr.main_type == main_type_new_post) ||
  588.         (nhr.main_type == main_type_email_name)) {
  589.       inloc = 0;
  590.       if (nhr.main_type == main_type_email_name) {
  591.         stype = nhr.minor_type;
  592.         inloc = strlen(buffer) + 1;
  593.         strcpy(mh.toUserName, buffer);
  594.         if (nhr.fromsys != net_sysnum) {
  595.           fprintf(stderr, "\n ■ Gate from #%hd@%hd to %s not yet supported",
  596.                   nhr.fromuser, nhr.fromsys, mh.toUserName);
  597.           continue;
  598.         }
  599.       } else
  600.         if (nhr.main_type == main_type_post)
  601.         stype = nhr.minor_type;
  602.       else {
  603.         stype = atoi(&buffer[inloc]);
  604.         sprintf(_temp_buffer, "%u", stype);
  605.         inloc += strlen(_temp_buffer) + 1;
  606.       }
  607.       strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  608.       stripcolors(mh.subject);
  609.       inloc += strlen(&buffer[inloc]) + 1;
  610.  
  611.       for (term = inloc; buffer[term] != '\r'; term++);
  612.       buffer[term] = '\0';
  613.  
  614.       if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
  615.         if (nhr.main_type != main_type_post) {
  616.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  617.             fprintf(stderr, "\n ■ No match for user #%hd... skipping message!",
  618.                     nhr.fromuser);
  619.             continue;
  620.           }
  621.         } else {
  622.           if (!num_to_name(mh.fromUserName, nhr.fromuser, 1)) {
  623.             fprintf(stderr, "\n ■ No match for user #%hd... skipping message!",
  624.                     nhr.fromuser);
  625.             continue;
  626.           }
  627.         }
  628.       } else {
  629.         strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  630.         stripcolors(mh.fromUserName);
  631.         strtok(mh.fromUserName, "#");
  632.         if ((nhr.main_type == main_type_post) &&
  633.             (nhr.fromsys != net_sysnum)) {
  634.           sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
  635.           strcat(mh.fromUserName, _temp_buffer);
  636.         }
  637.       }
  638.  
  639.       inloc = term + 2;
  640.  
  641.       while (buffer[inloc] != '\n')
  642.         inloc++;
  643.       inloc++;
  644.  
  645.       if (strncmp(&buffer[inloc], "0R", 3) == 0) {
  646.         while (buffer[inloc] != '\n')
  647.           ++inloc;
  648.         inloc++;
  649.       }
  650.       if (strnicmp(&buffer[inloc], "RE: ", 4) == 0) {
  651.         for (term = inloc; buffer[term] != '\r'; term++);
  652.         buffer[term] = '\0';
  653.         strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  654.         inloc = term + 2;
  655.       }
  656.       if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
  657.           (strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
  658.         for (term = inloc; buffer[term] != '\r'; term++);
  659.         buffer[term] = '\0';
  660.         strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
  661.         stripcolors(mh.toUserName);
  662.         inloc = term + 2;
  663.       } else {
  664.         if (nhr.main_type != main_type_email_name) {
  665.           strcpy(mh.toUserName, "ALL\n");
  666.         }
  667.       }
  668.  
  669.       outloc = 0;
  670.       do {
  671.         if (buffer[inloc] == 2) {
  672.           i = inloc + 1;
  673.           for (j = 1; j < 80; j++) {
  674.             if ((buffer[i] == '\r') ||
  675.                 (i > nhr.length))
  676.               break;
  677.             i++;
  678.           }
  679.           if (j < 80) {
  680.             i = (80 - j) / 2;
  681.             for (j = 1; j <= i; j++)
  682.               text[outloc++] = ' ';
  683.           }
  684.           inloc++;
  685.         } else
  686.           if (buffer[inloc] == 3)
  687.           inloc += 2;
  688.         else
  689.           if ((buffer[inloc] == 4) && (buffer[inloc + 1] == '0')) {
  690.           i = inloc;
  691.           for (j = 1; j < 80; j++) {
  692.             if ((buffer[i] == '\r') ||
  693.                 (i > nhr.length))
  694.               break;
  695.             i++;
  696.             inloc++;
  697.           }
  698.           inloc++;
  699.           }
  700.         else
  701.           if (buffer[inloc] >= 127) {
  702.           inloc++;
  703.         } else
  704.           if (buffer[inloc] == 1)
  705.           inloc++;
  706.         else
  707.           text[outloc++] = buffer[inloc++];
  708.       } while (inloc < nhr.length);
  709.  
  710.       text[outloc] = '\0';
  711.  
  712.       switch (nhr.main_type) {
  713.         case main_type_email:
  714.         case main_type_email_name:
  715.           i = 1;
  716.           sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
  717.           while (exist(outfn))
  718.             sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  719.           break;
  720.         case main_type_post:
  721.         case main_type_pre_post:
  722.           i = 1;
  723.           sprintf(outfn, "%sOUTBOUND\\%d.%d", net_data, stype, i);
  724.           while (exist(outfn))
  725.             sprintf(outfn, "%sOUTBOUND\\%d.%d", net_data, stype, ++i);
  726.           break;
  727.         default:
  728.           continue;
  729.       }
  730.  
  731.       fprintf(stderr, "\n ■ Creating: %s", outfn);
  732.       fprintf(stderr, "\n ■ From    : %s", mh.fromUserName);
  733.       if ((nhr.main_type == main_type_post) ||
  734.           (nhr.main_type == main_type_pre_post)) {
  735.         sprintf(fn1, "%sNEWS.RC", net_data);
  736.         fp = fsh_open(fn1, "rt");
  737.         if (!fp)
  738.           fprintf(stderr, "\n ■ %s not found!", fn1);
  739.         else {
  740.           ok = 0;
  741.           while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
  742.             groupname[0] = 0;
  743.             ss = strtok(_temp_buffer, " ");
  744.             if (ss) {
  745.               strcpy(groupname, ss);
  746.               ss = strtok(NULL, " ");
  747.               ss = strtok(NULL, "\r");
  748.               ttype = atoi(ss);
  749.               if (ttype == stype)
  750.                 ok = 1;
  751.             }
  752.           }
  753.           *ss = NULL;
  754.           fclose(fp);
  755.         }
  756.       }
  757.       if ((nhr.main_type == main_type_email) ||
  758.           (nhr.main_type == main_type_email_name)) {
  759.         fprintf(stderr, "\n ■ To      : %s", strlwr(mh.toUserName));
  760.       } else {
  761.         if (groupname[0] == 0)
  762.           fprintf(stderr, "\n ■ No match for subtype %u in NEWS.RC", stype);
  763.         else
  764.           fprintf(stderr, "\n ■ Post to : %s", groupname);
  765.       }
  766.  
  767.       fprintf(stderr, "\n ■ Subject : %s", mh.subject);
  768.  
  769.       outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
  770.       if (outfile == -1) {
  771.         if (buffer)
  772.           free(buffer);
  773.         if (text)
  774.           free(text);
  775.         fprintf(stderr, "\n ■ Unable to open output file %s", outfn);
  776.         return (1);
  777.       }
  778.       time(&some);
  779.       time_msg = localtime(&some);
  780.       strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
  781.       for (j = 0; j < strlen(mh.fromUserName); j++)
  782.         if (mh.fromUserName[j] == ' ')
  783.           mh.fromUserName[j] = '_';
  784.       if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)))
  785.         sprintf(_temp_buffer, "From: nowhere@no.net (%s)\n",
  786.                 mh.fromUserName);
  787.       else
  788.         sprintf(_temp_buffer, "From: %s@%s (%s)\n",
  789.                 POPNAME, DOMAIN, mh.fromUserName);
  790.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  791.       delay(2000);
  792.       sprintf(_temp_buffer, "Message-ID: <%lx-%s@%s>\n",
  793.               time(NULL), POPNAME, DOMAIN);
  794.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  795.       if ((nhr.main_type == main_type_email) ||
  796.           (nhr.main_type == main_type_email_name)) {
  797.         sprintf(_temp_buffer, "To: %s\n",
  798.                 mh.toUserName);
  799.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  800.       } else {
  801.         sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
  802.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  803.       }
  804.       sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
  805.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  806.       sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
  807.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  808.       sprintf(_temp_buffer, "Organization: %s * @%hu.%s * %s\n",
  809.               syscfg.systemname, net_sysnum, net_name, syscfg.systemphone);
  810.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  811.       if ((!spam) && ((nhr.main_type != main_type_post) || (nhr.main_type != main_type_new_post))) {
  812.         sprintf(_temp_buffer, "Reply-To: %s@%s (%s)\n",
  813.                 POPNAME, DOMAIN, mh.fromUserName);
  814.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  815.       }
  816.       sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n", VERSION);
  817.       write(outfile, _temp_buffer, strlen(_temp_buffer));
  818.  
  819.       if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post))) {
  820.         sprintf(_temp_buffer, "Reply to: %s@%s (%s)\n\n",
  821.                 POPNAME, DOMAIN, mh.fromUserName);
  822.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  823.       }
  824.       if ((nhr.main_type != main_type_email) &&
  825.           (nhr.main_type != main_type_email_name) &&
  826.           (strncmp(mh.toUserName, "ALL", 3) != 0)) {
  827.         sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
  828.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  829.       }
  830.       write(outfile, text, strlen(text));
  831.  
  832.       if (tagfile[0] == 0) {
  833.         sprintf(_temp_buffer, "\n\nOrigin: %s * %s * @%hu.FILEnet\n\n",
  834.                 syscfg.systemname, syscfg.systemphone, net_sysnum);
  835.         write(outfile, _temp_buffer, strlen(_temp_buffer));
  836.       } else {
  837.         fp = fsh_open(tagfile, "rt");
  838.         if (!fp)
  839.           fprintf(stderr, "\n ■ Error reading %s.", tagfile);
  840.         else {
  841.           sprintf(_temp_buffer, "\n\n");
  842.           write(outfile, _temp_buffer, strlen(_temp_buffer));
  843.           while (fgets(_temp_buffer, 120, fp)) {
  844.             for (i = 0; ((i < strlen(_temp_buffer)) &&
  845.                (_temp_buffer[i] != '\r') && (_temp_buffer[i] != '\n')); i++)
  846.               if ((_temp_buffer[i] < 32) || (_temp_buffer[i] > 126))
  847.                 _temp_buffer[i] = 32;
  848.             write(outfile, _temp_buffer, strlen(_temp_buffer));
  849.           }
  850.           fclose(fp);
  851.           sprintf(_temp_buffer, "\n\n");
  852.           write(outfile, _temp_buffer, strlen(_temp_buffer));
  853.         }
  854.       }
  855.       close(outfile);
  856.     } else {
  857.       if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x14))
  858.         fprintf(stderr, "\n ■ %s message skipped",
  859.                 main_type[nhr.main_type - 1]);
  860.       else
  861.         fprintf(stderr, "\n ■ Unknown Main_type %hd skipped", nhr.main_type);
  862.     }
  863.   }
  864.   close(infile);
  865.   unlink(fn);
  866.   if (text)
  867.     free(text);
  868.   if (buffer)
  869.     free(buffer);
  870.   return (0);
  871. }
  872.  
  873. void get_dir(char *s, int be)
  874. {
  875.   strcpy(s, "X:\\");
  876.   s[0] = 'A' + getdisk();
  877.   getcurdir(0, s + 3);
  878.   if (be) {
  879.     if (s[strlen(s) - 1] != '\\')
  880.       strcat(s, "\\");
  881.   }
  882. }
  883.  
  884. int main(int argc, char *argv[])
  885. {
  886.   char fn[MAXPATH], *ss;
  887.   int f, f1, maxmail, i;
  888.   struct ffblk ff;
  889.  
  890.   /* 0      1               2          3          4       5        6       */
  891.   /* exp s32767.net,      net_data, net_sysnum, popname, domain, net_name  */
  892.   /* exp netlog/contact,  net_data,   sysnum,     sent    recd   totaltime */
  893.  
  894.   fprintf(stderr, "\n ■ PPP Import/Export %s", VERSION);
  895.   if (argc != 7) {
  896.     fprintf(stderr, "\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
  897.     if (argc > 1) {
  898.       fprintf(stderr, "Command line was: ");
  899.       for (i = 0; i < argc; i++)
  900.         fprintf(stderr, "%s ", argv[i]);
  901.       fprintf(stderr, "\n\n");
  902.     }
  903.     return (1);
  904.   }
  905.   get_dir(maindir, 1);
  906.   strcpy(net_data, argv[2]);
  907.   sprintf(fn, "%s%s", net_data, argv[1]);
  908.   net_name = argv[6];
  909.   strcpy(POPNAME, argv[4]);
  910.   strcpy(DOMAIN, argv[5]);
  911.   net_sysnum = atoi(argv[3]);
  912.   tagfile[0] = 0;
  913.   spam = 0;
  914.  
  915.   f = sh_open1("CONFIG.DAT", O_RDONLY | O_BINARY);
  916.   if (f < 0) {
  917.     fprintf(stderr, "Could not open CONFIG.DAT!\n\n");
  918.     return (1);
  919.   }
  920.   read(f, (void *) &syscfg, sizeof(configrec));
  921.   close(f);
  922.  
  923.   ss = getenv("WWIV_INSTANCE");
  924.   if (ss) {
  925.     instance = atoi(ss);
  926.     if (instance >= 1000) {
  927.       fprintf(stderr, "\n ■ WWIV_INSTANCE set to %hd.  Can only be 1..999!",
  928.               instance);
  929.       instance = 1;
  930.     }
  931.   } else
  932.     instance = 1;
  933.  
  934.   parse_net_ini();
  935.  
  936.   fprintf(stderr, "\n ■ Postmaster default account set to %s #%hd.",
  937.           strupr(postmaster), defuser);
  938.   fprintf(stderr, "\n ■ Using user %s on outbound Internet mail.",
  939.           use_alias ? "aliases" : "real names");
  940.   if (spam)
  941.     fprintf(stderr, "\n ■ Using bogus originating address on newsgroup posts.");
  942.   if (tagfile[0] != 0)
  943.     fprintf(stderr, "\n ■ Using signature file : %s", tagfile);
  944.  
  945.   export(fn);
  946.  
  947.   sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
  948.   f1 = findfirst(fn, &ff, 0);
  949.   maxmail = 14;
  950.   while ((f1 == 0) && maxmail) {
  951.     sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  952.     if (!import(fn)) {
  953.       unlink(fn);
  954.       --maxmail;
  955.     }
  956.     f1 = findnext(&ff);
  957.   }
  958.   return 0;
  959. }
  960.