Changes done on the Mac.
[cocanwiki.git] / scripts / mail_import.ml
index 8549758..f609062 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.10 2006/03/27 18:09:46 rich Exp $
+ * $Id: mail_import.ml,v 1.11 2006/03/28 13:20:00 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
@@ -96,22 +96,21 @@ let run r (q : cgi) dbh hostid _ user =
                              if Char.code c < 32 then ' ' else c) subject in
 
   (* Parse the date field. *)
-  let date, time =
+  let message_date =
     try
       let date = Netdate.parse date in
-      let date, time =
-       { Dbi.year = date.Netdate.year;
-         Dbi.month = date.Netdate.month;
-         Dbi.day = date.Netdate.day; },
-       { Dbi.hour = date.Netdate.hour;
-         Dbi.min = date.Netdate.minute;
-         Dbi.sec = date.Netdate.second;
-         Dbi.microsec = 0;
-         Dbi.timezone = Some (date.Netdate.zone / 60); } in
-      date, time
+      let cal = Calendar.make
+       date.Netdate.year
+       date.Netdate.month
+       date.Netdate.day
+       date.Netdate.hour
+       date.Netdate.minute
+       date.Netdate.second in
+      let tz = Time_Zone.UTC_Plus (date.Netdate.zone / 60) in
+      cal, tz
     with
-       Invalid_argument _ ->
-         failwith ("cannot parse date: " ^ date) in
+      Invalid_argument _ ->
+       failwith ("cannot parse date: " ^ date) in
 
   (* Find the first thing in the In-Reply-To field which looks like a
    * message ID.
@@ -149,41 +148,38 @@ let run r (q : cgi) dbh hostid _ user =
    * else 'None' if this is a never-seen-before message.
    *)
   let overwrite =
-    let sth = dbh#prepare_cached "select id from messages
-                                   where hostid = ? and inet_message_id = ?" in
-    sth#execute [Some hostid; Some inet_message_id];
-    try
-      let id = sth#fetch1int () in
-      if not overwrite then (
-       ok ~title:"Message exists"
-          dbh hostid q "Message already imported";
-       return ()
-      );
-      Some id
-    with
-       Not_found -> None in
+    let rows = PGSQL(dbh)
+      "select id from messages
+        where hostid = $hostid and inet_message_id = $inet_message_id" in
+    match rows with
+    | [id] ->
+       if not overwrite then (
+         ok ~title:"Message exists"
+            dbh hostid q "Message already imported";
+         return ()
+       );
+       Some id
+    | [] -> None
+    | _ -> assert false in
 
   (* Save all of this in the database. *)
   let msgid =
     match overwrite with
        None ->                         (* Never-seen-before message. *)
-         let sth =
-           dbh#prepare_cached
-             "insert into messages (hostid, subject, inet_message_id,
-                 message_date) values (?, ?, ?, ?)" in
-         sth#execute [Some hostid; Some subject; Some inet_message_id;
-                      `Timestamp (date, time)];
-         let msgid = Int64.to_int (sth#serial "messages_id_seq") in
-
-         let sth =
-           dbh#prepare_cached
-             "insert into msg_references (message_id, inet_message_id,
-                 ordering) values (?, ?, ?)" in
+         PGSQL(dbh)
+           "insert into messages (hostid, subject, inet_message_id,
+               message_date)
+             values ($hostid, $subject, $inet_message_id, $message_date)";
+         let msgid = PGOCaml.serial4 dbh "messages_id_seq" in
+
          let ordering = ref 0 in
          List.iter (fun inet_message_id ->
-                      incr ordering; let ordering = !ordering in
-                      sth#execute [Some msgid; Some inet_message_id;
-                                   Some ordering]) references;
+                      incr ordering; let ordering = Int32.of_int !ordering in
+                      PGSQL(dbh)
+                        "insert into msg_references (message_id,
+                            inet_message_id, ordering)
+                          values ($msgid, $inet_message_id, $ordering)"
+                   ) references;
 
          msgid
 
@@ -276,12 +272,11 @@ let run r (q : cgi) dbh hostid _ user =
       (* Get all the titles from the database! We're going to exclude
        * mail messages from this.
        *)
-      let sth =
-       dbh#prepare_cached
-         "select lower (title) from pages where hostid = ?
+      let links =
+       PGSQL(dbh)
+         "select lower (title) from pages where hostid = $hostid
               and url is not null and title not like 'Mail/%'" in
-      sth#execute [Some hostid];
-      let links = sth#map (function [Some s] -> s | _ -> assert false) in
+      let links = List.map Option.get links in
 
       (* This code cannot find titles which are split across multiple lines.
        * XXX