Command-line tools for importing mail (uses 'curl').
authorrich <rich>
Tue, 12 Oct 2004 10:00:36 +0000 (10:00 +0000)
committerrich <rich>
Tue, 12 Oct 2004 10:00:36 +0000 (10:00 +0000)
Fixed the ok/error page - multiple HTML problems.
Error page now sets HTTP status code to 400, allowing us to detect
errors programmatically.

MANIFEST
debian/control
scripts/cocanwiki_ok.ml
scripts/mail_import.ml
templates/ok_error.html
tools/import_mail.sh [new file with mode: 0755]
tools/import_mbox.sh [new file with mode: 0755]

index e72c258..b53bcdf 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -236,4 +236,6 @@ templates/upload_image_form.html
 templates/users.html
 templates/visualise_links.html
 templates/what_links_here.html
-tools/delete_mail.ml
\ No newline at end of file
+tools/delete_mail.ml
+tools/import_mail.sh
+tools/import_mbox.sh
\ No newline at end of file
index add797a..7556950 100644 (file)
@@ -8,7 +8,9 @@ Package: cocanwiki
 Section: web
 Architecture: all
 Depends: libpgsql-ocaml, libdbi-ocaml (>= 0.9.9), libpcre-ocaml,
- ocaml-base-nox-3.08, libapache-mod-caml, libtemplate-ocaml-dev, imagemagick
+ ocaml-base-nox-3.08, libapache-mod-caml, libtemplate-ocaml-dev,
+ libocamlnet-ocaml-dev (>= 0.98),
+ imagemagick, curl (>= 7.12.1)
 Suggests: apache
 Recommends: graphviz
 Description: A Wiki written in Objective CAML (OCaml)
index 08cd71a..6067197 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: cocanwiki_ok.ml,v 1.5 2004/09/09 12:21:22 rich Exp $
+ * $Id: cocanwiki_ok.ml,v 1.6 2004/10/12 10:00:38 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
@@ -33,6 +33,8 @@ let ok_error_template = _get_template "ok_error.html"
 
 let error ?cookie ?cookies ?title ?(icon = "/_graphics/error.png")
     ?icon_alt ?back_button ?close_button q message =
+  (* Set the status so scripts can determine if the request failed. *)
+  Request.set_status q#request cHTTP_BAD_REQUEST;
   StdPages.error ?cookie ?cookies ~template:ok_error_template
     ?title ~icon ?icon_alt ?back_button ?close_button q message
 
index 19b1073..0cd7a67 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.3 2004/10/11 16:07:25 rich Exp $
+ * $Id: mail_import.ml,v 1.4 2004/10/12 10:00:38 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
@@ -57,8 +57,6 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
       upload.upload_value
     with
        Not_found ->
-         (* Force an error status which a script can detect. *)
-         Request.set_status r cHTTP_BAD_REQUEST;
          error ~back_button:true ~title:"No message"
            q "No message was uploaded.";
          return () in
@@ -86,7 +84,6 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
    * cannot thread it, so give up.
    *)
   if date = "" || inet_message_id = "" then (
-    Request.set_status r cHTTP_BAD_REQUEST;
     error ~back_button:true ~title:"Headers missing"
       q "Date or Message-ID header missing.  Cannot handle this message. ";
     return ()
index 8fa7653..fd53db5 100644 (file)
 <h1>::title_html::</h1>
 ::end::
 
-<div id="mid">
-<div id="cnt">
-
 ::if(has_icon)::
 <img style="float: left; clear:both; margin:10px;"
-     alt="::icon_alt_html_tag::"
-     src="::icon_html_tag::">
+     alt="::icon_alt_html_tag::" src="::icon_html_tag::"
+     width="32" height="32"/>
 ::end::
 <p>::message_html::</p>
 <hr>
@@ -37,9 +34,7 @@
 ::end::
 <input type="submit" value="::name_html_tag::"></form></td>
 ::end::
-
-</div>
-</div>
+</tr></table>
 
 </body>
 </html>
diff --git a/tools/import_mail.sh b/tools/import_mail.sh
new file mode 100755 (executable)
index 0000000..777eeff
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/sh -
+
+set -e
+
+# Check servername, username, password are supplied.
+if [ $# -lt 3 ]; then
+    echo "Usage: $0 servername username password"
+    echo "eg: $0 wiki.example.com fred secret"
+    exit 1
+fi
+servername=$1
+username="$2"
+password="$3"
+shift 3
+
+# Cookie jar.
+cj=/tmp/$$.cookies
+
+curl="curl -c $cj -o /dev/null -s -w %{http_code}"
+
+# Login to the remote server.  The side-effect of this is just to put
+# a cookie in the cookie jar.
+rv=`$curl -F username="$username" -F password="$password" http://$servername/_bin/login.cmo`
+if [ "$rv" != 200 ]; then
+    echo "Login failed. Username or password incorrect."
+    exit 1
+fi
+
+curl="curl -b $cj -c $cj -o /dev/null -s -w %{http_code}"
+
+# Import the mail messages.
+for m in "$@"; do
+    rv=`$curl -F file="<$m" http://$servername/_bin/mail_import.cmo`
+    if [ "$rv" != 200 ]; then
+       echo "Warning: Failed to import message $m"
+    fi
+done
+
+# Remove any temporary files.
+rm -f $cj
diff --git a/tools/import_mbox.sh b/tools/import_mbox.sh
new file mode 100755 (executable)
index 0000000..6578722
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/sh -
+
+# Usage: import_mbox.sh servername username password < mbox
+
+set -e
+
+d=/tmp/$$.mail
+mkdir --mode=0700 $d
+
+# Read the mbox from stdin and split it into separate files in the directory.
+formail -s sh -c "tail +2 > $d/\$FILENO"
+
+# Now run import_mail.sh on groups of files.
+ls -1 $d | sed -e "s|^|$d/|" | xargs ./import_mail.sh "$@"
+
+# Remove the mail directory.
+rm -rf $d