* Began implenmenting OpenID table import/export
[citadel.git] / citadel / modules / vandelay / serv_vandelay.c
index 576089f3ccd9492fb2bacd760cf80e3505411813..aeaf81ece5cd0faf198ce9dd4965a86afa19034e 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,14 +78,28 @@ 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"
 #include "undef_data.h"
+       cprintf("\n");
 }
 
 
+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);
 }
@@ -103,11 +117,12 @@ void artv_export_room_msg(long msgnum, void *userdata) {
 void artv_dump_room_msg(long msgnum, void *userdata) {
        cprintf(" msgnum: %ld\n", msgnum);
        fprintf(artv_global_message_list, "%ld\n", msgnum);
+       cprintf("\n");
 }//// TODO
 
 
 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"
@@ -146,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"
@@ -215,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;
@@ -238,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;
@@ -269,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);
@@ -303,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);
@@ -333,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);
@@ -370,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);
@@ -384,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;
@@ -393,7 +427,7 @@ void artv_export_messages(void) {
        Ctx = CC;
        artv_global_message_list = fopen(artv_tempfilename1, "r");
        if (artv_global_message_list != NULL) {
-               lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
+               CtdlLogPrintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
                while ((Ctx->kill_me != 1) && 
                       (fgets(buf, sizeof(buf), artv_global_message_list) != NULL)) {
                        msgnum = atol(buf);
@@ -405,9 +439,9 @@ void artv_export_messages(void) {
                fclose(artv_global_message_list);
        }
        if (Ctx->kill_me != 1)
-               lprintf(CTDL_INFO, "Exported %d messages.\n", count);
+               CtdlLogPrintf(CTDL_INFO, "Exported %d messages.\n", count);
        else
-               lprintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
+               CtdlLogPrintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
 }
 
 void artv_dump_messages(void) {
@@ -419,7 +453,7 @@ void artv_dump_messages(void) {
        Ctx = CC;
        artv_global_message_list = fopen(artv_tempfilename1, "r");
        if (artv_global_message_list != NULL) {
-               lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
+               CtdlLogPrintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
                while ((Ctx->kill_me != 1) && 
                       (fgets(buf, sizeof(buf), artv_global_message_list) != NULL)) {
                        msgnum = atol(buf);
@@ -431,9 +465,9 @@ void artv_dump_messages(void) {
                fclose(artv_global_message_list);
        }
        if (Ctx->kill_me != 1)
-               lprintf(CTDL_INFO, "Exported %d messages.\n", count);
+               CtdlLogPrintf(CTDL_INFO, "Exported %d messages.\n", count);
        else
-               lprintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
+               CtdlLogPrintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
 }
 
 
@@ -450,15 +484,16 @@ void artv_do_export(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_serialize.h"
 #include "dtds/config-defs.h"
 #include "undef_data.h"
-
+       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);
@@ -466,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)
@@ -475,7 +512,7 @@ void artv_do_export(void) {
        if (Ctx->kill_me != 1)
                artv_export_messages();
 
-       cprintf("000\n");
+       client_write("000\n", 4);
 }
 
 void artv_do_dump(void) {
@@ -489,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"
@@ -497,12 +534,12 @@ void artv_do_dump(void) {
 
        /* Export the control file */
        get_control();
-       cprintf("control\n");
-       cprintf("%ld\n", CitControl.MMhighest);
-       cprintf("%u\n", CitControl.MMflags);
-       cprintf("%ld\n", CitControl.MMnextuser);
-       cprintf("%ld\n", CitControl.MMnextroom);
-       cprintf("%d\n", CitControl.version);
+       client_write("control\n", 8);
+       cprintf(" MMhighest: %ld\n", CitControl.MMhighest);
+       cprintf(" MMflags: %u\n", CitControl.MMflags);
+       cprintf(" MMnextuser: %ld\n", CitControl.MMnextuser);
+       cprintf(" MMnextroom: %ld\n", CitControl.MMnextroom);
+       cprintf(" version: %d\n\n", CitControl.version);
        if (Ctx->kill_me != 1)
                artv_dump_users();
        if (Ctx->kill_me != 1)
@@ -514,7 +551,7 @@ void artv_do_dump(void) {
        if (Ctx->kill_me != 1)
                artv_dump_messages();
 
-       cprintf("000\n");
+       client_write("000\n", 4);
 }
 
 
@@ -524,7 +561,7 @@ void artv_import_config(void) {
        struct config *buf;
        buf = &config;
 
-       lprintf(CTDL_DEBUG, "Importing config file\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Importing config file\n");
 
 #include "artv_deserialize.h"
 #include "dtds/config-defs.h"
@@ -532,14 +569,14 @@ void artv_import_config(void) {
 
        config.c_enable_fulltext = 0;   /* always disable */
        put_config();
-       lprintf(CTDL_INFO, "Imported config file\n");
+       CtdlLogPrintf(CTDL_INFO, "Imported config file\n");
 }
 
 
 void artv_import_control(void) {
        char buf[SIZ];
 
-       lprintf(CTDL_DEBUG, "Importing control file\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Importing control file\n");
        client_getln(buf, sizeof buf);  CitControl.MMhighest = atol(buf);
        client_getln(buf, sizeof buf);  CitControl.MMflags = atoi(buf);
        client_getln(buf, sizeof buf);  CitControl.MMnextuser = atol(buf);
@@ -547,7 +584,7 @@ void artv_import_control(void) {
        client_getln(buf, sizeof buf);  CitControl.version = atoi(buf);
        CitControl.MMfulltext = (-1L);  /* always flush */
        put_control();
-       lprintf(CTDL_INFO, "Imported control file\n");
+       CtdlLogPrintf(CTDL_INFO, "Imported control file\n");
 }
 
 
@@ -579,7 +616,7 @@ void artv_import_user(void) {
 }
 
 
-void artv_import_room(void) {
+void artv_import_room(long *iterations) {
        char cbuf[SIZ];
        struct ctdlroom qrbuf, *buf;
        long msgnum;
@@ -610,15 +647,18 @@ void artv_import_room(void) {
        client_getln(cbuf, sizeof cbuf);        buf->QRdefaultview = atoi(cbuf);
 //*/
        putroom(buf);
-       lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
+       CtdlLogPrintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
        /* 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) {
+       while ((client_getln(cbuf, sizeof cbuf) >= 0) && (msgnum = atol(cbuf))) {
                CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
+               cprintdot(iterations);
                ++msgcount;
+               if (CtdlThreadCheckStop())
+                       break;
        }
-       lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
+       CtdlLogPrintf(CTDL_INFO, "(%d messages)\n", msgcount);
 }
 
 
@@ -642,12 +682,10 @@ void artv_import_floor(void) {
        client_getln(cbuf, sizeof cbuf);        buf->f_ep.expire_value = atoi(cbuf);
 //*/
        putfloor(buf, i);
-       lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
+       CtdlLogPrintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
 }
 
 
-/* 
- */
 void artv_import_visit(void) {
        struct visit vbuf;
        char buf[SIZ];
@@ -667,55 +705,103 @@ void artv_import_visit(void) {
        client_getln(buf, sizeof buf);  vbuf.v_flags = atoi(buf);
        client_getln(buf, sizeof buf);  vbuf.v_view = atoi(buf);
        put_visit(&vbuf);
-       lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
+       CtdlLogPrintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
                vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
 }
 
 
+void artv_import_openid(void) {
+       char buf[SIZ];
+       long usernum;
+       char openid[1024];
+       char *data;
+       int data_len;
+
+       client_getln(buf, sizeof buf);  usernum = atol(buf);
+       client_getln(openid, sizeof openid);
+       if (IsEmptyStr(openid)) return;
 
-void artv_import_message(void) {
+       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);
+
+       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;
        client_getln(buf, sizeof buf);  smi.meta_refcount = atoi(buf);
        client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
 
-       lprintf(CTDL_INFO, "message #%ld\n", msgnum);
+       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)) {
-               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);
-       lprintf(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);
-
+       
+       /**
+        * 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);
+       cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), *plain, msglen);
        PutMetaData(&smi);
-       lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
+       CtdlLogPrintf(CTDL_INFO, "Imported message %ld\n", msgnum);
+       
 }
 
 
@@ -727,49 +813,85 @@ 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;
+       
        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")) {
-
-               lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
+       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
+               if (CtdlThreadCheckStop())
+                       break;  // Should we break or return?
+               
+               if (buf[0] == '\0')
+                       continue;
+                       
+               CtdlLogPrintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
                if ((abuf[0] == '\0') || (strcasecmp(buf, abuf))) {
                        cprintf ("\n\nImporting datatype %s\n", buf);
                        strncpy (abuf, buf, SIZ);       
                        iterations = 0;
                }
                else {
-                       cprintf(".");
-                       if (iterations % 64 == 0)
-                               cprintf("\n");
-                       
+                       cprintdot(&iterations);
                }
                
                if (!strcasecmp(buf, "version")) {
                        client_getln(s_version, sizeof s_version);
                        version = atoi(s_version);
                        if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
-                               lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
+                               CtdlLogPrintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
                                break;
                        }
                }
                else if (!strcasecmp(buf, "config")) artv_import_config();
                else if (!strcasecmp(buf, "control")) artv_import_control();
                else if (!strcasecmp(buf, "user")) artv_import_user();
-               else if (!strcasecmp(buf, "room")) artv_import_room();
+               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();
+               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;
-               iterations ++;
        }
-       lprintf(CTDL_INFO, "Invalid keyword <%s>.  Flushing input.\n", buf);
-       while (client_getln(buf, sizeof buf), strcmp(buf, "000"))  ;;
+       free (b64mes);
+       free (plain);
+       
+       CtdlLogPrintf(CTDL_INFO, "Invalid keyword <%s>.  Flushing input.\n", buf);
+       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000"))  ;;
        rebuild_euid_index();
+       rebuild_usersbynumber();
 }
 
 
@@ -806,8 +928,11 @@ void cmd_artv(char *cmdbuf) {
 
 CTDL_MODULE_INIT(vandelay)
 {
-       CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
-
+       if (!threading)
+       {
+               CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
+       }
+       
        /* return our Subversion id for the Log */
        return "$Id$";
 }