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