home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B75.ZIP / EXP.CPP < prev    next >
Text File  |  1997-12-01  |  51KB  |  1,764 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
  427.       if (line[0] == '[') {
  428.       inlist = 0;
  429.       continue;
  430.       }
  431.     if (inlist) {
  432.       if ((line[0] != ';') && (line[0] != 0)) {
  433.         ss = strtok(line, "\"");
  434.         trimstr1(ss);
  435.         if (*ss) {
  436.           ss = strtok(NULL, "\"");
  437.           if (*ss) {
  438.             strncpy(maillist[nlists].opttext, ss, 29);
  439.             maillist[nlists].opttext[30] = '\0';
  440.           } else
  441.             maillist[nlists].opttext[0] = '\0';
  442.         }
  443.         ss = strtok(line, "*\n");
  444.         trimstr1(ss);
  445.         strcpy(maillist[nlists].ownername, ss);
  446.         ss = strtok(NULL, "\"\n");
  447.         trimstr1(ss);
  448.         if (*ss) {
  449.           strlwr(maillist[nlists].ownername);
  450.           strcpy(maillist[nlists++].subtype, ss);
  451.         } else
  452.           output("\n ■ Missing *subtype in maillist for %s.",
  453.                  maillist[nlists].ownername);
  454.       }
  455.       continue;
  456.     }
  457.     if (strnicmp(line, "POSTMASTER", 10) == 0) {
  458.       ss = strtok(line, "=");
  459.       if (ss) {
  460.         ss = strtok(NULL, "\n");
  461.         if (ss)
  462.           defuser = atoi(ss);
  463.       }
  464.       continue;
  465.     }
  466.     if (strnicmp(line, "SPAMCONTROL", 11) == 0) {
  467.       ss = strtok(line, "=");
  468.       if (ss) {
  469.         ss = strtok(NULL, "\n");
  470.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  471.           spam = 1;
  472.       }
  473.       continue;
  474.     }
  475.     if (strnicmp(line, "USERMAIL", 8) == 0) {
  476.       ss = strtok(line, "=");
  477.       if (ss) {
  478.         ss = strtok(NULL, "\n");
  479.         if ((ss[0] == 'n') || (ss[0] == 'N'))
  480.           usermail = 0;
  481.       }
  482.       continue;
  483.     }
  484.     if (strnicmp(line, "SPAMADDRESS", 9) == 0) {
  485.       ss = strtok(line, "=");
  486.       if (ss) {
  487.         ss = strtok(NULL, "\n");
  488.         trimstr1(ss);
  489.         strcpy(spamname, ss);
  490.         if (stricmp(spamname, "DEFAULT") == 0)
  491.           strcpy(spamname, "WWIV_BBS@nospam.net");
  492.       }
  493.       continue;
  494.     }
  495.     if (strnicmp(line, "REPLYTO", 7) == 0) {
  496.       ss = strtok(origline, "=");
  497.       if (ss) {
  498.         ss = strtok(NULL, "\n");
  499.         trimstr1(ss);
  500.         strcpy(REPLYTO, ss);
  501.       }
  502.       continue;
  503.     }
  504.     if (strnicmp(line, "SIGNATURE", 9) == 0) {
  505.       ss = strtok(line, "=");
  506.       if (ss) {
  507.         ss = strtok(NULL, "\n");
  508.         trimstr1(ss);
  509.         strcpy(tagfile, ss);
  510.         if (!exist(tagfile)) {
  511.           output("\n ■ Default signature file %s not found!", tagfile);
  512.           tagfile[0] = 0;
  513.         }
  514.       }
  515.       continue;
  516.     }
  517.     if (strnicmp(line, "REALNAME", 8) == 0) {
  518.       ss = strtok(line, "=");
  519.       if (ss) {
  520.         ss = strtok(NULL, "\n");
  521.         if ((ss[0] == 'y') || (ss[0] == 'Y'))
  522.           use_alias = 0;
  523.       }
  524.     }
  525.   }
  526.   num_to_name(postmaster, defuser, 1);
  527.   if (fp != NULL)
  528.     fclose(fp);
  529.   return;
  530. }
  531.  
  532. unsigned char *strrep(char *str, char old, char New)
  533. {
  534.   int i;
  535.  
  536.   for (i = 0; str[i]; i++)
  537.     if (str[i] == old)
  538.       str[i] = New;
  539.   return (str);
  540. }
  541.  
  542.  
  543. unsigned int name_to_num(char *name)
  544. {
  545.   int userfile, usernum;
  546.   userrec ur;
  547.   long pos;
  548.   char fn[MAXPATH], ur_name[60], ur_realname[60];
  549.  
  550.   sprintf(fn, "%sM%s.NET", net_data, name);
  551.   if (exist(fn)) {
  552.     output("\n ■ Matched \"%s\" mailing list.", name);
  553.     strcpy(alphasubtype, name);
  554.     return (unsigned) (65535L);
  555.   }
  556.   sprintf(fn, "%sUSER.LST", syscfg.datadir);
  557.   userfile = sh_open1(fn, O_RDONLY | O_BINARY);
  558.   if (userfile < 0) {
  559.     output("\n ■ Cannot open %s", fn);
  560.     return (0);
  561.   } else
  562.     output("\n ■ Searching for user \"%s\"...", name);
  563.   num_users = ((int) (filelength(userfile) / sizeof(userrec)));
  564.  
  565.   for (usernum = 1; usernum < num_users; usernum++) {
  566.     pos = ((long) sizeof(userrec) * ((long) usernum));
  567.     lseek(userfile, pos, SEEK_SET);
  568.     sh_read(userfile, &ur, sizeof(userrec));
  569.     strcpy(ur_realname, ur.realname);
  570.     strrep(ur_realname, ' ', '_');
  571.     strcpy(ur_name, ur.name);
  572.     strrep(ur_name, ' ', '_');
  573.     if ((strcmpi(ur.realname, name) == 0) || (strcmpi(ur_realname, name) == 0) ||
  574.         (strcmpi(ur.name, name) == 0) || (strcmpi(ur_name, name) == 0)) {
  575.       if (ur.inact == inact_deleted) {
  576.         output(" user #%d is deleted account.", usernum);
  577.         usernum = 0;
  578.         break;
  579.       } else {
  580.         output(" matched to user #%d.", usernum);
  581.         break;
  582.       }
  583.     }
  584.   }
  585.   userfile = sh_close(userfile);
  586.  
  587.   if (usernum >= num_users) {
  588.     output("... no match found.");
  589.     return (0);
  590.   }
  591.   return (usernum);
  592. }
  593.  
  594. char *find_name(char *name)
  595. {
  596.   char *ss, *ss1;
  597.  
  598.   curuser = 0;
  599.   ss1 = NULL;
  600.   if ((ss = strchr(name, '(')) != NULL) {
  601.     ss1 = strtok(name, "(");
  602.     ss1 = strtok(NULL, ")");
  603.   } else if ((ss = strchr(name, '\"')) != NULL) {
  604.     ss1 = strtok(ss, "\"");
  605.   } else if ((ss = strchr(name, '<')) != NULL) {
  606.     ss1 = strtok(name, "<");
  607.   }
  608.   if (*ss1) {
  609.     trimstr1(ss1);
  610.     curuser = name_to_num(ss1);
  611.   }
  612.   if (curuser == 0)
  613.     return ('\0');
  614.   else if (curuser == ((unsigned) 65535L))
  615.     return (alphasubtype);
  616.   else
  617.     return (ss1);
  618. }
  619.  
  620. void name_packet(char *pktname)
  621. {
  622.   int ok;
  623.   struct stat info;
  624.   unsigned i;
  625.  
  626.   ok = 0;
  627.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  628.     sprintf(pktname, "%sP0-%u.%3.3hu", net_data, i, instance);
  629.     if (stat(pktname, &info) == -1)
  630.       ok = 1;
  631.   }
  632. }
  633.  
  634. char *stristr(char *String, char *Pattern)
  635. {
  636.   char *pptr, *sptr, *start;
  637.   unsigned int slen, plen;
  638.  
  639.   for (start = String, pptr = Pattern, slen = strlen(String),
  640.        plen = strlen(Pattern); slen >= plen; start++, slen--) {
  641.     while (toupper(*start) != toupper(*Pattern)) {
  642.       start++;
  643.       slen--;
  644.       if (slen < plen)
  645.         return (NULL);
  646.     }
  647.     sptr = start;
  648.     pptr = Pattern;
  649.     while (toupper(*sptr) == toupper(*pptr)) {
  650.       sptr++;
  651.       pptr++;
  652.       if ('\0' == *pptr)
  653.         return (start);
  654.     }
  655.   }
  656.   return (NULL);
  657. }
  658.  
  659. #define FROM_RETURN 0x01
  660. #define FROM_FROM   0x02
  661. #define FROM_REPLY  0x04
  662.  
  663. int import(char *fn)
  664. {
  665.   char s[513], s1[121], pktname[MAXPATH], msgdate[61], *ss, *p, *id, *name;
  666.   char alphatype[21], recvdate[81];
  667.   int i, f, from, match, subj, intext, done, tolist;
  668.   long textlen, reallen;
  669.   struct msghdr mh;
  670.   net_header_rec nh;
  671.   FILE *fp;
  672.  
  673.   tolist = 0;
  674.   intext = 0;
  675.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  676.   if (f < 0)
  677.     return (1);
  678.   textlen = filelength(f);
  679.   if (textlen > 32767L) {
  680.     output("\n ■ Skipping %s - greater than 32K.", fn);
  681.     return (1);
  682.   }
  683.   p = (char *) malloc((int) (textlen + 1));
  684.   if (p == NULL) {
  685.     output("\n ■ Unable to allocate %ld bytes.", textlen);
  686.     return (1);
  687.   }
  688.   sh_read(f, (void *) p, (int) textlen);
  689.   sh_close(f);
  690.  
  691.   nh.tosys = net_sysnum;
  692.   nh.fromsys = 32767;
  693.   nh.fromuser = 0;
  694.   nh.touser = defuser;
  695.   nh.main_type = main_type_email;
  696.   nh.minor_type = 0;
  697.   nh.list_len = 0;
  698.   ++cur_daten;
  699.   nh.daten = cur_daten;
  700.   strncpy(msgdate, ctime(&(time_t) nh.daten), 24);
  701.   msgdate[24] = '\0';
  702.   sprintf(recvdate, "0RReceived: PPP Project %s on %s\r\n", VERSION, msgdate);
  703.   strcat(msgdate, "\r\n");
  704.   nh.method = 0;
  705.  
  706.   strcpy(mh.fromUserName, "Unknown");
  707.   strcpy(mh.toUserName, "Unknown");
  708.   strcpy(mh.subject, "None");
  709.  
  710.   if ((fp = fsh_open(fn, "rb")) == NULL) {
  711.     free(p);
  712.     return (1);
  713.   }
  714.   match = subj = done = 0;
  715.   while ((fgets(s, 254, fp)) && !done) {
  716.     if (s[0] == 4) {
  717.       ss = strtok(s, "R");
  718.       ss = strtok(NULL, "\r\n");
  719.       if (ss == NULL)
  720.         s[0] = 0;
  721.       else
  722.         strcpy(s, ss);
  723.     } else
  724.       intext = 1;
  725.     if (!intext) {
  726.       if (strncmpi(s, "from:", 5) == 0)
  727.         from = FROM_FROM;
  728.       else
  729.         if (strncmpi(s, "return-path:", 12) == 0)
  730.         from = FROM_RETURN;
  731.       else
  732.         if (strncmpi(s, "sender:", 7) == 0)
  733.         from = FROM_RETURN;
  734.       else
  735.         if (strncmpi(s, "x-sender:", 9) == 0)
  736.         from = FROM_RETURN;
  737.       else
  738.         if (strncmpi(s, "x-to:", 5) == 0)
  739.         from = FROM_RETURN;
  740.       else
  741.         if (strncmpi(s, "reply-to:", 9) == 0)
  742.         from = FROM_REPLY;
  743.       else
  744.         if (strncmpi(s, "x-reply-to:", 11) == 0)
  745.         from = FROM_REPLY;
  746.       else
  747.         from = 0;
  748.       if (from) {
  749.         ss = strtok(s, ": ");
  750.         ss = strtok(NULL, "\r\n");
  751.         trimstr1(ss);
  752.         if ((from & (FROM_RETURN | FROM_REPLY)) && (nh.main_type == main_type_email)) {
  753.           strcpy(s1, ss);
  754.           strlwr(s1);
  755.           for (i = 0; (i < nlists) && (nh.main_type == main_type_email); i++) {
  756.             if (stristr(s1, maillist[i].ownername) != NULL) {
  757.               if (atoi(maillist[i].subtype)) {
  758.                 nh.main_type = main_type_pre_post;
  759.                 nh.minor_type = atoi(maillist[i].subtype);
  760.               } else {
  761.                 nh.main_type = main_type_new_post;
  762.                 nh.minor_type = 0;
  763.                 strcpy(alphatype, maillist[i].subtype);
  764.               }
  765.               strcpy(alphasubtype, maillist[i].subtype);
  766.               nh.touser = 0;
  767.               from = 0;
  768.             }
  769.           }
  770.         }
  771.         if ((from > match) && ((nh.main_type == main_type_email) || (from == FROM_FROM))) {
  772.           match = from;
  773.           if (strcspn(ss, "<") != strlen(ss)) {
  774.             if ((strcspn(ss, " ")) < (strcspn(ss, "<"))) {
  775.               name = strtok(ss, "<");
  776.               trimstr1(name);
  777.               id = strtok(NULL, ">");
  778.               trimstr1(id);
  779.               sprintf(mh.fromUserName, "%s <%s>", name, id);
  780.             } else {
  781.               strncpy(mh.fromUserName, ss, 205);
  782.               trimstr1(mh.fromUserName);
  783.               if (strcspn(ss, " ") != strlen(ss))
  784.                 output("\nName is *after* host in \"%s\"", name);
  785.             }
  786.           } else
  787.             strncpy(mh.fromUserName, ss, 205);
  788.           mh.fromUserName[190] = 0;
  789.           strcat(mh.fromUserName, "\r\n");
  790.         }
  791.       } else if ((strncmpi(s, "subject:", 8) == 0) && (!subj)) {
  792.         ss = strtok(s, ": ");
  793.         ss = strtok(NULL, "\r\n");
  794.         trimstr1(ss);
  795.         strncpy(mh.subject, ss, 81);
  796.         mh.subject[72] = 0;
  797.         subj = 1;
  798.       } else if (strncmpi(s, "date:", 5) == 0) {
  799.         ss = strtok(s, ": ");
  800.         ss = strtok(NULL, "\r\n");
  801.         trimstr1(ss);
  802.         strncpy(msgdate, ss, 58);
  803.         msgdate[58] = '\0';
  804.         strcat(msgdate, "\r\n");
  805.       } else if ((strncmpi(s, "to:", 3) == 0) || (strncmpi(s, "cc:", 3) == 0)) {
  806.         ss = strtok(s, ":");
  807.         ss = strtok(NULL, "\r\n");
  808.         strncpy(mh.toUserName, ss, 81);
  809.         mh.toUserName[80] = 0;
  810.         curuser = 0;
  811.         trimstr1(mh.toUserName);
  812.         if ((_fstrchr(mh.toUserName, ' ') != NULL) || (_fstrchr(mh.toUserName, '\"') != NULL) ||
  813.             (_fstrchr(mh.toUserName, '(') != NULL))
  814.           find_name(mh.toUserName);
  815.         else
  816.           mh.toUserName[0] = 0;
  817.         if ((stristr(mh.toUserName, "Multiple recipients of list") != NULL) && (curuser != (unsigned) 65535L)) {
  818.           for (i = 0; (i < nlists) && (nh.main_type == main_type_email); i++) {
  819.             if (stristr(mh.toUserName, maillist[i].opttext) != NULL) {
  820.               if (atoi(maillist[i].subtype)) {
  821.                 nh.main_type = main_type_pre_post;
  822.                 nh.minor_type = atoi(maillist[i].subtype);
  823.               } else {
  824.                 nh.main_type = main_type_new_post;
  825.                 nh.minor_type = 0;
  826.                 strcpy(alphatype, maillist[i].subtype);
  827.               }
  828.               strcpy(alphasubtype, maillist[i].subtype);
  829.               nh.touser = 0;
  830.             }
  831.           }
  832.         }
  833.         if ((curuser == (unsigned) 65535L) && (nh.main_type == main_type_email)) {
  834.           strcpy(alphatype, alphasubtype);
  835.           nh.main_type = main_type_new_post;
  836.           nh.minor_type = 0;
  837.           nh.touser = 0;
  838.           tolist = 1;
  839.         } else
  840.           if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  841.           nh.touser = defuser;
  842.           strcpy(mh.toUserName, postmaster);
  843.         } else {
  844.           nh.touser = curuser;
  845.         }
  846.       } else if (strncmpi(s, "apparently-to:", 14) == 0) {
  847.         ss = strtok(s, ": ");
  848.         ss = strtok(NULL, "\r\n");
  849.         strncpy(mh.toUserName, ss, 81);
  850.         mh.toUserName[80] = 0;
  851.         curuser = 0;
  852.         trimstr1(mh.toUserName);
  853.         if (_fstrstr(mh.toUserName, " "))
  854.           find_name(mh.toUserName);
  855.         else
  856.           mh.toUserName[0] = 0;
  857.         if ((curuser == (unsigned) 65535L) && (nh.main_type == main_type_email)) {
  858.           strcpy(alphatype, alphasubtype);
  859.           nh.main_type = main_type_new_post;
  860.           nh.minor_type = 0;
  861.           nh.touser = 0;
  862.           tolist = 1;
  863.         } else if ((mh.toUserName[0] == 0) || (curuser == 0)) {
  864.           nh.touser = defuser;
  865.           strcpy(mh.toUserName, postmaster);
  866.         } else
  867.           nh.touser = curuser;
  868.       }
  869.     } else
  870.       done = 1;
  871.   }
  872.   if (fp != NULL)
  873.     fclose(fp);
  874.   output("\n ■ From    : %s", strlwr(mh.fromUserName));
  875.   if ((nh.main_type == main_type_pre_post) ||
  876.       (nh.main_type == main_type_new_post))
  877.     output(" ■ Post to : Sub %s", alphasubtype);
  878.   else
  879.     output(" ■ Sent to : %s #%hd", strupr(mh.toUserName), nh.touser);
  880.   output("\n ■ Subject : %s", mh.subject);
  881.   name_packet(pktname);
  882.   if ((fp = fsh_open(pktname, "wb")) == NULL) {
  883.     output("\n ■ Unable to create packet %s", pktname);
  884.     free(p);
  885.     return (1);
  886.   }
  887.   nh.length = textlen + strlen(mh.fromUserName) + strlen(mh.subject)
  888.       + strlen(msgdate) + strlen(recvdate) + 1;
  889.   if (nh.main_type == main_type_new_post)
  890.     nh.length += strlen(alphatype) + 1;
  891.   while (tolist >= 0) {
  892.     fsh_write(&nh, sizeof(net_header_rec), 1, fp);
  893.     if (nh.main_type == main_type_new_post)
  894.       fsh_write(alphatype, sizeof(char), strlen(alphatype) +1, fp);
  895.     fsh_write(mh.subject, sizeof(char), strlen(mh.subject) +1, fp);
  896.     fsh_write(mh.fromUserName, sizeof(char), strlen(mh.fromUserName), fp);
  897.     fsh_write(msgdate, sizeof(char), strlen(msgdate), fp);
  898.     fsh_write(recvdate, sizeof(char), strlen(recvdate), fp);
  899.     reallen = fsh_write(p, sizeof(char), (int) textlen, fp);
  900.     if (reallen != textlen)
  901.       output("\n ■ Expected %ld bytes, wrote %ld bytes.", textlen, reallen);
  902.     nh.tosys = 32767;
  903.     --tolist;
  904.   }
  905.   if (fp != NULL)
  906.     fclose(fp);
  907.   free(p);
  908.   return (0);
  909. }
  910.  
  911.  
  912. char *stripcolors(char *str)
  913. {
  914.   char *obuf, *nbuf;
  915.  
  916.   if (str) {
  917.     for (obuf = str, nbuf = str; *obuf; ++obuf) {
  918.       if (*obuf == 3)
  919.         ++obuf;
  920.       else
  921.         if (((*obuf < 32) && (*obuf != 9)) || (*obuf > 126))
  922.         continue;
  923.       else
  924.         *nbuf++ = *obuf;
  925.     }
  926.     *nbuf = NULL;
  927.   }
  928.   return (str);
  929. }
  930.  
  931. void properize(char *s)
  932. {
  933.   int i;
  934.  
  935.   for (i = 0; i < strlen(s); i++) {
  936.     if ((i == 0) || ((i > 0) && ((s[i - 1] == ' ') || (s[i - 1] == '.') ||
  937.            ((s[i - 1] == 'c') && (s[i - 2] == 'M')) || ((s[i - 1] == 'c') &&
  938.              (s[i - 2] == 'a') && (s[i - 3] == 'M')) || (s[i - 1] == '-') ||
  939.          (s[i - 1] == '_') || ((s[i - 1] == '\'') && (s[i - 2] == 'O'))))) {
  940.       s[i] = toupper(s[i]);
  941.     } else
  942.       s[i] = tolower(s[i]);
  943.   }
  944. }
  945.  
  946.  
  947.  
  948. int export(char *fn)
  949. {
  950.   char fn1[121], tagfn[121], groupname[81], outfn[121], _temp_buffer[256];
  951.   char *ss, *buffer, *text, alphatype[21], hold[21], tempoutfn[21];
  952.   unsigned stype, ttype;
  953.   int infile, outfile, inloc, outloc, term, ok, i, j, ns, i6, tolist;
  954.   net_header_rec nhr;
  955.   struct msghdr mh;
  956.   struct tm *time_msg;
  957.   FILE *fp;
  958.   time_t some;
  959.   char *main_type[] =
  960.   {
  961.     "Network Update", "email by usernum", "post from sub host", "file",
  962.     "post to sub host", "external message", "email by name",
  963.     "NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
  964.     "CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
  965.     "Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info",
  966.     "Unused 1", "Unused 2", "Unused 3", "Unused 4", "Unused 5", "new post",
  967.     "new external"
  968.   };
  969.  
  970.   if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
  971.     return 1;
  972.  
  973.   if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
  974.     sh_close(infile);
  975.     output("\n ■ Out of memory allocating input buffer!");
  976.     return 1;
  977.   }
  978.   if ((text = (char *) malloc(32 * 1024)) == NULL) {
  979.     output("\n ■ Out of memory allocating output buffer!");
  980.     sh_close(infile);
  981.     if (buffer != NULL)
  982.       free(buffer);
  983.     return 1;
  984.   }
  985.   while (sh_read(infile, &nhr, sizeof(nhr))) {
  986.     sh_read(infile, buffer, (int) nhr.length);
  987.     if (nhr.tosys != 32767) {
  988.       output("\n ■ Non-Internet system routing via @32767... aborting export.");
  989.       sh_close(infile);
  990.       if (text)
  991.         free(text);
  992.       if (buffer)
  993.         free(buffer);
  994.       return (1);
  995.     }
  996.     tolist = 0;
  997.     if (nhr.main_type == main_type_pre_post)
  998.       nhr.main_type = main_type_post;
  999.     if ((nhr.main_type == main_type_post) ||
  1000.         (nhr.main_type == main_type_new_post) ||
  1001.         (nhr.main_type == main_type_email_name) ||
  1002.         (nhr.main_type == main_type_ssm)) {
  1003.       inloc = 0;
  1004.       sprintf(hold, "%hu", nhr.minor_type);
  1005.       if ((nhr.main_type == main_type_email_name) ||
  1006.           (nhr.main_type == main_type_ssm) ||
  1007.           (nhr.main_type == main_type_new_post)) {
  1008.         stype = nhr.minor_type;
  1009.         inloc = strlen(buffer) + 1;
  1010.         if ((nhr.main_type == main_type_new_post) || ((nhr.fromsys != net_sysnum) &&
  1011.                                                   (nhr.fromsys != 32767))) {
  1012.           strcpy(alphasubtype, buffer);
  1013.           strcpy(hold, alphasubtype);
  1014.         } else
  1015.           strcpy(mh.toUserName, buffer);
  1016. /*
  1017.         if ((nhr.fromsys != net_sysnum) && (nhr.fromsys != 32767)) {
  1018.           output("\n ■ Gate from #%hd@%hd to %s not yet supported",
  1019.                  nhr.fromuser, nhr.fromsys, mh.toUserName);
  1020.           continue;
  1021.         }
  1022. */
  1023.       } else
  1024.         if (nhr.main_type == main_type_post) {
  1025.         stype = nhr.minor_type;
  1026.       } else {
  1027.         stype = atoi(&buffer[inloc]);
  1028.         sprintf(_temp_buffer, "%u", stype);
  1029.         inloc += strlen(_temp_buffer) + 1;
  1030.       }
  1031.  
  1032.       strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  1033.       stripcolors(mh.subject);
  1034.       inloc += strlen(&buffer[inloc]) + 1;
  1035.  
  1036.       for (term = inloc; buffer[term] != '\r'; term++);
  1037.       buffer[term] = '\0';
  1038.  
  1039.       if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
  1040.         if ((nhr.main_type != main_type_post) &&
  1041.             (nhr.main_type != main_type_new_post)) {
  1042.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  1043.             output("\n ■ No match for user #%hd... skipping message!",
  1044.                    nhr.fromuser);
  1045.             continue;
  1046.           }
  1047.         } else {
  1048.           strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  1049.           stripcolors(mh.fromUserName);
  1050.           strtok(mh.fromUserName, "#");
  1051.           trimstr1(mh.fromUserName);
  1052.         }
  1053.       } else {
  1054.         strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  1055.         stripcolors(mh.fromUserName);
  1056.         strtok(mh.fromUserName, "#");
  1057.         trimstr1(mh.fromUserName);
  1058.         if ((nhr.main_type == main_type_post) &&
  1059.             (nhr.main_type == main_type_new_post)) {
  1060.           sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
  1061.           strcat(mh.fromUserName, _temp_buffer);
  1062.           output("\nFrom: %s\n", mh.fromUserName);
  1063.         }
  1064.       }
  1065.  
  1066.       inloc = term + 2;
  1067.  
  1068.       while (buffer[inloc] != '\n')
  1069.         inloc++;
  1070.       inloc++;
  1071.  
  1072.       if (strnicmp(&buffer[inloc], "RE: ", 4) == 0) {
  1073.         for (term = inloc; buffer[term] != '\r'; term++);
  1074.         buffer[term] = '\0';
  1075.         strncpy(mh.subject, &buffer[inloc + 4], sizeof(mh.subject));
  1076.         if (strnicmp(mh.subject, "RE: ", 4) != 0) {
  1077.           strcpy(_temp_buffer, "Re: ");
  1078.           strcat(_temp_buffer, mh.subject);
  1079.         }
  1080.         strcpy(mh.subject, _temp_buffer);
  1081.         inloc = term + 2;
  1082.       }
  1083.       if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
  1084.           (strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
  1085.         for (term = inloc; buffer[term] != '\r'; term++);
  1086.         buffer[term] = '\0';
  1087.         strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
  1088.         stripcolors(mh.toUserName);
  1089.         if (strcspn(mh.toUserName, "<") != strlen(mh.toUserName)) {
  1090.           if ((_fstrstr(mh.toUserName, "\"") == 0) && (_fstrstr(mh.toUserName, "(") == 0)) {
  1091.             ss = strtok(mh.toUserName, "<");
  1092.             ss = strtok(NULL, ">");
  1093.             strcpy(mh.toUserName, ss);
  1094.           }
  1095.         }
  1096.         inloc = term + 2;
  1097.       } else {
  1098.         if (nhr.main_type != main_type_email_name) {
  1099.           strcpy(mh.toUserName, "ALL");
  1100.         }
  1101.       }
  1102.       outloc = 0;
  1103.       do {
  1104.         if (buffer[inloc] == 2) {
  1105.           i = inloc + 1;
  1106.           for (j = 1; j < 255; j++) {
  1107.             if (buffer[i] == 'π')
  1108.               buffer[i] = '\r';
  1109.             if ((buffer[i] == '\r') || (i > nhr.length))
  1110.               break;
  1111.             i++;
  1112.           }
  1113.           if (j < 80) {
  1114.             i = (80 - j) / 2;
  1115.             for (j = 1; j <= i; j++)
  1116.               text[outloc++] = ' ';
  1117.           }
  1118.           inloc++;
  1119.         } else
  1120.           if (buffer[inloc] == 3)
  1121.           inloc += 2;
  1122.         else
  1123.           if ((buffer[inloc] == 124) && (isdigit(buffer[inloc + 1])) && (isdigit(buffer[inloc + 2])))
  1124.           inloc += 3;
  1125.         else
  1126.           if ((buffer[inloc] == 4) && (isdigit(buffer[inloc + 1]))) {
  1127.           i = inloc;
  1128.           for (j = 1; j < 255; j++) {
  1129.             if ((buffer[i] == '\r') || (i > nhr.length))
  1130.               break;
  1131.             i++;
  1132.             inloc++;
  1133.           }
  1134.           inloc++;
  1135.           if (buffer[inloc] == '\n')
  1136.             inloc++;
  1137.         } else
  1138.           if (buffer[inloc] >= 127)
  1139.           inloc++;
  1140.         else
  1141.           if (buffer[inloc] == 1)
  1142.           inloc++;
  1143.         else
  1144.           text[outloc++] = buffer[inloc++];
  1145.       } while (inloc < nhr.length);
  1146.  
  1147.       text[outloc] = '\0';
  1148.  
  1149.       if ((nhr.main_type == main_type_post) ||
  1150.           (nhr.main_type == main_type_pre_post) ||
  1151.           (nhr.main_type == main_type_new_post)) {
  1152.         if (nhr.main_type == main_type_new_post) {
  1153.           sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
  1154.           if (exist(fn1)) {
  1155.             tolist = 1;
  1156.             nhr.main_type = main_type_email_name;
  1157.           }
  1158.         }
  1159.         for (i = 0; (i < nlists) && (nhr.main_type != main_type_email_name); i++) {
  1160.           if (nhr.main_type == main_type_new_post) {
  1161.             if (strcmpi(alphasubtype, maillist[i].subtype) == 0) {
  1162.               nhr.main_type = main_type_email_name;
  1163.               strcpy(mh.toUserName, maillist[i].ownername);
  1164.             }
  1165.           } else {
  1166.             ttype = atoi(maillist[i].subtype);
  1167.             if (ttype == stype) {
  1168.               nhr.main_type = main_type_email_name;
  1169.               strcpy(mh.toUserName, maillist[i].ownername);
  1170.             }
  1171.           }
  1172.         }
  1173.       }
  1174.       switch (nhr.main_type) {
  1175.         case main_type_email:
  1176.         case main_type_email_name:
  1177.         case main_type_ssm:
  1178.           i = 1;
  1179.           sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
  1180.           while (exist(outfn))
  1181.             sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  1182.           break;
  1183.         case main_type_new_post:
  1184.         case main_type_post:
  1185.         case main_type_pre_post:
  1186.           i = 1;
  1187.           strcpy(tempoutfn, hold);
  1188.           sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, i);
  1189.           while (exist(outfn))
  1190.             sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, ++i);
  1191.           break;
  1192.         default:
  1193.           continue;
  1194.       }
  1195.  
  1196.       properize(mh.fromUserName);
  1197.       output("\n ■ Creating: %s", outfn);
  1198.       if (nhr.fromsys != net_sysnum)
  1199.         output("\n ■ From    : %s", mh.fromUserName);
  1200.       else
  1201.         if (usermail)
  1202.         output("\n ■ From    : \"%s\" <%s@%s>", mh.fromUserName, POPNAME, DOMAIN);
  1203.       else
  1204.         output("\n ■ From    : <%s@%s>", POPNAME, DOMAIN);
  1205.       if ((nhr.main_type == main_type_post) ||
  1206.           (nhr.main_type == main_type_pre_post) ||
  1207.           (nhr.main_type == main_type_new_post)) {
  1208.         sprintf(fn1, "%sNEWS.RC", net_data);
  1209.         if ((fp = fsh_open(fn1, "rt")) == NULL) {
  1210.           output("\n ■ %s not found!", fn1);
  1211.           sh_close(infile);
  1212.           if (text)
  1213.             free(text);
  1214.           if (buffer)
  1215.             free(buffer);
  1216.           return 1;
  1217.         } else {
  1218.           ok = 0;
  1219.           while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
  1220.             groupname[0] = 0;
  1221.             ss = strtok(_temp_buffer, " ");
  1222.             if (ss) {
  1223.               strcpy(groupname, ss);
  1224.               ss = strtok(NULL, " ");
  1225.               ss = strtok(NULL, "\r");
  1226.               if (nhr.main_type == main_type_new_post) {
  1227.                 strcpy(alphatype, ss);
  1228.                 if (strncmpi(alphasubtype, alphatype, strlen(alphasubtype)) == 0)
  1229.                   ok = 1;
  1230.               } else {
  1231.                 ttype = atoi(ss);
  1232.                 if (ttype == stype)
  1233.                   ok = 1;
  1234.               }
  1235.             }
  1236.           }
  1237.           *ss = NULL;
  1238.           if (fp != NULL)
  1239.             fclose(fp);
  1240.           if (!ok) {
  1241.             if (nhr.main_type != main_type_new_post)
  1242.               sprintf(alphatype, "%u", stype);
  1243.             output("\n ■ Subtype %s not found in NEWS.RC!", alphatype);
  1244.             sh_close(infile);
  1245.             if (text)
  1246.               free(text);
  1247.             if (buffer)
  1248.               free(buffer);
  1249.             return 1;
  1250.           }
  1251.         }
  1252.       }
  1253.       if ((nhr.main_type == main_type_email) ||
  1254.           (nhr.main_type == main_type_email_name) ||
  1255.           (nhr.main_type == main_type_ssm)) {
  1256.         if (tolist)
  1257.           output("\n ■ Sent to : %s Mailing List", alphasubtype);
  1258.         else
  1259.           output("\n ■ Rcpt to : %s", mh.toUserName);
  1260.       } else
  1261.         output("\n ■ Post to : %s", groupname);
  1262.  
  1263.       strcpy(_temp_buffer, mh.subject);
  1264.       j = 0;
  1265.       for (i = 0; i < strlen(mh.subject); i++) {
  1266.         if (_temp_buffer[i] == 3)
  1267.           ++i;
  1268.         else
  1269.           mh.subject[j++] = _temp_buffer[i];
  1270.       }
  1271.       mh.subject[j] = '\0';
  1272.  
  1273.       output("\n ■ Subject : %s", mh.subject);
  1274.  
  1275.       outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
  1276.       if (outfile == -1) {
  1277.         sh_close(infile);
  1278.         if (buffer)
  1279.           free(buffer);
  1280.         if (text)
  1281.           free(text);
  1282.         output("\n ■ Unable to open output file %s", outfn);
  1283.         return 1;
  1284.       }
  1285.       time(&some);
  1286.       time_msg = localtime(&some);
  1287.       strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
  1288.       if (nhr.fromsys == 32767) {
  1289.         sprintf(_temp_buffer, "From: %s\n", mh.fromUserName);
  1290.       } else if (nhr.fromsys != net_sysnum) {
  1291.         sprintf(_temp_buffer, "From: \"%s #%hd-%hu\" <%s@%s>\n", mh.fromUserName,
  1292.                 nhr.fromuser, nhr.fromsys, POPNAME, DOMAIN);
  1293.       } else {
  1294.           if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)))
  1295.             if (spamname[0] == 0)
  1296.               sprintf(_temp_buffer, "From: \"%s\" <%s@dont.spam.me.%s>\n",
  1297.                       mh.fromUserName, POPNAME, DOMAIN);
  1298.             else
  1299.               sprintf(_temp_buffer, "From: \"%s\" <%s>\n", mh.fromUserName, spamname);
  1300.           else {
  1301.             if (usermail)
  1302.               sprintf(_temp_buffer, "From: \"%s\" <%s@%s>\n",
  1303.                       mh.fromUserName, POPNAME, DOMAIN);
  1304.             else
  1305.               sprintf(_temp_buffer, "From: <%s@%s>\n", POPNAME, DOMAIN);
  1306.           }
  1307.       }
  1308.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1309.       ++cur_daten;
  1310.       sprintf(_temp_buffer, "Message-ID: <%lx-%s@WWIV-BBS>\n",
  1311.               cur_daten, POPNAME);
  1312.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1313.       if ((nhr.main_type == main_type_email) ||
  1314.           (nhr.main_type == main_type_email_name) ||
  1315.           (nhr.main_type == main_type_ssm)) {
  1316.         if (!tolist) {
  1317.           sprintf(_temp_buffer, "To: %s\n", mh.toUserName);
  1318.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1319.         } else {
  1320.           sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
  1321.           i = 0;
  1322.           if ((fp = fsh_open(fn1, "rt")) != NULL) {
  1323.             while (fgets(_temp_buffer, 80, fp) != NULL) {
  1324.               if (_fstrstr(_temp_buffer, "@")) {
  1325.                 strcpy(mh.toUserName, _temp_buffer);
  1326.                 sprintf(_temp_buffer, "To: %s", mh.toUserName);
  1327.                 if (_temp_buffer[strlen(_temp_buffer) - 1] != '\n')
  1328.                   strcat(_temp_buffer, "\n");
  1329.                 sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1330.                 i = 1;
  1331.               }
  1332.             }
  1333.             if (fp != NULL)
  1334.               fclose(fp);
  1335.           }
  1336.           if (!i) {
  1337.             sh_close(infile);
  1338.             if (buffer)
  1339.               free(buffer);
  1340.             if (text)
  1341.               free(text);
  1342.             output("\n ■ Error processing mailing list %s.", alphasubtype);
  1343.             return (1);
  1344.           } else {
  1345.             sprintf(_temp_buffer, "Reply-To: \"%s\" <%s@%s>\n", alphasubtype,
  1346.                     POPNAME, DOMAIN);
  1347.             sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1348.           }
  1349.         }
  1350.       } else {
  1351.         sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
  1352.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1353.       }
  1354.       sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
  1355.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1356.       sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
  1357.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1358.       if (nhr.main_type != main_type_email_name) {
  1359.         sprintf(_temp_buffer, "Path: %s!%s\n", POPNAME, DOMAIN);
  1360.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1361.         sprintf(_temp_buffer, "Organization: %s * %s\n",
  1362.                 syscfg.systemname, syscfg.systemphone);
  1363.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1364.       }
  1365.       if (tolist) {
  1366.         sprintf(_temp_buffer, "X-Reply-To: \"%s\" <%s@%s>\n",
  1367.                 alphasubtype, POPNAME, DOMAIN);
  1368.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1369.       } else
  1370.         if ((!spam) && ((nhr.main_type != main_type_post) || (nhr.main_type != main_type_new_post))) {
  1371.         sprintf(_temp_buffer, "Reply-To: \"%s\" <%s@%s>\n",
  1372.                 mh.fromUserName, POPNAME, DOMAIN);
  1373.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1374.         }
  1375.  
  1376.       if (tolist) {
  1377.         sprintf(_temp_buffer, "Source: %s Mail List\n", alphasubtype);
  1378.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1379.       }
  1380.       if (((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)) && (!tolist)) {
  1381.         if (*REPLYTO)
  1382.           sprintf(_temp_buffer, "Reply-to: \"%s\" <%s>\n", mh.fromUserName, REPLYTO);
  1383.         else
  1384.           sprintf(_temp_buffer, "Reply-to: \"%s\" <%s@%s>\n",
  1385.                   mh.fromUserName, POPNAME, DOMAIN);
  1386.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1387.       }
  1388.       sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n", VERSION);
  1389.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1390.  
  1391.       if ((nhr.main_type != main_type_email) &&
  1392.           (nhr.main_type != main_type_email_name) &&
  1393.           (strncmp(mh.toUserName, "ALL", 3) != 0)) {
  1394.         sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
  1395.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1396.       }
  1397.       for (i = 0; i < strlen(text); i++)
  1398.         if (text[i] == 'π')
  1399.           text[i] = '\n';
  1400.  
  1401.       sh_write(outfile, text, strlen(text));
  1402.       sprintf(tagfn, "%sI%u.TAG", syscfg.datadir, stype);
  1403.       if (exist(tagfn))
  1404.         strcpy(tagfile, tagfn);
  1405.       tagfn[0] = 0;
  1406.       ns = 0;
  1407.       for (i6 = 0; i6 < 99; i6++) {
  1408.         sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, i6);
  1409.         if (exist(tagfn))
  1410.           ns++;
  1411.         else
  1412.           break;
  1413.       }
  1414.       sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, random(ns));
  1415.       if (exist(tagfn))
  1416.         strcpy(tagfile, tagfn);
  1417.       if (tagfile[0] == 0) {
  1418.         sprintf(_temp_buffer, "\n\nOrigin: %s * %s\n\n",
  1419.                 syscfg.systemname, syscfg.systemphone);
  1420.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1421.       } else {
  1422.         if ((fp = fsh_open(tagfile, "rt")) == NULL)
  1423.           output("\n ■ Error reading %s.", tagfile);
  1424.         else {
  1425.           sprintf(_temp_buffer, "\n\n");
  1426.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1427.           while (fgets(_temp_buffer, 120, fp)) {
  1428.             for (i = 0; ((i < strlen(_temp_buffer)) &&
  1429.                (_temp_buffer[i] != '\r') && (_temp_buffer[i] != '\n')); i++)
  1430.               if ((_temp_buffer[i] < 32) || (_temp_buffer[i] > 126))
  1431.                 _temp_buffer[i] = 32;
  1432.             sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1433.           }
  1434.           if (fp != NULL)
  1435.             fclose(fp);
  1436.           sprintf(_temp_buffer, "\n\n");
  1437.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1438.         }
  1439.       }
  1440.       sh_close(outfile);
  1441.     } else {
  1442.       if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x1b))
  1443.         output("\n ■ %s message skipped",
  1444.                main_type[nhr.main_type - 1]);
  1445.       else
  1446.         output("\n ■ Unknown Main_type %hd skipped", nhr.main_type);
  1447.     }
  1448.   }
  1449.   sh_close(infile);
  1450.   unlink(fn);
  1451.   if (text)
  1452.     free(text);
  1453.   if (buffer)
  1454.     free(buffer);
  1455.   return (0);
  1456. }
  1457.  
  1458. void get_dir(char *s, int be)
  1459. {
  1460.   strcpy(s, "X:\\");
  1461.   s[0] = 'A' + getdisk();
  1462.   getcurdir(0, s + 3);
  1463.   if (be) {
  1464.     if (s[strlen(s) - 1] != '\\')
  1465.       strcat(s, "\\");
  1466.   }
  1467. }
  1468.  
  1469. /*
  1470.   action: 1 = unsubscribed,
  1471.           2 = subscribed,
  1472.           3 = already subscribed,
  1473.           4 = not subscribed
  1474.           5 = invalid list
  1475. */
  1476.  
  1477. void send_note(int action, char *mailname, char *listname)
  1478. {
  1479.   char s[81], fn[MAXPATH];
  1480.   int i;
  1481.   FILE *fp;
  1482.  
  1483.   i = 1;
  1484.   sprintf(fn, "%sMQUEUE\\MSG.%d", net_data, i);
  1485.   while (exist(fn))
  1486.     sprintf(fn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  1487.  
  1488.   if ((fp = fsh_open(fn, "wt+")) == NULL) {
  1489.     output("\n ! Unable to create %s.", fn);
  1490.     return;
  1491.   }
  1492.  
  1493.   if (action != 5)
  1494.     fprintf(fp, "From: \"%s\" <%s@%s>\n", listname, POPNAME, DOMAIN);
  1495.   else
  1496.     fprintf(fp, "From: <%s@%s>\n", POPNAME, DOMAIN);
  1497.   fprintf(fp, "To: %s\n", mailname);
  1498.   fprintf(fp, "Subject: %s Mailing List\n\n", listname);
  1499.  
  1500.   switch (action) {
  1501.     case 1:
  1502.       fprintf(fp, "%s has been unsubscribed from mailing list:\n\n   %s\n\n",
  1503.           mailname, listname);
  1504.       break;
  1505.     case 2:
  1506.       fprintf(fp, "%s has been added to mailing list:\n\n   %s\n\n", mailname,
  1507.           listname);
  1508.       break;
  1509.     case 3:
  1510.       fprintf(fp, "%s was already subscribed to mailing list:\n\n   %s\n\n",
  1511.           mailname, listname);
  1512.       break;
  1513.     case 4:
  1514.       fprintf(fp, "%s was not subscribed to mailing list:\n\n   %s\n\n",
  1515.           mailname, listname);
  1516.       break;
  1517.     case 5:
  1518.       fprintf(fp, "%s has requested an invalid mailing list:\n\n   %s\n\n",
  1519.           mailname, listname);
  1520.       break;
  1521.   }
  1522.   if (fp != NULL)
  1523.     fclose(fp);
  1524. }
  1525.  
  1526. int copyfile(char *input, char *output)
  1527. {
  1528.   int f1, f2, i;
  1529.   char *b;
  1530.   struct ftime ft;
  1531.  
  1532.   if ((strcmp(input, output) != 0) && (exist(input)) && (!exist(output))) {
  1533.     if ((b = (char *) malloc(16400)) == NULL)
  1534.       return 0;
  1535.     f1 = sh_open1(input, O_RDONLY | O_BINARY);
  1536.     if (!f1) {
  1537.       free(b);
  1538.       b = NULL;
  1539.       return 0;
  1540.     }
  1541.     getftime(f1, &ft);
  1542.  
  1543.     f2 = sh_open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  1544.     if (!f2) {
  1545.       free(b);
  1546.       b = NULL;
  1547.       f1 = sh_close(f1);
  1548.       return 0;
  1549.     }
  1550.     i = read(f1, (void *) b, 16384);
  1551.     while (i > 0) {
  1552.       sh_write(f2, (void *) b, i);
  1553.       i = read(f1, (void *) b, 16384);
  1554.     }
  1555.     f1 = sh_close(f1);
  1556.     setftime(f2, &ft);
  1557.     f2 = sh_close(f2);
  1558.     free(b);
  1559.     b = NULL;
  1560.   }
  1561.   return 1;
  1562. }
  1563.  
  1564.  
  1565. int subscribe(char *fn)
  1566. {
  1567.   char *ss, s[81], s1[81], mailname[81], subtype[81];
  1568.   int i, done, found, unsubscribe;
  1569.   FILE *fp, *oldfp, *newfp;
  1570.  
  1571.   if ((fp = fsh_open(fn, "rt")) == NULL) {
  1572.     output("\n ! Unable to open %s.", fn);
  1573.     return 1;
  1574.   }
  1575.   done = unsubscribe = 0;
  1576.   while ((fgets(s1, 80, fp)) && (!done)) {
  1577.     strcpy(s, &(s1[3]));
  1578.     if (strnicmp(s, "from:", 5) == 0) {
  1579.       ss = strtok(s, ":");
  1580.       if (ss) {
  1581.         ss = strtok(NULL, "\r\n");
  1582.         trimstr1(ss);
  1583.         strcpy(mailname, ss);
  1584.       }
  1585.     }
  1586.     if (strnicmp(s, "subject:", 8) == 0) {
  1587.       done = 1;
  1588.       ss = strtok(s, ":");
  1589.       if (ss) {
  1590.         ss = strtok(NULL, "\r\n");
  1591.         trimstr1(ss);
  1592.         strcpy(s1, ss);
  1593.         if (strnicmp(s1, "subscribe", 9) == 0) {
  1594.           ss = strtok(s1, " ");
  1595.           if (ss) {
  1596.             ss = strtok(NULL, "\r\n");
  1597.             trimstr1(ss);
  1598.             strcpy(subtype, ss);
  1599.           }
  1600.         }
  1601.         if (strnicmp(s1, "unsubscribe", 11) == 0) {
  1602.           unsubscribe = 1;
  1603.           ss = strtok(s1, " ");
  1604.           if (ss) {
  1605.             ss = strtok(NULL, "\r\n");
  1606.             trimstr1(ss);
  1607.             strcpy(subtype, ss);
  1608.           }
  1609.         }
  1610.       }
  1611.       ss = NULL;
  1612.     }
  1613.   }
  1614.   if (fp != NULL)
  1615.     fclose(fp);
  1616.   if ((!*mailname) || (!*subtype)) {
  1617.     output("\n ! Invalid subscription request %s.", fn);
  1618.     return 1;
  1619.   }
  1620.   if (strlen(subtype) == 1) {
  1621.     output("\n ! %s attempted to write to M%s.NET!", mailname, subtype);
  1622.     return 1;
  1623.   }
  1624.   sprintf(s, "%sM%s.NET", net_data, subtype);
  1625.   if (!exist(s)) {
  1626.     output("\n ! %s subscriber list not found.", s);
  1627.     send_note(5, mailname, subtype);
  1628.     return 1;
  1629.   }
  1630.   if ((oldfp = fsh_open(s, "rt")) == NULL) {
  1631.     output("\n ! Unable to open input file %s.", s);
  1632.     return 1;
  1633.   }
  1634.   sprintf(s1, "%sM%s.TMP", net_data, subtype);
  1635.   if (exist(s1))
  1636.     unlink(s1);
  1637.   if ((newfp = fsh_open(s1, "wt+")) == NULL) {
  1638.     output("\n ! Unable to open output file %s.", s1);
  1639.     return 1;
  1640.   }
  1641.   found = 0;
  1642.   while (fgets(s, 80, oldfp)) {
  1643.     trimstr1(s);
  1644.     if (unsubscribe) {
  1645.       if (stricmp(s, mailname) == 0) {
  1646.         output("\n ■ Removing %s from %s mailing list.", mailname, subtype);
  1647.         send_note(2, mailname, subtype);
  1648.         found = 1;
  1649.       } else
  1650.         fprintf(newfp, s);
  1651.     } else {
  1652.       if (stricmp(s, mailname) == 0) {
  1653.         output("\n ■ %s already in %s mailing list.", mailname, subtype);
  1654.         send_note(3, mailname, subtype);
  1655.         found = 1;
  1656.       }
  1657.       fprintf(newfp, s);
  1658.     }
  1659.   }
  1660.   if (!found) {
  1661.     if (unsubscribe)
  1662.       send_note(4, mailname, subtype);
  1663.     else {
  1664.       output("\n ■ Adding %s to %s mailing list.", mailname, subtype);
  1665.       fprintf(newfp, "\n%s\n", mailname);
  1666.       send_note(2, mailname, subtype);
  1667.     }
  1668.   }
  1669.   if (oldfp != NULL)
  1670.     fclose(oldfp);
  1671.   if (newfp != NULL)
  1672.     fclose(newfp);
  1673.   sprintf(s, "%sM%s.TMP", net_data, subtype);
  1674.   sprintf(s1, "%sM%s.NET", net_data, subtype);
  1675.   if (exist(s1))
  1676.     unlink(s1);
  1677.   copyfile(s, s1);
  1678.   return 0;
  1679. }
  1680.  
  1681. int main(int argc, char *argv[])
  1682. {
  1683.   char fn[MAXPATH], *ss;
  1684.   int f, f1, i;
  1685.   struct ffblk ff;
  1686.   struct date dt;
  1687.   struct time tm;
  1688.  
  1689.   output("\n ■ PPP Import/Export %s", VERSION);
  1690.   if (argc != 7) {
  1691.     output("\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
  1692.     if (argc > 1) {
  1693.       output("Command line was: ");
  1694.       for (i = 0; i < argc; i++)
  1695.         output("%s ", argv[i]);
  1696.       output("\n\n");
  1697.     }
  1698.     return (1);
  1699.   }
  1700.   f = sh_open1("CONFIG.DAT", O_RDONLY | O_BINARY);
  1701.   if (f < 0) {
  1702.     output("Could not open CONFIG.DAT!\n\n");
  1703.     return 1;
  1704.   }
  1705.   sh_read(f, (void *) &syscfg, sizeof(configrec));
  1706.   sh_close(f);
  1707.  
  1708.   detect_multitask();
  1709.  
  1710.   get_dir(maindir, 1);
  1711.   strcpy(net_data, argv[2]);
  1712.   sprintf(fn, "%s%s", net_data, argv[1]);
  1713.   strcpy(net_name, argv[6]);
  1714.   strcpy(POPNAME, argv[4]);
  1715.   strcpy(DOMAIN, argv[5]);
  1716.   net_sysnum = atoi(argv[3]);
  1717.   tagfile[0] = 0;
  1718.   spam = 0;
  1719.  
  1720.   ss = getenv("WWIV_INSTANCE");
  1721.   if (ss) {
  1722.     instance = atoi(ss);
  1723.     if (instance >= 1000) {
  1724.       output("\n ■ WWIV_INSTANCE set to %hd.  Can only be 1..999!",
  1725.              instance);
  1726.       instance = 1;
  1727.     }
  1728.   } else
  1729.     instance = 1;
  1730.  
  1731.   parse_net_ini();
  1732.   gettime(&tm);
  1733.   getdate(&dt);
  1734.   cur_daten = dostounix(&dt, &tm);
  1735.  
  1736.   strupr(postmaster);
  1737.   export(fn);
  1738.  
  1739.   sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
  1740.   f1 = findfirst(fn, &ff, 0);
  1741.   while (f1 == 0) {
  1742.     sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1743.     if (!import(fn)) {
  1744.       unlink(fn);
  1745.     }
  1746.     f1 = findnext(&ff);
  1747.   }
  1748.  
  1749.   sprintf(fn, "%sINBOUND\\SUB*.*", net_data);
  1750.   f1 = findfirst(fn, &ff, 0);
  1751.   while (f1 == 0) {
  1752.     sprintf(fn, "%sINBOUND\\%s", net_data, ff.ff_name);
  1753.     output("\n ■ Processing subscriber request %s...", ff.ff_name);
  1754.     subscribe(fn);
  1755.     unlink(fn);
  1756.     f1 = findnext(&ff);
  1757.   }
  1758.  
  1759.   if (maillist != NULL)
  1760.     free(maillist);
  1761.  
  1762.   return 0;
  1763. }
  1764.