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

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