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