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