home *** CD-ROM | disk | FTP | other *** search
/ World of Ham Radio 1994 January / AMSOFT_1994.iso / packet / pbbs / cbbs / prog / mbrestm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-31  |  8.3 KB  |  400 lines

  1. /*
  2.  *  MBRESTM - 1/18/88
  3.  *
  4.  *  Copyright (C) 1988, 1989
  5.  *  By the CBBS Group.
  6.  *
  7.  *  This program reads the individual message text files and
  8.  *  produces MAIL.DAT from the information. It is intended to
  9.  *  be used after a mail file crash.
  10.  *
  11.  *  It is expected that this program will be run from the
  12.  *  directory containing the message files. The new MAIL.DAT
  13.  *  file will also be found in this directory. It will have to
  14.  *  copied the the proper bbs directory replacing the corrupted
  15.  *  original mail file.
  16.  *
  17.  */
  18.  
  19. #include "mb.h"
  20. #include "dos.h"
  21.  
  22. MAIL_HDR *mfhs;
  23. MSG_HDR *tmmhs;
  24. byte flds;
  25. char *fld[maxflds];
  26. char *cmd;
  27. char *line;
  28. char *nullstr = "";
  29.  
  30. char pp[44], name[500][13], msgfile[10], badfile[10];
  31. int count = 0;
  32. int cmp();
  33.  
  34. main()
  35. {
  36.   int x, c, y;
  37.   int xx = 1;
  38.   int recno = 1;
  39.   int msgfl, mail;
  40.   char *t;
  41.  
  42.   mfhs    =  (MAIL_HDR *) malloc(sizeof(MAIL_HDR));
  43.   tmmhs   =  (MSG_HDR *)  malloc(sizeof(MSG_HDR));
  44.   cmd     =  (char *) malloc(linelen);
  45.   line    =  (char *) malloc(linelen);
  46.  
  47.   if((mail = open("MAIL.DAT", O_CREAT | O_RDWR | O_BINARY, pmode)) < 0)
  48.     { puts("MAIL.DAT cannot be opened - process aborted!\n");
  49.       exit (1);
  50.     }
  51.  
  52.   dir_list();
  53.  
  54.   while( xx <= count)
  55.   {
  56.     y = true;
  57.     sprintf (msgfile, "%s",name[xx-1]);
  58.     printf("%s ", msgfile);
  59.     if((msgfl = open( msgfile, O_RDWR | O_BINARY)) < 0)
  60.       {
  61.         printf(" cannot be opened - bypassing this file\n");
  62.         xx++;
  63.         continue;
  64.       }
  65.  
  66.     read_rec(msgfl, 0, (char *) line);
  67.     if(!((line[253] is '\0') and (line[254] is '\r') and (line[255] is '\n')))
  68.       {
  69.         printf(" seems to be corrupted - bypassing this file\n");
  70.         close(msgfl);
  71.         sprintf(badfile, "%s.BAD", msgfile);
  72.         rename(msgfile, badfile);
  73.         xx++;
  74.         continue;
  75.       }
  76.  
  77.  
  78.     fill(tmmhs, '\0', 256);
  79.     tmmhs->rn       = xx;
  80.     tmmhs->read     = atoi( &line[61]);
  81.     tmmhs->number   = atoi( &line[0]);
  82.     tmmhs->size     = atoi( &line[9]);
  83.     tmmhs->type     = line[6];
  84.     switch (line[7])
  85.       {
  86.       case 'O': tmmhs->stat = m_stale; break;
  87.       case 'F': tmmhs->stat = m_fwd; break;
  88.       case 'H': tmmhs->stat = m_hold; break;
  89.       case 'Y': tmmhs->stat = m_read; break;
  90.       case 'K': tmmhs->stat = m_kill; break;
  91.       case '$': tmmhs->stat = m_bull; break;
  92.       default:  tmmhs->stat = '\0';
  93.       }
  94.  
  95.     strncpy(tmmhs->to, &line[15], ln_call);
  96.     strncpy(tmmhs->from, &line[22], ln_call);
  97.     strncpy(tmmhs->bbs, &line[29],ln_call);
  98.     strncpy(tmmhs->date, &line[36], ln_date);
  99.     strncpy(tmmhs->time, &line[43], ln_time);
  100.     fill(tmmhs->bid, ' ', ln_bid);
  101.     if (line[48] isnt ' ') unbl(tmmhs->bid, &line[48], ln_bid);
  102.     tmmhs->ext = line[68] - '0'; 
  103.     if (tmmhs->ext is 1)
  104.     {
  105.       strncpy(tmmhs->flag, &line[70], mmesn);
  106.       for( x=0, c=0; x < mmesn; x++, c++)
  107.         {
  108.         if (tmmhs->flag[x] is ' ') { tmmhs->flag[x] = '0'; c--;}
  109.         tmmhs->flag[x] = (tmmhs->flag[x]-'0');
  110.         }
  111.         tmmhs->count = c;
  112.         y = dodis();
  113.     }
  114.     if (tmmhs->ext is 2)
  115.     {
  116.       strcpy (cmd, &line[70]);
  117.       remnl (cmd);
  118.       strcpy(tmmhs->call[0], cmd);
  119.       t = strchr(&line[68], '\n');
  120.       t++;
  121.       strcpy(cmd, t);
  122.       remnl(cmd);
  123.       strcpy(tmmhs->title, cmd);
  124.     }
  125.     else
  126.     {
  127.       parse();
  128.       strcpy(tmmhs->title, fld[0]);
  129.     }
  130.     write_rec(mail,recno++,(char *)tmmhs);
  131.     close(msgfl);
  132.     if (y) puts("restored");
  133.     xx++;
  134.   }
  135.   mfhs->next    = recno;
  136.   mfhs->first   = 1;
  137.   mfhs->last    = recno-1;
  138.   mfhs->version = mb_version;
  139.   mfhs->free    = 0;
  140.   mfhs->count   = recno-1;
  141.   mfhs->unt_msg = 1;
  142.   fill(mfhs->unu, '\0', mfhsunu);
  143.   fill(mfhs->date, '\0', ln_date);
  144.   fill(mfhs->time, '\0', ln_time);
  145.   x = 1;
  146.   if (count)  x = 1 + atoi(name[count - 1]);
  147.   printf("The next message number will be %u\n", x);
  148.   printf("Enter return if OK or new message number  ");
  149.   gets(cmd);
  150.   if (*cmd)  mfhs->next_msg = atoi(cmd);
  151.   else  mfhs->next_msg = x;
  152.   mfhs->unt_msg = mfhs->next_msg;
  153.   write_rec(mail, 0, (char *)mfhs);
  154.  
  155.   close(mail);
  156. }
  157.  
  158. /*
  159.  *  Parse a command line.
  160.  *
  161.  *  Input:   Line of text in line.
  162.  *  Returns: Fields are placed in cmd.
  163.  *           Fields are pointed to by fld[].
  164.  *           flds is set to the number of fields found.
  165.  *           Each field is null-terminated.
  166.  *           Fields beyond maxflds are ignored.
  167.  */
  168.  
  169. parse()
  170. {
  171.   char skip;
  172.   register short bl;
  173.   register char *in, *out;
  174.  
  175.   for (flds = 0; flds < maxflds;)
  176.     fld[flds++] = nullstr;
  177.  
  178.   in  = &line[86];
  179.   out = cmd;
  180.   bl  = false;
  181.   flds = 0;
  182.   skip = true;
  183.  
  184.   while (*in and (flds < maxflds) and (out < (cmd + linelen - 1)))
  185.   {
  186.     if (*in is '\r') { *in++; continue;}
  187.     if (*in is '\n')  skip = !(skip);
  188.     *out = *in;
  189.     if (bl) {if ((*in <= ' ') and (skip)) { bl = false; *out = '\0'; } out++; }
  190.     else    {if (*in > ' ')  { bl = true; fld[flds++] = out++; }}
  191.     in++;
  192.   }
  193.   *out = '\0';
  194. }
  195.  
  196.  
  197. /*
  198.  *  Remove new line  and carriage returns from end of string.
  199.  */
  200.  
  201. remnl(p)
  202. char *p;
  203. {
  204.   for (; *p; p++)
  205.   {
  206.     if ((*p is '\n') or (*p is '\r')) { *p = '\0'; return; }
  207.   }
  208. }
  209. /*
  210.  *  Read random record.
  211.  */
  212.  
  213. read_rec(fid, rec, buffer)
  214. int fid;
  215. int rec;
  216. char buffer[];
  217. {
  218.   long lseek();
  219.   long offs;
  220.  
  221.   offs = (long)rec * (long)RECSIZE;
  222.   lseek(fid, offs, 0);
  223.   return (read(fid, buffer, RECSIZE) is RECSIZE);
  224. }
  225.  
  226. /*
  227.  *  Write random record.
  228.  */
  229.  
  230. write_rec(fid, rec, buffer)
  231. int fid;
  232. int rec;
  233. char buffer[];
  234. {
  235.   long lseek();
  236.   long offs;
  237.  
  238.   offs = (long)rec * (long)RECSIZE;
  239.   lseek(fid, offs, 0);
  240.   return (write(fid, buffer, RECSIZE) is RECSIZE);
  241. }
  242. /*
  243.  *  Fill some memory with a character.
  244.  */
  245.  
  246. fill(adr, ch, len)
  247. char *adr;
  248. char ch;
  249. int len;
  250. {
  251.   while (len--) *adr++ = ch;
  252. }
  253.  
  254. /*
  255.  *  Create the distribution list, if required.
  256.  */
  257.  
  258. dodis()
  259. {
  260.   register FILE *dfl;
  261.   char c;
  262.   char tmp[linelen];
  263.  
  264.   tmmhs->ext = 0;
  265.   if (*tmmhs->bbs is ' ') return;
  266.   unbl(tmp, tmmhs->bbs, ln_call);
  267.   strcat( tmp, ".DIS");
  268.  
  269.   if ((dfl = fopen( tmp, "r")) is NULL)
  270.      {
  271.      printf("restored without %s (file is not found)\n", tmp);
  272.      return false;
  273.      }
  274.   tmmhs->ext = 0x01;
  275.  
  276.   for (c = 0; (c <tmmhs->count) and (fgets( tmp, linelen, dfl) isnt NULL);
  277.     c++)
  278.     {
  279.     strupr(tmp);
  280.     pcall(tmmhs->call[c], tmp);
  281.     }
  282.   fclose(dfl);
  283.   return true;
  284. }
  285. /*
  286.  *  Copy LJSF string to C string.
  287.  */
  288.  
  289. unbl(to, from, size)
  290. char *to, *from;
  291. int size;
  292. {
  293.   while (size--)
  294.   {
  295.     if (*from <= ' ') { *to = '\0'; return; }
  296.     *to++ = *from++;
  297.   }
  298.   *to = '\0';
  299. }
  300. /*
  301.  *  Parse a callsign.
  302.  *  Blank pad the output field, remove trailing ssid from call.
  303.  *  Return the SSID.
  304. */
  305.  
  306. pcall(c, p)
  307. char *c;
  308. char *p;
  309. {
  310.   register short i;
  311.  
  312. /*
  313.  *  Blank fill the target buffer.
  314.  */
  315.  
  316.   fill (c, ' ', ln_call);
  317.  
  318. /*
  319.  *  Ignore leading spaces.
  320.  */
  321.  
  322.   while (*p and (*p is ' ')) p++;
  323.  
  324. /*
  325.  *  Copy the call from the string into the call buffer.
  326.  */
  327.  
  328.   for (i = ln_call; i and *p; i--)
  329.   {
  330.     if (*p <= ' ') return 0;
  331.     if (*p is '-') return atoi(++p);
  332.     *c++ = *p++;
  333.   }
  334.   if (*p++ is '-') return atoi(p); else return 0;
  335. }
  336.  
  337. /*
  338.  *  Read the file names from the current directory and store
  339.  *  the results in an array but exclude any name that is not
  340.  *  made up of all numbers.
  341.  *  Sort the resulting list in ascending order.
  342.  */
  343.  
  344. dir_list()
  345. {
  346.   int x, y;
  347.   char *file_name = "*.";
  348.  
  349.   printf("Reading directory - ");
  350.   bdos(0x1a, (unsigned)pp, 0);
  351.   if(18==bdos(0x4e, (unsigned)file_name, 0))
  352.     {
  353.       printf("No message files are found\n");
  354.       return;
  355.     }
  356.  
  357.   if (num( &pp[30])) strcpy (name[count++], &pp[30]);
  358.  
  359.   for(;;)
  360.   {
  361.     if(18==bdos(0x4f, 0, 0)) break;
  362.     if (num( &pp[30])) strcpy (name[count++], &pp[30]);
  363.   }
  364.   printf("There are %d message files\n", count);
  365.   if (count)
  366.     {
  367.       printf("***Sorting***\n");
  368.       qsort (name, count, 13, cmp);
  369.       for(x=0, y=1; x< count; x++, y++) 
  370.       {
  371.         printf("%6s", name[x]);
  372.         if(y is 10) { printf("\n"); y=0; }
  373.       }
  374.       printf("\n");
  375.     }
  376. }
  377.  
  378. /*
  379.  *  Compare two strings converted to integers.
  380.  */
  381.  
  382. int cmp(n1,n2)
  383. char *n1;
  384. char *n2;
  385. {
  386.    return (atoi(n1) - atoi(n2));
  387.  
  388. }
  389.  
  390. /*
  391.  *  Is the string a number?
  392.  */
  393.  
  394. num(p)
  395. char *p;
  396. {
  397.   for (; *p; p++) if (!isdigit(*p)) return false;
  398.   return true;
  399. }
  400.