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