changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > werc / changeset: Various small cleanups: remove unused code, clarity and reliability fixes here and here, move code to more appropriate locations, improve some comments.

changeset 316: a8c8620f7ff7
parent 315: a302f39ededd
child 317: c062cb8f9c32
author: uriel@engel.se.cat-v.org
date: Mon, 12 Jan 2009 02:07:31 +0100
files: apps/blagh/app.rc bin/cgilib.rc bin/werc.rc lib/_apps/brag/post_form.tpl
description: Various small cleanups: remove unused code, clarity and reliability fixes here and here, move code to more appropriate locations, improve some comments.
     1.1--- a/apps/blagh/app.rc	Sun Jan 11 19:52:48 2009 +0100
     1.2+++ b/apps/blagh/app.rc	Mon Jan 12 02:07:31 2009 +0100
     1.3@@ -42,3 +42,26 @@
     1.4     # NOTE: this breaks if any path element in blogDirs contain '/./' or '|'
     1.5     ls -F $*^/./[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/ >[2]/dev/null | sed -n 's,/\./,/|/,; /\/$/p' | sort -r '-t|' +1 | sed 's,/+\|/+,/,'
     1.6 }
     1.7+
     1.8+
     1.9+fn make_blog_post {
    1.10+    if(! ~ 0 $#1 $#2 $#3) {
    1.11+        bdir=$1
    1.12+        btitle=$2
    1.13+        btext=$3
    1.14+        date=`{/bin/date +%F}
    1.15+
    1.16+        n=1
    1.17+        for(f in $bdir^$date^'-'*) {
    1.18+            i=`{echo -n $f | sed -n 's,^.*/'$date'-([0-9]+)_.*,\1,p'|tr -d $NEW_LINE}
    1.19+            if(! ~ $#i 0 && test $i -ge $n)
    1.20+                n=`{hoc -e $i'+1'}
    1.21+        }
    1.22+        btitle=`{echo -n $"btitle | sed 's/[ 	]+/_/g; 1q'}
    1.23+
    1.24+        echo $btext > $bdir^'/'^$"date^'-'^$"n^_$"btitle.md 
    1.25+    }
    1.26+    if not
    1.27+        status=Missing blog post arguments $"1 $"2 $"3 
    1.28+}
    1.29+
     2.1--- a/bin/cgilib.rc	Sun Jan 11 19:52:48 2009 +0100
     2.2+++ b/bin/cgilib.rc	Mon Jan 12 02:07:31 2009 +0100
     2.3@@ -110,13 +110,11 @@
     2.4 }
     2.5 
     2.6 fn crop_text {
     2.7-    max_chars=$1
     2.8-
     2.9     ellipsis='...'
    2.10     if(~ $#* 2)
    2.11         ellipsis=$2
    2.12 
    2.13-    awk -v max'='^$"max_chars^' ' -v 'ellipsis='$ellipsis '
    2.14+    awk -v max'='^$"1^' ' -v 'ellipsis='$ellipsis '
    2.15     {
    2.16         nc += 1 + length;
    2.17         if(nc > max) {
    2.18@@ -174,47 +172,36 @@
    2.19 
    2.20 
    2.21 ##############################################
    2.22-# More werc-specific functions
    2.23+# Werc-specific functions
    2.24 
    2.25 fn template { awk -f bin/template.awk $* | rc $rcargs }
    2.26 
    2.27-# .rec parsing
    2.28-fn parse_rec {
    2.29-    ifs='
    2.30-' for(i in `{sed 's/% *//g; /^$/q' < $1}) {
    2.31-        v=`{echo -n $i | sed 's/^/rec_/; s/=.*//;'} 
    2.32-        $v=`{echo -n $i | sed 's/^[^=]*=//'}
    2.33-    }
    2.34-    ifs=() { rec_data=`{sed -n '/^[^%]./,$p' < $1} }
    2.35-}
    2.36-
    2.37-
    2.38 # Auth code
    2.39 
    2.40 # Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password)
    2.41 # login_user can't be used from a template because it sets a cookie 
    2.42 fn login_user {
    2.43-    # Note: get_user can use an existing cookie, so we might end up setting an existing cookie
    2.44+    # Note: we set the cookie even if it is already there.
    2.45     if(get_user $*)
    2.46         set_cookie werc_user $"logged_user^':0:'^$"logged_password
    2.47 }
    2.48 
    2.49-# Checks if we are logged in, if called with an argument, we check group membership too
    2.50+# Check loggin status, if called with group arg we check membership too
    2.51 fn check_user {
    2.52     if(! get_user)
    2.53         status='Not logged in'
    2.54-    if not if(! ~ $#1 0 && ! grep -s '^'^$logged_user^'$' etc/groups/$1)
    2.55+    if not if(~ $#1 1 && ! grep -s '^'^$logged_user^'$' etc/groups/$1)
    2.56         status=User $logged_user not in group $1
    2.57     if not
    2.58-        true
    2.59+        status=()
    2.60 }
    2.61 
    2.62-# If not logged in, try to get user login info from POST info or from cookie
    2.63+# If not logged in, try to get user login info from POST or from cookie
    2.64 fn get_user {
    2.65     if(~ $#logged_user 0) {
    2.66         if(~ $#* 2) {
    2.67             user_name=$1 
    2.68-            user_password $2
    2.69+            user_password=$2
    2.70         }
    2.71         if not if(~ $REQUEST_METHOD POST)
    2.72             get_post_args user_name user_password
    2.73@@ -229,7 +216,7 @@
    2.74         auth_user $user_name $user_password
    2.75     }
    2.76     if not
    2.77-        true
    2.78+        status=()
    2.79 }
    2.80 
    2.81 # Check if user_name and user_password represent a valid user account
    2.82@@ -249,46 +236,11 @@
    2.83         logged_user=$user_name
    2.84         logged_password=$user_password
    2.85         dprint Auth: success
    2.86+        status=()
    2.87     }
    2.88 }
    2.89 
    2.90-
    2.91-# Blog stuff
    2.92-fn make_blog_post {
    2.93-    bdir=$1
    2.94-    btitle=$2
    2.95-    btext=$3
    2.96-    if(! ~ 0 $#1 $#2 $#3) {
    2.97-        date=`{/bin/date +%F}
    2.98-
    2.99-        n=1
   2.100-        for(f in $bdir^$date^'-'*) {
   2.101-            i=`{echo -n $f | sed -n 's,^.*/'$date'-([0-9]+)_.*,\1,p'|tr -d $NEW_LINE}
   2.102-            if(! ~ $#i 0 && test $i -ge $n)
   2.103-                n=`{hoc -e $i'+1'}
   2.104-        }
   2.105-        btitle=`{echo -n $"btitle | sed 's/[ 	]+/_/g; 1q'}
   2.106-
   2.107-        echo $btext > $bdir^'/'^$"date^'-'^$"n^_$"btitle.md 
   2.108-    }
   2.109-    if not
   2.110-        status=Missing blog post arguments $1 $2 $3 
   2.111-}
   2.112-
   2.113-###################################
   2.114-# App framework
   2.115-
   2.116-fn init_apps {
   2.117-    for(a in $enabled_apps) {
   2.118-        . ./apps/$a/app.rc
   2.119-        $a^'_init'
   2.120-    }
   2.121-}
   2.122-
   2.123-
   2.124-##################################
   2.125-# Meta-data extract
   2.126-
   2.127+# .md '(meta-)data' extract
   2.128 fn get_md_file_attr {
   2.129     sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1
   2.130 }
     3.1--- a/bin/werc.rc	Sun Jan 11 19:52:48 2009 +0100
     3.2+++ b/bin/werc.rc	Mon Jan 12 02:07:31 2009 +0100
     3.3@@ -99,15 +99,19 @@
     3.4         handler_body_main=(txt_handler $local_path.txt)
     3.5 
     3.6     # Apps
     3.7-    if(! ~ $#enabled_apps 0)
     3.8-        init_apps
     3.9+    if(! ~ $#enabled_apps 0) {
    3.10+        for(a in $enabled_apps) {
    3.11+            . ./apps/$a/app.rc
    3.12+            $a^'_init'
    3.13+        }
    3.14+    }
    3.15 
    3.16     if(! ~ $#handler_body_main 0)
    3.17-        { }
    3.18+        { } # We are done
    3.19     # Dir listing
    3.20     if not if(~ $local_path */index)
    3.21         handler_body_main=(dir_listing_handler $local_path)
    3.22-    # Explicit .html urls, the web server might handle this first!
    3.23+    # Canonize explicit .html urls, the web server might handle this first!
    3.24     if not if(~ $local_path *.html && test -f $local_path)
    3.25         perm_redirect `{ echo $req_path|sed 's/.html$//' }
    3.26     # Fallback static file handler
    3.27@@ -116,18 +120,13 @@
    3.28     # File not found
    3.29     if not {
    3.30         handler_body_main=(tpl_handler `{get_lib_file 404.tpl})
    3.31+        echo 'Status: 404 Not Found'
    3.32         dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
    3.33-        echo 'Status: 404 Not Found'
    3.34     }
    3.35 }
    3.36 
    3.37-fn run_handlers {
    3.38-    for(h in $*)
    3.39-        run_handler $$h
    3.40-}
    3.41-fn run_handler {
    3.42-    $*(1) $*(2-)
    3.43-}
    3.44+fn run_handlers { for(h in $*) run_handler $$h }
    3.45+fn run_handler { $*(1) $*(2-) }
    3.46 
    3.47 # Careful, the proper p9p path might not be set until initrc.local is sourced
    3.48 path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin) 
     4.1--- a/lib/_apps/brag/post_form.tpl	Sun Jan 11 19:52:48 2009 +0100
     4.2+++ b/lib/_apps/brag/post_form.tpl	Mon Jan 12 02:07:31 2009 +0100
     4.3@@ -9,7 +9,7 @@
     4.4 
     4.5     if(~ $#blog_editors_group 0)
     4.6         echo Cant post to $target_blog_dir, no editors group set.
     4.7-    if not if (! check_user $blog_editors_group)
     4.8+    if not if(! check_user $blog_editors_group)
     4.9         echo Post to $target_blog_dir denied, user $logged_user not in group $blog_editors_group
    4.10     if not if(! make_blog_post $target_blog_dir $post_title $post_body)
    4.11         echo Posting to $target_blog_dir failed: $status