home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15A68.ZIP / EXP.CPP < prev    next >
Text File  |  1997-11-18  |  45KB  |  1,534 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dos.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <ctype.h>
  9. #include <mem.h>
  10. #include <conio.h>
  11. #include <io.h>
  12. #include <share.h>
  13. #include <errno.h>
  14. #include <dir.h>
  15. #include <time.h>
  16. #include <malloc.h>
  17. #include <process.h>
  18. #include <direct.h>
  19. #include "vardec.h"
  20. #include "net.h"
  21. #include "retcode.h"
  22.  
  23. #include "version.h"
  24.  
  25. #define WAIT_TIME 10
  26. #define TRIES 100
  27. #define SHARE_LEVEL 10
  28. #define MT_DESQVIEW 0x01
  29. #define MT_WINDOWS  0x02
  30. #define MT_OS2      0x04
  31. #define MT_NB       0x40
  32.  
  33.  
  34. struct msghdr {
  35.   char fromUserName[205];
  36.   char toUserName[81];
  37.   char subject[81];
  38.   char dateTime[81];
  39. };
  40.  
  41. typedef struct {
  42.   char ownername[60];
  43.   char subtype[8];
  44.   char opttext[30];
  45. } MAILLISTREC;
  46.  
  47. MAILLISTREC *maillist;
  48. configrec syscfg;
  49.  
  50. char net_name[31], postmaster[31], net_data[MAXPATH];
  51. char POPNAME[21], REPLYTO[81], DOMAIN[81], tagfile[MAXPATH], maindir[MAXPATH], spamname[81];
  52. unsigned short net_sysnum, defuser, num_users, use_alias, usermail, instance, spam;
  53. unsigned curuser;
  54. int multitasker = 0;
  55. int nlists = 0;
  56. char alphasubtype[8];
  57. unsigned long cur_daten;
  58.  
  59. void dv_pause(void)
  60. {
  61.   __emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
  62.   __emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
  63.   __emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
  64. }
  65.  
  66. void win_pause(void)
  67. {
  68.   __emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
  69. }
  70.  
  71. int get_dos_version(void)
  72. {
  73.   _AX = 0x3000;
  74.   geninterrupt(0x21);
  75.   if (_AX % 256 >= 10) {
  76.     multitasker |= MT_OS2;
  77.   }
  78.   return (_AX);
  79. }
  80.  
  81. int get_dv_version(void)
  82. {
  83.   int v;
  84.  
  85.   if (multitasker & MT_OS2)
  86.     return 0;
  87.   _AX = 0x2b01;
  88.   _CX = 0x4445;
  89.   _DX = 0x5351;
  90.   geninterrupt(0x21);
  91.   if (_AL == 0xff) {
  92.     return 0;
  93.   } else {
  94.     v = _BX;
  95.     multitasker |= MT_DESQVIEW;
  96.     return v;
  97.   }
  98. }
  99.  
  100. int get_win_version(void)
  101. {
  102.   int v = 0;
  103.  
  104.   __emit__(0x55, 0x06, 0x53);
  105.   _AX = 0x352f;
  106.   geninterrupt(0x21);
  107.   _AX = _ES;
  108.   if (_AX | _BX) {
  109.     _AX = 0x1600;
  110.     geninterrupt(0x2f);
  111.     v = _AX;
  112.     if (v % 256 <= 1)
  113.       v = 0;
  114.   }
  115.   __emit__(0x5b, 0x07, 0x5d);
  116.   if (v != 0)
  117.     multitasker |= MT_WINDOWS;
  118.   return (v);
  119. }
  120.  
  121. int get_nb_version(void)
  122. {
  123.   _AX = 0;
  124.   geninterrupt(0x2A);
  125.   return (_AH);
  126. }
  127.  
  128. void detect_multitask(void)
  129. {
  130.   get_dos_version();
  131.   get_win_version();
  132.   get_dv_version();
  133.   if (multitasker < 2)
  134.     if (get_nb_version())
  135.       multitasker = MT_NB;
  136. }
  137.  
  138. void giveup_timeslice(void)
  139. {
  140.   if (multitasker) {
  141.     switch (multitasker) {
  142.  case 1: 
  143.  case 3: 
  144.         dv_pause();
  145.         break;
  146.       case 2:
  147.       case 4:
  148.       case 5:
  149.       case 6:
  150.       case 7:
  151.         win_pause();
  152.         break;
  153.       default:
  154.         break;
  155.     }
  156.   }
  157. }
  158.  
  159. int sh_write(int handle, void *buffer, unsigned long len)
  160. {
  161.   if (handle == -1) {
  162.     return (-1);
  163.   }
  164.   return (write(handle, buffer, (unsigned) len));
  165. }
  166.  
  167. int sh_open(char *path, int file_access, unsigned fmode)
  168. {
  169.   int handle, count, share;
  170.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  171.  
  172.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  173.     share = SH_DENYRW;
  174.   } else {
  175.     share = SH_DENYWR;
  176.   }
  177.   handle = open(path, file_access | share, fmode);
  178.   if (handle < 0) {
  179.     count = 1;
  180.     fnsplit(path, drive, dir, file, ext);
  181.     if (access(path, 0) != -1) {
  182.       delay(WAIT_TIME);
  183.       handle = open(path, file_access | share, fmode);
  184.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  185.         if (count % 2)
  186.           delay(WAIT_TIME);
  187.         else
  188.           giveup_timeslice();
  189.         count++;
  190.         handle = open(path, file_access | share, fmode);
  191.       }
  192.     }
  193.   }
  194.   return (handle);
  195. }
  196.  
  197. int sh_open1(char *path, int access)
  198. {
  199.   unsigned fmode;
  200.  
  201.   fmode = 0;
  202.   if ((access & O_RDWR) || (access & O_WRONLY))
  203.     fmode |= S_IWRITE;
  204.   if ((access & O_RDWR) || (access & O_RDONLY))
  205.     fmode |= S_IREAD;
  206.   return (sh_open(path, access, fmode));
  207. }
  208.  
  209. int sh_close(int f)
  210. {
  211.   if (f != -1)
  212.     close(f);
  213.   return (-1);
  214. }
  215.  
  216. int sh_read(int handle, void *buf, unsigned len)
  217. {
  218.   if (handle == -1) {
  219.     return (-1);
  220.   }
  221.   return (read(handle, buf, len));
  222. }
  223.  
  224. long sh_lseek(int handle, long offset, int fromwhere)
  225. {
  226.   if (handle == -1) {
  227.     return (-1L);
  228.   }
  229.   return (lseek(handle, offset, fromwhere));
  230. }
  231.  
  232. FILE *fsh_open(char *path, char *fmode)
  233. {
  234.   FILE *f;
  235.   int count, share, md, fd;
  236.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  237.  
  238.   share = SH_DENYWR;
  239.   md = 0;
  240.   if (((char *) _fstrchr(fmode, 'w')) != NULL) {
  241.     share = SH_DENYRD;
  242.     md = O_RDWR | O_CREAT | O_TRUNC;
  243.   } else
  244.     if (((char *) _fstrchr(fmode, 'a')) != NULL) {
  245.     share = SH_DENYRD;
  246.     md = O_RDWR | O_CREAT;
  247.   } else {
  248.     md = O_RDONLY;
  249.   }
  250.   if (((char *) _fstrchr(fmode, 'b')) != NULL) {
  251.     md |= O_BINARY;
  252.   }
  253.   if (((char *) _fstrchr(fmode, '+')) != NULL) {
  254.     md &= ~O_RDONLY;
  255.     md |= O_RDWR;
  256.     share = SH_DENYRD;
  257.   }
  258.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  259.   if (fd < 0) {
  260.     count = 1;
  261.     fnsplit(path, drive, dir, file, ext);
  262.     if ((access(path, 0)) != -1) {
  263.       delay(WAIT_TIME);
  264.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  265.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  266.         delay(WAIT_TIME);
  267.         count++;
  268.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  269.       }
  270.     }
  271.   }
  272.   if (fd > 0) {
  273.     if (((char *) _fstrchr(fmode, 'a')) != NULL)
  274.       sh_lseek(fd, 0L, SEEK_END);
  275.     f = fdopen(fd, fmode);
  276.     if (!f) {
  277.       close(fd);
  278.     }
  279.   } else
  280.     f = 0;
  281.   return (f);
  282. }
  283.  
  284. size_t fsh_write(void *ptr, size_t size, size_t n, FILE * stream)
  285. {
  286.  
  287.   if (stream == NULL) {
  288.     return (0);
  289.   }
  290.   return (fwrite(ptr, size, n, stream));
  291. }
  292.  
  293.  
  294. int exist(char *s)
  295. {
  296.   int i;
  297.   struct ffblk ff;
  298.  
  299.   i = findfirst(s, &ff, 0);
  300.   if (i)
  301.     return (0);
  302.   else
  303.     return (1);
  304. }
  305.  
  306. char *stripspace(char *str)
  307. {
  308.   char *obuf, *nbuf;
  309.  
  310.   if (str) {
  311.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  312.       if (!isspace(*obuf))
  313.         *nbuf++ = *obuf;
  314.     }
  315.     *nbuf = NULL;
  316.   }
  317.   return (str);
  318. }
  319.  
  320. static unsigned char *trimstr1(unsigned char *s)
  321. {
  322.   int i;
  323.   static char *whitespace = " \r\n\t\"";
  324.  
  325.   i = (int) strlen(s);
  326.   while ((i > 0) && ((char *) _fstrchr(whitespace, s[i - 1]) != NULL))
  327.     --i;
  328.   while ((i > 0) && ((char *) _fstrchr(whitespace, *s) != NULL)) {
  329.     memmove(s, s + 1, --i);
  330.   }
  331.   s[i] = 0;
  332.   return (s);
  333. }
  334.  
  335. void output(char *fmt,...)
  336. {
  337.   va_list v;
  338.   char s[255];
  339.  
  340.   va_start(v, fmt);
  341.   vsprintf(s, fmt, v);
  342.   va_end(v);
  343.   fputs(s, stderr);
  344. }
  345.  
  346. int num_to_name(char *where, int whichuser, int alias)
  347. {
  348.   int userfile, num_users, found;
  349.   userrec ur;
  350.   long pos;
  351.   char fn[MAXPATH];
  352.  
  353.   found = 0;
  354.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  355.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  356.   if (userfile < 0) {
  357.     output("\n ■ Cannot open %s.", fn);
  358.     return (found);
  359.   }
  360.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  361.  
  362.   if (whichuser > num_users) {
  363.     output("\n ■ User #%d out of range.", whichuser);
  364.     return (found);
  365.   }
  366.   pos = ((long) sizeof(userrec) * ((long) whichuser));
  367.   lseek(userfile, pos, SEEK_SET);
  368.   sh_read(userfile, &ur, sizeof(userrec));
  369.   if (ur.realname[0] == 0)
  370.     output("\n ■ User #%d has blank real name field!", whichuser);
  371.   else {
  372.     if (ur.inact == inact_deleted)
  373.       output("\n ■ User #%d is marked as deleted!", whichuser);
  374.     else {
  375.       if (!alias)
  376.         strcpy(where, ur.realname);
  377.       else {
  378.         strcpy(where, ur.name);
  379.         strlwr(where);
  380.       }
  381.       found = 1;
  382.     }
  383.   }
  384.   sh_close(userfile);
  385.   return (found);
  386. }
  387.  
  388.  
  389. void parse_net_ini(void)
  390. {
  391.   char s[MAXPATH], origline[121], line[121], *ss, inlist = 0;
  392.   FILE *fp;
  393.   long fptr;
  394.  
  395.   defuser = 1;
  396.   use_alias = 1;
  397.   usermail = 1;
  398.   nlists = 0;
  399.   maillist = NULL;
  400.   *REPLYTO = 0;
  401.   sprintf(s, "%sNET.INI", maindir);
  402.   if ((fp = fsh_open(s, "rt")) == NULL) {
  403.     output("\n ■ Unable to open %s.", s);
  404.     return;
  405.   }
  406.   while (fgets(line, 80, fp)) {
  407.     ss = NULL;
  408.     strcpy(origline, line);
  409.     stripspace(line);
  410.     if ((line[0] == ';') || (line[0] == '\n') || (line[0] == 0))
  411.       continue;
  412.     if (strnicmp(line, "[MAILLIST]", 10) == 0) {
  413.       fptr = ftell(fp);
  414.       while ((fgets(line, 80, fp)) && (line[0] != '[')) {
  415.         if ((line[0] != '[') && (line[0] != ';') && (line[0] != 0))
  416.           ++nlists;
  417.       }
  418.       fseek(fp, fptr, SEEK_SET);
  419.       maillist = (MAILLISTREC *) malloc((nlists + 1) * sizeof(MAILLISTREC));
  420.       if (maillist == NULL)
  421.         output("\n ■ Not enough memory to process %d mailing lists.", nlists);
  422.       else
  423.         inlist = 1;
  424.       nlists = 0;
  425.       continue;
  426.     } else if (line[0] == '[') {
  427.       inlist = 0;
  428.       continue;
  429.     }
  430.     if (inlist) {
  431.       if ((line[0] != ';') && (line[0] != 0)) {
  432.         ss = strtok(line, "\"");
  433.         trimstr1(ss);
  434.         if (*ss) {
  435.           ss = strtok(NULL, "\"");
  436.           if (*ss) {
  437.             strncpy(maillist[nlists].opttext, ss, 29);
  438.             maillist[nlists].opttext[30] = '\0';
  439.           } else
  440.             maillist[nlists].opttext[0] = '\0';
  441.         }
  442.         ss = strtok(line, "*\n");
  443.         trimstr1(ss);
  444.         strcpy(maillist[nlists].ownername, ss);
  445.         ss = strtok(NULL, "\"\n");
  446.         trimstr1(ss);
  447.         if (*ss) {
  448.           strlwr(maillist[nlists].ownername);
  449.           strcpy(maillist[nlists++].subtype, ss);
  450.         } else
  451.           output("\n ■ Missing *subtype in maillist for %s.",
  452.                  maillist[nlists].ownername);
  453.       }
  454.       continue;
  455.     }
  456.     if (strnicmp(line, "POSTMASTER", 10) == 0) {
  457.       ss = strtok(line, "=");
  458.       if (ss) {
  459.         ss = strtok(NULL, "\n");
  460.         if (ss)
  461.           defuser = atoi(ss);
  462.       }
  463.       continue;
  464.     }
  465.     if (strnicmp(line, "SPAMCONTROL", 11) == 0) {
  466.       ss = strtok(line, "=");
  467.       if (ss) {
  468.         ss = strtok(NULL, "\n");
  469.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  470.           spam = 1;
  471.       }
  472.       continue;
  473.     }
  474.     if (strnicmp(line, "USERMAIL", 8) == 0) {
  475.       ss = strtok(line, "=");
  476.       if (ss) {
  477.         ss = strtok(NULL, "\n");
  478.         if ((ss[0] == 'n') || (ss[0] == 'N'))
  479.           usermail = 0;
  480.       }
  481.       continue;
  482.     }
  483.     if (strnicmp(line, "SPAMADDRESS", 9) == 0) {
  484.       ss = strtok(line, "=");
  485.       if (ss) {
  486.         ss = strtok(NULL, "\n");
  487.         trimstr1(ss);
  488.         strcpy(spamname, ss);
  489.         if (stricmp(spamname, "DEFAULT") == 0)
  490.           strcpy(spamname, "WWIV_BBS@nospam.net");
  491.       }
  492.       continue;
  493.     }
  494.     if (strnicmp(line, "REPLYTO", 7) == 0) {
  495.       ss = strtok(origline, "=");
  496.       if (ss) {
  497.         ss = strtok(NULL, "\n");
  498.         trimstr1(ss);
  499.         strcpy(REPLYTO, ss);
  500.       }
  501.       continue;
  502.     }
  503.     if (strnicmp(line, "SIGNATURE", 9) == 0) {
  504.       ss = strtok(line, "=");
  505.       if (ss) {
  506.         ss = strtok(NULL, "\n");
  507.         trimstr1(ss);
  508.         strcpy(tagfile, ss);
  509.         if (!exist(tagfile)) {
  510.           output("\n ■ Default signature file %s not found!", tagfile);
  511.           tagfile[0] = 0;
  512.         }
  513.       }
  514.       continue;
  515.     }
  516.     if (strnicmp(line, "REALNAME", 8) == 0) {
  517.       ss = strtok(line, "=");
  518.       if (ss) {
  519.         ss = strtok(NULL, "\n");
  520.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  521.           use_alias = 0;
  522.       }
  523.     }
  524.   }
  525.   num_to_name(postmaster, defuser, 1);
  526.   if (fp != NULL)
  527.     fclose(fp);
  528.   return;
  529. }
  530.  
  531. unsigned char *strrep(char *str, char old, char New)
  532. {
  533.   int i;
  534.  
  535.   for (i = 0; str[i]; i++)
  536.     if (str[i] == old)
  537.       str[i] = New;
  538.   return (str);
  539. }
  540.  
  541.  
  542. unsigned int name_to_num(char *name)
  543. {
  544.   int userfile, usernum;
  545.   userrec ur;
  546.   long pos;
  547.   char fn[MAXPATH], ur_name[60], ur_realname[60];
  548.  
  549.   sprintf(fn, "%sM%s.NET", net_data, name);
  550.   if (exist(fn)) {
  551.     strcpy(alphasubtype, name);
  552.     return (unsigned)(65535L);
  553.   }
  554.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  555.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  556.   if (userfile < 0) {
  557.     output("\n ■ Cannot open %s", fn);
  558.     return (0);
  559.   } else
  560.     output("\n ■ Searching for user \"%s\"...", name);
  561.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  562.  
  563.   for (usernum = 1; usernum < num_users; usernum++) {
  564.     pos = ((long) sizeof(userrec) * ((long) usernum));
  565.     lseek(userfile, pos, SEEK_SET);
  566.     sh_read(userfile, &ur, sizeof(userrec));
  567.     strcpy(ur_realname, ur.realname);
  568.     strrep(ur_realname, ' ', '_');
  569.     strcpy(ur_name, ur.name);
  570.     strrep(ur_name, ' ', '_');
  571.     if ((strcmpi(ur.realname, name) == 0) || (strcmpi(ur_realname, name) == 0) ||
  572.         (strcmpi(ur.name, name) == 0) || (strcmpi(ur_name, name) == 0)) {
  573.       if (ur.inact == inact_deleted) {
  574.         output(" user #%d is deleted account.", usernum);
  575.         usernum = 0;
  576.         break;
  577.       } else {
  578.         output(" matched to user #%d.", usernum);
  579.         break;
  580.       }
  581.     }
  582.   }
  583.   userfile = sh_close(userfile);
  584.  
  585.   if (usernum >= num_users) {
  586.     output("... no match found.");
  587.     return (0);
  588.   }
  589.   return (usernum);
  590. }
  591.  
  592. char *find_name(char *name)
  593. {
  594.   char *ss;
  595.  
  596.   curuser = 0;
  597.   if (strcspn(name, "(") != strlen(name)) {
  598.     ss = strtok(name, "(");
  599.     ss = strtok(NULL, ")");
  600.     strcpy(name, ss);
  601.     curuser = name_to_num(name);
  602.   } else if (strcspn(name, "\"") != strlen(name)) {
  603.     ss = strtok(name, "\"");
  604.     ss = strtok(NULL, "\"");
  605.     strcpy(name, ss);
  606.     curuser = name_to_num(name);
  607.   } else if (strcspn(name, "<") != strlen(name)) {
  608.     ss = strtok(name, "<");
  609.     trimstr1(ss);
  610.     strcpy(name, ss);
  611.     curuser = name_to_num(name);
  612.   }
  613.   if (curuser == 0)
  614.     return ('\0');
  615.   else if (curuser == ((unsigned)65535L))
  616.     return (alphasubtype);
  617.   else
  618.     return (ss);
  619. }
  620.  
  621. void name_packet(char *pktname)
  622. {
  623.   int ok;
  624.   struct stat info;
  625.   unsigned i;
  626.  
  627.   ok = 0;
  628.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  629.     sprintf(pktname, "%sP0-%u.%3.3hu", net_data, i, instance);
  630.     if (stat(pktname, &info) == -1)
  631.       ok = 1;
  632.   }
  633. }
  634.  
  635. char *stristr(char *String, char *Pattern)
  636. {
  637.   char *pptr, *sptr, *start;
  638.   unsigned int slen, plen;
  639.  
  640.   for (start = String, pptr = Pattern, slen = strlen(String),
  641.        plen = strlen(Pattern); slen >= plen; start++, slen--) {
  642.     while (toupper(*start) != toupper(*Pattern)) {
  643.       start++;
  644.       slen--;
  645.       if (slen < plen)
  646.         return (NULL);
  647.     }
  648.     sptr = start;
  649.     pptr = Pattern;
  650.     while (toupper(*sptr) == toupper(*pptr)) {
  651.       sptr++;
  652.       pptr++;
  653.       if ('\0' == *pptr)
  654.         return (start);
  655.     }
  656.   }
  657.   return (NULL);
  658. }
  659.  
  660. #define FROM_RETURN 0x01
  661. #define FROM_FROM   0x02
  662. #define FROM_REPLY  0x04
  663.  
  664. int import(char *fn)
  665. {
  666.   char s[513], s1[121], pktname[MAXPATH], msgdate[61], *ss, *p, *id, *name;
  667.   char alphatype[21], recvdate[81];
  668.   int i, f, from, match, subj, intext, done, tolist;
  669.   long textlen, reallen;
  670.   struct msghdr mh;
  671.   net_header_rec nh;
  672.   FILE *fp;
  673.  
  674.   tolist = 0;
  675.   intext = 0;
  676.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  677.   if (f < 0)
  678.     return (1);
  679.   textlen = filelength(f);
  680.   if (textlen > 32767L) {
  681.     output("\n ■ Skipping %s - greater than 32K.", fn);
  682.     return (1);
  683.   }
  684.   p = (char *) malloc((int) (textlen + 1));
  685.   if (p == NULL) {
  686.     output("\n ■ Unable to allocate %ld bytes.", textlen);
  687.     return (1);
  688.   }
  689.   sh_read(f, (void *) p, (int) textlen);
  690.   sh_close(f);
  691.  
  692.   nh.tosys = net_sysnum;
  693.   nh.fromsys = 32767;
  694.   nh.fromuser = 0;
  695.   nh.touser = defuser;
  696.   nh.main_type = main_type_email;
  697.   nh.minor_type = 0;
  698.   nh.list_len = 0;
  699.   ++cur_daten;
  700.   nh.daten = cur_daten;
  701.   strncpy(msgdate, ctime(&(time_t) nh.daten), 24);
  702.   msgdate[24] = '\0';
  703.   sprintf(recvdate, "0RReceived: PPP Project %s on %s\r\n", VERSION, msgdate);
  704.   strcat(msgdate, "\r\n");
  705.   nh.method = 0;
  706.  
  707.   strcpy(mh.fromUserName, "Unknown");
  708.   strcpy(mh.toUserName, "Unknown");
  709.   strcpy(mh.subject, "None");
  710.  
  711.   if ((fp = fsh_open(fn, "rb")) == NULL) {
  712.     free(p);
  713.     return (1);
  714.   }
  715.   match = subj = done = 0;
  716.   while ((fgets(s, 254, fp)) && !done) {
  717.     if (s[0] == 4) {
  718.       ss = strtok(s, "R");
  719.       ss = strtok(NULL, "\r\n");
  720.       if (ss == NULL)
  721.         s[0] = 0;
  722.       else
  723.         strcpy(s, ss);
  724.     } else
  725.       intext = 1;
  726.     if (!intext) {
  727.       if (strncmpi(s, "from:", 5) == 0)
  728.         from = FROM_FROM;
  729.       else if (strncmpi(s, "return-path:", 12) == 0)
  730.         from = FROM_RETURN;
  731.       else if (strncmpi(s, "sender:", 7) == 0)
  732.         from = FROM_RETURN;
  733.       else if (strncmpi(s, "x-sender:", 9) == 0)
  734.         from = FROM_RETURN;
  735.       else if (strncmpi(s, "x-to:", 5) == 0)
  736.         from = FROM_RETURN;
  737.       else if (strncmpi(s, "reply-to:", 9) == 0)
  738.         from = FROM_REPLY;
  739.       else if (strncmpi(s, "x-reply-to:", 9) == 0)
  740.         from = FROM_REPLY;
  741.       else
  742.         from = 0;
  743.       if (from) {
  744.         ss = strtok(s, ": ");
  745.         ss = strtok(NULL, "\r\n");
  746.         trimstr1(ss);
  747.         if ((from & (FROM_RETURN | FROM_REPLY)) && (nh.main_type == main_type_email)) {
  748.           strcpy(s1, ss);
  749.           strlwr(s1);
  750.           for (i = 0; (i < nlists) && (nh.main_type == main_type_email); i++) {
  751.             if (stristr(s1, maillist[i].ownername) != NULL) {
  752.               if (atoi(maillist[i].subtype)) {
  753.                 nh.main_type = main_type_pre_post;
  754.                 nh.minor_type = atoi(maillist[i].subtype);
  755.               } else {
  756.                 nh.main_type = main_type_new_post;
  757.                 nh.minor_type = 0;
  758.                 strcpy(alphatype, maillist[i].subtype);
  759.               }
  760.               strcpy(alphasubtype, maillist[i].subtype);
  761.               nh.touser = 0;
  762.               from = 0;
  763.             }
  764.           }
  765.         }
  766.         if ((from > match) && ((nh.main_type == main_type_email) || (from == FROM_FROM))) {
  767.           match = from;
  768.           if (strcspn(ss, "<") != strlen(ss)) {
  769.             if ((strcspn(ss, " ")) < (strcspn(ss, "<"))) {
  770.               name = strtok(ss, "<");
  771.               trimstr1(name);
  772.               id = strtok(NULL, ">");
  773.               trimstr1(id);
  774.               sprintf(mh.fromUserName, "%s <%s>", name, id);
  775.             } else {
  776.               strncpy(mh.fromUserName, ss, 205);
  777.               trimstr1(mh.fromUserName);
  778.               if (strcspn(ss, " ") != strlen(ss))
  779.                 output("\nName is *after* host in \"%s\"", name);
  780.             }
  781.           } else
  782.             strncpy(mh.fromUserName, ss, 205);
  783.           mh.fromUserName[190] = 0;
  784.           strcat(mh.fromUserName, "\r\n");
  785.         }
  786.       } else if ((strncmpi(s, "subject:", 8) == 0) && (!subj)) {
  787.         ss = strtok(s, ": ");
  788.         ss = strtok(NULL, "\r\n");
  789.         trimstr1(ss);
  790.         strncpy(mh.subject, ss, 81);
  791.         mh.subject[72] = 0;
  792.         subj = 1;
  793.       } else if (strncmpi(s, "date:", 5) == 0) {
  794.         ss = strtok(s, ": ");
  795.         ss = strtok(NULL, "\r\n");
  796.         trimstr1(ss);
  797.         strncpy(msgdate, ss, 58);
  798.         msgdate[58] = '\0';
  799.         strcat(msgdate, "\r\n");
  800.       } else if ((strncmpi(s, "to:", 3) == 0) || (strncmpi(s, "cc:", 3) == 0)) {
  801.         ss = strtok(s, ": ");
  802.         ss = strtok(NULL, "\r\n");
  803.         strncpy(mh.toUserName, ss, 81);
  804.         mh.toUserName[80] = 0;
  805.         curuser = 0;
  806.         trimstr1(mh.toUserName);
  807.         if (_fstrstr(mh.toUserName, " "))
  808.           find_name(mh.toUserName);
  809.         else
  810.           mh.toUserName[0] = 0;
  811.         if ((stristr(mh.toUserName, "Multiple recipients of list") != NULL) && (curuser != (unsigned)65535L)) {
  812.           for (i = 0; (i < nlists) && (nh.main_type == main_type_email); i++) {
  813.             if (stristr(mh.toUserName, maillist[i].opttext) != NULL) {
  814.               if (atoi(maillist[i].subtype)) {
  815.                 nh.main_type = main_type_pre_post;
  816.                 nh.minor_type = atoi(maillist[i].subtype);
  817.               } else {
  818.                 nh.main_type = main_type_new_post;
  819.                 nh.minor_type = 0;
  820.                 strcpy(alphatype, maillist[i].subtype);
  821.               }
  822.               strcpy(alphasubtype, maillist[i].subtype);
  823.               nh.touser = 0;
  824.             }
  825.           }
  826.         }
  827.         if ((curuser == (unsigned)65535L) && (nh.main_type == main_type_email)) {
  828.           strcpy(alphatype, alphasubtype);
  829.           nh.main_type = main_type_new_post;
  830.           nh.minor_type = 0;
  831.           nh.touser = 0;
  832.           tolist = 1;
  833.         } else if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  834.           nh.touser = defuser;
  835.           strcpy(mh.toUserName, postmaster);
  836.         } else {
  837.           nh.touser = curuser;
  838.         }
  839.       } else if (strncmpi(s, "apparently-to:", 14) == 0) {
  840.         ss = strtok(s, ": ");
  841.         ss = strtok(NULL, "\r\n");
  842.         strncpy(mh.toUserName, ss, 81);
  843.         mh.toUserName[80] = 0;
  844.         curuser = 0;
  845.         trimstr1(mh.toUserName);
  846.         if (_fstrstr(mh.toUserName, " "))
  847.           find_name(mh.toUserName);
  848.         else
  849.           mh.toUserName[0] = 0;
  850.         if ((curuser == (unsigned)65535L) && (nh.main_type == main_type_email)) {
  851.           strcpy(alphatype, alphasubtype);
  852.           nh.main_type = main_type_new_post;
  853.           nh.minor_type = 0;
  854.           nh.touser = 0;
  855.           tolist = 1;
  856.         } else if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  857.           nh.touser = defuser;
  858.           strcpy(mh.toUserName, postmaster);
  859.         } else {
  860.           nh.touser = curuser;
  861.         }
  862.       }
  863.     } else
  864.       done = 1;
  865.   }
  866.   if (fp != NULL)
  867.     fclose(fp);
  868.   output("\n ■ From    : %s", strlwr(mh.fromUserName));
  869.   if ((nh.main_type == main_type_pre_post) ||
  870.       (nh.main_type == main_type_new_post))
  871.     output(" ■ Post to : Sub %s", alphasubtype);
  872.   else
  873.     output(" ■ Sent to : %s #%hd", strupr(mh.toUserName), nh.touser);
  874.   output("\n ■ Subject : %s", mh.subject);
  875.   name_packet(pktname);
  876.   if ((fp = fsh_open(pktname, "wb")) == NULL) {
  877.     output("\n ■ Unable to create packet %s", pktname);
  878.     free(p);
  879.     return (1);
  880.   }
  881.   nh.length = textlen + strlen(mh.fromUserName) + strlen(mh.subject)
  882.       + strlen(msgdate) + strlen(recvdate) + 1;
  883.   if (nh.main_type == main_type_new_post)
  884.     nh.length += strlen(alphatype) + 1;
  885.   while (tolist >= 0) {
  886.     fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  887.     if (nh.main_type == main_type_new_post)
  888.       fsh_write(alphatype, sizeof(char), strlen(alphatype) +1, fp);
  889.     fsh_write(mh.subject, sizeof(char), strlen(mh.subject) +1, fp);
  890.     fsh_write(mh.fromUserName, sizeof(char), strlen(mh.fromUserName), fp);
  891.     fsh_write(msgdate, sizeof(char), strlen(msgdate), fp);
  892.     fsh_write(recvdate, sizeof(char), strlen(recvdate), fp);
  893.     reallen = fsh_write(p, sizeof(char), (int) textlen, fp);
  894.     if (reallen != textlen)
  895.       output("\n ■ Expected %ld bytes, wrote %ld bytes.", textlen, reallen);
  896.     nh.tosys = 32767;
  897.     --tolist;
  898.   }
  899.   if (fp != NULL)
  900.     fclose(fp);
  901.   free(p);
  902.   return (0);
  903. }
  904.  
  905.  
  906. char *stripcolors(char *str)
  907. {
  908.   char *obuf, *nbuf;
  909.  
  910.   if (str) {
  911.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  912.       if (*obuf == 3)
  913.         ++obuf;
  914.       else if (((*obuf < 32) && (*obuf != 9)) || (*obuf > 126))
  915.         continue;
  916.       else
  917.         *nbuf++ = *obuf;
  918.     }
  919.     *nbuf = NULL;
  920.   }
  921.   return (str);
  922. }
  923.  
  924. void properize(char *s)
  925. {
  926.   int i;
  927.  
  928.   for (i = 0; i < strlen(s); i++) {
  929.     if ((i == 0) || ((i > 0) && ((s[i - 1] == ' ') || (s[i - 1] == '.') ||
  930.           ((s[i - 1] == 'c') && (s[i - 2] == 'M')) || ((s[i - 1] == 'c') &&
  931.           (s[i - 2] == 'a') && (s[i - 3] == 'M')) || (s[i - 1] == '-') ||
  932.           (s[i - 1] == '_') || ((s[i - 1] == '\'') && (s[i - 2] == 'O'))))) {
  933.       s[i] = toupper(s[i]);
  934.     } else
  935.       s[i] = tolower(s[i]);
  936.   }
  937. }
  938.  
  939.  
  940.  
  941. int export(char *fn)
  942. {
  943.   char fn1[121], tagfn[121], groupname[81], outfn[121], _temp_buffer[256];
  944.   char *ss, *buffer, *text, alphatype[21], hold[21], tempoutfn[21];
  945.   unsigned stype, ttype;
  946.   int infile, outfile, inloc, outloc, term, ok, i, j, ns, i6, tolist;
  947.   net_header_rec nhr;
  948.   struct msghdr mh;
  949.   struct tm *time_msg;
  950.   FILE *fp;
  951.   time_t some;
  952.   char *main_type[] =
  953.   {
  954.     "Network Update", "email by usernum", "post from sub host", "file",
  955.     "post to sub host", "external message", "email by name",
  956.     "NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
  957.     "CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
  958.     "Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info",
  959.     "Unused 1", "Unused 2", "Unused 3", "Unused 4", "Unused 5", "new post",
  960.     "new external"
  961.   };
  962.  
  963.   if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
  964.     return 1;
  965.  
  966.   if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
  967.     sh_close(infile);
  968.     output("\n ■ Out of memory allocating input buffer!");
  969.     return 1;
  970.   }
  971.   if ((text = (char *) malloc(32 * 1024)) == NULL) {
  972.     output("\n ■ Out of memory allocating output buffer!");
  973.     sh_close(infile);
  974.     if (buffer != NULL)
  975.       free(buffer);
  976.     return 1;
  977.   }
  978.   while (sh_read(infile, &nhr, sizeof(nhr))) {
  979.     sh_read(infile, buffer, (int) nhr.length);
  980.     if (nhr.tosys != 32767) {
  981.       output("\n ■ Non-Internet system routing via @32767... aborting export.");
  982.       sh_close(infile);
  983.       if (text)
  984.         free(text);
  985.       if (buffer)
  986.         free(buffer);
  987.       return (1);
  988.     }
  989.     tolist = 0;
  990.     if (nhr.main_type == main_type_pre_post)
  991.       nhr.main_type = main_type_post;
  992.     if ((nhr.main_type == main_type_post) ||
  993.         (nhr.main_type == main_type_new_post) ||
  994.         (nhr.main_type == main_type_email_name) ||
  995.         (nhr.main_type == main_type_ssm)) {
  996.       inloc = 0;
  997.       sprintf(hold, "%hu", nhr.minor_type);
  998.       if ((nhr.main_type == main_type_email_name) ||
  999.           (nhr.main_type == main_type_ssm) ||
  1000.           (nhr.main_type == main_type_new_post)) {
  1001.         stype = nhr.minor_type;
  1002.         inloc = strlen(buffer) + 1;
  1003.         if ((nhr.main_type == main_type_new_post) || ((nhr.fromsys != net_sysnum) &&
  1004.                 (nhr.fromsys != 32767))) {
  1005.           strcpy(alphasubtype, buffer);
  1006.           strcpy(hold, alphasubtype);
  1007.         } else
  1008.           strcpy(mh.toUserName, buffer);
  1009. /*
  1010.         if ((nhr.fromsys != net_sysnum) && (nhr.fromsys != 32767)) {
  1011.           output("\n ■ Gate from #%hd@%hd to %s not yet supported",
  1012.                  nhr.fromuser, nhr.fromsys, mh.toUserName);
  1013.           continue;
  1014.         }
  1015. */
  1016.       } else if (nhr.main_type == main_type_post) {
  1017.         stype = nhr.minor_type;
  1018.       } else {
  1019.         stype = atoi(&buffer[inloc]);
  1020.         sprintf(_temp_buffer, "%u", stype);
  1021.         inloc += strlen(_temp_buffer) + 1;
  1022.       }
  1023.  
  1024.       strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  1025.       stripcolors(mh.subject);
  1026.       inloc += strlen(&buffer[inloc]) + 1;
  1027.  
  1028.       for (term = inloc; buffer[term] != '\r'; term++);
  1029.       buffer[term] = '\0';
  1030.  
  1031.       if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
  1032.         if ((nhr.main_type != main_type_post) &&
  1033.             (nhr.main_type != main_type_new_post)) {
  1034.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  1035.             output("\n ■ No match for user #%hd... skipping message!",
  1036.                    nhr.fromuser);
  1037.             continue;
  1038.           }
  1039.         } else {
  1040.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  1041.             output("\n ■ No match for user #%hd... skipping message!",
  1042.                    nhr.fromuser);
  1043.             continue;
  1044.           }
  1045.         }
  1046.       } else {
  1047.         strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  1048.         stripcolors(mh.fromUserName);
  1049.         strtok(mh.fromUserName, "#");
  1050.         trimstr1(mh.fromUserName);
  1051.         if ((nhr.main_type == main_type_post) &&
  1052.             (nhr.main_type == main_type_new_post) &&
  1053.             (nhr.fromsys != net_sysnum)) {
  1054.           sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
  1055.           strcat(mh.fromUserName, _temp_buffer);
  1056.           output("\nFrom: %s\n", mh.fromUserName);
  1057.         }
  1058.       }
  1059.  
  1060.       inloc = term + 2;
  1061.  
  1062.       while (buffer[inloc] != '\n')
  1063.         inloc++;
  1064.       inloc++;
  1065.  
  1066.       if (strnicmp(&buffer[inloc], "RE: ", 4) == 0) {
  1067.         for (term = inloc; buffer[term] != '\r'; term++);
  1068.         buffer[term] = '\0';
  1069.         strncpy(mh.subject, &buffer[inloc + 4], sizeof(mh.subject));
  1070.         if (strnicmp(mh.subject, "RE: ", 4) != 0) {
  1071.           strcpy(_temp_buffer, "Re: ");
  1072.           strcat(_temp_buffer, mh.subject);
  1073.         }
  1074.         strcpy(mh.subject, _temp_buffer);
  1075.         inloc = term + 2;
  1076.       }
  1077.       if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
  1078.           (strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
  1079.         for (term = inloc; buffer[term] != '\r'; term++);
  1080.         buffer[term] = '\0';
  1081.         strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
  1082.         stripcolors(mh.toUserName);
  1083.         if (strcspn(mh.toUserName, "<") != strlen(mh.toUserName)) {
  1084.           if ((_fstrstr(mh.toUserName, "\"") == 0) && (_fstrstr(mh.toUserName, "(") == 0)) {
  1085.             ss = strtok(mh.toUserName, "<");
  1086.             ss = strtok(NULL, ">");
  1087.             strcpy(mh.toUserName, ss);
  1088.           }
  1089.         }
  1090.         inloc = term + 2;
  1091.       } else {
  1092.         if (nhr.main_type != main_type_email_name) {
  1093.           strcpy(mh.toUserName, "ALL");
  1094.         }
  1095.       }
  1096.       outloc = 0;
  1097.       do {
  1098.         if (buffer[inloc] == 2) {
  1099.           i = inloc + 1;
  1100.           for (j = 1; j < 255; j++) {
  1101.             if (buffer[i] == 'π')
  1102.               buffer[i] = '\r';
  1103.             if ((buffer[i] == '\r') || (i > nhr.length))
  1104.               break;
  1105.             i++;
  1106.           }
  1107.           if (j < 80) {
  1108.             i = (80 - j) / 2;
  1109.             for (j = 1; j <= i; j++)
  1110.               text[outloc++] = ' ';
  1111.           }
  1112.           inloc++;
  1113.         } else
  1114.           if (buffer[inloc] == 3)
  1115.           inloc += 2;
  1116.         else
  1117.           if ((buffer[inloc] == 124) && (isdigit(buffer[inloc + 1])) && (isdigit(buffer[inloc + 2])))
  1118.           inloc += 3;
  1119.         else
  1120.           if ((buffer[inloc] == 4) && (isdigit(buffer[inloc + 1]))) {
  1121.           i = inloc;
  1122.           for (j = 1; j < 255; j++) {
  1123.             if ((buffer[i] == '\r') || (i > nhr.length))
  1124.               break;
  1125.             i++;
  1126.             inloc++;
  1127.           }
  1128.           inloc++;
  1129.           if (buffer[inloc] == '\n')
  1130.             inloc++;
  1131.         } else
  1132.           if (buffer[inloc] >= 127)
  1133.           inloc++;
  1134.         else
  1135.           if (buffer[inloc] == 1)
  1136.           inloc++;
  1137.         else
  1138.           text[outloc++] = buffer[inloc++];
  1139.       } while (inloc < nhr.length);
  1140.  
  1141.       text[outloc] = '\0';
  1142.  
  1143.       if ((nhr.main_type == main_type_post) ||
  1144.           (nhr.main_type == main_type_pre_post) ||
  1145.           (nhr.main_type == main_type_new_post)) {
  1146.         if (nhr.main_type == main_type_new_post) {
  1147.           sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
  1148.           if (exist(fn1)) {
  1149.             tolist = 1;
  1150.             nhr.main_type = main_type_email_name;
  1151.           }
  1152.         }
  1153.         for (i = 0; (i < nlists) && (nhr.main_type != main_type_email_name); i++) {
  1154.           if (nhr.main_type == main_type_new_post) {
  1155.             if (strcmpi(alphasubtype, maillist[i].subtype) == 0) {
  1156.               nhr.main_type = main_type_email_name;
  1157.               strcpy(mh.toUserName, maillist[i].ownername);
  1158.             }
  1159.           } else {
  1160.             ttype = atoi(maillist[i].subtype);
  1161.             if (ttype == stype) {
  1162.               nhr.main_type = main_type_email_name;
  1163.               strcpy(mh.toUserName, maillist[i].ownername);
  1164.             }
  1165.           }
  1166.         }
  1167.       }
  1168.       switch (nhr.main_type) {
  1169.         case main_type_email:
  1170.         case main_type_email_name:
  1171.         case main_type_ssm:
  1172.           i = 1;
  1173.           sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
  1174.           while (exist(outfn))
  1175.             sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  1176.           break;
  1177.         case main_type_new_post:
  1178.         case main_type_post:
  1179.         case main_type_pre_post:
  1180.           i = 1;
  1181.           strcpy(tempoutfn, hold);
  1182.           sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, i);
  1183.           while (exist(outfn))
  1184.             sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, ++i);
  1185.           break;
  1186.         default:
  1187.           continue;
  1188.       }
  1189.  
  1190.       properize(mh.fromUserName);
  1191.       output("\n ■ Creating: %s", outfn);
  1192.       if (nhr.fromsys != net_sysnum)
  1193.         output("\n ■ From    : %s", mh.fromUserName);
  1194.       else if (usermail)
  1195.         output("\n ■ From    : %s <%s@%s>", mh.fromUserName, POPNAME, DOMAIN);
  1196.       else
  1197.         output("\n ■ From    : <%s@%s>", POPNAME, DOMAIN);
  1198.       if ((nhr.main_type == main_type_post) ||
  1199.           (nhr.main_type == main_type_pre_post) ||
  1200.           (nhr.main_type == main_type_new_post)) {
  1201.         sprintf(fn1, "%sNEWS.RC", net_data);
  1202.         if ((fp = fsh_open(fn1, "rt")) == NULL) {
  1203.           output("\n ■ %s not found!", fn1);
  1204.           sh_close(infile);
  1205.           if (text)
  1206.             free(text);
  1207.           if (buffer)
  1208.             free(buffer);
  1209.           return 1;
  1210.         } else {
  1211.           ok = 0;
  1212.           while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
  1213.             groupname[0] = 0;
  1214.             ss = strtok(_temp_buffer, " ");
  1215.             if (ss) {
  1216.               strcpy(groupname, ss);
  1217.               ss = strtok(NULL, " ");
  1218.               ss = strtok(NULL, "\r");
  1219.               if (nhr.main_type == main_type_new_post) {
  1220.                 strcpy(alphatype, ss);
  1221.                 if (strncmpi(alphasubtype, alphatype, strlen(alphasubtype)) == 0)
  1222.                   ok = 1;
  1223.               } else {
  1224.                 ttype = atoi(ss);
  1225.                 if (ttype == stype)
  1226.                   ok = 1;
  1227.               }
  1228.             }
  1229.           }
  1230.           *ss = NULL;
  1231.           if (fp != NULL)
  1232.             fclose(fp);
  1233.           if (!ok) {
  1234.             if (nhr.main_type != main_type_new_post)
  1235.               sprintf(alphatype, "%u", stype);
  1236.             output("\n ■ Subtype %s not found in NEWS.RC!", alphatype);
  1237.             sh_close(infile);
  1238.             if (text)
  1239.               free(text);
  1240.             if (buffer)
  1241.               free(buffer);
  1242.             return 1;
  1243.           }
  1244.         }
  1245.       }
  1246.       if ((nhr.main_type == main_type_email) ||
  1247.           (nhr.main_type == main_type_email_name) ||
  1248.           (nhr.main_type == main_type_ssm)) {
  1249.         if (tolist)
  1250.           output("\n ■ Sent to : %s Mailing List", alphasubtype);
  1251.         else
  1252.           output("\n ■ Rcpt to : %s", mh.toUserName);
  1253.       } else
  1254.         output("\n ■ Post to : %s", groupname);
  1255.  
  1256.       strcpy(_temp_buffer, mh.subject);
  1257.       j = 0;
  1258.       for (i = 0; i < strlen(mh.subject); i++) {
  1259.         if (_temp_buffer[i] == 3)
  1260.           ++i;
  1261.         else
  1262.           mh.subject[j++] = _temp_buffer[i];
  1263.       }
  1264.       mh.subject[j] = '\0';
  1265.  
  1266.       output("\n ■ Subject : %s", mh.subject);
  1267.  
  1268.       outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
  1269.       if (outfile == -1) {
  1270.         sh_close(infile);
  1271.         if (buffer)
  1272.           free(buffer);
  1273.         if (text)
  1274.           free(text);
  1275.         output("\n ■ Unable to open output file %s", outfn);
  1276.         return 1;
  1277.       }
  1278.       time(&some);
  1279.       time_msg = localtime(&some);
  1280.       strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
  1281.       if (nhr.fromsys != net_sysnum) {
  1282.         sprintf(_temp_buffer, "From: %s #%hd-%hu <%s@%s>\n", mh.fromUserName,
  1283.             nhr.fromuser, nhr.fromsys, POPNAME, DOMAIN);
  1284.       } else {
  1285.         for (j = 0; j < strlen(mh.fromUserName); j++)
  1286.         if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)))
  1287.           if (spamname[0] == 0)
  1288.             sprintf(_temp_buffer, "From: %s <%s@dont.spam.me.%s>\n",
  1289.                     mh.fromUserName, POPNAME, DOMAIN);
  1290.           else
  1291.             sprintf(_temp_buffer, "From: %s <%s>\n", mh.fromUserName, spamname);
  1292.         else {
  1293.           if (usermail)
  1294.             sprintf(_temp_buffer, "From: %s <%s@%s>\n",
  1295.                     mh.fromUserName, POPNAME, DOMAIN);
  1296.           else
  1297.             sprintf(_temp_buffer, "From: <%s@%s>\n", POPNAME, DOMAIN);
  1298.         }
  1299.       }
  1300.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1301.       ++cur_daten;
  1302.       sprintf(_temp_buffer, "Message-ID: <%lx-%s@WWIV-BBS>\n",
  1303.               cur_daten, POPNAME);
  1304.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1305.       if ((nhr.main_type == main_type_email) ||
  1306.           (nhr.main_type == main_type_email_name) ||
  1307.           (nhr.main_type == main_type_ssm)) {
  1308.         if (!tolist) {
  1309.           sprintf(_temp_buffer, "To: %s\n", mh.toUserName);
  1310.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1311.         } else {
  1312.           sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
  1313.           i = 0;
  1314.           if ((fp = fsh_open(fn1, "rt")) != NULL) {
  1315.             while (fgets(_temp_buffer, 80, fp) != NULL) {
  1316.               if (_fstrstr(_temp_buffer, "@")) {
  1317.                 strcpy(mh.toUserName, _temp_buffer);
  1318.                 sprintf(_temp_buffer, "To: %s", mh.toUserName);
  1319.                 if (_temp_buffer[strlen(_temp_buffer) - 1] != '\n')
  1320.                   strcat(_temp_buffer, "\n");
  1321.                 sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1322.                 i = 1;
  1323.               }
  1324.             }
  1325.             if (fp != NULL)
  1326.               fclose(fp);
  1327.           }
  1328.           if (!i) {
  1329.             sh_close(infile);
  1330.             if (buffer)
  1331.               free(buffer);
  1332.             if (text)
  1333.               free(text);
  1334.             output("\n ■ Error processing mailing list %s.", alphasubtype);
  1335.             return (1);
  1336.           } else {
  1337.             sprintf(_temp_buffer, "Reply-To: %s <%s@%s>\n", alphasubtype,
  1338.                     POPNAME, DOMAIN);
  1339.             sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1340.           }
  1341.         }
  1342.       } else {
  1343.         sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
  1344.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1345.       }
  1346.       sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
  1347.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1348.       sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
  1349.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1350.       if (nhr.main_type != main_type_email_name) {
  1351.         sprintf(_temp_buffer, "Path: %s!%s\n", POPNAME, DOMAIN);
  1352.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1353.         sprintf(_temp_buffer, "Organization: %s * %s\n",
  1354.               syscfg.systemname, syscfg.systemphone);
  1355.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1356.       }
  1357.       if (tolist) {
  1358.         sprintf(_temp_buffer, "X-Reply-To: %s <%s@%s>\n",
  1359.                 alphasubtype, POPNAME, DOMAIN);
  1360.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1361.       } else if ((!spam) && ((nhr.main_type != main_type_post) || (nhr.main_type != main_type_new_post))) {
  1362.         sprintf(_temp_buffer, "Reply-To: %s <%s@%s>\n",
  1363.                 mh.fromUserName, POPNAME, DOMAIN);
  1364.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1365.       }
  1366.  
  1367.       if (tolist) {
  1368.         sprintf(_temp_buffer, "Source: %s Mail List\n", alphasubtype);
  1369.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1370.       }
  1371.  
  1372.       if (((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)) && (!tolist)) {
  1373.         if (*REPLYTO)
  1374.           sprintf(_temp_buffer, "Reply-to: %s <%s>\n", mh.fromUserName, REPLYTO);
  1375.         else
  1376.           sprintf(_temp_buffer, "Reply-to: %s <%s@%s>\n",
  1377.                   mh.fromUserName, POPNAME, DOMAIN);
  1378.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1379.       }
  1380.  
  1381.       sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n", VERSION);
  1382.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1383.  
  1384.       if ((nhr.main_type != main_type_email) &&
  1385.           (nhr.main_type != main_type_email_name) &&
  1386.           (strncmp(mh.toUserName, "ALL", 3) != 0)) {
  1387.         sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
  1388.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1389.       }
  1390.       for (i = 0; i < strlen(text); i++)
  1391.         if (text[i] == 'π')
  1392.           text[i] = '\n';
  1393.  
  1394.       sh_write(outfile, text, strlen(text));
  1395.       sprintf(tagfn, "%sI%u.TAG", syscfg.datadir, stype);
  1396.       if (exist(tagfn))
  1397.         strcpy(tagfile, tagfn);
  1398.       tagfn[0] = 0;
  1399.       ns = 0;
  1400.       for (i6 = 0; i6 < 99; i6++) {
  1401.         sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, i6);
  1402.         if (exist(tagfn))
  1403.           ns++;
  1404.         else
  1405.           break;
  1406.       }
  1407.       sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, random(ns));
  1408.       if (exist(tagfn))
  1409.         strcpy(tagfile, tagfn);
  1410.       if (tagfile[0] == 0) {
  1411.         sprintf(_temp_buffer, "\n\nOrigin: %s * %s\n\n",
  1412.                 syscfg.systemname, syscfg.systemphone);
  1413.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1414.       } else {
  1415.         if ((fp = fsh_open(tagfile, "rt")) == NULL)
  1416.           output("\n ■ Error reading %s.", tagfile);
  1417.         else {
  1418.           sprintf(_temp_buffer, "\n\n");
  1419.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1420.           while (fgets(_temp_buffer, 120, fp)) {
  1421.             for (i = 0; ((i < strlen(_temp_buffer)) &&
  1422.                (_temp_buffer[i] != '\r') && (_temp_buffer[i] != '\n')); i++)
  1423.               if ((_temp_buffer[i] < 32) || (_temp_buffer[i] > 126))
  1424.                 _temp_buffer[i] = 32;
  1425.             sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1426.           }
  1427.           if (fp != NULL)
  1428.             fclose(fp);
  1429.           sprintf(_temp_buffer, "\n\n");
  1430.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1431.         }
  1432.       }
  1433.       sh_close(outfile);
  1434.     } else {
  1435.       if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x1b))
  1436.         output("\n ■ %s message skipped",
  1437.                main_type[nhr.main_type - 1]);
  1438.       else
  1439.         output("\n ■ Unknown Main_type %hd skipped", nhr.main_type);
  1440.     }
  1441.   }
  1442.   sh_close(infile);
  1443.   unlink(fn);
  1444.   if (text)
  1445.     free(text);
  1446.   if (buffer)
  1447.     free(buffer);
  1448.   return (0);
  1449. }
  1450.  
  1451. void get_dir(char *s, int be)
  1452. {
  1453.   strcpy(s, "X:\\");
  1454.   s[0] = 'A' + getdisk();
  1455.   getcurdir(0, s + 3);
  1456.   if (be) {
  1457.     if (s[strlen(s) - 1] != '\\')
  1458.       strcat(s, "\\");
  1459.   }
  1460. }
  1461.  
  1462. int main(int argc, char *argv[])
  1463. {
  1464.   char fn[MAXPATH], *ss;
  1465.   int f, f1, i;
  1466.   struct ffblk ff;
  1467.   struct date dt;
  1468.   struct time tm;
  1469.  
  1470.   output("\n ■ PPP Import/Export %s", VERSION);
  1471.   if (argc != 7) {
  1472.     output("\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
  1473.     if (argc > 1) {
  1474.       output("Command line was: ");
  1475.       for (i = 0; i < argc; i++)
  1476.         output("%s ", argv[i]);
  1477.       output("\n\n");
  1478.     }
  1479.     return (1);
  1480.   }
  1481.  
  1482.   f = sh_open1("CONFIG.DAT", O_RDONLY | O_BINARY);
  1483.   if (f < 0) {
  1484.     output("Could not open CONFIG.DAT!\n\n");
  1485.     return 1;
  1486.   }
  1487.   sh_read(f, (void *) &syscfg, sizeof(configrec));
  1488.   sh_close(f);
  1489.  
  1490.   detect_multitask();
  1491.  
  1492.   get_dir(maindir, 1);
  1493.   strcpy(net_data, argv[2]);
  1494.   sprintf(fn, "%s%s", net_data, argv[1]);
  1495.   strcpy(net_name, argv[6]);
  1496.   strcpy(POPNAME, argv[4]);
  1497.   strcpy(DOMAIN, argv[5]);
  1498.   net_sysnum = atoi(argv[3]);
  1499.   tagfile[0] = 0;
  1500.   spam = 0;
  1501.  
  1502.   ss = getenv("WWIV_INSTANCE");
  1503.   if (ss) {
  1504.     instance = atoi(ss);
  1505.     if (instance >= 1000) {
  1506.       output("\n ■ WWIV_INSTANCE set to %hd.  Can only be 1..999!",
  1507.              instance);
  1508.       instance = 1;
  1509.     }
  1510.   } else
  1511.     instance = 1;
  1512.  
  1513.   parse_net_ini();
  1514.   gettime(&tm);
  1515.   getdate(&dt);
  1516.   cur_daten = dostounix(&dt, &tm);
  1517.  
  1518.   strupr(postmaster);
  1519.   export(fn);
  1520.  
  1521.   sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
  1522.   f1 = findfirst(fn, &ff, 0);
  1523.   while (f1 == 0) {
  1524.     sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1525.     if (!import(fn)) {
  1526.       unlink(fn);
  1527.     }
  1528.     f1 = findnext(&ff);
  1529.   }
  1530.   if (maillist != NULL)
  1531.     free(maillist);
  1532.   return 0;
  1533. }
  1534.