shithub: werc

ref: 1f64d2d6ff1fc0b68ff1364e8eefc67b6706882f
dir: /bin/cgilib.rc/

View raw version
# Useful functions

NEW_LINE = '
'

fn dprint { echo $* >[1=2] }

fn escape_html { sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' $* }

fn perm_redirect {
    echo 'Status: 301 Moved Permanantly
Location: '^$1^'

'
    exit
}

fn get_post_args {
    ifs='&
'   for(pair in `{cat}) {
        pair = `{echo -n $pair | sed 's/=/\&/'} \
        ifs=() \
        if(~ $pair(1) $*)
            $pair(1) = `{echo $pair(2) | urldecode | tr -d '
'}
    }
}

# Is this really useful?
fn awk_buffer {
    awk '{
        buf = buf $0"\n"
        if(length(buf) > 8192) {
            printf "%s", buf
            buf = ""
        }
    }
    END{ printf "%s", buf }'
}

fn template { 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}
}


fn urldecode {
awk '
BEGIN {
    hextab ["0"] = 0; hextab ["8"] = 8;
    hextab ["1"] = 1; hextab ["9"] = 9;
    hextab ["2"] = 2; hextab ["A"] = hextab ["a"] = 10
    hextab ["3"] = 3; hextab ["B"] = hextab ["b"] = 11;
    hextab ["4"] = 4; hextab ["C"] = hextab ["c"] = 12;
    hextab ["5"] = 5; hextab ["D"] = hextab ["d"] = 13;
    hextab ["6"] = 6; hextab ["E"] = hextab ["e"] = 14;
    hextab ["7"] = 7; hextab ["F"] = hextab ["f"] = 15;
}
{
    decoded = ""
    i   = 1
    len = length ($0)
    while ( i <= len ) {
        c = substr ($0, i, 1)
        if ( c == "%" ) {
            if ( i+2 <= len ) {
                c1 = substr ($0, i+1, 1)
                c2 = substr ($0, i+2, 1)
                if ( hextab [c1] == "" || hextab [c2] == "" ) {
                    print "WARNING: invalid hex encoding: %" c1 c2 | "cat >&2"
                } else {
                    code = 0 + hextab [c1] * 16 + hextab [c2] + 0
                    c = sprintf ("%c", code)
                    i = i + 2
                }
            } else {
                print "WARNING: invalid % encoding: " substr ($0, i, len - i)
            }
        } else if ( c == "+" ) {
            c = " "
        }
        decoded = decoded c
        ++i
    }
    print decoded
}
'
}

# Auth code
# Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password)

fn auth_user {
    group = $1
    user_name = $2
    user_pass = $3

    pfile = etc/users/$user_name/password
    grep -s '^'^$user_name^'$' etc/groups/$group && test -f $pfile && ~ $user_pass `{cat $pfile}
}


fn make_blog_post {
    bdir = $1
    title = $2

    date=`{/bin/date +%F}

    n = 1
    for(f in $bdir/$date-*) {
        i = `{echo $f | sed -n 's|^.*/'$date'-([0-9]+)_.*|\1|p'}
        if(! ~ $#i 0 && test $i -ge $n)
            n = `{hoc -e $i'+1'}
    }
    title = `{echo $"title | sed 's/[ 	]+/_/g; 1q'}

    $bdir/$"date^'-'^$"n^_$"title.md

}


# --------
#
#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 {
#
#}