#!/bin/bash
#
# script removes fullquotes from mail messages
# we first call fullquotedetector.awk -- -t
# if a fullquote is detected this returns start and end
# which we use for the removal.
# if no fullquote is detected the whole file is printed.
# if option -r is given we remove rigidly _everything_ below
# ---- Original message ----/---- Urspruengliche Nachricht ----

PRGVERSION="fullquoteremover 1.31 * 2014-11-22"

# Copyright 2003-2015: Andreas Schamanek <andreas@schamanek.net>
# License + History: see end of file

usage="Usage: $0 [-r]"
usage_try="Try '$0 --help' for more information.\n"

usage_help="Remove fullquotes (hopefully) from a mail message.

  -r          rigidly remove Outlook style fullquotes
  --help      display this help and exit
  --version   output version information and exit

  Example

    cat message.eml | fullquoteremover -r >message_clean.eml

Report bugs to Andreas.Schamanek@tuwien.ac.at\n"

case $# in
  1 )
    case "z${1}" in
      z--help )
         echo -e "$usage"; echo -e "$usage_help"; exit 0 ;;
      z--version )
         echo -e "$PRGVERSION"; exit 0 ;;
    esac
    ;;
  * ) ;;
esac

if [ $# -gt 1 ] ; then
  echo -e >&2 "$usage"
  echo -e >&2 "$usage_try"
  exit 1
fi

# --------------------------------------------------------------------
# variables
REMOVALNOTE="[ Fullquote removed - see http://wox.at/u/edit_messages ]
[ Fullquote geloescht - siehe http://wox.at/u/2quote    ]"

FQDETECTOR="fullquotedetector.awk"

# --------------------------------------------------------------------

function removeFQrigidly {
  awk -v removalnote="$REMOVALNOTE" '\
  BEGIN { qstart = 99999 } ;\
  /^[	 ]*-----(-* ?((Original|Reply) Message|(Urspr.*ngliche N|Original(-N|n))achricht) ?-| Reply message )-----*/ { qstart = NR } ;\
  NR < qstart { print } ; \
  NR == qstart { print removalnote ; exit }'
}

# --------------------------------------------------------------------
# main part of script

# message is expected on stdin
TMPFILE=`mktemp -q /tmp/fullquote.XXXXXX`
cat >$TMPFILE

# pipe through detector (and get qstart and qend of fullquote)
# -f tells FQDETECTOR to skip messages that seem to contain forwarded data
FULLQUOTE=$($FQDETECTOR -- -f -t < $TMPFILE)

# in case of -r rigidly removing outlook style fullquotes
# we want to accept fullquotes (without ">") if the Subject:-line
# signals a forwarded message. so in this case we fall back to 
# normal operation
RIGIDLY=1   # default is normal operation
if [ "X$1" == "X-r" ] ; then
   # rigid operation
   RIGIDLY=0
   if ( head -n 60 $TMPFILE \
        | grep -Eq "^(Subject|Betreff): (.*(\(fwd\)|Fwd)|F[Ww]: )" ) ; then
      # fall back to normal operation
      RIGIDLY=1
   else 
      # go on with rigidly removing
      RIGIDLY=0
   fi
fi

# default processing (not -r rigidly)
if [ "$RIGIDLY" -eq 1 ] ; then
   if [ -z "$FULLQUOTE" ] ; then
      # no fullquote found, cat message as is
      cat $TMPFILE
   else
      cat $TMPFILE \
      | awk -v removalnote="$REMOVALNOTE" $FULLQUOTE '\
      NR < qstart || NR > qend { print } ; \
      NR == qstart { print removalnote }'
   fi
else
   # also remove all lines below ----- Original Message -----
   if [ -z "$FULLQUOTE" ] ; then
      # no fullquote found, cat message as is
      cat $TMPFILE | removeFQrigidly
   else
      cat $TMPFILE \
      | awk -v removalnote="$REMOVALNOTE" $FULLQUOTE '\
      NR < qstart || NR > qend { print } ; \
      NR == qstart { print removalnote }' \
      | removeFQrigidly
   fi
fi

rm $TMPFILE

# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

# History
#
# 1.12 * 2004-11-24  fixed a typo in a regex for 'Fwd:?'
# 1.13 * 2004-12-02  new eMail-address, testing 60 lines for 'fwd|Fwd'
# 1.14 * 2004-12-09  changed Forward-Regex to (.*(\(fwd\)|Fwd)|F[Ww]: )
# 1.15 * 2004-12-23  changed Regex to ^[    ]*----- ?(Original Mess....
# 1.20 * 2005-02-15  calling new fullquotedetector.awk with -f (v 2.4+)
# 1.21 * 2005-10-27  changed URL in REMOVALNOTE
# 1.22 * 2007-08-15  changed Regex of MS+GMX forward style full quote
# 1.23 * 2010-04-15  minor performance tweak for -r/removeFQrigidly()
# 1.30 * 2011-11-26  added "Reply Message" (used by Android Mail?) to
#                    regex for -r/removeFQrigidly(), changed REMOVALNOTE
# 1.31 * 2014-11-22  fixed a typo in $REMOVALNOTE
