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"
47 #define END_OF_MESSAGE "---eom---dbd---"
49 char artv_tempfilename1[PATH_MAX];
50 char artv_tempfilename2[PATH_MAX];
51 FILE *artv_global_message_list;
53 void artv_export_users_backend(struct ctdluser *usbuf, void *data) {
55 cprintf("%d\n", usbuf->version);
56 cprintf("%ld\n", (long)usbuf->uid);
57 cprintf("%s\n", usbuf->password);
58 cprintf("%u\n", usbuf->flags);
59 cprintf("%ld\n", usbuf->timescalled);
60 cprintf("%ld\n", usbuf->posted);
61 cprintf("%d\n", usbuf->axlevel);
62 cprintf("%ld\n", usbuf->usernum);
63 cprintf("%ld\n", (long)usbuf->lastcall);
64 cprintf("%d\n", usbuf->USuserpurge);
65 cprintf("%s\n", usbuf->fullname);
66 cprintf("%d\n", usbuf->USscreenwidth);
67 cprintf("%d\n", usbuf->USscreenheight);
71 void artv_export_users(void) {
72 ForEachUser(artv_export_users_backend, NULL);
76 void artv_export_room_msg(long msgnum, void *userdata) {
77 cprintf("%ld\n", msgnum);
78 fprintf(artv_global_message_list, "%ld\n", msgnum);
82 void artv_export_rooms_backend(struct ctdlroom *qrbuf, void *data) {
84 cprintf("%s\n", qrbuf->QRname);
85 cprintf("%s\n", qrbuf->QRpasswd);
86 cprintf("%ld\n", qrbuf->QRroomaide);
87 cprintf("%ld\n", qrbuf->QRhighest);
88 cprintf("%ld\n", (long)qrbuf->QRgen);
89 cprintf("%u\n", qrbuf->QRflags);
90 cprintf("%s\n", qrbuf->QRdirname);
91 cprintf("%ld\n", qrbuf->QRinfo);
92 cprintf("%d\n", qrbuf->QRfloor);
93 cprintf("%ld\n", (long)qrbuf->QRmtime);
94 cprintf("%d\n", qrbuf->QRep.expire_mode);
95 cprintf("%d\n", qrbuf->QRep.expire_value);
96 cprintf("%ld\n", qrbuf->QRnumber);
97 cprintf("%d\n", qrbuf->QRorder);
98 cprintf("%u\n", qrbuf->QRflags2);
99 cprintf("%d\n", qrbuf->QRdefaultview);
101 getroom(&CC->room, qrbuf->QRname);
102 /* format of message list export is all message numbers output
103 * one per line terminated by a 0.
105 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
106 artv_export_room_msg, NULL);
113 void artv_export_rooms(void) {
115 artv_global_message_list = fopen(artv_tempfilename1, "w");
116 if (artv_global_message_list != NULL) {
117 ForEachRoom(artv_export_rooms_backend, NULL);
118 fclose(artv_global_message_list);
122 * Process the 'global' message list. (Sort it and remove dups.
123 * Dups are ok because a message may be in more than one room, but
124 * this will be handled by exporting the reference count, not by
125 * exporting the message multiple times.)
127 snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
129 snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
134 void artv_export_floors(void) {
138 for (i=0; i < MAXFLOORS; ++i) {
142 cprintf("%u\n", flbuf.f_flags);
143 cprintf("%s\n", flbuf.f_name);
144 cprintf("%d\n", flbuf.f_ref_count);
145 cprintf("%d\n", flbuf.f_ep.expire_mode);
146 cprintf("%d\n", flbuf.f_ep.expire_value);
155 * Traverse the visits file...
157 void artv_export_visits(void) {
159 struct cdbdata *cdbv;
161 cdb_rewind(CDB_VISIT);
163 while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
164 memset(&vbuf, 0, sizeof(struct visit));
165 memcpy(&vbuf, cdbv->ptr,
166 ((cdbv->len > sizeof(struct visit)) ?
167 sizeof(struct visit) : cdbv->len));
171 cprintf("%ld\n", vbuf.v_roomnum);
172 cprintf("%ld\n", vbuf.v_roomgen);
173 cprintf("%ld\n", vbuf.v_usernum);
175 if (strlen(vbuf.v_seen) > 0) {
176 cprintf("%s\n", vbuf.v_seen);
179 cprintf("%ld\n", vbuf.v_lastseen);
182 cprintf("%s\n", vbuf.v_answered);
183 cprintf("%u\n", vbuf.v_flags);
184 cprintf("%d\n", vbuf.v_view);
189 void artv_export_message(long msgnum) {
191 struct CtdlMessage *msg;
197 msg = CtdlFetchMessage(msgnum, 1);
198 if (msg == NULL) return; /* fail silently */
200 cprintf("message\n");
201 GetMetaData(&smi, msgnum);
202 cprintf("%ld\n", msgnum);
203 cprintf("%d\n", smi.meta_refcount);
204 cprintf("%s\n", smi.meta_content_type);
206 serialize_message(&smr, msg);
207 CtdlFreeMessage(msg);
209 /* write it in base64 */
210 strcpy(tempfile, tmpnam(NULL));
211 snprintf(buf, sizeof buf, "./base64 -e >%s", tempfile);
212 fp = popen(buf, "w");
213 fwrite(smr.ser, smr.len, 1, fp);
218 fp = fopen(tempfile, "r");
221 while (fgets(buf, sizeof(buf), fp) != NULL) {
222 buf[strlen(buf)-1] = 0;
223 cprintf("%s\n", buf);
227 cprintf("%s\n", END_OF_MESSAGE);
232 void artv_export_messages(void) {
237 artv_global_message_list = fopen(artv_tempfilename1, "r");
238 if (artv_global_message_list != NULL) {
239 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
240 while (fgets(buf, sizeof(buf),
241 artv_global_message_list) != NULL) {
244 artv_export_message(msgnum);
248 fclose(artv_global_message_list);
250 lprintf(CTDL_INFO, "Exported %d messages.\n", count);
256 void artv_do_export(void) {
257 cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
259 cprintf("version\n%d\n", REV_LEVEL);
261 /* export the config file */
263 cprintf("%s\n", config.c_nodename);
264 cprintf("%s\n", config.c_fqdn);
265 cprintf("%s\n", config.c_humannode);
266 cprintf("%s\n", config.c_phonenum);
267 cprintf("%ld\n", (long)config.c_ctdluid);
268 cprintf("%d\n", config.c_creataide);
269 cprintf("%d\n", config.c_sleeping);
270 cprintf("%d\n", config.c_initax);
271 cprintf("%d\n", config.c_regiscall);
272 cprintf("%d\n", config.c_twitdetect);
273 cprintf("%s\n", config.c_twitroom);
274 cprintf("%s\n", config.c_moreprompt);
275 cprintf("%d\n", config.c_restrict);
276 cprintf("%s\n", config.c_site_location);
277 cprintf("%s\n", config.c_sysadm);
278 cprintf("%d\n", config.c_setup_level);
279 cprintf("%d\n", config.c_maxsessions);
280 cprintf("%d\n", config.c_port_number);
281 cprintf("%d\n", config.c_ep.expire_mode);
282 cprintf("%d\n", config.c_ep.expire_value);
283 cprintf("%d\n", config.c_userpurge);
284 cprintf("%d\n", config.c_roompurge);
285 cprintf("%s\n", config.c_logpages);
286 cprintf("%d\n", config.c_createax);
287 cprintf("%ld\n", config.c_maxmsglen);
288 cprintf("%d\n", config.c_min_workers);
289 cprintf("%d\n", config.c_max_workers);
290 cprintf("%d\n", config.c_pop3_port);
291 cprintf("%d\n", config.c_smtp_port);
292 cprintf("%d\n", config.c_purge_hour);
293 cprintf("%d\n", config.c_mbxep.expire_mode);
294 cprintf("%d\n", config.c_mbxep.expire_value);
295 cprintf("%s\n", config.c_ldap_host);
296 cprintf("%d\n", config.c_ldap_port);
297 cprintf("%s\n", config.c_ldap_base_dn);
298 cprintf("%s\n", config.c_ldap_bind_dn);
299 cprintf("%s\n", config.c_ldap_bind_pw);
300 cprintf("%s\n", config.c_ip_addr);
301 cprintf("%d\n", config.c_msa_port);
302 cprintf("%d\n", config.c_imaps_port);
303 cprintf("%d\n", config.c_pop3s_port);
304 cprintf("%d\n", config.c_smtps_port);
306 /* Export the control file */
308 cprintf("control\n");
309 cprintf("%ld\n", CitControl.MMhighest);
310 cprintf("%u\n", CitControl.MMflags);
311 cprintf("%ld\n", CitControl.MMnextuser);
312 cprintf("%ld\n", CitControl.MMnextroom);
313 cprintf("%d\n", CitControl.version);
317 artv_export_floors();
318 artv_export_visits();
319 artv_export_messages();
326 void artv_import_config(void) {
329 lprintf(CTDL_DEBUG, "Importing config file\n");
330 client_getln(config.c_nodename, sizeof config.c_nodename);
331 client_getln(config.c_fqdn, sizeof config.c_fqdn);
332 client_getln(config.c_humannode, sizeof config.c_humannode);
333 client_getln(config.c_phonenum, sizeof config.c_phonenum);
334 client_getln(buf, sizeof buf); config.c_ctdluid = atoi(buf);
335 client_getln(buf, sizeof buf); config.c_creataide = atoi(buf);
336 client_getln(buf, sizeof buf); config.c_sleeping = atoi(buf);
337 client_getln(buf, sizeof buf); config.c_initax = atoi(buf);
338 client_getln(buf, sizeof buf); config.c_regiscall = atoi(buf);
339 client_getln(buf, sizeof buf); config.c_twitdetect = atoi(buf);
340 client_getln(config.c_twitroom, sizeof config.c_twitroom);
341 client_getln(config.c_moreprompt, sizeof config.c_moreprompt);
342 client_getln(buf, sizeof buf); config.c_restrict = atoi(buf);
343 client_getln(config.c_site_location, sizeof config.c_site_location);
344 client_getln(config.c_sysadm, sizeof config.c_sysadm);
345 client_getln(buf, sizeof buf); config.c_setup_level = atoi(buf);
346 client_getln(buf, sizeof buf); config.c_maxsessions = atoi(buf);
347 client_getln(buf, sizeof buf); config.c_port_number = atoi(buf);
348 client_getln(buf, sizeof buf); config.c_ep.expire_mode = atoi(buf);
349 client_getln(buf, sizeof buf); config.c_ep.expire_value = atoi(buf);
350 client_getln(buf, sizeof buf); config.c_userpurge = atoi(buf);
351 client_getln(buf, sizeof buf); config.c_roompurge = atoi(buf);
352 client_getln(config.c_logpages, sizeof config.c_logpages);
353 client_getln(buf, sizeof buf); config.c_createax = atoi(buf);
354 client_getln(buf, sizeof buf); config.c_maxmsglen = atol(buf);
355 client_getln(buf, sizeof buf); config.c_min_workers = atoi(buf);
356 client_getln(buf, sizeof buf); config.c_max_workers = atoi(buf);
357 client_getln(buf, sizeof buf); config.c_pop3_port = atoi(buf);
358 client_getln(buf, sizeof buf); config.c_smtp_port = atoi(buf);
359 client_getln(buf, sizeof buf); config.c_purge_hour = atoi(buf);
360 client_getln(buf, sizeof buf); config.c_mbxep.expire_mode = atoi(buf);
361 client_getln(buf, sizeof buf); config.c_mbxep.expire_value = atoi(buf);
362 client_getln(config.c_ldap_host, sizeof config.c_ldap_host);
363 client_getln(buf, sizeof buf); config.c_ldap_port = atoi(buf);
364 client_getln(config.c_ldap_base_dn, sizeof config.c_ldap_base_dn);
365 client_getln(config.c_ldap_bind_dn, sizeof config.c_ldap_bind_dn);
366 client_getln(config.c_ldap_bind_pw, sizeof config.c_ldap_bind_pw);
367 client_getln(config.c_ip_addr, sizeof config.c_ip_addr);
368 client_getln(buf, sizeof buf); config.c_msa_port = atoi(buf);
369 client_getln(buf, sizeof buf); config.c_imaps_port = atoi(buf);
370 client_getln(buf, sizeof buf); config.c_pop3s_port = atoi(buf);
371 client_getln(buf, sizeof buf); config.c_smtps_port = atoi(buf);
373 lprintf(CTDL_INFO, "Imported config file\n");
378 void artv_import_control(void) {
381 lprintf(CTDL_DEBUG, "Importing control file\n");
382 client_getln(buf, sizeof buf); CitControl.MMhighest = atol(buf);
383 client_getln(buf, sizeof buf); CitControl.MMflags = atoi(buf);
384 client_getln(buf, sizeof buf); CitControl.MMnextuser = atol(buf);
385 client_getln(buf, sizeof buf); CitControl.MMnextroom = atol(buf);
386 client_getln(buf, sizeof buf); CitControl.version = atoi(buf);
388 lprintf(CTDL_INFO, "Imported control file\n");
392 void artv_import_user(void) {
394 struct ctdluser usbuf;
396 client_getln(buf, sizeof buf); usbuf.version = atoi(buf);
397 client_getln(buf, sizeof buf); usbuf.uid = atoi(buf);
398 client_getln(usbuf.password, sizeof usbuf.password);
399 client_getln(buf, sizeof buf); usbuf.flags = atoi(buf);
400 client_getln(buf, sizeof buf); usbuf.timescalled = atol(buf);
401 client_getln(buf, sizeof buf); usbuf.posted = atol(buf);
402 client_getln(buf, sizeof buf); usbuf.axlevel = atoi(buf);
403 client_getln(buf, sizeof buf); usbuf.usernum = atol(buf);
404 client_getln(buf, sizeof buf); usbuf.lastcall = atol(buf);
405 client_getln(buf, sizeof buf); usbuf.USuserpurge = atoi(buf);
406 client_getln(usbuf.fullname, sizeof usbuf.fullname);
407 client_getln(buf, sizeof buf); usbuf.USscreenwidth = atoi(buf);
408 client_getln(buf, sizeof buf); usbuf.USscreenheight = atoi(buf);
413 void artv_import_room(void) {
415 struct ctdlroom qrbuf;
419 client_getln(qrbuf.QRname, sizeof qrbuf.QRname);
420 client_getln(qrbuf.QRpasswd, sizeof qrbuf.QRpasswd);
421 client_getln(buf, sizeof buf); qrbuf.QRroomaide = atol(buf);
422 client_getln(buf, sizeof buf); qrbuf.QRhighest = atol(buf);
423 client_getln(buf, sizeof buf); qrbuf.QRgen = atol(buf);
424 client_getln(buf, sizeof buf); qrbuf.QRflags = atoi(buf);
425 client_getln(qrbuf.QRdirname, sizeof qrbuf.QRdirname);
426 client_getln(buf, sizeof buf); qrbuf.QRinfo = atol(buf);
427 client_getln(buf, sizeof buf); qrbuf.QRfloor = atoi(buf);
428 client_getln(buf, sizeof buf); qrbuf.QRmtime = atol(buf);
429 client_getln(buf, sizeof buf); qrbuf.QRep.expire_mode = atoi(buf);
430 client_getln(buf, sizeof buf); qrbuf.QRep.expire_value = atoi(buf);
431 client_getln(buf, sizeof buf); qrbuf.QRnumber = atol(buf);
432 client_getln(buf, sizeof buf); qrbuf.QRorder = atoi(buf);
433 client_getln(buf, sizeof buf); qrbuf.QRflags2 = atoi(buf);
434 client_getln(buf, sizeof buf); qrbuf.QRdefaultview = atoi(buf);
436 lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
437 /* format of message list export is all message numbers output
438 * one per line terminated by a 0.
440 while (client_getln(buf, sizeof buf), msgnum = atol(buf), msgnum > 0) {
441 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0);
444 lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
448 void artv_import_floor(void) {
453 client_getln(buf, sizeof buf); i = atoi(buf);
454 client_getln(buf, sizeof buf); flbuf.f_flags = atoi(buf);
455 client_getln(flbuf.f_name, sizeof flbuf.f_name);
456 client_getln(buf, sizeof buf); flbuf.f_ref_count = atoi(buf);
457 client_getln(buf, sizeof buf); flbuf.f_ep.expire_mode = atoi(buf);
458 client_getln(buf, sizeof buf); flbuf.f_ep.expire_value = atoi(buf);
460 lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
466 void artv_import_visit(void) {
470 int is_textual_seen = 0;
472 client_getln(buf, sizeof buf); vbuf.v_roomnum = atol(buf);
473 client_getln(buf, sizeof buf); vbuf.v_roomgen = atol(buf);
474 client_getln(buf, sizeof buf); vbuf.v_usernum = atol(buf);
476 client_getln(buf, sizeof buf);
477 vbuf.v_lastseen = atol(buf);
478 for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
479 if (is_textual_seen) strcpy(vbuf.v_seen, buf);
481 client_getln(vbuf.v_answered, sizeof vbuf.v_answered);
482 client_getln(buf, sizeof buf); vbuf.v_flags = atoi(buf);
483 client_getln(buf, sizeof buf); vbuf.v_view = atoi(buf);
485 lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
486 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
491 void artv_import_message(void) {
500 memset(&smi, 0, sizeof(struct MetaData));
501 client_getln(buf, sizeof buf); msgnum = atol(buf);
502 smi.meta_msgnum = msgnum;
503 client_getln(buf, sizeof buf); smi.meta_refcount = atoi(buf);
504 client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
506 lprintf(CTDL_INFO, "message #%ld\n", msgnum);
508 /* decode base64 message text */
509 strcpy(tempfile, tmpnam(NULL));
510 snprintf(buf, sizeof buf, "./base64 -d >%s", tempfile);
511 fp = popen(buf, "w");
512 while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
513 fprintf(fp, "%s\n", buf);
516 fp = fopen(tempfile, "rb");
517 fseek(fp, 0L, SEEK_END);
520 lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
522 mbuf = malloc(msglen);
523 fp = fopen(tempfile, "rb");
524 fread(mbuf, msglen, 1, fp);
527 cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
533 lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
539 void artv_do_import(void) {
546 cprintf("%d sock it to me\n", SEND_LISTING);
547 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
549 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
551 if (!strcasecmp(buf, "version")) {
552 client_getln(s_version, sizeof s_version);
553 version = atoi(s_version);
554 if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
555 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
559 else if (!strcasecmp(buf, "config")) artv_import_config();
560 else if (!strcasecmp(buf, "control")) artv_import_control();
561 else if (!strcasecmp(buf, "user")) artv_import_user();
562 else if (!strcasecmp(buf, "room")) artv_import_room();
563 else if (!strcasecmp(buf, "floor")) artv_import_floor();
564 else if (!strcasecmp(buf, "visit")) artv_import_visit();
565 else if (!strcasecmp(buf, "message")) artv_import_message();
569 lprintf(CTDL_INFO, "Invalid keyword <%s>. Flushing input.\n", buf);
570 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) ;;
575 void cmd_artv(char *cmdbuf) {
577 static int is_running = 0;
579 if (CtdlAccessCheck(ac_internal)) return;
581 cprintf("%d The importer/exporter is already running.\n",
582 ERROR + RESOURCE_BUSY);
587 strcpy(artv_tempfilename1, tmpnam(NULL));
588 strcpy(artv_tempfilename2, tmpnam(NULL));
590 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
591 if (!strcasecmp(cmd, "export")) artv_do_export();
592 else if (!strcasecmp(cmd, "import")) artv_do_import();
593 else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
595 unlink(artv_tempfilename1);
596 unlink(artv_tempfilename2);
604 char *serv_vandelay_init(void)
606 CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");