1#!/bin/sh
2#---------------------------------------------
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#
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>
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# This handles backslashes but not quote marks.
178first_word()
179{
180    read first rest
181    echo "$first"
182}
183
184#-------------------------------------------------------------
185# map a binary to a .desktop file
186binary_to_desktop_file()
187{
188    search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
189    binary="`which "$1"`"
190    binary="`readlink -f "$binary"`"
191    base="`basename "$binary"`"
192    IFS=:
193    for dir in $search; do
194        unset IFS
195        [ "$dir" ] || continue
196        [ -d "$dir/applications" -o -d "$dir/applnk" ] || continue
197        for file in "$dir"/applications/*.desktop "$dir"/applications/*/*.desktop "$dir"/applnk/*.desktop "$dir"/applnk/*/*.desktop; do
198            [ -r "$file" ] || continue
199            # Check to make sure it's worth the processing.
200            grep -q "^Exec.*$base" "$file" || continue
201            # Make sure it's a visible desktop file (e.g. not "preferred-web-browser.desktop").
202            grep -Eq "^(NoDisplay|Hidden)=true" "$file" && continue
203            command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
204            command="`which "$command"`"
205            if [ x"`readlink -f "$command"`" = x"$binary" ]; then
206                # Fix any double slashes that got added path composition
207                echo "$file" | sed -e 's,//*,/,g'
208                return
209            fi
210        done
211    done
212}
213
214#-------------------------------------------------------------
215# map a .desktop file to a binary
216## FIXME: handle vendor dir case
217desktop_file_to_binary()
218{
219    search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
220    desktop="`basename "$1"`"
221    IFS=:
222    for dir in $search; do
223        unset IFS
224        [ "$dir" -a -d "$dir/applications" ] || continue
225        file="$dir/applications/$desktop"
226        [ -r "$file" ] || continue
227        # Remove any arguments (%F, %f, %U, %u, etc.).
228        command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
229        command="`which "$command"`"
230        readlink -f "$command"
231        return
232    done
233}
234
235#-------------------------------------------------------------
236# Exit script on successfully completing the desired operation
237
238exit_success()
239{
240    if [ $# -gt 0 ]; then
241        echo "$@"
242        echo
243    fi
244
245    exit 0
246}
247
248
249#-----------------------------------------
250# Exit script on malformed arguments, not enough arguments
251# or missing required option.
252# prints usage information
253
254exit_failure_syntax()
255{
256    if [ $# -gt 0 ]; then
257        echo "xdg-email: $@" >&2
258        echo "Try 'xdg-email --help' for more information." >&2
259    else
260        usage
261        echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
262    fi
263
264    exit 1
265}
266
267#-------------------------------------------------------------
268# Exit script on missing file specified on command line
269
270exit_failure_file_missing()
271{
272    if [ $# -gt 0 ]; then
273        echo "xdg-email: $@" >&2
274    fi
275
276    exit 2
277}
278
279#-------------------------------------------------------------
280# Exit script on failure to locate necessary tool applications
281
282exit_failure_operation_impossible()
283{
284    if [ $# -gt 0 ]; then
285        echo "xdg-email: $@" >&2
286    fi
287
288    exit 3
289}
290
291#-------------------------------------------------------------
292# Exit script on failure returned by a tool application
293
294exit_failure_operation_failed()
295{
296    if [ $# -gt 0 ]; then
297        echo "xdg-email: $@" >&2
298    fi
299
300    exit 4
301}
302
303#------------------------------------------------------------
304# Exit script on insufficient permission to read a specified file
305
306exit_failure_file_permission_read()
307{
308    if [ $# -gt 0 ]; then
309        echo "xdg-email: $@" >&2
310    fi
311
312    exit 5
313}
314
315#------------------------------------------------------------
316# Exit script on insufficient permission to write a specified file
317
318exit_failure_file_permission_write()
319{
320    if [ $# -gt 0 ]; then
321        echo "xdg-email: $@" >&2
322    fi
323
324    exit 6
325}
326
327check_input_file()
328{
329    if [ ! -e "$1" ]; then
330        exit_failure_file_missing "file '$1' does not exist"
331    fi
332    if [ ! -r "$1" ]; then
333        exit_failure_file_permission_read "no permission to read file '$1'"
334    fi
335}
336
337check_vendor_prefix()
338{
339    file_label="$2"
340    [ -n "$file_label" ] || file_label="filename"
341    file=`basename "$1"`
342    case "$file" in
343       [a-zA-Z]*-*)
344         return
345         ;;
346    esac
347
348    echo "xdg-email: $file_label '$file' does not have a proper vendor prefix" >&2
349    echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
350    echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
351    echo "Use --novendor to override or 'xdg-email --manual' for additional info." >&2
352    exit 1
353}
354
355check_output_file()
356{
357    # if the file exists, check if it is writeable
358    # if it does not exists, check if we are allowed to write on the directory
359    if [ -e "$1" ]; then
360        if [ ! -w "$1" ]; then
361            exit_failure_file_permission_write "no permission to write to file '$1'"
362        fi
363    else
364        DIR=`dirname "$1"`
365        if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
366            exit_failure_file_permission_write "no permission to create file '$1'"
367        fi
368    fi
369}
370
371#----------------------------------------
372# Checks for shared commands, e.g. --help
373
374check_common_commands()
375{
376    while [ $# -gt 0 ] ; do
377        parm="$1"
378        shift
379
380        case "$parm" in
381            --help)
382            usage
383            echo "Use 'man xdg-email' or 'xdg-email --manual' for additional info."
384            exit_success
385            ;;
386
387            --manual)
388            manualpage
389            exit_success
390            ;;
391
392            --version)
393            echo "xdg-email 1.1.0 rc1"
394            exit_success
395            ;;
396        esac
397    done
398}
399
400check_common_commands "$@"
401
402[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
403if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
404    # Be silent
405    xdg_redirect_output=" > /dev/null 2> /dev/null"
406else
407    # All output to stderr
408    xdg_redirect_output=" >&2"
409fi
410
411#--------------------------------------
412# Checks for known desktop environments
413# set variable DE to the desktop environments name, lowercase
414
415detectDE()
416{
417    # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
418    unset GREP_OPTIONS
419
420    if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
421      case "${XDG_CURRENT_DESKTOP}" in
422         GNOME)
423           DE=gnome;
424           ;;
425         KDE)
426           DE=kde;
427           ;;
428         LXDE)
429           DE=lxde;
430           ;;
431         XFCE)
432           DE=xfce
433      esac
434    fi
435
436    if [ x"$DE" = x"" ]; then
437      # classic fallbacks
438      if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
439      elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
440      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;
441      elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
442      elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
443      fi
444    fi
445
446    if [ x"$DE" = x"" ]; then
447      # fallback to checking $DESKTOP_SESSION
448      case "$DESKTOP_SESSION" in
449         gnome)
450           DE=gnome;
451           ;;
452         LXDE)
453           DE=lxde;
454           ;;
455         xfce|xfce4)
456           DE=xfce;
457           ;;
458      esac
459    fi
460
461    if [ x"$DE" = x"" ]; then
462      # fallback to uname output for other platforms
463      case "$(uname 2>/dev/null)" in
464        Darwin)
465          DE=darwin;
466          ;;
467      esac
468    fi
469
470    if [ x"$DE" = x"gnome" ]; then
471      # gnome-default-applications-properties is only available in GNOME 2.x
472      # but not in GNOME 3.x
473      which gnome-default-applications-properties > /dev/null 2>&1  || DE="gnome3"
474    fi
475}
476
477#----------------------------------------------------------------------------
478# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
479# It also always returns 1 in KDE 3.4 and earlier
480# Simply return 0 in such case
481
482kfmclient_fix_exit_code()
483{
484    version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
485    major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
486    minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
487    release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
488    test "$major" -gt 3 && return $1
489    test "$minor" -gt 5 && return $1
490    test "$release" -gt 4 && return $1
491    return 0
492}
493
494run_thunderbird()
495{
496    local THUNDERBIRD MAILTO NEWMAILTO TO CC BCC SUBJECT BODY ATTACH
497    THUNDERBIRD="$1"
498    MAILTO=$(echo "$2" | sed 's/^mailto://')
499    echo "$MAILTO" | grep -qs "^?"
500    if [ "$?" = "0" ] ; then
501        MAILTO=$(echo "$MAILTO" | sed 's/^?//')
502    else
503        MAILTO=$(echo "$MAILTO" | sed 's/^/to=/' | sed 's/?/\&/')
504    fi
505
506    MAILTO=$(echo "$MAILTO" | sed 's/&/\n/g')
507    TO=$(echo "$MAILTO" | grep '^to=' | sed 's/^to=//' | awk '{ printf "%s,",$0 }')
508    CC=$(echo "$MAILTO" | grep '^cc=' | sed 's/^cc=//' | awk '{ printf "%s,",$0 }')
509    BCC=$(echo "$MAILTO" | grep '^bcc=' | sed 's/^bcc=//' | awk '{ printf "%s,",$0 }')
510    SUBJECT=$(echo "$MAILTO" | grep '^subject=' | tail -n 1)
511    BODY=$(echo "$MAILTO" | grep '^body=' | tail -n 1)
512    ATTACH=$(echo "$MAILTO" | sed 's/^attach=/\n\nfile:\/\//g' | awk '/^file:/ { printf "%s,",$0 }' | sed 's/,$//')
513
514    if [ -z "$TO" ] ; then
515        NEWMAILTO=
516    else
517        NEWMAILTO="to='$TO'"
518    fi
519    if [ -n "$CC" ] ; then
520        NEWMAILTO="${NEWMAILTO},cc='$CC'"
521    fi
522    if [ -n "$BCC" ] ; then
523        NEWMAILTO="${NEWMAILTO},bcc='$BCC'"
524    fi
525    if [ -n "$SUBJECT" ] ; then
526        NEWMAILTO="${NEWMAILTO},$SUBJECT"
527    fi
528    if [ -n "$BODY" ] ; then
529        NEWMAILTO="${NEWMAILTO},$BODY"
530    fi
531
532    if [ -n "$ATTACH" ] ; then
533        NEWMAILTO="${NEWMAILTO},attachment='${ATTACH}'"
534    fi
535
536    NEWMAILTO=$(echo "$NEWMAILTO" | sed 's/^,//')
537    DEBUG 1 "Running $THUNDERBIRD -compose \"$NEWMAILTO\""
538    "$THUNDERBIRD" -compose "$NEWMAILTO"
539    if [ $? -eq 0 ]; then
540        exit_success
541    else
542        exit_failure_operation_failed
543    fi
544}
545
546open_kde()
547{
548    local client kde_email_profile_name
549    kde_email_profile_name=`kreadconfig --file emaildefaults --group Defaults --key Profile`
550    client=`kreadconfig --file emaildefaults --group PROFILE_"$kde_email_profile_name" --key EmailClient | cut -d ' ' -f 1`
551    echo $client | grep thunderbird > /dev/null 2>&1
552    if [ $? -eq 0 ] ; then
553        run_thunderbird "$client" "$1"
554    fi
555
556    if [ -f /etc/SuSE-release ] ; then
557        # Workaround for SUSE 10.0
558        [ -z "$client" ] && client="kmail"
559        if ! which "$client" > /dev/null 2> /dev/null; then
560            DEBUG 3 "KDE has $client configured as email client which isn't installed"
561            if which gnome-open > /dev/null 2> /dev/null && which evolution > /dev/null 2> /dev/null; then
562               DEBUG 3 "Try gnome-open instead"
563               open_gnome "$1"
564            fi
565        fi
566    fi
567    DEBUG 1 "Running kmailservice \"$1\""
568    if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
569        KMAILSERVICE=`kde4-config --locate kmailservice --path exe 2>/dev/null`
570        $KMAILSERVICE "$1"
571    else
572        KMAILSERVICE=`which kmailservice 2>/dev/null`
573        # KDE3 uses locale's encoding when decoding the URI, so set it to UTF-8
574        LC_ALL=C.UTF-8 $KMAILSERVICE "$1"
575    fi
576
577    if [ $? -eq 0 ]; then
578        exit_success
579    else
580        exit_failure_operation_failed
581    fi
582}
583
584open_gnome3()
585{
586    local client
587    local desktop
588    desktop=`xdg-mime query default "x-scheme-handler/mailto"`
589    client=`desktop_file_to_binary "$browser"`
590    echo $client | grep thunderbird > /dev/null 2>&1
591    if [ $? -eq 0 ] ; then
592        run_thunderbird "$client" "$1"
593    fi
594
595    if gvfs-open --help 2>/dev/null 1>&2; then
596        DEBUG 1 "Running gvfs-open \"$1\""
597        gvfs-open "$1"
598    else
599        DEBUG 1 "Running gnome-open \"$1\""
600        gnome-open "$1"
601    fi
602
603    if [ $? -eq 0 ]; then
604        exit_success
605    else
606        exit_failure_operation_failed
607    fi
608}
609
610open_gnome()
611{
612    local client
613    client=`gconftool-2 --get /desktop/gnome/url-handlers/mailto/command | cut -d ' ' -f 1` || ""
614    echo $client | grep thunderbird > /dev/null 2>&1
615    if [ $? -eq 0 ] ; then
616        run_thunderbird "$client" "$1"
617    fi
618
619    if gvfs-open --help 2>/dev/null 1>&2; then
620        DEBUG 1 "Running gvfs-open \"$1\""
621        gvfs-open "$1"
622    else
623        DEBUG 1 "Running gnome-open \"$1\""
624        gnome-open "$1"
625    fi
626
627    if [ $? -eq 0 ]; then
628        exit_success
629    else
630        exit_failure_operation_failed
631    fi
632}
633
634
635open_xfce()
636{
637    DEBUG 1 "Running exo-open \"$1\""
638    exo-open "$1"
639
640    if [ $? -eq 0 ]; then
641        exit_success
642    else
643        exit_failure_operation_failed
644    fi
645}
646
647open_generic()
648{
649    IFS=":"
650    for browser in $BROWSER; do
651        if [ x"$browser" != x"" ]; then
652
653            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
654            if [ $? -ne 0 ]; then browser_with_arg=$browser;
655            fi
656
657            if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
658            else $browser_with_arg;
659            fi
660
661            if [ $? -eq 0 ]; then exit_success;
662            fi
663        fi
664    done
665
666    exit_failure_operation_impossible "no method available for opening '$1'"
667}
668
669url_encode()
670{
671result=$(echo "$1" | $utf8 | awk '
672    BEGIN {
673        for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
674        e = ""
675        linenr = 1
676    }
677    {
678        if ( linenr++ != 1 ) {
679            e = e "%0D%0A"
680        }
681        for ( i=1; i<=length ($0); ++i ) {
682            c = substr ($0, i, 1)
683            if ( ord [c] > 127 ) {
684                e = e "%" sprintf("%02X", ord [c])
685            } else if ( c ~ /[@a-zA-Z0-9.-\\\/]/ ) {
686                e = e c
687            } else {
688                e = e "%" sprintf("%02X", ord [c])
689            }
690        }
691    }
692    END {
693        print e
694    }
695')
696}
697
698options=
699mailto=
700utf8="iconv -t utf8"
701while [ $# -gt 0 ] ; do
702    parm="$1"
703    shift
704
705    case "$parm" in
706      --utf8)
707        utf8="cat"
708        ;;
709
710      --to)
711        if [ -z "$1" ] ; then
712            exit_failure_syntax "email address argument missing for --to"
713        fi
714        url_encode "$1"
715        options="${options}to=${result}&"
716        shift
717        ;;
718
719      --cc)
720        if [ -z "$1" ] ; then
721             exit_failure_syntax "email address argument missing for --cc"
722        fi
723        url_encode "$1"
724        options="${options}cc=${result}&"
725        shift
726        ;;
727
728      --bcc)
729        if [ -z "$1" ] ; then
730            exit_failure_syntax "email address argument missing for --bcc"
731        fi
732        url_encode "$1"
733        options="${options}bcc=${result}&"
734        shift
735        ;;
736
737      --subject)
738        if [ -z "$1" ] ; then
739            exit_failure_syntax "text argument missing for --subject option"
740        fi
741        url_encode "$1"
742        options="${options}subject=${result}&"
743        shift
744        ;;
745
746      --body)
747        if [ -z "$1" ] ; then
748            exit_failure_syntax "text argument missing for --body option"
749        fi
750        url_encode "$1"
751        options="${options}body=${result}&"
752        shift
753        ;;
754
755      --attach)
756        if [ -z "$1" ] ; then
757            exit_failure_syntax "file argument missing for --attach option"
758        fi
759        check_input_file "$1"
760        file=`readlink -f "$1"` # Normalize path
761        if [ -z "$file" -o ! -f "$file" ] ; then
762            exit_failure_file_missing "file '$1' does not exist"
763        fi
764
765        url_encode "$file"
766        options="${options}attach=${result}&"
767        shift
768        ;;
769
770      -*)
771        exit_failure_syntax "unexpected option '$parm'"
772        ;;
773
774      mailto:*)
775        mailto="$parm"
776        ;;
777
778      *@*)
779        url_encode "$parm"
780        if [ -z "${mailto}" ] ; then
781            mailto="mailto:"${result}"?"
782        else
783            options="${options}to=${result}&"
784        fi
785        ;;
786
787      *)
788        exit_failure_syntax "unexpected argument '$parm'"
789        ;;
790    esac
791done
792
793if [ -z "${mailto}" ] ; then
794    # TO address is optional
795    mailto="mailto:?"
796fi
797
798case $mailto in
799      *\?)
800        mailto="${mailto}${options}"
801        ;;
802
803      *\?*)
804        mailto="${mailto}&${options}"
805        ;;
806
807      *)
808        mailto="${mailto}?${options}"
809        ;;
810esac
811
812# Strip trailing ? and &
813mailto=`echo "${mailto}"| sed 's/[?&]$//'`
814
815# Shouldn't happen
816[ x"${mailto}" != x"" ] || exit_failure_syntax
817
818if which xdg-email-hook.sh > /dev/null 2> /dev/null; then
819    xdg-email-hook.sh "${mailto}"
820    if [ $? -eq 0 ]; then
821        exit_success
822    else
823        exit_failure_operation_failed
824    fi
825fi
826
827detectDE
828
829if [ x"$DE" = x"" ]; then
830    DE=generic
831fi
832
833# if BROWSER variable is not set, check some well known browsers instead
834if [ x"$BROWSER" = x"" ]; then
835    BROWSER=links2:elinks:links:lynx:w3m
836    if [ -n "$DISPLAY" ]; then
837        BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
838    fi
839fi
840
841case "$DE" in
842    kde)
843    open_kde "${mailto}"
844    ;;
845
846    gnome)
847    open_gnome "${mailto}"
848    ;;
849
850    gnome3)
851    open_gnome3 "${mailto}"
852    ;;
853
854    xfce)
855    open_xfce "${mailto}"
856    ;;
857
858    generic|lxde)
859    open_generic "${mailto}"
860    ;;
861
862    *)
863    exit_failure_operation_impossible "no method available for opening '${mailto}'"
864    ;;
865esac
866