Subversion Repositories IRC-Archiver

Rev

Rev 8 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: main.c 17 2024-08-30 08:22:50Z nishi $ */
2
 
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
4 nishi 6
#include <unistd.h>
2 nishi 7
#include <sys/stat.h>
17 nishi 8
#include <sys/types.h>
4 nishi 9
#include <stdbool.h>
2 nishi 10
 
3 nishi 11
#include "ia_util.h"
4 nishi 12
#include "ia_bot.h"
8 nishi 13
#include "ia_db.h"
3 nishi 14
 
4 nishi 15
extern bool ia_do_log;
16
 
17
char* host = NULL;
18
int port = 0;
5 nishi 19
char* nickname = NULL;
8 nishi 20
char* webroot = NULL;
7 nishi 21
char* database = NULL;
4 nishi 22
char* username = NULL;
5 nishi 23
char* realname = NULL;
8 nishi 24
char* nickserv = NULL;
4 nishi 25
char* password = NULL;
26
char* admin = NULL;
5 nishi 27
char* channels[128];
28
int chanincr;
4 nishi 29
 
3 nishi 30
int main(int argc, char** argv) {
4 nishi 31
	const char* fn = "archiver.ini";
32
	int i;
33
	bool daemon = true;
34
	for(i = 1; i < argc; i++) {
35
		if(argv[i][0] == '-') {
36
			if(strcmp(argv[i], "-D") == 0) {
37
				ia_do_log = true;
38
				daemon = false;
39
			} else {
40
				fprintf(stderr, "Unknown option: %s\n", argv[i]);
41
				return 1;
42
			}
43
		} else {
44
			fn = argv[i];
45
		}
46
	}
47
 
2 nishi 48
	FILE* f = fopen(fn, "r");
3 nishi 49
	if(f == NULL) {
2 nishi 50
		fprintf(stderr, "Could not open the config: %s\n", fn);
51
		return 1;
52
	}
53
 
54
	struct stat s;
55
	stat(fn, &s);
56
 
57
	char* buf = malloc(s.st_size + 1);
58
	fread(buf, s.st_size, 1, f);
59
	buf[s.st_size] = 0;
60
 
61
	int incr = 0;
62
 
5 nishi 63
	channels[0] = NULL;
64
 
3 nishi 65
	for(i = 0;; i++) {
66
		if(buf[i] == 0 || buf[i] == '\n') {
2 nishi 67
			char oldc = buf[i];
68
			buf[i] = 0;
69
			char* line = buf + incr;
3 nishi 70
			if(strlen(line) > 0 && line[0] != '#') {
2 nishi 71
				int j;
3 nishi 72
				for(j = 0; line[j] != 0; j++) {
73
					if(line[j] == '=') {
2 nishi 74
						line[j] = 0;
75
						char* key = line;
76
						char* value = line + j + 1;
77
 
3 nishi 78
						if(strcmp(key, "host") == 0) {
79
							if(host != NULL) free(host);
80
							host = ia_strdup(value);
81
						} else if(strcmp(key, "port") == 0) {
82
							port = atoi(value);
7 nishi 83
						} else if(strcmp(key, "database") == 0) {
84
							if(database != NULL) free(database);
85
							database = ia_strdup(value);
3 nishi 86
						} else if(strcmp(key, "username") == 0) {
87
							if(username != NULL) free(username);
88
							username = ia_strdup(value);
5 nishi 89
						} else if(strcmp(key, "nickname") == 0) {
90
							if(nickname != NULL) free(nickname);
91
							nickname = ia_strdup(value);
3 nishi 92
						} else if(strcmp(key, "password") == 0) {
93
							if(password != NULL) free(password);
94
							password = ia_strdup(value);
4 nishi 95
						} else if(strcmp(key, "admin") == 0) {
96
							if(admin != NULL) free(admin);
97
							admin = ia_strdup(value);
5 nishi 98
						} else if(strcmp(key, "realname") == 0) {
99
							if(realname != NULL) free(realname);
100
							realname = ia_strdup(value);
8 nishi 101
						} else if(strcmp(key, "webroot") == 0) {
102
							if(webroot != NULL) free(webroot);
103
							webroot = ia_strdup(value);
104
						} else if(strcmp(key, "nickserv") == 0) {
105
							if(nickserv != NULL) free(nickserv);
106
							nickserv = ia_strdup(value);
5 nishi 107
						} else if(strcmp(key, "channel") == 0) {
108
							channels[chanincr++] = ia_strdup(value);
109
							channels[chanincr] = NULL;
3 nishi 110
						}
2 nishi 111
 
112
						break;
113
					}
114
				}
115
			}
116
			incr = i + 1;
117
			if(oldc == 0) break;
118
		}
119
	}
120
 
121
	free(buf);
122
	fclose(f);
3 nishi 123
 
124
	int st = 0;
125
	if(host == NULL) {
126
		fprintf(stderr, "Specify host\n");
127
		st = 1;
128
	}
7 nishi 129
	if(database == NULL) {
130
		fprintf(stderr, "Specify database\n");
131
		st = 1;
132
	}
3 nishi 133
	if(username == NULL) {
134
		fprintf(stderr, "Specify username\n");
135
		st = 1;
136
	}
5 nishi 137
	if(nickname == NULL) {
138
		fprintf(stderr, "Specify nickname\n");
139
		st = 1;
140
	}
3 nishi 141
	if(password == NULL) {
142
		fprintf(stderr, "Specify password\n");
143
		st = 1;
144
	}
4 nishi 145
	if(admin == NULL) {
146
		fprintf(stderr, "Specify admin\n");
147
		st = 1;
148
	}
8 nishi 149
	if(webroot == NULL) {
150
		fprintf(stderr, "Specify webroot\n");
151
		st = 1;
152
	}
5 nishi 153
	if(realname == NULL) {
154
		fprintf(stderr, "Specify realname\n");
155
		st = 1;
156
	}
3 nishi 157
	if(st == 1) return st;
158
 
7 nishi 159
	printf("Initializing the database\n");
160
 
8 nishi 161
	if(ia_db_init() != 0) {
162
		fprintf(stderr, "Failed to open database\n");
163
		goto cleanup;
164
	}
165
 
4 nishi 166
	pid_t pid = 0;
167
	if(daemon) {
7 nishi 168
		printf("Bot spawning a daemon\n");
4 nishi 169
		pid = fork();
170
	}
171
	if(pid == 0) {
172
		ia_bot_loop();
173
		_exit(0);
174
	} else {
175
		printf("Spawned daemon, I am exiting\n");
176
	}
177
 
8 nishi 178
cleanup:
3 nishi 179
	if(host != NULL) free(host);
8 nishi 180
	if(nickserv != NULL) free(nickserv);
181
	if(webroot != NULL) free(webroot);
5 nishi 182
	if(realname != NULL) free(realname);
7 nishi 183
	if(database != NULL) free(database);
3 nishi 184
	if(username != NULL) free(username);
5 nishi 185
	if(nickname != NULL) free(nickname);
3 nishi 186
	if(password != NULL) free(password);
4 nishi 187
	if(admin != NULL) free(admin);
5 nishi 188
	for(i = 0; channels[i] != NULL; i++) {
189
		free(channels[i]);
190
	}
8 nishi 191
	return st;
2 nishi 192
}