Subversion Repositories IRC-Archiver

Rev

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