home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume30 / tin / part10 / feed.c next >
C/C++ Source or Header  |  1992-05-20  |  15KB  |  580 lines

  1. /*
  2.  *  Project   : tin - a threaded Netnews reader
  3.  *  Module    : feed.c
  4.  *  Author    : I.Lea
  5.  *  Created   : 31-08-91
  6.  *  Updated   : 12-05-92
  7.  *  Notes     : provides same interface to mail,pipe,print and save commands
  8.  *  Copyright : (c) Copyright 1991-92 by Iain Lea
  9.  *              You may  freely  copy or  redistribute  this software,
  10.  *              so  long as there is no profit made from its use, sale
  11.  *              trade or  reproduction.  You may not change this copy-
  12.  *              right notice, and it must be included in any copy made
  13.  */
  14.  
  15. #include    "tin.h"
  16.  
  17. extern char *glob_group;            /* Group name */
  18. extern char note_h_date[LEN];        /* Date:    */
  19. extern char note_h_newsgroups[LEN];    /* Newsgroups:    */
  20. extern char note_h_subj[LEN];        /* Subject:    */
  21. extern FILE *note_fp;                /* the body of the current article */
  22. extern int note_end;                /* end of article ? */
  23. extern int note_page;                /* what page we're on */
  24. extern long note_mark[MAX_PAGES];    /* ftells on beginnings of pages */
  25.  
  26. char default_mail_address[LEN];
  27. char default_pipe_command[LEN];
  28. char default_save_file[PATH_LEN];
  29. char default_regex_pattern[LEN];
  30. char default_crosspost_group[LEN];
  31. char proc_ch_default;                /* set in change_rcfile () */
  32.  
  33.  
  34. void feed_articles (function, level, prompt, respnum, group_path)
  35.     int function;
  36.     int level;
  37.     char *prompt;
  38.     int respnum;
  39.     char *group_path;
  40. {
  41. #ifndef INDEX_DAEMON
  42.  
  43.     char address[LEN];
  44.     char command[LEN];
  45.     char filename[PATH_LEN], *p;
  46.     char group[LEN];
  47.     char mailbox[LEN];
  48.     char pattern[LEN];
  49.     char ch = 'a', ch_default = 'a';
  50.     char proc_ch = proc_ch_default;
  51.     FILE *fp = (FILE *) 0;
  52.     int b, i, j;
  53.     int confirm = TRUE;
  54.     int processed_ok = TRUE;
  55.     int proceed = FALSE;
  56.     int is_mailbox = FALSE;
  57.     int orig_note_end = 0;
  58.     int orig_note_page = 0;
  59.     int processed = 0;
  60.     int ret1 = FALSE;
  61.     int ret2 = FALSE;
  62.     int redraw_screen = FALSE;
  63.     
  64.     if (level == PAGE_LEVEL) {
  65.         orig_note_end = note_end;
  66.         orig_note_page = note_page;
  67.     }
  68.  
  69.     b = which_thread (respnum);
  70.  
  71.     /*
  72.      * try and work out what default the user wants
  73.      */
  74.     if (num_of_tagged_arts) {
  75.         ch_default = 'T';
  76.     } else if (num_of_hot_arts) {
  77.         ch_default = 'h';
  78.     } else if (num_of_responses (b)) {
  79.         ch_default = 't';
  80.     }
  81.  
  82.     if ((save_archive_name == FALSE || arts[respnum].archive == (char *) 0) ||
  83.         (save_archive_name == TRUE && function != FEED_SAVE) ||
  84.         ch_default == 'T') {
  85.         do {
  86.             sprintf (msg, "%s%s%c", prompt, txt_art_thread_regex_tag, ch_default);
  87.             wait_message (msg);
  88.             MoveCursor (LINES, (int) strlen (msg)-1);
  89.             if ((ch = (char) ReadCh ()) == CR)
  90.                 ch = ch_default;
  91.         } while (ch != ESC && ch != 'a' && ch != 't' && ch != 'T' && 
  92.             ch != 'h' && ch != 'p' && ch != 'q');
  93.     } else {
  94.         filename[0] = '\0';
  95.         ch = ch_default;
  96.         if (proc_ch != 'n') {
  97.             if (str_str (glob_group, "sources", 7)) {
  98.                 proc_ch = 's';        /* *source* group */ 
  99.             } else if (str_str (glob_group, "binaries", 8)) {
  100.                 proc_ch = 'u';        /* *binaries* group */
  101.             } else {
  102.                 proc_ch = 's';
  103.             }
  104.         }    
  105.     }
  106.  
  107.     if (ch == 'q' || ch == ESC) {    /* exit */
  108.         clear_message ();
  109.         return;
  110.     }
  111.     
  112.     if (ch == 'p') {
  113.         sprintf (msg, txt_feed_pattern, default_regex_pattern);
  114.         if (! prompt_string (msg, pattern)) {
  115.             clear_message ();
  116.             return;
  117.         }    
  118.         if (strlen (pattern)) {
  119.             my_strncpy (default_regex_pattern, pattern, 
  120.                 sizeof (default_regex_pattern));
  121.         } else {
  122.             if (default_regex_pattern[0]) {
  123.                 my_strncpy (pattern, default_regex_pattern, 
  124.                     sizeof (default_regex_pattern));
  125.             } else {
  126.                 info_message (txt_no_match);
  127.                 return;
  128.             }
  129.         }
  130.     }
  131.  
  132.     switch (function) {
  133.         case FEED_MAIL:
  134.             sprintf (msg, txt_mail_art_to, 
  135.                 COLS-(strlen(txt_mail_art_to)+30), default_mail_address);
  136.             if (! prompt_string (msg, address)) {
  137.                 clear_message ();
  138.                 return;
  139.             }    
  140.             if (strlen (address)) {
  141.                 strcpy (default_mail_address, address);
  142.             } else {
  143.                 if (default_mail_address[0]) {
  144.                     strcpy (address, default_mail_address);
  145.                 } else {
  146.                     info_message (txt_no_mail_address);    
  147.                     return;
  148.                 }
  149.             }
  150.             break;
  151.         case FEED_PIPE:
  152.             sprintf (msg, txt_pipe_to_command, 
  153.                 COLS-(strlen(txt_pipe_to_command)+30), default_pipe_command);
  154.             if (! prompt_string (msg, command)) {
  155.                 clear_message ();
  156.                 return;
  157.             }
  158.             if (strlen (command)) {
  159.                 strcpy (default_pipe_command, command);
  160.             } else {
  161.                 if (default_pipe_command[0]) {
  162.                     strcpy (command, default_pipe_command);
  163.                 } else {
  164.                     info_message (txt_no_command);    
  165.                     return;
  166.                 }
  167.             }
  168.  
  169.             if ((fp = (FILE *) popen (command, "w")) == NULL) {
  170.                 perror_message (txt_command_failed_s, command);
  171.                 return;
  172.             }
  173.             wait_message (txt_piping);
  174.             Raw (FALSE);
  175.             break;
  176.         case FEED_PRINT:    
  177.             if (default_printer) {
  178. #ifdef sinix
  179.                     sprintf (command, "%s -dru=%s %s",
  180.                         printer, get_val ("PRINTER","ps"), redirect_output);
  181. #else
  182.                     sprintf (command, "%s -P%s %s",
  183.                         printer, get_val ("PRINTER","ps"), redirect_output);
  184. #endif
  185.             } else {
  186.                 if (cmd_line_printer[0]) {
  187.                     sprintf (command, "%s %s",
  188.                         cmd_line_printer, redirect_output);
  189.                 } else {
  190.                     sprintf (command, "%s %s",
  191.                         printer, redirect_output);
  192.                 }
  193.             }
  194.             if ((fp = (FILE *) popen (command, "w")) == NULL) {
  195.                 perror_message (txt_command_failed_s, command);
  196.                 return;
  197.             }
  198.             break;
  199.         case FEED_SAVE:        /* ask user for filename */
  200.             free_save_array ();
  201.             if ((save_archive_name == FALSE || arts[respnum].archive == (char *) 0)) {
  202.                 sprintf (msg, txt_save_filename, default_save_file);
  203.                 if (! prompt_string (msg, filename)) {
  204.                     clear_message ();
  205.                     return;
  206.                 }
  207.                 if (strlen (filename)) {
  208.                     my_strncpy (default_save_file, filename,
  209.                         sizeof (default_save_file));
  210.                 } else {
  211.                     if (default_save_file[0]) {
  212.                         my_strncpy (filename, default_save_file,
  213.                             sizeof (filename));
  214.                     } else {
  215.                         info_message (txt_no_filename);    
  216.                         return;
  217.                     }
  218.                 }
  219.                 for (p = filename; *p && (*p == ' ' || *p == '\t'); p++) {
  220.                     continue;
  221.                 }
  222.                 if (! *p) {
  223.                     info_message (txt_no_filename);
  224.                     return;
  225.                 }
  226.                 if ((filename[0] == '~' || filename[0] == '+') && strlen (filename) == 1) {
  227.                     info_message (txt_no_filename);
  228.                     return;
  229.                 }
  230.                 is_mailbox = create_path (filename);
  231.                 if (is_mailbox) {
  232.                     if ((int) strlen (filename) > 1) {
  233.                         my_strncpy (mailbox, filename+1, sizeof (mailbox));        
  234.                     } else {
  235.                         my_strncpy (mailbox, glob_group, sizeof (mailbox));
  236.                         /*
  237.                          *  convert 1st letter to uppercase
  238.                          */
  239.                         if (mailbox[0] >= 'a' && mailbox[0] <= 'z') {
  240.                             mailbox[0] = mailbox[0] - 32;
  241.                         }
  242.                     }
  243.                     my_strncpy (filename, mailbox, sizeof (filename));
  244.                 } else {        /* ask for post processing type */
  245.                     do {
  246.                         sprintf (msg, "%s%c", txt_post_process_type, proc_ch_default);
  247.                         wait_message (msg);
  248.                         MoveCursor (LINES, (int) strlen (msg)-1);
  249.                         if ((proc_ch = (char) ReadCh ()) == CR)
  250.                             proc_ch = proc_ch_default;
  251.                     } while (proc_ch != 'n' && proc_ch != 's' &&
  252.                          proc_ch != 'u' && proc_ch != 'U');
  253.                 }
  254.             }
  255.             clear_message ();
  256.             break;
  257.         case FEED_XPOST:    /* ask user for newsgroups */
  258.             sprintf (msg, txt_crosspost_group, default_crosspost_group);
  259.     
  260.             if (! prompt_string (msg, group)) {
  261.                 clear_message ();
  262.                 return;
  263.             }
  264.  
  265.             if (strlen (group)) {
  266.                 my_strncpy (default_crosspost_group, group,
  267.                     sizeof (default_crosspost_group));
  268.             } else {
  269.                 if (default_crosspost_group[0]) {
  270.                     my_strncpy (group, default_crosspost_group, 
  271.                         sizeof (group));
  272.                 } else {
  273.                     info_message (txt_no_group);
  274.                     return;
  275.                 }
  276.             }
  277.             break;
  278.     }
  279.     
  280.     switch (ch) {
  281.         case 'a':        /* article */
  282.             if (level == GROUP_LEVEL) {
  283.                 note_page = art_open (arts[respnum].artnum, group_path);    
  284.             }
  285.             switch (function) {
  286.                 case FEED_MAIL:
  287.                     redraw_screen = mail_to_someone (address, TRUE, &processed_ok);
  288.                     break;
  289.                 case FEED_PIPE:
  290.                     fseek (note_fp, 0L, 0);
  291.                     copy_fp (note_fp, fp, "");
  292.                     break;
  293.                 case FEED_PRINT:
  294.                     processed_ok = print_file (fp, respnum, 1);
  295.                     pclose (fp);        
  296.                     break;
  297.                 case FEED_SAVE:
  298.                     wait_message (txt_saving);
  299.                     add_to_save_list (0, &arts[respnum], is_mailbox, filename);
  300.                     processed_ok = save_art_to_file (respnum, 0, FALSE, "");
  301.                     break;
  302.                 case FEED_XPOST:
  303.                     redraw_screen = crosspost_article (group, respnum);
  304.                     break;
  305.             }
  306.             if (processed_ok) {
  307.                 processed++;
  308.             }    
  309.             if (mark_saved_read) {
  310.                 if (processed_ok) {
  311.                     arts[respnum].unread = ART_READ;
  312.                 }
  313.             }
  314.             if (level == GROUP_LEVEL) {
  315.                 art_close ();
  316.             }
  317.             break;
  318.             
  319.         case 't':         /* thread */
  320.             confirm = TRUE;
  321.             for (i = (int) base[b]; i >= 0; i = arts[i].thread) {
  322.                 if (function == FEED_PRINT) {
  323.                     if ((fp = (FILE *) popen (command, "w")) == NULL) {
  324.                         perror_message (txt_command_failed_s, command);
  325.                         return;
  326.                     }
  327.                 }
  328.                 if (level == PAGE_LEVEL) {
  329.                     art_close ();
  330.                 }
  331.                 note_page = art_open (arts[i].artnum, group_path);    
  332.                 switch (function) {
  333.                     case FEED_MAIL:
  334.                         processed_ok = TRUE;    
  335.                         mail_to_someone (address, confirm, &processed_ok);
  336.                         confirm = FALSE;
  337.                         break;
  338.                     case FEED_PIPE:
  339.                         fseek (note_fp, 0L, 0);
  340.                         copy_fp (note_fp, fp, "");
  341.                         break;
  342.                     case FEED_PRINT:
  343.                         processed_ok = print_file (fp, i, processed+1);
  344.                         pclose (fp);
  345.                         break;
  346.                     case FEED_SAVE:
  347.                         add_to_save_list (i, &arts[i], is_mailbox, filename);
  348.                         break;
  349.                     case FEED_XPOST:
  350.                         redraw_screen = crosspost_article (group, i);
  351.                         break;
  352.                 }
  353.                 if (processed_ok) {
  354.                     processed++;
  355.                 }
  356.                 if (mark_saved_read) {
  357.                     if (processed_ok) {
  358.                         arts[i].unread = ART_READ;
  359.                     }    
  360.                 }
  361.                 art_close ();
  362.             }
  363.             if (function == FEED_SAVE) {
  364.                 sort_save_list ();
  365.                 (void) save_thread_to_file (is_mailbox, group_path);
  366.             }
  367.             break;
  368.  
  369.         case 'T':         /* tagged articles */
  370.             confirm = TRUE;
  371.             for (i=1 ; i <= num_of_tagged_arts ; i++) {
  372.                 for (j=0 ; j < top ; j++) {
  373.                     if (arts[j].tagged && arts[j].tagged == i) { 
  374.  
  375.                         if (function == FEED_PRINT) {
  376.                             if ((fp = (FILE *) popen (command, "w")) == NULL) {
  377.                                 perror_message (txt_command_failed_s, command);
  378.                                 return;
  379.                             }
  380.                         }
  381.                         if (level == PAGE_LEVEL) {
  382.                             art_close ();
  383.                         }
  384.                         note_page = art_open (arts[j].artnum, group_path);    
  385.                         switch (function) {
  386.                             case FEED_MAIL:
  387.                                 processed_ok = TRUE;
  388.                                 mail_to_someone (address, confirm, &processed_ok);
  389.                                 confirm = FALSE;
  390.                                 break;
  391.                             case FEED_PIPE:
  392.                                 fseek (note_fp, 0L, 0);
  393.                                 copy_fp (note_fp, fp, "");
  394.                                 break;
  395.                             case FEED_PRINT:
  396.                                 processed_ok = print_file (fp, j, processed+1);
  397.                                 pclose (fp);
  398.                                 break;
  399.                             case FEED_SAVE:
  400.                                 add_to_save_list (j, &arts[j], is_mailbox, filename);
  401.                                 break;
  402.                             case FEED_XPOST:
  403.                                 redraw_screen = crosspost_article (group, j);
  404.                                 break;
  405.                         }
  406.                         if (processed_ok) {
  407.                             processed++;
  408.                         }    
  409.                         if (mark_saved_read) {
  410.                             if (processed_ok) {
  411.                                 arts[j].unread = ART_READ;
  412.                             }    
  413.                         }
  414.                         art_close ();
  415.                     }
  416.                 }
  417.             }
  418.             if (function == FEED_SAVE) {                
  419.                 (void) save_regex_arts (is_mailbox, group_path);
  420.             }
  421.             untag_all_articles ();
  422.             break;
  423.  
  424.         case 'h':         /* hot (auto-selected) articles */
  425.         case 'p':         /* regex pattern matched articles */
  426.             confirm = TRUE;
  427.             for (i = 0 ; i < top_base ; i++) {
  428.                 for (j = (int) base[i]; j >= 0; j = arts[j].thread) {
  429.                     if (ch == 'p') {
  430.                         if (STR_MATCH(arts[j].subject, pattern)) {
  431.                             proceed = TRUE;
  432.                         }
  433.                     } else if (arts[j].hot) {
  434.                         proceed = TRUE;
  435.                     }                
  436.                     if (proceed) {
  437.                         proceed = FALSE;
  438.                         if (function == FEED_PRINT) {
  439.                             if ((fp = (FILE *) popen (command, "w")) == NULL) {
  440.                                 perror_message (txt_command_failed_s, command);
  441.                                 return;
  442.                             }
  443.                         }
  444.                         if (level == PAGE_LEVEL) {
  445.                             art_close ();
  446.                         }
  447.                         note_page = art_open (arts[j].artnum, group_path);    
  448.                         switch (function) {
  449.                             case FEED_MAIL:
  450.                                 processed_ok = TRUE;
  451.                                 mail_to_someone (address, confirm, &processed_ok);
  452.                                 /* confirm = FALSE; */
  453.                                 break;
  454.                             case FEED_PIPE:
  455.                                 fseek (note_fp, 0L, 0);
  456.                                 copy_fp (note_fp, fp, "");
  457.                                 break;
  458.                             case FEED_PRINT:
  459.                                 processed_ok = print_file (fp, j, processed+1);
  460.                                 pclose (fp);
  461.                                 break;
  462.                             case FEED_SAVE:
  463.                                 sprintf (filename, "%s.%02d", filename, processed+1);
  464.                                 add_to_save_list (0, &arts[j], is_mailbox, filename);
  465.                                 processed_ok = save_art_to_file (respnum, 0, FALSE, "");
  466.                                 break;
  467.                             case FEED_XPOST:
  468.                                 redraw_screen = crosspost_article (group, j);
  469.                                 break;
  470.                         }
  471.                         if (processed_ok) {
  472.                             processed++;
  473.                         }    
  474.                         if (mark_saved_read) {
  475.                             if (processed_ok) {
  476.                                 arts[j].unread = ART_READ;
  477.                                 if (ch == 'h') {
  478.                                     arts[j].hot = FALSE;
  479.                                     num_of_hot_arts--;
  480.                                 }    
  481.                             }    
  482.                         }
  483.                          art_close ();
  484.                     }
  485.                 }    
  486.             }
  487.             break;
  488.     }
  489.  
  490.     redraw_screen = mail_check ();    /* in case of sending to oneself */
  491.  
  492.     switch (function) {
  493.         case FEED_PIPE:
  494.             pclose (fp);        
  495.             Raw (TRUE);
  496.             continue_prompt ();
  497.             redraw_screen = TRUE;
  498.             break;
  499.         case FEED_SAVE:
  500.             if (proc_ch != 'n' && is_mailbox == FALSE) {
  501.                 ret2 = post_process_files (proc_ch);
  502.             }
  503.             free_save_array ();
  504.             break;
  505.     }
  506.  
  507.     if (level == GROUP_LEVEL) {
  508.         ret1 = (mark_saved_read ? TRUE : FALSE);
  509.     }
  510.     if ((ret1 || ret2) && is_mailbox == FALSE) {
  511.         redraw_screen = TRUE;
  512.     }
  513.  
  514.     if (level == PAGE_LEVEL) {
  515.         if (ch != 'a') {
  516.             note_page = art_open (arts[respnum].artnum, group_path);
  517.         }
  518.         note_end = orig_note_end;
  519.         note_page = orig_note_page;
  520.         fseek (note_fp, note_mark[note_page], 0);
  521.         if (redraw_screen) {
  522.             if (note_page == 0) {
  523.                 show_note_page (respnum, glob_group);
  524.             } else {
  525.                 redraw_page (respnum, glob_group);
  526.             }
  527.         } else {
  528.             if (function == FEED_PIPE) {
  529.                 clear_message ();
  530.             }
  531.         }
  532.     } else {
  533.         if (redraw_screen) {
  534.             show_group_page (glob_group);
  535.         }
  536.     }
  537.     if (function == FEED_MAIL) {    
  538.         sprintf (msg, txt_mailed, processed);
  539.         info_message (msg);
  540.     } else if (function == FEED_PRINT) {    
  541.         sprintf (msg, txt_printed, processed);
  542.         info_message (msg);
  543.     } else if (function == FEED_SAVE) {    
  544.         if (ch == 'a') {
  545.             sprintf (msg, txt_saved, processed);
  546.             info_message (msg);
  547.         }    
  548.     }
  549.  
  550. #endif /* INDEX_DAEMON */
  551. }
  552.  
  553.  
  554. int print_file (fp, respnum, count)
  555.     FILE *fp;
  556.     int respnum;
  557.     int count;
  558. {                            
  559.     sprintf (msg, "%s%d", txt_printing, count);
  560.     wait_message (msg);
  561.     
  562.     if (print_header) {
  563.         fseek(note_fp, 0L, 0);
  564.     } else {
  565.         fprintf (fp, "Newsgroups: %s\n", note_h_newsgroups);
  566.         if (arts[respnum].from == arts[respnum].name) {
  567.             fprintf (fp, "From: %s\n", arts[respnum].from);
  568.         } else {
  569.             fprintf (fp, "From: %s (%s)\n",
  570.                 arts[respnum].from, arts[respnum].name);
  571.         }        
  572.         fprintf (fp, "Subject: %s\n", note_h_subj);
  573.         fprintf (fp, "Date: %s\n\n", note_h_date);
  574.         fseek (note_fp, note_mark[0], 0);
  575.     }
  576.     copy_fp (note_fp, fp, "");
  577.  
  578.     return (TRUE);    /* a hack that will check if file was really checked later */
  579. }                        
  580.