2
#---------------------------------------------
5
# Utility script to open the users favorite email program, using the
6
# RFC 2368 mailto: URI spec
8
# Refer to the usage() function below for usage.
10
# Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
11
# Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
12
# Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
13
# Copyright 2006, Jeremy White <jwhite@codeweavers.com>
17
#---------------------------------------------
35
local THUNDERBIRD MAILTO NEWMAILTO TO CC BCC SUBJECT BODY ATTACH
37
MAILTO=$(echo "$2" | sed 's/^mailto://')
38
echo "$MAILTO" | grep -qs "^?"
39
if [ "$?" = "0" ] ; then
40
MAILTO=$(echo "$MAILTO" | sed 's/^?//')
42
MAILTO=$(echo "$MAILTO" | sed 's/^/to=/' | sed 's/?/\&/')
45
MAILTO=$(echo "$MAILTO" | sed 's/&/\n/g')
46
TO=$(echo -e $(echo "$MAILTO" | grep '^to=' | sed 's/^to=//;s/%\(..\)/\\x\1/g' | awk '{ printf "\"%s\",",$0 }'))
47
CC=$(echo -e $(echo "$MAILTO" | grep '^cc=' | sed 's/^cc=//;s/%\(..\)/\\x\1/g' | awk '{ printf "\"%s\",",$0 }'))
48
BCC=$(echo -e $(echo "$MAILTO" | grep '^bcc=' | sed 's/^bcc=//;s/%\(..\)/\\x\1/g' | awk '{ printf "\"%s\",",$0 }'))
49
SUBJECT=$(echo "$MAILTO" | grep '^subject=' | tail -n 1)
50
BODY=$(echo "$MAILTO" | grep '^body=' | tail -n 1)
51
ATTACH=$(echo -e $(echo "$MAILTO" | grep '^attach=' | sed 's/^attach=//;s/%\(..\)/\\x\1/g' | awk '{ printf "%s,",$0 }' | sed 's/,$//'))
53
if [ -z "$TO" ] ; then
58
if [ -n "$CC" ] ; then
59
NEWMAILTO="${NEWMAILTO},cc='$CC'"
61
if [ -n "$BCC" ] ; then
62
NEWMAILTO="${NEWMAILTO},bcc='$BCC'"
64
if [ -n "$SUBJECT" ] ; then
65
NEWMAILTO="${NEWMAILTO},$SUBJECT"
67
if [ -n "$BODY" ] ; then
68
NEWMAILTO="${NEWMAILTO},$BODY"
71
if [ -n "$ATTACH" ] ; then
72
NEWMAILTO="${NEWMAILTO},attachment='${ATTACH}'"
75
NEWMAILTO=$(echo "$NEWMAILTO" | sed 's/^,//')
76
DEBUG 1 "Running $THUNDERBIRD -compose \"$NEWMAILTO\""
77
"$THUNDERBIRD" -compose "$NEWMAILTO"
81
exit_failure_operation_failed
87
local client kde_email_profile_name
88
kde_email_profile_name=`kreadconfig --file emaildefaults --group Defaults --key Profile`
89
client=`kreadconfig --file emaildefaults --group PROFILE_"$kde_email_profile_name" --key EmailClient | cut -d ' ' -f 1`
90
echo $client | grep thunderbird > /dev/null 2>&1
91
if [ $? -eq 0 ] ; then
92
run_thunderbird "$client" "$1"
95
if [ -f /etc/SuSE-release ] ; then
96
# Workaround for SUSE 10.0
97
[ -z "$client" ] && client="kmail"
98
if ! which "$client" > /dev/null 2> /dev/null; then
99
DEBUG 3 "KDE has $client configured as email client which isn't installed"
100
if which gnome-open > /dev/null 2> /dev/null && which evolution > /dev/null 2> /dev/null; then
101
DEBUG 3 "Try gnome-open instead"
106
DEBUG 1 "Running kmailservice \"$1\""
107
if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
108
KMAILSERVICE=`kde4-config --locate kmailservice --path exe 2>/dev/null`
111
KMAILSERVICE=`which kmailservice 2>/dev/null`
112
# KDE3 uses locale's encoding when decoding the URI, so set it to UTF-8
113
LC_ALL=C.UTF-8 $KMAILSERVICE "$1"
116
if [ $? -eq 0 ]; then
119
exit_failure_operation_failed
127
desktop=`xdg-mime query default "x-scheme-handler/mailto"`
128
client=`desktop_file_to_binary "$browser"`
129
echo $client | grep thunderbird > /dev/null 2>&1
130
if [ $? -eq 0 ] ; then
131
run_thunderbird "$client" "$1"
134
if gvfs-open --help 2>/dev/null 1>&2; then
135
DEBUG 1 "Running gvfs-open \"$1\""
138
DEBUG 1 "Running gnome-open \"$1\""
142
if [ $? -eq 0 ]; then
145
exit_failure_operation_failed
152
client=`gconftool-2 --get /desktop/gnome/url-handlers/mailto/command | cut -d ' ' -f 1` || ""
153
echo $client | grep thunderbird > /dev/null 2>&1
154
if [ $? -eq 0 ] ; then
155
run_thunderbird "$client" "$1"
158
if gvfs-open --help 2>/dev/null 1>&2; then
159
DEBUG 1 "Running gvfs-open \"$1\""
162
DEBUG 1 "Running gnome-open \"$1\""
166
if [ $? -eq 0 ]; then
169
exit_failure_operation_failed
176
DEBUG 1 "Running exo-open \"$1\""
179
if [ $? -eq 0 ]; then
182
exit_failure_operation_failed
195
if [ $? -eq 0 ]; then
200
exit_failure_operation_failed
206
for browser in $BROWSER; do
207
if [ x"$browser" != x"" ]; then
209
browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
210
if [ $? -ne 0 ]; then browser_with_arg=$browser;
213
if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
214
else $browser_with_arg;
217
if [ $? -eq 0 ]; then exit_success;
222
exit_failure_operation_impossible "no method available for opening '$1'"
227
str=$(echo "$1" | $utf8)
228
local ORIG_LANG="$LANG"
229
local ORIG_LC_ALL="$LC_ALL"
232
result=$(echo "$str" | awk '
234
for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
239
if ( linenr++ != 1 ) {
242
for ( i=1; i<=length ($0); ++i ) {
243
c = substr ($0, i, 1)
244
if ( ord [c] > 127 ) {
245
e = e "%" sprintf("%02X", ord [c])
246
} else if ( c ~ /[@a-zA-Z0-9.-\\\/]/ ) {
249
e = e "%" sprintf("%02X", ord [c])
258
LC_ALL="$ORIG_LC_ALL"
264
while [ $# -gt 0 ] ; do
274
if [ -z "$1" ] ; then
275
exit_failure_syntax "email address argument missing for --to"
278
options="${options}to=${result}&"
283
if [ -z "$1" ] ; then
284
exit_failure_syntax "email address argument missing for --cc"
287
options="${options}cc=${result}&"
292
if [ -z "$1" ] ; then
293
exit_failure_syntax "email address argument missing for --bcc"
296
options="${options}bcc=${result}&"
301
if [ -z "$1" ] ; then
302
exit_failure_syntax "text argument missing for --subject option"
305
options="${options}subject=${result}&"
310
if [ -z "$1" ] ; then
311
exit_failure_syntax "text argument missing for --body option"
314
options="${options}body=${result}&"
319
if [ -z "$1" ] ; then
320
exit_failure_syntax "file argument missing for --attach option"
322
check_input_file "$1"
323
file=`readlink -f "$1"` # Normalize path
324
if [ -z "$file" ] || [ ! -f "$file" ] ; then
325
exit_failure_file_missing "file '$1' does not exist"
329
options="${options}attach=${result}&"
334
exit_failure_syntax "unexpected option '$parm'"
343
if [ -z "${mailto}" ] ; then
344
mailto="mailto:"${result}"?"
346
options="${options}to=${result}&"
351
exit_failure_syntax "unexpected argument '$parm'"
356
if [ -z "${mailto}" ] ; then
357
# TO address is optional
363
mailto="${mailto}${options}"
367
mailto="${mailto}&${options}"
371
mailto="${mailto}?${options}"
375
# Strip trailing ? and &
376
mailto=`echo "${mailto}"| sed 's/[?&]$//'`
379
[ x"${mailto}" != x"" ] || exit_failure_syntax
381
if which @NAME@-hook.sh > /dev/null 2> /dev/null; then
382
@NAME@-hook.sh "${mailto}"
383
if [ $? -eq 0 ]; then
386
exit_failure_operation_failed
392
if [ x"$DE" = x"" ]; then
396
if [ -r ~/.muttrc ] || [ -r ~/.mutt/muttrc ] && \
397
which mutt >/dev/null 2>&1; then
398
MAILER="x-terminal-emulator -e mutt"
401
if [ x"$MAILER" != x"" ]; then
405
# if BROWSER variable is not set, check some well known browsers instead
406
if [ x"$BROWSER" = x"" ]; then
407
BROWSER=links2:elinks:links:lynx:w3m
408
if [ -n "$DISPLAY" ]; then
409
BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
415
open_envvar "${mailto}"
423
open_gnome "${mailto}"
427
open_gnome3 "${mailto}"
431
open_xfce "${mailto}"
435
open_generic "${mailto}"
439
exit_failure_operation_impossible "no method available for opening '${mailto}'"