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