make cookies.py default sample cookie script
[uzbl-mobile] / examples / scripts / cookies.sh
index ecb6d60..a875482 100755 (executable)
@@ -1,5 +1,8 @@
 #!/bin/bash
-# this is an example script of how you could manage your cookies..
+
+# THIS IS EXPERIMENTAL AND COULD BE INSECURE !!!!!!
+
+# 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
 # we use the cookies.txt format (See http://kb.mozillazine.org/Cookies.txt)
 # This is one textfile with entries like this:
 # kb.mozillazine.org   FALSE   /       FALSE   1146030396      wikiUserID      16993
 # http://kb.mozillazine.org/Cookies.txt
 # don't always append cookies, sometimes we need to overwrite
 
-[ -f /usr/share/uzbl/examples/configs/cookies ] && file=/usr/share/uzbl/examples/configs/cookies
-[ -f $XDG_CONFIG_HOME/uzbl/cookies            ] && file=$XDG_CONFIG_HOME/uzbl/cookies
-[ -f ./examples/configs/cookies               ] && file=./examples/configs/cookies #useful when developing
-[ -z "$file" ] && exit 1
+[ -f /usr/share/uzbl/examples/configs/cookies ] && cookie_config=/usr/share/uzbl/examples/configs/cookies
+[ -f $XDG_CONFIG_HOME/uzbl/cookies            ] && cookie_config=$XDG_CONFIG_HOME/uzbl/cookies
+[ -f ./examples/configs/cookies               ] && cookie_config=./examples/configs/cookies #useful when developing
+[ -z "$cookie_config" ] && exit 1
+
+[ -d /usr/share/uzbl/examples/data ] && cookie_data=/usr/share/uzbl/examples/data/cookies.txt
+[ -d $XDG_DATA_HOME/uzbl/          ] && cookie_data=$XDG_DATA_HOME/uzbl/cookies.txt
+[ -d ./examples/data/              ] && cookie_data=./examples/data/cookies.txt #useful when developing
+[ -z "$cookie_data" ] && exit 1
 
-[ -d /usr/share/uzbl/examples/data ] && cookie_file=/usr/share/uzbl/examples/data/cookies.txt
-[ -d $XDG_DATA_HOME/uzbl/          ] && cookie_file=$XDG_DATA_HOME/uzbl/cookies.txt
-[ -d ./examples/data/              ] && cookie_file=./examples/data/cookies.txt #useful when developing
-[ -z "$cookie_file" ] && exit 1
 
+notifier=
+#notifier=notify-send
+#notify_wrapper () {
+#      echo "$@" >> $HOME/cookielog
+#}
+#notifier=notifier_wrapper
 
+# if this variable is set, we will use it to inform you when and which cookies we store, and when/which we send.
+# it's primarily used for debugging
+notifier=
 which zenity &>/dev/null || exit 2
 
 # Example cookie:
@@ -53,6 +66,10 @@ field_name=
 field_value=
 field_exp='end_session'
 
+function notify () {
+       [ -n "$notifier" ] && $notifier "$@"
+}
+
 
 # FOR NOW LETS KEEP IT SIMPLE AND JUST ALWAYS PUT AND ALWAYS GET
 function parse_cookie () {
@@ -77,22 +94,35 @@ function parse_cookie () {
        unset IFS
 }
 
-# match cookies in cookies.txt againsh hostname and path
+# match cookies in cookies.txt against hostname and path
 function get_cookie () {
        path_esc=${path//\//\\/}
-       cookie=`awk "/^[^\t]*$host\t[^\t]*\t$path_esc/" $cookie_file 2>/dev/null | tail -n 1`
+       search="^[^\t]*$host\t[^\t]*\t$path_esc"
+       cookie=`awk "/$search/" $cookie_data 2>/dev/null | tail -n 1`
        if [ -z "$cookie" ]
        then
+               notify "Get_cookie: search: $search in $cookie_data -> no result"
                false
        else
+               notify "Get_cookie: search: $search in $cookie_data -> result: $cookie"
                read domain alow_read_other_subdomains path http_required expiration name value <<< "$cookie"
                cookie="$name=$value" 
-               #echo "COOKIE $cookie" >> $HOME/cookielog
                true
        fi
 }
 
-[ $action == PUT ] && parse_cookie && echo -e "$field_domain\tFALSE\t$field_path\tFALSE\t$field_exp\t$field_name\t$field_value" >> $cookie_file
+function save_cookie () {
+       if parse_cookie
+       then
+               data="$field_domain\tFALSE\t$field_path\tFALSE\t$field_exp\t$field_name\t$field_value"
+               notify "save_cookie: adding $data to $cookie_data"
+               echo -e "$data" >> $cookie_data
+       else
+               notify "not saving a cookie. since we don't have policies yet, parse_cookie must have returned false. this is a bug"
+       fi
+}
+
+[ $action == PUT ] && save_cookie
 [ $action == GET ] && get_cookie && echo "$cookie"
 
 exit
@@ -102,15 +132,15 @@ exit
 # $1 = section (TRUSTED or DENY)
 # $2 =url
 function match () {
-       sed -n "/$1/,/^\$/p" $file 2>/dev/null | grep -q "^$host"
+       sed -n "/$1/,/^\$/p" $cookie_config 2>/dev/null | grep -q "^$host"
 }
 
 function fetch_cookie () {
-       cookie=`cat $cookie_file/$host.cookie`
+       cookie=`cat $cookie_data`
 }
 
 function store_cookie () {
-       echo $cookie > $cookie_file/$host.cookie
+       echo $cookie > $cookie_data
 }
 
 if match TRUSTED $host