MIGRATE: By Edward Flick (DTX) properly set the message ID for the meta data record...
[citadel.git] / citadel / modules / migrate / serv_migrate.c
index 7885a10a220eb80b872acdd33c4cc757ca4edb98..39ffc73b9782f664d84d6f1afe5690d227e7814a 100644 (file)
@@ -1,24 +1,15 @@
 /*
- * $Id: serv_migrate.c 7274 2009-03-27 15:11:14Z ajc $
- * Copyright (c) 2000-2009 by the citadel.org development team
- *
  * This module dumps and/or loads the Citadel database in XML format.
  *
- * 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
- * the Free Software Foundation; either version 3, or (at your option)
- * any later version.
+ * Copyright (c) 1987-2012 by the citadel.org team
+ *
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- * 
  */
 
 #include "sysdep.h"
 #include "database.h"
 #include "msgbase.h"
 #include "user_ops.h"
-#include "room_ops.h"
 #include "control.h"
 #include "euidindex.h"
-
-
 #include "ctdl_module.h"
 
-
-
 #define END_OF_MESSAGE "---eom---dbd---"
 
 char migr_tempfilename1[PATH_MAX];
@@ -72,14 +58,10 @@ char migr_tempfilename2[PATH_MAX];
 FILE *migr_global_message_list;
 
 
-
-
 /*
  * Code which implements the export appears in this section
  */
 
-
-
 /*
  * Output a string to the client with these characters escaped:  & < >
  */
@@ -88,8 +70,11 @@ void xml_strout(char *str) {
        char *c = str;
 
        while (*c != 0) {
-               if (*c == '&') {
-                       client_write("&amp;", 5);
+               if (*c == '\"') {
+                       client_write("&quot;", 6);
+               }
+               else if (*c == '\'') {
+                       client_write("&apos;", 6);
                }
                else if (*c == '<') {
                        client_write("&lt;", 4);
@@ -97,6 +82,9 @@ void xml_strout(char *str) {
                else if (*c == '>') {
                        client_write("&gt;", 4);
                }
+               else if (*c == '&') {
+                       client_write("&amp;", 5);
+               }
                else {
                        client_write(c, 1);
                }
@@ -144,7 +132,11 @@ void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
        cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
        cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
        cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
-       client_write("<QRdirname>", 11);        xml_strout(buf->QRdirname);     client_write("</QRdirname>\n", 13);
+       if (buf->QRflags & QR_DIRECTORY) {
+               client_write("<QRdirname>", 11);
+               xml_strout(buf->QRdirname);
+               client_write("</QRdirname>\n", 13);
+       }
        cprintf("<QRinfo>%ld</QRinfo>\n", buf->QRinfo);
        cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
        cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
@@ -158,7 +150,7 @@ void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
 
        /* message list goes inside this tag */
 
-       getroom(&CC->room, buf->QRname);
+       CtdlGetRoom(&CC->room, buf->QRname);
        client_write("<room_messages>", 15);
        client_write("<FRname>", 8);    xml_strout(CC->room.QRname);    client_write("</FRname>\n", 10);
        client_write("<FRmsglist>", 11);
@@ -174,7 +166,7 @@ void migr_export_rooms(void) {
        char cmd[SIZ];
        migr_global_message_list = fopen(migr_tempfilename1, "w");
        if (migr_global_message_list != NULL) {
-               ForEachRoom(migr_export_rooms_backend, NULL);
+               CtdlForEachRoom(migr_export_rooms_backend, NULL);
                fclose(migr_global_message_list);
        }
 
@@ -184,10 +176,10 @@ void migr_export_rooms(void) {
         * this will be handled by exporting the reference count, not by
         * exporting the message multiple times.)
         */
-       snprintf(cmd, sizeof cmd, "sort <%s >%s", migr_tempfilename1, migr_tempfilename2);
-       if (system(cmd) != 0) CtdlLogPrintf(CTDL_ALERT, "Error %d\n", errno);
+       snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
+       if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d\n", errno);
        snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
-       if (system(cmd) != 0) CtdlLogPrintf(CTDL_ALERT, "Error %d\n", errno);
+       if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d\n", errno);
 }
 
 
@@ -198,7 +190,7 @@ void migr_export_floors(void) {
         for (i=0; i < MAXFLOORS; ++i) {
                client_write("<floor>\n", 8);
                cprintf("<f_num>%d</f_num>\n", i);
-                getfloor(&qfbuf, i);
+                CtdlGetFloor(&qfbuf, i);
                buf = &qfbuf;
                cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
                client_write("<f_name>", 8); xml_strout(buf->f_name); client_write("</f_name>\n", 10);
@@ -210,21 +202,38 @@ void migr_export_floors(void) {
 }
 
 
+/*
+ * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
+ */
+int is_sequence_set(char *s) {
+       if (!s) return(0);
+
+       char *c = s;
+       char ch;
+       while (ch = *c++, ch) {
+               if (!strchr("0123456789*,:", ch)) {
+                       return(0);
+               }
+       }
+       return(1);
+}
+
+
 
 /* 
  *  Traverse the visits file...
  */
 void migr_export_visits(void) {
-       struct visit vbuf;
+       visit vbuf;
        struct cdbdata *cdbv;
 
        cdb_rewind(CDB_VISIT);
 
        while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
-               memset(&vbuf, 0, sizeof(struct visit));
+               memset(&vbuf, 0, sizeof(visit));
                memcpy(&vbuf, cdbv->ptr,
-                      ((cdbv->len > sizeof(struct visit)) ?
-                       sizeof(struct visit) : cdbv->len));
+                      ((cdbv->len > sizeof(visit)) ?
+                       sizeof(visit) : cdbv->len));
                cdb_free(cdbv);
 
                client_write("<visit>\n", 8);
@@ -233,7 +242,7 @@ void migr_export_visits(void) {
                cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
 
                client_write("<v_seen>", 8);
-               if (!IsEmptyStr(vbuf.v_seen)) {
+               if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
                        xml_strout(vbuf.v_seen);
                }
                else {
@@ -241,7 +250,12 @@ void migr_export_visits(void) {
                }
                client_write("</v_seen>", 9);
 
-               client_write("<v_answered>", 12); xml_strout(vbuf.v_answered); client_write("</v_answered>\n", 14);
+               if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
+                       client_write("<v_answered>", 12);
+                       xml_strout(vbuf.v_answered);
+                       client_write("</v_answered>\n", 14);
+               }
+
                cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
                cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
                client_write("</visit>\n", 9);
@@ -254,6 +268,25 @@ void migr_export_message(long msgnum) {
        struct CtdlMessage *msg;
        struct ser_ret smr;
 
+       /* We can use a static buffer here because there will never be more than
+        * one of this operation happening at any given time, and it's really best
+        * to just keep it allocated once instead of torturing malloc/free.
+        * Call this function with msgnum "-1" to free the buffer when finished.
+        */
+       static int encoded_alloc = 0;
+       static char *encoded_msg = NULL;
+
+       if (msgnum < 0) {
+               if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
+                       free(encoded_msg);
+                       encoded_alloc = 0;
+                       encoded_msg = NULL;
+               }
+               return;
+       }
+
+       /* Ok, here we go ... */
+
        msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;        /* fail silently */
 
@@ -264,17 +297,24 @@ void migr_export_message(long msgnum) {
        client_write("<msg_meta_content_type>", 23); xml_strout(smi.meta_content_type); client_write("</msg_meta_content_type>\n", 25);
 
        client_write("<msg_text>", 10);
-       serialize_message(&smr, msg);
-       CtdlFreeMessage(msg);
-
-       int encoded_len = 0;
-       int encoded_alloc = smr.len * 139 / 100;        /* well-tested formula for predicting encoded size */
-       char *encoded_msg = malloc(encoded_alloc);
+       CtdlSerializeMessage(&smr, msg);
+       CM_Free(msg);
+
+       /* Predict the buffer size we need.  Expand the buffer if necessary. */
+       int encoded_len = smr.len * 15 / 10 ;
+       if (encoded_len > encoded_alloc) {
+               encoded_alloc = encoded_len;
+               encoded_msg = realloc(encoded_msg, encoded_alloc);
+       }
 
-       if (encoded_msg != NULL) {
+       if (encoded_msg == NULL) {
+               /* Questionable hack that hopes it'll work next time and we only lose one message */
+               encoded_alloc = 0;
+       }
+       else {
+               /* Once we do the encoding we know the exact size */
                encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
                client_write(encoded_msg, encoded_len);
-               free(encoded_msg);
        }
 
        free(smr.ser);
@@ -313,13 +353,13 @@ void migr_export_messages(void) {
        char buf[SIZ];
        long msgnum;
        int count = 0;
-       t_context *Ctx;
+       CitContext *Ctx;
 
        Ctx = CC;
        migr_global_message_list = fopen(migr_tempfilename1, "r");
        if (migr_global_message_list != NULL) {
-               CtdlLogPrintf(CTDL_INFO, "Opened %s\n", migr_tempfilename1);
-               while ((Ctx->kill_me != 1) && 
+               syslog(LOG_INFO, "Opened %s\n", migr_tempfilename1);
+               while ((Ctx->kill_me == 0) && 
                       (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
                        msgnum = atol(buf);
                        if (msgnum > 0L) {
@@ -329,18 +369,18 @@ void migr_export_messages(void) {
                }
                fclose(migr_global_message_list);
        }
-       if (Ctx->kill_me != 1)
-               CtdlLogPrintf(CTDL_INFO, "Exported %d messages.\n", count);
+       if (Ctx->kill_me == 0)
+               syslog(LOG_INFO, "Exported %d messages.\n", count);
        else
-               CtdlLogPrintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
+               syslog(LOG_ERR, "Export aborted due to client disconnect! \n");
+
+       migr_export_message(-1L);       /* This frees the encoding buffer */
 }
 
 
 
 void migr_do_export(void) {
-       struct config *buf;
-       buf = &config;
-       t_context *Ctx;
+       CitContext *Ctx;
 
        Ctx = CC;
        cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
@@ -367,7 +407,6 @@ void migr_do_export(void) {
        cprintf("<c_restrict>%d</c_restrict>\n", config.c_restrict);
        client_write("<c_site_location>", 17);  xml_strout(config.c_site_location);     client_write("</c_site_location>\n", 19);
        client_write("<c_sysadm>", 10);         xml_strout(config.c_sysadm);            client_write("</c_sysadm>\n", 12);
-       cprintf("<c_setup_level>%d</c_setup_level>\n", config.c_setup_level);
        cprintf("<c_maxsessions>%d</c_maxsessions>\n", config.c_maxsessions);
        client_write("<c_ip_addr>", 11);        xml_strout(config.c_ip_addr);           client_write("</c_ip_addr>\n", 13);
        cprintf("<c_port_number>%d</c_port_number>\n", config.c_port_number);
@@ -403,7 +442,6 @@ void migr_do_export(void) {
        cprintf("<c_pop3s_port>%d</c_pop3s_port>\n", config.c_pop3s_port);
        cprintf("<c_smtps_port>%d</c_smtps_port>\n", config.c_smtps_port);
        cprintf("<c_auto_cull>%d</c_auto_cull>\n", config.c_auto_cull);
-       cprintf("<c_instant_expunge>%d</c_instant_expunge>\n", config.c_instant_expunge);
        cprintf("<c_allow_spoofing>%d</c_allow_spoofing>\n", config.c_allow_spoofing);
        cprintf("<c_journal_email>%d</c_journal_email>\n", config.c_journal_email);
        cprintf("<c_journal_pubmsgs>%d</c_journal_pubmsgs>\n", config.c_journal_pubmsgs);
@@ -438,12 +476,12 @@ void migr_do_export(void) {
        cprintf("<control_version>%d</control_version>\n", CitControl.version);
        client_write("</control>\n", 11);
 
-       if (Ctx->kill_me != 1)  migr_export_users();
-       if (Ctx->kill_me != 1)  migr_export_openids();
-       if (Ctx->kill_me != 1)  migr_export_rooms();
-       if (Ctx->kill_me != 1)  migr_export_floors();
-       if (Ctx->kill_me != 1)  migr_export_visits();
-       if (Ctx->kill_me != 1)  migr_export_messages();
+       if (Ctx->kill_me == 0)  migr_export_users();
+       if (Ctx->kill_me == 0)  migr_export_openids();
+       if (Ctx->kill_me == 0)  migr_export_rooms();
+       if (Ctx->kill_me == 0)  migr_export_floors();
+       if (Ctx->kill_me == 0)  migr_export_visits();
+       if (Ctx->kill_me == 0)  migr_export_messages();
        client_write("</citadel_migrate_data>\n", 24);
        client_write("000\n", 4);
        Ctx->dont_term = 0;
@@ -471,7 +509,7 @@ long openid_usernum = 0;
 char FRname[ROOMNAMELEN];
 struct floor flbuf;
 int floornum = 0;
-struct visit vbuf;
+visit vbuf;
 struct MetaData smi;
 long import_msgnum = 0;
 char *decoded_msg = NULL;
@@ -486,6 +524,7 @@ void migr_xml_chardata(void *data, const XML_Char *s, int len) {
 
        old_len = migr_chardata_len;
        new_len = old_len + len;
+
        new_buffer = realloc(migr_chardata, new_len + 1);
        if (new_buffer != NULL) {
                memcpy(&new_buffer[old_len], s, len);
@@ -523,7 +562,7 @@ void migr_xml_start(void *data, const char *el, const char **attr) {
        }
 
        if (citadel_migrate_data != 1) {
-               CtdlLogPrintf(CTDL_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!\n");
+               syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!\n", el);
                return;
        }
 
@@ -535,7 +574,7 @@ void migr_xml_start(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "room"))               memset(&qrbuf, 0, sizeof (struct ctdlroom));
        else if (!strcasecmp(el, "room_messages"))      memset(FRname, 0, sizeof FRname);
        else if (!strcasecmp(el, "floor"))              memset(&flbuf, 0, sizeof (struct floor));
-       else if (!strcasecmp(el, "visit"))              memset(&vbuf, 0, sizeof (struct visit));
+       else if (!strcasecmp(el, "visit"))              memset(&vbuf, 0, sizeof (visit));
 
        else if (!strcasecmp(el, "message")) {
                memset(&smi, 0, sizeof (struct MetaData));
@@ -549,7 +588,7 @@ void migr_xml_start(void *data, const char *el, const char **attr) {
 }
 
 
-void migr_xml_end(void *data, const char *el, const char **attr) {
+void migr_xml_end(void *data, const char *el) {
        char *ptr;
        int msgcount = 0;
        long msgnum = 0L;
@@ -568,7 +607,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        }
 
        if (citadel_migrate_data != 1) {
-               CtdlLogPrintf(CTDL_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!\n");
+               syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!\n", el);
                return;
        }
 
@@ -577,14 +616,14 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
                migr_chardata_len = 0;
        }
 
-       // CtdlLogPrintf(CTDL_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
+       // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
 
        /*** CONFIG ***/
 
        if (!strcasecmp(el, "config")) {
                config.c_enable_fulltext = 0;   /* always disable */
                put_config();
-               CtdlLogPrintf(CTDL_INFO, "Completed import of server configuration\n");
+               syslog(LOG_INFO, "Completed import of server configuration\n");
        }
 
        else if (!strcasecmp(el, "c_nodename"))                 safestrncpy(config.c_nodename, migr_chardata, sizeof config.c_nodename);
@@ -602,7 +641,6 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "c_restrict"))                 config.c_restrict = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_site_location"))            safestrncpy(config.c_site_location, migr_chardata, sizeof config.c_site_location);
        else if (!strcasecmp(el, "c_sysadm"))                   safestrncpy(config.c_sysadm, migr_chardata, sizeof config.c_sysadm);
-       else if (!strcasecmp(el, "c_setup_level"))              config.c_setup_level = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_maxsessions"))              config.c_maxsessions = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_ip_addr"))                  safestrncpy(config.c_ip_addr, migr_chardata, sizeof config.c_ip_addr);
        else if (!strcasecmp(el, "c_port_number"))              config.c_port_number = atoi(migr_chardata);
@@ -638,7 +676,6 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "c_pop3s_port"))               config.c_pop3s_port = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_smtps_port"))               config.c_smtps_port = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_auto_cull"))                config.c_auto_cull = atoi(migr_chardata);
-       else if (!strcasecmp(el, "c_instant_expunge"))          config.c_instant_expunge = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_allow_spoofing"))           config.c_allow_spoofing = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_journal_email"))            config.c_journal_email = atoi(migr_chardata);
        else if (!strcasecmp(el, "c_journal_pubmsgs"))          config.c_journal_pubmsgs = atoi(migr_chardata);
@@ -673,7 +710,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "control")) {
                CitControl.MMfulltext = (-1L);  /* always flush */
                put_control();
-               CtdlLogPrintf(CTDL_INFO, "Completed import of control record\n");
+               syslog(LOG_INFO, "Completed import of control record\n");
        }
 
        /*** USER ***/
@@ -691,8 +728,8 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "u_fullname"))                 safestrncpy(usbuf.fullname, migr_chardata, sizeof usbuf.fullname);
 
        else if (!strcasecmp(el, "user")) {
-               putuser(&usbuf);
-               CtdlLogPrintf(CTDL_INFO, "Imported user: %s\n", usbuf.fullname);
+               CtdlPutUser(&usbuf);
+               syslog(LOG_INFO, "Imported user: %s\n", usbuf.fullname);
        }
 
        /*** OPENID ***/
@@ -709,7 +746,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
                memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
                cdb_store(CDB_OPENID, openid_url, strlen(openid_url), oid_data, oid_data_len);
                free(oid_data);
-               CtdlLogPrintf(CTDL_INFO, "Imported OpenID: %s (%ld)\n", openid_url, openid_usernum);
+               syslog(LOG_INFO, "Imported OpenID: %s (%ld)\n", openid_url, openid_usernum);
        }
 
        /*** ROOM ***/
@@ -732,8 +769,8 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "QRdefaultview"))              qrbuf.QRdefaultview = atoi(migr_chardata);
 
        else if (!strcasecmp(el, "room")) {
-               putroom(&qrbuf);
-               CtdlLogPrintf(CTDL_INFO, "Imported room: %s\n", qrbuf.QRname);
+               CtdlPutRoom(&qrbuf);
+               syslog(LOG_INFO, "Imported room: %s\n", qrbuf.QRname);
        }
 
        /*** ROOM MESSAGE POINTERS ***/
@@ -746,7 +783,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
                        msglist_alloc = 1000;
                        msglist = malloc(sizeof(long) * msglist_alloc);
 
-                       CtdlLogPrintf(CTDL_DEBUG, "Message list for: %s\n", FRname);
+                       syslog(LOG_DEBUG, "Message list for: %s\n", FRname);
 
                        ptr = migr_chardata;
                        while (*ptr != 0) {
@@ -769,13 +806,13 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
                                }
                        }
                        if (msgcount > 0) {
-                               CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL);
+                               CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
                        }
                        free(msglist);
                        msglist = NULL;
                        msglist_alloc = 0;
-                       CtdlLogPrintf(CTDL_DEBUG, "Imported %d messages.\n", msgcount);
-                       if (CtdlThreadCheckStop()) {
+                       syslog(LOG_DEBUG, "Imported %d messages.\n", msgcount);
+                       if (server_shutting_down) {
                                return;
                }
        }
@@ -790,8 +827,8 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
        else if (!strcasecmp(el, "f_ep_expire_value"))          flbuf.f_ep.expire_value = atoi(migr_chardata);
 
        else if (!strcasecmp(el, "floor")) {
-               putfloor(&flbuf, floornum);
-               CtdlLogPrintf(CTDL_INFO, "Imported floor #%d (%s)\n", floornum, flbuf.f_name);
+               CtdlPutFloor(&flbuf, floornum);
+               syslog(LOG_INFO, "Imported floor #%d (%s)\n", floornum, flbuf.f_name);
        }
 
        /*** VISITS ***/
@@ -813,7 +850,7 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
 
        else if (!strcasecmp(el, "visit")) {
                put_visit(&vbuf);
-               CtdlLogPrintf(CTDL_INFO, "Imported visit: %ld/%ld/%ld\n", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
+               syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld\n", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
        }
 
        /*** MESSAGES ***/
@@ -832,8 +869,9 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
                cdb_store(CDB_MSGMAIN, &import_msgnum, sizeof(long), decoded_msg, msglen);
                free(decoded_msg);
                decoded_msg = NULL;
+               smi.meta_msgnum = import_msgnum;
                PutMetaData(&smi);
-               CtdlLogPrintf(CTDL_INFO, "Imported message #%ld, size=%ld, refcount=%d, content-type: %s\n",
+               syslog(LOG_INFO, "Imported message #%ld, size=%ld, refcount=%d, content-type: %s\n",
                        import_msgnum, msglen, smi.meta_refcount, smi.meta_content_type);
        }
 
@@ -853,12 +891,12 @@ void migr_xml_end(void *data, const char *el, const char **attr) {
  * Import begins here
  */
 void migr_do_import(void) {
-       char buf[SIZ];
+       StrBuf *Buf;
        XML_Parser xp;
-       int linelen;
+       int Finished = 0;
        
        unbuffer_output();
-
+       Buf = NewStrBufPlain(NULL, SIZ);
        xp = XML_ParserCreate(NULL);
        if (!xp) {
                cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
@@ -872,22 +910,28 @@ void migr_do_import(void) {
        cprintf("%d sock it to me\n", SEND_LISTING);
        unbuffer_output();
 
-       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
-               linelen = strlen(buf);
-               strcpy(&buf[linelen++], "\n");
+       client_set_inbound_buf(SIZ * 10);
 
-               if (CtdlThreadCheckStop())
+       while (!Finished && client_read_random_blob(Buf, -1) >= 0) {
+               if ((StrLength(Buf) > 4) &&
+                   !strcmp(ChrPtr(Buf) + StrLength(Buf) - 4, "000\n"))
+               {
+                       Finished = 1;
+                       StrBufCutAt(Buf, StrLength(Buf) - 4, NULL);
+               }
+               if (server_shutting_down)
                        break;  // Should we break or return?
                
-               if (buf[0] == '\0')
+               if (StrLength(Buf) == 0)
                        continue;
 
-               XML_Parse(xp, buf, linelen, 0);
+               XML_Parse(xp, ChrPtr(Buf), StrLength(Buf), 0);
+               FlushStrBuf(Buf);
        }
 
        XML_Parse(xp, "", 0, 1);
        XML_ParserFree(xp);
-       
+       FreeStrBuf(&Buf);
        rebuild_euid_index();
        rebuild_usersbynumber();
        CC->dont_term = 0;
@@ -895,6 +939,23 @@ void migr_do_import(void) {
 
 
 
+/*
+ * Dump out the pathnames of directories which can be copied "as is"
+ */
+void migr_do_listdirs(void) {
+       cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
+       cprintf("bio|%s\n",             ctdl_bio_dir);
+       cprintf("files|%s\n",           ctdl_file_dir);
+       cprintf("userpics|%s\n",        ctdl_usrpic_dir);
+       cprintf("messages|%s\n",        ctdl_message_dir);
+       cprintf("netconfigs|%s\n",      ctdl_netcfg_dir);
+       cprintf("keys|%s\n",            ctdl_key_dir);
+       cprintf("images|%s\n",          ctdl_image_dir);
+       cprintf("info|%s\n",            ctdl_info_dir);
+       cprintf("000\n");
+}
+
+
 /*
  * Common code appears in this section
  */
@@ -917,6 +978,9 @@ void cmd_migr(char *cmdbuf) {
                else if (!strcasecmp(cmd, "import")) {
                        migr_do_import();
                }
+               else if (!strcasecmp(cmd, "listdirs")) {
+                       migr_do_listdirs();
+               }
                else {
                        cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
                }
@@ -938,9 +1002,9 @@ CTDL_MODULE_INIT(migrate)
        if (!threading)
        {
                CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
-               CtdlRegisterProtoHook(cmd_migr, "ARTV", "Across-the-wire migration (legacy calling syntax)"
+               CtdlRegisterProtoHook(cmd_migr, "ARTV", "Across-the-wire migration (legacy syntax)");
        }
        
-       /* return our Subversion id for the Log */
-       return "$Id: serv_migrate.c 7274 2009-03-27 15:11:14Z ajc $";
+       /* return our module name for the log */
+       return "migrate";
 }