4 * This is the "Art Vandelay" module. It is an importer/exporter.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #include "citserver.h"
44 #include "euidindex.h"
47 #include "ctdl_module.h"
52 #define END_OF_MESSAGE "---eom---dbd---"
54 char artv_tempfilename1[PATH_MAX];
55 char artv_tempfilename2[PATH_MAX];
56 FILE *artv_global_message_list;
58 void artv_export_users_backend(struct ctdluser *buf, void *data) {
61 #include "artv_serialize.h"
62 #include "dtds/user-defs.h"
63 #include "undef_data.h"
65 cprintf("%d\n", buf->version);
66 cprintf("%ld\n", (long)buf->uid);
67 cprintf("%s\n", buf->password);
68 cprintf("%u\n", buf->flags);
69 cprintf("%ld\n", buf->timescalled);
70 cprintf("%ld\n", buf->posted);
71 cprintf("%d\n", buf->axlevel);
72 cprintf("%ld\n", buf->usernum);
73 cprintf("%ld\n", (long)buf->lastcall);
74 cprintf("%d\n", buf->USuserpurge);
75 cprintf("%s\n", buf->fullname);
76 cprintf("%d\n", buf->USscreenwidth);
77 cprintf("%d\n", buf->USscreenheight);
80 void artv_dump_users_backend(struct ctdluser *buf, void *data) {
83 #include "artv_dump.h"
84 #include "dtds/user-defs.h"
85 #include "undef_data.h"
89 void artv_export_users(void) {
90 ForEachUser(artv_export_users_backend, NULL);
93 void artv_dump_users(void) {
94 ForEachUser(artv_dump_users_backend, NULL);
98 void artv_export_room_msg(long msgnum, void *userdata) {
99 cprintf("%ld\n", msgnum);
100 fprintf(artv_global_message_list, "%ld\n", msgnum);
103 void artv_dump_room_msg(long msgnum, void *userdata) {
104 cprintf(" msgnum: %ld\n", msgnum);
105 fprintf(artv_global_message_list, "%ld\n", msgnum);
109 void artv_export_rooms_backend(struct ctdlroom *buf, void *data) {
112 #include "artv_serialize.h"
113 #include "dtds/room-defs.h"
114 #include "undef_data.h"
116 cprintf("%s\n", buf->QRname);
117 cprintf("%s\n", buf->QRpasswd);
118 cprintf("%ld\n", buf->QRroomaide);
119 cprintf("%ld\n", buf->QRhighest);
120 cprintf("%ld\n", (long)buf->QRgen);
121 cprintf("%u\n", buf->QRflags);
122 cprintf("%s\n", buf->QRdirname);
123 cprintf("%ld\n", buf->QRinfo);
124 cprintf("%d\n", buf->QRfloor);
125 cprintf("%ld\n", (long)buf->QRmtime);
126 cprintf("%d\n", buf->QRep.expire_mode);
127 cprintf("%d\n", buf->QRep.expire_value);
128 cprintf("%ld\n", buf->QRnumber);
129 cprintf("%d\n", buf->QRorder);
130 cprintf("%u\n", buf->QRflags2);
131 cprintf("%d\n", buf->QRdefaultview);
133 getroom(&CC->room, buf->QRname);
134 /* format of message list export is all message numbers output
135 * one per line terminated by a 0.
138 getroom(&CC->room, buf->QRname);
139 /* format of message list export is all message numbers output
140 * one per line terminated by a 0.
142 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
143 artv_export_room_msg, NULL);
148 void artv_dump_rooms_backend(struct ctdlroom *buf, void *data) {
151 #include "artv_dump.h"
152 #include "dtds/room-defs.h"
153 #include "undef_data.h"
155 getroom(&CC->room, buf->QRname);
156 /* format of message list export is all message numbers output
157 * one per line terminated by a 0.
160 getroom(&CC->room, buf->QRname);
161 /* format of message list export is all message numbers output
162 * one per line terminated by a 0.
164 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
165 artv_dump_room_msg, NULL);
172 void artv_export_rooms(void) {
174 artv_global_message_list = fopen(artv_tempfilename1, "w");
175 if (artv_global_message_list != NULL) {
176 ForEachRoom(artv_export_rooms_backend, NULL);
177 fclose(artv_global_message_list);
181 * Process the 'global' message list. (Sort it and remove dups.
182 * Dups are ok because a message may be in more than one room, but
183 * this will be handled by exporting the reference count, not by
184 * exporting the message multiple times.)
186 snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
188 snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
192 void artv_dump_rooms(void) {
194 artv_global_message_list = fopen(artv_tempfilename1, "w");
195 if (artv_global_message_list != NULL) {
196 ForEachRoom(artv_dump_rooms_backend, NULL);
197 fclose(artv_global_message_list);
201 * Process the 'global' message list. (Sort it and remove dups.
202 * Dups are ok because a message may be in more than one room, but
203 * this will be handled by exporting the reference count, not by
204 * exporting the message multiple times.)
206 snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
208 snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
213 void artv_export_floors(void) {
214 struct floor qfbuf, *buf;
217 for (i=0; i < MAXFLOORS; ++i) {
223 #include "artv_serialize.h"
224 #include "dtds/floor-defs.h"
225 #include "undef_data.h"
227 cprintf("%u\n", buf->f_flags);
228 cprintf("%s\n", buf->f_name);
229 cprintf("%d\n", buf->f_ref_count);
230 cprintf("%d\n", buf->f_ep.expire_mode);
231 cprintf("%d\n", buf->f_ep.expire_value);
236 void artv_dump_floors(void) {
237 struct floor qfbuf, *buf;
240 for (i=0; i < MAXFLOORS; ++i) {
246 #include "artv_serialize.h"
247 #include "dtds/floor-defs.h"
248 #include "undef_data.h"
257 * Traverse the visits file...
259 void artv_export_visits(void) {
261 struct cdbdata *cdbv;
263 cdb_rewind(CDB_VISIT);
265 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
266 memset(&vbuf, 0, sizeof(struct visit));
267 memcpy(&vbuf, cdbv->ptr,
268 ((cdbv->len > sizeof(struct visit)) ?
269 sizeof(struct visit) : cdbv->len));
273 cprintf("%ld\n", vbuf.v_roomnum);
274 cprintf("%ld\n", vbuf.v_roomgen);
275 cprintf("%ld\n", vbuf.v_usernum);
277 if (!IsEmptyStr(vbuf.v_seen)) {
278 cprintf("%s\n", vbuf.v_seen);
281 cprintf("%ld\n", vbuf.v_lastseen);
284 cprintf("%s\n", vbuf.v_answered);
285 cprintf("%u\n", vbuf.v_flags);
286 cprintf("%d\n", vbuf.v_view);
291 * Traverse the visits file...
293 void artv_dump_visits(void) {
295 struct cdbdata *cdbv;
297 cdb_rewind(CDB_VISIT);
299 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
300 memset(&vbuf, 0, sizeof(struct visit));
301 memcpy(&vbuf, cdbv->ptr,
302 ((cdbv->len > sizeof(struct visit)) ?
303 sizeof(struct visit) : cdbv->len));
306 cprintf("---visit---\n");
307 cprintf(" Room-Num: %ld\n", vbuf.v_roomnum);
308 cprintf(" Room-Gen%ld\n", vbuf.v_roomgen);
309 cprintf(" User-Num%ld\n", vbuf.v_usernum);
311 if (!IsEmptyStr(vbuf.v_seen)) {
312 cprintf(" Seen: %s\n", vbuf.v_seen);
315 cprintf(" LastSeen: %ld\n", vbuf.v_lastseen);
318 cprintf(" Answered: %s\n", vbuf.v_answered);
319 cprintf(" Flags: %u\n", vbuf.v_flags);
320 cprintf(" View: %d\n", vbuf.v_view);
325 void artv_export_message(long msgnum) {
327 struct CtdlMessage *msg;
331 char tempfile[PATH_MAX];
333 msg = CtdlFetchMessage(msgnum, 1);
334 if (msg == NULL) return; /* fail silently */
336 cprintf("message\n");
337 GetMetaData(&smi, msgnum);
338 cprintf("%ld\n", msgnum);
339 cprintf("%d\n", smi.meta_refcount);
340 cprintf("%s\n", smi.meta_content_type);
342 serialize_message(&smr, msg);
343 CtdlFreeMessage(msg);
345 /* write it in base64 */
346 CtdlMakeTempFileName(tempfile, sizeof tempfile);
347 snprintf(buf, sizeof buf, "%s -e >%s", file_base64, tempfile);
348 fp = popen(buf, "w");
349 fwrite(smr.ser, smr.len, 1, fp);
354 fp = fopen(tempfile, "r");
357 while (fgets(buf, sizeof(buf), fp) != NULL) {
358 buf[strlen(buf)-1] = 0;
359 cprintf("%s\n", buf);
363 cprintf("%s\n", END_OF_MESSAGE);
366 void artv_dump_message(long msgnum) {
368 struct CtdlMessage *msg;
372 char tempfile[PATH_MAX];
374 msg = CtdlFetchMessage(msgnum, 1);
375 if (msg == NULL) return; /* fail silently */
377 cprintf("message\n");
378 GetMetaData(&smi, msgnum);
379 cprintf(" MessageNum: %ld\n", msgnum);
380 cprintf(" MetaRefcount: %d\n", smi.meta_refcount);
381 cprintf(" MetaContentType: %s\n", smi.meta_content_type);
383 serialize_message(&smr, msg);
384 CtdlFreeMessage(msg);
386 /* write it in base64 * /
387 CtdlMakeTempFileName(tempfile, sizeof tempfile);
388 snprintf(buf, sizeof buf, "%s -e >%s", file_base64, tempfile);
389 fp = popen(buf, "w");
390 fwrite(smr.ser, smr.len, 1, fp);
395 fp = fopen(tempfile, "r");
398 while (fgets(buf, sizeof(buf), fp) != NULL) {
399 buf[strlen(buf)-1] = 0;
400 cprintf("%s\n", buf);
404 cprintf("%s\n", END_OF_MESSAGE);
409 void artv_export_messages(void) {
416 artv_global_message_list = fopen(artv_tempfilename1, "r");
417 if (artv_global_message_list != NULL) {
418 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
419 while ((Ctx->kill_me != 1) &&
420 (fgets(buf, sizeof(buf), artv_global_message_list) != NULL)) {
423 artv_export_message(msgnum);
427 fclose(artv_global_message_list);
429 if (Ctx->kill_me != 1)
430 lprintf(CTDL_INFO, "Exported %d messages.\n", count);
432 lprintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
435 void artv_dump_messages(void) {
442 artv_global_message_list = fopen(artv_tempfilename1, "r");
443 if (artv_global_message_list != NULL) {
444 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
445 while ((Ctx->kill_me != 1) &&
446 (fgets(buf, sizeof(buf), artv_global_message_list) != NULL)) {
449 artv_dump_message(msgnum);
453 fclose(artv_global_message_list);
455 if (Ctx->kill_me != 1)
456 lprintf(CTDL_INFO, "Exported %d messages.\n", count);
458 lprintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
464 void artv_do_export(void) {
470 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
472 cprintf("version\n%d\n", REV_LEVEL);
474 /* export the config file (this is done using x-macros) */
477 #include "artv_serialize.h"
478 #include "dtds/config-defs.h"
479 #include "undef_data.h"
481 /* Export the control file */
483 cprintf("control\n");
484 cprintf("%ld\n", CitControl.MMhighest);
485 cprintf("%u\n", CitControl.MMflags);
486 cprintf("%ld\n", CitControl.MMnextuser);
487 cprintf("%ld\n", CitControl.MMnextroom);
488 cprintf("%d\n", CitControl.version);
489 if (Ctx->kill_me != 1)
491 if (Ctx->kill_me != 1)
493 if (Ctx->kill_me != 1)
494 artv_export_floors();
495 if (Ctx->kill_me != 1)
496 artv_export_visits();
497 if (Ctx->kill_me != 1)
498 artv_export_messages();
503 void artv_do_dump(void) {
509 cprintf("%d dumping Citadel structures.\n", LISTING_FOLLOWS);
511 cprintf("version\n%d\n", REV_LEVEL);
513 /* export the config file (this is done using x-macros) */
516 #include "artv_serialize.h"
517 #include "dtds/config-defs.h"
518 #include "undef_data.h"
520 /* Export the control file */
522 cprintf("control\n");
523 cprintf("%ld\n", CitControl.MMhighest);
524 cprintf("%u\n", CitControl.MMflags);
525 cprintf("%ld\n", CitControl.MMnextuser);
526 cprintf("%ld\n", CitControl.MMnextroom);
527 cprintf("%d\n", CitControl.version);
528 if (Ctx->kill_me != 1)
530 if (Ctx->kill_me != 1)
532 if (Ctx->kill_me != 1)
534 if (Ctx->kill_me != 1)
536 if (Ctx->kill_me != 1)
537 artv_dump_messages();
544 void artv_import_config(void) {
549 lprintf(CTDL_DEBUG, "Importing config file\n");
551 #include "artv_deserialize.h"
552 #include "dtds/config-defs.h"
553 #include "undef_data.h"
555 config.c_enable_fulltext = 0; /* always disable */
557 lprintf(CTDL_INFO, "Imported config file\n");
561 void artv_import_control(void) {
564 lprintf(CTDL_DEBUG, "Importing control file\n");
565 client_getln(buf, sizeof buf); CitControl.MMhighest = atol(buf);
566 client_getln(buf, sizeof buf); CitControl.MMflags = atoi(buf);
567 client_getln(buf, sizeof buf); CitControl.MMnextuser = atol(buf);
568 client_getln(buf, sizeof buf); CitControl.MMnextroom = atol(buf);
569 client_getln(buf, sizeof buf); CitControl.version = atoi(buf);
570 CitControl.MMfulltext = (-1L); /* always flush */
572 lprintf(CTDL_INFO, "Imported control file\n");
576 void artv_import_user(void) {
578 struct ctdluser usbuf, *buf;
581 #include "artv_deserialize.h"
582 #include "dtds/user-defs.h"
583 #include "undef_data.h"
586 client_getln(cbuf, sizeof cbuf); buf->version = atoi(cbuf);
587 client_getln(cbuf, sizeof cbuf); buf->uid = atoi(cbuf);
588 client_getln(buf->password, sizeof buf->password);
589 client_getln(cbuf, sizeof cbuf); buf->flags = atoi(cbuf);
590 client_getln(cbuf, sizeof cbuf); buf->timescalled = atol(cbuf);
591 client_getln(cbuf, sizeof cbuf); buf->posted = atol(cbuf);
592 client_getln(cbuf, sizeof cbuf); buf->axlevel = atoi(cbuf);
593 client_getln(cbuf, sizeof cbuf); buf->usernum = atol(cbuf);
594 client_getln(cbuf, sizeof cbuf); buf->lastcall = atol(cbuf);
595 client_getln(cbuf, sizeof cbuf); buf->USuserpurge = atoi(cbuf);
596 client_getln(buf->fullname, sizeof buf->fullname);
597 client_getln(cbuf, sizeof cbuf); buf->USscreenwidth = atoi(cbuf);
598 client_getln(cbuf, sizeof cbuf); buf->USscreenheight = atoi(cbuf);
604 void artv_import_room(void) {
606 struct ctdlroom qrbuf, *buf;
612 #include "artv_deserialize.h"
613 #include "dtds/room-defs.h"
614 #include "undef_data.h"
617 client_getln(buf->QRname, sizeof buf->QRname);
618 client_getln(buf->QRpasswd, sizeof buf->QRpasswd);
619 client_getln(cbuf, sizeof cbuf); buf->QRroomaide = atol(cbuf);
620 client_getln(cbuf, sizeof cbuf); buf->QRhighest = atol(cbuf);
621 client_getln(cbuf, sizeof cbuf); buf->QRgen = atol(cbuf);
622 client_getln(cbuf, sizeof cbuf); buf->QRflags = atoi(cbuf);
623 client_getln(buf->QRdirname, sizeof buf->QRdirname);
624 client_getln(cbuf, sizeof cbuf); buf->QRinfo = atol(cbuf);
625 client_getln(cbuf, sizeof cbuf); buf->QRfloor = atoi(cbuf);
626 client_getln(cbuf, sizeof cbuf); buf->QRmtime = atol(cbuf);
627 client_getln(cbuf, sizeof cbuf); buf->QRep.expire_mode = atoi(cbuf);
628 client_getln(cbuf, sizeof cbuf); buf->QRep.expire_value = atoi(cbuf);
629 client_getln(cbuf, sizeof cbuf); buf->QRnumber = atol(cbuf);
630 client_getln(cbuf, sizeof cbuf); buf->QRorder = atoi(cbuf);
631 client_getln(cbuf, sizeof cbuf); buf->QRflags2 = atoi(cbuf);
632 client_getln(cbuf, sizeof cbuf); buf->QRdefaultview = atoi(cbuf);
635 lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
636 /* format of message list export is all message numbers output
637 * one per line terminated by a 0.
639 while (client_getln(cbuf, sizeof cbuf), msgnum = atol(cbuf), msgnum > 0) {
640 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
643 lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
647 void artv_import_floor(void) {
648 struct floor flbuf, *buf;
653 memset(buf, 0, sizeof(buf));
654 client_getln(cbuf, sizeof cbuf); i = atoi(cbuf);
656 #include "artv_deserialize.h"
657 #include "dtds/floor-defs.h"
658 #include "undef_data.h"
660 client_getln(cbuf, sizeof cbuf); buf->f_flags = atoi(cbuf);
661 client_getln(buf->f_name, sizeof buf->f_name);
662 client_getln(cbuf, sizeof cbuf); buf->f_ref_count = atoi(cbuf);
663 client_getln(cbuf, sizeof cbuf); buf->f_ep.expire_mode = atoi(cbuf);
664 client_getln(cbuf, sizeof cbuf); buf->f_ep.expire_value = atoi(cbuf);
667 lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
673 void artv_import_visit(void) {
677 int is_textual_seen = 0;
679 client_getln(buf, sizeof buf); vbuf.v_roomnum = atol(buf);
680 client_getln(buf, sizeof buf); vbuf.v_roomgen = atol(buf);
681 client_getln(buf, sizeof buf); vbuf.v_usernum = atol(buf);
683 client_getln(buf, sizeof buf);
684 vbuf.v_lastseen = atol(buf);
685 for (i=0; buf[i]; ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
686 if (is_textual_seen) strcpy(vbuf.v_seen, buf);
688 client_getln(vbuf.v_answered, sizeof vbuf.v_answered);
689 client_getln(buf, sizeof buf); vbuf.v_flags = atoi(buf);
690 client_getln(buf, sizeof buf); vbuf.v_view = atoi(buf);
692 lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
693 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
698 void artv_import_message(void) {
704 char tempfile[PATH_MAX];
707 memset(&smi, 0, sizeof(struct MetaData));
708 client_getln(buf, sizeof buf); msgnum = atol(buf);
709 smi.meta_msgnum = msgnum;
710 client_getln(buf, sizeof buf); smi.meta_refcount = atoi(buf);
711 client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
713 lprintf(CTDL_INFO, "message #%ld\n", msgnum);
715 /* decode base64 message text */
716 CtdlMakeTempFileName(tempfile, sizeof tempfile);
717 snprintf(buf, sizeof buf, "%s -d >%s", file_base64, tempfile);
718 fp = popen(buf, "w");
719 while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
720 fprintf(fp, "%s\n", buf);
723 fp = fopen(tempfile, "rb");
724 fseek(fp, 0L, SEEK_END);
727 lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
729 mbuf = malloc(msglen);
730 fp = fopen(tempfile, "rb");
731 fread(mbuf, msglen, 1, fp);
734 cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
740 lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
746 void artv_do_import(void) {
755 cprintf("%d sock it to me\n", SEND_LISTING);
759 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
761 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
762 if ((abuf[0] == '\0') || (strcasecmp(buf, abuf))) {
763 cprintf ("\n\nImporting datatype %s\n", buf);
764 strncpy (abuf, buf, SIZ);
769 if (iterations % 64 == 0)
774 if (!strcasecmp(buf, "version")) {
775 client_getln(s_version, sizeof s_version);
776 version = atoi(s_version);
777 if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
778 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
782 else if (!strcasecmp(buf, "config")) artv_import_config();
783 else if (!strcasecmp(buf, "control")) artv_import_control();
784 else if (!strcasecmp(buf, "user")) artv_import_user();
785 else if (!strcasecmp(buf, "room")) artv_import_room();
786 else if (!strcasecmp(buf, "floor")) artv_import_floor();
787 else if (!strcasecmp(buf, "visit")) artv_import_visit();
788 else if (!strcasecmp(buf, "message")) artv_import_message();
792 lprintf(CTDL_INFO, "Invalid keyword <%s>. Flushing input.\n", buf);
793 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) ;;
794 rebuild_euid_index();
799 void cmd_artv(char *cmdbuf) {
801 static int is_running = 0;
803 if (CtdlAccessCheck(ac_internal)) return;
805 cprintf("%d The importer/exporter is already running.\n",
806 ERROR + RESOURCE_BUSY);
811 CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
812 CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
814 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
815 if (!strcasecmp(cmd, "export")) artv_do_export();
816 else if (!strcasecmp(cmd, "import")) artv_do_import();
817 else if (!strcasecmp(cmd, "dump")) artv_do_dump();
818 else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
820 unlink(artv_tempfilename1);
821 unlink(artv_tempfilename2);
829 CTDL_MODULE_INIT(vandelay)
831 CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
833 /* return our Subversion id for the Log */