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>
33 #include <libcitadel.h>
36 #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;
370 msg = CtdlFetchMessage(msgnum, 1);
371 if (msg == NULL) return; /* fail silently */
373 cprintf("message\n");
374 GetMetaData(&smi, msgnum);
375 cprintf(" MessageNum: %ld\n", msgnum);
376 cprintf(" MetaRefcount: %d\n", smi.meta_refcount);
377 cprintf(" MetaContentType: %s\n", smi.meta_content_type);
379 dump_message(msg, 80);
380 CtdlFreeMessage(msg);
382 cprintf("%s\n", END_OF_MESSAGE);
387 void artv_export_messages(void) {
394 artv_global_message_list = fopen(artv_tempfilename1, "r");
395 if (artv_global_message_list != NULL) {
396 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
397 while ((Ctx->kill_me != 1) &&
398 (fgets(buf, sizeof(buf), artv_global_message_list) != NULL)) {
401 artv_export_message(msgnum);
405 fclose(artv_global_message_list);
407 if (Ctx->kill_me != 1)
408 lprintf(CTDL_INFO, "Exported %d messages.\n", count);
410 lprintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
413 void artv_dump_messages(void) {
420 artv_global_message_list = fopen(artv_tempfilename1, "r");
421 if (artv_global_message_list != NULL) {
422 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
423 while ((Ctx->kill_me != 1) &&
424 (fgets(buf, sizeof(buf), artv_global_message_list) != NULL)) {
427 artv_dump_message(msgnum);
431 fclose(artv_global_message_list);
433 if (Ctx->kill_me != 1)
434 lprintf(CTDL_INFO, "Exported %d messages.\n", count);
436 lprintf(CTDL_ERR, "Export aborted due to client disconnect! \n");
442 void artv_do_export(void) {
448 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
450 cprintf("version\n%d\n", REV_LEVEL);
452 /* export the config file (this is done using x-macros) */
455 #include "artv_serialize.h"
456 #include "dtds/config-defs.h"
457 #include "undef_data.h"
459 /* Export the control file */
461 cprintf("control\n");
462 cprintf("%ld\n", CitControl.MMhighest);
463 cprintf("%u\n", CitControl.MMflags);
464 cprintf("%ld\n", CitControl.MMnextuser);
465 cprintf("%ld\n", CitControl.MMnextroom);
466 cprintf("%d\n", CitControl.version);
467 if (Ctx->kill_me != 1)
469 if (Ctx->kill_me != 1)
471 if (Ctx->kill_me != 1)
472 artv_export_floors();
473 if (Ctx->kill_me != 1)
474 artv_export_visits();
475 if (Ctx->kill_me != 1)
476 artv_export_messages();
481 void artv_do_dump(void) {
487 cprintf("%d dumping Citadel structures.\n", LISTING_FOLLOWS);
489 cprintf("version\n%d\n", REV_LEVEL);
491 /* export the config file (this is done using x-macros) */
494 #include "artv_dump.h"
495 #include "dtds/config-defs.h"
496 #include "undef_data.h"
498 /* Export the control file */
500 cprintf("control\n");
501 cprintf("%ld\n", CitControl.MMhighest);
502 cprintf("%u\n", CitControl.MMflags);
503 cprintf("%ld\n", CitControl.MMnextuser);
504 cprintf("%ld\n", CitControl.MMnextroom);
505 cprintf("%d\n", CitControl.version);
506 if (Ctx->kill_me != 1)
508 if (Ctx->kill_me != 1)
510 if (Ctx->kill_me != 1)
512 if (Ctx->kill_me != 1)
514 if (Ctx->kill_me != 1)
515 artv_dump_messages();
522 void artv_import_config(void) {
527 lprintf(CTDL_DEBUG, "Importing config file\n");
529 #include "artv_deserialize.h"
530 #include "dtds/config-defs.h"
531 #include "undef_data.h"
533 config.c_enable_fulltext = 0; /* always disable */
535 lprintf(CTDL_INFO, "Imported config file\n");
539 void artv_import_control(void) {
542 lprintf(CTDL_DEBUG, "Importing control file\n");
543 client_getln(buf, sizeof buf); CitControl.MMhighest = atol(buf);
544 client_getln(buf, sizeof buf); CitControl.MMflags = atoi(buf);
545 client_getln(buf, sizeof buf); CitControl.MMnextuser = atol(buf);
546 client_getln(buf, sizeof buf); CitControl.MMnextroom = atol(buf);
547 client_getln(buf, sizeof buf); CitControl.version = atoi(buf);
548 CitControl.MMfulltext = (-1L); /* always flush */
550 lprintf(CTDL_INFO, "Imported control file\n");
554 void artv_import_user(void) {
556 struct ctdluser usbuf, *buf;
559 #include "artv_deserialize.h"
560 #include "dtds/user-defs.h"
561 #include "undef_data.h"
564 client_getln(cbuf, sizeof cbuf); buf->version = atoi(cbuf);
565 client_getln(cbuf, sizeof cbuf); buf->uid = atoi(cbuf);
566 client_getln(buf->password, sizeof buf->password);
567 client_getln(cbuf, sizeof cbuf); buf->flags = atoi(cbuf);
568 client_getln(cbuf, sizeof cbuf); buf->timescalled = atol(cbuf);
569 client_getln(cbuf, sizeof cbuf); buf->posted = atol(cbuf);
570 client_getln(cbuf, sizeof cbuf); buf->axlevel = atoi(cbuf);
571 client_getln(cbuf, sizeof cbuf); buf->usernum = atol(cbuf);
572 client_getln(cbuf, sizeof cbuf); buf->lastcall = atol(cbuf);
573 client_getln(cbuf, sizeof cbuf); buf->USuserpurge = atoi(cbuf);
574 client_getln(buf->fullname, sizeof buf->fullname);
575 client_getln(cbuf, sizeof cbuf); buf->USscreenwidth = atoi(cbuf);
576 client_getln(cbuf, sizeof cbuf); buf->USscreenheight = atoi(cbuf);
582 void artv_import_room(void) {
584 struct ctdlroom qrbuf, *buf;
590 #include "artv_deserialize.h"
591 #include "dtds/room-defs.h"
592 #include "undef_data.h"
595 client_getln(buf->QRname, sizeof buf->QRname);
596 client_getln(buf->QRpasswd, sizeof buf->QRpasswd);
597 client_getln(cbuf, sizeof cbuf); buf->QRroomaide = atol(cbuf);
598 client_getln(cbuf, sizeof cbuf); buf->QRhighest = atol(cbuf);
599 client_getln(cbuf, sizeof cbuf); buf->QRgen = atol(cbuf);
600 client_getln(cbuf, sizeof cbuf); buf->QRflags = atoi(cbuf);
601 client_getln(buf->QRdirname, sizeof buf->QRdirname);
602 client_getln(cbuf, sizeof cbuf); buf->QRinfo = atol(cbuf);
603 client_getln(cbuf, sizeof cbuf); buf->QRfloor = atoi(cbuf);
604 client_getln(cbuf, sizeof cbuf); buf->QRmtime = atol(cbuf);
605 client_getln(cbuf, sizeof cbuf); buf->QRep.expire_mode = atoi(cbuf);
606 client_getln(cbuf, sizeof cbuf); buf->QRep.expire_value = atoi(cbuf);
607 client_getln(cbuf, sizeof cbuf); buf->QRnumber = atol(cbuf);
608 client_getln(cbuf, sizeof cbuf); buf->QRorder = atoi(cbuf);
609 client_getln(cbuf, sizeof cbuf); buf->QRflags2 = atoi(cbuf);
610 client_getln(cbuf, sizeof cbuf); buf->QRdefaultview = atoi(cbuf);
613 lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
614 /* format of message list export is all message numbers output
615 * one per line terminated by a 0.
617 while (client_getln(cbuf, sizeof cbuf), msgnum = atol(cbuf), msgnum > 0) {
618 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
621 lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
625 void artv_import_floor(void) {
626 struct floor flbuf, *buf;
631 memset(buf, 0, sizeof(buf));
632 client_getln(cbuf, sizeof cbuf); i = atoi(cbuf);
634 #include "artv_deserialize.h"
635 #include "dtds/floor-defs.h"
636 #include "undef_data.h"
638 client_getln(cbuf, sizeof cbuf); buf->f_flags = atoi(cbuf);
639 client_getln(buf->f_name, sizeof buf->f_name);
640 client_getln(cbuf, sizeof cbuf); buf->f_ref_count = atoi(cbuf);
641 client_getln(cbuf, sizeof cbuf); buf->f_ep.expire_mode = atoi(cbuf);
642 client_getln(cbuf, sizeof cbuf); buf->f_ep.expire_value = atoi(cbuf);
645 lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
651 void artv_import_visit(void) {
655 int is_textual_seen = 0;
657 client_getln(buf, sizeof buf); vbuf.v_roomnum = atol(buf);
658 client_getln(buf, sizeof buf); vbuf.v_roomgen = atol(buf);
659 client_getln(buf, sizeof buf); vbuf.v_usernum = atol(buf);
661 client_getln(buf, sizeof buf);
662 vbuf.v_lastseen = atol(buf);
663 for (i=0; buf[i]; ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
664 if (is_textual_seen) strcpy(vbuf.v_seen, buf);
666 client_getln(vbuf.v_answered, sizeof vbuf.v_answered);
667 client_getln(buf, sizeof buf); vbuf.v_flags = atoi(buf);
668 client_getln(buf, sizeof buf); vbuf.v_view = atoi(buf);
670 lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
671 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
676 void artv_import_message(void) {
682 char tempfile[PATH_MAX];
685 memset(&smi, 0, sizeof(struct MetaData));
686 client_getln(buf, sizeof buf); msgnum = atol(buf);
687 smi.meta_msgnum = msgnum;
688 client_getln(buf, sizeof buf); smi.meta_refcount = atoi(buf);
689 client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
691 lprintf(CTDL_INFO, "message #%ld\n", msgnum);
693 /* decode base64 message text */
694 CtdlMakeTempFileName(tempfile, sizeof tempfile);
695 snprintf(buf, sizeof buf, "%s -d >%s", file_base64, tempfile);
696 fp = popen(buf, "w");
697 while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
698 fprintf(fp, "%s\n", buf);
701 fp = fopen(tempfile, "rb");
702 fseek(fp, 0L, SEEK_END);
705 lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
707 mbuf = malloc(msglen);
708 fp = fopen(tempfile, "rb");
709 fread(mbuf, msglen, 1, fp);
712 cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
718 lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
724 void artv_do_import(void) {
733 cprintf("%d sock it to me\n", SEND_LISTING);
737 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
739 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
740 if ((abuf[0] == '\0') || (strcasecmp(buf, abuf))) {
741 cprintf ("\n\nImporting datatype %s\n", buf);
742 strncpy (abuf, buf, SIZ);
747 if (iterations % 64 == 0)
752 if (!strcasecmp(buf, "version")) {
753 client_getln(s_version, sizeof s_version);
754 version = atoi(s_version);
755 if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
756 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
760 else if (!strcasecmp(buf, "config")) artv_import_config();
761 else if (!strcasecmp(buf, "control")) artv_import_control();
762 else if (!strcasecmp(buf, "user")) artv_import_user();
763 else if (!strcasecmp(buf, "room")) artv_import_room();
764 else if (!strcasecmp(buf, "floor")) artv_import_floor();
765 else if (!strcasecmp(buf, "visit")) artv_import_visit();
766 else if (!strcasecmp(buf, "message")) artv_import_message();
770 lprintf(CTDL_INFO, "Invalid keyword <%s>. Flushing input.\n", buf);
771 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) ;;
772 rebuild_euid_index();
777 void cmd_artv(char *cmdbuf) {
779 static int is_running = 0;
781 if (CtdlAccessCheck(ac_internal)) return;
783 cprintf("%d The importer/exporter is already running.\n",
784 ERROR + RESOURCE_BUSY);
789 CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
790 CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
792 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
793 if (!strcasecmp(cmd, "export")) artv_do_export();
794 else if (!strcasecmp(cmd, "import")) artv_do_import();
795 else if (!strcasecmp(cmd, "dump")) artv_do_dump();
796 else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
798 unlink(artv_tempfilename1);
799 unlink(artv_tempfilename2);
807 CTDL_MODULE_INIT(vandelay)
809 CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
811 /* return our Subversion id for the Log */