Subversion Repositories IRC-Archiver

Rev

Rev 7 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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