storing cookies now works with handler
[uzbl-00z.git] / examples / scripts / cookies.sh
blob55498a14850324d5e3280a57a9913d8354dce7f1
1 #!/bin/bash
2 # this is an example script of how you could manage your cookies..
3 # you probably want your cookies config file in your $XDG_CONFIG_HOME ( eg $HOME/.config/uzbl/cookies)
5 # MAYBE TODO: allow user to edit cookie before saving. this cannot be done with zenity :(
6 # TODO: different cookie paths per config (eg per group of uzbl instances)
8 # TODO: correct implementation.
9 # see http://curl.haxx.se/rfc/cookie_spec.html
10 # http://en.wikipedia.org/wiki/HTTP_cookie
12 # TODO : check expires= before sending.
13 # write sample script that cleans up cookies dir based on expires attribute.
14 # TODO: check uri against domain attribute. and path also.
15 # implement secure attribute.
18 if [ -f /usr/share/uzbl/examples/configs/cookies ]
19 then
20 file=/usr/share/uzbl/examples/configs/cookies
21 else
22 file=./examples/configs/cookies #useful when developing
25 if [ -d $XDG_DATA_HOME/uzbl/cookies ]
26 then
27 cookie_dir=$XDG_DATA_HOME/uzbl/cookies
28 else
29 cookie_dir=./examples/data
32 which zenity &>/dev/null || exit 2
34 uri=$6
35 uri=${uri/http:\/\/} # strip 'http://' part
36 action=$8 # GET/PUT
37 cookie=$9
38 host=${uri/\/*/}
42 # $1 = section (TRUSTED or DENY)
43 # $2 =url
44 function match () {
45 sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host"
48 function fetch_cookie () {
49 cookie=`cat $cookie_dir/$host.cookie`
52 function store_cookie () {
53 echo $cookie > $cookie_dir/$host.cookie
56 if match TRUSTED $host
57 then
58 [ $action == PUT ] && store_cookie $host
59 [ $action == GET ] && fetch_cookie && echo "$cookie"
60 elif ! match DENY $host
61 then
62 [ $action == PUT ] && cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Accept this cookie from $host ?" --entry-text="$cookie"` && store_cookie $host
63 [ $action == GET ] && fetch_cookie && cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Submit this cookie to $host ?" --entry-text="$cookie"` && echo $cookie
65 exit 0