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