Changes between Version 4 and Version 5 of ConfigTricks/CheckAttach


Ignore:
Timestamp:
Oct 24, 2013 5:14:51 AM (4 years ago)
Author:
brisant
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ConfigTricks/CheckAttach

    v4 v5  
    139139----
    140140Another simple script was posted to mutt-dev http://marc.info/?l=mutt-dev&m=110663305206264&w=2
     141
     142
     143----
     144I have changed the above script to make it look like beeing interactive in mutt.
     145When the checks detect attchments are missing it will print a question at the status line of mutt and the user can answer how to proceed:
     146
     147Pro: No X needed, No extra header needed (but still available).
     148Con: One of the parent processes need to have a valid pts, tty executable must be available
     149
     150Snippets are taken from a mairix script, which integrates mairix search in a similar way.
     151
     152{{{
     153#!/bin/zsh
     154##
     155## Script: ~/.mutt/sendmail_wrapper.zsh
     156##
     157## Source: http://wiki.mutt.org/?ConfigTricks/CheckAttach
     158##
     159## muttrc should most probably contain this line:
     160## set sendmail = "~/.mutt/sendmail_wrapper.zsh /usr/bin/sendmail -oem -oi"
     161
     162## Attachment keywords that the message body will be searched for:
     163KEYWORDS='attach|anhang|anhaenge|anhänge|angehaengt|angehangen|anbei|angehängt'
     164ERRORMESSAGE="Attachment missing!? (Use \"X-attached: none\" to override) Send mail anyway? "
     165
     166## Check that sendmail or other program is supplied as first argument.
     167if [ ! -x "$1" ]
     168then
     169    echo "Usage: $0 </path/to/mailprog> <args> ..."
     170    echo "e.g.: $0 /usr/sbin/sendmail -oem -oi"
     171    exit 2
     172fi
     173
     174## Save msg in file to re-use it for multiple tests.
     175TMPFILE=`mktemp -t mutt_checkattach.XXXXXX` || exit 2
     176cat > "$TMPFILE"
     177
     178## Define test for multipart message.
     179function multipart {
     180    grep -q '^Content-Type: multipart' "$TMPFILE"
     181}
     182
     183## Define test for keyword search.
     184function word-attach {
     185    grep -v '^>' "$TMPFILE" | grep -E -i -q "$KEYWORDS"
     186}
     187
     188## Header override.
     189function header-override {
     190    grep -i -E "^X-attached: none$" "$TMPFILE"
     191}
     192
     193function interactive-ask {
     194  # disable globbing
     195  set -f
     196  # try to get stdin/stdout/stderr from parent process
     197  PID_cur=$$
     198  TTY_cur=$(ps h -o tty -p ${PID_cur})
     199  while ! tty > /dev/null
     200  do
     201    PID_cur=$(cut -f4 -d' ' /proc/${PID_cur}/stat)
     202    TTY_cur=$(ps h -o tty -p ${PID_cur})
     203    TTY_use="/dev/${TTY_cur}"
     204    if [ -e ${TTY_use} ]
     205    then
     206      exec 0</dev/${TTY_cur} 1> /dev/${TTY_cur} 2>/dev/null
     207    fi
     208  done
     209  # reset terminal settings to current state when exit
     210  saved_tty_settings=$(stty -g)
     211  trap '
     212  printf "\r"; tput ed; tput rc
     213  printf "/" >&3
     214  stty "$saved_tty_settings"
     215  exit
     216  ' INT TERM
     217
     218  # put the terminal in cooked mode. Set eof to <return> so that pressing
     219  # <return> doesn't move the cursor to the next line. Disable <Ctrl-Z>
     220  stty icanon echo -ctlecho crterase eof '^M' intr '^G' susp ''
     221
     222  set $(stty size) # retrieve the size of the screen
     223  tput sc          # save cursor position
     224  tput cup "$1" 0  # go to last line of the screen
     225  tput ed          # clear and write prompt
     226  tput sgr0
     227
     228  # print message
     229  printf ${ERRORMESSAGE}
     230
     231  # read from the terminal. We can't use "read" because, there won't be
     232  # any NL in the input as <return> is eof.
     233  answer=$(dd count=1 2>/dev/null)
     234
     235  # fix the terminal
     236  printf '\r'; tput ed; tput rc
     237  stty "${saved_tty_settings}"
     238
     239  # apply return code
     240  if [ "${answer}" = "y" ]
     241  then
     242    return 0
     243  else
     244    return 1
     245  fi 
     246}
     247
     248## FINAL DECISION:
     249if multipart || ! word-attach || header-override || interactive-ask; then
     250    "$@" < "$TMPFILE"
     251    EXIT_STATUS=$?
     252else
     253  EXIT_STATUS=1
     254fi
     255
     256## Delete the temporary file.
     257rm -f "$TMPFILE"
     258
     259## That's all folks.
     260exit $EXIT_STATUS
     261}}}