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