about summary refs log tree commit diff
path: root/contrib/mmailto
blob: 7feabb40adb2c116c894425107de8892da4da9a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/sh
# mmailto mailto:... - mailto: handler spawning mcom in a terminal emulator

tryterm() {
	if [ -z "$TERMINAL" ] && command -v "$1" >/dev/null; then
		TERMINAL="$*"
	fi
}

tryterm x-terminal-emulator
tryterm urxvt
tryterm xterm

if [ -z "$TERMINAL" ]; then
	echo 'No terminal emulator found, set $TERMINAL.' 1>&2
        exit 1
fi

IFS='
'

exec $TERMINAL -e mcom $(
awk -v url="$1" '

function decode(s) {
  hexdigits = "0123456789abcdef"
  for (i = 1; i < length(s); i++) {
    if (substr(s, i, 3) ~ /%[0-9a-fA-F][0-9a-fA-F]/) {
      c = sprintf("%c", (index(hexdigits, tolower(substr(s, i+1, 1)))-1) * 16 + \
                         index(hexdigits, tolower(substr(s, i+2, 1)))-1)
      if (c == "\n") c = " "
      s = substr(s, 1, i-1) c substr(s, i+3)
      i += 2
    }
  }
  return s
}

BEGIN {
  url = decode(url)
  sub(/^mailto:/, "", url)
  split(url, parts, "?")
  to = parts[1]
  split(parts[2], fields, "&") 
  args[1] = to
  for (i in fields) {
    split(fields[i], kv, "=")
    if (kv[1] != "r") {
      args[length(args)+1] = "-" kv[1]
      args[length(args)+1] =  kv[2]
    }
  }
  for (i in args) {
    print decode(args[i])
  }
}
'
)