Subversion Repositories IRC-Archiver

Rev

Rev 3 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 4
Line 1... Line 1...
1
/* $Id: main.c 3 2024-08-29 18:49:46Z nishi $ */
1
/* $Id: main.c 4 2024-08-30 01:49:08Z nishi $ */
2
 
2
 
3
#include <stdio.h>
3
#include <stdio.h>
4
#include <stdlib.h>
4
#include <stdlib.h>
5
#include <string.h>
5
#include <string.h>
-
 
6
#include <unistd.h>
6
#include <sys/stat.h>
7
#include <sys/stat.h>
-
 
8
#include <stdbool.h>
7
 
9
 
8
#include "ia_util.h"
10
#include "ia_util.h"
9
#include "ia_util.h"
11
#include "ia_bot.h"
-
 
12
 
-
 
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;
10
 
20
 
11
int main(int argc, char** argv) {
21
int main(int argc, char** argv) {
12
	const char* fn = argv[1] == NULL ? "archiver.ini" : argv[1];
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
 
13
	FILE* f = fopen(fn, "r");
39
	FILE* f = fopen(fn, "r");
14
	if(f == NULL) {
40
	if(f == NULL) {
15
		fprintf(stderr, "Could not open the config: %s\n", fn);
41
		fprintf(stderr, "Could not open the config: %s\n", fn);
16
		return 1;
42
		return 1;
17
	}
43
	}
Line 21... Line 47...
21
 
47
 
22
	char* buf = malloc(s.st_size + 1);
48
	char* buf = malloc(s.st_size + 1);
23
	fread(buf, s.st_size, 1, f);
49
	fread(buf, s.st_size, 1, f);
24
	buf[s.st_size] = 0;
50
	buf[s.st_size] = 0;
25
 
51
 
26
	int i;
-
 
27
	int incr = 0;
52
	int incr = 0;
28
 
53
 
29
	char* host = NULL;
-
 
30
	int port = 0;
-
 
31
	char* username = NULL;
-
 
32
	char* password = NULL;
-
 
33
 
-
 
34
	for(i = 0;; i++) {
54
	for(i = 0;; i++) {
35
		if(buf[i] == 0 || buf[i] == '\n') {
55
		if(buf[i] == 0 || buf[i] == '\n') {
36
			char oldc = buf[i];
56
			char oldc = buf[i];
37
			buf[i] = 0;
57
			buf[i] = 0;
38
			char* line = buf + incr;
58
			char* line = buf + incr;
Line 53... Line 73...
53
							if(username != NULL) free(username);
73
							if(username != NULL) free(username);
54
							username = ia_strdup(value);
74
							username = ia_strdup(value);
55
						} else if(strcmp(key, "password") == 0) {
75
						} else if(strcmp(key, "password") == 0) {
56
							if(password != NULL) free(password);
76
							if(password != NULL) free(password);
57
							password = ia_strdup(value);
77
							password = ia_strdup(value);
-
 
78
						} else if(strcmp(key, "admin") == 0) {
-
 
79
							if(admin != NULL) free(admin);
-
 
80
							admin = ia_strdup(value);
58
						}
81
						}
59
 
82
 
60
						break;
83
						break;
61
					}
84
					}
62
				}
85
				}
Line 80... Line 103...
80
	}
103
	}
81
	if(password == NULL) {
104
	if(password == NULL) {
82
		fprintf(stderr, "Specify password\n");
105
		fprintf(stderr, "Specify password\n");
83
		st = 1;
106
		st = 1;
84
	}
107
	}
-
 
108
	if(admin == NULL) {
-
 
109
		fprintf(stderr, "Specify admin\n");
-
 
110
		st = 1;
-
 
111
	}
85
	if(st == 1) return st;
112
	if(st == 1) return st;
86
 
113
 
-
 
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
 
87
	if(host != NULL) free(host);
126
	if(host != NULL) free(host);
88
	if(username != NULL) free(username);
127
	if(username != NULL) free(username);
89
	if(password != NULL) free(password);
128
	if(password != NULL) free(password);
-
 
129
	if(admin != NULL) free(admin);
90
}
130
}