#!/mod/bin/jimsh package require cgi source /mod/webif/lib/setup require system.class set dir [cgi_get dir ""] if {$dir eq "" && [llength $argv]} { set dir [lindex $argv 0] } set root [system mediaroot] if {$dir eq "" || $dir eq $root} { set dir $root set cf "/mod/etc/sweeper.conf" } elseif {[file isdirectory $dir]} { set cf "$dir/.sweeper" } else { set cf $dir } httpheader "application/json" # Load rules if {![file exists $cf]} { set rules {} } else { if {[catch {set fp [open $cf r]} msg]} { log "Error opening sweeper ruleset ($cf), $msg" 0 exit } set rules [split [$fp read] "\n"] } set lcomment "" proc comment {id comment} { set ::lcomment [string trim $comment] } set json "" proc add_json {str} { global json append json $str } proc quot {str} { return [string map { "\\" "\\\\" } [cgi_quote_html $str]] } set composite {and or} proc clause {cmd arg {indent 6}} { global lockfound composite set istr [string repeat " " $indent] if {[string index $cmd 0] eq "!"} { set negate 1 set cmd [string range $cmd 1 end] } else { set negate 0 } if {$cmd eq "lock"} { set lockfound $arg } add_json "$istr{\n" add_json "$istr \"negate\": $negate,\n" add_json "$istr \"cmd\": \"[quot $cmd]\",\n" add_json "$istr \"arg\": \"[quot $arg]\"" if {$cmd in $composite} { add_json ",\n" add_json "$istr \"criteria\": \[\n" set c 0 while {[llength $arg] > 1} { set arg [lassign $arg subcmd subarg] if {$c} { add_json ",\n" } incr c clause $subcmd $subarg $($indent + 6) } add_json "\n$istr ]" } add_json "\n" add_json "$istr}" } proc rule {id rule} { global lcomment lockfound set enabled 1 if {[lindex $rule 0] eq "##"} { set enabled 0 set rule [lrange $rule 1 end] } if {$lcomment eq ""} { set lcomment "Unnamed rule" } switch -- [lindex $rule 0] { folder { set type "folder" set depth 0 set rule [lrange $rule 1 end] } global { set type "recurse" set depth 1 set rule [lrange $rule 1 end] } recurse { set type "recurse" set depth [lindex $rule 1] set rule [lrange $rule 2 end] } default { set type "file" set depth 0 } } if {$id > 1} { add_json ",\n" } add_json " {\n" add_json " \"raw\": \"[quot $rule]\",\n" add_json " \"name\": \"[quot $lcomment]\",\n" add_json " \"type\": \"$type\",\n" add_json " \"depth\": \"$depth\",\n" add_json " \"enabled\": \"$enabled\",\n" add_json " \"criteria\": \[\n" set lcomment "" set c 0 set lockfound -1 while {[llength $rule] > 1} { set rule [lassign $rule cmd arg] if {$cmd eq "action"} break if {$c} { add_json ",\n" } incr c clause $cmd $arg } add_json "\n" if {$cmd ne "action"} { if {$lockfound != -1} { if {$lockfound == "1"} { set arg "lock" } else { set arg "unlock" } } else { set arg "continue" } } lassign "$arg" cmd arg if {$cmd eq "preserve"} { set cmd 'stop' } add_json " ],\n" add_json " \"action\": {\n" add_json " \"cmd\": \"[quot $cmd]\",\n" add_json " \"arg\": \"[quot $arg]\"\n" add_json " }\n" add_json " }" } add_json "\[\n" set n 0 foreach rule $rules { if {[string index $rule 0] eq "#"} { if {[string index $rule 1] ne "#"} { comment $n [string range $rule 1 end] continue } } if {[llength $rule] < 2} continue incr n rule $n $rule } add_json "\n]\n" puts $json