shithub: werc

Download patch

ref: 389f063a915a26e08adab95461a17ab4338e0d55
parent: 19d6aa26e4a6bc77be5a0048ed55f4822b115181
author: uriel <uriel@engel.se.cat-v.org>
date: Sat Jan 10 23:03:46 EST 2009

Handler interface reorg, take oportunity to cleanup default_master.tpl and move comments bits to bridge app.

--- a/apps/bridge/app.rc
+++ b/apps/bridge/app.rc
@@ -1,26 +1,35 @@
 fn bridge_init {
-    if(! ~ $#allowComments 0 && ~ $REQUEST_METHOD POST) {
-        get_post_args comment_text
-        d=`{date -n} # FIXME Obvious race
-        d=$local_path.md_werc/comments/$d/
+    if(! ~ $#allowComments 0) {
+        ll_add handlers_body_foot bridge_body_foot 
+        if(get_post_args comment_text) {
+            dprint POST COMMNET $"comment_text
+            d=`{date -n} # FIXME Obvious race
+            d=$local_path^'_werc/comments/'^$d/
 
-        u=$logged_user
-        if(~ $#logged_user 0) {
-            get_post_args comment_user_name comment_user_password
-            # TODO Should take this path too if the user can login but doesn't
-            # belong to required group
-            if(! login_user $comment_user_name $comment_user_password) {
-                u=$comment_user_name ':' $comment_user_password
-                d=$d^'_pending'
+            u=$logged_user
+            if(~ $#logged_user 0) {
+                get_post_args comment_user_name comment_user_password
+                # XXX Should do this too if user not in required group
+                if(! login_user $comment_user_name $comment_user_password) {
+                    u=$comment_user_name ':' $comment_user_password
+                    d=$d^'_pending'
+                }
+                if not
+                    u = $logged_user
             }
-            if not
-                u = $logged_user
-        }
 
-        umask 002
-        mkdir -m 775 -p $d
-        echo $u > $d/user
-        echo $comment_text > $d/body
+            umask 002
+            mkdir -m 775 -p $d
+            echo $u > $d/user
+            echo $comment_text > $d/body
+        }
+        if not
+            dprint SSS $status
     }
 
 }
+
+fn bridge_body_foot {
+    template apps/bridge/foot.tpl
+}
+
--- /dev/null
+++ b/apps/bridge/foot.tpl
@@ -1,0 +1,25 @@
+% cdir = $local_path^'_werc/comments'
+% if(test -d $cdir) { 
+    <hr /><h2>Comments</h2>
+%    for(c in `{ls $cdir/}) {
+        <div>By: '`{cat $c/user} '<br />
+%       cat $c/body | escape_html | sed 's,$,<br />,'
+        <hr /></div>
+%    }
+% }
+
+% get_post_args action
+% dprint XXX  $"action
+
+<hr /><hr />
+<form action="" method="post">
+% if(! check_user) {
+    User: <input type="text" name="comment_user_name" value="" />
+    Password: <input type="password" name="comment_user_password" value="" />
+    <small>If you are not registered enter your desired user/password and your account will be created when your comment is approved.</small>
+% }
+
+    <textarea name="comment_text" id="comment_text" cols="80" rows="16"></textarea>
+    <input type="submit" name="post_comment" value="Post a comment" />
+</form>
+
--- a/bin/werc.rc
+++ b/bin/werc.rc
@@ -58,8 +58,6 @@
 
 
 # Handlers
-fn set_handler { handler=$* }
-
 fn md_handler { cat $* | $formatter }
 
 fn tpl_handler { template $1 }
@@ -86,69 +84,49 @@
     echo '</ul>'
 }
 
-fn select_handler {
+fn setup_handlers {
 
     if(test -f $local_path.md)
-        set_handler md_handler $local_path.md
-
+        handler_body_main=(md_handler $local_path.md)
     if not if(test -f $local_path.tpl)
-        set_handler tpl_handler $local_path.tpl
-
+        handler_body_main=(tpl_handler $local_path.tpl)
     if not if(test -f $local_path.html)
-        set_handler html_handler $local_path.html
-
+        handler_body_main=(html_handler $local_path.html)
     # Global tpl (eg sitemap.tpl), should take precedence over txt handler!
     if not if(test -f lib^$req_path^.tpl)
-        set_handler tpl_handler lib^$req_path^.tpl
-
+        handler_body_main=(tpl_handler lib^$req_path^.tpl)
     if not if(test -f $local_path.txt)
-        set_handler txt_handler $local_path.txt
+        handler_body_main=(txt_handler $local_path.txt)
 
     # Apps
-    if not if(! ~ $#enabled_apps 0 && select_apps)
-        set_handler app_handler
+    if(! ~ $#enabled_apps 0)
+        init_apps
 
     # Dir listing
-    if not if(~ $local_path */index)
+    if(~ $#handler_body_main 0 && ~ $local_path */index)
         set_handler dir_listing_handler $local_path
-
-    # Explicit .html urls, unfortunately usually the web server will handle this as static files
-    if not if(~ $local_path *.html && test -f $local_path)
+    # Explicit .html urls, the web server might handle this first!
+    if not if(~ $#handler_body_main 0 && ~ $local_path *.html && test -f $local_path)
         perm_redirect `{ echo $req_path|sed 's/.html$//' }
-
     # Fallback static file handler
-    if not if(test -f $local_path) {
-        m='text/plain'
-        if(~ $req_path *.css)
-            m='text/css'
-        if not if(~ $req_path *.ico)
-            m='image/x-icon'
-        if not if(~ $req_path *.png)
-            m='image/png'
-        if not if(~ $req_path *.jpg *.jpeg)
-            m='image/jpeg'
-        if not if(~ $req_path *.gif)
-            m='image/gif'
-        if not if(~ $req_path *.pdf)
-            m='application/pdf'
-
-        echo 'Content-Type: '^$m
-        echo
-        cat $local_path
-        exit
-    }
-
+    if not if(~ $#handler_body_main 0 && test -f $local_path)
+        static_file $local_path
     # File not found
-    if not {
+    if not if(~ $#handler_body_main 0) {
         set_handler tpl_handler `{get_lib_file 404.tpl}
         dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
         echo 'Status: 404 Not Found'
     }
+}
 
+fn run_handlers {
+    for(h in $*)
+        run_handler $$h
 }
+fn run_handler {
+    $*(1) $*(2-)
+}
 
-fn genbody { $handler(1) $handler(2-) }
-
 # Careful, the proper p9p path might not be set until initrc.local is sourced
 path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin) 
 
@@ -226,7 +204,7 @@
 if not
     pageTitle=$"pageTitle^' | '^$"siteTitle^' '^$"siteSubTitle
 
-select_handler
+setup_handlers
 
 if(! ~ $#debug 0)
     dprint '  '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler
--- a/lib/default_master.tpl
+++ b/lib/default_master.tpl
@@ -42,39 +42,16 @@
 % }
 
 <div id="main-copy">
-% genbody
 
-% if(! ~ $#allowComments 0) {
+% run_handlers $handlers_body_head
 
-%{
-cdir = $local_path.md_werc/comments
-if(test -d $cdir) { 
-    echo '<hr /><h2>Comments</h2>'
-    for(c in `{ls $cdir/}) {
-%}
-        <div>
-%       echo  By: `{cat $c/user}
-        <br />
-%       cat $c/body | escape_html | sed 's,$,<br />,'
-        <hr /></div>
-%{
-    }
-}
-%}
-<hr /><hr />
-<form action="" method="post">
-% if(! check_user) {
-    User: <input type="text" name="comment_user_name" value="" /> Password:
-    <input type="password" name="comment_user_password" value="" />
-    <small>If you are not registered enter your desired user/password and your account will be created when your comment is approved.</small>
-% }
-    <textarea name="comment_text" id="comment_text" cols="80" rows="16"></textarea>
-    <input type="submit" name="post_comment" value="Post a comment" />
-</form>
-% }
+% run_handler $handler_body_main
 
+% run_handlers $handlers_body_foot
+
 </div>
 
 <div id="footer">
 % cat `{ get_lib_file footer.inc }
+% echo $"logged_user
 </div>