home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / kfile.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  9KB  |  404 lines

  1. /* $Id: kfile.c,v 4.4 1991/09/09 20:18:23 sob Exp sob $
  2.  *
  3.  * $Log: kfile.c,v $
  4.  * Revision 4.4  1991/09/09  20:18:23  sob
  5.  * release 4.4
  6.  *
  7.  *
  8.  * 
  9.  */
  10. /* This software is Copyright 1991 by Stan Barber. 
  11.  *
  12.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  13.  * use this software as long as: there is no monetary profit gained
  14.  * specifically from the use or reproduction of this software, it is not
  15.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  16.  * included prominently in any copy made. 
  17.  *
  18.  * The author make no claims as to the fitness or correctness of this software
  19.  * for any use whatsoever, and it is provided as is. Any use of this software
  20.  * is at the user's own risk. 
  21.  */
  22.  
  23. /*** OS2: We made a patch using DosCopy, so we need os2.h ***/
  24. #include <os2.h>
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #include "term.h"
  29. #include "util.h"
  30. #include "artsrch.h"
  31. #include "ng.h"
  32. #include "bits.h"
  33. #include "intrp.h"
  34. #include "ngstuff.h"
  35. #include "rcstuff.h"
  36. #include "rn.h"
  37. #ifdef USETHREADS
  38. #include "threads.h"
  39. #include "rthreads.h"
  40. #endif
  41. #include "INTERN.h"
  42. #include "kfile.h"
  43.  
  44. static bool exitcmds = FALSE;
  45.  
  46. void
  47. kfile_init()
  48. {
  49.     ;
  50. }
  51.  
  52. #ifndef KILLFILES
  53. int
  54. edit_kfile()
  55. {
  56.     notincl("^K");
  57.     return -1;
  58. }
  59.  
  60. #else /* KILLFILES */
  61.  
  62. char killglobal[] = KILLGLOBAL;
  63. char killlocal[] = KILLLOCAL;
  64. char killlocalold[] = KILLLOCALOLD;
  65.  
  66. void
  67. mention(str)
  68. char *str;
  69. {
  70. #ifdef VERBOSE
  71.     IF(verbose) {
  72. #ifdef NOFIREWORKS
  73.     no_sofire();
  74. #endif
  75.     standout();
  76.     fputs(str,stdout);
  77.     un_standout();
  78.     putchar('\n');
  79.     }
  80.     ELSE
  81. #endif
  82. #ifdef TERSE
  83.     putchar('.');
  84. #endif
  85.     fflush(stdout);
  86. }
  87.  
  88. bool kill_mentioned;
  89.  
  90. int
  91. do_kfile(kfp,entering)
  92. FILE *kfp;
  93. int entering;
  94. {
  95. #ifdef USETHREADS
  96.     int i;
  97.     ART_NUM kill_thread;
  98. #endif
  99.  
  100.     art = lastart+1;
  101.     fseek(kfp,0L,0);            /* rewind file */
  102.     while (fgets(buf,LBUFLEN,kfp) != Nullch) {
  103.     buf[strlen(buf)-1] = '\0';
  104.     if (strnEQ(buf,"THRU",4)) {
  105.         ART_NUM tmpart;
  106.  
  107.         tmpart = atol(buf+4)+1;
  108.         if (tmpart < absfirst)
  109.         tmpart = absfirst;
  110.         check_first(tmpart);
  111.         firstart = tmpart;
  112.         continue;
  113.     }
  114.     if (*buf == 'X') {        /* exit command? */
  115.         if (entering) {
  116.         exitcmds = TRUE;
  117.         continue;
  118.         }
  119.         strcpy(buf,buf+1);
  120.     }
  121.     else {
  122.         if (!entering)
  123.         continue;
  124.     }
  125.     if (*buf == '&') {
  126.         mention(buf);
  127.         switcheroo();
  128.     }
  129.     else if (*buf == '/' && firstart <= lastart) {
  130.         mention(buf);
  131.         kill_mentioned = TRUE;
  132.         switch (art_search(buf, (sizeof buf), FALSE)) {
  133.         case SRCH_ABORT:
  134.         continue;
  135.         case SRCH_INTR:
  136. #ifdef VERBOSE
  137.         IF(verbose){
  138.             printf("\n(Interrupted at article %ld)\n",(long)art); 
  139.             FLUSH;
  140.         }
  141.         ELSE
  142. #endif
  143. #ifdef TERSE
  144.             printf("\n(Intr at %ld)\n",(long)art) ; FLUSH;
  145. #endif
  146.         return -1;
  147.         case SRCH_DONE:
  148.         break;
  149.         case SRCH_SUBJDONE:
  150.         fputs("\tsubject not found (?)\n",stdout) ; FLUSH;
  151.         break;
  152.         case SRCH_NOTFOUND:
  153.         fputs("\tnot found\n",stdout) ; FLUSH;
  154.         break;
  155.         case SRCH_FOUND:
  156.         fputs("\tfound\n",stdout) ; FLUSH;
  157.         }
  158.     }
  159. #ifdef USETHREADS
  160.     else if (*buf == 'T' && firstart <= lastart && p_roots) {
  161.         /* kill a thread by its root id number */
  162.         kill_thread = atol(buf+1);
  163.         for (i = 0; i < total.root; i++) {
  164.         if (p_roots[i].root_num == kill_thread) {
  165.             if (count_one_root(i) != 0) {
  166.             mention(buf);
  167.             kill_mentioned = TRUE;
  168.             printf("%ldx%d ",(long)kill_thread,
  169.                      root_article_cnts[i]);
  170. #ifdef VERBOSE
  171.             IF(verbose)
  172.                 putchar('\n') ; FLUSH;
  173. #endif
  174.             p_art = p_articles + p_roots[i].articles;
  175.             art = p_art->num;
  176.             follow_thread('J');
  177.             }
  178.             break;
  179.         }
  180.         }
  181.     }
  182. #endif
  183.     }
  184.  
  185.     return 0;
  186. }
  187.  
  188. void
  189. kill_unwanted(starting,message,entering)
  190. ART_NUM starting;
  191. char *message;
  192. int entering;
  193. {
  194.     bool intr = FALSE;            /* did we get an interrupt? */
  195.     ART_NUM oldfirst;
  196.     bool anytokill = (toread[ng] > 0);
  197.  
  198.     if (localkfp || globkfp) {
  199.     if (!entering && !exitcmds)
  200.         return;
  201.     exitcmds = FALSE;
  202.     oldfirst = firstart;
  203.     firstart = starting;
  204.     clear();
  205. #ifdef VERBOSE
  206. # ifdef TERSE
  207.     if (message && (verbose || entering))
  208. # else
  209.     if (message)
  210. # endif
  211. #else
  212.     if (message && entering)
  213. #endif
  214.         fputs(message,stdout) ; FLUSH;
  215.  
  216.     kill_mentioned = FALSE;
  217.     if (localkfp)
  218.         intr = do_kfile(localkfp,entering);
  219.     if (globkfp && !intr)
  220.         intr = do_kfile(globkfp,entering);
  221.     if (entering && localkfp && !intr)
  222.         setthru(lastart);
  223.     putchar('\n') ; FLUSH;
  224.     if (entering && kill_mentioned)
  225. #ifdef VERBOSE
  226.         IF(verbose)
  227.         get_anything();
  228.         ELSE
  229. #endif
  230. #ifdef TERSE
  231.         pad(just_a_sec);
  232. #endif
  233.     if (anytokill)            /* if there was anything to kill */
  234.         forcelast = FALSE;        /* allow for having killed it all */
  235.     firstart = oldfirst;
  236.     }
  237. }
  238.  
  239. void
  240. setthru(thru)
  241. ART_NUM thru;
  242. {
  243.     FILE *newkfp;
  244.     bool no_kills = 0;
  245.     char filenamebuffer[255+1];
  246. #ifdef USETHREADS
  247.     int i;
  248.     ART_NUM kill_thread;
  249. #endif
  250.  
  251.     fseek(localkfp,0L,0);        /* rewind current file */
  252.     if (fgets(buf,LBUFLEN,localkfp) != Nullch
  253.      && (strnNE(buf,"THRU",4) || fgets(buf,LBUFLEN,localkfp) != Nullch))
  254.     fseek(localkfp,0L,0);
  255.     else
  256.     no_kills = 1;
  257.     strcpy(buf,filexp(getval("KILLLOCAL",killlocal)));
  258.     strcpy(filenamebuffer,filexp(getval("KILLLOCALOLD",killlocalold)));
  259. /*** OS2: They want to delete the file, although it is
  260.           opened (localkfp). Does this really work in Un*x ?
  261.           OK, what can we do? (See patches.os2 or the following code) ***/
  262.     fclose(localkfp);
  263.     if (DosCopy(buf,filenamebuffer,DCPY_EXISTING))
  264.        printf("Cannot copy Killfile (%s->%S)!\n",buf,filenamebuffer);
  265.     if ((localkfp = fos2open(filexp(KILLLOCALOLD),"r")) == NULL)
  266.     {  printf("Can't locate old killfile\n");
  267.        UNLINK(filexp(KILLLOCALOLD));
  268.        return;
  269.     }
  270. /*    UNLINK(buf);  /* OS2: we renamed, so we don't need UNLINK */
  271.                       /* to prevent file reuse */
  272.     if (no_kills)
  273.     open_kfile(KF_LOCAL);        /* close file and reset open flag */
  274.     else if (newkfp = fos2open(buf,"w")) {
  275.     fprintf(newkfp,"THRU %ld\n",(long)thru);
  276.     while (fgets(buf,LBUFLEN,localkfp) != Nullch) {
  277.         if (strnEQ(buf,"THRU",4))
  278.         continue;
  279. #ifdef USETHREADS
  280.         /* Leave out any outdated thread kills */
  281.         if (*buf == 'T' && p_roots) {
  282.         kill_thread = atol(buf+1);
  283.         for (i = 0; i < total.root; i++) {
  284.             if (p_roots[i].root_num == kill_thread) {
  285.             break;
  286.             }
  287.         }
  288.         if (i == total.root)
  289.             continue;
  290.         }
  291. #endif
  292.         fputs(buf,newkfp);
  293.     }
  294.     fclose(newkfp);
  295.     open_kfile(KF_LOCAL);        /* and reopen local file */
  296.     }
  297.     else
  298.     printf(cantcreate,buf) ; FLUSH;
  299. /*** OS2: unlink our temporarily created file ***/
  300.     UNLINK(filexp(KILLLOCALOLD));
  301. }
  302.  
  303. /* edit KILL file for newsgroup */
  304.  
  305. int
  306. edit_kfile()
  307. {
  308.     int r = -1;
  309.  
  310.     if (in_ng)
  311.     strcpy(buf,filexp(getval("KILLLOCAL",killlocal)));
  312.     else
  313.     strcpy(buf,filexp(getval("KILLGLOBAL",killglobal)));
  314.     if ((r = makedir(buf,MD_FILE)) >= 0) {
  315. /*** OS2: we use the editor defined in the uupc-rc-files.
  316.           So don't use this stuff with visual or editor-variables ***/
  317.  
  318. /*    sprintf(cmd_buf,"%s %s",               */
  319. /*        filexp(getval("VISUAL",getval("EDITOR",defeditor))),buf);  */
  320. /*** the defeditor variable looks like this: "ed %s" ***/
  321.         change_sl2bsl(buf);
  322.         sprintf(cmd_buf,defeditor,buf);
  323.         printf("\nEditing %s KILL file:\n%s\n",
  324.         (in_ng?"local":"global"),cmd_buf) ; FLUSH;
  325.     resetty();            /* make sure tty is friendly */
  326.  
  327. /*** OS2: Under OS/2, we have to close the file before we
  328.           can call the editor, otherwise the editor would
  329.           be unable to write back the new file****/
  330.         if (in_ng) {
  331.             if (localkfp)
  332.                 fclose(localkfp);
  333.             localkfp = NULL;
  334.         } else {
  335.             if (globkfp)
  336.                 fclose(globkfp);
  337.             globkfp = NULL;
  338.         }
  339.  
  340.     r = doshell(sh,cmd_buf);/* invoke the shell */
  341.     noecho();            /* and make terminal */
  342.     crmode();            /*   unfriendly again */
  343.     termlib_init();
  344.     clear();
  345.         printf("\n");
  346.     open_kfile(in_ng);
  347.     }
  348.     else
  349.     printf("Can't make %s\n",buf) ; FLUSH;
  350.     return r;
  351. }
  352.  
  353. void
  354. open_kfile(local)
  355. int local;
  356. {
  357.     char *kname = filexp(local ?
  358.     getval("KILLLOCAL",killlocal) :
  359.     getval("KILLGLOBAL",killglobal)
  360.     );
  361.     
  362.     stat(kname,&filestat);
  363.     if (!filestat.st_size)        /* nothing in the file? */
  364.     UNLINK(kname);            /* delete the file */
  365.     if (local) {
  366.     if (localkfp)
  367.         fclose(localkfp);
  368.     localkfp = fos2open(kname,"r");
  369.     }
  370.     else {
  371.     if (globkfp)
  372.         fclose(globkfp);
  373.     globkfp = fos2open(kname,"r");
  374.     }
  375. }
  376.  
  377. void
  378. kf_append(cmd)
  379. char *cmd;
  380. {
  381.     strcpy(cmd_buf,filexp(getval("KILLLOCAL",killlocal)));
  382.     if (makedir(cmd_buf,MD_FILE) >= 0) {
  383. #ifdef VERBOSE
  384.     IF(verbose)
  385.         printf("\nDepositing command in %s...",cmd_buf);
  386.     ELSE
  387. #endif
  388. #ifdef TERSE
  389.         printf("\n--> %s...",cmd_buf);
  390. #endif
  391.     fflush(stdout);
  392.     sleep(1);
  393.     if ((tmpfp = fos2open(cmd_buf,"a")) != Nullfp) {
  394.         fseek(tmpfp,0L,2);        /* get to EOF for sure */
  395.         fprintf(tmpfp,"%s\n",cmd);
  396.         fclose(tmpfp);
  397.         fputs("done\n",stdout) ; FLUSH;
  398.     }
  399.     else
  400.         printf(cantopen,cmd_buf) ; FLUSH;
  401.     }
  402. }
  403. #endif /* KILLFILES */
  404.