252a4f122119c393f066440d15a243a85e6eeb44
[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
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 ctdluser *usbuf, void *data) {
54         cprintf("user\n");
55         cprintf("%d\n", usbuf->version);
56         cprintf("%ld\n", (long)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", (long)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 }
69
70
71 void artv_export_users(void) {
72         ForEachUser(artv_export_users_backend, NULL);
73 }
74
75
76 void artv_export_room_msg(long msgnum, void *userdata) {
77         cprintf("%ld\n", msgnum);
78         fprintf(artv_global_message_list, "%ld\n", msgnum);
79 }
80
81
82 void artv_export_rooms_backend(struct ctdlroom *qrbuf, void *data) {
83         cprintf("room\n");
84         cprintf("%s\n", qrbuf->QRname);
85         cprintf("%s\n", qrbuf->QRpasswd);
86         cprintf("%ld\n", qrbuf->QRroomaide);
87         cprintf("%ld\n", qrbuf->QRhighest);
88         cprintf("%ld\n", (long)qrbuf->QRgen);
89         cprintf("%u\n", qrbuf->QRflags);
90         cprintf("%s\n", qrbuf->QRdirname);
91         cprintf("%ld\n", qrbuf->QRinfo);
92         cprintf("%d\n", qrbuf->QRfloor);
93         cprintf("%ld\n", (long)qrbuf->QRmtime);
94         cprintf("%d\n", qrbuf->QRep.expire_mode);
95         cprintf("%d\n", qrbuf->QRep.expire_value);
96         cprintf("%ld\n", qrbuf->QRnumber);
97         cprintf("%d\n", qrbuf->QRorder);
98         cprintf("%u\n", qrbuf->QRflags2);
99         cprintf("%d\n", qrbuf->QRdefaultview);
100
101         getroom(&CC->room, qrbuf->QRname);
102         /* format of message list export is all message numbers output
103          * one per line terminated by a 0.
104          */
105         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
106                 artv_export_room_msg, NULL);
107         cprintf("0\n");
108
109 }
110
111
112
113 void artv_export_rooms(void) {
114         char cmd[SIZ];
115         artv_global_message_list = fopen(artv_tempfilename1, "w");
116         ForEachRoom(artv_export_rooms_backend, NULL);
117         fclose(artv_global_message_list);
118
119         /*
120          * Process the 'global' message list.  (Sort it and remove dups.
121          * Dups are ok because a message may be in more than one room, but
122          * this will be handled by exporting the reference count, not by
123          * exporting the message multiple times.)
124          */
125         snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
126         system(cmd);
127         snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
128         system(cmd);
129 }
130
131
132 void artv_export_floors(void) {
133         struct floor flbuf;
134         int i;
135
136         for (i=0; i < MAXFLOORS; ++i) {
137                 cprintf("floor\n");
138                 cprintf("%d\n", i);
139                 getfloor(&flbuf, i);
140                 cprintf("%u\n", flbuf.f_flags);
141                 cprintf("%s\n", flbuf.f_name);
142                 cprintf("%d\n", flbuf.f_ref_count);
143                 cprintf("%d\n", flbuf.f_ep.expire_mode);
144                 cprintf("%d\n", flbuf.f_ep.expire_value);
145         }
146 }
147
148
149
150
151
152 /* 
153  *  Traverse the visits file...
154  */
155 void artv_export_visits(void) {
156         struct visit vbuf;
157         struct cdbdata *cdbv;
158
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("%s\n", vbuf.v_answered);
181                 cprintf("%u\n", vbuf.v_flags);
182                 cprintf("%d\n", vbuf.v_view);
183         }
184 }
185
186
187 void artv_export_message(long msgnum) {
188         struct MetaData smi;
189         struct CtdlMessage *msg;
190         struct ser_ret smr;
191         FILE *fp;
192         char buf[SIZ];
193         char tempfile[SIZ];
194
195         msg = CtdlFetchMessage(msgnum);
196         if (msg == NULL) return;        /* fail silently */
197
198         cprintf("message\n");
199         GetMetaData(&smi, msgnum);
200         cprintf("%ld\n", msgnum);
201         cprintf("%d\n", smi.meta_refcount);
202         cprintf("%s\n", smi.meta_content_type);
203
204         serialize_message(&smr, msg);
205         CtdlFreeMessage(msg);
206
207         /* write it in base64 */
208         strcpy(tempfile, tmpnam(NULL));
209         snprintf(buf, sizeof 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 %d 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("%ld\n", (long)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("%s\n", config.c_bbs_city);
270         cprintf("%s\n", config.c_sysadm);
271         cprintf("%d\n", config.c_setup_level);
272         cprintf("%d\n", config.c_maxsessions);
273         cprintf("%d\n", config.c_port_number);
274         cprintf("%d\n", config.c_ep.expire_mode);
275         cprintf("%d\n", config.c_ep.expire_value);
276         cprintf("%d\n", config.c_userpurge);
277         cprintf("%d\n", config.c_roompurge);
278         cprintf("%s\n", config.c_logpages);
279         cprintf("%d\n", config.c_createax);
280         cprintf("%ld\n", config.c_maxmsglen);
281         cprintf("%d\n", config.c_min_workers);
282         cprintf("%d\n", config.c_max_workers);
283         cprintf("%d\n", config.c_pop3_port);
284         cprintf("%d\n", config.c_smtp_port);
285         cprintf("%d\n", config.c_purge_hour);
286         cprintf("%d\n", config.c_mbxep.expire_mode);
287         cprintf("%d\n", config.c_mbxep.expire_value);
288         cprintf("%s\n", config.c_ldap_host);
289         cprintf("%d\n", config.c_ldap_port);
290         cprintf("%s\n", config.c_ldap_base_dn);
291         cprintf("%s\n", config.c_ldap_bind_dn);
292         cprintf("%s\n", config.c_ldap_bind_pw);
293
294         /* Export the control file */
295         get_control();
296         cprintf("control\n");
297         cprintf("%ld\n", CitControl.MMhighest);
298         cprintf("%u\n", CitControl.MMflags);
299         cprintf("%ld\n", CitControl.MMnextuser);
300         cprintf("%ld\n", CitControl.MMnextroom);
301         cprintf("%d\n", CitControl.version);
302
303         artv_export_users();
304         artv_export_rooms();
305         artv_export_floors();
306         artv_export_visits();
307         artv_export_messages();
308
309         cprintf("000\n");
310 }
311
312
313
314 void artv_import_config(void) {
315         char buf[SIZ];
316
317         lprintf(9, "Importing config file\n");
318         client_gets(config.c_nodename);
319         lprintf(9, "c_nodename = %s\n", config.c_nodename);
320         client_gets(config.c_fqdn);
321         client_gets(config.c_humannode);
322         client_gets(config.c_phonenum);
323         client_gets(buf);       config.c_bbsuid = atoi(buf);
324         client_gets(buf);       config.c_creataide = atoi(buf);
325         client_gets(buf);       config.c_sleeping = atoi(buf);
326         client_gets(buf);       config.c_initax = atoi(buf);
327         client_gets(buf);       config.c_regiscall = atoi(buf);
328         client_gets(buf);       config.c_twitdetect = atoi(buf);
329         client_gets(config.c_twitroom);
330         client_gets(config.c_moreprompt);
331         client_gets(buf);       config.c_restrict = atoi(buf);
332         client_gets(config.c_bbs_city);
333         client_gets(config.c_sysadm);
334         lprintf(9, "c_sysadm = %s\n", config.c_sysadm);
335         client_gets(buf);       config.c_setup_level = atoi(buf);
336         client_gets(buf);       config.c_maxsessions = atoi(buf);
337         client_gets(buf);       config.c_port_number = atoi(buf);
338         client_gets(buf);       config.c_ep.expire_mode = atoi(buf);
339         client_gets(buf);       config.c_ep.expire_value = atoi(buf);
340         client_gets(buf);       config.c_userpurge = atoi(buf);
341         client_gets(buf);       config.c_roompurge = atoi(buf);
342         client_gets(config.c_logpages);
343         client_gets(buf);       config.c_createax = atoi(buf);
344         client_gets(buf);       config.c_maxmsglen = atol(buf);
345         client_gets(buf);       config.c_min_workers = atoi(buf);
346         client_gets(buf);       config.c_max_workers = atoi(buf);
347         client_gets(buf);       config.c_pop3_port = atoi(buf);
348         client_gets(buf);       config.c_smtp_port = atoi(buf);
349         client_gets(buf);       config.c_purge_hour = atoi(buf);
350         client_gets(buf);       config.c_mbxep.expire_mode = atoi(buf);
351         client_gets(buf);       config.c_mbxep.expire_value = atoi(buf);
352         client_gets(config.c_ldap_host);
353         client_gets(buf);       config.c_ldap_port = atoi(buf);
354         client_gets(config.c_ldap_base_dn);
355         client_gets(config.c_ldap_bind_dn);
356         client_gets(config.c_ldap_bind_pw);
357         put_config();
358         lprintf(7, "Imported config file\n");
359 }
360
361
362
363 void artv_import_control(void) {
364         char buf[SIZ];
365
366         lprintf(9, "Importing control file\n");
367         client_gets(buf);       CitControl.MMhighest = atol(buf);
368         client_gets(buf);       CitControl.MMflags = atoi(buf);
369         client_gets(buf);       CitControl.MMnextuser = atol(buf);
370         client_gets(buf);       CitControl.MMnextroom = atol(buf);
371         client_gets(buf);       CitControl.version = atoi(buf);
372         put_control();
373         lprintf(7, "Imported control file\n");
374 }
375
376
377 void artv_import_user(void) {
378         char buf[SIZ];
379         struct ctdluser usbuf;
380
381         client_gets(buf);       usbuf.version = atoi(buf);
382         client_gets(buf);       usbuf.uid = atoi(buf);
383         client_gets(usbuf.password);
384         client_gets(buf);       usbuf.flags = atoi(buf);
385         client_gets(buf);       usbuf.timescalled = atol(buf);
386         client_gets(buf);       usbuf.posted = atol(buf);
387         client_gets(buf);       usbuf.axlevel = atoi(buf);
388         client_gets(buf);       usbuf.usernum = atol(buf);
389         client_gets(buf);       usbuf.lastcall = atol(buf);
390         client_gets(buf);       usbuf.USuserpurge = atoi(buf);
391         client_gets(usbuf.fullname);
392         client_gets(buf);       usbuf.USscreenwidth = atoi(buf);
393         client_gets(buf);       usbuf.USscreenheight = atoi(buf);
394         putuser(&usbuf);
395 }
396
397
398 void artv_import_room(void) {
399         char buf[SIZ];
400         struct ctdlroom qrbuf;
401         long msgnum;
402         int msgcount = 0;
403
404         client_gets(qrbuf.QRname);
405         client_gets(qrbuf.QRpasswd);
406         client_gets(buf);       qrbuf.QRroomaide = atol(buf);
407         client_gets(buf);       qrbuf.QRhighest = atol(buf);
408         client_gets(buf);       qrbuf.QRgen = atol(buf);
409         client_gets(buf);       qrbuf.QRflags = atoi(buf);
410         client_gets(qrbuf.QRdirname);
411         client_gets(buf);       qrbuf.QRinfo = atol(buf);
412         client_gets(buf);       qrbuf.QRfloor = atoi(buf);
413         client_gets(buf);       qrbuf.QRmtime = atol(buf);
414         client_gets(buf);       qrbuf.QRep.expire_mode = atoi(buf);
415         client_gets(buf);       qrbuf.QRep.expire_value = atoi(buf);
416         client_gets(buf);       qrbuf.QRnumber = atol(buf);
417         client_gets(buf);       qrbuf.QRorder = atoi(buf);
418         client_gets(buf);       qrbuf.QRflags2 = atoi(buf);
419         client_gets(buf);       qrbuf.QRdefaultview = atoi(buf);
420         putroom(&qrbuf);
421         lprintf(7, "Imported room <%s>\n", qrbuf.QRname);
422         /* format of message list export is all message numbers output
423          * one per line terminated by a 0.
424          */
425         while (client_gets(buf), msgnum = atol(buf), msgnum > 0) {
426                 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0);
427                 ++msgcount;
428         }
429         lprintf(7, "(%d messages)\n", msgcount);
430 }
431
432
433 void artv_import_floor(void) {
434         struct floor flbuf;
435         int i;
436         char buf[SIZ];
437
438         client_gets(buf);               i = atoi(buf);
439         client_gets(buf);               flbuf.f_flags = atoi(buf);
440         client_gets(flbuf.f_name);
441         client_gets(buf);               flbuf.f_ref_count = atoi(buf);
442         client_gets(buf);               flbuf.f_ep.expire_mode = atoi(buf);
443         client_gets(buf);               flbuf.f_ep.expire_value = atoi(buf);
444         putfloor(&flbuf, i);
445         lprintf(7, "Imported floor #%d (%s)\n", i, flbuf.f_name);
446 }
447
448
449 /* 
450  */
451 void artv_import_visit(void) {
452         struct visit vbuf;
453         char buf[SIZ];
454         int i;
455         int is_textual_seen = 0;
456
457         client_gets(buf);       vbuf.v_roomnum = atol(buf);
458         client_gets(buf);       vbuf.v_roomgen = atol(buf);
459         client_gets(buf);       vbuf.v_usernum = atol(buf);
460
461         client_gets(buf);
462         vbuf.v_lastseen = atol(buf);
463         for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
464         if (is_textual_seen)    strcpy(vbuf.v_seen, buf);
465
466         client_gets(vbuf.v_answered);
467         client_gets(buf);       vbuf.v_flags = atoi(buf);
468         client_gets(buf);       vbuf.v_view = atoi(buf);
469         put_visit(&vbuf);
470         lprintf(7, "Imported visit %ld/%ld/%ld\n",
471                 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
472 }
473
474
475
476 void artv_import_message(void) {
477         struct MetaData smi;
478         long msgnum;
479         long msglen;
480         FILE *fp;
481         char buf[SIZ];
482         char tempfile[SIZ];
483         char *mbuf;
484
485         memset(&smi, 0, sizeof(struct MetaData));
486         client_gets(buf);       msgnum = atol(buf);
487                                 smi.meta_msgnum = msgnum;
488         client_gets(buf);       smi.meta_refcount = atoi(buf);
489         client_gets(smi.meta_content_type);
490
491         lprintf(7, "message #%ld\n", msgnum);
492
493         /* decode base64 message text */
494         strcpy(tempfile, tmpnam(NULL));
495         snprintf(buf, sizeof buf, "./base64 -d >%s", tempfile);
496         fp = popen(buf, "w");
497         while (client_gets(buf), strcasecmp(buf, END_OF_MESSAGE)) {
498                 fprintf(fp, "%s\n", buf);
499         }
500         pclose(fp);
501         fp = fopen(tempfile, "rb");
502         fseek(fp, 0L, SEEK_END);
503         msglen = ftell(fp);
504         fclose(fp);
505         lprintf(9, "msglen = %ld\n", msglen);
506
507         mbuf = mallok(msglen);
508         fp = fopen(tempfile, "rb");
509         fread(mbuf, msglen, 1, fp);
510         fclose(fp);
511
512         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
513
514         phree(mbuf);
515         unlink(tempfile);
516
517         PutMetaData(&smi);
518         lprintf(7, "Imported message %ld\n", msgnum);
519 }
520
521
522
523
524 void artv_do_import(void) {
525         char buf[SIZ];
526         char s_version[SIZ];
527         int version;
528
529         cprintf("%d sock it to me\n", SEND_LISTING);
530         while (client_gets(buf), strcmp(buf, "000")) {
531
532                 lprintf(9, "import keyword: <%s>\n", buf);
533
534                 if (!strcasecmp(buf, "version")) {
535                         client_gets(s_version);
536                         version = atoi(s_version);
537                         if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
538                                 lprintf(7, "Version mismatch - aborting\n");
539                                 break;
540                         }
541                 }
542                 else if (!strcasecmp(buf, "config")) artv_import_config();
543                 else if (!strcasecmp(buf, "control")) artv_import_control();
544                 else if (!strcasecmp(buf, "user")) artv_import_user();
545                 else if (!strcasecmp(buf, "room")) artv_import_room();
546                 else if (!strcasecmp(buf, "floor")) artv_import_floor();
547                 else if (!strcasecmp(buf, "visit")) artv_import_visit();
548                 else if (!strcasecmp(buf, "message")) artv_import_message();
549                 else break;
550
551         }
552         lprintf(7, "Invalid keyword <%s>.  Flushing input.\n", buf);
553         while (client_gets(buf), strcmp(buf, "000"))  ;;
554 }
555
556
557
558 void cmd_artv(char *cmdbuf) {
559         char cmd[SIZ];
560         static int is_running = 0;
561
562         if (CtdlAccessCheck(ac_internal)) return;
563         if (is_running) {
564                 cprintf("%d The importer/exporter is already running.\n",
565                         ERROR + RESOURCE_BUSY);
566                 return;
567         }
568         is_running = 1;
569
570         strcpy(artv_tempfilename2, tmpnam(NULL));
571
572         extract(cmd, cmdbuf, 0);
573         if (!strcasecmp(cmd, "export")) artv_do_export();
574         else if (!strcasecmp(cmd, "import")) artv_do_import();
575         else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
576
577         unlink(artv_tempfilename1);
578         unlink(artv_tempfilename2);
579
580         is_running = 0;
581 }
582
583
584
585
586 char *serv_vandelay_init(void)
587 {
588         CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
589         return "$Id$";
590 }