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