swerc

anselm's simpler werc fork
git clone git://git.suckless.org/swerc
Log | Files | Refs | README

commit 82980b8711da4e9909d1bf2a3489fb9ec793b454
parent 6ce92608a9de5dfe6742112da11a025543ef1a1f
Author: uriel@engel.se.cat-v.org <unknown>
Date:   Mon, 12 Jan 2009 02:07:31 +0100

Various small cleanups: remove unused code, clarity and reliability fixes here and here, move code to more appropriate locations, improve some comments.
Diffstat:
Mapps/blagh/app.rc | 23+++++++++++++++++++++++
Mbin/cgilib.rc | 70+++++++++++-----------------------------------------------------------
Mbin/werc.rc | 23+++++++++++------------
Mlib/_apps/brag/post_form.tpl | 2+-
4 files changed, 46 insertions(+), 72 deletions(-)

diff --git a/apps/blagh/app.rc b/apps/blagh/app.rc @@ -42,3 +42,26 @@ fn get_post_list { # NOTE: this breaks if any path element in blogDirs contain '/./' or '|' 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,/+\|/+,/,' } + + +fn make_blog_post { + if(! ~ 0 $#1 $#2 $#3) { + bdir=$1 + btitle=$2 + btext=$3 + date=`{/bin/date +%F} + + n=1 + for(f in $bdir^$date^'-'*) { + i=`{echo -n $f | sed -n 's,^.*/'$date'-([0-9]+)_.*,\1,p'|tr -d $NEW_LINE} + if(! ~ $#i 0 && test $i -ge $n) + n=`{hoc -e $i'+1'} + } + btitle=`{echo -n $"btitle | sed 's/[ ]+/_/g; 1q'} + + echo $btext > $bdir^'/'^$"date^'-'^$"n^_$"btitle.md + } + if not + status=Missing blog post arguments $"1 $"2 $"3 +} + diff --git a/bin/cgilib.rc b/bin/cgilib.rc @@ -110,13 +110,11 @@ BEGIN { } fn crop_text { - max_chars=$1 - ellipsis='...' if(~ $#* 2) ellipsis=$2 - awk -v max'='^$"max_chars^' ' -v 'ellipsis='$ellipsis ' + awk -v max'='^$"1^' ' -v 'ellipsis='$ellipsis ' { nc += 1 + length; if(nc > max) { @@ -174,47 +172,36 @@ fn ll_add { ############################################## -# More werc-specific functions +# Werc-specific functions fn template { awk -f bin/template.awk $* | rc $rcargs } -# .rec parsing -fn parse_rec { - ifs=' -' for(i in `{sed 's/% *//g; /^$/q' < $1}) { - v=`{echo -n $i | sed 's/^/rec_/; s/=.*//;'} - $v=`{echo -n $i | sed 's/^[^=]*=//'} - } - ifs=() { rec_data=`{sed -n '/^[^%]./,$p' < $1} } -} - - # Auth code # Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password) # login_user can't be used from a template because it sets a cookie fn login_user { - # Note: get_user can use an existing cookie, so we might end up setting an existing cookie + # Note: we set the cookie even if it is already there. if(get_user $*) set_cookie werc_user $"logged_user^':0:'^$"logged_password } -# Checks if we are logged in, if called with an argument, we check group membership too +# Check loggin status, if called with group arg we check membership too fn check_user { if(! get_user) status='Not logged in' - if not if(! ~ $#1 0 && ! grep -s '^'^$logged_user^'$' etc/groups/$1) + if not if(~ $#1 1 && ! grep -s '^'^$logged_user^'$' etc/groups/$1) status=User $logged_user not in group $1 if not - true + status=() } -# If not logged in, try to get user login info from POST info or from cookie +# If not logged in, try to get user login info from POST or from cookie fn get_user { if(~ $#logged_user 0) { if(~ $#* 2) { user_name=$1 - user_password $2 + user_password=$2 } if not if(~ $REQUEST_METHOD POST) get_post_args user_name user_password @@ -229,7 +216,7 @@ fn get_user { auth_user $user_name $user_password } if not - true + status=() } # Check if user_name and user_password represent a valid user account @@ -249,46 +236,11 @@ fn auth_user { logged_user=$user_name logged_password=$user_password dprint Auth: success + status=() } } - -# Blog stuff -fn make_blog_post { - bdir=$1 - btitle=$2 - btext=$3 - if(! ~ 0 $#1 $#2 $#3) { - date=`{/bin/date +%F} - - n=1 - for(f in $bdir^$date^'-'*) { - i=`{echo -n $f | sed -n 's,^.*/'$date'-([0-9]+)_.*,\1,p'|tr -d $NEW_LINE} - if(! ~ $#i 0 && test $i -ge $n) - n=`{hoc -e $i'+1'} - } - btitle=`{echo -n $"btitle | sed 's/[ ]+/_/g; 1q'} - - echo $btext > $bdir^'/'^$"date^'-'^$"n^_$"btitle.md - } - if not - status=Missing blog post arguments $1 $2 $3 -} - -################################### -# App framework - -fn init_apps { - for(a in $enabled_apps) { - . ./apps/$a/app.rc - $a^'_init' - } -} - - -################################## -# Meta-data extract - +# .md '(meta-)data' extract fn get_md_file_attr { sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1 } diff --git a/bin/werc.rc b/bin/werc.rc @@ -99,15 +99,19 @@ fn setup_handlers { handler_body_main=(txt_handler $local_path.txt) # Apps - if(! ~ $#enabled_apps 0) - init_apps + if(! ~ $#enabled_apps 0) { + for(a in $enabled_apps) { + . ./apps/$a/app.rc + $a^'_init' + } + } if(! ~ $#handler_body_main 0) - { } + { } # We are done # Dir listing if not if(~ $local_path */index) handler_body_main=(dir_listing_handler $local_path) - # Explicit .html urls, the web server might handle this first! + # Canonize explicit .html urls, the web server might handle this first! if not if(~ $local_path *.html && test -f $local_path) perm_redirect `{ echo $req_path|sed 's/.html$//' } # Fallback static file handler @@ -116,18 +120,13 @@ fn setup_handlers { # File not found if not { handler_body_main=(tpl_handler `{get_lib_file 404.tpl}) - dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT echo 'Status: 404 Not Found' + dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT } } -fn run_handlers { - for(h in $*) - run_handler $$h -} -fn run_handler { - $*(1) $*(2-) -} +fn run_handlers { for(h in $*) run_handler $$h } +fn run_handler { $*(1) $*(2-) } # Careful, the proper p9p path might not be set until initrc.local is sourced path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin) diff --git a/lib/_apps/brag/post_form.tpl b/lib/_apps/brag/post_form.tpl @@ -9,7 +9,7 @@ if(~ $REQUEST_METHOD POST && ! ~ $#target_blog_dir 0 && ! ~ $#post_title 0) { if(~ $#blog_editors_group 0) echo Cant post to $target_blog_dir, no editors group set. - if not if (! check_user $blog_editors_group) + if not if(! check_user $blog_editors_group) echo Post to $target_blog_dir denied, user $logged_user not in group $blog_editors_group if not if(! make_blog_post $target_blog_dir $post_title $post_body) echo Posting to $target_blog_dir failed: $status