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

  1.  
  2. /*
  3.  *  MBFILE.C - 4/20/89
  4.  */
  5.  
  6. #include "mb.h"
  7.  
  8. word filesize;
  9. char *fm;
  10. DIRPATH *dphd;
  11.  
  12. /*
  13.  *  Prepend path given by opt2 to file spec given by p.
  14.  */
  15.  
  16. DIRPATH *getdir(p)
  17. char *p;
  18. {
  19.   register DIRPATH *dp;
  20.  
  21.   for (dp = dphd; dp isnt NULL; dp = dp->next) if (dp->id is port->opt2)
  22.   {
  23.     strcpy(port->line, dp->path);
  24.     strcat(port->line, p);
  25.     if (!(port->mode & p_sysop)) for (; *p; p++)
  26.       if ((*p is ':') or (*p is '\\') or (*p is '/'))
  27.       { port->msg = mcant; return NULL; }
  28.     return dp;
  29.   }
  30.   port->msg = mndir;
  31.   return NULL;
  32. }
  33.  
  34. /*
  35.  *  Display the directory paths.
  36.  */
  37.  
  38. shpaths()
  39. {
  40.   register DIRPATH *dp;
  41.   int crcount = 1;
  42.  
  43.   sprintf(port->line, "Use %c and directory ID:\n", port->opt1);
  44.   outstr(port->line);
  45.   for (dp = dphd; dp isnt NULL; dp = dp->next)
  46.   {
  47.     ++crcount;
  48.     sprintf(port->line, "%c%c %-36s", port->opt1, dp->id, dp->name);
  49.     outstr(port->line);
  50.     if (crcount % 2) outstr("\n"); else outstr(" ");
  51.   }
  52.   outstr("\n");
  53. }
  54.  
  55. /*
  56.  *  Display directory.
  57.  */
  58.  
  59. prtdir()
  60. {
  61.   DIRDEF dirdef;
  62.   DIRENT *dp;
  63.  
  64.   register short gap, i, k, l, ndir;
  65.   register word totsize;
  66.  
  67.   if (port->mode & ops)
  68.   {
  69.     if (port->opt2 is ' ')
  70.     {
  71.       if (port->flds is 1) { shpaths(); return; }
  72.       strcpy(port->line, port->fld[1]);
  73.     }
  74.     else if (getdir(port->fld[1]) is NULL) return;
  75.   }
  76.   else
  77.   {
  78.     if (port->opt2 is ' ') { shpaths(); return; }
  79.     if (getdir(port->fld[1]) is NULL) return;
  80.   }
  81.  
  82.   ndir = 0;
  83.   dp = (DIRENT *)tmp->scr;
  84.  
  85. /*
  86.  *  Get first directory entry.
  87.  */
  88.  
  89.   if (!diropen(port->line, dp, &dirdef)) { port->msg = mfind; return; }
  90.  
  91. /*
  92.  *  Get the rest of the directory entries.
  93.  */
  94.  
  95.   while ((ndir < dirmax) and (dp->size >= 0))
  96.   {
  97.     ndir++;
  98.     dirnext(++dp, &dirdef);
  99.   }
  100.  
  101. /*
  102.  *  Sort 'em.
  103.  */
  104.  
  105.   sort(tmp->scr, ndir, sizeof(DIRENT), tmp->scr + (sizeof(DIRENT) * ndir));
  106.  
  107. /*
  108.  *  Print 'em.
  109.  */
  110.  
  111.   outchar('\n');
  112.   totsize = 0;
  113.   gap = (ndir + 2) / 3;
  114.   for (i = 0; i < gap; i++)
  115.   {
  116.     if (pgck() is 'Q') break;
  117.     for (l = 0, k = i; (l < 4) and (k < ndir); l++, k += gap)
  118.     {
  119.       totsize += tmp->dirent[k].size;
  120.       if (k isnt i) outstr(" | ");
  121.       sprintf (port->line,
  122.         "%-12s %4uk", tmp->dirent[k].name, tmp->dirent[k].size);
  123.       outstr(port->line);
  124.     }
  125.     outchar('\n');
  126.   }
  127.  
  128.   sprintf(port->line, "\n%uk of %uk used, %uk free.\n\n",
  129.     totsize, dirdef.size, dirdef.free);
  130.   outstr(port->line);
  131. }
  132.  
  133. /*
  134.  *  Output a line from the help file.
  135.  */
  136.  
  137. helpo()
  138. {
  139.   if ((*port->line isnt '!') or (port->user->options & u_sysop))
  140.   { if (*port->line is '!') *port->line = ' '; outstr(port->line); }
  141. }
  142.  
  143. /*
  144.  *  Find specific subject header in file.
  145.  */
  146.  
  147. helps()
  148. {
  149.   register short found = false;
  150.  
  151.   while (!found and (fgets(port->line, linelen, port->fl) isnt NULL))
  152.     if ((port->line[0] is port->opt1) and
  153.         (port->line[2] is port->opt2)) found = true;
  154.  
  155.   if (!found)
  156.   {
  157.     sprintf(port->line, "\nNo help for - %c\n\n", port->opt2);
  158.     port->msg = port->line;
  159.     return;
  160.   }
  161.  
  162.   found = false;
  163.   while (!found and (fgets(port->line, linelen, port->fl) isnt NULL))
  164.   {
  165.     found = ((*port->line is '#') or (*port->line is '?'));
  166.     if (!found) helpo();
  167.   }
  168. }
  169.  
  170. /*
  171.  *  Dump all the help information.
  172.  */
  173.  
  174. helpa()
  175. {
  176.   register short ok = false;
  177.  
  178.   while ((fgets(port->line, linelen, port->fl) isnt NULL))
  179.   {
  180.     if ((*port->line is '#') or (*port->line is '?'))
  181.     {
  182.       ok = (*port->line is port->opt1);
  183.     }
  184.     else if (ok) helpo();
  185.   }
  186. }
  187.  
  188. /*
  189.  *  User wants help.
  190.  */
  191.  
  192. help()
  193. {
  194.   if (port->flds is 1) port->opt2 = '_'; else port->opt2 = *port->fld[1];
  195.   if (port->opt1 is 'H') port->opt1 = '#';
  196.  
  197.   if ((port->fl = fopen(helpfile, "r")) is NULL) { port->msg = mfind; return; }
  198.  
  199.   if (port->opt2 is '?') helpa(); else helps();
  200.  
  201.   fclose(port->fl);
  202. }
  203.  
  204. /*
  205.  *  N command, rename a file.
  206.  */
  207.  
  208. renfil()
  209. {
  210.   register int fl;
  211.  
  212.   if ((fl = open(port->fld[1], O_RDONLY)) < 0)
  213.     { nofile(port->fld[1]); return; }
  214.  
  215.   close(fl);
  216.  
  217.   if ((fl = open(port->fld[2], O_RDONLY)) >= 0)
  218.     { close(fl); port->msg = mexst; }
  219.   else rename (port->fld[1], port->fld[2]);
  220. }
  221.  
  222. /*
  223.  *  Z command, delete a file.
  224.  */
  225.  
  226. kilfil()
  227. {
  228.   if (port->opt2 is ' ') strcpy(port->line, port->fld[1]);
  229.   else if (getdir(port->fld[1]) is NULL) return;
  230.  
  231.   if (!unlink(port->line)) port->msg = mdone; else nofile(port->line);
  232. }
  233.  
  234. /*
  235.  *  Are the two files in the same directory?
  236.  */
  237.  
  238. samedir(t, f)
  239. char *t, *f;
  240. {
  241.   register char *te, *fe;
  242.  
  243. /*
  244.  *  Are they on the same device?
  245.  */
  246.  
  247.   te = strchr(t, ':');
  248.   fe = strchr(f, ':');
  249.   if ((te isnt NULL) or (fe isnt NULL))
  250.   {
  251.     if ((te - t) isnt (fe - f)) return false;
  252.     if (!matchn(te, fe, (int)(fe - f) + 1)) return false;
  253.   }
  254.  
  255. /*
  256.  *  Are they in the same subdirectory on the device?
  257.  */
  258.  
  259.   te = strrchr(t, '\\');
  260.   fe = strrchr(f, '\\');
  261.   if ((te is NULL) and (fe is NULL)) return true;
  262.   if ((te - t) isnt (fe - f)) return false;
  263.   return matchn(t, f, (int)(fe - f) + 1);
  264. }
  265.  
  266. /*
  267.  *  Copy a file.
  268.  */
  269.  
  270. copy(f, t, h)
  271. char *f, *t;
  272. int h;
  273. {
  274.   register int n, in, out;
  275.  
  276.   filesize = 0;
  277.  
  278.   if ((in = open(f, O_RDONLY | O_BINARY)) < 0) return false;
  279.  
  280.   out = open(t, O_CREAT | O_RDWR | O_BINARY, pmode);
  281.   if (h) lseek(out, (long)RECSIZE, 0);
  282.  
  283.   while ((n = read(in, tmp->scr, scrmax)) > 0)
  284.   { filesize += n; write (out, tmp->scr, n); }
  285.  
  286.   close(in);
  287.   close(out);
  288.   return true;
  289. }
  290.  
  291. /*
  292.  *  V command: copy a file.
  293.  */
  294.  
  295. copfil()
  296. {
  297.   register int n;
  298.  
  299.   if ((n = open(port->fld[2], O_RDONLY | O_BINARY)) >= 0)
  300.   { port->msg = mexst; close(n); return; }
  301.  
  302.   if ((n = open(port->fld[1], O_RDONLY | O_BINARY)) < 0)
  303.     { nofile(port->fld[1]); return;}
  304.  
  305.   close(n);
  306.   copy(port->fld[1], port->fld[2], false);
  307. }
  308.  
  309. /*
  310.  *  Upload, common code.
  311.  */
  312.  
  313. uload(tname)
  314. char *tname;
  315. {
  316.   register char *tp;
  317.   register PORTS *p;
  318.  
  319.   p = port;
  320.  
  321.   if ((p->fl = fopen(tname, "w")) is NULL) { p->msg = mcant; return; }
  322.  
  323.   filesize = 0;
  324.   while (true)
  325.   {
  326.     while(!getdat());
  327.  
  328. /*
  329.  *  If user disconnected, timed out, or forced off, zap the file.
  330.  */
  331.  
  332.     if (p->mode & gone)
  333.     {
  334.       fclose(p->fl);
  335.       unlink(tname);
  336.       return;
  337.     }
  338.  
  339.     if ((tp = strchr(p->line, cpmeof)) is NULL)
  340.     {
  341.       filesize += strlen(p->line);
  342.       fputs(p->line, p->fl);
  343.     }
  344.     else
  345.     {
  346.       if (tp isnt p->line)
  347.       {
  348.         *tp++ = '\n';
  349.         *tp   = '\0';
  350.         filesize += strlen(p->line);
  351.         fputs(p->line, p->fl);
  352.       }
  353.       fclose(p->fl);
  354.       return;
  355.     }
  356.   }
  357. }
  358.  
  359. /*
  360.  *  Upload a file, from local console and remote sysop.
  361.  */
  362.  
  363. uloadl()
  364. {
  365.   if ((port->fl = fopen(port->fld[1], "r")) isnt NULL)
  366.     { port->msg = mexst; fclose(port->fl); return; }
  367.   prtx(fm);
  368.   uload(port->fld[1]);
  369. }
  370.  
  371. /*
  372.  *  Upload a file, from logged in user.
  373.  */
  374.  
  375. uloadr()
  376. {
  377.   register DIRPATH *dp;
  378.  
  379.   if (port->opt2 is ' ') { shpaths(); return; }
  380.   if ((dp = getdir(port->fld[1])) is NULL) return;
  381.   if (!(dp->flags & dp_upload)) { port->msg = mcant; return; }
  382.   if (!(port->priv & p_upload)) { port->msg = mcant; return; }
  383.   if ((port->fl = fopen(port->line, "r")) isnt NULL)
  384.     { port->msg = mexst; fclose(port->fl); return; }
  385.   log('F', 'U', ' ', port->line);
  386.   prtx(fm);
  387.   uload(port->line);
  388. }
  389.  
  390. /*
  391.  *  Download, common code.
  392.  */
  393.  
  394. dload(fname)
  395. char *fname;
  396. {
  397.   if ((port->fl = fopen(fname, "r")) is NULL) { nofile(fname); return; }
  398.  
  399.   while(fgets(tmp->scr, scrmax, port->fl) isnt NULL)
  400.   {
  401.      if (chkdis()) break;
  402.      outstr(tmp->scr);
  403.   }
  404.   fclose (port->fl);
  405. }
  406.  
  407. /*
  408.  *  Display the "info" file.
  409.  */
  410.  
  411. dloadi()
  412. {
  413.   dload(infofile);
  414. }
  415.  
  416.  
  417. /*
  418.  *  Download a file, by logged in user.
  419.  */
  420.  
  421. dloadr()
  422. {
  423.   register DIRPATH *dp;
  424.  
  425.   if (port->opt2 is ' ') { shpaths(); return; }
  426.   if ((dp = getdir(port->fld[1])) is NULL) return;
  427.   if (!(dp->flags & dp_dnload)) { port->msg = mcant; return; }
  428.   if (!(port->priv & p_dnload)) { port->msg = mcant; return; }
  429.   log('F', 'D', ' ', port->line);
  430.   dload(port->line);
  431. }
  432.  
  433. /*
  434.  *  Download a file, by remote sysop.
  435.  */
  436.  
  437. dloads()
  438. {
  439.   if (port->opt2 is ' ')
  440.   {
  441.     if (port->flds is 1) { shpaths(); return; }
  442.     strcpy(port->line, port->fld[1]);
  443.   }
  444.   else if (getdir(port->fld[1]) is NULL) return;
  445.  
  446.   log('F', 'D', ' ', port->line);
  447.   dload(port->line);
  448. }
  449.  
  450. /*
  451.  *  Download a file, by local console.
  452.  */
  453.  
  454. dloadl()
  455. {
  456.   register PORTS *tp;
  457.  
  458.   if (port->opt2 is ' ') port->opt2 = 'L';
  459.   if ((tp = findport(port->opt2)) is NULL) { port->msg = mnport; return; }
  460.   if ((cport->fl = fopen(cport->fld[1], "r")) is NULL)
  461.     { nofile(cport->fld[1]); return; }
  462.  
  463.   ioport(tp);
  464.   pgst(NULL);
  465.   while(fgets(tmp->scr, scrmax, cport->fl) isnt NULL)
  466.   {
  467.     outstr(tmp->scr);
  468.     if (pgck() is 'Q') break;
  469.   }
  470.   fclose (cport->fl);
  471.   if (tp isnt cport) term(tp);
  472.   ioport(cport);
  473. }
  474.  
  475. /*
  476.  *  Move killed mail over to a subdirectory if it exists. NTS for all
  477.  *  type 'T' and 'S' mail. Sysop's call for all to and from the sysop
  478.  *  and KILL for non-bbs mail.
  479.  */
  480.  
  481. arcmsg()
  482. {
  483.   register int n, in, out;
  484.   static char bbs[8];
  485.   FILE *index;
  486.  
  487.   msgfile(port->line, port->mmhs->number);
  488.   strcpy(bbs, "KILL");
  489.  
  490.   if (*port->mmhs->bbs isnt ' ')
  491.     unbl(bbs, port->mmhs->bbs, ln_call);
  492.   if ((matchn(port->mmhs->to, cport->user->call, ln_call)) or
  493.     (matchn(port->mmhs->from, cport->user->call, ln_call)))
  494.     unbl(bbs, cport->user->call, ln_call);
  495.   if ((port->mmhs->type is 'T') or (port->mmhs->type is 'S'))
  496.     strcpy(bbs, "NTS");
  497.   if (port->mmhs->stat & m_busy) strcpy( bbs, "BUSY");
  498.   if (port->mmhs->stat & m_noarc) strcpy(bbs, "NOARC");
  499.  
  500.   while (true)
  501.   {
  502.     sprintf(port->cmd, "%s%s\\%u", msgdir, bbs, port->mmhs->number);
  503.  
  504.     if ((out = open(port->cmd, O_CREAT | O_RDWR | O_BINARY, pmode)) < 0)
  505.     {
  506.       if((matchn(bbs, "KILL", 4)) or (matchn(bbs, "BUSY", 4))
  507.        or (port->mmhs->stat & m_noarc))
  508.       {
  509.          unlink(port->line);
  510.          outstr(" Deleted\n");
  511.          return;
  512.       }
  513.       strcpy(bbs, "KILL");
  514.     }
  515.     else break;
  516.   }
  517.   makehdr();
  518.   sprintf(port->cmd, "%s%s\\INDEX", msgdir, bbs);
  519.   if ((index = fopen(port->cmd, "a+")) isnt NULL)
  520.   {
  521.     fprintf( index, "%s", tmp->scr);
  522.     fclose(index);
  523.   }
  524.   remnl (tmp->scr);
  525.   strcat (tmp->scr, "\r\n");
  526.  
  527.   if (*port->mmhs->bid isnt ' ') {
  528.     sprintf(port->cmd, "   BID: -%12.12s\r\n", port->mmhs->bid);
  529.     strcat(tmp->scr, port->cmd);
  530.     }
  531.   write(out, tmp->scr, strlen(tmp->scr));
  532.  
  533.   in = open(port->line, O_RDONLY | O_BINARY);
  534.   lseek(in, (long)RECSIZE, 0);
  535.  
  536.   while ((n = read(in, tmp->scr, scrmax)) > 0)
  537.     write( out, tmp->scr, n);
  538.  
  539.   close(out);
  540.   close(in);
  541.   unlink(port->line);
  542.   sprintf(port->line," Copied to %s\n", bbs);
  543.   outstr(port->line);
  544. }
  545.  
  546.