home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rn_4_3_blars.lzh / respond.c < prev    next >
Text File  |  1990-08-25  |  10KB  |  447 lines

  1. /* $Header: respond.c,v 4.3.2.3 90/03/22 23:05:19 sob Exp $
  2.  *
  3.  * $Log:    respond.c,v $
  4.  * Revision 4.3.2.3  90/03/22  23:05:19  sob
  5.  * Fixes provided by Wayne Davison <drivax!davison>
  6.  * 
  7.  * Revision 4.3.2.2  89/11/26  18:25:10  sob
  8.  * Enlarged the size of the header buffer to accomodate long references lines.
  9.  * Fix provided by Joe Buck.
  10.  * 
  11.  * Revision 4.3.2.1  89/11/06  01:00:26  sob
  12.  * Added RRN support from NNTP 1.5
  13.  * 
  14.  * Revision 4.3.1.5  85/09/10  11:05:00  lwall
  15.  * Improved %m in in_char().
  16.  * 
  17.  * Revision 4.3.1.4  85/05/23  17:24:49  lwall
  18.  * Now allows 'r' and 'f' on null articles.
  19.  * 
  20.  * Revision 4.3.1.3  85/05/15  14:42:32  lwall
  21.  * Removed duplicate include of intrp.h.
  22.  * 
  23.  * Revision 4.3.1.2  85/05/14  08:55:15  lwall
  24.  * Default for normal/mailbox question was applied to wrong buffer.
  25.  * 
  26.  * Revision 4.3.1.1  85/05/10  11:37:33  lwall
  27.  * Branch for patches.
  28.  * 
  29.  * Revision 4.3  85/05/01  11:47:04  lwall
  30.  * Baseline for release with 4.3bsd.
  31.  * 
  32.  */
  33.  
  34. #include "EXTERN.h"
  35. #include "common.h"
  36. #include "intrp.h"
  37. #include "head.h"
  38. #include "term.h"
  39. #include "ng.h"
  40. #include "util.h"
  41. #include "rn.h"
  42. #include "artio.h"
  43. #include "final.h"
  44. #include "INTERN.h"
  45. #include "respond.h"
  46. #include "fileinfo.h"
  47.  
  48. static char nullart[] = "\nNull article\n";
  49.  
  50. void
  51. respond_init()
  52. {
  53.     ;
  54. }
  55.  
  56. int
  57. save_article()
  58. {
  59.     bool use_pref;
  60.     register char *s, *c;
  61.     char altbuf[CBUFLEN];
  62.     int iter;
  63.     bool interactive = (buf[1] == FINISHCMD);
  64.     
  65.     if (!finish_command(interactive))    /* get rest of command */
  66.     return SAVE_ABORT;
  67.     use_pref = isupper(*buf);
  68. #ifdef ASYNC_PARSE
  69.     parse_maybe(art);
  70. #endif
  71.     savefrom = (*buf=='w' || *buf=='W' ? htype[PAST_HEADER].ht_minpos : 0);
  72.     if (artopen(art) == Nullfp) {
  73. #ifdef VERBOSE
  74.     IF(verbose)
  75.         fputs("\n\
  76. Saving null articles is not very productive!  :-)\n\
  77. ",stdout) FLUSH;
  78.     ELSE
  79. #endif
  80. #ifdef TERSE
  81.         fputs(nullart,stdout) FLUSH;
  82. #endif
  83.     return SAVE_DONE;
  84.     }
  85.     if (chdir(cwd)) {
  86.     printf(nocd,cwd) FLUSH;
  87.     sig_catcher(0);
  88.     }
  89.     if (savedest)
  90.     free(savedest);
  91.     if ((s = index(buf,'|')) != Nullch) {
  92.                 /* is it a pipe command? */
  93.     s++;            /* skip the | */
  94.     while (*s == ' ') s++;
  95.     safecpy(altbuf,filexp(s),sizeof altbuf);
  96.     savedest = altbuf;
  97.     interp(cmd_buf, (sizeof cmd_buf), getval("PIPESAVER",PIPESAVER));
  98.                 /* then set up for command */
  99.     resetty();        /* restore tty state */
  100.     if (use_pref)        /* use preferred shell? */
  101.         doshell(Nullch,cmd_buf);
  102.                 /* do command with it */
  103.     else
  104.         doshell(sh,cmd_buf);    /* do command with sh */
  105.     noecho();        /* and stop echoing */
  106.     crmode();        /* and start cbreaking */
  107.     savedest = savestr(savedest);
  108.     }
  109.     else {            /* normal save */
  110.     bool there, mailbox;
  111.     char *savename = getval("SAVENAME",SAVENAME);
  112.  
  113.     s = buf+1;        /* skip s or S */
  114.     if (*s == '-') {    /* if they are confused, skip - also */
  115. #ifdef VERBOSE
  116.         IF(verbose)
  117.         fputs("Warning: '-' ignored.  This isn't readnews.\n",stdout)
  118.           FLUSH;
  119.         ELSE
  120. #endif
  121. #ifdef TERSE
  122.         fputs("'-' ignored.\n",stdout) FLUSH;
  123. #endif
  124.         s++;
  125.     }
  126.     for (; *s == ' '; s++);    /* skip spaces */
  127.     safecpy(altbuf,filexp(s),sizeof altbuf);
  128.     s = altbuf;
  129.     if (! index(s,'/')) {
  130.         interp(buf, (sizeof buf), getval("SAVEDIR",SAVEDIR));
  131.         if (makedir(buf,MD_DIR))    /* ensure directory exists */
  132.         strcpy(buf,cwd);
  133.         if (*s) {
  134.         for (c = buf; *c; c++) ;
  135.         *c++ = '/';
  136.         strcpy(c,s);        /* add filename */
  137.         }
  138.         s = buf;
  139.     }
  140.     for (iter = 0;
  141.         (there = stat(s,&filestat) >= 0) &&
  142.         (filestat.st_mode & S_IFDIR);
  143.         iter++) {            /* is it a directory? */
  144.  
  145.         c = (s+strlen(s));
  146.         *c++ = '/';            /* put a slash before filename */
  147.         interp(c, s==buf?(sizeof buf):(sizeof altbuf),
  148.         iter ? "News" : savename );
  149.                 /* generate a default name somehow or other */
  150.         if (index(c,'/')) {        /* yikes, a '/' in the filename */
  151.         makedir(s,MD_FILE);
  152.         }
  153.     }
  154.     if (*s != '/') {        /* relative path? */
  155.         c = (s==buf ? altbuf : buf);
  156.         sprintf(c, "%s/%s", cwd, s);
  157.         s = c;            /* absolutize it */
  158.     }
  159.     s = savedest = savestr(s);    /* doesn't move any more */
  160.                     /* make it handy for %b */
  161.     if (!there) {
  162.         if (mbox_always)
  163.         mailbox = TRUE;
  164.         else if (norm_always)
  165.         mailbox = FALSE;
  166.         else {
  167.         char *dflt = (instr(savename,"%a") ? "nyq" : "ynq");
  168.         
  169.         sprintf(cmd_buf,
  170.         "\nFile %s doesn't exist--\n    use mailbox format? [%s] ",
  171.           s,dflt);
  172.           reask_save:
  173.         in_char(cmd_buf, 'M');
  174.         putchar('\n') FLUSH;
  175.         setdef(buf,dflt);
  176. #ifdef VERIFY
  177.         printcmd();
  178. #endif
  179.         if (*buf == 'h') {
  180. #ifdef VERBOSE
  181.             IF(verbose)
  182.             printf("\n\
  183. Type y to create %s as a mailbox.\n\
  184. Type n to create it as a normal file.\n\
  185. Type q to abort the save.\n\
  186. ",s) FLUSH;
  187.             ELSE
  188. #endif
  189. #ifdef TERSE
  190.             fputs("\n\
  191. y to create mailbox.\n\
  192. n to create normal file.\n\
  193. q to abort.\n\
  194. ",stdout) FLUSH;
  195. #endif
  196.             goto reask_save;
  197.         }
  198.         else if (*buf == 'n') {
  199.             mailbox = FALSE;
  200.         }
  201.         else if (*buf == 'y') {
  202.             mailbox = TRUE;
  203.         }
  204.         else if (*buf == 'q') {
  205.             goto s_bomb;
  206.         }
  207.         else {
  208.             fputs(hforhelp,stdout) FLUSH;
  209.             settle_down();
  210.             goto reask_save;
  211.         }
  212.         }
  213.     }
  214.     else if (filestat.st_mode & S_IFCHR)
  215.         mailbox = FALSE;
  216.     else {
  217.         int tmpfd;
  218.         
  219.         tmpfd = open(s, OPEN_READ);
  220.         if (tmpfd == -1)
  221.         mailbox = FALSE;
  222.         else {
  223.         read(tmpfd,buf,LBUFLEN);
  224.         c = buf;
  225.         if (!isspace(MBOXCHAR))
  226.             while (isspace(*c))
  227.             c++;
  228.         mailbox = (*c == MBOXCHAR);
  229.         close(tmpfd);
  230.         }
  231.     }
  232.  
  233.     safecpy(cmd_buf, filexp(mailbox ?
  234.         getval("MBOXSAVER",MBOXSAVER) :
  235.         getval("NORMSAVER",NORMSAVER) ), sizeof cmd_buf);
  236.                 /* format the command */
  237.     resetty();        /* make terminal behave */
  238.     if (doshell(use_pref?Nullch:SH,cmd_buf))
  239.         fputs("Not saved",stdout);
  240.     else
  241.         printf("%s to %s %s",
  242.           there?"Appended":"Saved",
  243.           mailbox?"mailbox":"file",
  244.           s);
  245.     if (interactive)
  246.         putchar('\n') FLUSH;
  247.     noecho();        /* make terminal do what we want */
  248.     crmode();
  249.     }
  250. s_bomb:
  251. #ifdef SERVER
  252.     if (chdir(spool)) {
  253. #else not SERVER
  254.     if (chdir(spool) || chdir(ngdir)) {
  255. #endif SERVER
  256.     printf(nocd,ngdir) FLUSH;
  257.     sig_catcher(0);
  258.     }
  259.     return SAVE_DONE;
  260. }
  261.  
  262. int
  263. cancel_article()
  264. {
  265.     char *artid_buf;
  266.     char *ngs_buf;
  267.     char *from_buf;
  268.     char *reply_buf;
  269.     int myuid = getuid();
  270.     int r = -1;
  271.  
  272.     if (artopen(art) == Nullfp) {
  273. #ifdef VERBOSE
  274.     IF(verbose)
  275.         fputs("\n\
  276. Cancelling null articles is your idea of fun?  :-)\n\
  277. ",stdout) FLUSH;
  278.     ELSE
  279. #endif
  280. #ifdef TERSE
  281.         fputs(nullart,stdout) FLUSH;
  282. #endif
  283.     return r;
  284.     }
  285.     reply_buf = fetchlines(art,REPLY_LINE);
  286.     from_buf = fetchlines(art,FROM_LINE);
  287.     artid_buf = fetchlines(art,ARTID_LINE);
  288.     ngs_buf = fetchlines(art,NGS_LINE);
  289.     if (!instr(from_buf,sitename) ||
  290.     (!instr(from_buf,logname) &&
  291.      !instr(reply_buf,logname) &&
  292. #ifdef NEWSADMIN
  293.      myuid != newsuid &&
  294. #endif
  295.      myuid != ROOTID ) )
  296. #ifdef VERBOSE
  297.         IF(verbose)
  298.         fputs("\nYou can't cancel someone else's article\n",stdout)
  299.           FLUSH;
  300.         ELSE
  301. #endif
  302. #ifdef TERSE
  303.         fputs("\nNot your article\n",stdout) FLUSH;
  304. #endif
  305.     else {
  306.     tmpfp = fopen(headname,"w");    /* open header file */
  307.     if (tmpfp == Nullfp) {
  308.         printf(cantcreate,headname) FLUSH;
  309.         goto no_cancel;
  310.     }
  311.     interp(buf, (sizeof buf), getval("CANCELHEADER",CANCELHEADER));
  312.     fputs(buf,tmpfp);
  313.     fclose(tmpfp);
  314.     fputs("\nCanceling...\n",stdout) FLUSH;
  315.     r = doshell(sh,filexp(getval("CANCEL",CANCEL)));
  316.     }
  317. no_cancel:
  318.     free(artid_buf);
  319.     free(ngs_buf);
  320.     free(from_buf);
  321.     free(reply_buf);
  322.     return r;
  323. }
  324.  
  325. void
  326. reply()
  327. {
  328.     bool incl_body = (*buf == 'R');
  329.     char *maildoer = savestr(filexp(getval("MAILPOSTER",MAILPOSTER)));
  330.  
  331.     artopen(art);
  332.     tmpfp = fopen(headname,"w");    /* open header file */
  333.     if (tmpfp == Nullfp) {
  334.     printf(cantcreate,headname) FLUSH;
  335.     goto no_reply;
  336.     }
  337.     interp(buf, (sizeof buf), getval("MAILHEADER",MAILHEADER));
  338.     fputs(buf,tmpfp);
  339.     if (!instr(maildoer,"%h"))
  340. #ifdef VERBOSE
  341.     IF(verbose)
  342.         printf("\n%s\n(Above lines saved in file %s)\n",buf,headname)
  343.           FLUSH;
  344.     ELSE
  345. #endif
  346. #ifdef TERSE
  347.         printf("\n%s\n(Header in %s)\n",buf,headname) FLUSH;
  348. #endif
  349.     if (incl_body && artfp != Nullfp) {
  350.     interp(buf, (sizeof buf), getval("YOUSAID",YOUSAID));
  351.     fprintf(tmpfp,"%s\n",buf);
  352. #ifdef ASYNC_PARSE
  353.     parse_maybe(art);
  354. #endif
  355.     fseek(artfp,(long)htype[PAST_HEADER].ht_minpos,0);
  356.     while (fgets(buf,LBUFLEN,artfp) != Nullch) {
  357.         fprintf(tmpfp,"%s%s",indstr,buf);
  358.     }
  359.     fprintf(tmpfp,"\n");
  360.     }
  361.     fclose(tmpfp);
  362.     interp(cmd_buf, (sizeof cmd_buf), maildoer);
  363.     invoke(cmd_buf,origdir);
  364.     UNLINK(headname);        /* kill the header file */
  365. no_reply:
  366.     free(maildoer);
  367. }
  368.  
  369. void
  370. followup()
  371. {
  372.     bool incl_body = (*buf == 'F');
  373.     char hbuf[4*LBUFLEN];    /* four times the old size */
  374.  
  375.     artopen(art);
  376.     tmpfp = fopen(headname,"w");
  377.     if (tmpfp == Nullfp) {
  378.     printf(cantcreate,headname) FLUSH;
  379.     return;
  380.     }
  381.     interp(hbuf, (sizeof hbuf), getval("NEWSHEADER",NEWSHEADER));
  382.     fprintf(tmpfp,"%s",hbuf);
  383.     if (incl_body && artfp != Nullfp) {
  384. #ifdef VERBOSE
  385.     if (verbose)
  386.         fputs("\n\
  387. (Be sure to double-check the attribution against the signature, and\n\
  388. trim the quoted article down as much as possible.)\n\
  389. ",stdout) FLUSH;
  390. #endif
  391.     interp(buf, (sizeof buf), getval("ATTRIBUTION",ATTRIBUTION));
  392.     fprintf(tmpfp,"%s\n",buf);
  393. #ifdef ASYNC_PARSE
  394.     parse_maybe(art);
  395. #endif
  396.     fseek(artfp,(long)htype[PAST_HEADER].ht_minpos,0);
  397.     while (fgets(buf,LBUFLEN,artfp) != Nullch) {
  398.         fprintf(tmpfp,"%s%s",indstr,buf);
  399.     }
  400.     fprintf(tmpfp,"\n");
  401.     }
  402.     fclose(tmpfp);
  403.     safecpy(cmd_buf,filexp(getval("NEWSPOSTER",NEWSPOSTER)),sizeof cmd_buf);
  404.     invoke(cmd_buf,origdir);
  405.     UNLINK(headname);
  406. }
  407.  
  408. void
  409. invoke(cmd,dir)
  410. char *cmd,*dir;
  411. {
  412.     if (chdir(dir)) {
  413.     printf(nocd,dir) FLUSH;
  414.     return;
  415.     }
  416. #ifdef VERBOSE
  417.     IF(verbose)
  418.     printf("\n(leaving cbreak mode; cwd=%s)\nInvoking command: %s\n\n",
  419.         dir,cmd) FLUSH;
  420.     ELSE
  421. #endif
  422. #ifdef TERSE
  423.     printf("\n(-cbreak; cwd=%s)\nInvoking: %s\n\n",dir,cmd) FLUSH;
  424. #endif
  425.     resetty();            /* make terminal well-behaved */
  426.     doshell(sh,cmd);        /* do the command */
  427.     noecho();            /* set no echo */
  428.     crmode();            /* and cbreak mode */
  429. #ifdef VERBOSE
  430.     IF(verbose)
  431.     fputs("\n(re-entering cbreak mode)\n",stdout) FLUSH;
  432.     ELSE
  433. #endif
  434. #ifdef TERSE
  435.     fputs("\n(+cbreak)\n",stdout) FLUSH;
  436. #endif
  437. #ifdef SERVER
  438.     if (chdir(spool)) {
  439. #else not SERVER
  440.     if (chdir(spool) || chdir(ngdir)) {
  441. #endif SERVER
  442.     printf(nocd,ngdir) FLUSH;
  443.     sig_catcher(0);
  444.     }
  445. }
  446.  
  447.