#!/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() { kfmclient exec "$1" if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi } open_gnome() { gnome-open "$1" if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi } open_xfce() { exo-open "$1" if [ $? -eq 0 ]; then exit_success else exit_failure_operation_failed fi } 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 ( c ~ /[@a-zA-Z0-9.-]/ ) { e = e c } else { e = e "%" sprintf("%02X", ord [c]) } } } END { print e } ') } [ 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 --cc" 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 url_encode "$1" options=${options}"attach="${result}"&" shift ;; -*) exit_failure_syntax "unexpected option '$parm'" ;; *@*) url_encode "$parm" if [ -z "${mailto}" ] ; then mailto="mailto:"${result}"?" else options=${options}"to="${result}"&" fi ;; mailto:*) mailto="$parm" ;; *) 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 # Shouldn't happen [ x"${mailto}" != x"" ] || exit_failure_syntax detectDE case "$DE" in kde) open_kde "${mailto}" ;; gnome) open_gnome "${mailto}" ;; xfce) open_xfce "${mailto}" ;; *) exit_failure_operation_impossible "no method available for opening '${mailto}'" ;; esac