Re: Patch: filter-message

2002-03-27 Thread Steve Talley

Yep.  I've gotten this same feedback from a few others.  Modified
patch (w/ mapping to ^\) is attached.

Thanks,

Steve

Jeremy Blosser wrote:

 On Mar 26, Steve Talley [[EMAIL PROTECTED]] wrote:
  filter-message (default: )

 FWIW, the edit-threads patch, which quite a lot of people use, uses
  for one of it's primary functions.


diff -pruN2d mutt-1.3.28.orig/OPS mutt-1.3.28/OPS
--- mutt-1.3.28.orig/OPSSat Jan 27 06:33:53 2001
+++ mutt-1.3.28/OPS Tue Mar 26 10:47:39 2002
@@ -82,4 +82,5 @@ OP_ENTER_MASK enter a file mask
 OP_EXIT exit this menu
 OP_FILTER filter attachment through a shell command
+OP_FILTER_MESSAGE filter message through a shell command
 OP_FIRST_ENTRY move to the first entry
 OP_FLAG_MESSAGE toggle a message's 'important' flag
diff -pruN2d mutt-1.3.28.orig/PATCHES mutt-1.3.28/PATCHES
--- mutt-1.3.28.orig/PATCHESMon Nov 26 12:16:52 2001
+++ mutt-1.3.28/PATCHES Tue Mar 26 10:56:38 2002
@@ -0,0 +1 @@
+patch-1.3.28.st.filter_message.1
diff -pruN2d mutt-1.3.28.orig/commands.c mutt-1.3.28/commands.c
--- mutt-1.3.28.orig/commands.c Thu Nov  8 01:56:48 2001
+++ mutt-1.3.28/commands.c  Tue Mar 26 10:47:39 2002
@@ -301,5 +301,10 @@ void pipe_msg (HEADER *h, FILE *fp, int 
 
 
-/* the following code is shared between printing and piping */
+/*
+ * the following code is shared between printing and piping
+ *
+ * fpfout: NULL to direct the command's STDOUT to mutt's STDOUT, or
+ * non-null to redirect.
+ */
 
 static int _mutt_pipe_message (HEADER *h, char *cmd,
@@ -307,5 +312,6 @@ static int _mutt_pipe_message (HEADER *h
   int print,
   int split,
-  char *sep)
+  char *sep,
+  FILE **fpfout)
 {
   
@@ -330,5 +336,5 @@ static int _mutt_pipe_message (HEADER *h
 #endif
 
-if ((thepid = mutt_create_filter (cmd, fpout, NULL, NULL))  0)
+if ((thepid = mutt_create_filter (cmd, fpout, fpfout, NULL))  0)
 {
   mutt_perror _(Can't create filter process);
@@ -369,5 +375,5 @@ static int _mutt_pipe_message (HEADER *h
  mutt_message_hook (Context, Context-hdrs[Context-v2r[i]], M_MESSAGEHOOK);
  mutt_endwin (NULL);
- if ((thepid = mutt_create_filter (cmd, fpout, NULL, NULL))  0)
+ if ((thepid = mutt_create_filter (cmd, fpout, fpfout, NULL))  0)
  {
mutt_perror _(Can't create filter process);
@@ -386,5 +392,5 @@ static int _mutt_pipe_message (HEADER *h
 {
   mutt_endwin (NULL);
-  if ((thepid = mutt_create_filter (cmd, fpout, NULL, NULL))  0)
+  if ((thepid = mutt_create_filter (cmd, fpout, fpfout, NULL))  0)
   {
mutt_perror _(Can't create filter process);
@@ -426,5 +432,6 @@ void mutt_pipe_message (HEADER *h)
  0, 
  option (OPTPIPESPLIT),
- PipeSep);
+ PipeSep,
+ NULL);
 }
 
@@ -447,5 +454,6 @@ void mutt_print_message (HEADER *h)
  1,
  option (OPTPRINTSPLIT),
- \f) == 0)
+ \f,
+ NULL) == 0)
 mutt_message (h ? _(Message printed) : _(Messages printed));
   else
@@ -454,4 +462,87 @@ void mutt_print_message (HEADER *h)
 }
 
+/*
+ * Filter a single message through the given command
+ */
+int filter_one_message (CONTEXT *ctx, HEADER *h, char *command)
+{
+  FILE *fpfout;
+  char tmp[_POSIX_PATH_MAX];
+  int omagic;
+  int rc;
+  int oerrno;
+  CONTEXT tmpctx;
+
+  _mutt_pipe_message (h, command,
+ option (OPTPIPEDECODE),
+ 0, 
+ option (OPTPIPESPLIT),
+ PipeSep,
+ fpfout);
+
+  /* Create tmp mbox for filter output */
+  mutt_mktemp (tmp);
+  omagic = DefaultMagic;
+  DefaultMagic = M_MBOX;
+  rc = (mx_open_mailbox (tmp, M_APPEND, tmpctx) == NULL) ? -1 : 0;
+  DefaultMagic = omagic;
+
+  if (rc == -1)
+  {
+mutt_error (_(could not create temporary folder: %s), strerror (errno));
+return -1;
+  }
+
+  /* Copy filter output to tmp mbox */
+  rc = mutt_copy_stream (fpfout, tmpctx.fp);
+  oerrno = errno;
+  rc = fflush(tmpctx.fp);
+
+  /* Close stream and tmp mbox */
+  safe_fclose (fpfout);
+  mx_close_mailbox (tmpctx, NULL);
+
+  if (rc == -1)
+  {
+mutt_error (_(could not write temporary mail folder: %s), strerror (errno));
+return -1;
+  }
+
+  /* Replace the selected message with the filter output */
+  return mutt_replace_message (ctx, h, tmp, 0);
+}
+
+/*
+ * Filter a single or tagged messages through a user-specified command
+ */
+int mutt_filter_message (CONTEXT *ctx, HEADER *hdr)
+{
+  int i, j;
+  char buffer[LONG_STRING];
+
+  buffer[0] = 0;
+  if (mutt_get_field (_(Filter command: ), buffer, sizeof (buffer), M_CMD)
+  != 0 || !buffer[0])
+return 0;
+
+  mutt_expand_path (buffer, sizeof (buffer));
+
+  /* 

Patch: filter-message

2002-03-26 Thread Steve Talley

Attached is a patch that implements the filter-message command
discussed last week (Replacing a message with its filtered output).
It combines parts of the pipe-message and edit commands, so that
$editor doesn't have to be set/reset to filter a message through an
external program.

Quick description:

filter-message (default: )

Asks for an external shell command and filters the current or tagged
message(s) through it.  Each tagged message will be filtered through a
separate invocation of the command.  The stdout of the command will be
appended to the current folder as a new message, and the original
message will be marked for deletion.

This command is available in the index and pager. As with pipe-
message, the variables $pipe_decode and $wait_key control the
exact behaviour of this function.

Comments are welcome.  Thanks!

Steve


diff -pruN2d mutt-1.3.28.orig/OPS mutt-1.3.28/OPS
--- mutt-1.3.28.orig/OPSSat Jan 27 06:33:53 2001
+++ mutt-1.3.28/OPS Tue Mar 26 10:47:39 2002
 -82,4 +82,5  OP_ENTER_MASK enter a file mask
 OP_EXIT exit this menu
 OP_FILTER filter attachment through a shell command
+OP_FILTER_MESSAGE filter message through a shell command
 OP_FIRST_ENTRY move to the first entry
 OP_FLAG_MESSAGE toggle a message's 'important' flag
diff -pruN2d mutt-1.3.28.orig/PATCHES mutt-1.3.28/PATCHES
--- mutt-1.3.28.orig/PATCHESMon Nov 26 12:16:52 2001
+++ mutt-1.3.28/PATCHES Tue Mar 26 10:56:38 2002
 -0,0 +1 
+patch-1.3.28.st.filter_message.1
diff -pruN2d mutt-1.3.28.orig/commands.c mutt-1.3.28/commands.c
--- mutt-1.3.28.orig/commands.c Thu Nov  8 01:56:48 2001
+++ mutt-1.3.28/commands.c  Tue Mar 26 10:47:39 2002
 -301,5 +301,10  void pipe_msg (HEADER *h, FILE *fp, int 
 
 
-/* the following code is shared between printing and piping */
+/*
+ * the following code is shared between printing and piping
+ *
+ * fpfout: NULL to direct the command's STDOUT to mutt's STDOUT, or
+ * non-null to redirect.
+ */
 
 static int _mutt_pipe_message (HEADER *h, char *cmd,
 -307,5 +312,6  static int _mutt_pipe_message (HEADER *h
   int print,
   int split,
-  char *sep)
+  char *sep,
+  FILE **fpfout)
 {
   
 -330,5 +336,5  static int _mutt_pipe_message (HEADER *h
 #endif
 
-if ((thepid = mutt_create_filter (cmd, fpout, NULL, NULL))  0)
+if ((thepid = mutt_create_filter (cmd, fpout, fpfout, NULL))  0)
 {
   mutt_perror _(Can't create filter process);
 -369,5 +375,5  static int _mutt_pipe_message (HEADER *h
  mutt_message_hook (Context, Context-hdrs[Context-v2r[i]], M_MESSAGEHOOK);
  mutt_endwin (NULL);
- if ((thepid = mutt_create_filter (cmd, fpout, NULL, NULL))  0)
+ if ((thepid = mutt_create_filter (cmd, fpout, fpfout, NULL))  0)
  {
mutt_perror _(Can't create filter process);
 -386,5 +392,5  static int _mutt_pipe_message (HEADER *h
 {
   mutt_endwin (NULL);
-  if ((thepid = mutt_create_filter (cmd, fpout, NULL, NULL))  0)
+  if ((thepid = mutt_create_filter (cmd, fpout, fpfout, NULL))  0)
   {
mutt_perror _(Can't create filter process);
 -426,5 +432,6  void mutt_pipe_message (HEADER *h)
  0, 
  option (OPTPIPESPLIT),
- PipeSep);
+ PipeSep,
+ NULL);
 }
 
 -447,5 +454,6  void mutt_print_message (HEADER *h)
  1,
  option (OPTPRINTSPLIT),
- \f) == 0)
+ \f,
+ NULL) == 0)
 mutt_message (h ? _(Message printed) : _(Messages printed));
   else
 -454,4 +462,87  void mutt_print_message (HEADER *h)
 }
 
+/*
+ * Filter a single message through the given command
+ */
+int filter_one_message (CONTEXT *ctx, HEADER *h, char *command)
+{
+  FILE *fpfout;
+  char tmp[_POSIX_PATH_MAX];
+  int omagic;
+  int rc;
+  int oerrno;
+  CONTEXT tmpctx;
+
+  _mutt_pipe_message (h, command,
+ option (OPTPIPEDECODE),
+ 0, 
+ option (OPTPIPESPLIT),
+ PipeSep,
+ fpfout);
+
+  /* Create tmp mbox for filter output */
+  mutt_mktemp (tmp);
+  omagic = DefaultMagic;
+  DefaultMagic = M_MBOX;
+  rc = (mx_open_mailbox (tmp, M_APPEND, tmpctx) == NULL) ? -1 : 0;
+  DefaultMagic = omagic;
+
+  if (rc == -1)
+  {
+mutt_error (_(could not create temporary folder: %s), strerror (errno));
+return -1;
+  }
+
+  /* Copy filter output to tmp mbox */
+  rc = mutt_copy_stream (fpfout, tmpctx.fp);
+  oerrno = errno;
+  rc = fflush(tmpctx.fp);
+
+  /* Close stream and tmp mbox */
+  safe_fclose (fpfout);
+  mx_close_mailbox (tmpctx, NULL);
+
+  if (rc == -1)
+  {
+mutt_error (_(could not write temporary mail folder: %s), strerror (errno));
+return 

Re: Patch: filter-message

2002-03-26 Thread Dan Boger

On Tue, Mar 26, 2002 at 11:19:14AM -0700, Steve Talley wrote:
 Quick description:
 
 filter-message (default: )
 
 Asks for an external shell command and filters the current or tagged
 message(s) through it.  Each tagged message will be filtered through a
 separate invocation of the command.  The stdout of the command will be
 appended to the current folder as a new message, and the original
 message will be marked for deletion.

just curious - wouldn't this break the compression patch?  Sounds like a
useful idea, but I can't even try it if it does :/

-- 
Dan Boger
[EMAIL PROTECTED]



msg26169/pgp0.pgp
Description: PGP signature


Re: Patch: filter-message

2002-03-26 Thread David T-G

Dan --

...and then Dan Boger said...
% 
% On Tue, Mar 26, 2002 at 11:19:14AM -0700, Steve Talley wrote:
%  Quick description:
...
%  separate invocation of the command.  The stdout of the command will be
%  appended to the current folder as a new message, and the original
%  message will be marked for deletion.
% 
% just curious - wouldn't this break the compression patch?  Sounds like a
% useful idea, but I can't even try it if it does :/

Don't see why it would; can you clarify your hesitation?  If the working
folder is modified, say by a save or an edit-message or any such stuff,
then mutt will utilize the close-hook command to bundle the temp folder
into the place of the compressed folder (whereas if no changes are saved
then the temp folder can be thrown away without changing the original
folder).

Or am I missing something?


% 
% -- 
% Dan Boger
% [EMAIL PROTECTED]


HTH  HAND

:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg26173/pgp0.pgp
Description: PGP signature


Re: Patch: filter-message

2002-03-26 Thread Dan Boger

On Tue, Mar 26, 2002 at 01:47:50PM -0500, David T-G wrote:
 Don't see why it would; can you clarify your hesitation?  If the working
 folder is modified, say by a save or an edit-message or any such stuff,
 then mutt will utilize the close-hook command to bundle the temp folder
 into the place of the compressed folder (whereas if no changes are saved
 then the temp folder can be thrown away without changing the original
 folder).
 
 Or am I missing something?

I think I am missing something... when a message is filtered, it will
append it to the TEMP copy of the folder?  if so, that's great :)  I was
worried that it will append it to the original, and that would, of
course, break things...

-- 
Dan Boger
[EMAIL PROTECTED]



msg26197/pgp0.pgp
Description: PGP signature


Re: Patch: filter-message

2002-03-26 Thread David T-G

Dan --

...and then Dan Boger said...
% 
% On Tue, Mar 26, 2002 at 01:47:50PM -0500, David T-G wrote:
%  Don't see why it would; can you clarify your hesitation?  If the working
%  folder is modified, say by a save or an edit-message or any such stuff,
...
%  Or am I missing something?
% 
% I think I am missing something... when a message is filtered, it will
% append it to the TEMP copy of the folder?  if so, that's great :)  I was

That's how I read it.


% worried that it will append it to the original, and that would, of
% course, break things...

Well, you should make a test compressed folder and try it out, of
course :-)


% 
% -- 
% Dan Boger
% [EMAIL PROTECTED]


:-D
-- 
David T-G  * It's easier to fight for one's principles
(play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie
(work) [EMAIL PROTECTED]
http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg!




msg26203/pgp0.pgp
Description: PGP signature