cookie doc
[uzbl-mobile] / examples / scripts / cookies.sh
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)
4
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)
7
8 # TODO: correct implementation.
9 # see http://curl.haxx.se/rfc/cookie_spec.html
10 # http://en.wikipedia.org/wiki/HTTP_cookie
11
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.
16 # support blocking or not for 3rd parties
17 # http://kb.mozillazine.org/Cookies.txt
18
19 if [ -f /usr/share/uzbl/examples/configs/cookies ]
20 then
21         file=/usr/share/uzbl/examples/configs/cookies
22 else
23         file=./examples/configs/cookies #useful when developing
24 fi
25
26 if [ -d $XDG_DATA_HOME/uzbl/cookies ]
27 then
28         cookie_dir=$XDG_DATA_HOME/uzbl/cookies
29 else
30         cookie_dir=./examples/data
31 fi
32
33 which zenity &>/dev/null || exit 2
34
35 uri=$6
36 uri=${uri/http:\/\/} # strip 'http://' part
37 action=$8 # GET/PUT
38 cookie=$9
39 host=${uri/\/*/}
40
41
42
43 # $1 = section (TRUSTED or DENY)
44 # $2 =url
45 function match () {
46         sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host"
47 }
48
49 function fetch_cookie () {
50         cookie=`cat $cookie_dir/$host.cookie`
51 }
52
53 function store_cookie () {
54         echo $cookie > $cookie_dir/$host.cookie
55 }
56
57 if match TRUSTED $host
58 then
59         [ $action == PUT ] && store_cookie $host
60         [ $action == GET ] && fetch_cookie && echo "$cookie"
61 elif ! match DENY $host
62 then
63         [ $action == PUT ] &&                 cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Accept this cookie from $host ?" --entry-text="$cookie"` && store_cookie $host
64         [ $action == GET ] && fetch_cookie && cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Submit this cookie to $host ?"   --entry-text="$cookie"` && echo $cookie
65 fi
66 exit 0