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