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