#!/bin/sh
#---------------------------------------------
#   xdg-email
#
#   Utility script to open the users favorite email program, using the
#       RFC 2368 mailto: URI spec
#
#   Refer to the usage() function below for usage.
#
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
#
#   LICENSE:
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.
#
#---------------------------------------------

manualpage()
{
cat << _MANUALPAGE
Name

xdg-email - command line tool for sending mail using the user's preferred
e-mail composer

Synopsis

xdg-email [--utf8] [--cc address] [--bcc address] [--subject text] [--body text
] [--attach file] { mailto-uri | address(es) }

xdg-email { --help | --manual | --version }

Description

xdg-email opens the user's preferred e-mail composer in order to send a mail to
address(es) or mailto-uri. RFC2368 defines mailto: URIs. xdg-email limits
support to, cc, subject and body fields in mailto-uri, all other fields are
silently ignored. address(es) must follow the syntax of RFC822. Multiple
addresses may be provided as separate arguments.

All information provided on the command line is used to prefill corresponding
fields in the user's e-mail composer. The user will have the opportunity to
change any of this information before actually sending the e-mail.

xdg-email is for use inside a desktop session only. It is not recommended to
use xdg-email as root.

See http://portland.freedesktop.org/EmailConfig for information on how the user
can change the e-mail composer that is used.

Options

--utf8
    Indicates that all command line options that follow are in utf8. Without
    this option, command line options are expected to be encoded according to
    locale. If the locale already specifies utf8 this option has no effect.
    This option does not affect mailto URIs that are passed on the command
    line.
--cc address
    Specify a recipient to be copied on the e-mail.
--bcc address
    Specify a recipient to be blindly copied on the e-mail.
--subject text
    Specify a subject for the e-mail.
--body text
    Specify a body for the e-mail. Since the user will be able to make changes
    before actually sending the e-mail, this can be used to provide the user
    with a template for the e-mail. text may contain linebreaks.
--attach file

    Specify an attachment for the e-mail. file must point to an existing file.

    Some e-mail applications require the file to remain present after xdg-email
    returns.

--help
    Show command synopsis.
--manual
    Show this manualpage.
--version
    Show the xdg-utils version information.

Environment Variables

xdg-email honours the following environment variables:

XDG_UTILS_DEBUG_LEVEL
    Setting this environment variable to a non-zero numerical value makes
    xdg-email do more verbose reporting on stderr. Setting a higher value
    increases the verbosity.

Exit Codes

An exit code of 0 indicates success while a non-zero exit code indicates
failure. The following failure codes can be returned:

1
    Error in command line syntax.
2
    One of the files passed on the command line did not exist.
3
    A required tool could not be found.
4
    The action failed.
5
    No permission to read one of the files passed on the command line.

Configuration

Visit http://portland.freedesktop.org/EmailConfig for information how to
configure xdg-email to use the email client of your choice.

Examples

xdg-email 'Jeremy White <jwhite@example.com>'

xdg-email --attach /tmp/logo.png \
          --subject 'Logo contest' \
          --body 'Attached you find the logo for the contest.' \
          'jwhite@example.com'

xdg-email --subject 'Your password is about to expire' \
          'jwhite@example.com' 'bastian@example.com' 'whipple@example.com'

_MANUALPAGE
}

usage()
{
cat << _USAGE
xdg-email - command line tool for sending mail using the user's preferred
e-mail composer

Synopsis

xdg-email [--utf8] [--cc address] [--bcc address] [--subject text] [--body text
] [--attach file] { mailto-uri | address(es) }

xdg-email { --help | --manual | --version }

_USAGE
}

#@xdg-utils-common@

#----------------------------------------------------------------------------
#   Common utility functions included in all XDG wrapper scripts
#----------------------------------------------------------------------------

DEBUG()
{
  [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  shift
  echo "$@" >&2
}

#-------------------------------------------------------------
# Exit script on successfully completing the desired operation

exit_success()
{
    if [ $# -gt 0 ]; then
        echo "$@"
        echo
    fi

    exit 0
}


#-----------------------------------------
# Exit script on malformed arguments, not enough arguments
# or missing required option.
# prints usage information

exit_failure_syntax()
{
    if [ $# -gt 0 ]; then
        echo "xdg-email: $@" >&2
        echo "Try 'xdg-email --help' for more information." >&2
    else
        usage
        echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
    fi

    exit 1
}

#-------------------------------------------------------------
# Exit script on missing file specified on command line

exit_failure_file_missing()
{
    if [ $# -gt 0 ]; then
        echo "xdg-email: $@" >&2
    fi

    exit 2
}

#-------------------------------------------------------------
# Exit script on failure to locate necessary tool applications

exit_failure_operation_impossible()
{
    if [ $# -gt 0 ]; then
        echo "xdg-email: $@" >&2
    fi

    exit 3
}

#-------------------------------------------------------------
# Exit script on failure returned by a tool application

exit_failure_operation_failed()
{
    if [ $# -gt 0 ]; then
        echo "xdg-email: $@" >&2
    fi

    exit 4
}

#------------------------------------------------------------
# Exit script on insufficient permission to read a specified file

exit_failure_file_permission_read()
{
    if [ $# -gt 0 ]; then
        echo "xdg-email: $@" >&2
    fi

    exit 5
}

#------------------------------------------------------------
# Exit script on insufficient permission to read a specified file

exit_failure_file_permission_write()
{
    if [ $# -gt 0 ]; then
        echo "xdg-email: $@" >&2
    fi

    exit 6
}

check_input_file()
{
    if [ ! -f "$1" ]; then
        exit_failure_file_missing "file '$1' does not exist"
    fi
    if [ ! -r "$1" ]; then
        exit_failure_file_permission_read "no permission to read file '$1'"
    fi
}

check_vendor_prefix()
{
    file_label="$2"
    [ -n "$file_label" ] || file_label="filename"
    file=`basename "$1"`
    case "$file" in
       [a-zA-Z]*-*)
         return
         ;;
    esac

    echo "xdg-email: $file_label '$file' does not have a proper vendor prefix" >&2
    echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
    echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
    echo "Use --novendor to override or 'xdg-email --manual' for additional info." >&2
    exit 1
}

check_output_file()
{
    # if the file exists, check if it is writeable
    # if it does not exists, check if we are allowed to write on the directory
    if [ -f "$1" ]; then
        if [ ! -w "$1" ]; then
            exit_failure_file_permission_write "no permission to write to file '$1'"
        fi
    else
        DIR=`dirname "$1"`
        if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
            exit_failure_file_permission_write "no permission to create file '$1'"
        fi
    fi
}

#----------------------------------------
# Checks for shared commands, e.g. --help

check_common_commands()
{
    while [ $# -gt 0 ] ; do
        parm="$1"
        shift

        case "$parm" in
            --help)
            usage
            echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
            exit_success
            ;;

            --manual)
            manualpage
            exit_success
            ;;

            --version)
            echo "xdg-email 1.0.1"
            exit_success
            ;;
        esac
    done
}

check_common_commands "$@"

[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
    # Be silent
    xdg_redirect_output=" > /dev/null 2> /dev/null"
else
    # All output to stderr
    xdg_redirect_output=" >&2"
fi

#--------------------------------------
# Checks for known desktop environments
# set variable DE to the desktop environments name, lowercase

detectDE()
{
    if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
    elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
    fi
}

#----------------------------------------------------------------------------
# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
# It also always returns 1 in KDE 3.4 and earlier
# Simply return 0 in such case

kfmclient_fix_exit_code()
{
    version=`kde-config --version 2>/dev/null | grep KDE`
    major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
    minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
    release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
    test "$major" -gt 3 && return $1
    test "$minor" -gt 5 && return $1
    test "$release" -gt 4 && return $1
    return 0
}

Which()
{
    OLD_IFS="$IFS"
    IFS=":"
    status=1

    for i in $PATH; do 
        if [ -x "$i/$1" ]; then
            printf "%s\n" "$i/$1"
            status=0
            break
        fi
    done

    IFS="$OLD_IFS"
    return $status
}                                                                                                                                                                                  
open_kde()
{
    if [ -f /etc/SuSE-release ] ; then
        # Workaround for SUSE 10.0
        client=`kreadconfig --file emaildefaults --group PROFILE_Default --key EmailClient| cut -d ' ' -f 1`
        [ -z $client ] && client="kmail"
        if ! Which $client > /dev/null 2> /dev/null; then
            DEBUG 3 "KDE has $client configured as email client which isn't installed"
            if Which gnome-open > /dev/null 2> /dev/null && Which evolution > /dev/null 2> /dev/null; then
               DEBUG 3 "Try gnome-open instead"
               open_gnome "$1"
            fi
        fi
    fi
    DEBUG 1 "Running kmailservice \"$1\""
    # KDE uses locale's encoding when decoding the URI, so set it to UTF-8
    LC_ALL=C.UTF-8 kmailservice "$1"
    kfmclient_fix_exit_code $?

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

open_gnome()
{
    DEBUG 1 "Running gnome-open \"$1\""
    gnome-open "$1"

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}


open_xfce()
{
    DEBUG 1 "Running exo-open \"$1\""
    exo-open "$1"

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

open_generic()
{
    IFS=":"
    for browser in $BROWSER; do
        if [ x"$browser" != x"" ]; then

            browser_with_arg=`echo "$browser" | sed s#%s#"$1"#`

            if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
            else $browser_with_arg;
            fi

            if [ $? -eq 0 ]; then exit_success;
            fi
        fi
    done

    exit_failure_operation_impossible "no method available for opening '$1'"
}

url_encode()
{
result=`echo "$1" | $utf8 | awk '
    BEGIN {
        for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
        e = ""
        linenr = 1   
    }
    {
        if ( linenr++ != 1 ) {
		e = e "%0D%0A"
		    }
            for ( i=1; i<=length ($0); ++i ) {
		c = substr ($0, i, 1)
		if ( ord [c] > 127 ) {
			e = e "%" sprintf("%02X", ord [c])
			    } else if ( c ~ /[@a-zA-Z0-9.-]/ ) {
			e = e c
			    } else {
			e = e "%" sprintf("%02X", ord [c])
			    }
            }
    }
    END {
        print e
    }
    '`
}

if [ -x /usr/bin/test ]; then
	TestCmd=/usr/bin/test
else
	TestCmd=test
fi

if $TestCmd -e . 2>/dev/null; then
	TestCmdOption="-e"
else
	TestCmdOption="-f"
fi
readlink()
{
    OLDPWD="`pwd`"
    lfile="$1"

    while $TestCmd -h "$lfile" -a $TestCmdOption "$lfile" ; do
        CWD="`dirname "$lfile"`"
        lfile="`basename "$lfile"`"
        cd $CWD
        lfile="`ls -l "$lfile"|sed 's/^.*-> *\(.*\) *$/\1/'`"
    done

    if $TestCmd $TestCmdOption "$lfile" ; then
        CWD="`dirname "$lfile"`"
        lfile="`basename "$lfile"`"
        cd $CWD
        printf "%s\n" "`pwd`/$lfile"
        cd "$OLDPWD"
        return 0
    fi

    cd "$OLDPWD"
    return 1
}


[ x"$1" != x"" ] || exit_failure_syntax

options=
mailto=
utf8="iconv -t utf8"
while [ $# -gt 0 ] ; do
    parm="$1"
    shift

    case "$parm" in
      --utf8)
        utf8="cat"
        ;;

      --to)
        if [ -z "$1" ] ; then
            exit_failure_syntax "email address argument missing for --to"
        fi
        url_encode "$1"
        options="${options}to=${result}&" 
        shift
        ;;

      --cc)
        if [ -z "$1" ] ; then
             exit_failure_syntax "email address argument missing for --cc"
        fi
        url_encode "$1"
        options="${options}cc=${result}&"
        shift
        ;;

      --bcc)
        if [ -z "$1" ] ; then
            exit_failure_syntax "email address argument missing for --bcc"
        fi
        url_encode "$1"
        options="${options}bcc=${result}&"
        shift
        ;;

      --subject)
        if [ -z "$1" ] ; then
            exit_failure_syntax "text argument missing for --subject option"
        fi
        url_encode "$1"
        options="${options}subject=${result}&"
        shift
        ;;

      --body)
        if [ -z "$1" ] ; then
            exit_failure_syntax "text argument missing for --body option"
        fi
        url_encode "$1"
        options="${options}body=${result}&" 
        shift
        ;;

      --attach)
        if [ -z "$1" ] ; then
            exit_failure_syntax "file argument missing for --attach option"
        fi
        check_input_file "$1"
        file=`readlink "$1"` # Normalize path
        if [ -z "$file" -o ! -f "$file" ] ; then
            exit_failure_file_missing "file '$1' does not exist"
        fi

        url_encode "$file"
        options="${options}attach=${result}&"
        shift
        ;;

      -*)
        exit_failure_syntax "unexpected option '$parm'"
        ;;

      mailto:*)
        mailto="$parm"
        ;;

      *@*)
        url_encode "$parm"
        if [ -z "${mailto}" ] ; then
            mailto="mailto:"${result}"?"
        else
            options="${options}to=${result}&"
        fi
        ;;

      *)
        exit_failure_syntax "unexpected argument '$parm'"
        ;;
    esac
done

if [ -z "${mailto}" ] ; then
    exit_failure_syntax "email address or mailto URI argument missing"
fi

case $mailto in
      *\?)
        mailto="${mailto}${options}"
        ;;

      *\?*)
        mailto="${mailto}&${options}"
        ;;

      *)
        mailto="${mailto}?${options}"
        ;;
esac

# Strip trailing ? and &
mailto=`echo "${mailto}"| sed 's/[?&]$//'`

# Shouldn't happen
[ x"${mailto}" != x"" ] || exit_failure_syntax   

if Which xdg-email-hook.sh > /dev/null 2> /dev/null; then
    xdg-email-hook.sh "${mailto}"
    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
fi

detectDE

default_mail_client="`gconftool-2 -g /desktop/gnome/url-handlers/mailto/command 2>/dev/null | cut -d ' ' -f 1 2>/dev/null`"
app_name="`basename $default_mail_client 2>/dev/null`"

if [ "${app_name}" = "thunderbird" ] ; then
    mymailto="`echo $mailto | cut -d '?' -f 2- 2> /dev/null`"
    i=1
    newmailto=""
    while true
    do
        val="`echo $mymailto | cut -d '&' -f $i 2> /dev/null`"
        if [ -z "$val" ] ; then
            break
        fi
        param="`echo $val | cut -d '=' -f 1 2> /dev/null`"
        argu="`echo $val | cut -d '=' -f 2 2> /dev/null`"
        if [ "$param" = "attach" ] ; then
            param="attachment"
            argu="`echo "file://$argu"`"
        fi
        if [ "$i" -eq "1" ] ; then
            newmailto="${param}=\"${argu}\""
        else
            newmailto="${newmailto},${param}=\"${argu}\""
        fi
	if [ "${val}" = "${mailto}" ] ; then
            break
	fi
	i=`expr $i + 1`
    done
    exit_status="`eval \"$default_mail_client -compose $newmailto\"`"
    exit $exit_status
fi

if [ x"$DE" = x"" ]; then
    # if BROWSER variable is not set, check some well known browsers instead
    if [ x"$BROWSER" = x"" ]; then
        BROWSER=firefox:mozilla:netscape
    fi
    DE=generic
fi

case "$DE" in
    kde)
    open_kde "${mailto}"
    ;;

    gnome)
    open_gnome "${mailto}"
    ;;

    xfce)
    open_xfce "${mailto}"
    ;;

    generic)
    open_generic "${mailto}"
    ;;

    *)
    exit_failure_operation_impossible "no method available for opening '${mailto}'"
    ;;
esac
