* Applied matt's funambol patch
[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         cprintf("%d\n", config.c_managesieve_port);
322         cprintf("%d\n", config.c_auth_mode);
323         cprintf("%s\n", config.c_funambol_host);
324         cprintf("%d\n", config.c_funambol_port);
325         cprintf("%s\n", config.c_funambol_source);
326         cprintf("%s\n", config.c_funambol_auth);
327
328         /* Export the control file */
329         get_control();
330         cprintf("control\n");
331         cprintf("%ld\n", CitControl.MMhighest);
332         cprintf("%u\n", CitControl.MMflags);
333         cprintf("%ld\n", CitControl.MMnextuser);
334         cprintf("%ld\n", CitControl.MMnextroom);
335         cprintf("%d\n", CitControl.version);
336
337         artv_export_users();
338         artv_export_rooms();
339         artv_export_floors();
340         artv_export_visits();
341         artv_export_messages();
342
343         cprintf("000\n");
344 }
345
346
347
348 void artv_import_config(void) {
349         char buf[SIZ];
350
351         lprintf(CTDL_DEBUG, "Importing config file\n");
352         client_getln(config.c_nodename, sizeof config.c_nodename);
353         client_getln(config.c_fqdn, sizeof config.c_fqdn);
354         client_getln(config.c_humannode, sizeof config.c_humannode);
355         client_getln(config.c_phonenum, sizeof config.c_phonenum);
356         client_getln(buf, sizeof buf);  config.c_ctdluid = atoi(buf);
357         client_getln(buf, sizeof buf);  config.c_creataide = atoi(buf);
358         client_getln(buf, sizeof buf);  config.c_sleeping = atoi(buf);
359         client_getln(buf, sizeof buf);  config.c_initax = atoi(buf);
360         client_getln(buf, sizeof buf);  config.c_regiscall = atoi(buf);
361         client_getln(buf, sizeof buf);  config.c_twitdetect = atoi(buf);
362         client_getln(config.c_twitroom, sizeof config.c_twitroom);
363         client_getln(config.c_moreprompt, sizeof config.c_moreprompt);
364         client_getln(buf, sizeof buf);  config.c_restrict = atoi(buf);
365         client_getln(config.c_site_location, sizeof config.c_site_location);
366         client_getln(config.c_sysadm, sizeof config.c_sysadm);
367         client_getln(buf, sizeof buf);  config.c_setup_level = atoi(buf);
368         client_getln(buf, sizeof buf);  config.c_maxsessions = atoi(buf);
369         client_getln(buf, sizeof buf);  config.c_port_number = atoi(buf);
370         client_getln(buf, sizeof buf);  config.c_ep.expire_mode = atoi(buf);
371         client_getln(buf, sizeof buf);  config.c_ep.expire_value = atoi(buf);
372         client_getln(buf, sizeof buf);  config.c_userpurge = atoi(buf);
373         client_getln(buf, sizeof buf);  config.c_roompurge = atoi(buf);
374         client_getln(config.c_logpages, sizeof config.c_logpages);
375         client_getln(buf, sizeof buf);  config.c_createax = atoi(buf);
376         client_getln(buf, sizeof buf);  config.c_maxmsglen = atol(buf);
377         client_getln(buf, sizeof buf);  config.c_min_workers = atoi(buf);
378         client_getln(buf, sizeof buf);  config.c_max_workers = atoi(buf);
379         client_getln(buf, sizeof buf);  config.c_pop3_port = atoi(buf);
380         client_getln(buf, sizeof buf);  config.c_smtp_port = atoi(buf);
381         client_getln(buf, sizeof buf);  config.c_purge_hour = atoi(buf);
382         client_getln(buf, sizeof buf);  config.c_mbxep.expire_mode = atoi(buf);
383         client_getln(buf, sizeof buf);  config.c_mbxep.expire_value = atoi(buf);
384         client_getln(config.c_ldap_host, sizeof config.c_ldap_host);
385         client_getln(buf, sizeof buf);  config.c_ldap_port = atoi(buf);
386         client_getln(config.c_ldap_base_dn, sizeof config.c_ldap_base_dn);
387         client_getln(config.c_ldap_bind_dn, sizeof config.c_ldap_bind_dn);
388         client_getln(config.c_ldap_bind_pw, sizeof config.c_ldap_bind_pw);
389         client_getln(config.c_ip_addr, sizeof config.c_ip_addr);
390         client_getln(buf, sizeof buf);  config.c_msa_port = atoi(buf);
391         client_getln(buf, sizeof buf);  config.c_imaps_port = atoi(buf);
392         client_getln(buf, sizeof buf);  config.c_pop3s_port = atoi(buf);
393         client_getln(buf, sizeof buf);  config.c_smtps_port = atoi(buf);
394         client_getln(buf, sizeof buf);  config.c_rfc822_strict_from = atoi(buf);
395         client_getln(buf, sizeof buf);  config.c_aide_zap = atoi(buf);
396         client_getln(buf, sizeof buf);  config.c_imap_port = atoi(buf);
397         client_getln(buf, sizeof buf);  config.c_net_freq = atol(buf);
398         client_getln(buf, sizeof buf);  config.c_disable_newu = atoi(buf);
399         client_getln(config.c_baseroom, sizeof config.c_baseroom);
400         client_getln(config.c_aideroom, sizeof config.c_aideroom);
401         client_getln(buf, sizeof buf);  config.c_auto_cull = atoi(buf);
402         client_getln(buf, sizeof buf);  config.c_instant_expunge = atoi(buf);
403         client_getln(buf, sizeof buf);  config.c_allow_spoofing = atoi(buf);
404         client_getln(buf, sizeof buf);  config.c_journal_email = atoi(buf);
405         client_getln(buf, sizeof buf);  config.c_journal_pubmsgs = atoi(buf);
406         client_getln(config.c_journal_dest, sizeof config.c_journal_dest);
407         client_getln(config.c_default_cal_zone, sizeof config.c_default_cal_zone);
408         client_getln(buf, sizeof buf);  config.c_pftcpdict_port = atoi(buf);
409         client_getln(buf, sizeof buf);  config.c_managesieve_port = atoi(buf);
410         client_getln(buf, sizeof buf);  config.c_auth_mode = atoi(buf);
411         client_getln(config.c_funambol_host, sizeof config.c_funambol_host);
412         client_getln(buf, sizeof buf); config.c_funambol_port = atoi(buf);
413         client_getln(config.c_funambol_source, sizeof config.c_funambol_source);
414         client_getln(config.c_funambol_auth, sizeof config.c_funambol_auth);
415         
416         config.c_enable_fulltext = 0;   /* always disable */
417         put_config();
418         lprintf(CTDL_INFO, "Imported config file\n");
419 }
420
421
422 void artv_import_control(void) {
423         char buf[SIZ];
424
425         lprintf(CTDL_DEBUG, "Importing control file\n");
426         client_getln(buf, sizeof buf);  CitControl.MMhighest = atol(buf);
427         client_getln(buf, sizeof buf);  CitControl.MMflags = atoi(buf);
428         client_getln(buf, sizeof buf);  CitControl.MMnextuser = atol(buf);
429         client_getln(buf, sizeof buf);  CitControl.MMnextroom = atol(buf);
430         client_getln(buf, sizeof buf);  CitControl.version = atoi(buf);
431         CitControl.MMfulltext = (-1L);  /* always flush */
432         put_control();
433         lprintf(CTDL_INFO, "Imported control file\n");
434 }
435
436
437 void artv_import_user(void) {
438         char buf[SIZ];
439         struct ctdluser usbuf;
440
441         client_getln(buf, sizeof buf);  usbuf.version = atoi(buf);
442         client_getln(buf, sizeof buf);  usbuf.uid = atoi(buf);
443         client_getln(usbuf.password, sizeof usbuf.password);
444         client_getln(buf, sizeof buf);  usbuf.flags = atoi(buf);
445         client_getln(buf, sizeof buf);  usbuf.timescalled = atol(buf);
446         client_getln(buf, sizeof buf);  usbuf.posted = atol(buf);
447         client_getln(buf, sizeof buf);  usbuf.axlevel = atoi(buf);
448         client_getln(buf, sizeof buf);  usbuf.usernum = atol(buf);
449         client_getln(buf, sizeof buf);  usbuf.lastcall = atol(buf);
450         client_getln(buf, sizeof buf);  usbuf.USuserpurge = atoi(buf);
451         client_getln(usbuf.fullname, sizeof usbuf.fullname);
452         client_getln(buf, sizeof buf);  usbuf.USscreenwidth = atoi(buf);
453         client_getln(buf, sizeof buf);  usbuf.USscreenheight = atoi(buf);
454         putuser(&usbuf);
455 }
456
457
458 void artv_import_room(void) {
459         char buf[SIZ];
460         struct ctdlroom qrbuf;
461         long msgnum;
462         int msgcount = 0;
463
464         client_getln(qrbuf.QRname, sizeof qrbuf.QRname);
465         client_getln(qrbuf.QRpasswd, sizeof qrbuf.QRpasswd);
466         client_getln(buf, sizeof buf);  qrbuf.QRroomaide = atol(buf);
467         client_getln(buf, sizeof buf);  qrbuf.QRhighest = atol(buf);
468         client_getln(buf, sizeof buf);  qrbuf.QRgen = atol(buf);
469         client_getln(buf, sizeof buf);  qrbuf.QRflags = atoi(buf);
470         client_getln(qrbuf.QRdirname, sizeof qrbuf.QRdirname);
471         client_getln(buf, sizeof buf);  qrbuf.QRinfo = atol(buf);
472         client_getln(buf, sizeof buf);  qrbuf.QRfloor = atoi(buf);
473         client_getln(buf, sizeof buf);  qrbuf.QRmtime = atol(buf);
474         client_getln(buf, sizeof buf);  qrbuf.QRep.expire_mode = atoi(buf);
475         client_getln(buf, sizeof buf);  qrbuf.QRep.expire_value = atoi(buf);
476         client_getln(buf, sizeof buf);  qrbuf.QRnumber = atol(buf);
477         client_getln(buf, sizeof buf);  qrbuf.QRorder = atoi(buf);
478         client_getln(buf, sizeof buf);  qrbuf.QRflags2 = atoi(buf);
479         client_getln(buf, sizeof buf);  qrbuf.QRdefaultview = atoi(buf);
480         putroom(&qrbuf);
481         lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
482         /* format of message list export is all message numbers output
483          * one per line terminated by a 0.
484          */
485         while (client_getln(buf, sizeof buf), msgnum = atol(buf), msgnum > 0) {
486                 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0, NULL);
487                 ++msgcount;
488         }
489         lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
490 }
491
492
493 void artv_import_floor(void) {
494         struct floor flbuf;
495         int i;
496         char buf[SIZ];
497
498         client_getln(buf, sizeof buf);  i = atoi(buf);
499         client_getln(buf, sizeof buf);  flbuf.f_flags = atoi(buf);
500         client_getln(flbuf.f_name, sizeof flbuf.f_name);
501         client_getln(buf, sizeof buf);  flbuf.f_ref_count = atoi(buf);
502         client_getln(buf, sizeof buf);  flbuf.f_ep.expire_mode = atoi(buf);
503         client_getln(buf, sizeof buf);  flbuf.f_ep.expire_value = atoi(buf);
504         putfloor(&flbuf, i);
505         lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
506 }
507
508
509 /* 
510  */
511 void artv_import_visit(void) {
512         struct visit vbuf;
513         char buf[SIZ];
514         int i;
515         int is_textual_seen = 0;
516
517         client_getln(buf, sizeof buf);  vbuf.v_roomnum = atol(buf);
518         client_getln(buf, sizeof buf);  vbuf.v_roomgen = atol(buf);
519         client_getln(buf, sizeof buf);  vbuf.v_usernum = atol(buf);
520
521         client_getln(buf, sizeof buf);
522         vbuf.v_lastseen = atol(buf);
523         for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
524         if (is_textual_seen)    strcpy(vbuf.v_seen, buf);
525
526         client_getln(vbuf.v_answered, sizeof vbuf.v_answered);
527         client_getln(buf, sizeof buf);  vbuf.v_flags = atoi(buf);
528         client_getln(buf, sizeof buf);  vbuf.v_view = atoi(buf);
529         put_visit(&vbuf);
530         lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
531                 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
532 }
533
534
535
536 void artv_import_message(void) {
537         struct MetaData smi;
538         long msgnum;
539         long msglen;
540         FILE *fp;
541         char buf[SIZ];
542         char tempfile[PATH_MAX];
543         char *mbuf;
544
545         memset(&smi, 0, sizeof(struct MetaData));
546         client_getln(buf, sizeof buf);  msgnum = atol(buf);
547                                 smi.meta_msgnum = msgnum;
548         client_getln(buf, sizeof buf);  smi.meta_refcount = atoi(buf);
549         client_getln(smi.meta_content_type, sizeof smi.meta_content_type);
550
551         lprintf(CTDL_INFO, "message #%ld\n", msgnum);
552
553         /* decode base64 message text */
554         CtdlMakeTempFileName(tempfile, sizeof tempfile);
555         snprintf(buf, sizeof buf, "./base64 -d >%s", tempfile);
556         fp = popen(buf, "w");
557         while (client_getln(buf, sizeof buf), strcasecmp(buf, END_OF_MESSAGE)) {
558                 fprintf(fp, "%s\n", buf);
559         }
560         pclose(fp);
561         fp = fopen(tempfile, "rb");
562         fseek(fp, 0L, SEEK_END);
563         msglen = ftell(fp);
564         fclose(fp);
565         lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
566
567         mbuf = malloc(msglen);
568         fp = fopen(tempfile, "rb");
569         fread(mbuf, msglen, 1, fp);
570         fclose(fp);
571
572         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
573
574         free(mbuf);
575         unlink(tempfile);
576
577         PutMetaData(&smi);
578         lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
579 }
580
581
582
583
584 void artv_do_import(void) {
585         char buf[SIZ];
586         char s_version[SIZ];
587         int version;
588
589         unbuffer_output();
590
591         cprintf("%d sock it to me\n", SEND_LISTING);
592         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
593
594                 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
595
596                 if (!strcasecmp(buf, "version")) {
597                         client_getln(s_version, sizeof s_version);
598                         version = atoi(s_version);
599                         if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
600                                 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
601                                 break;
602                         }
603                 }
604                 else if (!strcasecmp(buf, "config")) artv_import_config();
605                 else if (!strcasecmp(buf, "control")) artv_import_control();
606                 else if (!strcasecmp(buf, "user")) artv_import_user();
607                 else if (!strcasecmp(buf, "room")) artv_import_room();
608                 else if (!strcasecmp(buf, "floor")) artv_import_floor();
609                 else if (!strcasecmp(buf, "visit")) artv_import_visit();
610                 else if (!strcasecmp(buf, "message")) artv_import_message();
611                 else break;
612
613         }
614         lprintf(CTDL_INFO, "Invalid keyword <%s>.  Flushing input.\n", buf);
615         while (client_getln(buf, sizeof buf), strcmp(buf, "000"))  ;;
616         rebuild_euid_index();
617 }
618
619
620
621 void cmd_artv(char *cmdbuf) {
622         char cmd[32];
623         static int is_running = 0;
624
625         if (CtdlAccessCheck(ac_internal)) return;
626         if (is_running) {
627                 cprintf("%d The importer/exporter is already running.\n",
628                         ERROR + RESOURCE_BUSY);
629                 return;
630         }
631         is_running = 1;
632
633         CtdlMakeTempFileName(artv_tempfilename1, sizeof artv_tempfilename1);
634         CtdlMakeTempFileName(artv_tempfilename2, sizeof artv_tempfilename2);
635
636         extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
637         if (!strcasecmp(cmd, "export")) artv_do_export();
638         else if (!strcasecmp(cmd, "import")) artv_do_import();
639         else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
640
641         unlink(artv_tempfilename1);
642         unlink(artv_tempfilename2);
643
644         is_running = 0;
645 }
646
647
648
649
650 char *serv_vandelay_init(void)
651 {
652         CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
653         return "$Id$";
654 }