Mon Aug 24 20:04:04 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[citadel.git] / citadel / import.c
1 /* cc import.c database.o control.o -lgdbm -o import */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <stdarg.h>
11 #include <pthread.h>
12 #include "citadel.h"
13 #include "server.h"
14
15 int ssv_maxrooms = 0;
16 int ssv_maxfloors = 0;
17 FILE *imfp;
18
19
20 /**** stubs which need to be defined for database.c to work ****/
21
22 struct config config;
23 struct CitContext MyCC;
24
25 struct CitContext *MyContext() {
26         return(&MyCC);
27         }
28
29 void begin_critical_section(int c) { }
30 void end_critical_section(int c) { }
31
32 void lprintf(int loglevel, const char *format, ...) {
33         va_list arg_ptr;
34         char buf[256];
35         int rc;
36
37         va_start(arg_ptr, format);
38         rc = vsprintf(buf, format, arg_ptr);
39         va_end(arg_ptr);
40
41         fprintf(stderr, "%s", buf);
42         fflush(stderr);
43         }
44
45
46 void fpgetfield(fp,string)
47 FILE *fp;
48 char string[];
49 {
50         int a,b;
51         strcpy(string,"");
52         a=0;
53         do {
54                 b=getc(fp);
55                 if (b<1) {
56                         string[a]=0;
57                         return;
58                         }
59                 string[a]=b;
60                 ++a;
61                 } while (b!=0);
62         }
63
64 void imp_ssv() {
65         char key[256], value[256];
66         
67         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
68                 fpgetfield(imfp, value);
69                 lprintf(9, " %s = %s\n", key, value);
70                 
71                 if (!strcasecmp(key, "maxrooms")) {
72                         ssv_maxrooms = atol(value);
73                         if (ssv_maxrooms > MAXROOMS) {
74                                 lprintf(3, "ERROR: maxrooms is %d, need %d\n",
75                                         ssv_maxrooms, MAXROOMS);
76                                 fclose(imfp);
77                                 return;
78                                 }
79                         }
80
81                 if (!strcasecmp(key, "maxfloors")) {
82                         ssv_maxfloors = atol(value);
83                         if (ssv_maxfloors > MAXFLOORS) {
84                                 lprintf(3, "ERROR: maxfloors is %d, need %d\n",
85                                         ssv_maxfloors, MAXFLOORS);
86                                 fclose(imfp);
87                                 return;
88                                 }
89                         }
90                 }
91         }
92
93
94 void imp_config() { 
95         char key[256], value[256];
96         FILE *fp;
97
98         bzero(&config, sizeof(struct config));  
99         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
100                 fpgetfield(imfp, value);
101                 lprintf(9, " %s = %s\n", key, value);
102
103                 if (!strcasecmp(key, "c_nodename"))
104                         strcpy(config.c_nodename, value);
105                 if (!strcasecmp(key, "c_fqdn"))
106                         strcpy(config.c_fqdn, value);
107                 if (!strcasecmp(key, "c_humannode"))
108                         strcpy(config.c_humannode, value);
109                 if (!strcasecmp(key, "c_phonenum"))
110                         strcpy(config.c_phonenum, value);
111                 if (!strcasecmp(key, "c_phonenum"))
112                         strcpy(config.c_phonenum, value);
113                 if (!strcasecmp(key, "c_bbsuid"))
114                         config.c_bbsuid = atoi(value);
115                 if (!strcasecmp(key, "c_creataide"))
116                         config.c_creataide = atoi(value);
117                 if (!strcasecmp(key, "c_sleeping"))
118                         config.c_sleeping = atoi(value);
119                 if (!strcasecmp(key, "c_initax"))
120                         config.c_initax = atoi(value);
121                 if (!strcasecmp(key, "c_regiscall"))
122                         config.c_regiscall = atoi(value);
123                 if (!strcasecmp(key, "c_twitdetect"))
124                         config.c_twitdetect = atoi(value);
125                 if (!strcasecmp(key, "c_twitroom"))
126                         strcpy(config.c_twitroom, value);
127                 if (!strcasecmp(key, "c_defent"))
128                         config.c_defent = atoi(value);
129                 if (!strcasecmp(key, "c_moreprompt"))
130                         strcpy(config.c_moreprompt, value);
131                 if (!strcasecmp(key, "c_restrict"))
132                         config.c_restrict = atoi(value);
133                 if (!strcasecmp(key, "c_bbs_city"))
134                         strcpy(config.c_bbs_city, value);
135                 if (!strcasecmp(key, "c_sysadm"))
136                         strcpy(config.c_sysadm, value);
137                 if (!strcasecmp(key, "c_bucket_dir"))
138                         strcpy(config.c_bucket_dir, value);
139                 if (!strcasecmp(key, "c_setup_level"))
140                         config.c_setup_level = atoi(value);
141                 if (!strcasecmp(key, "c_maxsessions"))
142                         config.c_maxsessions = atoi(value);
143                 if (!strcasecmp(key, "c_net_password"))
144                         strcpy(config.c_net_password, value);
145                 if (!strcasecmp(key, "c_port_number"))
146                         config.c_port_number = atoi(value);
147                 }
148
149         fp = fopen("citadel.config", "wb");
150         fwrite(&config, sizeof(struct config), 1, fp);
151         fclose(fp);
152         }
153                 
154                 
155 void imp_globals() {
156         char key[256], value[256];
157
158         get_control();
159         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
160                 fpgetfield(imfp, value);
161                 lprintf(9, " %s = %s\n", key, value);
162
163                 if (!strcasecmp(key, "mmhighest"))
164                         CitControl.MMhighest = atol(value);
165                 if (!strcasecmp(key, "mmnextuser"))
166                         CitControl.MMnextuser = atol(value);
167
168                 }
169         put_control();
170         }
171
172 void import_message(long msgnum, long msglen) {
173         long count;
174         char *msgtext;
175
176         msgtext = malloc(msglen);
177         if (msgtext == NULL) {
178                 lprintf(3, "ERROR: cannot allocate memory\n");
179                 lprintf(3, "Your data files are now corrupt.\n");
180                 fclose(imfp);
181                 exit(1);
182                 }
183
184         fread(msgtext, msglen, 1, imfp);
185         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), msgtext, msglen);
186         free(msgtext);
187         }
188
189 void import_a_user() {
190         char key[256], value[256], list[256];
191         char lcasename[256];
192         struct usersupp us;
193         int a;
194         long *mbox = NULL;
195         int mbox_size = 0;
196         long msgnum;
197         long msglen;
198
199         bzero(&us, sizeof(struct usersupp));    
200         while(fpgetfield(imfp, key), strcasecmp(key, "enduser")) {
201                 if ((strcasecmp(key, "mail"))
202                    &&(strcasecmp(key, "lastseen"))
203                    &&(strcasecmp(key, "generation"))
204                    &&(strcasecmp(key, "forget")) ) {
205                         fpgetfield(imfp, value);
206                         }
207                 else {
208                         strcpy(value, "");
209                         }
210
211                 if (!strcasecmp(key, "usuid"))
212                         us.USuid = atoi(value);
213                 if (!strcasecmp(key, "password")) {
214                         strcpy(us.password, value);
215                         }
216                 if (!strcasecmp(key, "lastseen"))
217                         for (a=0; a<ssv_maxrooms; ++a) {
218                                 fpgetfield(imfp, list);
219                                 us.lastseen[a] = atol(list);
220                                 }
221                 if (!strcasecmp(key, "generation"))
222                         for (a=0; a<ssv_maxrooms; ++a) {
223                                 fpgetfield(imfp, list);
224                                 us.generation[a] = atoi(list);
225                                 }
226                 if (!strcasecmp(key, "forget"))
227                         for (a=0; a<ssv_maxrooms; ++a) {
228                                 fpgetfield(imfp, list);
229                                 us.forget[a] = atol(list);
230                                 }
231                 if (!strcasecmp(key, "flags"))
232                         us.flags = atoi(value);
233                 if (!strcasecmp(key, "timescalled"))
234                         us.timescalled = atoi(value);
235                 if (!strcasecmp(key, "posted"))
236                         us.posted = atoi(value);
237                 if (!strcasecmp(key, "fullname")) {
238                         strcpy(us.fullname, value);
239                         lprintf(9, "User <%s>", us.fullname);
240                         }
241                 if (!strcasecmp(key, "axlevel"))
242                         us.axlevel = atoi(value);
243                 if (!strcasecmp(key, "usscreenwidth"))
244                         us.USscreenwidth = atoi(value);
245                 if (!strcasecmp(key, "usscreenheight"))
246                         us.USscreenheight = atoi(value);
247                 if (!strcasecmp(key, "usernum"))
248                         us.usernum = atol(value);
249                 if (!strcasecmp(key, "lastcall"))
250                         us.lastcall = atol(value);
251                 if (!strcasecmp(key, "usname"))
252                         strcpy(us.USname, value);
253                 if (!strcasecmp(key, "usaddr"))
254                         strcpy(us.USaddr, value);
255                 if (!strcasecmp(key, "uscity"))
256                         strcpy(us.UScity, value);
257                 if (!strcasecmp(key, "usstate"))
258                         strcpy(us.USstate, value);
259                 if (!strcasecmp(key, "uszip"))
260                         strcpy(us.USzip, value);
261                 if (!strcasecmp(key, "usphone"))
262                         strcpy(us.USphone, value);
263                 if (!strcasecmp(key, "usemail"))
264                         strcpy(us.USemail, value);
265                 if (!strcasecmp(key, "mail")) {
266                         lprintf(9, ".");
267                         fpgetfield(imfp, list);
268                         msgnum = atol(list);
269                         fpgetfield(imfp, list);
270                         msglen = atol(list);
271                         import_message(msgnum, msglen);
272                         ++mbox_size;
273                         mbox = realloc(mbox, (sizeof(long)*mbox_size) );
274                         mbox[mbox_size - 1] = msgnum;
275                         }
276                 }
277         
278         for (a=0; a<=strlen(us.fullname); ++a) {
279                 lcasename[a] = tolower(us.fullname[a]);
280                 }
281         cdb_store(CDB_USERSUPP,
282                 lcasename, strlen(lcasename),
283                 &us, sizeof(struct usersupp));
284         if (mbox_size > 0)  {
285                 cdb_store(CDB_MAILBOXES, 
286                         &us.usernum, sizeof(long),
287                         mbox, (mbox_size * sizeof(long)) );
288                 free(mbox);
289                 }
290         lprintf(9, "\n");
291         }
292
293
294 void imp_usersupp() {
295         char key[256], value[256];
296         
297         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
298                 if (strcasecmp(key, "user")) {
299                         fpgetfield(imfp, value);
300                         }
301                 else {
302                         strcpy(value, "");
303                         }
304                 lprintf(9, " %s = %s\n", key, value);
305
306                 if (!strcasecmp(key, "user")) {
307                         import_a_user();
308                         }
309                 }               
310         }
311
312
313
314 void imp_floors() {
315         char key[256], tag[256], tval[256];
316         struct floor fl;
317         int floornum = 0;
318
319
320         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
321
322                 if (!strcasecmp(key, "floor")) {
323                         bzero(&fl, sizeof(struct floor));
324
325                         while(fpgetfield(imfp, tag),
326                              strcasecmp(tag, "endfloor")) {
327                                 fpgetfield(imfp, tval);
328
329                                 if (!strcasecmp(tag, "f_flags")) 
330                                         fl.f_flags = atoi(tval);
331                                 if (!strcasecmp(tag, "f_name")) {
332                                         lprintf(9, "Floor <%s>\n", tval);
333                                         strcpy(fl.f_name, tval);        
334                                         }
335                                 if (!strcasecmp(tag, "f_ref_count")) 
336                                         fl.f_ref_count = atoi(tval);
337                                 }
338
339                         cdb_store(CDB_FLOORTAB,
340                                 &floornum, sizeof(int),
341                                 &fl, sizeof(struct floor) );
342                         ++floornum;
343                         }
344                 else {
345                         lprintf(3, "ERROR: invalid floor section.\n");
346                         lprintf(3, "Your data files are now corrupt.\n");
347                         fclose(imfp);
348                         return;
349                         }
350                 }
351         }
352
353
354 void imp_rooms() {
355         char key[256], value[256];
356         char tag[256], tval[256];
357         int roomnum = 0;
358         struct quickroom qr;
359         long *msglist;
360         int num_msgs = 0;
361         long msgnum, msglen;
362         
363         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
364                 if (!strcasecmp(key, "room")) {
365                         bzero(&qr, sizeof(struct quickroom));
366                         msglist = NULL;
367                         num_msgs = 0;
368                         lprintf(9, "Room ");
369
370                         while(fpgetfield(imfp, tag),
371                              strcasecmp(tag, "endroom")) {
372                                 if (strcasecmp(tag, "message")) {
373                                         fpgetfield(imfp, tval);
374                                         }
375                                 else {
376                                         strcpy(tval, "");
377                                         }
378
379                                 if (!strcasecmp(tag, "qrname")) {
380                                         strcpy(qr.QRname, tval);
381                                         lprintf(9, "<%s>", qr.QRname);
382                                         }
383                                 if (!strcasecmp(tag, "qrpasswd"))
384                                         strcpy(qr.QRpasswd, tval);
385                                 if (!strcasecmp(tag, "qrroomaide"))
386                                         qr.QRroomaide = atol(tval);
387                                 if (!strcasecmp(tag, "qrhighest"))
388                                         qr.QRhighest = atol(tval);
389                                 if (!strcasecmp(tag, "qrgen"))
390                                         qr.QRgen = atoi(tval);
391                                 if (!strcasecmp(tag, "qrflags"))
392                                         qr.QRflags = atoi(tval);
393                                 if (!strcasecmp(tag, "qrdirname"))
394                                         strcpy(qr.QRdirname, tval);
395                                 if (!strcasecmp(tag, "qrinfo"))
396                                         qr.QRinfo = atol(tval);
397                                 if (!strcasecmp(tag, "qrfloor"))
398                                         qr.QRfloor = atoi(tval);
399                                 if (!strcasecmp(tag, "message")) {
400                                         lprintf(9, ".");
401                                         fpgetfield(imfp, tval);
402                                         msgnum = atol(tval);
403                                         fpgetfield(imfp, tval);
404                                         msglen = atol(tval);
405                                         import_message(msgnum, msglen);
406                                         ++num_msgs;
407                                         msglist = realloc(msglist,
408                                                 (sizeof(long)*num_msgs) );
409                                         msglist[num_msgs - 1] = msgnum;
410                                         }
411
412                                 }
413
414                         lprintf(9, "\n");
415                         if ((roomnum!=1)&&(qr.QRflags&QR_INUSE))
416                            cdb_store(CDB_QUICKROOM,
417                                 &roomnum, sizeof(int),
418                                 &qr, sizeof(struct quickroom) );
419
420                         if (num_msgs > 0) {
421                                 if ((roomnum!=1)&&(qr.QRflags&QR_INUSE))
422                                  cdb_store(CDB_MSGLISTS, &roomnum, sizeof(int),
423                                         msglist, (sizeof(long)*num_msgs) );
424                                 free(msglist);
425                                 }
426
427                         ++roomnum;
428
429                         }
430                 else {
431                         lprintf(3, "ERROR: invalid room section.\n");
432                         lprintf(3, "Your data files are now corrupt.\n");
433                         fclose(imfp);
434                         return;
435                         }
436                 }
437         }
438
439
440
441 void import_databases() {
442         char section[256];
443
444         lprintf(9, " ** IMPORTING ** \n");
445         imfp = fopen("/appl/citadel/exported", "rb");
446         while (fpgetfield(imfp, section), strcasecmp(section, "endfile")) {
447                 lprintf(9, "Section: <%s>\n", section);
448
449                 if (!strcasecmp(section, "ssv"))                imp_ssv();
450                 else if (!strcasecmp(section, "config"))        imp_config();
451                 else if (!strcasecmp(section, "globals"))       imp_globals();
452                 else if (!strcasecmp(section, "usersupp"))      imp_usersupp();
453                 else if (!strcasecmp(section, "rooms"))         imp_rooms();
454                 else if (!strcasecmp(section, "floors"))        imp_floors();
455                 else {
456                         lprintf(3, "ERROR: invalid import section.\n");
457                         lprintf(3, "Your data files are now corrupt.\n");
458                         fclose(imfp);
459                         return;
460                         }
461
462                 }
463
464         }
465
466
467
468
469 void main() {
470         open_databases();
471         import_databases();
472         close_databases();
473         exit(0);
474         }