69b786fee8256a58937316ff68419eef3e4be252
[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 if [ -f /usr/share/uzbl/examples/configs/cookies ]
9 then
10         file=/usr/share/uzbl/examples/configs/cookies
11 else
12         file=./examples/configs/cookies #useful when developing
13 fi
14
15 if [ -d $XDG_DATA_HOME/uzbl/cookies ]
16 then
17         cookie_dir=$XDG_DATA_HOME/uzbl/cookies
18 else
19         cookie_dir=./examples/data
20 fi
21
22 which zenity &>/dev/null || exit 2
23
24 uri=$6
25 action=$8 # GET/PUT
26 host=${uri/\/*/}
27
28
29
30 # $1 = section (TRUSTED or DENY)
31 # $2 =url
32 function match () {
33         sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host"
34 }
35
36 function readcookie () {
37         cookie=
38         while read
39         do
40                 cookie="$REPLY
41 "
42         done
43 }
44
45 function fetch_cookie () {
46         cookie=`cat $cookie_dir/$host.cookie`
47 }
48
49 function store_cookie () {
50         echo $cookie > $cookie_dir/$host.cookie
51 }
52
53 if match TRUSTED $host
54 then
55         [ $action == PUT ] && readcookie && store_cookie $host
56         [ $action == GET ] && fetch_cookie && echo "$cookie"
57 elif ! match DENY $host
58 then
59         [ $action == PUT ] && readcookie && zenity --question --title 'Uzbl Cookie handler' --text "Accept cookie from $host ? Contents:\n$cookie" && store_cookie $host
60         [ $action == GET ] && fetch_cookie && zenity --question --title 'Uzbl Cookie handler' --text "Submit cookie to $host ? Contents:\n$cookie" && echo $cookie
61 fi
62 exit 0