blob: 0422dab3e7b22fa19ccb422d5fe0c764be559633 [file] [log] [blame]
mdm@chromium.org56df6d32010-08-31 18:16:49 +00001#!/bin/sh
dkegel@google.comf20da1f2009-06-30 19:16:32 +00002#---------------------------------------------
3# xdg-email
4#
5# Utility script to open the users favorite email program, using the
6# RFC 2368 mailto: URI spec
7#
8# Refer to the usage() function below for usage.
9#
mdm@chromium.org741e06f2011-03-30 22:51:57 +000010# Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
11# Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
dkegel@google.comf20da1f2009-06-30 19:16:32 +000012# Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
13# Copyright 2006, Jeremy White <jwhite@codeweavers.com>
14#
15# LICENSE:
16#
17# Permission is hereby granted, free of charge, to any person obtaining a
18# copy of this software and associated documentation files (the "Software"),
19# to deal in the Software without restriction, including without limitation
20# the rights to use, copy, modify, merge, publish, distribute, sublicense,
21# and/or sell copies of the Software, and to permit persons to whom the
22# Software is furnished to do so, subject to the following conditions:
23#
24# The above copyright notice and this permission notice shall be included
25# in all copies or substantial portions of the Software.
26#
27# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33# OTHER DEALINGS IN THE SOFTWARE.
34#
35#---------------------------------------------
36
37manualpage()
38{
39cat << _MANUALPAGE
40Name
41
42xdg-email - command line tool for sending mail using the user's preferred
43e-mail composer
44
45Synopsis
46
47xdg-email [--utf8] [--cc address] [--bcc address] [--subject text] [--body text
48] [--attach file] [ mailto-uri | address(es) ]
49
50xdg-email { --help | --manual | --version }
51
52Description
53
54xdg-email opens the user's preferred e-mail composer in order to send a mail to
55address(es) or mailto-uri. RFC2368 defines mailto: URIs. xdg-email limits
56support to, cc, subject and body fields in mailto-uri, all other fields are
57silently ignored. address(es) must follow the syntax of RFC822. Multiple
58addresses may be provided as separate arguments.
59
60All information provided on the command line is used to prefill corresponding
61fields in the user's e-mail composer. The user will have the opportunity to
62change any of this information before actually sending the e-mail.
63
64xdg-email is for use inside a desktop session only. It is not recommended to
65use xdg-email as root.
66
67See http://portland.freedesktop.org/EmailConfig for information on how the user
68can change the e-mail composer that is used.
69
70Options
71
72--utf8
73 Indicates that all command line options that follow are in utf8. Without
74 this option, command line options are expected to be encoded according to
75 locale. If the locale already specifies utf8 this option has no effect.
76 This option does not affect mailto URIs that are passed on the command
77 line.
78--cc address
79 Specify a recipient to be copied on the e-mail.
80--bcc address
81 Specify a recipient to be blindly copied on the e-mail.
82--subject text
83 Specify a subject for the e-mail.
84--body text
85 Specify a body for the e-mail. Since the user will be able to make changes
86 before actually sending the e-mail, this can be used to provide the user
87 with a template for the e-mail. text may contain linebreaks.
88--attach file
89
90 Specify an attachment for the e-mail. file must point to an existing file.
91
92 Some e-mail applications require the file to remain present after xdg-email
93 returns.
94
95--help
96 Show command synopsis.
97--manual
98 Show this manualpage.
99--version
100 Show the xdg-utils version information.
101
102Environment Variables
103
104xdg-email honours the following environment variables:
105
106XDG_UTILS_DEBUG_LEVEL
107 Setting this environment variable to a non-zero numerical value makes
108 xdg-email do more verbose reporting on stderr. Setting a higher value
109 increases the verbosity.
110
111Exit Codes
112
113An exit code of 0 indicates success while a non-zero exit code indicates
114failure. The following failure codes can be returned:
115
1161
117 Error in command line syntax.
1182
119 One of the files passed on the command line did not exist.
1203
121 A required tool could not be found.
1224
123 The action failed.
1245
125 No permission to read one of the files passed on the command line.
126
127Configuration
128
129Visit http://portland.freedesktop.org/EmailConfig for information how to
130configure xdg-email to use the email client of your choice.
131
132Examples
133
134xdg-email 'Jeremy White <jwhite@example.com>'
135
136xdg-email --attach /tmp/logo.png \
137 --subject 'Logo contest' \
138 --body 'Attached you find the logo for the contest.' \
139 'jwhite@example.com'
140
141xdg-email --subject 'Your password is about to expire' \
142 'jwhite@example.com' 'bastian@example.com' 'whipple@example.com'
143
144_MANUALPAGE
145}
146
147usage()
148{
149cat << _USAGE
150xdg-email - command line tool for sending mail using the user's preferred
151e-mail composer
152
153Synopsis
154
155xdg-email [--utf8] [--cc address] [--bcc address] [--subject text] [--body text
156] [--attach file] [ mailto-uri | address(es) ]
157
158xdg-email { --help | --manual | --version }
159
160_USAGE
161}
162
163#@xdg-utils-common@
164
165#----------------------------------------------------------------------------
166# Common utility functions included in all XDG wrapper scripts
167#----------------------------------------------------------------------------
168
169DEBUG()
170{
171 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
172 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
173 shift
174 echo "$@" >&2
175}
176
177#-------------------------------------------------------------
178# Exit script on successfully completing the desired operation
179
180exit_success()
181{
182 if [ $# -gt 0 ]; then
183 echo "$@"
184 echo
185 fi
186
187 exit 0
188}
189
190
191#-----------------------------------------
192# Exit script on malformed arguments, not enough arguments
193# or missing required option.
194# prints usage information
195
196exit_failure_syntax()
197{
198 if [ $# -gt 0 ]; then
199 echo "xdg-email: $@" >&2
200 echo "Try 'xdg-email --help' for more information." >&2
201 else
202 usage
203 echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
204 fi
205
206 exit 1
207}
208
209#-------------------------------------------------------------
210# Exit script on missing file specified on command line
211
212exit_failure_file_missing()
213{
214 if [ $# -gt 0 ]; then
215 echo "xdg-email: $@" >&2
216 fi
217
218 exit 2
219}
220
221#-------------------------------------------------------------
222# Exit script on failure to locate necessary tool applications
223
224exit_failure_operation_impossible()
225{
226 if [ $# -gt 0 ]; then
227 echo "xdg-email: $@" >&2
228 fi
229
230 exit 3
231}
232
233#-------------------------------------------------------------
234# Exit script on failure returned by a tool application
235
236exit_failure_operation_failed()
237{
238 if [ $# -gt 0 ]; then
239 echo "xdg-email: $@" >&2
240 fi
241
242 exit 4
243}
244
245#------------------------------------------------------------
246# Exit script on insufficient permission to read a specified file
247
248exit_failure_file_permission_read()
249{
250 if [ $# -gt 0 ]; then
251 echo "xdg-email: $@" >&2
252 fi
253
254 exit 5
255}
256
257#------------------------------------------------------------
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000258# Exit script on insufficient permission to write a specified file
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000259
260exit_failure_file_permission_write()
261{
262 if [ $# -gt 0 ]; then
263 echo "xdg-email: $@" >&2
264 fi
265
266 exit 6
267}
268
269check_input_file()
270{
271 if [ ! -e "$1" ]; then
272 exit_failure_file_missing "file '$1' does not exist"
273 fi
274 if [ ! -r "$1" ]; then
275 exit_failure_file_permission_read "no permission to read file '$1'"
276 fi
277}
278
279check_vendor_prefix()
280{
281 file_label="$2"
282 [ -n "$file_label" ] || file_label="filename"
283 file=`basename "$1"`
284 case "$file" in
285 [a-zA-Z]*-*)
286 return
287 ;;
288 esac
289
290 echo "xdg-email: $file_label '$file' does not have a proper vendor prefix" >&2
291 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
292 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
293 echo "Use --novendor to override or 'xdg-email --manual' for additional info." >&2
294 exit 1
295}
296
297check_output_file()
298{
299 # if the file exists, check if it is writeable
300 # if it does not exists, check if we are allowed to write on the directory
301 if [ -e "$1" ]; then
302 if [ ! -w "$1" ]; then
303 exit_failure_file_permission_write "no permission to write to file '$1'"
304 fi
305 else
306 DIR=`dirname "$1"`
307 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
308 exit_failure_file_permission_write "no permission to create file '$1'"
309 fi
310 fi
311}
312
313#----------------------------------------
314# Checks for shared commands, e.g. --help
315
316check_common_commands()
317{
318 while [ $# -gt 0 ] ; do
319 parm="$1"
320 shift
321
322 case "$parm" in
323 --help)
324 usage
325 echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
326 exit_success
327 ;;
328
329 --manual)
330 manualpage
331 exit_success
332 ;;
333
334 --version)
mdm@chromium.org7bf13222011-03-30 23:20:46 +0000335 echo "xdg-email 1.1.0 rc1"
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000336 exit_success
337 ;;
338 esac
339 done
340}
341
342check_common_commands "$@"
343
344[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
345if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
346 # Be silent
347 xdg_redirect_output=" > /dev/null 2> /dev/null"
348else
349 # All output to stderr
350 xdg_redirect_output=" >&2"
351fi
352
353#--------------------------------------
354# Checks for known desktop environments
355# set variable DE to the desktop environments name, lowercase
356
357detectDE()
358{
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000359 # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
360 unset GREP_OPTIONS
361
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000362 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
363 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000364 elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
365 elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000366 elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
mdm@chromium.org672539c2011-04-05 03:39:34 +0000367 fi
368
369 if [ x"$DE" = x"" ]; then
370 # fallback to checking $DESKTOP_SESSION
371 case "$DESKTOP_SESSION" in
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000372 gnome)
373 DE=gnome;
mdm@chromium.org672539c2011-04-05 03:39:34 +0000374 LXDE)
375 DE=lxde;
376 ;;
377 xfce|xfce4)
378 DE=xfce;
379 ;;
380 esac
381 fi
382
383 if [ x"$DE" = x"" ]; then
384 # fallback to uname output for other platforms
385 case "$(uname 2>/dev/null)" in
386 Darwin)
387 DE=darwin;
388 ;;
389 esac
390 fi
391
392 if [ x"$DE" = x"gnome" ]; then
393 # gnome-default-applications-properties is only available in GNOME 2.x
394 # but not in GNOME 3.x
mdm@chromium.orga4f19d42011-04-09 00:50:42 +0000395 which gnome-default-applications-properties > /dev/null 2> /dev/null || DE="gnome3"
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000396 fi
397}
398
399#----------------------------------------------------------------------------
400# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
401# It also always returns 1 in KDE 3.4 and earlier
402# Simply return 0 in such case
403
404kfmclient_fix_exit_code()
405{
mdm@chromium.org7bf13222011-03-30 23:20:46 +0000406 version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
407 major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
408 minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
409 release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000410 test "$major" -gt 3 && return $1
411 test "$minor" -gt 5 && return $1
412 test "$release" -gt 4 && return $1
413 return 0
414}
415
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000416run_thunderbird()
417{
418 local THUNDERBIRD MAILTO NEWMAILTO TO CC BCC SUBJECT BODY ATTACH
419 THUNDERBIRD="$1"
420 MAILTO=$(echo "$2" | sed 's/^mailto://')
421 echo "$MAILTO" | grep -qs "^?"
422 if [ "$?" = "0" ] ; then
423 MAILTO=$(echo "$MAILTO" | sed 's/^?//')
424 else
425 MAILTO=$(echo "$MAILTO" | sed 's/^/to=/' | sed 's/?/\&/')
426 fi
427
428 MAILTO=$(echo "$MAILTO" | sed 's/&/\n/g')
429 TO=$(echo "$MAILTO" | grep '^to=' | sed 's/^to=//' | awk '{ printf "%s,",$0 }')
430 CC=$(echo "$MAILTO" | grep '^cc=' | sed 's/^cc=//' | awk '{ printf "%s,",$0 }')
431 BCC=$(echo "$MAILTO" | grep '^bcc=' | sed 's/^bcc=//' | awk '{ printf "%s,",$0 }')
432 SUBJECT=$(echo "$MAILTO" | grep '^subject=' | tail -n 1)
433 BODY=$(echo "$MAILTO" | grep '^body=' | tail -n 1)
mdm@chromium.org741e06f2011-03-30 22:51:57 +0000434 ATTACH=$(echo "$MAILTO" | sed 's/^attach=/\n\nfile:\/\//g' | awk '/^file:/ { printf "%s,",$0 }' | sed 's/,$//')
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000435
436 if [ -z "$TO" ] ; then
437 NEWMAILTO=
438 else
439 NEWMAILTO="to='$TO'"
440 fi
441 if [ -n "$CC" ] ; then
442 NEWMAILTO="${NEWMAILTO},cc='$CC'"
443 fi
444 if [ -n "$BCC" ] ; then
445 NEWMAILTO="${NEWMAILTO},bcc='$BCC'"
446 fi
447 if [ -n "$SUBJECT" ] ; then
448 NEWMAILTO="${NEWMAILTO},$SUBJECT"
449 fi
450 if [ -n "$BODY" ] ; then
451 NEWMAILTO="${NEWMAILTO},$BODY"
452 fi
453
454 if [ -n "$ATTACH" ] ; then
455 NEWMAILTO="${NEWMAILTO},attachment='${ATTACH}'"
456 fi
457
458 NEWMAILTO=$(echo "$NEWMAILTO" | sed 's/^,//')
459 DEBUG 1 "Running $THUNDERBIRD -compose \"$NEWMAILTO\""
460 "$THUNDERBIRD" -compose "$NEWMAILTO"
461 if [ $? -eq 0 ]; then
462 exit_success
463 else
464 exit_failure_operation_failed
465 fi
466}
467
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000468open_kde()
469{
mdm@chromium.org7bf13222011-03-30 23:20:46 +0000470 local client kde_email_profile_name
471 kde_email_profile_name=`kreadconfig --file emaildefaults --group Defaults --key Profile`
472 client=`kreadconfig --file emaildefaults --group PROFILE_"$kde_email_profile_name" --key EmailClient | cut -d ' ' -f 1`
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000473 echo $client | grep thunderbird > /dev/null 2>&1
474 if [ $? -eq 0 ] ; then
475 run_thunderbird "$client" "$1"
476 fi
477
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000478 if [ -f /etc/SuSE-release ] ; then
479 # Workaround for SUSE 10.0
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000480 [ -z "$client" ] && client="kmail"
481 if ! which "$client" > /dev/null 2> /dev/null; then
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000482 DEBUG 3 "KDE has $client configured as email client which isn't installed"
483 if which gnome-open > /dev/null 2> /dev/null && which evolution > /dev/null 2> /dev/null; then
484 DEBUG 3 "Try gnome-open instead"
485 open_gnome "$1"
486 fi
487 fi
488 fi
489 DEBUG 1 "Running kmailservice \"$1\""
mdm@chromium.orgad856b92009-07-27 21:50:25 +0000490 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
491 KMAILSERVICE=`kde4-config --locate kmailservice --path exe 2>/dev/null`
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000492 $KMAILSERVICE "$1"
mdm@chromium.orgad856b92009-07-27 21:50:25 +0000493 else
494 KMAILSERVICE=`which kmailservice 2>/dev/null`
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000495 # KDE3 uses locale's encoding when decoding the URI, so set it to UTF-8
496 LC_ALL=C.UTF-8 $KMAILSERVICE "$1"
mdm@chromium.orgad856b92009-07-27 21:50:25 +0000497 fi
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000498
499 if [ $? -eq 0 ]; then
500 exit_success
501 else
502 exit_failure_operation_failed
503 fi
504}
505
mdm@chromium.org672539c2011-04-05 03:39:34 +0000506open_gnome3()
507{
508 local client
509 local desktop
510 desktop=`xdg-mime query default "x-scheme-handler/mailto"`
511 client=`desktop_file_to_binary "$browser"`
512 echo $client | grep thunderbird > /dev/null 2>&1
513 if [ $? -eq 0 ] ; then
514 run_thunderbird "$client" "$1"
515 fi
516
517 if gvfs-open --help 2>/dev/null 1>&2; then
518 DEBUG 1 "Running gvfs-open \"$1\""
519 gvfs-open "$1"
520 else
521 DEBUG 1 "Running gnome-open \"$1\""
522 gnome-open "$1"
523 fi
524
525 if [ $? -eq 0 ]; then
526 exit_success
527 else
528 exit_failure_operation_failed
529 fi
530}
531
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000532open_gnome()
533{
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000534 local client
535 client=`gconftool-2 --get /desktop/gnome/url-handlers/mailto/command | cut -d ' ' -f 1` || ""
536 echo $client | grep thunderbird > /dev/null 2>&1
537 if [ $? -eq 0 ] ; then
538 run_thunderbird "$client" "$1"
539 fi
540
541 if gvfs-open --help 2>/dev/null 1>&2; then
542 DEBUG 1 "Running gvfs-open \"$1\""
543 gvfs-open "$1"
544 else
545 DEBUG 1 "Running gnome-open \"$1\""
546 gnome-open "$1"
547 fi
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000548
549 if [ $? -eq 0 ]; then
550 exit_success
551 else
552 exit_failure_operation_failed
553 fi
554}
555
556
557open_xfce()
558{
559 DEBUG 1 "Running exo-open \"$1\""
560 exo-open "$1"
561
562 if [ $? -eq 0 ]; then
563 exit_success
564 else
565 exit_failure_operation_failed
566 fi
567}
568
569open_generic()
570{
571 IFS=":"
572 for browser in $BROWSER; do
573 if [ x"$browser" != x"" ]; then
574
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000575 browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
576 if [ $? -ne 0 ]; then browser_with_arg=$browser;
577 fi
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000578
579 if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
580 else $browser_with_arg;
581 fi
582
583 if [ $? -eq 0 ]; then exit_success;
584 fi
585 fi
586 done
587
588 exit_failure_operation_impossible "no method available for opening '$1'"
589}
590
591url_encode()
592{
593result=$(echo "$1" | $utf8 | awk '
594 BEGIN {
595 for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
596 e = ""
597 linenr = 1
598 }
599 {
600 if ( linenr++ != 1 ) {
601 e = e "%0D%0A"
602 }
603 for ( i=1; i<=length ($0); ++i ) {
604 c = substr ($0, i, 1)
605 if ( ord [c] > 127 ) {
606 e = e "%" sprintf("%02X", ord [c])
mdm@chromium.org741e06f2011-03-30 22:51:57 +0000607 } else if ( c ~ /[@a-zA-Z0-9.-\\\/]/ ) {
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000608 e = e c
609 } else {
610 e = e "%" sprintf("%02X", ord [c])
611 }
612 }
613 }
614 END {
615 print e
616 }
617')
618}
619
620options=
621mailto=
622utf8="iconv -t utf8"
623while [ $# -gt 0 ] ; do
624 parm="$1"
625 shift
626
627 case "$parm" in
628 --utf8)
629 utf8="cat"
630 ;;
631
632 --to)
633 if [ -z "$1" ] ; then
634 exit_failure_syntax "email address argument missing for --to"
635 fi
636 url_encode "$1"
637 options="${options}to=${result}&"
638 shift
639 ;;
640
641 --cc)
642 if [ -z "$1" ] ; then
643 exit_failure_syntax "email address argument missing for --cc"
644 fi
645 url_encode "$1"
646 options="${options}cc=${result}&"
647 shift
648 ;;
649
650 --bcc)
651 if [ -z "$1" ] ; then
652 exit_failure_syntax "email address argument missing for --bcc"
653 fi
654 url_encode "$1"
655 options="${options}bcc=${result}&"
656 shift
657 ;;
658
659 --subject)
660 if [ -z "$1" ] ; then
661 exit_failure_syntax "text argument missing for --subject option"
662 fi
663 url_encode "$1"
664 options="${options}subject=${result}&"
665 shift
666 ;;
667
668 --body)
669 if [ -z "$1" ] ; then
670 exit_failure_syntax "text argument missing for --body option"
671 fi
672 url_encode "$1"
673 options="${options}body=${result}&"
674 shift
675 ;;
676
677 --attach)
678 if [ -z "$1" ] ; then
679 exit_failure_syntax "file argument missing for --attach option"
680 fi
681 check_input_file "$1"
682 file=`readlink -f "$1"` # Normalize path
683 if [ -z "$file" -o ! -f "$file" ] ; then
684 exit_failure_file_missing "file '$1' does not exist"
685 fi
686
687 url_encode "$file"
688 options="${options}attach=${result}&"
689 shift
690 ;;
691
692 -*)
693 exit_failure_syntax "unexpected option '$parm'"
694 ;;
695
696 mailto:*)
697 mailto="$parm"
698 ;;
699
700 *@*)
701 url_encode "$parm"
702 if [ -z "${mailto}" ] ; then
703 mailto="mailto:"${result}"?"
704 else
705 options="${options}to=${result}&"
706 fi
707 ;;
708
709 *)
710 exit_failure_syntax "unexpected argument '$parm'"
711 ;;
712 esac
713done
714
715if [ -z "${mailto}" ] ; then
716 # TO address is optional
717 mailto="mailto:?"
718fi
719
720case $mailto in
721 *\?)
722 mailto="${mailto}${options}"
723 ;;
724
725 *\?*)
726 mailto="${mailto}&${options}"
727 ;;
728
729 *)
730 mailto="${mailto}?${options}"
731 ;;
732esac
733
734# Strip trailing ? and &
735mailto=`echo "${mailto}"| sed 's/[?&]$//'`
736
737# Shouldn't happen
738[ x"${mailto}" != x"" ] || exit_failure_syntax
739
740if which xdg-email-hook.sh > /dev/null 2> /dev/null; then
741 xdg-email-hook.sh "${mailto}"
742 if [ $? -eq 0 ]; then
743 exit_success
744 else
745 exit_failure_operation_failed
746 fi
747fi
748
749detectDE
750
751if [ x"$DE" = x"" ]; then
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000752 DE=generic
753fi
754
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000755# if BROWSER variable is not set, check some well known browsers instead
756if [ x"$BROWSER" = x"" ]; then
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000757 BROWSER=links2:elinks:links:lynx:w3m
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000758 if [ -n "$DISPLAY" ]; then
mdm@chromium.orgd4d2c6a2011-04-20 01:40:01 +0000759 BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000760 fi
761fi
762
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000763case "$DE" in
764 kde)
765 open_kde "${mailto}"
766 ;;
767
768 gnome)
769 open_gnome "${mailto}"
770 ;;
771
mdm@chromium.org672539c2011-04-05 03:39:34 +0000772 gnome3)
773 open_gnome3 "${mailto}"
774 ;;
775
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000776 xfce)
777 open_xfce "${mailto}"
778 ;;
779
mdm@chromium.org7bf13222011-03-30 23:20:46 +0000780 generic|lxde)
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000781 open_generic "${mailto}"
782 ;;
783
784 *)
785 exit_failure_operation_impossible "no method available for opening '${mailto}'"
786 ;;
787esac