home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B39.ZIP / EXP.C < prev    next >
Text File  |  1997-09-05  |  40KB  |  1,400 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.   unsigned long basetime;
  836.   int infile, outfile, inloc, outloc, term, ok, i, j, ns, i6, tolist;
  837.   net_header_rec nhr;
  838.   struct msghdr mh;
  839.   struct tm *time_msg;
  840.   FILE *fp;
  841.   time_t some;
  842.   char *main_type[] =
  843.   {
  844.     "Network Update", "email by usernum", "post from sub host", "file",
  845.     "post to sub host", "external message", "email by name",
  846.     "NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
  847.     "CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
  848.     "Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info",
  849.     "Unused 1", "Unused 2", "Unused 3", "Unused 4", "Unused 5", "new post",
  850.     "new external"
  851.   };
  852.  
  853.   if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
  854.     return (1);
  855.  
  856.   if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
  857.     sh_close(infile);
  858.     output("\n ■ Out of memory allocating input buffer!");
  859.     return (1);
  860.   }
  861.   if ((text = (char *) malloc(32 * 1024)) == NULL) {
  862.     output("\n ■ Out of memory allocating output buffer!");
  863.     sh_close(infile);
  864.     if (buffer)
  865.       free(buffer);
  866.     return (1);
  867.   }
  868.   while (sh_read(infile, &nhr, sizeof(nhr))) {
  869.     sh_read(infile, buffer, (int) nhr.length);
  870.     if (nhr.tosys != 32767) {
  871.       output("\n ■ Non-Internet system routing via @32767... aborting export.");
  872.       sh_close(infile);
  873.       if (text)
  874.         free(text);
  875.       if (buffer)
  876.         free(buffer);
  877.       return (1);
  878.     }
  879.     tolist = 0;
  880.     if (nhr.main_type == main_type_pre_post)
  881.       nhr.main_type = main_type_post;
  882.     if ((nhr.main_type == main_type_post) ||
  883.         (nhr.main_type == main_type_new_post) ||
  884.         (nhr.main_type == main_type_email_name) ||
  885.         (nhr.main_type == main_type_ssm)) {
  886.       inloc = 0;
  887.       sprintf(hold, "%hu", nhr.minor_type);
  888.       if ((nhr.main_type == main_type_email_name) ||
  889.           (nhr.main_type == main_type_ssm) ||
  890.           (nhr.main_type == main_type_new_post)) {
  891.         stype = nhr.minor_type;
  892.         inloc = strlen(buffer) + 1;
  893.         if (nhr.main_type == main_type_new_post) {
  894.           strcpy(alphasubtype, buffer);
  895.           strcpy(hold, alphasubtype);
  896.         } else
  897.           strcpy(mh.toUserName, buffer);
  898.         if ((nhr.fromsys != net_sysnum) && (nhr.fromsys != 32767)) {
  899.           output("\n ■ Gate from #%hd@%hd to %s not yet supported",
  900.                   nhr.fromuser, nhr.fromsys, mh.toUserName);
  901.           continue;
  902.         }
  903.       } else
  904.         if (nhr.main_type == main_type_post)
  905.         stype = nhr.minor_type;
  906.       else {
  907.         stype = atoi(&buffer[inloc]);
  908.         sprintf(_temp_buffer, "%u", stype);
  909.         inloc += strlen(_temp_buffer) + 1;
  910.       }
  911.       strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
  912.       stripcolors(mh.subject);
  913.       inloc += strlen(&buffer[inloc]) + 1;
  914.  
  915.       for (term = inloc; buffer[term] != '\r'; term++);
  916.       buffer[term] = '\0';
  917.  
  918.       if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
  919.         if ((nhr.main_type != main_type_post) &&
  920.             (nhr.main_type != main_type_new_post)) {
  921.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  922.             output("\n ■ No match for user #%hd... skipping message!",
  923.                     nhr.fromuser);
  924.             continue;
  925.           }
  926.         } else {
  927.           if (!num_to_name(mh.fromUserName, nhr.fromuser, use_alias)) {
  928.             output("\n ■ No match for user #%hd... skipping message!",
  929.                     nhr.fromuser);
  930.             continue;
  931.           }
  932.         }
  933.       } else {
  934.         strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
  935.         stripcolors(mh.fromUserName);
  936.         strtok(mh.fromUserName, "#");
  937.         if ((nhr.main_type == main_type_post) &&
  938.             (nhr.main_type == main_type_new_post) &&
  939.             (nhr.fromsys != net_sysnum)) {
  940.           sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
  941.           strcat(mh.fromUserName, _temp_buffer);
  942.         }
  943.       }
  944.  
  945.       inloc = term + 2;
  946.  
  947.       while (buffer[inloc] != '\n')
  948.         inloc++;
  949.       inloc++;
  950.  
  951.       if (strnicmp(&buffer[inloc], "RE: ", 4) == 0) {
  952.         for (term = inloc; buffer[term] != '\r'; term++);
  953.         buffer[term] = '\0';
  954.         strncpy(mh.subject, &buffer[inloc + 4], sizeof(mh.subject));
  955.         if (strnicmp(mh.subject, "RE: ", 4) != 0) {
  956.           strcpy(_temp_buffer, "Re: ");
  957.           strcat(_temp_buffer, mh.subject);
  958.         }
  959.         strcpy(mh.subject, _temp_buffer);
  960.         inloc = term + 2;
  961.       }
  962.       if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
  963.           (strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
  964.         for (term = inloc; buffer[term] != '\r'; term++);
  965.         buffer[term] = '\0';
  966.         strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
  967.         stripcolors(mh.toUserName);
  968.         if (strcspn(mh.toUserName, "<") != strlen(mh.toUserName)) {
  969.           if ((strstr(mh.toUserName, "\"") == 0) && (strstr(mh.toUserName, "(") == 0)) {
  970.             ss = strtok(mh.toUserName, "<");
  971.             ss = strtok(NULL, ">");
  972.             strcpy(mh.toUserName, ss);
  973.           }
  974.         }
  975.         inloc = term + 2;
  976.       } else {
  977.         if (nhr.main_type != main_type_email_name) {
  978.           strcpy(mh.toUserName, "ALL");
  979.         }
  980.       }
  981.       outloc = 0;
  982.       do {
  983.         if (buffer[inloc] == 2) {
  984.           i = inloc + 1;
  985.           for (j = 1; j < 80; j++) {
  986.             if ((buffer[i] == '\r') || (i > nhr.length))
  987.               break;
  988.             i++;
  989.           }
  990.           if (j < 80) {
  991.             i = (80 - j) / 2;
  992.             for (j = 1; j <= i; j++)
  993.               text[outloc++] = ' ';
  994.           }
  995.           inloc++;
  996.         } else if (buffer[inloc] == 3) {
  997.           inloc += 2;
  998.         } else if (buffer[inloc] == '|') {
  999.           if ((isdigit(buffer[inloc+1])) && (isdigit(buffer[inloc+2])))
  1000.             inloc += 3;
  1001.         } else if ((buffer[inloc] == 4) && ((buffer[inloc + 1] >= 48) && (buffer[inloc + 1] <= 57))) {
  1002.           while ((inloc < nhr.length) && (buffer[inloc] != '\r'))
  1003.             ++inloc;
  1004.           ++inloc;
  1005.           if (buffer[inloc] == '\n')
  1006.             ++inloc;
  1007.         } else if (buffer[inloc] >= 127) {
  1008.           inloc++;
  1009.         } else if (buffer[inloc] == 1) {
  1010.           inloc++;
  1011.         } else {
  1012.           text[outloc++] = buffer[inloc++];
  1013.         }
  1014.       } while (inloc < nhr.length);
  1015.  
  1016.       text[outloc] = '\0';
  1017.  
  1018.       if ((nhr.main_type == main_type_post) ||
  1019.           (nhr.main_type == main_type_pre_post) ||
  1020.           (nhr.main_type == main_type_new_post)) {
  1021.         if (nhr.main_type == main_type_new_post) {
  1022.           sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
  1023.           if (exist(fn1)) {
  1024.             tolist = 1;
  1025.             nhr.main_type = main_type_email_name;
  1026.           }
  1027.         }
  1028.         for (i = 0; (i < nlists) && (nhr.main_type != main_type_email_name); i++) {
  1029.           if (nhr.main_type == main_type_new_post) {
  1030.             if (strcmpi(alphasubtype, maillist[i].subtype) == 0) {
  1031.               nhr.main_type = main_type_email_name;
  1032.               strcpy(mh.toUserName, maillist[i].ownername);
  1033.             }
  1034.           } else {
  1035.             ttype = atoi(maillist[i].subtype);
  1036.             if (ttype == stype) {
  1037.               nhr.main_type = main_type_email_name;
  1038.               strcpy(mh.toUserName, maillist[i].ownername);
  1039.             }
  1040.           }
  1041.         }
  1042.       }
  1043.       switch (nhr.main_type) {
  1044.         case main_type_email:
  1045.         case main_type_email_name:
  1046.         case main_type_ssm:
  1047.           i = 1;
  1048.           sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
  1049.           while (exist(outfn))
  1050.             sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
  1051.           break;
  1052.         case main_type_new_post:
  1053.         case main_type_post:
  1054.         case main_type_pre_post:
  1055.           i = 1;
  1056.           strcpy(tempoutfn, hold);
  1057.           sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, i);
  1058.           while (exist(outfn))
  1059.             sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, ++i);
  1060.           break;
  1061.         default:
  1062.           continue;
  1063.       }
  1064.  
  1065.       output("\n ■ Creating: %s", outfn);
  1066.       output("\n ■ From    : %s", mh.fromUserName);
  1067.       if ((nhr.main_type == main_type_post) ||
  1068.           (nhr.main_type == main_type_pre_post) ||
  1069.           (nhr.main_type == main_type_new_post)) {
  1070.         sprintf(fn1, "%sNEWS.RC", net_data);
  1071.         if ((fp = fsh_open(fn1, "rt")) == NULL) {
  1072.           output("\n ■ %s not found!", fn1);
  1073.           sh_close(infile);
  1074.           if (text)
  1075.             free(text);
  1076.           if (buffer)
  1077.             free(buffer);
  1078.           return 1;
  1079.         } else {
  1080.           ok = 0;
  1081.           while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
  1082.             groupname[0] = 0;
  1083.             ss = strtok(_temp_buffer, " ");
  1084.             if (ss) {
  1085.               strcpy(groupname, ss);
  1086.               ss = strtok(NULL, " ");
  1087.               ss = strtok(NULL, "\r");
  1088.               if (nhr.main_type == main_type_new_post) {
  1089.                 strcpy(alphatype, ss);
  1090.                 if (strncmpi(alphasubtype, alphatype, strlen(alphasubtype)) == 0)
  1091.                   ok = 1;
  1092.               } else {
  1093.                 ttype = atoi(ss);
  1094.                 if (ttype == stype)
  1095.                   ok = 1;
  1096.               }
  1097.             }
  1098.           }
  1099.           *ss = NULL;
  1100.           if (fp != NULL)
  1101.             fclose(fp);
  1102.           if (!ok) {
  1103.             if (nhr.main_type != main_type_new_post)
  1104.               sprintf(alphatype, "%u", stype);
  1105.             output("\n ■ Subtype %s not found in NEWS.RC!", alphatype);
  1106.             sh_close(infile);
  1107.             if (text)
  1108.               free(text);
  1109.             if (buffer)
  1110.               free(buffer);
  1111.             return 1;
  1112.           }
  1113.         }
  1114.       }
  1115.       if ((nhr.main_type == main_type_email) ||
  1116.           (nhr.main_type == main_type_email_name) ||
  1117.           (nhr.main_type == main_type_ssm)) {
  1118.         output("\n ■ To      : %s", mh.toUserName);
  1119.       } else
  1120.         output("\n ■ Post to : %s", groupname);
  1121.  
  1122.       strcpy(_temp_buffer, mh.subject);
  1123.       j = 0;
  1124.       for (i = 0; i < strlen(mh.subject); i++) {
  1125.         if (_temp_buffer[i] == 3)
  1126.           ++i;
  1127.         else
  1128.           mh.subject[j++] = _temp_buffer[i];
  1129.       }
  1130.       mh.subject[j] = '\0';
  1131.  
  1132.       output("\n ■ Subject : %s", mh.subject);
  1133.  
  1134.       outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
  1135.       if (outfile == -1) {
  1136.         sh_close(infile);
  1137.         if (buffer)
  1138.           free(buffer);
  1139.         if (text)
  1140.           free(text);
  1141.         output("\n ■ Unable to open output file %s", outfn);
  1142.         return 1;
  1143.       }
  1144.       time(&some);
  1145.       time_msg = localtime(&some);
  1146.       strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
  1147.       if (nhr.fromsys != net_sysnum ) {
  1148.           sprintf(_temp_buffer, "From: %s\n", mh.fromUserName);
  1149.       } else {
  1150.         for (j = 0; j < strlen(mh.fromUserName); j++)
  1151.           if (mh.fromUserName[j] == ' ')
  1152.             mh.fromUserName[j] = '_';
  1153.         if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)))
  1154.           if (spamname[0] == 0)
  1155.             sprintf(_temp_buffer, "From: %s@dont.spam.me.%s (%s)\n",
  1156.                     POPNAME, DOMAIN,
  1157.                     mh.fromUserName);
  1158.           else
  1159.             sprintf(_temp_buffer, "From: %s (%s)\n",
  1160.                     spamname,
  1161.                     mh.fromUserName);
  1162.         else
  1163.           sprintf(_temp_buffer, "From: %s@%s (%s)\n",
  1164.                   POPNAME, DOMAIN, mh.fromUserName);
  1165.       }
  1166.       basetime = time(NULL);
  1167.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1168.       sprintf(_temp_buffer, "Message-ID: <%lx-%s@%s>\n",
  1169.               ++basetime, POPNAME, DOMAIN);
  1170.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1171.       if ((nhr.main_type == main_type_email) ||
  1172.           (nhr.main_type == main_type_email_name) ||
  1173.           (nhr.main_type == main_type_ssm)) {
  1174.         if (!tolist) {
  1175.           sprintf(_temp_buffer, "To: %s\n", mh.toUserName);
  1176.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1177.         } else {
  1178.           sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
  1179.           i = 0;
  1180.           if ((fp = fsh_open(fn1, "rt")) != NULL) {
  1181.             while (fgets(_temp_buffer, 80, fp) != NULL) {
  1182.               if (strstr(_temp_buffer, "@")) {
  1183.                 strcpy(mh.toUserName, _temp_buffer);
  1184.                 sprintf(_temp_buffer, "To: %s", mh.toUserName);
  1185.                 sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1186.                 i = 1;
  1187.               }
  1188.             }
  1189.             if (fp != NULL)
  1190.               fclose(fp);
  1191.           }
  1192.           if (!i) {
  1193.             sh_close(infile);
  1194.             if (buffer)
  1195.               free(buffer);
  1196.             if (text)
  1197.               free(text);
  1198.             output("\n ■ Error processing mailing list %s.", alphasubtype);
  1199.             return (1);
  1200.           }
  1201.         }
  1202.       } else {
  1203.         sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
  1204.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1205.       }
  1206.       sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
  1207.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1208.       sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
  1209.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1210.       sprintf(_temp_buffer, "Path: %s!%s\n", POPNAME, DOMAIN);
  1211.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1212.       sprintf(_temp_buffer, "Organization: %s * %s\n",
  1213.               syscfg.systemname, syscfg.systemphone);
  1214.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1215.       if (tolist) {
  1216.         sprintf(_temp_buffer, "Reply-To: %s@%s (%s)\n",
  1217.                 POPNAME, DOMAIN, alphasubtype);
  1218.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1219.       } else if ((!spam) && ((nhr.main_type != main_type_post) || (nhr.main_type != main_type_new_post))) {
  1220.         sprintf(_temp_buffer, "Reply-To: %s@%s (%s)\n",
  1221.                 POPNAME, DOMAIN, mh.fromUserName);
  1222.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1223.       }
  1224.       if (tolist) {
  1225.         sprintf(_temp_buffer, "Source: %s Mail List\n", alphasubtype);
  1226.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1227.       }
  1228.       sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n", VERSION);
  1229.       sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1230.  
  1231.       if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post))) {
  1232.         sprintf(_temp_buffer, "Reply to: %s@%s (%s)\n\n",
  1233.                 POPNAME, DOMAIN, mh.fromUserName);
  1234.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1235.       }
  1236.       if ((nhr.main_type != main_type_email) &&
  1237.           (nhr.main_type != main_type_email_name) &&
  1238.           (strncmp(mh.toUserName, "ALL", 3) != 0)) {
  1239.         sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
  1240.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1241.       }
  1242.       for (i = 0; i < strlen(text); i++)
  1243.         if (text[i] == 227)
  1244.           text[i] = '\n';
  1245.       sh_write(outfile, text, strlen(text));
  1246.       sprintf(tagfn, "%sI%u.TAG", syscfg.datadir, stype);
  1247.       if (exist(tagfn))
  1248.         strcpy(tagfile, tagfn);
  1249.       tagfn[0] = 0;
  1250.       ns = 0;
  1251.       for (i6 = 0; i6 < 99; i6++) {
  1252.         sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, i6);
  1253.         if (exist(tagfn))
  1254.           ns++;
  1255.         else
  1256.           break;
  1257.       }
  1258.       sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, random(ns));
  1259.       if (exist(tagfn))
  1260.         strcpy(tagfile, tagfn);
  1261.       if (tagfile[0] == 0) {
  1262.         sprintf(_temp_buffer, "\n\nOrigin: %s * %s\n\n",
  1263.                 syscfg.systemname, syscfg.systemphone);
  1264.         sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1265.       } else {
  1266.         if ((fp = fsh_open(tagfile, "rt")) == NULL)
  1267.           output("\n ■ Error reading %s.", tagfile);
  1268.         else {
  1269.           sprintf(_temp_buffer, "\n\n");
  1270.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1271.           while (fgets(_temp_buffer, 120, fp)) {
  1272.             for (i = 0; ((i < strlen(_temp_buffer)) &&
  1273.                (_temp_buffer[i] != '\r') && (_temp_buffer[i] != '\n')); i++)
  1274.               if ((_temp_buffer[i] < 32) || (_temp_buffer[i] > 126))
  1275.                 _temp_buffer[i] = 32;
  1276.             sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1277.           }
  1278.           if (fp != NULL)
  1279.             fclose(fp);
  1280.           sprintf(_temp_buffer, "\n\n");
  1281.           sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
  1282.         }
  1283.       }
  1284.       sh_close(outfile);
  1285.     } else {
  1286.       if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x1b))
  1287.         output("\n ■ %s message skipped",
  1288.                 main_type[nhr.main_type - 1]);
  1289.       else
  1290.         output("\n ■ Unknown Main_type %hd skipped", nhr.main_type);
  1291.     }
  1292.   }
  1293.   sh_close(infile);
  1294.   unlink(fn);
  1295.   if (text)
  1296.     free(text);
  1297.   if (buffer)
  1298.     free(buffer);
  1299.   return (0);
  1300. }
  1301.  
  1302. void get_dir(char *s, int be)
  1303. {
  1304.   strcpy(s, "X:\\");
  1305.   s[0] = 'A' + getdisk();
  1306.   getcurdir(0, s + 3);
  1307.   if (be) {
  1308.     if (s[strlen(s) - 1] != '\\')
  1309.       strcat(s, "\\");
  1310.   }
  1311. }
  1312.  
  1313. int main(int argc, char *argv[])
  1314. {
  1315.   char fn[MAXPATH], *ss;
  1316.   int f, f1, maxmail, i;
  1317.   struct ffblk ff;
  1318.   struct date dt;
  1319.   struct time tm;
  1320.  
  1321.   /* 0      1               2          3          4       5        6       */
  1322.   /* exp s32767.net,      net_data, net_sysnum, popname, domain, net_name  */
  1323.   /* exp netlog/contact,  net_data,   sysnum,     sent    recd   totaltime */
  1324.  
  1325.   output("\n ■ PPP Import/Export %s", VERSION);
  1326.   if (argc != 7) {
  1327.     output("\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
  1328.     if (argc > 1) {
  1329.       output("Command line was: ");
  1330.       for (i = 0; i < argc; i++)
  1331.         output("%s ", argv[i]);
  1332.       output("\n\n");
  1333.     }
  1334.     return (1);
  1335.   }
  1336.   detect_multitask();
  1337.  
  1338.   get_dir(maindir, 1);
  1339.   strcpy(net_data, argv[2]);
  1340.   sprintf(fn, "%s%s", net_data, argv[1]);
  1341.   net_name = argv[6];
  1342.   strcpy(POPNAME, argv[4]);
  1343.   strcpy(DOMAIN, argv[5]);
  1344.   net_sysnum = atoi(argv[3]);
  1345.   tagfile[0] = 0;
  1346.   spam = 0;
  1347.  
  1348.   f = sh_open1("CONFIG.DAT", O_RDONLY | O_BINARY);
  1349.   if (f < 0) {
  1350.     output("Could not open CONFIG.DAT!\n\n");
  1351.     return (1);
  1352.   }
  1353.   sh_read(f, (void *) &syscfg, sizeof(configrec));
  1354.   sh_close(f);
  1355.  
  1356.   ss = getenv("WWIV_INSTANCE");
  1357.   if (ss) {
  1358.     instance = atoi(ss);
  1359.     if (instance >= 1000) {
  1360.       output("\n ■ WWIV_INSTANCE set to %hd.  Can only be 1..999!",
  1361.               instance);
  1362.       instance = 1;
  1363.     }
  1364.   } else
  1365.     instance = 1;
  1366.  
  1367.   parse_net_ini();
  1368.   gettime(&tm);
  1369.   getdate(&dt);
  1370.   cur_daten = dostounix(&dt, &tm);
  1371.  
  1372. /*
  1373.   output("\n ■ Postmaster default account set to %s #%hd.",
  1374.           strupr(postmaster), defuser);
  1375.   output("\n ■ Using user %s on outbound Internet mail.",
  1376.           use_alias ? "aliases" : "real names");
  1377.   if (spam)
  1378.     output("\n ■ Using bogus originating address on newsgroup posts.");
  1379.   if (tagfile[0] != 0)
  1380.     output("\n ■ Using signature file : %s", tagfile);
  1381. */
  1382.   strupr(postmaster);
  1383.   export(fn);
  1384.  
  1385.   sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
  1386.   f1 = findfirst(fn, &ff, 0);
  1387.   maxmail = 14;
  1388.   while ((f1 == 0) && maxmail) {
  1389.     sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
  1390.     if (!import(fn)) {
  1391.       unlink(fn);
  1392.       --maxmail;
  1393.     }
  1394.     f1 = findnext(&ff);
  1395.   }
  1396.   if (maillist != NULL)
  1397.     free(maillist);
  1398.   return 0;
  1399. }
  1400.