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