fix possible crash on user deletion
[srvx.git] / src / checkdb.c
1 #include "conf.h"
2 #include "modcmd.h"
3 #include "saxdb.h"
4 #include "timeq.h"
5
6 int bad;
7 const char *hidden_host_suffix;
8
9 /* because recdb likes to log stuff.. */
10 void log_module(UNUSED_ARG(struct log_type *lt), UNUSED_ARG(enum log_severity ls), const char *format, ...)
11 {
12     va_list va;
13     va_start(va, format);
14     vfprintf(stderr, format, va);
15     va_end(va);
16     bad = 1;
17 }
18
19 /* and because saxdb is tied in to lots of stuff.. */
20
21 unsigned long now;
22
23 void *conf_get_data(UNUSED_ARG(const char *full_path), UNUSED_ARG(enum recdb_type type)) {
24     return NULL;
25 }
26
27 void conf_register_reload(UNUSED_ARG(conf_reload_func crf)) {
28 }
29
30 void reg_exit_func(UNUSED_ARG(exit_func_t handler)) {
31 }
32
33 void timeq_add(UNUSED_ARG(unsigned long when), UNUSED_ARG(timeq_func func), UNUSED_ARG(void *data)) {
34 }
35
36 void timeq_del(UNUSED_ARG(unsigned long when), UNUSED_ARG(timeq_func func), UNUSED_ARG(void *data), UNUSED_ARG(int mask)) {
37 }
38
39 int send_message(UNUSED_ARG(struct userNode *dest), UNUSED_ARG(struct userNode *src), UNUSED_ARG(const char *message), ...) {
40     return 0;
41 }
42
43 struct module *module_register(UNUSED_ARG(const char *name), UNUSED_ARG(enum log_type clog), UNUSED_ARG(const char *helpfile_name), UNUSED_ARG(expand_func_t expand_help)) {
44     return NULL;
45 }
46
47 struct modcmd *modcmd_register(UNUSED_ARG(struct module *module), UNUSED_ARG(const char *name), UNUSED_ARG(modcmd_func_t func), UNUSED_ARG(unsigned int min_argc), UNUSED_ARG(unsigned int flags), ...) {
48     return NULL;
49 }
50
51 void table_send(UNUSED_ARG(struct userNode *from), UNUSED_ARG(const char *to), UNUSED_ARG(unsigned int size), UNUSED_ARG(irc_send_func irc_send), UNUSED_ARG(struct helpfile_table table)) {
52 }
53
54 /* back to our regularly scheduled code: */
55
56 int check_record(const char *key, void *data, UNUSED_ARG(void *extra))
57 {
58     struct record_data *rd = data;
59     switch (rd->type) {
60     case RECDB_INVALID:
61         fprintf(stdout, "Invalid database record type for key %s\n", key);
62         return 1;
63     case RECDB_QSTRING:
64     case RECDB_STRING_LIST:
65         return 0;
66     case RECDB_OBJECT:
67         return dict_foreach(rd->d.object, check_record, NULL) ? 1 : 0;
68     }
69     return 0;
70 }
71
72 int main(int argc, char *argv[])
73 {
74     dict_t db;
75     char *infile;
76
77     if (argc < 2 || argc > 3) {
78         fprintf(stderr, "%s usage: %s <dbfile> [outputfile]\n\n", argv[0], argv[0]);
79         fprintf(stderr, "If [outputfile] is specified, dbfile is rewritten into outputfile after being\nparsed.\n\n");
80         fprintf(stderr, "<dbfile> and/or [outputfile] may be given as '-' to use stdin and stdout,\nrespectively.\n");
81         return 1;
82     }
83
84     tools_init();
85     if (!strcmp(argv[1], "-")) {
86         infile = "/dev/stdin";
87     } else {
88         infile = argv[1];
89     }
90     if (!(db = parse_database(infile))) return 2;
91     fprintf(stdout, "Database read okay.\n");
92     fflush(stdout);
93     if (dict_foreach(db, check_record, 0)) return 3;
94     if (!bad) {
95         fprintf(stdout, "Database checked okay.\n");
96         fflush(stdout);
97     }
98
99     if (argc == 3) {
100         FILE *f;
101
102         if (!strcmp(argv[2], "-")) {
103             f = stdout;
104         } else {
105             if (!(f = fopen(argv[2], "w+"))) {
106                 fprintf(stderr, "fopen: %s\n", strerror(errno));
107                 return 4;
108             }
109         }
110
111         write_database(f, db);
112         fclose(f);
113         fprintf(stdout, "Database written okay.\n");
114         fflush(stdout);
115     }
116
117     return 0;
118 }