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