4 * This is the "Art Vandelay" module. It is an importer/exporter.
16 #include <sys/types.h>
24 #include "sysdep_decls.h"
25 #include "citserver.h"
28 #include "dynloader.h"
36 #define END_OF_MESSAGE "---eom---dbd---"
38 char artv_tempfilename1[PATH_MAX];
39 char artv_tempfilename2[PATH_MAX];
40 FILE *artv_global_message_list;
42 void artv_export_users_backend(struct usersupp *usbuf, void *data) {
44 cprintf("%d\n", usbuf->version);
45 cprintf("%d\n", usbuf->uid);
46 cprintf("%s\n", usbuf->password);
47 cprintf("%u\n", usbuf->flags);
48 cprintf("%ld\n", usbuf->timescalled);
49 cprintf("%ld\n", usbuf->posted);
50 cprintf("%d\n", usbuf->axlevel);
51 cprintf("%ld\n", usbuf->usernum);
52 cprintf("%ld\n", usbuf->lastcall);
53 cprintf("%d\n", usbuf->USuserpurge);
54 cprintf("%s\n", usbuf->fullname);
55 cprintf("%d\n", usbuf->USscreenwidth);
56 cprintf("%d\n", usbuf->USscreenheight);
57 cprintf("%d\n", usbuf->moderation_filter);
61 void artv_export_users(void) {
62 ForEachUser(artv_export_users_backend, NULL);
66 void artv_export_room_msg(long msgnum, void *userdata) {
67 cprintf("%ld\n", msgnum);
68 fprintf(artv_global_message_list, "%ld\n", msgnum);
72 void artv_export_rooms_backend(struct quickroom *qrbuf, void *data) {
74 cprintf("%s\n", qrbuf->QRname);
75 cprintf("%s\n", qrbuf->QRpasswd);
76 cprintf("%ld\n", qrbuf->QRroomaide);
77 cprintf("%ld\n", qrbuf->QRhighest);
78 cprintf("%ld\n", qrbuf->QRgen);
79 cprintf("%u\n", qrbuf->QRflags);
80 cprintf("%s\n", qrbuf->QRdirname);
81 cprintf("%ld\n", qrbuf->QRinfo);
82 cprintf("%d\n", qrbuf->QRfloor);
83 cprintf("%ld\n", qrbuf->QRmtime);
84 cprintf("%d\n", qrbuf->QRep.expire_mode);
85 cprintf("%d\n", qrbuf->QRep.expire_value);
86 cprintf("%ld\n", qrbuf->QRnumber);
87 cprintf("%d\n", qrbuf->QRorder);
89 getroom(&CC->quickroom, qrbuf->QRname);
90 /* format of message list export is all message numbers output
91 * one per line terminated by a 0.
93 CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
94 artv_export_room_msg, NULL);
101 void artv_export_rooms(void) {
103 artv_global_message_list = fopen(artv_tempfilename1, "w");
104 ForEachRoom(artv_export_rooms_backend, NULL);
105 fclose(artv_global_message_list);
108 * Process the 'global' message list. (Sort it and remove dups.
109 * Dups are ok because a message may be in more than one room, but
110 * this will be handled by exporting the reference count, not by
111 * exporting the message multiple times.)
113 sprintf(cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
115 sprintf(cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
120 void artv_export_floors(void) {
124 for (i=0; i < MAXFLOORS; ++i) {
128 cprintf("%u\n", flbuf.f_flags);
129 cprintf("%s\n", flbuf.f_name);
130 cprintf("%d\n", flbuf.f_ref_count);
131 cprintf("%d\n", flbuf.f_ep.expire_mode);
132 cprintf("%d\n", flbuf.f_ep.expire_value);
141 * Traverse the visits file...
143 void artv_export_visits(void) {
145 struct cdbdata *cdbv;
147 cdb_begin_transaction();
148 cdb_rewind(CDB_VISIT);
150 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
151 memset(&vbuf, 0, sizeof(struct visit));
152 memcpy(&vbuf, cdbv->ptr,
153 ((cdbv->len > sizeof(struct visit)) ?
154 sizeof(struct visit) : cdbv->len));
158 cprintf("%ld\n", vbuf.v_roomnum);
159 cprintf("%ld\n", vbuf.v_roomgen);
160 cprintf("%ld\n", vbuf.v_usernum);
161 cprintf("%ld\n", vbuf.v_lastseen);
162 cprintf("%u\n", vbuf.v_flags);
164 cdb_end_transaction();
168 void artv_export_message(long msgnum) {
169 struct SuppMsgInfo smi;
170 struct CtdlMessage *msg;
176 msg = CtdlFetchMessage(msgnum);
177 if (msg == NULL) return; /* fail silently */
179 cprintf("message\n");
180 GetSuppMsgInfo(&smi, msgnum);
181 cprintf("%ld\n", msgnum);
182 cprintf("%d\n", smi.smi_refcount);
183 cprintf("%s\n", smi.smi_content_type);
184 cprintf("%d\n", smi.smi_mod);
186 serialize_message(&smr, msg);
187 CtdlFreeMessage(msg);
189 /* write it in base64 */
190 strcpy(tempfile, tmpnam(NULL));
191 sprintf(buf, "./base64 -e >%s", tempfile);
192 fp = popen(buf, "w");
193 fwrite(smr.ser, smr.len, 1, fp);
198 fp = fopen(tempfile, "r");
200 while (fgets(buf, sizeof(buf), fp) != NULL) {
201 buf[strlen(buf)-1] = 0;
202 cprintf("%s\n", buf);
205 cprintf("%s\n", END_OF_MESSAGE);
210 void artv_export_messages(void) {
215 artv_global_message_list = fopen(artv_tempfilename1, "r");
216 lprintf(7, "Opened %s\n", artv_tempfilename1);
217 while (fgets(buf, sizeof(buf), artv_global_message_list) != NULL) {
220 artv_export_message(msgnum);
224 fclose(artv_global_message_list);
225 lprintf(7, "Exported %ld messages.\n", count);
231 void artv_do_export(void) {
232 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
234 cprintf("version\n%d\n", REV_LEVEL);
236 /* export the config file */
238 cprintf("%s\n", config.c_nodename);
239 cprintf("%s\n", config.c_fqdn);
240 cprintf("%s\n", config.c_humannode);
241 cprintf("%s\n", config.c_phonenum);
242 cprintf("%d\n", config.c_bbsuid);
243 cprintf("%d\n", config.c_creataide);
244 cprintf("%d\n", config.c_sleeping);
245 cprintf("%d\n", config.c_initax);
246 cprintf("%d\n", config.c_regiscall);
247 cprintf("%d\n", config.c_twitdetect);
248 cprintf("%s\n", config.c_twitroom);
249 cprintf("%s\n", config.c_moreprompt);
250 cprintf("%d\n", config.c_restrict);
251 cprintf("%ld\n", config.c_msgbase);
252 cprintf("%s\n", config.c_bbs_city);
253 cprintf("%s\n", config.c_sysadm);
254 cprintf("%s\n", config.c_bucket_dir);
255 cprintf("%d\n", config.c_setup_level);
256 cprintf("%d\n", config.c_maxsessions);
257 cprintf("%s\n", config.c_net_password);
258 cprintf("%d\n", config.c_port_number);
259 cprintf("%d\n", config.c_ipgm_secret);
260 cprintf("%d\n", config.c_ep.expire_mode);
261 cprintf("%d\n", config.c_ep.expire_value);
262 cprintf("%d\n", config.c_userpurge);
263 cprintf("%d\n", config.c_roompurge);
264 cprintf("%s\n", config.c_logpages);
265 cprintf("%d\n", config.c_createax);
266 cprintf("%ld\n", config.c_maxmsglen);
267 cprintf("%d\n", config.c_min_workers);
268 cprintf("%d\n", config.c_max_workers);
269 cprintf("%d\n", config.c_pop3_port);
270 cprintf("%d\n", config.c_smtp_port);
271 cprintf("%d\n", config.c_default_filter);
273 /* Export the control file */
275 cprintf("control\n");
276 cprintf("%ld\n", CitControl.MMhighest);
277 cprintf("%u\n", CitControl.MMflags);
278 cprintf("%ld\n", CitControl.MMnextuser);
279 cprintf("%ld\n", CitControl.MMnextroom);
280 cprintf("%d\n", CitControl.version);
284 artv_export_floors();
285 artv_export_visits();
286 artv_export_messages();
293 void artv_import_config(void) {
296 lprintf(9, "Importing config file\n");
297 client_gets(config.c_nodename);
298 lprintf(9, "c_nodename = %s\n", config.c_nodename);
299 client_gets(config.c_fqdn);
300 client_gets(config.c_humannode);
301 client_gets(config.c_phonenum);
302 client_gets(buf); config.c_bbsuid = atoi(buf);
303 client_gets(buf); config.c_creataide = atoi(buf);
304 client_gets(buf); config.c_sleeping = atoi(buf);
305 client_gets(buf); config.c_initax = atoi(buf);
306 client_gets(buf); config.c_regiscall = atoi(buf);
307 client_gets(buf); config.c_twitdetect = atoi(buf);
308 client_gets(config.c_twitroom);
309 client_gets(config.c_moreprompt);
310 client_gets(buf); config.c_restrict = atoi(buf);
311 client_gets(buf); config.c_msgbase = atol(buf);
312 client_gets(config.c_bbs_city);
313 client_gets(config.c_sysadm);
314 lprintf(9, "c_sysadm = %s\n", config.c_sysadm);
315 client_gets(config.c_bucket_dir);
316 client_gets(buf); config.c_setup_level = atoi(buf);
317 client_gets(buf); config.c_maxsessions = atoi(buf);
318 client_gets(config.c_net_password);
319 client_gets(buf); config.c_port_number = atoi(buf);
320 client_gets(buf); config.c_ipgm_secret = atoi(buf);
321 client_gets(buf); config.c_ep.expire_mode = atoi(buf);
322 client_gets(buf); config.c_ep.expire_value = atoi(buf);
323 client_gets(buf); config.c_userpurge = atoi(buf);
324 client_gets(buf); config.c_roompurge = atoi(buf);
325 client_gets(config.c_logpages);
326 client_gets(buf); config.c_createax = atoi(buf);
327 client_gets(buf); config.c_maxmsglen = atol(buf);
328 client_gets(buf); config.c_min_workers = atoi(buf);
329 client_gets(buf); config.c_max_workers = atoi(buf);
330 client_gets(buf); config.c_pop3_port = atoi(buf);
331 client_gets(buf); config.c_smtp_port = atoi(buf);
332 client_gets(buf); config.c_default_filter = atoi(buf);
334 lprintf(7, "Imported config file\n");
339 void artv_import_control(void) {
342 lprintf(9, "Importing control file\n");
343 client_gets(buf); CitControl.MMhighest = atol(buf);
344 client_gets(buf); CitControl.MMflags = atoi(buf);
345 client_gets(buf); CitControl.MMnextuser = atol(buf);
346 client_gets(buf); CitControl.MMnextroom = atol(buf);
347 client_gets(buf); CitControl.version = atoi(buf);
349 lprintf(7, "Imported control file\n");
353 void artv_import_user(void) {
355 struct usersupp usbuf;
357 client_gets(buf); usbuf.version = atoi(buf);
358 client_gets(buf); usbuf.uid = atoi(buf);
359 client_gets(usbuf.password);
360 client_gets(buf); usbuf.flags = atoi(buf);
361 client_gets(buf); usbuf.timescalled = atol(buf);
362 client_gets(buf); usbuf.posted = atol(buf);
363 client_gets(buf); usbuf.axlevel = atoi(buf);
364 client_gets(buf); usbuf.usernum = atol(buf);
365 client_gets(buf); usbuf.lastcall = atol(buf);
366 client_gets(buf); usbuf.USuserpurge = atoi(buf);
367 client_gets(usbuf.fullname);
368 client_gets(buf); usbuf.USscreenwidth = atoi(buf);
369 client_gets(buf); usbuf.USscreenheight = atoi(buf);
370 client_gets(buf); usbuf.moderation_filter = atoi(buf);
375 void artv_import_room(void) {
377 struct quickroom qrbuf;
381 client_gets(qrbuf.QRname);
382 client_gets(qrbuf.QRpasswd);
383 client_gets(buf); qrbuf.QRroomaide = atol(buf);
384 client_gets(buf); qrbuf.QRhighest = atol(buf);
385 client_gets(buf); qrbuf.QRgen = atol(buf);
386 client_gets(buf); qrbuf.QRflags = atoi(buf);
387 client_gets(qrbuf.QRdirname);
388 client_gets(buf); qrbuf.QRinfo = atol(buf);
389 client_gets(buf); qrbuf.QRfloor = atoi(buf);
390 client_gets(buf); qrbuf.QRmtime = atol(buf);
391 client_gets(buf); qrbuf.QRep.expire_mode = atoi(buf);
392 client_gets(buf); qrbuf.QRep.expire_value = atoi(buf);
393 client_gets(buf); qrbuf.QRnumber = atol(buf);
394 client_gets(buf); qrbuf.QRorder = atoi(buf);
396 lprintf(7, "Imported room <%s>\n", qrbuf.QRname);
397 /* format of message list export is all message numbers output
398 * one per line terminated by a 0.
400 while (client_gets(buf), msgnum = atol(buf), msgnum > 0) {
401 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0);
404 lprintf(7, "(%d messages)\n", msgcount);
408 void artv_import_floor(void) {
413 client_gets(buf); i = atoi(buf);
414 client_gets(buf); flbuf.f_flags = atoi(buf);
415 client_gets(flbuf.f_name);
416 client_gets(buf); flbuf.f_ref_count = atoi(buf);
417 client_gets(buf); flbuf.f_ep.expire_mode = atoi(buf);
418 client_gets(buf); flbuf.f_ep.expire_value = atoi(buf);
420 lprintf(7, "Imported floor #%d (%s)\n", i, flbuf.f_name);
426 void artv_import_visit(void) {
430 client_gets(buf); vbuf.v_roomnum = atol(buf);
431 client_gets(buf); vbuf.v_roomgen = atol(buf);
432 client_gets(buf); vbuf.v_usernum = atol(buf);
433 client_gets(buf); vbuf.v_lastseen = atol(buf);
434 client_gets(buf); vbuf.v_flags = atoi(buf);
436 lprintf(7, "Imported visit %ld/%ld/%ld\n",
437 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
442 void artv_import_message(void) {
443 struct SuppMsgInfo smi;
451 memset(&smi, 0, sizeof(struct SuppMsgInfo));
452 client_gets(buf); msgnum = atol(buf);
453 smi.smi_msgnum = msgnum;
454 client_gets(buf); smi.smi_refcount = atoi(buf);
455 client_gets(smi.smi_content_type);
456 client_gets(buf); smi.smi_mod = atoi(buf);
458 lprintf(7, "message #%ld\n", msgnum);
460 /* decode base64 message text */
461 strcpy(tempfile, tmpnam(NULL));
462 sprintf(buf, "./base64 -d >%s", tempfile);
463 fp = popen(buf, "w");
464 while (client_gets(buf), strcasecmp(buf, END_OF_MESSAGE)) {
465 fprintf(fp, "%s\n", buf);
468 fp = fopen(tempfile, "rb");
469 fseek(fp, 0L, SEEK_END);
472 lprintf(9, "msglen = %ld\n", msglen);
474 mbuf = mallok(msglen);
475 fp = fopen(tempfile, "rb");
476 fread(mbuf, msglen, 1, fp);
479 cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
484 PutSuppMsgInfo(&smi);
485 lprintf(7, "Imported message %ld\n", msgnum);
491 void artv_do_import(void) {
496 cprintf("%d sock it to me\n", SEND_LISTING);
497 while (client_gets(buf), strcmp(buf, "000")) {
499 lprintf(9, "import keyword: <%s>\n", buf);
501 if (!strcasecmp(buf, "version")) {
502 client_gets(s_version);
503 version = atoi(s_version);
504 if ((version < REV_MIN) || (version > REV_LEVEL)) {
505 lprintf(7, "Version mismatch - aborting\n");
509 else if (!strcasecmp(buf, "config")) artv_import_config();
510 else if (!strcasecmp(buf, "control")) artv_import_control();
511 else if (!strcasecmp(buf, "user")) artv_import_user();
512 else if (!strcasecmp(buf, "room")) artv_import_room();
513 else if (!strcasecmp(buf, "floor")) artv_import_floor();
514 else if (!strcasecmp(buf, "visit")) artv_import_visit();
515 else if (!strcasecmp(buf, "message")) artv_import_message();
519 lprintf(7, "Invalid keyword <%s>. Flushing input.\n", buf);
520 while (client_gets(buf), strcmp(buf, "000")) ;;
525 void cmd_artv(char *cmdbuf) {
527 static int is_running = 0;
529 if (CtdlAccessCheck(ac_aide)) return; /* FIXME should be intpgm */
531 cprintf("%d The importer/exporter is already running.\n",
537 strcpy(artv_tempfilename1, tmpnam(NULL));
538 strcpy(artv_tempfilename2, tmpnam(NULL));
540 extract(cmd, cmdbuf, 0);
541 if (!strcasecmp(cmd, "export")) artv_do_export();
542 else if (!strcasecmp(cmd, "import")) artv_do_import();
543 else cprintf("%d illegal command\n", ERROR);
545 unlink(artv_tempfilename1);
546 unlink(artv_tempfilename2);
554 char *Dynamic_Module_Init(void)
556 CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");