oops - removed unnecessary float cast
[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 if [ -f /usr/share/uzbl/examples/configs/cookies ]
25 then
26         file=/usr/share/uzbl/examples/configs/cookies
27 else
28         file=./examples/configs/cookies #useful when developing
29 fi
30
31 #cookie_file=$XDG_DATA_HOME/uzbl/cookies.txt
32 cookie_file=./examples/data/cookies.txt
33
34 which zenity &>/dev/null || exit 2
35
36 # Example cookie:
37 # test_cookie=CheckForPermission; expires=Thu, 07-May-2009 19:17:55 GMT; path=/; domain=.doubleclick.net
38
39 # uri=$6
40 # uri=${uri/http:\/\/} # strip 'http://' part
41 # host=${uri/\/*/}
42 action=$8 # GET/PUT
43 host=$9
44 shift
45 path=$9
46 shift
47 cookie=$9
48
49 field_domain=$host
50 field_path=$path
51 field_name=
52 field_value=
53 field_exp='end_session'
54
55
56 # FOR NOW LETS KEEP IT SIMPLE AND JUST ALWAYS PUT AND ALWAYS GET
57 function parse_cookie () {
58         IFS=$';'
59         first_pair=1
60         for pair in $cookie
61         do
62                 if [ "$first_pair" == 1 ]
63                 then
64                         field_name=${pair%%=*}
65                         field_value=${pair#*=}
66                         first_pair=0
67                 else
68                         read -r pair <<< "$pair" #strip leading/trailing wite space
69                         key=${pair%%=*}
70                         val=${pair#*=}
71                         [ "$key" == expires ] && field_exp=`date -u -d "$val" +'%s'`
72                         # TODO: domain
73                         [ "$key" == path ] && field_path=$val
74                 fi
75         done
76         unset IFS
77 }
78
79 # match cookies in cookies.txt againsh hostname and path
80 function get_cookie () {
81         path_esc=${path//\//\\/}
82         cookie=`awk "/^[^\t]*$host\t[^\t]*\t$path_esc/" $cookie_file 2>/dev/null | tail -n 1`
83         if [ -z "$cookie" ]
84         then
85                 false
86         else
87                 read domain alow_read_other_subdomains path http_required expiration name value <<< "$cookie"
88                 cookie="$name=$value" 
89                 #echo "COOKIE $cookie" >> $HOME/cookielog
90                 true
91         fi
92 }
93
94 [ $action == PUT ] && parse_cookie && echo -e "$field_domain\tFALSE\t$field_path\tFALSE\t$field_exp\t$field_name\t$field_value" >> $cookie_file
95 [ $action == GET ] && get_cookie && echo "$cookie"
96
97 exit
98
99
100 # TODO: implement this later.
101 # $1 = section (TRUSTED or DENY)
102 # $2 =url
103 function match () {
104         sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host"
105 }
106
107 function fetch_cookie () {
108         cookie=`cat $cookie_file/$host.cookie`
109 }
110
111 function store_cookie () {
112         echo $cookie > $cookie_file/$host.cookie
113 }
114
115 if match TRUSTED $host
116 then
117         [ $action == PUT ] && store_cookie $host
118         [ $action == GET ] && fetch_cookie && echo "$cookie"
119 elif ! match DENY $host
120 then
121         [ $action == PUT ] &&                 cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Accept this cookie from $host ?" --entry-text="$cookie"` && store_cookie $host
122         [ $action == GET ] && fetch_cookie && cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Submit this cookie to $host ?"   --entry-text="$cookie"` && echo $cookie
123 fi
124 exit 0