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