]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/vandelay/serv_vandelay.c
Added code to provide a preliminary single user mode.
[citadel.git] / citadel / modules / vandelay / serv_vandelay.c
index 42878cceaacc7846bb4d82eeb6baf680839d5e94..79533aa2f2e9160f0d4bbef2c64e9d63dc8c9ec3 100644 (file)
@@ -56,7 +56,7 @@ char artv_tempfilename2[PATH_MAX];
 FILE *artv_global_message_list;
 
 void artv_export_users_backend(struct ctdluser *buf, void *data) {
-       cprintf("user\n");
+       client_write("user\n", 5);
 /*
 #include "artv_serialize.h"
 #include "dtds/user-defs.h"
@@ -78,7 +78,7 @@ void artv_export_users_backend(struct ctdluser *buf, void *data) {
 }
 
 void artv_dump_users_backend(struct ctdluser *buf, void *data) {
-       cprintf("user\n");
+       client_write("user\n", 5);
 
 #include "artv_dump.h"
 #include "dtds/user-defs.h"
@@ -87,6 +87,19 @@ void artv_dump_users_backend(struct ctdluser *buf, void *data) {
 }
 
 
+INLINE int cprintdot (long *iterations)
+{
+       int retval = 0;
+       
+       retval += client_write(".", 1);
+       ++(*iterations);
+       if ((*iterations) % 64 == 0)
+               retval += client_write("\n", 1);
+       return retval;
+}
+
+
+
 void artv_export_users(void) {
        ForEachUser(artv_export_users_backend, NULL);
 }
@@ -109,7 +122,7 @@ void artv_dump_room_msg(long msgnum, void *userdata) {
 
 
 void artv_export_rooms_backend(struct ctdlroom *buf, void *data) {
-       cprintf("room\n");
+       client_write("room\n", 5);
 /*
 #include "artv_serialize.h"
 #include "dtds/room-defs.h"
@@ -148,7 +161,7 @@ void artv_export_rooms_backend(struct ctdlroom *buf, void *data) {
 }
 
 void artv_dump_rooms_backend(struct ctdlroom *buf, void *data) {
-       cprintf("room\n");
+       client_write("room\n", 5);
 
 #include "artv_dump.h"
 #include "dtds/room-defs.h"
@@ -217,7 +230,7 @@ void artv_export_floors(void) {
         int i;
 
         for (i=0; i < MAXFLOORS; ++i) {
-               cprintf("floor\n");
+               client_write("floor\n", 6);
                cprintf("%d\n", i);
                 getfloor(&qfbuf, i);
                buf = &qfbuf;
@@ -240,7 +253,7 @@ void artv_dump_floors(void) {
         int i;
 
         for (i=0; i < MAXFLOORS; ++i) {
-               cprintf("floor\n");
+               client_write("floor\n", 6);
                cprintf("%d\n", i);
                 getfloor(&qfbuf, i);
                buf = &qfbuf;
@@ -271,7 +284,7 @@ void artv_export_visits(void) {
                        sizeof(struct visit) : cdbv->len));
                cdb_free(cdbv);
 
-               cprintf("visit\n");
+               client_write("visit\n", 6);
                cprintf("%ld\n", vbuf.v_roomnum);
                cprintf("%ld\n", vbuf.v_roomgen);
                cprintf("%ld\n", vbuf.v_usernum);
@@ -305,7 +318,7 @@ void artv_dump_visits(void) {
                        sizeof(struct visit) : cdbv->len));
                cdb_free(cdbv);
 
-               cprintf("---visit---\n");
+               client_write("---visit---\n", 12);
                cprintf(" Room-Num: %ld\n", vbuf.v_roomnum);
                cprintf(" Room-Gen%ld\n", vbuf.v_roomgen);
                cprintf(" User-Num%ld\n", vbuf.v_usernum);
@@ -335,7 +348,7 @@ void artv_export_message(long msgnum) {
        msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;        /* fail silently */
 
-       cprintf("message\n");
+       client_write("message\n", 8);
        GetMetaData(&smi, msgnum);
        cprintf("%ld\n", msgnum);
        cprintf("%d\n", smi.meta_refcount);
@@ -372,7 +385,7 @@ void artv_dump_message(long msgnum) {
        msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;        /* fail silently */
 
-       cprintf("message\n");
+       client_write("message\n", 8);
        GetMetaData(&smi, msgnum);
        cprintf(" MessageNum: %ld\n", msgnum);
        cprintf(" MetaRefcount: %d\n", smi.meta_refcount);
@@ -386,6 +399,25 @@ void artv_dump_message(long msgnum) {
 
 
 
+void artv_export_openids(void) {
+       struct cdbdata *cdboi;
+       long usernum;
+
+       cdb_rewind(CDB_OPENID);
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       client_write("openid\n", 7);
+                       memcpy(&usernum, cdboi->ptr, sizeof(long));
+                       cprintf("%s\n", (cdboi->ptr)+sizeof(long) );
+                       cprintf("%ld\n", usernum);
+               }
+               cdb_free(cdboi);
+       }
+}
+
+
+
+
 void artv_export_messages(void) {
        char buf[SIZ];
        long msgnum;
@@ -448,20 +480,20 @@ void artv_do_export(void) {
 
        Ctx = CC;
        cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
-
+       Ctx->dont_term = 1;
        cprintf("version\n%d\n", REV_LEVEL);
 
        /* export the config file (this is done using x-macros) */
-       cprintf("config\n");
+       client_write("config\n", 7);
 
 #include "artv_serialize.h"
 #include "dtds/config-defs.h"
 #include "undef_data.h"
-       cprintf("\n");
+       client_write("\n", 1);
        
        /* Export the control file */
        get_control();
-       cprintf("control\n");
+       client_write("control\n", 8);
        cprintf("%ld\n", CitControl.MMhighest);
        cprintf("%u\n", CitControl.MMflags);
        cprintf("%ld\n", CitControl.MMnextuser);
@@ -469,6 +501,8 @@ void artv_do_export(void) {
        cprintf("%d\n", CitControl.version);
        if (Ctx->kill_me != 1)
                artv_export_users();
+       if (Ctx->kill_me != 1)
+               artv_export_openids();
        if (Ctx->kill_me != 1)
                artv_export_rooms();
        if (Ctx->kill_me != 1)
@@ -477,8 +511,8 @@ void artv_do_export(void) {
                artv_export_visits();
        if (Ctx->kill_me != 1)
                artv_export_messages();
-
-       cprintf("000\n");
+       client_write("000\n", 4);
+       Ctx->dont_term = 0;
 }
 
 void artv_do_dump(void) {
@@ -492,7 +526,7 @@ void artv_do_dump(void) {
        cprintf("version\n%d\n", REV_LEVEL);
 
        /* export the config file (this is done using x-macros) */
-       cprintf("config\n");
+       client_write("config\n", 7);
 
 #include "artv_dump.h"
 #include "dtds/config-defs.h"
@@ -500,7 +534,7 @@ void artv_do_dump(void) {
 
        /* Export the control file */
        get_control();
-       cprintf("control\n");
+       client_write("control\n", 8);
        cprintf(" MMhighest: %ld\n", CitControl.MMhighest);
        cprintf(" MMflags: %u\n", CitControl.MMflags);
        cprintf(" MMnextuser: %ld\n", CitControl.MMnextuser);
@@ -517,7 +551,7 @@ void artv_do_dump(void) {
        if (Ctx->kill_me != 1)
                artv_dump_messages();
 
-       cprintf("000\n");
+       client_write("000\n", 4);
 }
 
 
@@ -617,13 +651,9 @@ void artv_import_room(long *iterations) {
        /* format of message list export is all message numbers output
         * one per line terminated by a 0.
         */
-       while (client_getln(cbuf, sizeof cbuf), msgnum = atol(cbuf), msgnum > 0) {
-               CtdlLogPrintf(CTDL_DEBUG, "import room message link %d\n", msgnum);
+       while ((client_getln(cbuf, sizeof cbuf) >= 0) && (msgnum = atol(cbuf))) {
                CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
-               cprintf(".");
-               ++(*iterations);
-               if ((*iterations) % 64 == 0)
-                       cprintf("\n");
+               cprintdot(iterations);
                ++msgcount;
                if (CtdlThreadCheckStop())
                        break;
@@ -656,8 +686,6 @@ void artv_import_floor(void) {
 }
 
 
-/* 
- */
 void artv_import_visit(void) {
        struct visit vbuf;
        char buf[SIZ];
@@ -682,16 +710,39 @@ void artv_import_visit(void) {
 }
 
 
+void artv_import_openid(void) {
+       char buf[SIZ];
+       long usernum;
+       char openid[1024];
+       char *data;
+       int data_len;
+
+       client_getln(openid, sizeof openid);
+       client_getln(buf, sizeof buf);  usernum = atol(buf);
+       if (IsEmptyStr(openid)) return;
+
+       data_len = sizeof(long) + strlen(openid) + 1;
+       data = malloc(data_len);
+
+       memcpy(data, &usernum, sizeof(long));
+       memcpy(&data[sizeof(long)], openid, strlen(openid) + 1);
+
+       cdb_store(CDB_OPENID, openid, strlen(openid), data, data_len);
+       free(data);
 
-void artv_import_message(long *iterations) {
+       CtdlLogPrintf(CTDL_INFO, "Imported OpenID %s for user #%ld\n", openid, usernum);
+}
+
+
+void artv_import_message(long *iterations, char **b64buf, size_t *b64size, char **plain, size_t *plain_size) {
        struct MetaData smi;
        long msgnum;
        long msglen;
-       FILE *fp;
        char buf[SIZ];
-       char tempfile[PATH_MAX];
-       char *mbuf;
-
+       size_t b64len = 0;
+       char *tbuf, *tbuf2;
+       size_t mlen;
+       
        memset(&smi, 0, sizeof(struct MetaData));
        client_getln(buf, sizeof buf);  msgnum = atol(buf);
                                smi.meta_msgnum = msgnum;
@@ -701,35 +752,56 @@ void artv_import_message(long *iterations) {
        CtdlLogPrintf(CTDL_INFO, "message #%ld\n", msgnum);
 
        /* decode base64 message text */
-       CtdlMakeTempFileName(tempfile, sizeof tempfile);
-       snprintf(buf, sizeof buf, "%s -d >%s", file_base64, tempfile);
-       fp = popen(buf, "w");
-       while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
-               cprintf(".");
-               ++(*iterations);
-               if ((*iterations) % 64 == 0)
-                       cprintf("\n");
-               fprintf(fp, "%s\n", buf);
+       while (client_getln(buf, sizeof buf) >= 0 && strcasecmp(buf, END_OF_MESSAGE)) {
+               if (CtdlThreadCheckStop())
+                       return;
+                       
+               cprintdot(iterations);
+               
+               /**
+                * Grow the buffers if we need to
+                */
+               mlen = strlen (buf);
+               if (b64len + mlen > *b64size)
+               {
+                       tbuf = realloc (*b64buf, *b64size + SIZ);
+                       tbuf2 = realloc (*plain, *plain_size + SIZ);
+                       if (tbuf && tbuf2)
+                       {
+                               *b64buf = tbuf;
+                               *plain = tbuf2;
+                               *b64size += SIZ;
+                               *plain_size += SIZ;
+                       }
+                       else
+                       {
+                               CtdlLogPrintf(CTDL_DEBUG, "ARTV import: realloc() failed.\n");
+                               cprintf("\nMemory allocation failure.\n");
+                               return;
+                       }
+               }
+               strcat (*b64buf, buf);
+               b64len += mlen;
        }
-       pclose(fp);
-       fp = fopen(tempfile, "rb");
-       fseek(fp, 0L, SEEK_END);
-       msglen = ftell(fp);
-       fclose(fp);
+       
+       /**
+        * FIXME: This is an ideal place for a "sub thread". What we should do is create a new thread
+        * This new thread would be given the base64 encoded message, it would then decode it and store
+        * it. This would allow the thread that is reading from the client to continue doing so, basically
+        * backgrounding the decode and store operation. This would increase the speed of the import from
+        * the users perspective.
+        */
+       
+       /**
+        * Decode and store the message
+        * If this decode and store takes more than 5 seconds the sendcommand WD timer may expire.
+        */
+       msglen = CtdlDecodeBase64(*plain, *b64buf, b64len);
        CtdlLogPrintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
-
-       mbuf = malloc(msglen);
-       fp = fopen(tempfile, "rb");
-       fread(mbuf, msglen, 1, fp);
-       fclose(fp);
-
-        cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
-
-       free(mbuf);
-       unlink(tempfile);
-
+       cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), *plain, msglen);
        PutMetaData(&smi);
        CtdlLogPrintf(CTDL_INFO, "Imported message %ld\n", msgnum);
+       
 }
 
 
@@ -741,14 +813,41 @@ void artv_do_import(void) {
        char s_version[SIZ];
        int version;
        long iterations;
-
+       char *b64mes = NULL;
+       char *plain = NULL;
+       size_t b64size, plain_size;
+       
        unbuffer_output();
 
+       /* Prepare buffers for base 64 decoding of messages.
+       */
+       b64mes = malloc(SIZ);
+       if (b64mes == NULL)
+       {
+               cprintf("%d Malloc failed in import/export.\n",
+                       ERROR + RESOURCE_BUSY);
+               return;
+       }
+       b64mes[0] = 0;
+       b64size=SIZ;
+       plain = malloc(SIZ);
+       if (plain == NULL)
+       {
+               cprintf("%d Malloc failed in import/export.\n",
+                       ERROR + RESOURCE_BUSY);
+               free(b64mes);
+               return;
+       }
+       plain[0] = 0;
+       plain_size = SIZ;
+       
+       CC->dont_term = 1;
+
        cprintf("%d sock it to me\n", SEND_LISTING);
        abuf[0] = '\0';
        unbuffer_output();
        iterations = 0;
-       while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
+       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
                if (CtdlThreadCheckStop())
                        break;  // Should we break or return?
                
@@ -762,10 +861,7 @@ void artv_do_import(void) {
                        iterations = 0;
                }
                else {
-                       cprintf(".");
-                       iterations ++;
-                       if (iterations % 64 == 0)
-                               cprintf("\n");
+                       cprintdot(&iterations);
                }
                
                if (!strcasecmp(buf, "version")) {
@@ -782,12 +878,23 @@ void artv_do_import(void) {
                else if (!strcasecmp(buf, "room")) artv_import_room(&iterations);
                else if (!strcasecmp(buf, "floor")) artv_import_floor();
                else if (!strcasecmp(buf, "visit")) artv_import_visit();
-               else if (!strcasecmp(buf, "message")) artv_import_message(&iterations);
+               else if (!strcasecmp(buf, "openid")) artv_import_openid();
+               else if (!strcasecmp(buf, "message"))
+               {
+                       b64mes[0] = 0;
+                       plain[0] = 0;
+                       artv_import_message(&iterations, &b64mes, &b64size, &plain, &plain_size);
+               }
                else break;
        }
+       free (b64mes);
+       free (plain);
+       
        CtdlLogPrintf(CTDL_INFO, "Invalid keyword <%s>.  Flushing input.\n", buf);
-       while (client_getln(buf, sizeof buf), strcmp(buf, "000"))  ;;
+       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000"))  ;;
        rebuild_euid_index();
+       rebuild_usersbynumber();
+       CC->dont_term = 0;
 }
 
 
@@ -797,26 +904,28 @@ void cmd_artv(char *cmdbuf) {
        static int is_running = 0;
 
        if (CtdlAccessCheck(ac_internal)) return;
-       if (is_running) {
+       
+       if (CtdlTrySingleUser())
+       {
+               CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
+               CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
+
+               extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
+               if (!strcasecmp(cmd, "export")) artv_do_export();
+               else if (!strcasecmp(cmd, "import")) artv_do_import();
+               else if (!strcasecmp(cmd, "dump")) artv_do_dump();
+               else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
+
+               unlink(artv_tempfilename1);
+               unlink(artv_tempfilename2);
+               
+               CtdlEndSingleUser();
+       }
+       else
+       {
                cprintf("%d The importer/exporter is already running.\n",
                        ERROR + RESOURCE_BUSY);
-               return;
        }
-       is_running = 1;
-
-       CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
-       CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
-
-       extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
-       if (!strcasecmp(cmd, "export")) artv_do_export();
-       else if (!strcasecmp(cmd, "import")) artv_do_import();
-       else if (!strcasecmp(cmd, "dump")) artv_do_dump();
-       else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
-
-       unlink(artv_tempfilename1);
-       unlink(artv_tempfilename2);
-
-       is_running = 0;
 }