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