select more sense making paths for cookie data and config
[uzbl-mobile] / examples / scripts / cookies.sh
1 #!/bin/bash
2 # this is an example script of how you could manage your cookies..
3 # we use the cookies.txt format (See http://kb.mozillazine.org/Cookies.txt)
4 # This is one textfile with entries like this:
5 # kb.mozillazine.org    FALSE   /       FALSE   1146030396      wikiUserID      16993
6 # domain alow-read-other-subdomains path http-required expiration name value  
7 # you probably want your cookies config file in your $XDG_CONFIG_HOME ( eg $HOME/.config/uzbl/cookies)
8 # Note. in uzbl there is no strict definition on what a session is.  it's YOUR job to clear cookies marked as end_session if you want to keep cookies only valid during a "session"
9 # MAYBE TODO: allow user to edit cookie before saving. this cannot be done with zenity :(
10 # TODO: different cookie paths per config (eg per group of uzbl instances)
11
12 # TODO: correct implementation.
13 # see http://curl.haxx.se/rfc/cookie_spec.html
14 # http://en.wikipedia.org/wiki/HTTP_cookie
15
16 # TODO : check expires= before sending.
17 # write sample script that cleans up cookies dir based on expires attribute.
18 # TODO: check uri against domain attribute. and path also.
19 # implement secure attribute.
20 # support blocking or not for 3rd parties
21 # http://kb.mozillazine.org/Cookies.txt
22 # don't always append cookies, sometimes we need to overwrite
23
24 [ -f /usr/share/uzbl/examples/configs/cookies ] && file=/usr/share/uzbl/examples/configs/cookies
25 [ -f $XDG_CONFIG_HOME/uzbl/cookies            ] && file=$XDG_CONFIG_HOME/uzbl/cookies
26 [ -f ./examples/configs/cookies               ] && file=./examples/configs/cookies #useful when developing
27 [ -z "$file" ] && exit 1
28
29 [ -d /usr/share/uzbl/examples/data ] && cookie_file=/usr/share/uzbl/examples/data/cookies.txt
30 [ -d $XDG_DATA_HOME/uzbl/          ] && cookie_file=$XDG_DATA_HOME/uzbl/cookies.txt
31 [ -d ./examples/data/              ] && cookie_file=./examples/data/cookies.txt #useful when developing
32 [ -z "$cookie_file" ] && exit 1
33
34
35 which zenity &>/dev/null || exit 2
36
37 # Example cookie:
38 # test_cookie=CheckForPermission; expires=Thu, 07-May-2009 19:17:55 GMT; path=/; domain=.doubleclick.net
39
40 # uri=$6
41 # uri=${uri/http:\/\/} # strip 'http://' part
42 # host=${uri/\/*/}
43 action=$8 # GET/PUT
44 host=$9
45 shift
46 path=$9
47 shift
48 cookie=$9
49
50 field_domain=$host
51 field_path=$path
52 field_name=
53 field_value=
54 field_exp='end_session'
55
56
57 # FOR NOW LETS KEEP IT SIMPLE AND JUST ALWAYS PUT AND ALWAYS GET
58 function parse_cookie () {
59         IFS=$';'
60         first_pair=1
61         for pair in $cookie
62         do
63                 if [ "$first_pair" == 1 ]
64                 then
65                         field_name=${pair%%=*}
66                         field_value=${pair#*=}
67                         first_pair=0
68                 else
69                         read -r pair <<< "$pair" #strip leading/trailing wite space
70                         key=${pair%%=*}
71                         val=${pair#*=}
72                         [ "$key" == expires ] && field_exp=`date -u -d "$val" +'%s'`
73                         # TODO: domain
74                         [ "$key" == path ] && field_path=$val
75                 fi
76         done
77         unset IFS
78 }
79
80 # match cookies in cookies.txt againsh hostname and path
81 function get_cookie () {
82         path_esc=${path//\//\\/}
83         cookie=`awk "/^[^\t]*$host\t[^\t]*\t$path_esc/" $cookie_file 2>/dev/null | tail -n 1`
84         if [ -z "$cookie" ]
85         then
86                 false
87         else
88                 read domain alow_read_other_subdomains path http_required expiration name value <<< "$cookie"
89                 cookie="$name=$value" 
90                 #echo "COOKIE $cookie" >> $HOME/cookielog
91                 true
92         fi
93 }
94
95 [ $action == PUT ] && parse_cookie && echo -e "$field_domain\tFALSE\t$field_path\tFALSE\t$field_exp\t$field_name\t$field_value" >> $cookie_file
96 [ $action == GET ] && get_cookie && echo "$cookie"
97
98 exit
99
100
101 # TODO: implement this later.
102 # $1 = section (TRUSTED or DENY)
103 # $2 =url
104 function match () {
105         sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host"
106 }
107
108 function fetch_cookie () {
109         cookie=`cat $cookie_file/$host.cookie`
110 }
111
112 function store_cookie () {
113         echo $cookie > $cookie_file/$host.cookie
114 }
115
116 if match TRUSTED $host
117 then
118         [ $action == PUT ] && store_cookie $host
119         [ $action == GET ] && fetch_cookie && echo "$cookie"
120 elif ! match DENY $host
121 then
122         [ $action == PUT ] &&                 cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Accept this cookie from $host ?" --entry-text="$cookie"` && store_cookie $host
123         [ $action == GET ] && fetch_cookie && cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Submit this cookie to $host ?"   --entry-text="$cookie"` && echo $cookie
124 fi
125 exit 0