More thorough inclusion of the page bug. This should mean the page bug appears just...
[cocanwiki.git] / scripts / mail_import.ml
index 0cd7a67..9e24bfb 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: mail_import.ml,v 1.4 2004/10/12 10:00:38 rich Exp $
+ * $Id: mail_import.ml,v 1.9 2005/11/24 14:54:12 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -36,6 +36,7 @@ open Cocanwiki_template
 open Cocanwiki_date
 open Cocanwiki_strings
 open Cocanwiki_pages
+open Cocanwiki_mail
 
 let irt_re = Pcre.regexp "<.*?>"
 let ws_re = Pcre.regexp "\\s+"
@@ -58,7 +59,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
     with
        Not_found ->
          error ~back_button:true ~title:"No message"
-           q "No message was uploaded.";
+           dbh hostid q "No message was uploaded.";
          return () in
 
   (* Parse the message. *)
@@ -85,10 +86,15 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
    *)
   if date = "" || inet_message_id = "" then (
     error ~back_button:true ~title:"Headers missing"
-      q "Date or Message-ID header missing.  Cannot handle this message. ";
+      dbh hostid q
+      "Date or Message-ID header missing.  Cannot handle this message. ";
     return ()
   );
 
+  (* Can't handle funny characters in subject lines - remove them. *)
+  let subject = String.map (fun c ->
+                             if Char.code c < 32 then ' ' else c) subject in
+
   (* Parse the date field. *)
   let date, time =
     try
@@ -150,7 +156,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
       let id = sth#fetch1int () in
       if not overwrite then (
        ok ~title:"Message exists"
-          q "Message already imported";
+          dbh hostid q "Message already imported";
        return ()
       );
       Some id
@@ -167,7 +173,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
                  message_date) values (?, ?, ?, ?)" in
          sth#execute [`Int hostid; `String subject; `String inet_message_id;
                       `Timestamp (date, time)];
-         let msgid = sth#serial "messages_id_seq" in
+         let msgid = Int64.to_int (sth#serial "messages_id_seq") in
 
          let sth =
            dbh#prepare_cached
@@ -187,7 +193,10 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
           *)
          msgid in
 
-  (* The message is referred to by a unique title: *)
+  (* The message is referred to by a unique title.
+   * NB. Do not change this unique title - it is also used during thread
+   * indexing.
+   *)
   let title = sprintf "Mail/%s (%d)" subject msgid in
 
   (* Choose a suitable URL. *)
@@ -290,7 +299,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
               try
                 String.find (String.lowercase str) sub_lc
               with
-                  String.Invalid_string -> -1
+                  Invalid_string -> -1
             in
             let rec loop line = function
                 [] -> line
@@ -312,9 +321,7 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
             loop line links) lines in
 
       let lines = List.map trim lines in
-      let lines =
-       List.map (fun line -> if line <> "" then line ^ " <br>" else "")
-         lines in
+      let lines = List.map (fun line -> line ^ "<br>") lines in
 
       String.concat "\n" lines
     in
@@ -348,12 +355,16 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
    *)
   save_page dbh hostid ~user ~r model;
 
+  (* Rebuild threads? *)
+  if rebuild then
+    thread_mail dbh hostid ~user ~r date.Dbi.year date.Dbi.month;
+
   (* Commit to the database. *)
   dbh#commit ();
 
   (* Finish off. *)
   ok ~title:"Imported"
-    q ("Message " ^ inet_message_id ^ " was imported.")
+    dbh hostid q ("Message " ^ inet_message_id ^ " was imported.")
 
 let () =
   register_script ~restrict:[CanImportMail] run