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