home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trn / part03 / kfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  7.2 KB  |  360 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. #include "EXTERN.h"
  24. #include "common.h"
  25. #include "term.h"
  26. #include "util.h"
  27. #include "artsrch.h"
  28. #include "ng.h"
  29. #include "bits.h"
  30. #include "intrp.h"
  31. #include "ngstuff.h"
  32. #include "rcstuff.h"
  33. #include "rn.h"
  34. #ifdef USETHREADS
  35. #include "threads.h"
  36. #include "rthreads.h"
  37. #endif
  38. #include "INTERN.h"
  39. #include "kfile.h"
  40.  
  41. static bool exitcmds = FALSE;
  42.  
  43. void
  44. kfile_init()
  45. {
  46.     ;
  47. }
  48.  
  49. #ifndef KILLFILES
  50. int
  51. edit_kfile()
  52. {
  53.     notincl("^K");
  54.     return -1;
  55. }
  56.  
  57. #else /* KILLFILES */
  58.  
  59. char killglobal[] = KILLGLOBAL;
  60. char killlocal[] = KILLLOCAL;
  61.  
  62. void
  63. mention(str)
  64. char *str;
  65. {
  66. #ifdef VERBOSE
  67.     IF(verbose) {
  68. #ifdef NOFIREWORKS
  69.     no_sofire();
  70. #endif
  71.     standout();
  72.     fputs(str,stdout);
  73.     un_standout();
  74.     putchar('\n');
  75.     }
  76.     ELSE
  77. #endif
  78. #ifdef TERSE
  79.     putchar('.');
  80. #endif
  81.     fflush(stdout);
  82. }
  83.  
  84. bool kill_mentioned;
  85.  
  86. int
  87. do_kfile(kfp,entering)
  88. FILE *kfp;
  89. int entering;
  90. {
  91. #ifdef USETHREADS
  92.     int i;
  93.     ART_NUM kill_thread;
  94. #endif
  95.  
  96.     art = lastart+1;
  97.     fseek(kfp,0L,0);            /* rewind file */
  98.     while (fgets(buf,LBUFLEN,kfp) != Nullch) {
  99.     buf[strlen(buf)-1] = '\0';
  100.     if (strnEQ(buf,"THRU",4)) {
  101.         ART_NUM tmpart;
  102.  
  103.         tmpart = atol(buf+4)+1;
  104.         if (tmpart < absfirst)
  105.         tmpart = absfirst;
  106.         check_first(tmpart);
  107.         firstart = tmpart;
  108.         continue;
  109.     }
  110.     if (*buf == 'X') {        /* exit command? */
  111.         if (entering) {
  112.         exitcmds = TRUE;
  113.         continue;
  114.         }
  115.         strcpy(buf,buf+1);
  116.     }
  117.     else {
  118.         if (!entering)
  119.         continue;
  120.     }
  121.     if (*buf == '&') {
  122.         mention(buf);
  123.         switcheroo();
  124.     }
  125.     else if (*buf == '/' && firstart <= lastart) {
  126.         mention(buf);
  127.         kill_mentioned = TRUE;
  128.         switch (art_search(buf, (sizeof buf), FALSE)) {
  129.         case SRCH_ABORT:
  130.         continue;
  131.         case SRCH_INTR:
  132. #ifdef VERBOSE
  133.         IF(verbose)
  134.             printf("\n(Interrupted at article %ld)\n",(long)art)
  135.               FLUSH;
  136.         ELSE
  137. #endif
  138. #ifdef TERSE
  139.             printf("\n(Intr at %ld)\n",(long)art) FLUSH;
  140. #endif
  141.         return -1;
  142.         case SRCH_DONE:
  143.         break;
  144.         case SRCH_SUBJDONE:
  145.         fputs("\tsubject not found (???)\n",stdout) FLUSH;
  146.         break;
  147.         case SRCH_NOTFOUND:
  148.         fputs("\tnot found\n",stdout) FLUSH;
  149.         break;
  150.         case SRCH_FOUND:
  151.         fputs("\tfound\n",stdout) FLUSH;
  152.         }
  153.     }
  154. #ifdef USETHREADS
  155.     else if (*buf == 'T' && firstart <= lastart && p_roots) {
  156.         /* kill a thread by its root id number */
  157.         kill_thread = atol(buf+1);
  158.         for (i = 0; i < total.root; i++) {
  159.         if (p_roots[i].root_num == kill_thread) {
  160.             if (count_one_root(i) != 0) {
  161.             mention(buf);
  162.             kill_mentioned = TRUE;
  163.             printf("%ldx%d ",(long)kill_thread,
  164.                      root_article_cnts[i]);
  165. #ifdef VERBOSE
  166.             IF(verbose)
  167.                 putchar('\n') FLUSH;
  168. #endif
  169.             p_art = p_articles + p_roots[i].articles;
  170.             art = p_art->num;
  171.             follow_thread('J');
  172.             }
  173.             break;
  174.         }
  175.         }
  176.     }
  177. #endif
  178.     }
  179.  
  180.     return 0;
  181. }
  182.  
  183. void
  184. kill_unwanted(starting,message,entering)
  185. ART_NUM starting;
  186. char *message;
  187. int entering;
  188. {
  189.     bool intr = FALSE;            /* did we get an interrupt? */
  190.     ART_NUM oldfirst;
  191.     bool anytokill = (toread[ng] > 0);
  192.  
  193.     if (localkfp || globkfp) {
  194.     if (!entering && !exitcmds)
  195.         return;
  196.     exitcmds = FALSE;
  197.     oldfirst = firstart;
  198.     firstart = starting;
  199.     clear();
  200. #ifdef VERBOSE
  201. # ifdef TERSE
  202.     if (message && (verbose || entering))
  203. # else
  204.     if (message)
  205. # endif
  206. #else
  207.     if (message && entering)
  208. #endif
  209.         fputs(message,stdout) FLUSH;
  210.  
  211.     kill_mentioned = FALSE;
  212.     if (localkfp)
  213.         intr = do_kfile(localkfp,entering);
  214.     if (globkfp && !intr)
  215.         intr = do_kfile(globkfp,entering);
  216.     if (entering && localkfp && !intr)
  217.         setthru(lastart);
  218.     putchar('\n') FLUSH;
  219.     if (entering && kill_mentioned)
  220. #ifdef VERBOSE
  221.         IF(verbose)
  222.         get_anything();
  223.         ELSE
  224. #endif
  225. #ifdef TERSE
  226.         pad(just_a_sec);
  227. #endif
  228.     if (anytokill)            /* if there was anything to kill */
  229.         forcelast = FALSE;        /* allow for having killed it all */
  230.     firstart = oldfirst;
  231.     }
  232. }
  233.  
  234. void
  235. setthru(thru)
  236. ART_NUM thru;
  237. {
  238.     FILE *newkfp;
  239.     bool no_kills = 0;
  240. #ifdef USETHREADS
  241.     int i;
  242.     ART_NUM kill_thread;
  243. #endif
  244.  
  245.     fseek(localkfp,0L,0);        /* rewind current file */
  246.     if (fgets(buf,LBUFLEN,localkfp) != Nullch
  247.      && (strnNE(buf,"THRU",4) || fgets(buf,LBUFLEN,localkfp) != Nullch))
  248.     fseek(localkfp,0L,0);
  249.     else
  250.     no_kills = 1;
  251.     strcpy(buf,filexp(getval("KILLLOCAL",killlocal)));
  252.     UNLINK(buf);            /* to prevent file reuse */
  253.     if (no_kills)
  254.     open_kfile(KF_LOCAL);        /* close file and reset open flag */
  255.     else if (newkfp = fopen(buf,"w")) {
  256.     fprintf(newkfp,"THRU %ld\n",(long)thru);
  257.     while (fgets(buf,LBUFLEN,localkfp) != Nullch) {
  258.         if (strnEQ(buf,"THRU",4))
  259.         continue;
  260. #ifdef USETHREADS
  261.         /* Leave out any outdated thread kills */
  262.         if (*buf == 'T' && p_roots) {
  263.         kill_thread = atol(buf+1);
  264.         for (i = 0; i < total.root; i++) {
  265.             if (p_roots[i].root_num == kill_thread) {
  266.             break;
  267.             }
  268.         }
  269.         if (i == total.root)
  270.             continue;
  271.         }
  272. #endif
  273.         fputs(buf,newkfp);
  274.     }
  275.     fclose(newkfp);
  276.     open_kfile(KF_LOCAL);        /* and reopen local file */
  277.     }
  278.     else
  279.     printf(cantcreate,buf) FLUSH;
  280. }
  281.  
  282. /* edit KILL file for newsgroup */
  283.  
  284. int
  285. edit_kfile()
  286. {
  287.     int r = -1;
  288.  
  289.     if (in_ng)
  290.     strcpy(buf,filexp(getval("KILLLOCAL",killlocal)));
  291.     else
  292.     strcpy(buf,filexp(getval("KILLGLOBAL",killglobal)));
  293.     if ((r = makedir(buf,MD_FILE)) >= 0) {
  294.     sprintf(cmd_buf,"%s %s",
  295.         filexp(getval("VISUAL",getval("EDITOR",defeditor))),buf);
  296.     printf("\nEditing %s KILL file:\n%s\n",
  297.         (in_ng?"local":"global"),cmd_buf) FLUSH;
  298.     resetty();            /* make sure tty is friendly */
  299.     r = doshell(sh,cmd_buf);/* invoke the shell */
  300.     noecho();            /* and make terminal */
  301.     crmode();            /*   unfriendly again */
  302.     open_kfile(in_ng);
  303.     }
  304.     else
  305.     printf("Can't make %s\n",buf) FLUSH;
  306.     return r;
  307. }
  308.  
  309. void
  310. open_kfile(local)
  311. int local;
  312. {
  313.     char *kname = filexp(local ?
  314.     getval("KILLLOCAL",killlocal) :
  315.     getval("KILLGLOBAL",killglobal)
  316.     );
  317.     
  318.     stat(kname,&filestat);
  319.     if (!filestat.st_size)        /* nothing in the file? */
  320.     UNLINK(kname);            /* delete the file */
  321.     if (local) {
  322.     if (localkfp)
  323.         fclose(localkfp);
  324.     localkfp = fopen(kname,"r");
  325.     }
  326.     else {
  327.     if (globkfp)
  328.         fclose(globkfp);
  329.     globkfp = fopen(kname,"r");
  330.     }
  331. }
  332.  
  333. void
  334. kf_append(cmd)
  335. char *cmd;
  336. {
  337.     strcpy(cmd_buf,filexp(getval("KILLLOCAL",killlocal)));
  338.     if (makedir(cmd_buf,MD_FILE) >= 0) {
  339. #ifdef VERBOSE
  340.     IF(verbose)
  341.         printf("\nDepositing command in %s...",cmd_buf);
  342.     ELSE
  343. #endif
  344. #ifdef TERSE
  345.         printf("\n--> %s...",cmd_buf);
  346. #endif
  347.     fflush(stdout);
  348.     sleep(2);
  349.     if ((tmpfp = fopen(cmd_buf,"a")) != Nullfp) {
  350.         fseek(tmpfp,0L,2);        /* get to EOF for sure */
  351.         fprintf(tmpfp,"%s\n",cmd);
  352.         fclose(tmpfp);
  353.         fputs("done\n",stdout) FLUSH;
  354.     }
  355.     else
  356.         printf(cantopen,cmd_buf) FLUSH;
  357.     }
  358. }
  359. #endif /* KILLFILES */
  360.