0627187a6b4c1fc1768a45b3336d4c699eb2d8e2
[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, 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         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);
321
322         /* Export the control file */
323         get_control();
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);
330
331         artv_export_users();
332         artv_export_rooms();
333         artv_export_floors();
334         artv_export_visits();
335         artv_export_messages();
336
337         cprintf("000\n");
338 }
339
340
341
342 void artv_import_config(void) {
343         char buf[SIZ];
344
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 */
404         put_config();
405         lprintf(CTDL_INFO, "Imported config file\n");
406 }
407
408
409
410 void artv_import_control(void) {
411         char buf[SIZ];
412
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 */
420         put_control();
421         lprintf(CTDL_INFO, "Imported control file\n");
422 }
423
424
425 void artv_import_user(void) {
426         char buf[SIZ];
427         struct ctdluser usbuf;
428
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);
442         putuser(&usbuf);
443 }
444
445
446 void artv_import_room(void) {
447         char buf[SIZ];
448         struct ctdlroom qrbuf;
449         long msgnum;
450         int msgcount = 0;
451
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);
468         putroom(&qrbuf);
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.
472          */
473         while (client_getln(buf, sizeof buf), msgnum = atol(buf), msgnum > 0) {
474                 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
475                 ++msgcount;
476         }
477         lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
478 }
479
480
481 void artv_import_floor(void) {
482         struct floor flbuf;
483         int i;
484         char buf[SIZ];
485
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);
492         putfloor(&flbuf, i);
493         lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
494 }
495
496
497 /* 
498  */
499 void artv_import_visit(void) {
500         struct visit vbuf;
501         char buf[SIZ];
502         int i;
503         int is_textual_seen = 0;
504
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);
508
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);
513
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);
517         put_visit(&vbuf);
518         lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
519                 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
520 }
521
522
523
524 void artv_import_message(void) {
525         struct MetaData smi;
526         long msgnum;
527         long msglen;
528         FILE *fp;
529         char buf[SIZ];
530         char tempfile[PATH_MAX];
531         char *mbuf;
532
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);
538
539         lprintf(CTDL_INFO, "message #%ld\n", msgnum);
540
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);
547         }
548         pclose(fp);
549         fp = fopen(tempfile, "rb");
550         fseek(fp, 0L, SEEK_END);
551         msglen = ftell(fp);
552         fclose(fp);
553         lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
554
555         mbuf = malloc(msglen);
556         fp = fopen(tempfile, "rb");
557         fread(mbuf, msglen, 1, fp);
558         fclose(fp);
559
560         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
561
562         free(mbuf);
563         unlink(tempfile);
564
565         PutMetaData(&smi);
566         lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
567 }
568
569
570
571
572 void artv_do_import(void) {
573         char buf[SIZ];
574         char s_version[SIZ];
575         int version;
576
577         unbuffer_output();
578
579         cprintf("%d sock it to me\n", SEND_LISTING);
580         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
581
582                 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
583
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");
589                                 break;
590                         }
591                 }
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();
599                 else break;
600
601         }
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();
605 }
606
607
608
609 void cmd_artv(char *cmdbuf) {
610         char cmd[32];
611         static int is_running = 0;
612
613         if (CtdlAccessCheck(ac_internal)) return;
614         if (is_running) {
615                 cprintf("%d The importer/exporter is already running.\n",
616                         ERROR + RESOURCE_BUSY);
617                 return;
618         }
619         is_running = 1;
620
621         CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
622         CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
623
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);
628
629         unlink(artv_tempfilename1);
630         unlink(artv_tempfilename2);
631
632         is_running = 0;
633 }
634
635
636
637
638 char *serv_vandelay_init(void)
639 {
640         CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
641         return "$Id$";
642 }