Subversion Repositories IRC-Archiver

Rev

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