werclib.rc (8901B)
1 fn get_lib_file { 2 if(! ~ $#sitedir 0 && test -f $sitedir/_werc/lib/$1) 3 echo -n $sitedir/_werc/lib/$1 4 if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1) 5 echo -n $sitesdir/$masterSite/_werc/lib/$1 6 if not if(test -f lib/$1) 7 echo -n lib/$1 8 if not if(~ $#* 2) 9 echo -n $2 10 if not 11 status='Can''t find lib file: '$1 12 } 13 14 fn template { awk -f bin/template.awk $* | rc $rcargs } 15 16 # Auth code 17 # TODO: check http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf 18 allowed_user_chars='[a-zA-Z0-9_]' 19 # Cookie format: WERC_USER: name:timestamp:hash(name.timestamp.password) 20 # login_user can't be used from a template because it sets a cookie 21 fn login_user { 22 # Note: we set the cookie even if it is already there. 23 if(get_user $*) 24 set_cookie werc_user $"logged_user^':0:'^$"logged_password 25 } 26 27 # Check login status, if called with group arg we check membership too 28 fn check_user { 29 get_user 30 g=($* admin) 31 _status=$status 32 if(! ~ $"_status '') 33 _status=(Not logged in: $"_status) 34 if not if(! ~ $#* 0 && ! ~ $logged_user $* && ! grep -s '^'^$logged_user^'$' $werc_root/etc/users/$g/members >[2]/dev/null) 35 _status=(User $logged_user not in: $*) 36 status=$_status 37 } 38 39 # If not logged in, try to get user login info from POST or from cookie 40 fn get_user { 41 if(~ $#logged_user 0) { 42 if(~ $#* 2) { 43 user_name=$1 44 user_password=$2 45 } 46 if not if(~ $REQUEST_METHOD POST) 47 get_post_args user_name user_password 48 49 if(~ $#user_name 0) { 50 ifs=':' { cu=`{ifs=$difs {get_cookie werc_user} | tr -d $NEW_LINE} } 51 if(! ~ $#cu 0) { 52 user_name=$cu(1) 53 user_password=$cu(3) 54 } 55 } 56 auth_user $user_name $user_password 57 } 58 if not 59 status=() 60 } 61 62 # Check if user_name and user_password represent a valid user account 63 # If valid, 'log in' by setting logged_user 64 fn auth_user { 65 user_name=$1 66 user_password=$2 67 68 pfile=$werc_root/etc/users/$"user_name/password 69 if(~ $#user_name 0 || ~ $#user_password 0) 70 status=('Auth: missing user name or pass: '^$"user_name^' / '^$"user_password) 71 if not if(! test -f $pfile) 72 status=('Auth: cant find '^$pfile) 73 if not if(! test -s $pfile || ! ~ $user_password `{cat $pfile}) 74 status=('Auth: Pass '$user_password' doesnt match '^`{cat $pfile}) 75 if not { 76 logged_user=$user_name 77 logged_password=$user_password 78 dprint Auth: success 79 status=() 80 } 81 } 82 83 fn user_controls { 84 echo User: $"logged_user 85 } 86 87 88 # .md '(meta-)data' extract 89 fn get_md_file_attr { 90 sed -n '/^\* '$2': /p; /^\* '$2': /q; /^$/q' < $1 91 } 92 93 94 # File title extraction 95 fn get_md_title { 96 #sed 's/^(................................................................[^ ]*).*$/\1/g; 1q' < $1 97 sed -n -e '1N; /^.*\n===*$/N; /.*\n===*\n *$/!b' -e 's/\n==*\n//p' < $1 98 } 99 100 fn get_html_title { 101 t=`{sed -n '32q; s/^.*<[Tt][Ii][Tt][Ll][Ee]> *([^<]+) *(<\/[Tt][Ii][Tt][Ll][Ee]>.*)?$/\1/p' < $1} 102 103 # As a backup we might want to pick the first 'non-tag' text in the file with: 104 if(~ $"t '') 105 t=`{sed -n -e 's/^(<[^>]+>)*([^<]+).*/\2/p; 32q' < $1 | sed 1q} 106 107 echo $t 108 } 109 110 fn get_file_title { 111 if (~ $1 *.md) 112 get_md_title $1 113 if not if(~ $1 *.html) 114 get_html_title $1 115 if not if(~ $1 */) { 116 if(test -f $1/index.md) 117 get_md_title $1/index.md 118 if not if(test -f $1/index.html) 119 get_html_title $1/index.html 120 } 121 } 122 123 fn ndate { 124 if(~ $#* 7) 125 date=$*(2-) 126 if not 127 date=`{date} 128 switch($date(2)){ 129 case Jan; mo=01 130 case Feb; mo=02 131 case Mar; mo=03 132 case Apr; mo=04 133 case May; mo=05 134 case Jun; mo=06 135 case Jul; mo=07 136 case Aug; mo=08 137 case Sep; mo=09 138 case Oct; mo=10 139 case Nov; mo=11 140 case Dec; mo=12 141 } 142 switch($date(3)){ 143 case [0-9] 144 da=0^$date(3) 145 case * 146 da=$date(3) 147 } 148 switch($date(5)){ 149 case A; tz=+0100 150 case ADT; tz=-0300 151 case AFT; tz=+430 152 case AKDT; tz=-0800 153 case AKST; tz=-0900 154 case ALMT; tz=+0600 155 case AMST; tz=-0300 156 case AMT; tz=-0400 157 case ANAST; tz=+1200 158 case ANAT; tz=+1200 159 case AQTT; tz=+0500 160 case ART; tz=-0300 161 case AST; tz=-0400 162 case AZOST; tz=+0000 163 case AZOT; tz=-0100 164 case AZST; tz=+0500 165 case AZT; tz=+0400 166 case B; tz=+0200 167 case BNT; tz=+0800 168 case BOT; tz=-0400 169 case BRST; tz=-0200 170 case BRT; tz=-0300 171 case BST; tz=+0100 172 case BTT; tz=+0600 173 case C; tz=+0300 174 case CAST; tz=+0800 175 case CAT; tz=+0200 176 case CCT; tz=+0630 177 case CDT; tz=-0500 178 case CEST; tz=+0200 179 case CET; tz=+0100 180 case CHADT; tz=+1345 181 case CHAST; tz=+1245 182 case CKT; tz=-1000 183 case CLST; tz=-0300 184 case CLT; tz=-0400 185 case COT; tz=-0500 186 case CST; tz=-0600 187 case CVT; tz=-0100 188 case CXT; tz=+0700 189 case ChST; tz=+1000 190 case D; tz=+0400 191 case DAVT; tz=+0700 192 case E; tz=+0500 193 case EASST; tz=-0500 194 case EAST; tz=-0600 195 case EAT; tz=+0300 196 case ECT; tz=-0500 197 case EDT; tz=-0400 198 case EEST; tz=+0300 199 case EET; tz=+0200 200 case EGST; tz=+0000 201 case EGT; tz=-0100 202 case EST; tz=-0500 203 case ET; tz=-0500 204 case F; tz=+0600 205 case FJST; tz=+1300 206 case FJT; tz=+1200 207 case FKST; tz=-0300 208 case FKT; tz=-0400 209 case FNT; tz=-0200 210 case G; tz=+0700 211 case GALT; tz=-0600 212 case GAMT; tz=-0900 213 case GET; tz=+0400 214 case GFT; tz=-0300 215 case GILT; tz=+1200 216 case GMT; tz=+0000 217 case GST; tz=+0400 218 case GYT; tz=-0400 219 case H; tz=+0800 220 case HAA; tz=-0300 221 case HAC; tz=-0500 222 case HADT; tz=-0900 223 case HAE; tz=-0400 224 case HAP; tz=-0700 225 case HAR; tz=-0600 226 case HAST; tz=-1000 227 case HAT; tz=-0230 228 case HAY; tz=-0800 229 case HKT; tz=+0800 230 case HLV; tz=-0430 231 case HNA; tz=-0400 232 case HNC; tz=-0600 233 case HNE; tz=-0500 234 case HNP; tz=-0800 235 case HNR; tz=-0700 236 case HNT; tz=-0330 237 case HNY; tz=-0900 238 case HOVT; tz=+0700 239 case I; tz=+0900 240 case ICT; tz=+0700 241 case IDT; tz=+0300 242 case IOT; tz=+0600 243 case IRDT; tz=+0430 244 case IRKST; tz=+0900 245 case IRKT; tz=+0800 246 case IRST; tz=+0330 247 case IST; tz=+0200 248 case JST; tz=+0900 249 case K; tz=+1000 250 case KGT; tz=+0600 251 case KRAST; tz=+0800 252 case KRAT; tz=+0700 253 case KST; tz=+0900 254 case KUYT; tz=+0400 255 case L; tz=+1100 256 case LHDT; tz=+1100 257 case LHST; tz=+1030 258 case LINT; tz=+1400 259 case M; tz=+1200 260 case MAGST; tz=+1200 261 case MAGT; tz=+1100 262 case MART; tz=-0930 263 case MAWT; tz=+0500 264 case MDT; tz=-0600 265 case MHT; tz=+1200 266 case MMT; tz=+0630 267 case MSD; tz=+0400 268 case MSK; tz=+0300 269 case MST; tz=-0700 270 case MUT; tz=+0400 271 case MVT; tz=+0500 272 case MYT; tz=+0800 273 case N; tz=-0100 274 case NCT; tz=+1100 275 case NDT; tz=-0230 276 case NFT; tz=+1130 277 case NOVST; tz=+0700 278 case NOVT; tz=+0600 279 case NPT; tz=+0545 280 case NST; tz=-0330 281 case NUT; tz=-1100 282 case NZDT; tz=+1300 283 case NZST; tz=+1200 284 case O; tz=-0200 285 case OMSST; tz=+0700 286 case OMST; tz=+0600 287 case P; tz=-0300 288 case PDT; tz=-0700 289 case PET; tz=-0500 290 case PETST; tz=+1200 291 case PETT; tz=+1200 292 case PGT; tz=+1000 293 case PHOT; tz=+1300 294 case PHT; tz=+0800 295 case PKT; tz=+0500 296 case PMDT; tz=-0200 297 case PMST; tz=-0300 298 case PONT; tz=+1100 299 case PST; tz=-0800 300 case PT; tz=-0800 301 case PWT; tz=+0900 302 case PYST; tz=-0300 303 case PYT; tz=-0400 304 case Q; tz=-0400 305 case R; tz=-0500 306 case RET; tz=+0400 307 case S; tz=-0600 308 case SAMT; tz=+0400 309 case SAST; tz=+0200 310 case SBT; tz=+1100 311 case SCT; tz=+0400 312 case SGT; tz=+0800 313 case SRT; tz=-0300 314 case SST; tz=-1100 315 case T; tz=-0700 316 case TAHT; tz=-1000 317 case TFT; tz=+0500 318 case TJT; tz=+0500 319 case TKT; tz=-1000 320 case TLT; tz=+0900 321 case TMT; tz=+0500 322 case TVT; tz=+1200 323 case U; tz=-0800 324 case ULAT; tz=+0800 325 case UYST; tz=-0200 326 case UYT; tz=-0300 327 case UZT; tz=+0500 328 case V; tz=-0900 329 case VET; tz=-0430 330 case VLAST; tz=+1100 331 case VLAT; tz=+1000 332 case VUT; tz=+1100 333 case W; tz=-1000 334 case WAST; tz=+0200 335 case WAT; tz=+0100 336 case WDT; tz=+0900 337 case WEST; tz=+0100 338 case WET; tz=+0000 339 case WFT; tz=+1200 340 case WGST; tz=-0200 341 case WGT; tz=-0300 342 case WIB; tz=+0700 343 case WIT; tz=+0900 344 case WITA; tz=+0800 345 case WST; tz=+0800 346 case WT; tz=+0000 347 case X; tz=-1100 348 case Y; tz=-1200 349 case YAKST; tz=+1000 350 case YAKT; tz=+0900 351 case YAPT; tz=+1000 352 case YEKST; tz=+0600 353 case YEKT; tz=+0500 354 case Z; tz=+0000 355 } 356 switch($1){ 357 case -a # rfc3339 358 tz=`{echo $tz | sed 's/00$/:00/'} 359 echo $date(6)^-$mo-$da^T^$date(4)^$tz 360 case -i # iso-8601 lite 361 echo $date(6)^-$mo-$da 362 case -m # rfc2822 363 echo $date(1)^, $da $date(2) $date(6) $date(4) $tz 364 case -t # iso-8601 365 echo $date(6)^-$mo-$da^T^$date(4)^$tz 366 } 367 } 368 369 ########################################################################## 370 ########################################################################## 371 #app_blog_methods = ( _post index.rss ) 372 #fn app_blog__post { 373 # echo 374 #} 375 # 376 #app_blog___default { 377 # if (~ $blog) 378 # call_app blogpost 379 #} 380 # 381 ## -- 382 #app_blogpost_methods = ( comment _edit ) 383 # 384 #fn app_blogpost_comment { 385 # call_app comments 386 #} 387 # 388 ## -- 389 #app_comments_methods = ( _post _edit ) 390 # 391 #fn app_comments___default { 392 # 393 #}