Show
Ignore:
Timestamp:
2000-11-19 12:33:24 (8 years ago)
Author:
Thomas Roessler <roessler@…>
Branch:
HEAD
Message:

Introduce a new option named $print_split. While we are on it,
unify the pipe-message and print-message code, and fix the PGP
key-extraction code so it doesn't interact with OPTPIPEDECODE in
strange manners any more.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • commands.c

    r2161 r2189  
    11/* 
    22 * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu> 
     3 * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org> 
    34 *  
    45 *     This program is free software; you can redistribute it and/or modify 
     
    256257} 
    257258 
    258 static void pipe_print_set_flags (int decode, int *cmflags, int *chflags) 
     259static void pipe_set_flags (int decode, int *cmflags, int *chflags) 
    259260{ 
    260261  if (decode) 
     
    271272} 
    272273 
    273 void mutt_pipe_message_to_state (HEADER *h, STATE *s) 
     274void pipe_msg (HEADER *h, FILE *fp, int decode) 
    274275{ 
    275276  int cmflags = 0; 
    276277  int chflags = CH_FROM; 
    277278   
    278   pipe_print_set_flags (option (OPTPIPEDECODE), &cmflags, &chflags); 
    279    
    280   if (option (OPTPIPEDECODE)) 
     279  pipe_set_flags (decode, &cmflags, &chflags); 
     280 
     281#ifdef HAVE_PGP 
     282   
     283  if (decode && (h->pgp & PGPENCRYPT)) 
     284  { 
     285    if (!pgp_valid_passphrase()) 
     286      return; 
     287    endwin(); 
     288  } 
     289   
     290#endif 
     291 
     292  if (decode) 
    281293    mutt_parse_mime_message (Context, h); 
    282294 
    283   mutt_copy_message (s->fpout, Context, h, 
    284                      cmflags, chflags); 
    285 } 
    286  
    287 int mutt_pipe_message (HEADER *h) 
    288 { 
    289   STATE s; 
    290   char buffer[LONG_STRING]; 
    291   int i, rc = 0;  
     295  mutt_copy_message (fp, Context, h, cmflags, chflags); 
     296} 
     297 
     298 
     299/* the following code is shared between printing and piping */ 
     300 
     301static int _mutt_pipe_message (HEADER *h, char *cmd, 
     302                        int decode, 
     303                        int split, 
     304                        char *sep) 
     305{ 
     306   
     307  int i, rc = 0; 
    292308  pid_t thepid; 
    293  
    294   buffer[0] = 0; 
    295   if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD) 
    296       != 0 || !buffer[0]) 
    297     return 0; 
    298   mutt_expand_path (buffer, sizeof (buffer)); 
    299  
    300   memset (&s, 0, sizeof (s)); 
    301  
     309  FILE *fpout; 
     310   
    302311  endwin (); 
    303312  if (h) 
     
    307316 
    308317#ifdef HAVE_PGP 
    309     if (option (OPTPIPEDECODE)) 
     318    if (decode) 
    310319    { 
    311320      mutt_parse_mime_message (Context, h); 
     
    316325#endif 
    317326 
    318  
    319  
    320     thepid = mutt_create_filter (buffer, &s.fpout, NULL, NULL); 
    321     mutt_pipe_message_to_state (h, &s); 
    322     fclose (s.fpout); 
     327    thepid = mutt_create_filter (cmd, &fpout, NULL, NULL); 
     328    pipe_msg (h, fpout, decode); 
     329    fclose (fpout); 
    323330    rc = mutt_wait_filter (thepid); 
    324331  } 
     
    330337#ifdef HAVE_PGP 
    331338 
    332     if(option(OPTPIPEDECODE)) 
     339    if (decode) 
    333340    { 
    334341      for (i = 0; i < Context->vcount; i++) 
     
    344351#endif 
    345352     
    346  
    347  
    348     if (option (OPTPIPESPLIT)) 
     353    if (split) 
    349354    { 
    350355      for (i = 0; i < Context->vcount; i++) 
     
    354359          mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); 
    355360          endwin (); 
    356           thepid = mutt_create_filter (buffer, &(s.fpout), NULL, NULL); 
    357           mutt_pipe_message_to_state (Context->hdrs[Context->v2r[i]], &s); 
     361          thepid = mutt_create_filter (cmd, &fpout, NULL, NULL); 
     362          pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode); 
    358363          /* add the message separator */ 
    359           if (PipeSep) 
    360             state_puts (PipeSep, &s); 
    361           fclose (s.fpout); 
     364          if (sep)  fputs (sep, fpout); 
     365          fclose (fpout); 
    362366          if (mutt_wait_filter (thepid) != 0) 
    363367            rc = 1; 
     
    368372    { 
    369373      endwin (); 
    370       thepid = mutt_create_filter (buffer, &(s.fpout), NULL, NULL); 
     374      thepid = mutt_create_filter (cmd, &fpout, NULL, NULL); 
    371375      for (i = 0; i < Context->vcount; i++) 
    372376      { 
     
    374378        { 
    375379          mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); 
    376           mutt_pipe_message_to_state (Context->hdrs[Context->v2r[i]], &s); 
     380          pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode); 
    377381          /* add the message separator */ 
    378           if (PipeSep) 
    379             state_puts (PipeSep, &s); 
     382          if (sep) fputs (sep, fpout); 
    380383        } 
    381384      } 
    382       fclose (s.fpout); 
     385      fclose (fpout); 
    383386      if (mutt_wait_filter (thepid) != 0) 
    384387        rc = 1; 
     
    388391  if (rc || option (OPTWAITKEY)) 
    389392    mutt_any_key_to_continue (NULL); 
    390   return 1; 
    391 } 
     393  return rc; 
     394} 
     395 
     396void mutt_pipe_message (HEADER *h) 
     397{ 
     398  char buffer[LONG_STRING]; 
     399 
     400  buffer[0] = 0; 
     401  if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD) 
     402      != 0 || !buffer[0]) 
     403    return; 
     404 
     405  mutt_expand_path (buffer, sizeof (buffer)); 
     406  _mutt_pipe_message (h, buffer, 
     407                      option (OPTPIPEDECODE), 
     408                      option (OPTPIPESPLIT), 
     409                      PipeSep); 
     410} 
     411 
     412void mutt_print_message (HEADER *h) 
     413{ 
     414 
     415  if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) 
     416  { 
     417    mutt_message (_("No printing command has been defined.")); 
     418    return; 
     419  } 
     420   
     421  if (query_quadoption (OPT_PRINT, 
     422                        h ? _("Print message?") : _("Print tagged messages?")) 
     423                        != M_YES) 
     424    return; 
     425 
     426  if (_mutt_pipe_message (h, PrintCmd, 
     427                          option (OPTPRINTDECODE), 
     428                          option (OPTPRINTSPLIT), 
     429                          "\f") == 0) 
     430    mutt_message (h ? _("Message printed") : _("Messages printed")); 
     431  else 
     432    mutt_message (h ? _("Message could not be printed") : 
     433                  _("Messages could not be printed")); 
     434} 
     435 
    392436 
    393437int mutt_select_sort (int reverse) 
     
    721765} 
    722766 
    723 /* XXX - merge this with mutt_pipe_message_to_state? */ 
    724  
    725 static void print_msg (FILE *fp, CONTEXT *ctx, HEADER *h) 
    726 { 
    727   int cmflags = 0; 
    728   int chflags = CH_FROM; 
    729  
    730   pipe_print_set_flags (option (OPTPRINTDECODE), &cmflags, &chflags); 
    731  
    732 #ifdef HAVE_PGP 
    733   if (option (OPTPRINTDECODE) && (h->pgp & PGPENCRYPT)) 
    734   { 
    735     if (!pgp_valid_passphrase ()) 
    736       return; 
    737     endwin (); 
    738   } 
    739 #endif 
    740  
    741   if (option (OPTPRINTDECODE)) 
    742     mutt_parse_mime_message (ctx, h); 
    743  
    744   mutt_copy_message (fp, ctx, h, cmflags, chflags); 
    745 } 
    746  
    747 void mutt_print_message (HEADER *h) 
    748 { 
    749   int i, count = 0; 
    750   pid_t thepid; 
    751   FILE *fp; 
    752  
    753    
    754   if (query_quadoption (OPT_PRINT, 
    755                         h ? _("Print message?") : _("Print tagged messages?")) 
    756                         != M_YES) 
    757     return; 
    758   endwin (); 
    759   if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fp, NULL, NULL)) == -1) 
    760     return; 
    761   if (h) 
    762   { 
    763     mutt_message_hook (Context, h, M_MESSAGEHOOK); 
    764     print_msg (fp, Context, h); 
    765     count++; 
    766   } 
    767   else 
    768   { 
    769     for (i = 0 ; i < Context->vcount ; i++) 
    770     { 
    771       if (Context->hdrs[Context->v2r[i]]->tagged) 
    772       { 
    773         mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); 
    774         print_msg (fp, Context, Context->hdrs[Context->v2r[i]]); 
    775         /* add a formfeed */ 
    776         fputc ('\f', fp); 
    777         count++; 
    778       } 
    779     } 
    780   } 
    781   fclose (fp); 
    782   if (mutt_wait_filter (thepid) || option (OPTWAITKEY)) 
    783     mutt_any_key_to_continue (NULL); 
    784   mutt_message ((count > 1) ? _("Message printed") : _("Messages printed")); 
    785 } 
    786767 
    787768void mutt_version (void)