Hi Peter!

On Mi, 12 Feb 2014, Peter Davis wrote:

> I'm thinking of implementing a feature which allows a given keystroke
> to pipe a message to a perl script which, after a couple of prompts,
> adds a rule to my procmailrc or blacklist file to remove all future
> occurrences of such messages. The script would probably just ask me
> what part of the message to filter on (sender, subject keywords, etc.)
> and then add the rule(s).
> 
> Has anyone already done this?
> 
> I use mutt on my local MH store, but also on an IMAP server. Any ideas
> how to do this for IMAP? 

Years ago I've written a simple shell script, that uses the score 
functionality of mutt to ignore or filter unwanted messages from 
different mailinglist. I added a macro, that will pipe the current 
message to that shell script and generated a score statement that could 
be read back by mutt. It can easily be improved by filtering for 
subjects or whatever you like.

I don't use it anymore, but I still have it laying around. Here is the 
relevant config:


,----[ ~/.mutt/macros.conf ]-
| # This macros calls an external shell script that will generate a
| # scoring value for the author of a message which in turn can be fed to
| # mutt 
| # Depending on the Options given to the script, mutt will set mails
| # from that author read, or delete them or reset it
| 
| macro index,pager <ESC>s "<enter-command>set wait_key=no<enter>\
| <pipe-message>~/bin/mutt_score.sh a <enter>\
| <enter-command>source ~/.mutt/score<enter>\
| <enter-command>set wait_key=yes<enter>" "Score author of message 
interactively"
| 
| macro index,pager <ESC>i "<enter-command>set wait_key=no<enter>\
| <pipe-message>~/bin/mutt_score.sh i <enter>\
| <enter-command>source ~/.mutt/score<enter>\
| <enter-command>set wait_key=yes<enter>" "Ignore future messages of author"
| 
| macro index,pager <ESC>d "<enter-command>set wait_key=no<enter>\
| <pipe-message>~/bin/mutt_score.sh d <enter>\
| <enter-command>source ~/.mutt/score<enter>\
| <enter-command>set wait_key=yes<enter>" "Delete future messages of author"
| 
| macro index,pager <ESC>r "<enter-command>set wait_key=no<enter>\
| <pipe-message>~/bin/mutt_score.sh r <enter>\
| <enter-command>source ~/.mutt/score<enter>\
| <enter-command>set wait_key=yes<enter>" "Reset scoring for author"
`----


,----[ ~/bin/mutt_score.sh ]-
| #!/bin/bash --
| 
| # in which file to store the score_config
| CFG="/home/chrisbra/.mutt/score_gen"
| 
| # contains the message in a variable
| STDIN="`cat`"
| 
| #FROM=`echo ${STDIN}|formail -zx "From" |~/mimedecode |sed -e 's/,.*$//' 
|tail -1`
| #FROM=`echo $STDIN`|formail -zx "To:" 
| #while read line; do 
| #    echo $line|formail -zx"To:" |~/mimedecode|sed -e 's/,.*$//'
| #done
| 
| # The sed statement will cut out only the relevant part from
| # the e-mail address between (e.g. for "Foo" <foo....@foobar.xyz> it
| # will only print foo\.bar@foobar\.xyz) and will also translate
| # allowed characters like "." or "+" by escaping them
| FROM=`echo "${STDIN}"|formail -tzx "From:" | sed 
's/.*<//;s/>//;s/\./\\\\&/g;s/+/\\\\&/g'`
| 
| # Record the date
| DATE=`date +%d.%m.%Y`
| 
| function ignore(){
| MODE="score '~f \""$FROM"\"'\t=1\t # mark msg as read (score set on ${DATE})"
| echo -e "$MODE" >>"$CFG"
| }
| function delete(){
| MODE="score '~f \""$FROM"\"'\t=0\t # mark msg as deleted (score set on 
${DATE})"
| echo -e "$MODE" >>"$CFG"
| }
| 
| function interactive(){
| clear
| echo "What would you like to do with the author of the provided message?"
| echo "i)gnore messages from author"
| echo "d)elete messages from author"
| echo "u)ndo scoring messages from author"
| echo "q)quit"
| read input </dev/tty
| case "$input" in
|     i) ignore; ;;
|     d) delete; ;;
|     u) undo; ;;
|     q) exit 1; ;;
| esac
| }
| 
| function help(){
| clear
| cat <<EOF
| NAME
| 
| This script takes a message on stdin, determines the author
| and depending on the given parameter it will either set a score
| for ignoring/deleting messages from that author or reset the 
| score for that author.
| 
| SYNOPSIS
| `basename $0` [i|d|r|a|h]
| 
| The following Options are supported
| i\tset score to ignore message from the author
| d\tset score to delete message from the author
| r\treset score for author
| a\tinteractive guide
| h\tdisplays this help screen
| 
| The output will be a command so this script can be used from within mutt.
| 
| EOF
| }
| 
| function undo() {
| FROM=${FROM//\\/\\\\}
| sed -i -e "/"$FROM"/d" "$CFG"
| }
| 
| 
| case "$1" in
|     i)  SCORE=Y
|       ignore;
|     ;;         # Author will be ignored (msg marked read)
|     d)  SCORE=Y
|       delete;
|       ;;         # msgs from Author will be deleted
|     r)  SCORE=N
|       undo;
|       ;;         # Author score will be reseted
|     a)  SCORE=Y
|       interactive;
|       ;;         # interactive (ask what to do)
|     *)  SCORE=N
|       help;   
|       ;;         # display Help screen
| esac
`----



regards,
Christian
-- 
A man always remembers his first love with special tenderness, but after
that begins to bunch them.
                -- Mencken

Reply via email to