Various small cleanups: remove unused code, clarity and reliability fixes here and here, move code to more appropriate locations, improve some comments.
This commit is contained in:
parent
76bd359a11
commit
ae20a466a3
4 changed files with 46 additions and 72 deletions
|
|
@ -42,3 +42,26 @@ fn get_post_list {
|
||||||
# NOTE: this breaks if any path element in blogDirs contain '/./' or '|'
|
# 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,/+\|/+,/,'
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -110,13 +110,11 @@ BEGIN {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn crop_text {
|
fn crop_text {
|
||||||
max_chars=$1
|
|
||||||
|
|
||||||
ellipsis='...'
|
ellipsis='...'
|
||||||
if(~ $#* 2)
|
if(~ $#* 2)
|
||||||
ellipsis=$2
|
ellipsis=$2
|
||||||
|
|
||||||
awk -v max'='^$"max_chars^' ' -v 'ellipsis='$ellipsis '
|
awk -v max'='^$"1^' ' -v 'ellipsis='$ellipsis '
|
||||||
{
|
{
|
||||||
nc += 1 + length;
|
nc += 1 + length;
|
||||||
if(nc > max) {
|
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 }
|
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
|
# Auth code
|
||||||
|
|
||||||
# Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password)
|
# Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password)
|
||||||
# login_user can't be used from a template because it sets a cookie
|
# login_user can't be used from a template because it sets a cookie
|
||||||
fn login_user {
|
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 $*)
|
if(get_user $*)
|
||||||
set_cookie werc_user $"logged_user^':0:'^$"logged_password
|
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 {
|
fn check_user {
|
||||||
if(! get_user)
|
if(! get_user)
|
||||||
status='Not logged in'
|
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
|
status=User $logged_user not in group $1
|
||||||
if not
|
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 {
|
fn get_user {
|
||||||
if(~ $#logged_user 0) {
|
if(~ $#logged_user 0) {
|
||||||
if(~ $#* 2) {
|
if(~ $#* 2) {
|
||||||
user_name=$1
|
user_name=$1
|
||||||
user_password $2
|
user_password=$2
|
||||||
}
|
}
|
||||||
if not if(~ $REQUEST_METHOD POST)
|
if not if(~ $REQUEST_METHOD POST)
|
||||||
get_post_args user_name user_password
|
get_post_args user_name user_password
|
||||||
|
|
@ -229,7 +216,7 @@ fn get_user {
|
||||||
auth_user $user_name $user_password
|
auth_user $user_name $user_password
|
||||||
}
|
}
|
||||||
if not
|
if not
|
||||||
true
|
status=()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if user_name and user_password represent a valid user account
|
# Check if user_name and user_password represent a valid user account
|
||||||
|
|
@ -249,46 +236,11 @@ fn auth_user {
|
||||||
logged_user=$user_name
|
logged_user=$user_name
|
||||||
logged_password=$user_password
|
logged_password=$user_password
|
||||||
dprint Auth: success
|
dprint Auth: success
|
||||||
|
status=()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# .md '(meta-)data' extract
|
||||||
# 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
|
|
||||||
|
|
||||||
fn get_md_file_attr {
|
fn get_md_file_attr {
|
||||||
sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1
|
sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1
|
||||||
}
|
}
|
||||||
|
|
|
||||||
23
bin/werc.rc
23
bin/werc.rc
|
|
@ -99,15 +99,19 @@ fn setup_handlers {
|
||||||
handler_body_main=(txt_handler $local_path.txt)
|
handler_body_main=(txt_handler $local_path.txt)
|
||||||
|
|
||||||
# Apps
|
# Apps
|
||||||
if(! ~ $#enabled_apps 0)
|
if(! ~ $#enabled_apps 0) {
|
||||||
init_apps
|
for(a in $enabled_apps) {
|
||||||
|
. ./apps/$a/app.rc
|
||||||
|
$a^'_init'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(! ~ $#handler_body_main 0)
|
if(! ~ $#handler_body_main 0)
|
||||||
{ }
|
{ } # We are done
|
||||||
# Dir listing
|
# Dir listing
|
||||||
if not if(~ $local_path */index)
|
if not if(~ $local_path */index)
|
||||||
handler_body_main=(dir_listing_handler $local_path)
|
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)
|
if not if(~ $local_path *.html && test -f $local_path)
|
||||||
perm_redirect `{ echo $req_path|sed 's/.html$//' }
|
perm_redirect `{ echo $req_path|sed 's/.html$//' }
|
||||||
# Fallback static file handler
|
# Fallback static file handler
|
||||||
|
|
@ -116,18 +120,13 @@ fn setup_handlers {
|
||||||
# File not found
|
# File not found
|
||||||
if not {
|
if not {
|
||||||
handler_body_main=(tpl_handler `{get_lib_file 404.tpl})
|
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'
|
echo 'Status: 404 Not Found'
|
||||||
|
dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_handlers {
|
fn run_handlers { for(h in $*) run_handler $$h }
|
||||||
for(h in $*)
|
fn run_handler { $*(1) $*(2-) }
|
||||||
run_handler $$h
|
|
||||||
}
|
|
||||||
fn run_handler {
|
|
||||||
$*(1) $*(2-)
|
|
||||||
}
|
|
||||||
|
|
||||||
# Careful, the proper p9p path might not be set until initrc.local is sourced
|
# Careful, the proper p9p path might not be set until initrc.local is sourced
|
||||||
path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin)
|
path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ if(~ $REQUEST_METHOD POST && ! ~ $#target_blog_dir 0 && ! ~ $#post_title 0) {
|
||||||
|
|
||||||
if(~ $#blog_editors_group 0)
|
if(~ $#blog_editors_group 0)
|
||||||
echo Cant post to $target_blog_dir, no editors group set.
|
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
|
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)
|
if not if(! make_blog_post $target_blog_dir $post_title $post_body)
|
||||||
echo Posting to $target_blog_dir failed: $status
|
echo Posting to $target_blog_dir failed: $status
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue