#!/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 # Copyright 2006, Jeremy White # # LICENSE: # #--------------------------------------------- manualpage() { cat << _MANUALPAGE _MANUALPAGE } usage() { cat << _USAGE _USAGE } #@xdg-utils-common@ open_kde() { local client 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 } ') } 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 -f "$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 # TO address is optional mailto="mailto:?" 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 @NAME@-hook.sh > /dev/null 2> /dev/null; then @NAME@-hook.sh "${mailto}" if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi fi detectDE 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