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