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 "sysdep_decls.h"
36 #include "citserver.h"
39 #include "serv_extensions.h"
46 #include "euidindex.h"
48 #define END_OF_MESSAGE "---eom---dbd---"
50 char artv_tempfilename1[PATH_MAX];
51 char artv_tempfilename2[PATH_MAX];
52 FILE *artv_global_message_list;
54 void artv_export_users_backend(struct ctdluser *usbuf, void *data) {
56 cprintf("%d\n", usbuf->version);
57 cprintf("%ld\n", (long)usbuf->uid);
58 cprintf("%s\n", usbuf->password);
59 cprintf("%u\n", usbuf->flags);
60 cprintf("%ld\n", usbuf->timescalled);
61 cprintf("%ld\n", usbuf->posted);
62 cprintf("%d\n", usbuf->axlevel);
63 cprintf("%ld\n", usbuf->usernum);
64 cprintf("%ld\n", (long)usbuf->lastcall);
65 cprintf("%d\n", usbuf->USuserpurge);
66 cprintf("%s\n", usbuf->fullname);
67 cprintf("%d\n", usbuf->USscreenwidth);
68 cprintf("%d\n", usbuf->USscreenheight);
72 void artv_export_users(void) {
73 ForEachUser(artv_export_users_backend, NULL);
77 void artv_export_room_msg(long msgnum, void *userdata) {
78 cprintf("%ld\n", msgnum);
79 fprintf(artv_global_message_list, "%ld\n", msgnum);
83 void artv_export_rooms_backend(struct ctdlroom *qrbuf, void *data) {
85 cprintf("%s\n", qrbuf->QRname);
86 cprintf("%s\n", qrbuf->QRpasswd);
87 cprintf("%ld\n", qrbuf->QRroomaide);
88 cprintf("%ld\n", qrbuf->QRhighest);
89 cprintf("%ld\n", (long)qrbuf->QRgen);
90 cprintf("%u\n", qrbuf->QRflags);
91 cprintf("%s\n", qrbuf->QRdirname);
92 cprintf("%ld\n", qrbuf->QRinfo);
93 cprintf("%d\n", qrbuf->QRfloor);
94 cprintf("%ld\n", (long)qrbuf->QRmtime);
95 cprintf("%d\n", qrbuf->QRep.expire_mode);
96 cprintf("%d\n", qrbuf->QRep.expire_value);
97 cprintf("%ld\n", qrbuf->QRnumber);
98 cprintf("%d\n", qrbuf->QRorder);
99 cprintf("%u\n", qrbuf->QRflags2);
100 cprintf("%d\n", qrbuf->QRdefaultview);
102 getroom(&CC->room, qrbuf->QRname);
103 /* format of message list export is all message numbers output
104 * one per line terminated by a 0.
106 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
107 artv_export_room_msg, NULL);
114 void artv_export_rooms(void) {
116 artv_global_message_list = fopen(artv_tempfilename1, "w");
117 if (artv_global_message_list != NULL) {
118 ForEachRoom(artv_export_rooms_backend, NULL);
119 fclose(artv_global_message_list);
123 * Process the 'global' message list. (Sort it and remove dups.
124 * Dups are ok because a message may be in more than one room, but
125 * this will be handled by exporting the reference count, not by
126 * exporting the message multiple times.)
128 snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
130 snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
135 void artv_export_floors(void) {
139 for (i=0; i < MAXFLOORS; ++i) {
143 cprintf("%u\n", flbuf.f_flags);
144 cprintf("%s\n", flbuf.f_name);
145 cprintf("%d\n", flbuf.f_ref_count);
146 cprintf("%d\n", flbuf.f_ep.expire_mode);
147 cprintf("%d\n", flbuf.f_ep.expire_value);
156 * Traverse the visits file...
158 void artv_export_visits(void) {
160 struct cdbdata *cdbv;
162 cdb_rewind(CDB_VISIT);
164 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
165 memset(&vbuf, 0, sizeof(struct visit));
166 memcpy(&vbuf, cdbv->ptr,
167 ((cdbv->len > sizeof(struct visit)) ?
168 sizeof(struct visit) : cdbv->len));
172 cprintf("%ld\n", vbuf.v_roomnum);
173 cprintf("%ld\n", vbuf.v_roomgen);
174 cprintf("%ld\n", vbuf.v_usernum);
176 if (strlen(vbuf.v_seen) > 0) {
177 cprintf("%s\n", vbuf.v_seen);
180 cprintf("%ld\n", vbuf.v_lastseen);
183 cprintf("%s\n", vbuf.v_answered);
184 cprintf("%u\n", vbuf.v_flags);
185 cprintf("%d\n", vbuf.v_view);
190 void artv_export_message(long msgnum) {
192 struct CtdlMessage *msg;
196 char tempfile[PATH_MAX];
198 msg = CtdlFetchMessage(msgnum, 1);
199 if (msg == NULL) return; /* fail silently */
201 cprintf("message\n");
202 GetMetaData(&smi, msgnum);
203 cprintf("%ld\n", msgnum);
204 cprintf("%d\n", smi.meta_refcount);
205 cprintf("%s\n", smi.meta_content_type);
207 serialize_message(&smr, msg);
208 CtdlFreeMessage(msg);
210 /* write it in base64 */
211 CtdlMakeTempFileName(tempfile, sizeof tempfile);
212 snprintf(buf, sizeof buf, "./base64 -e >%s", tempfile);
213 fp = popen(buf, "w");
214 fwrite(smr.ser, smr.len, 1, fp);
219 fp = fopen(tempfile, "r");
222 while (fgets(buf, sizeof(buf), fp) != NULL) {
223 buf[strlen(buf)-1] = 0;
224 cprintf("%s\n", buf);
228 cprintf("%s\n", END_OF_MESSAGE);
233 void artv_export_messages(void) {
238 artv_global_message_list = fopen(artv_tempfilename1, "r");
239 if (artv_global_message_list != NULL) {
240 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
241 while (fgets(buf, sizeof(buf),
242 artv_global_message_list) != NULL) {
245 artv_export_message(msgnum);
249 fclose(artv_global_message_list);
251 lprintf(CTDL_INFO, "Exported %d messages.\n", count);
257 void artv_do_export(void) {
258 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
260 cprintf("version\n%d\n", REV_LEVEL);
262 /* export the config file */
264 cprintf("%s\n", config.c_nodename);
265 cprintf("%s\n", config.c_fqdn);
266 cprintf("%s\n", config.c_humannode);
267 cprintf("%s\n", config.c_phonenum);
268 cprintf("%ld\n", (long)config.c_ctdluid);
269 cprintf("%d\n", config.c_creataide);
270 cprintf("%d\n", config.c_sleeping);
271 cprintf("%d\n", config.c_initax);
272 cprintf("%d\n", config.c_regiscall);
273 cprintf("%d\n", config.c_twitdetect);
274 cprintf("%s\n", config.c_twitroom);
275 cprintf("%s\n", config.c_moreprompt);
276 cprintf("%d\n", config.c_restrict);
277 cprintf("%s\n", config.c_site_location);
278 cprintf("%s\n", config.c_sysadm);
279 cprintf("%d\n", config.c_setup_level);
280 cprintf("%d\n", config.c_maxsessions);
281 cprintf("%d\n", config.c_port_number);
282 cprintf("%d\n", config.c_ep.expire_mode);
283 cprintf("%d\n", config.c_ep.expire_value);
284 cprintf("%d\n", config.c_userpurge);
285 cprintf("%d\n", config.c_roompurge);
286 cprintf("%s\n", config.c_logpages);
287 cprintf("%d\n", config.c_createax);
288 cprintf("%ld\n", config.c_maxmsglen);
289 cprintf("%d\n", config.c_min_workers);
290 cprintf("%d\n", config.c_max_workers);
291 cprintf("%d\n", config.c_pop3_port);
292 cprintf("%d\n", config.c_smtp_port);
293 cprintf("%d\n", config.c_purge_hour);
294 cprintf("%d\n", config.c_mbxep.expire_mode);
295 cprintf("%d\n", config.c_mbxep.expire_value);
296 cprintf("%s\n", config.c_ldap_host);
297 cprintf("%d\n", config.c_ldap_port);
298 cprintf("%s\n", config.c_ldap_base_dn);
299 cprintf("%s\n", config.c_ldap_bind_dn);
300 cprintf("%s\n", config.c_ldap_bind_pw);
301 cprintf("%s\n", config.c_ip_addr);
302 cprintf("%d\n", config.c_msa_port);
303 cprintf("%d\n", config.c_imaps_port);
304 cprintf("%d\n", config.c_pop3s_port);
305 cprintf("%d\n", config.c_smtps_port);
306 cprintf("%d\n", config.c_rfc822_strict_from);
307 cprintf("%d\n", config.c_aide_zap);
308 cprintf("%d\n", config.c_imap_port);
309 cprintf("%ld\n", config.c_net_freq);
310 cprintf("%d\n", config.c_disable_newu);
311 cprintf("%s\n", config.c_baseroom);
312 cprintf("%s\n", config.c_aideroom);
313 cprintf("%d\n", config.c_auto_cull);
314 cprintf("%d\n", config.c_instant_expunge);
315 cprintf("%d\n", config.c_allow_spoofing);
316 cprintf("%d\n", config.c_journal_email);
317 cprintf("%d\n", config.c_journal_pubmsgs);
318 cprintf("%s\n", config.c_journal_dest);
319 cprintf("%s\n", config.c_default_cal_zone);
320 cprintf("%d\n", config.c_pftcpdict_port);
322 /* Export the control file */
324 cprintf("control\n");
325 cprintf("%ld\n", CitControl.MMhighest);
326 cprintf("%u\n", CitControl.MMflags);
327 cprintf("%ld\n", CitControl.MMnextuser);
328 cprintf("%ld\n", CitControl.MMnextroom);
329 cprintf("%d\n", CitControl.version);
333 artv_export_floors();
334 artv_export_visits();
335 artv_export_messages();
342 void artv_import_config(void) {
345 lprintf(CTDL_DEBUG, "Importing config file\n");
346 client_getln(config.c_nodename, sizeof config.c_nodename);
347 client_getln(config.c_fqdn, sizeof config.c_fqdn);
348 client_getln(config.c_humannode, sizeof config.c_humannode);
349 client_getln(config.c_phonenum, sizeof config.c_phonenum);
350 client_getln(buf, sizeof buf); config.c_ctdluid = atoi(buf);
351 client_getln(buf, sizeof buf); config.c_creataide = atoi(buf);
352 client_getln(buf, sizeof buf); config.c_sleeping = atoi(buf);
353 client_getln(buf, sizeof buf); config.c_initax = atoi(buf);
354 client_getln(buf, sizeof buf); config.c_regiscall = atoi(buf);
355 client_getln(buf, sizeof buf); config.c_twitdetect = atoi(buf);
356 client_getln(config.c_twitroom, sizeof config.c_twitroom);
357 client_getln(config.c_moreprompt, sizeof config.c_moreprompt);
358 client_getln(buf, sizeof buf); config.c_restrict = atoi(buf);
359 client_getln(config.c_site_location, sizeof config.c_site_location);
360 client_getln(config.c_sysadm, sizeof config.c_sysadm);
361 client_getln(buf, sizeof buf); config.c_setup_level = atoi(buf);
362 client_getln(buf, sizeof buf); config.c_maxsessions = atoi(buf);
363 client_getln(buf, sizeof buf); config.c_port_number = atoi(buf);
364 client_getln(buf, sizeof buf); config.c_ep.expire_mode = atoi(buf);
365 client_getln(buf, sizeof buf); config.c_ep.expire_value = atoi(buf);
366 client_getln(buf, sizeof buf); config.c_userpurge = atoi(buf);
367 client_getln(buf, sizeof buf); config.c_roompurge = atoi(buf);
368 client_getln(config.c_logpages, sizeof config.c_logpages);
369 client_getln(buf, sizeof buf); config.c_createax = atoi(buf);
370 client_getln(buf, sizeof buf); config.c_maxmsglen = atol(buf);
371 client_getln(buf, sizeof buf); config.c_min_workers = atoi(buf);
372 client_getln(buf, sizeof buf); config.c_max_workers = atoi(buf);
373 client_getln(buf, sizeof buf); config.c_pop3_port = atoi(buf);
374 client_getln(buf, sizeof buf); config.c_smtp_port = atoi(buf);
375 client_getln(buf, sizeof buf); config.c_purge_hour = atoi(buf);
376 client_getln(buf, sizeof buf); config.c_mbxep.expire_mode = atoi(buf);
377 client_getln(buf, sizeof buf); config.c_mbxep.expire_value = atoi(buf);
378 client_getln(config.c_ldap_host, sizeof config.c_ldap_host);
379 client_getln(buf, sizeof buf); config.c_ldap_port = atoi(buf);
380 client_getln(config.c_ldap_base_dn, sizeof config.c_ldap_base_dn);
381 client_getln(config.c_ldap_bind_dn, sizeof config.c_ldap_bind_dn);
382 client_getln(config.c_ldap_bind_pw, sizeof config.c_ldap_bind_pw);
383 client_getln(config.c_ip_addr, sizeof config.c_ip_addr);
384 client_getln(buf, sizeof buf); config.c_msa_port = atoi(buf);
385 client_getln(buf, sizeof buf); config.c_imaps_port = atoi(buf);
386 client_getln(buf, sizeof buf); config.c_pop3s_port = atoi(buf);
387 client_getln(buf, sizeof buf); config.c_smtps_port = atoi(buf);
388 client_getln(buf, sizeof buf); config.c_rfc822_strict_from = atoi(buf);
389 client_getln(buf, sizeof buf); config.c_aide_zap = atoi(buf);
390 client_getln(buf, sizeof buf); config.c_imap_port = atoi(buf);
391 client_getln(buf, sizeof buf); config.c_net_freq = atol(buf);
392 client_getln(buf, sizeof buf); config.c_disable_newu = atoi(buf);
393 client_getln(config.c_baseroom, sizeof config.c_baseroom);
394 client_getln(config.c_aideroom, sizeof config.c_aideroom);
395 client_getln(buf, sizeof buf); config.c_auto_cull = atoi(buf);
396 client_getln(buf, sizeof buf); config.c_instant_expunge = atoi(buf);
397 client_getln(buf, sizeof buf); config.c_allow_spoofing = atoi(buf);
398 client_getln(buf, sizeof buf); config.c_journal_email = atoi(buf);
399 client_getln(buf, sizeof buf); config.c_journal_pubmsgs = atoi(buf);
400 client_getln(config.c_journal_dest, sizeof config.c_journal_dest);
401 client_getln(config.c_default_cal_zone, sizeof config.c_default_cal_zone);
402 client_getln(buf, sizeof buf); config.c_pftcpdict_port = atoi(buf);
403 config.c_enable_fulltext = 0; /* always disable */
405 lprintf(CTDL_INFO, "Imported config file\n");
410 void artv_import_control(void) {
413 lprintf(CTDL_DEBUG, "Importing control file\n");
414 client_getln(buf, sizeof buf); CitControl.MMhighest = atol(buf);
415 client_getln(buf, sizeof buf); CitControl.MMflags = atoi(buf);
416 client_getln(buf, sizeof buf); CitControl.MMnextuser = atol(buf);
417 client_getln(buf, sizeof buf); CitControl.MMnextroom = atol(buf);
418 client_getln(buf, sizeof buf); CitControl.version = atoi(buf);
419 CitControl.MMfulltext = (-1L); /* always flush */
421 lprintf(CTDL_INFO, "Imported control file\n");
425 void artv_import_user(void) {
427 struct ctdluser usbuf;
429 client_getln(buf, sizeof buf); usbuf.version = atoi(buf);
430 client_getln(buf, sizeof buf); usbuf.uid = atoi(buf);
431 client_getln(usbuf.password, sizeof usbuf.password);
432 client_getln(buf, sizeof buf); usbuf.flags = atoi(buf);
433 client_getln(buf, sizeof buf); usbuf.timescalled = atol(buf);
434 client_getln(buf, sizeof buf); usbuf.posted = atol(buf);
435 client_getln(buf, sizeof buf); usbuf.axlevel = atoi(buf);
436 client_getln(buf, sizeof buf); usbuf.usernum = atol(buf);
437 client_getln(buf, sizeof buf); usbuf.lastcall = atol(buf);
438 client_getln(buf, sizeof buf); usbuf.USuserpurge = atoi(buf);
439 client_getln(usbuf.fullname, sizeof usbuf.fullname);
440 client_getln(buf, sizeof buf); usbuf.USscreenwidth = atoi(buf);
441 client_getln(buf, sizeof buf); usbuf.USscreenheight = atoi(buf);
446 void artv_import_room(void) {
448 struct ctdlroom qrbuf;
452 client_getln(qrbuf.QRname, sizeof qrbuf.QRname);
453 client_getln(qrbuf.QRpasswd, sizeof qrbuf.QRpasswd);
454 client_getln(buf, sizeof buf); qrbuf.QRroomaide = atol(buf);
455 client_getln(buf, sizeof buf); qrbuf.QRhighest = atol(buf);
456 client_getln(buf, sizeof buf); qrbuf.QRgen = atol(buf);
457 client_getln(buf, sizeof buf); qrbuf.QRflags = atoi(buf);
458 client_getln(qrbuf.QRdirname, sizeof qrbuf.QRdirname);
459 client_getln(buf, sizeof buf); qrbuf.QRinfo = atol(buf);
460 client_getln(buf, sizeof buf); qrbuf.QRfloor = atoi(buf);
461 client_getln(buf, sizeof buf); qrbuf.QRmtime = atol(buf);
462 client_getln(buf, sizeof buf); qrbuf.QRep.expire_mode = atoi(buf);
463 client_getln(buf, sizeof buf); qrbuf.QRep.expire_value = atoi(buf);
464 client_getln(buf, sizeof buf); qrbuf.QRnumber = atol(buf);
465 client_getln(buf, sizeof buf); qrbuf.QRorder = atoi(buf);
466 client_getln(buf, sizeof buf); qrbuf.QRflags2 = atoi(buf);
467 client_getln(buf, sizeof buf); qrbuf.QRdefaultview = atoi(buf);
469 lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
470 /* format of message list export is all message numbers output
471 * one per line terminated by a 0.
473 while (client_getln(buf, sizeof buf), msgnum = atol(buf), msgnum > 0) {
474 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
477 lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
481 void artv_import_floor(void) {
486 client_getln(buf, sizeof buf); i = atoi(buf);
487 client_getln(buf, sizeof buf); flbuf.f_flags = atoi(buf);
488 client_getln(flbuf.f_name, sizeof flbuf.f_name);
489 client_getln(buf, sizeof buf); flbuf.f_ref_count = atoi(buf);
490 client_getln(buf, sizeof buf); flbuf.f_ep.expire_mode = atoi(buf);
491 client_getln(buf, sizeof buf); flbuf.f_ep.expire_value = atoi(buf);
493 lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
499 void artv_import_visit(void) {
503 int is_textual_seen = 0;
505 client_getln(buf, sizeof buf); vbuf.v_roomnum = atol(buf);
506 client_getln(buf, sizeof buf); vbuf.v_roomgen = atol(buf);
507 client_getln(buf, sizeof buf); vbuf.v_usernum = atol(buf);
509 client_getln(buf, sizeof buf);
510 vbuf.v_lastseen = atol(buf);
511 for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
512 if (is_textual_seen) strcpy(vbuf.v_seen, buf);
514 client_getln(vbuf.v_answered, sizeof vbuf.v_answered);
515 client_getln(buf, sizeof buf); vbuf.v_flags = atoi(buf);
516 client_getln(buf, sizeof buf); vbuf.v_view = atoi(buf);
518 lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
519 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
524 void artv_import_message(void) {
530 char tempfile[PATH_MAX];
533 memset(&smi, 0, sizeof(struct MetaData));
534 client_getln(buf, sizeof buf); msgnum = atol(buf);
535 smi.meta_msgnum = msgnum;
536 client_getln(buf, sizeof buf); smi.meta_refcount = atoi(buf);
537 client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
539 lprintf(CTDL_INFO, "message #%ld\n", msgnum);
541 /* decode base64 message text */
542 CtdlMakeTempFileName(tempfile, sizeof tempfile);
543 snprintf(buf, sizeof buf, "./base64 -d >%s", tempfile);
544 fp = popen(buf, "w");
545 while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
546 fprintf(fp, "%s\n", buf);
549 fp = fopen(tempfile, "rb");
550 fseek(fp, 0L, SEEK_END);
553 lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
555 mbuf = malloc(msglen);
556 fp = fopen(tempfile, "rb");
557 fread(mbuf, msglen, 1, fp);
560 cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
566 lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
572 void artv_do_import(void) {
579 cprintf("%d sock it to me\n", SEND_LISTING);
580 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
582 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
584 if (!strcasecmp(buf, "version")) {
585 client_getln(s_version, sizeof s_version);
586 version = atoi(s_version);
587 if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
588 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
592 else if (!strcasecmp(buf, "config")) artv_import_config();
593 else if (!strcasecmp(buf, "control")) artv_import_control();
594 else if (!strcasecmp(buf, "user")) artv_import_user();
595 else if (!strcasecmp(buf, "room")) artv_import_room();
596 else if (!strcasecmp(buf, "floor")) artv_import_floor();
597 else if (!strcasecmp(buf, "visit")) artv_import_visit();
598 else if (!strcasecmp(buf, "message")) artv_import_message();
602 lprintf(CTDL_INFO, "Invalid keyword <%s>. Flushing input.\n", buf);
603 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) ;;
604 rebuild_euid_index();
609 void cmd_artv(char *cmdbuf) {
611 static int is_running = 0;
613 if (CtdlAccessCheck(ac_internal)) return;
615 cprintf("%d The importer/exporter is already running.\n",
616 ERROR + RESOURCE_BUSY);
621 CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
622 CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
624 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
625 if (!strcasecmp(cmd, "export")) artv_do_export();
626 else if (!strcasecmp(cmd, "import")) artv_do_import();
627 else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
629 unlink(artv_tempfilename1);
630 unlink(artv_tempfilename2);
638 char *serv_vandelay_init(void)
640 CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");