changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/werclib.rc

changeset 662: bd76af29cfa7
parent: fb460a671d19
author: sl
date: Wed, 10 Feb 2016 20:29:27 -0500
permissions: -rwxr-xr-x
description: bin/werclib.rc: add datei() and datet()
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 datei { datet | sed 's/T.*$//' }
124 
125 fn datet {
126  date=`{read}
127  switch($date(2)){
128  case Jan; mo=01
129  case Feb; mo=02
130  case Mar; mo=03
131  case Apr; mo=04
132  case May; mo=05
133  case Jun; mo=06
134  case Jul; mo=07
135  case Aug; mo=08
136  case Sep; mo=09
137  case Oct; mo=10
138  case Nov; mo=11
139  case Dec; mo=12
140  }
141  switch($date(3)){
142  case [0-9]
143  da=0^$date(3)
144  case *
145  da=$date(3)
146  }
147  switch($date(5)){
148  case A; tz=+0100
149  case ADT; tz=-0300
150  case AFT; tz=+430
151  case AKDT; tz=-0800
152  case AKST; tz=-0900
153  case ALMT; tz=+0600
154  case AMST; tz=-0300
155  case AMT; tz=-0400
156  case ANAST; tz=+1200
157  case ANAT; tz=+1200
158  case AQTT; tz=+0500
159  case ART; tz=-0300
160  case AST; tz=-0400
161  case AZOST; tz=+0000
162  case AZOT; tz=-0100
163  case AZST; tz=+0500
164  case AZT; tz=+0400
165  case B; tz=+0200
166  case BNT; tz=+0800
167  case BOT; tz=-0400
168  case BRST; tz=-0200
169  case BRT; tz=-0300
170  case BST; tz=+0100
171  case BTT; tz=+0600
172  case C; tz=+0300
173  case CAST; tz=+0800
174  case CAT; tz=+0200
175  case CCT; tz=+0630
176  case CDT; tz=-0500
177  case CEST; tz=+0200
178  case CET; tz=+0100
179  case CHADT; tz=+1345
180  case CHAST; tz=+1245
181  case CKT; tz=-1000
182  case CLST; tz=-0300
183  case CLT; tz=-0400
184  case COT; tz=-0500
185  case CST; tz=-0600
186  case CVT; tz=-0100
187  case CXT; tz=+0700
188  case ChST; tz=+1000
189  case D; tz=+0400
190  case DAVT; tz=+0700
191  case E; tz=+0500
192  case EASST; tz=-0500
193  case EAST; tz=-0600
194  case EAT; tz=+0300
195  case ECT; tz=-0500
196  case EDT; tz=-0400
197  case EEST; tz=+0300
198  case EET; tz=+0200
199  case EGST; tz=+0000
200  case EGT; tz=-0100
201  case EST; tz=-0500
202  case ET; tz=-0500
203  case F; tz=+0600
204  case FJST; tz=+1300
205  case FJT; tz=+1200
206  case FKST; tz=-0300
207  case FKT; tz=-0400
208  case FNT; tz=-0200
209  case G; tz=+0700
210  case GALT; tz=-0600
211  case GAMT; tz=-0900
212  case GET; tz=+0400
213  case GFT; tz=-0300
214  case GILT; tz=+1200
215  case GMT; tz=+0000
216  case GST; tz=+0400
217  case GYT; tz=-0400
218  case H; tz=+0800
219  case HAA; tz=-0300
220  case HAC; tz=-0500
221  case HADT; tz=-0900
222  case HAE; tz=-0400
223  case HAP; tz=-0700
224  case HAR; tz=-0600
225  case HAST; tz=-1000
226  case HAT; tz=-0230
227  case HAY; tz=-0800
228  case HKT; tz=+0800
229  case HLV; tz=-0430
230  case HNA; tz=-0400
231  case HNC; tz=-0600
232  case HNE; tz=-0500
233  case HNP; tz=-0800
234  case HNR; tz=-0700
235  case HNT; tz=-0330
236  case HNY; tz=-0900
237  case HOVT; tz=+0700
238  case I; tz=+0900
239  case ICT; tz=+0700
240  case IDT; tz=+0300
241  case IOT; tz=+0600
242  case IRDT; tz=+0430
243  case IRKST; tz=+0900
244  case IRKT; tz=+0800
245  case IRST; tz=+0330
246  case IST; tz=+0200
247  case JST; tz=+0900
248  case K; tz=+1000
249  case KGT; tz=+0600
250  case KRAST; tz=+0800
251  case KRAT; tz=+0700
252  case KST; tz=+0900
253  case KUYT; tz=+0400
254  case L; tz=+1100
255  case LHDT; tz=+1100
256  case LHST; tz=+1030
257  case LINT; tz=+1400
258  case M; tz=+1200
259  case MAGST; tz=+1200
260  case MAGT; tz=+1100
261  case MART; tz=-0930
262  case MAWT; tz=+0500
263  case MDT; tz=-0600
264  case MHT; tz=+1200
265  case MMT; tz=+0630
266  case MSD; tz=+0400
267  case MSK; tz=+0300
268  case MST; tz=-0700
269  case MUT; tz=+0400
270  case MVT; tz=+0500
271  case MYT; tz=+0800
272  case N; tz=-0100
273  case NCT; tz=+1100
274  case NDT; tz=-0230
275  case NFT; tz=+1130
276  case NOVST; tz=+0700
277  case NOVT; tz=+0600
278  case NPT; tz=+0545
279  case NST; tz=-0330
280  case NUT; tz=-1100
281  case NZDT; tz=+1300
282  case NZST; tz=+1200
283  case O; tz=-0200
284  case OMSST; tz=+0700
285  case OMST; tz=+0600
286  case P; tz=-0300
287  case PDT; tz=-0700
288  case PET; tz=-0500
289  case PETST; tz=+1200
290  case PETT; tz=+1200
291  case PGT; tz=+1000
292  case PHOT; tz=+1300
293  case PHT; tz=+0800
294  case PKT; tz=+0500
295  case PMDT; tz=-0200
296  case PMST; tz=-0300
297  case PONT; tz=+1100
298  case PST; tz=-0800
299  case PT; tz=-0800
300  case PWT; tz=+0900
301  case PYST; tz=-0300
302  case PYT; tz=-0400
303  case Q; tz=-0400
304  case R; tz=-0500
305  case RET; tz=+0400
306  case S; tz=-0600
307  case SAMT; tz=+0400
308  case SAST; tz=+0200
309  case SBT; tz=+1100
310  case SCT; tz=+0400
311  case SGT; tz=+0800
312  case SRT; tz=-0300
313  case SST; tz=-1100
314  case T; tz=-0700
315  case TAHT; tz=-1000
316  case TFT; tz=+0500
317  case TJT; tz=+0500
318  case TKT; tz=-1000
319  case TLT; tz=+0900
320  case TMT; tz=+0500
321  case TVT; tz=+1200
322  case U; tz=-0800
323  case ULAT; tz=+0800
324  case UYST; tz=-0200
325  case UYT; tz=-0300
326  case UZT; tz=+0500
327  case V; tz=-0900
328  case VET; tz=-0430
329  case VLAST; tz=+1100
330  case VLAT; tz=+1000
331  case VUT; tz=+1100
332  case W; tz=-1000
333  case WAST; tz=+0200
334  case WAT; tz=+0100
335  case WDT; tz=+0900
336  case WEST; tz=+0100
337  case WET; tz=+0000
338  case WFT; tz=+1200
339  case WGST; tz=-0200
340  case WGT; tz=-0300
341  case WIB; tz=+0700
342  case WIT; tz=+0900
343  case WITA; tz=+0800
344  case WST; tz=+0800
345  case WT; tz=+0000
346  case X; tz=-1100
347  case Y; tz=-1200
348  case YAKST; tz=+1000
349  case YAKT; tz=+0900
350  case YAPT; tz=+1000
351  case YEKST; tz=+0600
352  case YEKT; tz=+0500
353  case Z; tz=+0000
354  }
355  echo $date(6)^-$mo-$da^T^$date(4)^$tz
356 }
357 
358 ##########################################################################
359 ##########################################################################
360 #app_blog_methods = ( _post index.rss )
361 #fn app_blog__post {
362 # echo
363 #}
364 #
365 #app_blog___default {
366 # if (~ $blog)
367 # call_app blogpost
368 #}
369 #
370 ## --
371 #app_blogpost_methods = ( comment _edit )
372 #
373 #fn app_blogpost_comment {
374 # call_app comments
375 #}
376 #
377 ## --
378 #app_comments_methods = ( _post _edit )
379 #
380 #fn app_comments___default {
381 #
382 #}