bc54ced0fd159076fe49afd938bad97a324d8ddd
[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 "dynloader.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
47 #define END_OF_MESSAGE  "---eom---dbd---"
48
49 char artv_tempfilename1[PATH_MAX];
50 char artv_tempfilename2[PATH_MAX];
51 FILE *artv_global_message_list;
52
53 void artv_export_users_backend(struct usersupp *usbuf, void *data) {
54         cprintf("user\n");
55         cprintf("%d\n", usbuf->version);
56         cprintf("%d\n", usbuf->uid);
57         cprintf("%s\n", usbuf->password);
58         cprintf("%u\n", usbuf->flags);
59         cprintf("%ld\n", usbuf->timescalled);
60         cprintf("%ld\n", usbuf->posted);
61         cprintf("%d\n", usbuf->axlevel);
62         cprintf("%ld\n", usbuf->usernum);
63         cprintf("%ld\n", usbuf->lastcall);
64         cprintf("%d\n", usbuf->USuserpurge);
65         cprintf("%s\n", usbuf->fullname);
66         cprintf("%d\n", usbuf->USscreenwidth);
67         cprintf("%d\n", usbuf->USscreenheight);
68         cprintf("%d\n", usbuf->moderation_filter);
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 quickroom *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", 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", 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
100         getroom(&CC->quickroom, qrbuf->QRname);
101         /* format of message list export is all message numbers output
102          * one per line terminated by a 0.
103          */
104         CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
105                 artv_export_room_msg, NULL);
106         cprintf("0\n");
107
108 }
109
110
111
112 void artv_export_rooms(void) {
113         char cmd[SIZ];
114         artv_global_message_list = fopen(artv_tempfilename1, "w");
115         ForEachRoom(artv_export_rooms_backend, NULL);
116         fclose(artv_global_message_list);
117
118         /*
119          * Process the 'global' message list.  (Sort it and remove dups.
120          * Dups are ok because a message may be in more than one room, but
121          * this will be handled by exporting the reference count, not by
122          * exporting the message multiple times.)
123          */
124         sprintf(cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
125         system(cmd);
126         sprintf(cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
127         system(cmd);
128 }
129
130
131 void artv_export_floors(void) {
132         struct floor flbuf;
133         int i;
134
135         for (i=0; i < MAXFLOORS; ++i) {
136                 cprintf("floor\n");
137                 cprintf("%d\n", i);
138                 getfloor(&flbuf, i);
139                 cprintf("%u\n", flbuf.f_flags);
140                 cprintf("%s\n", flbuf.f_name);
141                 cprintf("%d\n", flbuf.f_ref_count);
142                 cprintf("%d\n", flbuf.f_ep.expire_mode);
143                 cprintf("%d\n", flbuf.f_ep.expire_value);
144         }
145 }
146
147
148
149
150
151 /* 
152  *  Traverse the visits file...
153  */
154 void artv_export_visits(void) {
155         struct visit vbuf;
156         struct cdbdata *cdbv;
157
158         cdb_begin_transaction();
159         cdb_rewind(CDB_VISIT);
160
161         while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
162                 memset(&vbuf, 0, sizeof(struct visit));
163                 memcpy(&vbuf, cdbv->ptr,
164                        ((cdbv->len > sizeof(struct visit)) ?
165                         sizeof(struct visit) : cdbv->len));
166                 cdb_free(cdbv);
167
168                 cprintf("visit\n");
169                 cprintf("%ld\n", vbuf.v_roomnum);
170                 cprintf("%ld\n", vbuf.v_roomgen);
171                 cprintf("%ld\n", vbuf.v_usernum);
172
173                 if (strlen(vbuf.v_seen) > 0) {
174                         cprintf("%s\n", vbuf.v_seen);
175                 }
176                 else {
177                         cprintf("%ld\n", vbuf.v_lastseen);
178                 }
179
180                 cprintf("%u\n", vbuf.v_flags);
181         }
182         cdb_end_transaction();
183 }
184
185
186 void artv_export_message(long msgnum) {
187         struct SuppMsgInfo smi;
188         struct CtdlMessage *msg;
189         struct ser_ret smr;
190         FILE *fp;
191         char buf[SIZ];
192         char tempfile[SIZ];
193
194         msg = CtdlFetchMessage(msgnum);
195         if (msg == NULL) return;        /* fail silently */
196
197         cprintf("message\n");
198         GetSuppMsgInfo(&smi, msgnum);
199         cprintf("%ld\n", msgnum);
200         cprintf("%d\n", smi.smi_refcount);
201         cprintf("%s\n", smi.smi_content_type);
202         cprintf("%d\n", smi.smi_mod);
203
204         serialize_message(&smr, msg);
205         CtdlFreeMessage(msg);
206
207         /* write it in base64 */
208         strcpy(tempfile, tmpnam(NULL));
209         sprintf(buf, "./base64 -e >%s", tempfile);
210         fp = popen(buf, "w");
211         fwrite(smr.ser, smr.len, 1, fp);
212         pclose(fp);
213
214         phree(smr.ser);
215
216         fp = fopen(tempfile, "r");
217         unlink(tempfile);
218         while (fgets(buf, sizeof(buf), fp) != NULL) {
219                 buf[strlen(buf)-1] = 0;
220                 cprintf("%s\n", buf);
221         }
222         fclose(fp);
223         cprintf("%s\n", END_OF_MESSAGE);
224 }
225
226
227
228 void artv_export_messages(void) {
229         char buf[SIZ];
230         long msgnum;
231         int count = 0;
232
233         artv_global_message_list = fopen(artv_tempfilename1, "r");
234         lprintf(7, "Opened %s\n", artv_tempfilename1);
235         while (fgets(buf, sizeof(buf), artv_global_message_list) != NULL) {
236                 msgnum = atol(buf);
237                 if (msgnum > 0L) {
238                         artv_export_message(msgnum);
239                         ++count;
240                 }
241         }
242         fclose(artv_global_message_list);
243         lprintf(7, "Exported %ld messages.\n", count);
244 }
245
246
247
248
249 void artv_do_export(void) {
250         cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
251
252         cprintf("version\n%d\n", REV_LEVEL);
253
254         /* export the config file */
255         cprintf("config\n");
256         cprintf("%s\n", config.c_nodename);
257         cprintf("%s\n", config.c_fqdn);
258         cprintf("%s\n", config.c_humannode);
259         cprintf("%s\n", config.c_phonenum);
260         cprintf("%d\n", config.c_bbsuid);
261         cprintf("%d\n", config.c_creataide);
262         cprintf("%d\n", config.c_sleeping);
263         cprintf("%d\n", config.c_initax);
264         cprintf("%d\n", config.c_regiscall);
265         cprintf("%d\n", config.c_twitdetect);
266         cprintf("%s\n", config.c_twitroom);
267         cprintf("%s\n", config.c_moreprompt);
268         cprintf("%d\n", config.c_restrict);
269         cprintf("%ld\n", config.c_msgbase);
270         cprintf("%s\n", config.c_bbs_city);
271         cprintf("%s\n", config.c_sysadm);
272         cprintf("%s\n", config.c_bucket_dir);
273         cprintf("%d\n", config.c_setup_level);
274         cprintf("%d\n", config.c_maxsessions);
275         cprintf("%s\n", config.c_net_password);
276         cprintf("%d\n", config.c_port_number);
277         cprintf("%d\n", config.c_ipgm_secret);
278         cprintf("%d\n", config.c_ep.expire_mode);
279         cprintf("%d\n", config.c_ep.expire_value);
280         cprintf("%d\n", config.c_userpurge);
281         cprintf("%d\n", config.c_roompurge);
282         cprintf("%s\n", config.c_logpages);
283         cprintf("%d\n", config.c_createax);
284         cprintf("%ld\n", config.c_maxmsglen);
285         cprintf("%d\n", config.c_min_workers);
286         cprintf("%d\n", config.c_max_workers);
287         cprintf("%d\n", config.c_pop3_port);
288         cprintf("%d\n", config.c_smtp_port);
289         cprintf("%d\n", config.c_default_filter);
290
291         /* Export the control file */
292         get_control();
293         cprintf("control\n");
294         cprintf("%ld\n", CitControl.MMhighest);
295         cprintf("%u\n", CitControl.MMflags);
296         cprintf("%ld\n", CitControl.MMnextuser);
297         cprintf("%ld\n", CitControl.MMnextroom);
298         cprintf("%d\n", CitControl.version);
299
300         artv_export_users();
301         artv_export_rooms();
302         artv_export_floors();
303         artv_export_visits();
304         artv_export_messages();
305
306         cprintf("000\n");
307 }
308
309
310
311 void artv_import_config(void) {
312         char buf[SIZ];
313
314         lprintf(9, "Importing config file\n");
315         client_gets(config.c_nodename);
316         lprintf(9, "c_nodename = %s\n", config.c_nodename);
317         client_gets(config.c_fqdn);
318         client_gets(config.c_humannode);
319         client_gets(config.c_phonenum);
320         client_gets(buf);       config.c_bbsuid = atoi(buf);
321         client_gets(buf);       config.c_creataide = atoi(buf);
322         client_gets(buf);       config.c_sleeping = atoi(buf);
323         client_gets(buf);       config.c_initax = atoi(buf);
324         client_gets(buf);       config.c_regiscall = atoi(buf);
325         client_gets(buf);       config.c_twitdetect = atoi(buf);
326         client_gets(config.c_twitroom);
327         client_gets(config.c_moreprompt);
328         client_gets(buf);       config.c_restrict = atoi(buf);
329         client_gets(buf);       config.c_msgbase = atol(buf);
330         client_gets(config.c_bbs_city);
331         client_gets(config.c_sysadm);
332         lprintf(9, "c_sysadm = %s\n", config.c_sysadm);
333         client_gets(config.c_bucket_dir);
334         client_gets(buf);       config.c_setup_level = atoi(buf);
335         client_gets(buf);       config.c_maxsessions = atoi(buf);
336         client_gets(config.c_net_password);
337         client_gets(buf);       config.c_port_number = atoi(buf);
338         client_gets(buf);       config.c_ipgm_secret = atoi(buf);
339         client_gets(buf);       config.c_ep.expire_mode = atoi(buf);
340         client_gets(buf);       config.c_ep.expire_value = atoi(buf);
341         client_gets(buf);       config.c_userpurge = atoi(buf);
342         client_gets(buf);       config.c_roompurge = atoi(buf);
343         client_gets(config.c_logpages);
344         client_gets(buf);       config.c_createax = atoi(buf);
345         client_gets(buf);       config.c_maxmsglen = atol(buf);
346         client_gets(buf);       config.c_min_workers = atoi(buf);
347         client_gets(buf);       config.c_max_workers = atoi(buf);
348         client_gets(buf);       config.c_pop3_port = atoi(buf);
349         client_gets(buf);       config.c_smtp_port = atoi(buf);
350         client_gets(buf);       config.c_default_filter = atoi(buf);
351         put_config();
352         lprintf(7, "Imported config file\n");
353 }
354
355
356
357 void artv_import_control(void) {
358         char buf[SIZ];
359
360         lprintf(9, "Importing control file\n");
361         client_gets(buf);       CitControl.MMhighest = atol(buf);
362         client_gets(buf);       CitControl.MMflags = atoi(buf);
363         client_gets(buf);       CitControl.MMnextuser = atol(buf);
364         client_gets(buf);       CitControl.MMnextroom = atol(buf);
365         client_gets(buf);       CitControl.version = atoi(buf);
366         put_control();
367         lprintf(7, "Imported control file\n");
368 }
369
370
371 void artv_import_user(void) {
372         char buf[SIZ];
373         struct usersupp usbuf;
374
375         client_gets(buf);       usbuf.version = atoi(buf);
376         client_gets(buf);       usbuf.uid = atoi(buf);
377         client_gets(usbuf.password);
378         client_gets(buf);       usbuf.flags = atoi(buf);
379         client_gets(buf);       usbuf.timescalled = atol(buf);
380         client_gets(buf);       usbuf.posted = atol(buf);
381         client_gets(buf);       usbuf.axlevel = atoi(buf);
382         client_gets(buf);       usbuf.usernum = atol(buf);
383         client_gets(buf);       usbuf.lastcall = atol(buf);
384         client_gets(buf);       usbuf.USuserpurge = atoi(buf);
385         client_gets(usbuf.fullname);
386         client_gets(buf);       usbuf.USscreenwidth = atoi(buf);
387         client_gets(buf);       usbuf.USscreenheight = atoi(buf);
388         client_gets(buf);       usbuf.moderation_filter = atoi(buf);
389         putuser(&usbuf);
390 }
391
392
393 void artv_import_room(void) {
394         char buf[SIZ];
395         struct quickroom qrbuf;
396         long msgnum;
397         int msgcount = 0;
398
399         client_gets(qrbuf.QRname);
400         client_gets(qrbuf.QRpasswd);
401         client_gets(buf);       qrbuf.QRroomaide = atol(buf);
402         client_gets(buf);       qrbuf.QRhighest = atol(buf);
403         client_gets(buf);       qrbuf.QRgen = atol(buf);
404         client_gets(buf);       qrbuf.QRflags = atoi(buf);
405         client_gets(qrbuf.QRdirname);
406         client_gets(buf);       qrbuf.QRinfo = atol(buf);
407         client_gets(buf);       qrbuf.QRfloor = atoi(buf);
408         client_gets(buf);       qrbuf.QRmtime = atol(buf);
409         client_gets(buf);       qrbuf.QRep.expire_mode = atoi(buf);
410         client_gets(buf);       qrbuf.QRep.expire_value = atoi(buf);
411         client_gets(buf);       qrbuf.QRnumber = atol(buf);
412         client_gets(buf);       qrbuf.QRorder = atoi(buf);
413         putroom(&qrbuf);
414         lprintf(7, "Imported room <%s>\n", qrbuf.QRname);
415         /* format of message list export is all message numbers output
416          * one per line terminated by a 0.
417          */
418         while (client_gets(buf), msgnum = atol(buf), msgnum > 0) {
419                 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0);
420                 ++msgcount;
421         }
422         lprintf(7, "(%d messages)\n", msgcount);
423 }
424
425
426 void artv_import_floor(void) {
427         struct floor flbuf;
428         int i;
429         char buf[SIZ];
430
431         client_gets(buf);               i = atoi(buf);
432         client_gets(buf);               flbuf.f_flags = atoi(buf);
433         client_gets(flbuf.f_name);
434         client_gets(buf);               flbuf.f_ref_count = atoi(buf);
435         client_gets(buf);               flbuf.f_ep.expire_mode = atoi(buf);
436         client_gets(buf);               flbuf.f_ep.expire_value = atoi(buf);
437         putfloor(&flbuf, i);
438         lprintf(7, "Imported floor #%d (%s)\n", i, flbuf.f_name);
439 }
440
441
442 /* 
443  */
444 void artv_import_visit(void) {
445         struct visit vbuf;
446         char buf[SIZ];
447         int i;
448         int is_textual_seen = 0;
449
450         client_gets(buf);       vbuf.v_roomnum = atol(buf);
451         client_gets(buf);       vbuf.v_roomgen = atol(buf);
452         client_gets(buf);       vbuf.v_usernum = atol(buf);
453
454         client_gets(buf);
455         vbuf.v_lastseen = atol(buf);
456         for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
457         if (is_textual_seen)    strcpy(vbuf.v_seen, buf);
458
459         client_gets(buf);       vbuf.v_flags = atoi(buf);
460         put_visit(&vbuf);
461         lprintf(7, "Imported visit %ld/%ld/%ld\n",
462                 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
463 }
464
465
466
467 void artv_import_message(void) {
468         struct SuppMsgInfo smi;
469         long msgnum;
470         int msglen;
471         FILE *fp;
472         char buf[SIZ];
473         char tempfile[SIZ];
474         char *mbuf;
475
476         memset(&smi, 0, sizeof(struct SuppMsgInfo));
477         client_gets(buf);       msgnum = atol(buf);
478                                 smi.smi_msgnum = msgnum;
479         client_gets(buf);       smi.smi_refcount = atoi(buf);
480         client_gets(smi.smi_content_type);
481         client_gets(buf);       smi.smi_mod = atoi(buf);
482
483         lprintf(7, "message #%ld\n", msgnum);
484
485         /* decode base64 message text */
486         strcpy(tempfile, tmpnam(NULL));
487         sprintf(buf, "./base64 -d >%s", tempfile);
488         fp = popen(buf, "w");
489         while (client_gets(buf), strcasecmp(buf, END_OF_MESSAGE)) {
490                 fprintf(fp, "%s\n", buf);
491         }
492         pclose(fp);
493         fp = fopen(tempfile, "rb");
494         fseek(fp, 0L, SEEK_END);
495         msglen = ftell(fp);
496         fclose(fp);
497         lprintf(9, "msglen = %ld\n", msglen);
498
499         mbuf = mallok(msglen);
500         fp = fopen(tempfile, "rb");
501         fread(mbuf, msglen, 1, fp);
502         fclose(fp);
503
504         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
505
506         phree(mbuf);
507         unlink(tempfile);
508
509         PutSuppMsgInfo(&smi);
510         lprintf(7, "Imported message %ld\n", msgnum);
511 }
512
513
514
515
516 void artv_do_import(void) {
517         char buf[SIZ];
518         char s_version[SIZ];
519         int version;
520
521         cprintf("%d sock it to me\n", SEND_LISTING);
522         while (client_gets(buf), strcmp(buf, "000")) {
523
524                 lprintf(9, "import keyword: <%s>\n", buf);
525
526                 if (!strcasecmp(buf, "version")) {
527                         client_gets(s_version);
528                         version = atoi(s_version);
529                         if ((version < REV_MIN) || (version > REV_LEVEL)) {
530                                 lprintf(7, "Version mismatch - aborting\n");
531                                 break;
532                         }
533                 }
534                 else if (!strcasecmp(buf, "config")) artv_import_config();
535                 else if (!strcasecmp(buf, "control")) artv_import_control();
536                 else if (!strcasecmp(buf, "user")) artv_import_user();
537                 else if (!strcasecmp(buf, "room")) artv_import_room();
538                 else if (!strcasecmp(buf, "floor")) artv_import_floor();
539                 else if (!strcasecmp(buf, "visit")) artv_import_visit();
540                 else if (!strcasecmp(buf, "message")) artv_import_message();
541                 else break;
542
543         }
544         lprintf(7, "Invalid keyword <%s>.  Flushing input.\n", buf);
545         while (client_gets(buf), strcmp(buf, "000"))  ;;
546 }
547
548
549
550 void cmd_artv(char *cmdbuf) {
551         char cmd[SIZ];
552         static int is_running = 0;
553
554         if (CtdlAccessCheck(ac_internal)) return;
555         if (is_running) {
556                 cprintf("%d The importer/exporter is already running.\n",
557                         ERROR);
558                 return;
559         }
560         is_running = 1;
561
562         strcpy(artv_tempfilename1, tmpnam(NULL));
563         strcpy(artv_tempfilename2, tmpnam(NULL));
564
565         extract(cmd, cmdbuf, 0);
566         if (!strcasecmp(cmd, "export")) artv_do_export();
567         else if (!strcasecmp(cmd, "import")) artv_do_import();
568         else cprintf("%d illegal command\n", ERROR);
569
570         unlink(artv_tempfilename1);
571         unlink(artv_tempfilename2);
572
573         is_running = 0;
574 }
575
576
577
578
579 char *Dynamic_Module_Init(void)
580 {
581         CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
582         return "$Id$";
583 }