Subversion Repositories IRC-Archiver

Rev

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