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