78139d69dc5b666d76659b0b0acfbb47169f0aa2
[uzbl-mobile] / examples / data / uzbl / scripts / cookies.sh
1 #!/bin/bash
2
3 # THIS IS EXPERIMENTAL AND COULD BE INSECURE !!!!!!
4
5 # this is an example bash script of how you could manage your cookies. it is very raw and basic and not as good as cookies.py
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 cookie_config=$XDG_CONFIG_HOME/uzbl/cookies
28 [ -z "$cookie_config" ] && exit 1
29 [ -d "$XDG_DATA_HOME/uzbl" ] || exit 1
30 [ -d $XDG_DATA_HOME/uzbl/          ] && cookie_data=$XDG_DATA_HOME/uzbl/cookies.txt
31
32
33 notifier=
34 #notifier=notify-send
35 #notify_wrapper () {
36 #       echo "$@" >> $HOME/cookielog
37 #}
38 #notifier=notifier_wrapper
39
40 # if this variable is set, we will use it to inform you when and which cookies we store, and when/which we send.
41 # it's primarily used for debugging
42 notifier=
43 which zenity &>/dev/null || exit 2
44
45 # Example cookie:
46 # test_cookie=CheckForPermission; expires=Thu, 07-May-2009 19:17:55 GMT; path=/; domain=.doubleclick.net
47
48 # uri=$6
49 # uri=${uri/http:\/\/} # strip 'http://' part
50 # host=${uri/\/*/}
51 action=$8 # GET/PUT
52 host=$9
53 shift
54 path=$9
55 shift
56 cookie=$9
57
58 field_domain=$host
59 field_path=$path
60 field_name=
61 field_value=
62 field_exp='end_session'
63
64 function notify () {
65         [ -n "$notifier" ] && $notifier "$@"
66 }
67
68
69 # FOR NOW LETS KEEP IT SIMPLE AND JUST ALWAYS PUT AND ALWAYS GET
70 function parse_cookie () {
71         IFS=$';'
72         first_pair=1
73         for pair in $cookie
74         do
75                 if [ "$first_pair" == 1 ]
76                 then
77                         field_name=${pair%%=*}
78                         field_value=${pair#*=}
79                         first_pair=0
80                 else
81                         read -r pair <<< "$pair" #strip leading/trailing wite space
82                         key=${pair%%=*}
83                         val=${pair#*=}
84                         [ "$key" == expires ] && field_exp=`date -u -d "$val" +'%s'`
85                         # TODO: domain
86                         [ "$key" == path ] && field_path=$val
87                 fi
88         done
89         unset IFS
90 }
91
92 # match cookies in cookies.txt against hostname and path
93 function get_cookie () {
94         path_esc=${path//\//\\/}
95         search="^[^\t]*$host\t[^\t]*\t$path_esc"
96         cookie=`awk "/$search/" $cookie_data 2>/dev/null | tail -n 1`
97         if [ -z "$cookie" ]
98         then
99                 notify "Get_cookie: search: $search in $cookie_data -> no result"
100                 false
101         else
102                 notify "Get_cookie: search: $search in $cookie_data -> result: $cookie"
103                 read domain alow_read_other_subdomains path http_required expiration name value <<< "$cookie"
104                 cookie="$name=$value" 
105                 true
106         fi
107 }
108
109 function save_cookie () {
110         if parse_cookie
111         then
112                 data="$field_domain\tFALSE\t$field_path\tFALSE\t$field_exp\t$field_name\t$field_value"
113                 notify "save_cookie: adding $data to $cookie_data"
114                 echo -e "$data" >> $cookie_data
115         else
116                 notify "not saving a cookie. since we don't have policies yet, parse_cookie must have returned false. this is a bug"
117         fi
118 }
119
120 [ $action == PUT ] && save_cookie
121 [ $action == GET ] && get_cookie && echo "$cookie"
122
123 exit
124
125
126 # TODO: implement this later.
127 # $1 = section (TRUSTED or DENY)
128 # $2 =url
129 function match () {
130         sed -n "/$1/,/^\$/p" $cookie_config 2>/dev/null | grep -q "^$host"
131 }
132
133 function fetch_cookie () {
134         cookie=`cat $cookie_data`
135 }
136
137 function store_cookie () {
138         echo $cookie > $cookie_data
139 }
140
141 if match TRUSTED $host
142 then
143         [ $action == PUT ] && store_cookie $host
144         [ $action == GET ] && fetch_cookie && echo "$cookie"
145 elif ! match DENY $host
146 then
147         [ $action == PUT ] &&                 cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Accept this cookie from $host ?" --entry-text="$cookie"` && store_cookie $host
148         [ $action == GET ] && fetch_cookie && cookie=`zenity --entry --title 'Uzbl Cookie handler' --text "Submit this cookie to $host ?"   --entry-text="$cookie"` && echo $cookie
149 fi
150 exit 0