werc-1.5.0-tweaks

Tweaks for the werc website builder created by the mad architect Uriel
Log | Files | Refs | README

app.rc (3472B)


      1 comment_file_types=(md html)
      2 
      3 fn conf_enable_comments {
      4     if(~ $1 -n) {
      5         allow_new_user_comments=yes
      6         shift
      7     }
      8     if not if(~ $1 -a) {
      9         bridge_anon_comments=yes 
     10     }
     11     enable_comments=yes
     12     groups_allowed_comments=$*
     13     conf_enable_app bridge
     14 }
     15 
     16 fn bridge_init {
     17     if(~ $#enable_comments 1 && ! ~ `{ls $local_path.$comment_file_types >[2]/dev/null|wc -l} 0) {
     18 
     19         comments_dir=$sitedir$req_path'_werc/comments'
     20         if(~ $REQUEST_METHOD GET && test -d $comments_dir)
     21             ll_add handlers_body_foot template apps/bridge/comments_list.tpl
     22 
     23         if(check_user $groups_allowed_comments || {~ $#logged_user 0 && ~ 1 $#allow_new_user_comments $#bridge_anon_comments}) {
     24 
     25             if(~ $#post_arg_bridge_post 1) {
     26                 ll_add handlers_body_foot template apps/bridge/foot.tpl
     27 
     28                 if(mk_new_comment $comments_dir)
     29                     post_redirect $base_url^$req_path
     30                 if not
     31                     saved_comment_text=$post_arg_comment_text
     32             }
     33             if not if(~ $REQUEST_METHOD GET)
     34                 ll_add handlers_body_foot template apps/bridge/foot.tpl
     35         }
     36         if not if(~ $REQUEST_METHOD GET)
     37             ll_add handlers_body_foot echo '<hr><p>To post a comment you need to <a href="/_users/login">login</a> first.</p>'
     38     }
     39 }
     40 
     41 fn validate_new_user {
     42     usr=$1; pass=$2; pass2=$3
     43     _status=()
     44 
     45     if(~ $"usr '' || ! echo $usr |sed 1q|grep -s '^'$allowed_user_chars'+$')
     46         _status='Requested user name is invalid, must match: '^$allowed_user_chars^'+'
     47     if not if(test -d etc/users/$usr)
     48         _status='Sorry, user name '''^$usr^''' already taken, please pick a different one.'
     49 
     50     if(~ $"pass '' || ! ~ $"pass $"pass2)
     51         _status=($_status 'Provided passwords don''t match.')
     52 
     53     status=$_status
     54 }
     55 
     56 
     57 fn mk_new_comment {
     58     _status=()
     59     dir=$1
     60     if(~ $"post_arg_comment_text '')
     61         _status='Provide a comment!'
     62     if not if(~ $#logged_user 0) {
     63         if(! ~ $#allow_new_user_comments 0) {
     64             if(validate_new_user $"post_arg_comment_user $post_arg_comment_passwd $post_arg_comment_passwd2) {
     65                 u=$post_arg_comment_user':'$post_arg_comment_passwd
     66                 dir=$comments_dir^'_pending'
     67                 # XXX: This doesn't work because we then do a redirect. 
     68                 notify_notes='Saved comment and registration info, they will be enabled when approved by an admin.'
     69             }
     70             if not
     71                 _status=$status
     72         }
     73         if not if(! ~ $#bridge_anon_comments 0) {
     74             if(~ $"post_arg_ima_robot 'not')
     75                 u='Glenda' # Anonymous
     76             if not
     77                 _status='You are a robot!'
     78         }
     79         if not
     80             _status='You need to log in to comment.'
     81     }
     82     if not if(check_user $groups_allowed_comments)
     83         u=$logged_user
     84     if not
     85         _status='You are not a member of a group allowed to comment.'
     86 
     87     if(~ $#_status 0) {
     88         umask 002
     89 
     90         dir=$dir'/'`{date -n} # FIXME Obvious race
     91         mkdir -m 775 -p $dir &&
     92             echo $u > $dir/user &&
     93             echo $current_date_time > $dir/posted &&
     94             echo $post_arg_comment_text > $dir/body
     95         _s=$status
     96         if(! ~ $"_s '') {
     97            dprint 'ERROR XXX: Could not create comment: ' $_s 
     98             _status='Could not post comment due internal error, sorry.'
     99         }
    100     }
    101     notify_errors=$_status
    102     status=$_status
    103 }