Mailman shortcut working
[hcoop/domtool2.git] / src / plugins / apache.sml
index be0e4f2..f36bfa4 100644 (file)
@@ -22,12 +22,14 @@ structure Apache :> APACHE = struct
 
 open Ast
 
+fun webNode node =
+    List.exists (fn (x, _) => x = node) Config.Apache.webNodes_all
+    orelse (Domain.hasPriv "www"
+           andalso List.exists (fn (x, _) => x = node) Config.Apache.webNodes_admin)
+
 val _ = Env.type_one "web_node"
        Env.string
-       (fn node =>
-           List.exists (fn (x, _) => x = node) Config.Apache.webNodes_all
-           orelse (Domain.hasPriv "www"
-                   andalso List.exists (fn (x, _) => x = node) Config.Apache.webNodes_admin))
+       webNode
 
 val _ = Env.registerFunction ("web_node_to_node",
                              fn [e] => SOME e
@@ -236,6 +238,7 @@ fun isWaklog node =
 fun down () = if imVersion1 () then Config.Apache.down1 else Config.Apache.down
 fun undown () = if imVersion1 () then Config.Apache.undown1 else Config.Apache.undown
 fun reload () = if imVersion1 () then Config.Apache.reload1 else Config.Apache.reload
+fun fixperms () = if imVersion1 () then Config.Apache.fixperms1 else Config.Apache.fixperms
 
 fun logDir {user, node, vhostId} =
     String.concat [Config.Apache.logDirOf (isVersion1 node) user,
@@ -255,10 +258,12 @@ val () = Slave.registerFileHandler (fn fs =>
                                                                            file = #file spl}
 
                                                   val user = findVhostUser (#file fs)
-                                                  val oldUser = findVhostUser realVhostFile
+                                                  val oldUser = case #action fs of
+                                                                    Slave.Delete false => user
+                                                                  | _ => findVhostUser realVhostFile
                                               in
                                                   if (oldUser = NONE andalso #action fs <> Slave.Add)
-                                                     orelse (user = NONE andalso #action fs <> Slave.Delete) then
+                                                     orelse (user = NONE andalso not (Slave.isDelete (#action fs))) then
                                                       print ("Can't find user in " ^ #file fs ^ " or " ^ realVhostFile ^ "!  Taking no action.\n")
                                                   else
                                                       let
@@ -271,21 +276,31 @@ val () = Slave.registerFileHandler (fn fs =>
                                                               logDir {user = valOf user,
                                                                       node = Slave.hostname (),
                                                                       vhostId = vhostId}
+
+                                                          fun backupLogs () =
+                                                              OS.Path.joinDirFile
+                                                                  {dir = Config.Apache.backupLogDirOf
+                                                                             (isVersion1 (Slave.hostname ())),
+                                                                   file = vhostId}
                                                       in
                                                           vhostsChanged := true;
                                                           case #action fs of
-                                                              Slave.Delete =>
-                                                              (if !logDeleted then
-                                                                   ()
-                                                               else
-                                                                   (ignore (OS.Process.system (down ()));
-                                                                    logDeleted := true);
-                                                               ignore (OS.Process.system (Config.rm
-                                                                                          ^ " -rf "
-                                                                                          ^ realVhostFile));
-                                                               ignore (OS.Process.system (Config.rm
-                                                                                          ^ " -rf "
-                                                                                          ^ realLogDir oldUser)))
+                                                              Slave.Delete _ =>
+                                                              let
+                                                                  val ldir = realLogDir oldUser
+                                                              in
+                                                                  if !logDeleted then
+                                                                      ()
+                                                                  else
+                                                                      (ignore (OS.Process.system (down ()));
+                                                                       ignore (OS.Process.system (fixperms ()));
+                                                                       logDeleted := true);
+                                                                  ignore (OS.Process.system (Config.rm
+                                                                                             ^ " -rf "
+                                                                                             ^ realVhostFile));
+                                                                  Slave.moveDirCreate {from = ldir,
+                                                                                       to = backupLogs ()}
+                                                              end
                                                             | Slave.Add =>
                                                               let
                                                                   val rld = realLogDir user
@@ -298,7 +313,8 @@ val () = Slave.registerFileHandler (fn fs =>
                                                                   if Posix.FileSys.access (rld, []) then
                                                                       ()
                                                                   else
-                                                                      Slave.mkDirAll rld
+                                                                      Slave.moveDirCreate {from = backupLogs (),
+                                                                                           to = rld}
                                                               end
                                                               
                                                             | _ =>
@@ -367,6 +383,9 @@ fun registerPost f =
        post := (fn () => (old (); f ()))
     end
 
+fun doPre x = !pre x
+fun doPost () = !post ()
+
 val aliaser = ref (fn _ : string => ())
 fun registerAliaser f =
     let