From: rich Date: Tue, 12 Oct 2004 10:00:36 +0000 (+0000) Subject: Command-line tools for importing mail (uses 'curl'). X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=eb57cb1d3f0af6474edcca4fbe5a5be2b6288b6d;p=cocanwiki.git Command-line tools for importing mail (uses 'curl'). Fixed the ok/error page - multiple HTML problems. Error page now sets HTTP status code to 400, allowing us to detect errors programmatically. --- diff --git a/MANIFEST b/MANIFEST index e72c258..b53bcdf 100644 --- 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 diff --git a/debian/control b/debian/control index add797a..7556950 100644 --- a/debian/control +++ b/debian/control @@ -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) diff --git a/scripts/cocanwiki_ok.ml b/scripts/cocanwiki_ok.ml index 08cd71a..6067197 100644 --- a/scripts/cocanwiki_ok.ml +++ b/scripts/cocanwiki_ok.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * 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 diff --git a/scripts/mail_import.ml b/scripts/mail_import.ml index 19b1073..0cd7a67 100644 --- a/scripts/mail_import.ml +++ b/scripts/mail_import.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * 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 () diff --git a/templates/ok_error.html b/templates/ok_error.html index 8fa7653..fd53db5 100644 --- a/templates/ok_error.html +++ b/templates/ok_error.html @@ -11,13 +11,10 @@

::title_html::

::end:: -
-
- ::if(has_icon):: ::icon_alt_html_tag:: + alt="::icon_alt_html_tag::" src="::icon_html_tag::" + width="32" height="32"/> ::end::

::message_html::


@@ -37,9 +34,7 @@ ::end:: ::end:: - -
-
+ diff --git a/tools/import_mail.sh b/tools/import_mail.sh new file mode 100755 index 0000000..777eeff --- /dev/null +++ b/tools/import_mail.sh @@ -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 index 0000000..6578722 --- /dev/null +++ b/tools/import_mbox.sh @@ -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