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