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