~saiarcot895/ubuntu/wily/xdg-utils/fix-multiple-execs

« back to all changes in this revision

Viewing changes to scripts/xdg-email.in

  • Committer: Package Import Robot
  • Author(s): Per Olofsson
  • Date: 2011-12-12 12:04:25 UTC
  • mfrom: (1.2.8)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: package-import@ubuntu.com-20111212120425-0rdnrl8tn45vz171
Tags: 1.1.0~rc1+git20111210-1
* New upstream git snapshot b961235b197647d6649ef3d48d7cc2cecafe3d47.
* Drop patches applied upstream:
  - xdg-mime-follow-symlinks.diff
  - x-www-browser.diff
  - xdg-mime-generic-use-mimetype.diff
  - bashisms.diff
  - xdg-screensaver-check-gnome.diff
* Modify patches to make changes to scripts/xdg-*.in, instead of the
  generated scripts.
* Also check for ~/.mutt/muttrc when deciding whether to use mutt as
  MUA or not. Thanks to martin f krafft. Closes: #648733.
* Build scripts/xdg-* from *.in files.
* Build-depend on xmlto and awk, as they are needed when building the
  scripts.
* Also build the manual pages.
* Bump Standards-Version to 3.9.2. No changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#---------------------------------------------
 
18
 
 
19
manualpage()
 
20
{
 
21
cat << _MANUALPAGE
 
22
_MANUALPAGE
 
23
}
 
24
 
 
25
usage()
 
26
{
 
27
cat << _USAGE
 
28
_USAGE
 
29
}
 
30
 
 
31
#@xdg-utils-common@
 
32
 
 
33
run_thunderbird()
 
34
{
 
35
    local THUNDERBIRD MAILTO NEWMAILTO TO CC BCC SUBJECT BODY ATTACH
 
36
    THUNDERBIRD="$1"
 
37
    MAILTO=$(echo "$2" | sed 's/^mailto://')
 
38
    echo "$MAILTO" | grep -qs "^?"
 
39
    if [ "$?" = "0" ] ; then
 
40
        MAILTO=$(echo "$MAILTO" | sed 's/^?//')
 
41
    else
 
42
        MAILTO=$(echo "$MAILTO" | sed 's/^/to=/' | sed 's/?/\&/')
 
43
    fi
 
44
 
 
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/,$//'))
 
52
 
 
53
    if [ -z "$TO" ] ; then
 
54
        NEWMAILTO=
 
55
    else
 
56
        NEWMAILTO="to='$TO'"
 
57
    fi
 
58
    if [ -n "$CC" ] ; then
 
59
        NEWMAILTO="${NEWMAILTO},cc='$CC'"
 
60
    fi
 
61
    if [ -n "$BCC" ] ; then
 
62
        NEWMAILTO="${NEWMAILTO},bcc='$BCC'"
 
63
    fi
 
64
    if [ -n "$SUBJECT" ] ; then
 
65
        NEWMAILTO="${NEWMAILTO},$SUBJECT"
 
66
    fi
 
67
    if [ -n "$BODY" ] ; then
 
68
        NEWMAILTO="${NEWMAILTO},$BODY"
 
69
    fi
 
70
 
 
71
    if [ -n "$ATTACH" ] ; then
 
72
        NEWMAILTO="${NEWMAILTO},attachment='${ATTACH}'"
 
73
    fi
 
74
 
 
75
    NEWMAILTO=$(echo "$NEWMAILTO" | sed 's/^,//')
 
76
    DEBUG 1 "Running $THUNDERBIRD -compose \"$NEWMAILTO\""
 
77
    "$THUNDERBIRD" -compose "$NEWMAILTO"
 
78
    if [ $? -eq 0 ]; then
 
79
        exit_success
 
80
    else
 
81
        exit_failure_operation_failed
 
82
    fi
 
83
}
 
84
 
 
85
open_kde()
 
86
{
 
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"
 
93
    fi
 
94
 
 
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"
 
102
               open_gnome "$1"
 
103
            fi
 
104
        fi
 
105
    fi
 
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`
 
109
        $KMAILSERVICE "$1"
 
110
    else
 
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"
 
114
    fi
 
115
 
 
116
    if [ $? -eq 0 ]; then
 
117
        exit_success
 
118
    else
 
119
        exit_failure_operation_failed
 
120
    fi
 
121
}
 
122
 
 
123
open_gnome3()
 
124
{
 
125
    local client
 
126
    local desktop
 
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"
 
132
    fi
 
133
 
 
134
    if gvfs-open --help 2>/dev/null 1>&2; then
 
135
        DEBUG 1 "Running gvfs-open \"$1\""
 
136
        gvfs-open "$1"
 
137
    else
 
138
        DEBUG 1 "Running gnome-open \"$1\""
 
139
        gnome-open "$1"
 
140
    fi
 
141
 
 
142
    if [ $? -eq 0 ]; then
 
143
        exit_success
 
144
    else
 
145
        exit_failure_operation_failed
 
146
    fi
 
147
}
 
148
 
 
149
open_gnome()
 
150
{
 
151
    local client
 
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"
 
156
    fi
 
157
 
 
158
    if gvfs-open --help 2>/dev/null 1>&2; then
 
159
        DEBUG 1 "Running gvfs-open \"$1\""
 
160
        gvfs-open "$1"
 
161
    else
 
162
        DEBUG 1 "Running gnome-open \"$1\""
 
163
        gnome-open "$1"
 
164
    fi
 
165
 
 
166
    if [ $? -eq 0 ]; then
 
167
        exit_success
 
168
    else
 
169
        exit_failure_operation_failed
 
170
    fi
 
171
}
 
172
 
 
173
 
 
174
open_xfce()
 
175
{
 
176
    DEBUG 1 "Running exo-open \"$1\""
 
177
    exo-open "$1"
 
178
 
 
179
    if [ $? -eq 0 ]; then
 
180
        exit_success
 
181
    else
 
182
        exit_failure_operation_failed
 
183
    fi
 
184
}
 
185
 
 
186
open_envvar()
 
187
{
 
188
    local OLDIFS="$IFS"
 
189
    IFS=:
 
190
    for i in $MAILER; do
 
191
        IFS="$OLDIFS"
 
192
 
 
193
        eval "$i" '"$1"'
 
194
 
 
195
        if [ $? -eq 0 ]; then
 
196
            exit_success
 
197
        fi
 
198
    done
 
199
 
 
200
    exit_failure_operation_failed
 
201
}
 
202
 
 
203
open_generic()
 
204
{
 
205
    IFS=":"
 
206
    for browser in $BROWSER; do
 
207
        if [ x"$browser" != x"" ]; then
 
208
 
 
209
            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
 
210
            if [ $? -ne 0 ]; then browser_with_arg=$browser;
 
211
            fi
 
212
 
 
213
            if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
 
214
            else $browser_with_arg;
 
215
            fi
 
216
 
 
217
            if [ $? -eq 0 ]; then exit_success;
 
218
            fi
 
219
        fi
 
220
    done
 
221
 
 
222
    exit_failure_operation_impossible "no method available for opening '$1'"
 
223
}
 
224
 
 
225
url_encode()
 
226
{
 
227
str=$(echo "$1" | $utf8)
 
228
local ORIG_LANG="$LANG"
 
229
local ORIG_LC_ALL="$LC_ALL"
 
230
LANG=C
 
231
LC_ALL=C
 
232
result=$(echo "$str" | awk '
 
233
    BEGIN {
 
234
        for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
 
235
        e = ""
 
236
        linenr = 1
 
237
    }
 
238
    {
 
239
        if ( linenr++ != 1 ) {
 
240
            e = e "%0D%0A"
 
241
        }
 
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.-\\\/]/ ) {
 
247
                e = e c
 
248
            } else {
 
249
                e = e "%" sprintf("%02X", ord [c])
 
250
            }
 
251
        }
 
252
    }
 
253
    END {
 
254
        print e
 
255
    }
 
256
')
 
257
LANG="$ORIG_LANG"
 
258
LC_ALL="$ORIG_LC_ALL"
 
259
}
 
260
 
 
261
options=
 
262
mailto=
 
263
utf8="iconv -t utf8"
 
264
while [ $# -gt 0 ] ; do
 
265
    parm="$1"
 
266
    shift
 
267
 
 
268
    case "$parm" in
 
269
      --utf8)
 
270
        utf8="cat"
 
271
        ;;
 
272
 
 
273
      --to)
 
274
        if [ -z "$1" ] ; then
 
275
            exit_failure_syntax "email address argument missing for --to"
 
276
        fi
 
277
        url_encode "$1"
 
278
        options="${options}to=${result}&"
 
279
        shift
 
280
        ;;
 
281
 
 
282
      --cc)
 
283
        if [ -z "$1" ] ; then
 
284
             exit_failure_syntax "email address argument missing for --cc"
 
285
        fi
 
286
        url_encode "$1"
 
287
        options="${options}cc=${result}&"
 
288
        shift
 
289
        ;;
 
290
 
 
291
      --bcc)
 
292
        if [ -z "$1" ] ; then
 
293
            exit_failure_syntax "email address argument missing for --bcc"
 
294
        fi
 
295
        url_encode "$1"
 
296
        options="${options}bcc=${result}&"
 
297
        shift
 
298
        ;;
 
299
 
 
300
      --subject)
 
301
        if [ -z "$1" ] ; then
 
302
            exit_failure_syntax "text argument missing for --subject option"
 
303
        fi
 
304
        url_encode "$1"
 
305
        options="${options}subject=${result}&"
 
306
        shift
 
307
        ;;
 
308
 
 
309
      --body)
 
310
        if [ -z "$1" ] ; then
 
311
            exit_failure_syntax "text argument missing for --body option"
 
312
        fi
 
313
        url_encode "$1"
 
314
        options="${options}body=${result}&"
 
315
        shift
 
316
        ;;
 
317
 
 
318
      --attach)
 
319
        if [ -z "$1" ] ; then
 
320
            exit_failure_syntax "file argument missing for --attach option"
 
321
        fi
 
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"
 
326
        fi
 
327
 
 
328
        url_encode "$file"
 
329
        options="${options}attach=${result}&"
 
330
        shift
 
331
        ;;
 
332
 
 
333
      -*)
 
334
        exit_failure_syntax "unexpected option '$parm'"
 
335
        ;;
 
336
 
 
337
      mailto:*)
 
338
        mailto="$parm"
 
339
        ;;
 
340
 
 
341
      *@*)
 
342
        url_encode "$parm"
 
343
        if [ -z "${mailto}" ] ; then
 
344
            mailto="mailto:"${result}"?"
 
345
        else
 
346
            options="${options}to=${result}&"
 
347
        fi
 
348
        ;;
 
349
 
 
350
      *)
 
351
        exit_failure_syntax "unexpected argument '$parm'"
 
352
        ;;
 
353
    esac
 
354
done
 
355
 
 
356
if [ -z "${mailto}" ] ; then
 
357
    # TO address is optional
 
358
    mailto="mailto:?"
 
359
fi
 
360
 
 
361
case $mailto in
 
362
      *\?)
 
363
        mailto="${mailto}${options}"
 
364
        ;;
 
365
 
 
366
      *\?*)
 
367
        mailto="${mailto}&${options}"
 
368
        ;;
 
369
 
 
370
      *)
 
371
        mailto="${mailto}?${options}"
 
372
        ;;
 
373
esac
 
374
 
 
375
# Strip trailing ? and &
 
376
mailto=`echo "${mailto}"| sed 's/[?&]$//'`
 
377
 
 
378
# Shouldn't happen
 
379
[ x"${mailto}" != x"" ] || exit_failure_syntax
 
380
 
 
381
if which @NAME@-hook.sh > /dev/null 2> /dev/null; then
 
382
    @NAME@-hook.sh "${mailto}"
 
383
    if [ $? -eq 0 ]; then
 
384
        exit_success
 
385
    else
 
386
        exit_failure_operation_failed
 
387
    fi
 
388
fi
 
389
 
 
390
detectDE
 
391
 
 
392
if [ x"$DE" = x"" ]; then
 
393
    DE=generic
 
394
fi
 
395
 
 
396
if [ -r ~/.muttrc ] || [ -r ~/.mutt/muttrc ] && \
 
397
    which mutt >/dev/null 2>&1; then
 
398
    MAILER="x-terminal-emulator -e mutt"
 
399
fi
 
400
 
 
401
if [ x"$MAILER" != x"" ]; then
 
402
    DE=envvar
 
403
fi
 
404
 
 
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
 
410
    fi
 
411
fi
 
412
 
 
413
case "$DE" in
 
414
    envvar)
 
415
    open_envvar "${mailto}"
 
416
    ;;
 
417
 
 
418
    kde)
 
419
    open_kde "${mailto}"
 
420
    ;;
 
421
 
 
422
    gnome)
 
423
    open_gnome "${mailto}"
 
424
    ;;
 
425
 
 
426
    gnome3)
 
427
    open_gnome3 "${mailto}"
 
428
    ;;
 
429
 
 
430
    xfce)
 
431
    open_xfce "${mailto}"
 
432
    ;;
 
433
 
 
434
    generic|lxde)
 
435
    open_generic "${mailto}"
 
436
    ;;
 
437
 
 
438
    *)
 
439
    exit_failure_operation_impossible "no method available for opening '${mailto}'"
 
440
    ;;
 
441
esac