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