501e5b74f5163d8d29074a5751f4945a589ae511
[citadel.git] / citadel / serv_vandelay.c
1 /*
2  * $Id$
3  *
4  * This is the "Art Vandelay" module.  It is an importer/exporter.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "serv_extensions.h"
40 #include "database.h"
41 #include "msgbase.h"
42 #include "tools.h"
43 #include "user_ops.h"
44 #include "room_ops.h"
45 #include "control.h"
46 #include "euidindex.h"
47
48 #define END_OF_MESSAGE  "---eom---dbd---"
49
50 char artv_tempfilename1[PATH_MAX];
51 char artv_tempfilename2[PATH_MAX];
52 FILE *artv_global_message_list;
53
54 void artv_export_users_backend(struct ctdluser *usbuf, void *data) {
55         cprintf("user\n");
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);
69 }
70
71
72 void artv_export_users(void) {
73         ForEachUser(artv_export_users_backend, NULL);
74 }
75
76
77 void artv_export_room_msg(long msgnum, void *userdata) {
78         cprintf("%ld\n", msgnum);
79         fprintf(artv_global_message_list, "%ld\n", msgnum);
80 }
81
82
83 void artv_export_rooms_backend(struct ctdlroom *qrbuf, void *data) {
84         cprintf("room\n");
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);
101
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.
105          */
106         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
107                 artv_export_room_msg, NULL);
108         cprintf("0\n");
109
110 }
111
112
113
114 void artv_export_rooms(void) {
115         char cmd[SIZ];
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);
120         }
121
122         /*
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.)
127          */
128         snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
129         system(cmd);
130         snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
131         system(cmd);
132 }
133
134
135 void artv_export_floors(void) {
136         struct floor flbuf;
137         int i;
138
139         for (i=0; i < MAXFLOORS; ++i) {
140                 cprintf("floor\n");
141                 cprintf("%d\n", i);
142                 getfloor(&flbuf, 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);
148         }
149 }
150
151
152
153
154
155 /* 
156  *  Traverse the visits file...
157  */
158 void artv_export_visits(void) {
159         struct visit vbuf;
160         struct cdbdata *cdbv;
161
162         cdb_rewind(CDB_VISIT);
163
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));
169                 cdb_free(cdbv);
170
171                 cprintf("visit\n");
172                 cprintf("%ld\n", vbuf.v_roomnum);
173                 cprintf("%ld\n", vbuf.v_roomgen);
174                 cprintf("%ld\n", vbuf.v_usernum);
175
176                 if (strlen(vbuf.v_seen) > 0) {
177                         cprintf("%s\n", vbuf.v_seen);
178                 }
179                 else {
180                         cprintf("%ld\n", vbuf.v_lastseen);
181                 }
182
183                 cprintf("%s\n", vbuf.v_answered);
184                 cprintf("%u\n", vbuf.v_flags);
185                 cprintf("%d\n", vbuf.v_view);
186         }
187 }
188
189
190 void artv_export_message(long msgnum) {
191         struct MetaData smi;
192         struct CtdlMessage *msg;
193         struct ser_ret smr;
194         FILE *fp;
195         char buf[SIZ];
196         char tempfile[PATH_MAX];
197
198         msg = CtdlFetchMessage(msgnum, 1);
199         if (msg == NULL) return;        /* fail silently */
200
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);
206
207         serialize_message(&smr, msg);
208         CtdlFreeMessage(msg);
209
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);
215         pclose(fp);
216
217         free(smr.ser);
218
219         fp = fopen(tempfile, "r");
220         unlink(tempfile);
221         if (fp != NULL) {
222                 while (fgets(buf, sizeof(buf), fp) != NULL) {
223                         buf[strlen(buf)-1] = 0;
224                         cprintf("%s\n", buf);
225                 }
226                 fclose(fp);
227         }
228         cprintf("%s\n", END_OF_MESSAGE);
229 }
230
231
232
233 void artv_export_messages(void) {
234         char buf[SIZ];
235         long msgnum;
236         int count = 0;
237
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) {
243                         msgnum = atol(buf);
244                         if (msgnum > 0L) {
245                                 artv_export_message(msgnum);
246                                 ++count;
247                         }
248                 }
249                 fclose(artv_global_message_list);
250         }
251         lprintf(CTDL_INFO, "Exported %d messages.\n", count);
252 }
253
254
255
256
257 void artv_do_export(void) {
258         cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
259
260         cprintf("version\n%d\n", REV_LEVEL);
261
262         /* export the config file */
263         cprintf("config\n");
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
317         /* Export the control file */
318         get_control();
319         cprintf("control\n");
320         cprintf("%ld\n", CitControl.MMhighest);
321         cprintf("%u\n", CitControl.MMflags);
322         cprintf("%ld\n", CitControl.MMnextuser);
323         cprintf("%ld\n", CitControl.MMnextroom);
324         cprintf("%d\n", CitControl.version);
325
326         artv_export_users();
327         artv_export_rooms();
328         artv_export_floors();
329         artv_export_visits();
330         artv_export_messages();
331
332         cprintf("000\n");
333 }
334
335
336
337 void artv_import_config(void) {
338         char buf[SIZ];
339
340         lprintf(CTDL_DEBUG, "Importing config file\n");
341         client_getln(config.c_nodename, sizeof config.c_nodename);
342         client_getln(config.c_fqdn, sizeof config.c_fqdn);
343         client_getln(config.c_humannode, sizeof config.c_humannode);
344         client_getln(config.c_phonenum, sizeof config.c_phonenum);
345         client_getln(buf, sizeof buf);  config.c_ctdluid = atoi(buf);
346         client_getln(buf, sizeof buf);  config.c_creataide = atoi(buf);
347         client_getln(buf, sizeof buf);  config.c_sleeping = atoi(buf);
348         client_getln(buf, sizeof buf);  config.c_initax = atoi(buf);
349         client_getln(buf, sizeof buf);  config.c_regiscall = atoi(buf);
350         client_getln(buf, sizeof buf);  config.c_twitdetect = atoi(buf);
351         client_getln(config.c_twitroom, sizeof config.c_twitroom);
352         client_getln(config.c_moreprompt, sizeof config.c_moreprompt);
353         client_getln(buf, sizeof buf);  config.c_restrict = atoi(buf);
354         client_getln(config.c_site_location, sizeof config.c_site_location);
355         client_getln(config.c_sysadm, sizeof config.c_sysadm);
356         client_getln(buf, sizeof buf);  config.c_setup_level = atoi(buf);
357         client_getln(buf, sizeof buf);  config.c_maxsessions = atoi(buf);
358         client_getln(buf, sizeof buf);  config.c_port_number = atoi(buf);
359         client_getln(buf, sizeof buf);  config.c_ep.expire_mode = atoi(buf);
360         client_getln(buf, sizeof buf);  config.c_ep.expire_value = atoi(buf);
361         client_getln(buf, sizeof buf);  config.c_userpurge = atoi(buf);
362         client_getln(buf, sizeof buf);  config.c_roompurge = atoi(buf);
363         client_getln(config.c_logpages, sizeof config.c_logpages);
364         client_getln(buf, sizeof buf);  config.c_createax = atoi(buf);
365         client_getln(buf, sizeof buf);  config.c_maxmsglen = atol(buf);
366         client_getln(buf, sizeof buf);  config.c_min_workers = atoi(buf);
367         client_getln(buf, sizeof buf);  config.c_max_workers = atoi(buf);
368         client_getln(buf, sizeof buf);  config.c_pop3_port = atoi(buf);
369         client_getln(buf, sizeof buf);  config.c_smtp_port = atoi(buf);
370         client_getln(buf, sizeof buf);  config.c_purge_hour = atoi(buf);
371         client_getln(buf, sizeof buf);  config.c_mbxep.expire_mode = atoi(buf);
372         client_getln(buf, sizeof buf);  config.c_mbxep.expire_value = atoi(buf);
373         client_getln(config.c_ldap_host, sizeof config.c_ldap_host);
374         client_getln(buf, sizeof buf);  config.c_ldap_port = atoi(buf);
375         client_getln(config.c_ldap_base_dn, sizeof config.c_ldap_base_dn);
376         client_getln(config.c_ldap_bind_dn, sizeof config.c_ldap_bind_dn);
377         client_getln(config.c_ldap_bind_pw, sizeof config.c_ldap_bind_pw);
378         client_getln(config.c_ip_addr, sizeof config.c_ip_addr);
379         client_getln(buf, sizeof buf);  config.c_msa_port = atoi(buf);
380         client_getln(buf, sizeof buf);  config.c_imaps_port = atoi(buf);
381         client_getln(buf, sizeof buf);  config.c_pop3s_port = atoi(buf);
382         client_getln(buf, sizeof buf);  config.c_smtps_port = atoi(buf);
383         client_getln(buf, sizeof buf);  config.c_rfc822_strict_from = atoi(buf);
384         client_getln(buf, sizeof buf);  config.c_aide_zap = atoi(buf);
385         client_getln(buf, sizeof buf);  config.c_imap_port = atoi(buf);
386         client_getln(buf, sizeof buf);  config.c_net_freq = atol(buf);
387         client_getln(buf, sizeof buf);  config.c_disable_newu = atoi(buf);
388         client_getln(config.c_baseroom, sizeof config.c_baseroom);
389         client_getln(config.c_aideroom, sizeof config.c_aideroom);
390         client_getln(buf, sizeof buf);  config.c_auto_cull = atoi(buf);
391         client_getln(buf, sizeof buf);  config.c_instant_expunge = atoi(buf);
392         client_getln(buf, sizeof buf);  config.c_allow_spoofing = atoi(buf);
393         config.c_enable_fulltext = 0;   /* always disable */
394         put_config();
395         lprintf(CTDL_INFO, "Imported config file\n");
396 }
397
398
399
400 void artv_import_control(void) {
401         char buf[SIZ];
402
403         lprintf(CTDL_DEBUG, "Importing control file\n");
404         client_getln(buf, sizeof buf);  CitControl.MMhighest = atol(buf);
405         client_getln(buf, sizeof buf);  CitControl.MMflags = atoi(buf);
406         client_getln(buf, sizeof buf);  CitControl.MMnextuser = atol(buf);
407         client_getln(buf, sizeof buf);  CitControl.MMnextroom = atol(buf);
408         client_getln(buf, sizeof buf);  CitControl.version = atoi(buf);
409         CitControl.MMfulltext = (-1L);  /* always flush */
410         put_control();
411         lprintf(CTDL_INFO, "Imported control file\n");
412 }
413
414
415 void artv_import_user(void) {
416         char buf[SIZ];
417         struct ctdluser usbuf;
418
419         client_getln(buf, sizeof buf);  usbuf.version = atoi(buf);
420         client_getln(buf, sizeof buf);  usbuf.uid = atoi(buf);
421         client_getln(usbuf.password, sizeof usbuf.password);
422         client_getln(buf, sizeof buf);  usbuf.flags = atoi(buf);
423         client_getln(buf, sizeof buf);  usbuf.timescalled = atol(buf);
424         client_getln(buf, sizeof buf);  usbuf.posted = atol(buf);
425         client_getln(buf, sizeof buf);  usbuf.axlevel = atoi(buf);
426         client_getln(buf, sizeof buf);  usbuf.usernum = atol(buf);
427         client_getln(buf, sizeof buf);  usbuf.lastcall = atol(buf);
428         client_getln(buf, sizeof buf);  usbuf.USuserpurge = atoi(buf);
429         client_getln(usbuf.fullname, sizeof usbuf.fullname);
430         client_getln(buf, sizeof buf);  usbuf.USscreenwidth = atoi(buf);
431         client_getln(buf, sizeof buf);  usbuf.USscreenheight = atoi(buf);
432         putuser(&usbuf);
433 }
434
435
436 void artv_import_room(void) {
437         char buf[SIZ];
438         struct ctdlroom qrbuf;
439         long msgnum;
440         int msgcount = 0;
441
442         client_getln(qrbuf.QRname, sizeof qrbuf.QRname);
443         client_getln(qrbuf.QRpasswd, sizeof qrbuf.QRpasswd);
444         client_getln(buf, sizeof buf);  qrbuf.QRroomaide = atol(buf);
445         client_getln(buf, sizeof buf);  qrbuf.QRhighest = atol(buf);
446         client_getln(buf, sizeof buf);  qrbuf.QRgen = atol(buf);
447         client_getln(buf, sizeof buf);  qrbuf.QRflags = atoi(buf);
448         client_getln(qrbuf.QRdirname, sizeof qrbuf.QRdirname);
449         client_getln(buf, sizeof buf);  qrbuf.QRinfo = atol(buf);
450         client_getln(buf, sizeof buf);  qrbuf.QRfloor = atoi(buf);
451         client_getln(buf, sizeof buf);  qrbuf.QRmtime = atol(buf);
452         client_getln(buf, sizeof buf);  qrbuf.QRep.expire_mode = atoi(buf);
453         client_getln(buf, sizeof buf);  qrbuf.QRep.expire_value = atoi(buf);
454         client_getln(buf, sizeof buf);  qrbuf.QRnumber = atol(buf);
455         client_getln(buf, sizeof buf);  qrbuf.QRorder = atoi(buf);
456         client_getln(buf, sizeof buf);  qrbuf.QRflags2 = atoi(buf);
457         client_getln(buf, sizeof buf);  qrbuf.QRdefaultview = atoi(buf);
458         putroom(&qrbuf);
459         lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
460         /* format of message list export is all message numbers output
461          * one per line terminated by a 0.
462          */
463         while (client_getln(buf, sizeof buf), msgnum = atol(buf), msgnum > 0) {
464                 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
465                 ++msgcount;
466         }
467         lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
468 }
469
470
471 void artv_import_floor(void) {
472         struct floor flbuf;
473         int i;
474         char buf[SIZ];
475
476         client_getln(buf, sizeof buf);  i = atoi(buf);
477         client_getln(buf, sizeof buf);  flbuf.f_flags = atoi(buf);
478         client_getln(flbuf.f_name, sizeof flbuf.f_name);
479         client_getln(buf, sizeof buf);  flbuf.f_ref_count = atoi(buf);
480         client_getln(buf, sizeof buf);  flbuf.f_ep.expire_mode = atoi(buf);
481         client_getln(buf, sizeof buf);  flbuf.f_ep.expire_value = atoi(buf);
482         putfloor(&flbuf, i);
483         lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
484 }
485
486
487 /* 
488  */
489 void artv_import_visit(void) {
490         struct visit vbuf;
491         char buf[SIZ];
492         int i;
493         int is_textual_seen = 0;
494
495         client_getln(buf, sizeof buf);  vbuf.v_roomnum = atol(buf);
496         client_getln(buf, sizeof buf);  vbuf.v_roomgen = atol(buf);
497         client_getln(buf, sizeof buf);  vbuf.v_usernum = atol(buf);
498
499         client_getln(buf, sizeof buf);
500         vbuf.v_lastseen = atol(buf);
501         for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
502         if (is_textual_seen)    strcpy(vbuf.v_seen, buf);
503
504         client_getln(vbuf.v_answered, sizeof vbuf.v_answered);
505         client_getln(buf, sizeof buf);  vbuf.v_flags = atoi(buf);
506         client_getln(buf, sizeof buf);  vbuf.v_view = atoi(buf);
507         put_visit(&vbuf);
508         lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
509                 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
510 }
511
512
513
514 void artv_import_message(void) {
515         struct MetaData smi;
516         long msgnum;
517         long msglen;
518         FILE *fp;
519         char buf[SIZ];
520         char tempfile[PATH_MAX];
521         char *mbuf;
522
523         memset(&smi, 0, sizeof(struct MetaData));
524         client_getln(buf, sizeof buf);  msgnum = atol(buf);
525                                 smi.meta_msgnum = msgnum;
526         client_getln(buf, sizeof buf);  smi.meta_refcount = atoi(buf);
527         client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
528
529         lprintf(CTDL_INFO, "message #%ld\n", msgnum);
530
531         /* decode base64 message text */
532         CtdlMakeTempFileName(tempfile, sizeof tempfile);
533         snprintf(buf, sizeof buf, "./base64 -d >%s", tempfile);
534         fp = popen(buf, "w");
535         while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
536                 fprintf(fp, "%s\n", buf);
537         }
538         pclose(fp);
539         fp = fopen(tempfile, "rb");
540         fseek(fp, 0L, SEEK_END);
541         msglen = ftell(fp);
542         fclose(fp);
543         lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
544
545         mbuf = malloc(msglen);
546         fp = fopen(tempfile, "rb");
547         fread(mbuf, msglen, 1, fp);
548         fclose(fp);
549
550         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
551
552         free(mbuf);
553         unlink(tempfile);
554
555         PutMetaData(&smi);
556         lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
557 }
558
559
560
561
562 void artv_do_import(void) {
563         char buf[SIZ];
564         char s_version[SIZ];
565         int version;
566
567         unbuffer_output();
568
569         cprintf("%d sock it to me\n", SEND_LISTING);
570         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
571
572                 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
573
574                 if (!strcasecmp(buf, "version")) {
575                         client_getln(s_version, sizeof s_version);
576                         version = atoi(s_version);
577                         if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
578                                 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
579                                 break;
580                         }
581                 }
582                 else if (!strcasecmp(buf, "config")) artv_import_config();
583                 else if (!strcasecmp(buf, "control")) artv_import_control();
584                 else if (!strcasecmp(buf, "user")) artv_import_user();
585                 else if (!strcasecmp(buf, "room")) artv_import_room();
586                 else if (!strcasecmp(buf, "floor")) artv_import_floor();
587                 else if (!strcasecmp(buf, "visit")) artv_import_visit();
588                 else if (!strcasecmp(buf, "message")) artv_import_message();
589                 else break;
590
591         }
592         lprintf(CTDL_INFO, "Invalid keyword <%s>.  Flushing input.\n", buf);
593         while (client_getln(buf, sizeof buf), strcmp(buf, "000"))  ;;
594         rebuild_euid_index();
595 }
596
597
598
599 void cmd_artv(char *cmdbuf) {
600         char cmd[32];
601         static int is_running = 0;
602
603         if (CtdlAccessCheck(ac_internal)) return;
604         if (is_running) {
605                 cprintf("%d The importer/exporter is already running.\n",
606                         ERROR + RESOURCE_BUSY);
607                 return;
608         }
609         is_running = 1;
610
611         CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
612         CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
613
614         extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
615         if (!strcasecmp(cmd, "export")) artv_do_export();
616         else if (!strcasecmp(cmd, "import")) artv_do_import();
617         else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
618
619         unlink(artv_tempfilename1);
620         unlink(artv_tempfilename2);
621
622         is_running = 0;
623 }
624
625
626
627
628 char *serv_vandelay_init(void)
629 {
630         CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
631         return "$Id$";
632 }