thirdculture-werc/bin/werclib.rc

390 lines
8.7 KiB
Text
Executable file

fn get_lib_file {
if(! ~ $#sitedir 0 && test -f $sitedir/_werc/lib/$1)
echo -n $sitedir/_werc/lib/$1
if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1)
echo -n $sitesdir/$masterSite/_werc/lib/$1
if not if(test -f lib/$1)
echo -n lib/$1
if not if(~ $#* 2)
echo -n $2
if not
status='Can''t find lib file: '$1
}
fn template { awk -f bin/template.awk $* | rc $rcargs }
# Auth code
# TODO: check http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf
allowed_user_chars='[a-zA-Z0-9_]'
# 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: we set the cookie even if it is already there.
if(get_user $*)
set_cookie werc_user $"logged_user^':0:'^$"logged_password
}
# Check login status, if called with group arg we check membership too
fn check_user {
get_user
g=($* admin)
_status=$status
if(! ~ $"_status '')
_status=(Not logged in: $"_status)
if not if(! ~ $#* 0 && ! ~ $logged_user $* && ! grep -s '^'^$logged_user^'$' $werc_root/etc/users/$g/members >[2]/dev/null)
_status=(User $logged_user not in: $*)
status=$_status
}
# 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
}
if not if(~ $REQUEST_METHOD POST)
get_post_args user_name user_password
if(~ $#user_name 0) {
ifs=':' { cu=`{ifs=$difs {get_cookie werc_user} | tr -d $NEW_LINE} }
if(! ~ $#cu 0) {
user_name=$cu(1)
user_password=$cu(3)
}
}
auth_user $user_name $user_password
}
if not
status=()
}
# Check if user_name and user_password represent a valid user account
# If valid, 'log in' by setting logged_user
fn auth_user {
user_name=$1
user_password=$2
pfile=$werc_root/etc/users/$"user_name/password
if(~ $#user_name 0 || ~ $#user_password 0)
status=('Auth: missing user name or pass: '^$"user_name^' / '^$"user_password)
if not if(! test -f $pfile)
status=('Auth: cant find '^$pfile)
if not if(! test -s $pfile || ! ~ $user_password `{cat $pfile})
status=('Auth: Pass '$user_password' doesnt match '^`{cat $pfile})
if not {
logged_user=$user_name
logged_password=$user_password
dprint Auth: success
status=()
}
}
fn user_controls {
echo User: $"logged_user
}
# .md '(meta-)data' extract
fn get_md_file_attr {
sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1
}
# File title extraction
fn get_md_title {
#sed 's/^(................................................................[^ ]*).*$/\1/g; 1q' < $1
sed -n -e '1N; /^.*\n===*$/N; /.*\n===*\n *$/!b' -e 's/\n==*\n//p' < $1
}
fn get_html_title {
t=`{sed -n '32q; s/^.*<[Tt][Ii][Tt][Ll][Ee]> *([^<]+) *(<\/[Tt][Ii][Tt][Ll][Ee]>.*)?$/\1/p' < $1}
# As a backup we might want to pick the first 'non-tag' text in the file with:
if(~ $"t '')
t=`{sed -n -e 's/^(<[^>]+>)*([^<]+).*/\2/p; 32q' < $1 | sed 1q}
echo $t
}
fn get_file_title {
if (~ $1 *.md)
get_md_title $1
if not if(~ $1 *.html)
get_html_title $1
if not if(~ $1 */) {
if(test -f $1/index.md)
get_md_title $1/index.md
if not if(test -f $1/index.html)
get_html_title $1/index.html
}
}
fn ndate {
if(~ $#* 7)
date=$*(2-)
if not
date=`{date}
switch($date(2)){
case Jan; mo=01
case Feb; mo=02
case Mar; mo=03
case Apr; mo=04
case May; mo=05
case Jun; mo=06
case Jul; mo=07
case Aug; mo=08
case Sep; mo=09
case Oct; mo=10
case Nov; mo=11
case Dec; mo=12
}
switch($date(3)){
case [0-9]
da=0^$date(3)
case *
da=$date(3)
}
switch($date(5)){
case A; tz=+01:00
case ADT; tz=-03:00
case AFT; tz=+430
case AKDT; tz=-08:00
case AKST; tz=-09:00
case ALMT; tz=+06:00
case AMST; tz=-03:00
case AMT; tz=-04:00
case ANAST; tz=+12:00
case ANAT; tz=+12:00
case AQTT; tz=+05:00
case ART; tz=-03:00
case AST; tz=-04:00
case AZOST; tz=+00:00
case AZOT; tz=-01:00
case AZST; tz=+05:00
case AZT; tz=+04:00
case B; tz=+02:00
case BNT; tz=+08:00
case BOT; tz=-04:00
case BRST; tz=-02:00
case BRT; tz=-03:00
case BST; tz=+01:00
case BTT; tz=+06:00
case C; tz=+03:00
case CAST; tz=+08:00
case CAT; tz=+02:00
case CCT; tz=+0630
case CDT; tz=-05:00
case CEST; tz=+02:00
case CET; tz=+01:00
case CHADT; tz=+1345
case CHAST; tz=+1245
case CKT; tz=-10:00
case CLST; tz=-03:00
case CLT; tz=-04:00
case COT; tz=-05:00
case CST; tz=-06:00
case CVT; tz=-01:00
case CXT; tz=+07:00
case ChST; tz=+10:00
case D; tz=+04:00
case DAVT; tz=+07:00
case E; tz=+05:00
case EASST; tz=-05:00
case EAST; tz=-06:00
case EAT; tz=+03:00
case ECT; tz=-05:00
case EDT; tz=-04:00
case EEST; tz=+03:00
case EET; tz=+02:00
case EGST; tz=+00:00
case EGT; tz=-01:00
case EST; tz=-05:00
case ET; tz=-05:00
case F; tz=+06:00
case FJST; tz=+13:00
case FJT; tz=+12:00
case FKST; tz=-03:00
case FKT; tz=-04:00
case FNT; tz=-02:00
case G; tz=+07:00
case GALT; tz=-06:00
case GAMT; tz=-09:00
case GET; tz=+04:00
case GFT; tz=-03:00
case GILT; tz=+12:00
case GMT; tz=+00:00
case GST; tz=+04:00
case GYT; tz=-04:00
case H; tz=+08:00
case HAA; tz=-03:00
case HAC; tz=-05:00
case HADT; tz=-09:00
case HAE; tz=-04:00
case HAP; tz=-07:00
case HAR; tz=-06:00
case HAST; tz=-10:00
case HAT; tz=-0230
case HAY; tz=-08:00
case HKT; tz=+08:00
case HLV; tz=-0430
case HNA; tz=-04:00
case HNC; tz=-06:00
case HNE; tz=-05:00
case HNP; tz=-08:00
case HNR; tz=-07:00
case HNT; tz=-0330
case HNY; tz=-09:00
case HOVT; tz=+07:00
case I; tz=+09:00
case ICT; tz=+07:00
case IDT; tz=+03:00
case IOT; tz=+06:00
case IRDT; tz=+0430
case IRKST; tz=+09:00
case IRKT; tz=+08:00
case IRST; tz=+0330
case IST; tz=+02:00
case JST; tz=+09:00
case K; tz=+10:00
case KGT; tz=+06:00
case KRAST; tz=+08:00
case KRAT; tz=+07:00
case KST; tz=+09:00
case KUYT; tz=+04:00
case L; tz=+11:00
case LHDT; tz=+11:00
case LHST; tz=+1030
case LINT; tz=+14:00
case M; tz=+12:00
case MAGST; tz=+12:00
case MAGT; tz=+11:00
case MART; tz=-0930
case MAWT; tz=+05:00
case MDT; tz=-06:00
case MHT; tz=+12:00
case MMT; tz=+0630
case MSD; tz=+04:00
case MSK; tz=+03:00
case MST; tz=-07:00
case MUT; tz=+04:00
case MVT; tz=+05:00
case MYT; tz=+08:00
case N; tz=-01:00
case NCT; tz=+11:00
case NDT; tz=-0230
case NFT; tz=+1130
case NOVST; tz=+07:00
case NOVT; tz=+06:00
case NPT; tz=+0545
case NST; tz=-0330
case NUT; tz=-11:00
case NZDT; tz=+13:00
case NZST; tz=+12:00
case O; tz=-02:00
case OMSST; tz=+07:00
case OMST; tz=+06:00
case P; tz=-03:00
case PDT; tz=-07:00
case PET; tz=-05:00
case PETST; tz=+12:00
case PETT; tz=+12:00
case PGT; tz=+10:00
case PHOT; tz=+13:00
case PHT; tz=+08:00
case PKT; tz=+05:00
case PMDT; tz=-02:00
case PMST; tz=-03:00
case PONT; tz=+11:00
case PST; tz=-08:00
case PT; tz=-08:00
case PWT; tz=+09:00
case PYST; tz=-03:00
case PYT; tz=-04:00
case Q; tz=-04:00
case R; tz=-05:00
case RET; tz=+04:00
case S; tz=-06:00
case SAMT; tz=+04:00
case SAST; tz=+02:00
case SBT; tz=+11:00
case SCT; tz=+04:00
case SGT; tz=+08:00
case SRT; tz=-03:00
case SST; tz=-11:00
case T; tz=-07:00
case TAHT; tz=-10:00
case TFT; tz=+05:00
case TJT; tz=+05:00
case TKT; tz=-10:00
case TLT; tz=+09:00
case TMT; tz=+05:00
case TVT; tz=+12:00
case U; tz=-08:00
case ULAT; tz=+08:00
case UYST; tz=-02:00
case UYT; tz=-03:00
case UZT; tz=+05:00
case V; tz=-09:00
case VET; tz=-0430
case VLAST; tz=+11:00
case VLAT; tz=+10:00
case VUT; tz=+11:00
case W; tz=-10:00
case WAST; tz=+02:00
case WAT; tz=+01:00
case WDT; tz=+09:00
case WEST; tz=+01:00
case WET; tz=+00:00
case WFT; tz=+12:00
case WGST; tz=-02:00
case WGT; tz=-03:00
case WIB; tz=+07:00
case WIT; tz=+09:00
case WITA; tz=+08:00
case WST; tz=+08:00
case WT; tz=+00:00
case X; tz=-11:00
case Y; tz=-12:00
case YAKST; tz=+10:00
case YAKT; tz=+09:00
case YAPT; tz=+10:00
case YEKST; tz=+06:00
case YEKT; tz=+05:00
case Z; tz=+00:00
}
switch($1){
case -i
echo $date(6)^-$mo-$da
case -m
echo $date(1)^, $da $date(2) $date(6) $date(4) $tz
case -t
echo $date(6)^-$mo-$da^T^$date(4)^$tz
}
}
##########################################################################
##########################################################################
#app_blog_methods = ( _post index.rss )
#fn app_blog__post {
# echo
#}
#
#app_blog___default {
# if (~ $blog)
# call_app blogpost
#}
#
## --
#app_blogpost_methods = ( comment _edit )
#
#fn app_blogpost_comment {
# call_app comments
#}
#
## --
#app_comments_methods = ( _post _edit )
#
#fn app_comments___default {
#
#}