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