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