-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix-extension
executable file
·169 lines (153 loc) · 6.21 KB
/
fix-extension
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# this needs to recognize commented-out fields!
# became a problem for betterprivacy 1.68
ansi_colors=(black red green yellow blue magenta cyan white)
c=${#ansi_colors[@]}
while let c--; do
eval echo_${ansi_colors[$c]}'()
{ echo -ne "\e[1;3'"$c"'m$@\e[m\n" >&2; }'
eval echo_${ansi_colors[$c]}'_n()
{ echo -ne "\e[1;3'"$c"'m$@\e[m" >&2; }'
done
full_path_strict() { readlink -e "$(dirname "$1")/$(basename "$1")"; }
good-xml() {
local str=$(file -bi "${1}" 2>/dev/null)
[ "${str%%;*}" = 'application/xml' ] || return 1
cat "$1" | xml val -w -q -e - || return 1
}
xmlns-args() { local xmlns prefix file IFS=$'\n'
ns="" #namespace strings (pass to xml as -N)
defns="" # default namespace prefix in xpaths
good-xml "$1" && file=$1 || return 1
# for every xmlns declaration in the file
for xmlns in $(cat "$file" | grep -ioE 'xmlns(:[a-z0-9]+)?="[^"]*"' | sort -u); do
# echo "DOING: $xmlns"
# remove double-quotes
xmlns=${xmlns//\"}
if [ ${xmlns:5:1} = = ]; then
# it starts like xmlns=http://...
# so we set a "default namespace"
defns=defns:
xmlns=defns=${xmlns:6}
else
# it starts like xmlns:foo=http://...
# so we remove 'xmlns:'
xmlns=${xmlns:6}
# if foo is rdf/RDF, make that the default prefix
prefix=${xmlns%%=*}
if [ ${prefix,,} = rdf ]; then
defns=$prefix:
fi
fi
# add to namespace string
ns=$ns" -N $xmlns"
done
}
for arg; do
if item=$(full_path_strict "$arg"); then
if [ -d "$item" ]; then
xpj="$item"
elif unzip -qqt "$item" &>/dev/null; then
xpi="$item"
else
echo_red "Not an xpi or dir: $item"
fi
else
echo_red "Unknown option: $item"
fi
done
# remove any trailing slash
xpj=${xpj%/}
# figure out the right namespace strings to pass to xmlstarlet
tmprdf=/tmp/.fix-extension.$EUID.$$.$(date +%s).$RANDOM.rdf
trap "rm -rf ${tmprdf}; exit;" EXIT HUP QUIT INT TERM ABRT TSTP CONT
unzip -qp "$xpi" install.rdf > $tmprdf
xmlns-args $tmprdf
# get the unique id string and use it to create new file (xpj)
mainr_xpath="//${defns}Description[@about='urn:mozilla:install-manifest'] | //${defns}Description[@${defns}about='urn:mozilla:install-manifest']"
idstr_xpath="//${defns}Description[@about='urn:mozilla:install-manifest']/em:id | //${defns}Description[@about='urn:mozilla:install-manifest']/@em:id | //${defns}Description[@${defns}about='urn:mozilla:install-manifest']/@em:id | //${defns}Description[@${defns}about='urn:mozilla:install-manifest']/em:id"
unpak_xpath="//${defns}Description[@about='urn:mozilla:install-manifest']/em:unpack | //${defns}Description[@${defns}about='urn:mozilla:install-manifest']/@em:unpack | //${defns}Description[@${defns}about='urn:mozilla:install-manifest']/em:unpack"
if idstr=$(unzip -qp "$xpi" install.rdf | xml sel $ns -t -v "$idstr_xpath" 2>/dev/null); then
[[ "$idstr" =~ [a-zA-Z0-9\.{}@_-]{15,60} ]] || croak "This extension has a WEIRD id string."
[ -a "$xpj/$idstr" ] || [ -a "$xpj/$idstr.xpi" ] && {
echo_red "Failing, won't overwrite: $xpj"
exit 1
}
# leave as zip or unpack?
if unpak=$(unzip -qp "$xpi" install.rdf | xml sel $ns -t -v "$unpak_xpath" 2>/dev/null) && [ "$unpak" = true ]; then
export dtype=dir
export xpj=$xpj/$idstr
mkdir -p "$xpj"
unzip -q "$xpi" -d "$xpj/"
else
export dtype=zip
export xpj=$xpj/$idstr.xpi
cp "$xpi" "$xpj"
fi
else
echo_red "Could not find a valid ID string in the manifest. Something is wrong."
exit 1
fi
# update internal path in new one, from pipe
# (whether extracted or not)
toxpj() {
case $dtype in
zip) cat <&0 | ./rezip "$xpj" "$1";;
dir) cat <&0 >| "$xpj/$1";;
esac
}
# check for seamonkey maxversion
newst=2.30
maxver_xpath="//*[@em:id='{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}']/@em:maxVersion | //*[em:id='{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}']/em:maxVersion"
if verst=$(unzip -qp "$xpi" install.rdf | xml sel $ns -t -v "$maxver_xpath" 2>/dev/null); then
# a seamonkey clause exists!
case $verst in
2.\*|2.26.1|2.26.\*|2.27a1|2.27a2|2.27|2.27.\*|2.28a1|2.28a2|2.28|2.28.\*|2.29a1|2.29a2|2.29|2.29.\*|2.30a1|2.30a2|2.30|3.0.\*|99.\*)
# echo_green "This plugin is already compatible with latest stable :)"
:;;
1.0a|1.0|1.0.\*|1.1a|1.1b|1.1|1.1.\*|1.5a|2.0a|2.0a1pre|2.0a1|2.0a2pre) :;&
2.0a2|2.0a3pre|2.0a3|2.0b1pre|2.0b1|2.0b2pre|2.0b2|2.0pre|2.0|2.0.1) :;&
2.0.\*|2.1a1pre|2.1a1|2.1a2pre|2.1a2|2.1a3pre|2.1a3|2.1b1pre|2.1b1) :;&
2.1b2pre|2.1b2|2.1b3pre|2.1b3|2.1|2.1.\*|2.2a1pre|2.2|2.2.\*|2.3a2|2.3) :;&
2.3.\*|2.4a1|2.4a2|2.4|2.4.\*|2.5a1|2.5a2|2.5|2.5.\*|2.6.\*|2.6a1|2.6a2|2.6) :;&
2.6.1|2.7a1|2.7a2|2.7|2.7.1|2.7.2|2.7.\*|2.8a1|2.8a2|2.8|2.8.\*|2.9a1|2.9a2) :;&
2.9|2.9.\*|2.10a1|2.10a2|2.10|2.10.\*|2.11a1|2.11a2|2.11|2.11.\*|2.12a1|2.12a2) :;&
2.12|2.12.\*|2.13a1|2.13a2|2.13|2.13.1|2.13.2|2.13.\*|2.14a1|2.14a2|2.14) :;&
2.14.1|2.14.\*|2.15a1|2.15a2|2.15|2.15.1|2.15.2|2.15.\*|2.16a1|2.16a2|2.16) :;&
2.16.1|2.16.2|2.16.\*|2.17a1|2.17a2|2.17|2.17.1|2.17.\*|2.18a1|2.18a2|2.18) :;&
2.18.\*|2.19a1|2.19a2|2.19|2.19.\*|2.20a1|2.20a2|2.20|2.20.\*|2.21a1|2.21a2) :;&
2.21|2.21.\*|2.22a1|2.22a2|2.22|2.22.1|2.22.\*|2.23a1|2.23a2|2.23|2.23.\*) :;&
2.24a1|2.24a2|2.24|2.24.\*|2.25a1|2.25a2|2.25|2.25.\*|2.26a1|2.26a2|2.26)
echo_green "Bumping from $verst to $newst"
unzip -qp "$xpi" install.rdf \
| xml ed -P -u "$maxver_xpath" -v $newst \
| toxpj install.rdf
;;
*) echo_red "Unrecognized version string: $verst";;
esac
else
echo_green "Adding Seamonkey to install.rdf"
u=%$EUID.$$.$(date +%s).$RANDOM%
unzip -qp "$xpi" install.rdf \
| xml ed $ns -s "$mainr_xpath" -t elem -n 'em:targetApplication' -v $u \
| sed s_${u}_'\
<!-- Seamonkey - added by script! -->\
<Description>\
<em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id>\
<em:minVersion>2.0a1pre</em:minVersion>\
<em:maxVersion>'$newst'</em:maxVersion>\
</Description>\n'_ig \
| toxpj install.rdf
echo_green "Adding navigator overlay to chrome.manifest"
unzip -qp "$xpi" chrome.manifest \
| awk '
{print}
/^overlay/ {
if ($2 == "chrome://browser/content/browser.xul") {
$2 = "chrome://navigator/content/navigator.xul"
print
}
}' \
| uniq \
| toxpj chrome.manifest
fi