Subversion Repositories Okuu

Rev

Rev 14 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 nishi 1
/* $Id: bot.c 15 2024-09-13 16:13:27Z nishi $ */
2
 
3
#include "ok_bot.h"
4
 
5
#include "ok_util.h"
6
#include "ok_news.h"
7
 
8
#include "ircfw.h"
9
 
10
#include <stdint.h>
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <strings.h>
14
#include <unistd.h>
15
#include <signal.h>
16
#include <stdbool.h>
17
#include <dirent.h>
18
#include <poll.h>
13 nishi 19
#include <sys/wait.h>
3 nishi 20
 
21
#include <sys/socket.h>
22
#include <netinet/tcp.h>
23
#include <arpa/inet.h>
24
 
25
#ifdef __FreeBSD__
26
#include <netinet/in.h>
27
#endif
28
 
29
#define OKUU_VERSION "1.00"
30
 
31
extern char* nntppath;
32
extern char* nntpcount;
6 nishi 33
extern char* nntpfrom;
3 nishi 34
 
35
extern char* ircserver;
36
extern char* ircchan;
37
extern char* ircpass;
38
extern char* ircuser;
39
extern char* ircnick;
40
extern char* ircreal;
41
extern int ircport;
42
 
43
int ok_sock;
44
struct sockaddr_in ok_addr;
45
 
13 nishi 46
void ok_close(int sock) {
14 nishi 47
	char c;
13 nishi 48
	while(close(sock) == 0);
14 nishi 49
	while(recv(sock, &c, 1, 0) > 0);
13 nishi 50
}
3 nishi 51
 
11 nishi 52
void ok_bot_kill(int sig) {
3 nishi 53
	fprintf(stderr, "Shutdown\n");
54
	ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown (Signal)");
55
	exit(1);
56
}
57
 
58
bool ok_is_number(const char* str) {
59
	int i;
60
	for(i = 0; str[i] != 0; i++) {
61
		if(!('0' <= str[i] && str[i] <= '9')) return false;
62
	}
63
	return true;
64
}
65
 
11 nishi 66
char* ok_null(const char* str) { return str == NULL ? "(null)" : (char*)str; }
3 nishi 67
 
11 nishi 68
int namesort(const struct dirent** a_, const struct dirent** b_) {
3 nishi 69
	const struct dirent* a = *a_;
70
	const struct dirent* b = *b_;
71
	return atoi(a->d_name) - atoi(b->d_name);
72
}
73
 
11 nishi 74
int nodots(const struct dirent* d) { return (strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) ? 0 : 1; }
3 nishi 75
 
11 nishi 76
void ok_bot(void) {
77
	if((ok_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
3 nishi 78
		fprintf(stderr, "Socket creation failure\n");
79
		return;
80
	}
81
 
82
	bzero((char*)&ok_addr, sizeof(ok_addr));
83
	ok_addr.sin_family = PF_INET;
84
	ok_addr.sin_addr.s_addr = inet_addr(ircserver);
85
	ok_addr.sin_port = htons(ircport);
5 nishi 86
 
87
	int yes = 1;
88
 
89
	if(setsockopt(ok_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(yes)) < 0) {
90
		fprintf(stderr, "setsockopt failure");
91
		ok_close(ok_sock);
92
		return;
93
	}
11 nishi 94
 
95
	if(connect(ok_sock, (struct sockaddr*)&ok_addr, sizeof(ok_addr)) < 0) {
3 nishi 96
		fprintf(stderr, "Connection failure\n");
97
		ok_close(ok_sock);
98
		return;
99
	}
100
 
101
	signal(SIGINT, ok_bot_kill);
102
	signal(SIGTERM, ok_bot_kill);
103
 
15 nishi 104
	char* construct = malloc(4097);
3 nishi 105
 
11 nishi 106
	if(ircpass != NULL && strlen(ircpass) > 0) {
3 nishi 107
		sprintf(construct, "PASS :%s", ircpass);
108
		ircfw_socket_send_cmd(ok_sock, NULL, construct);
109
	}
110
 
111
	sprintf(construct, "USER %s %s %s :%s", ircuser, ircuser, ircuser, ircreal);
112
	ircfw_socket_send_cmd(ok_sock, NULL, construct);
113
 
114
	sprintf(construct, "NICK :%s", ircnick);
115
	ircfw_socket_send_cmd(ok_sock, NULL, construct);
116
 
117
	bool is_in = false;
4 nishi 118
	bool sendable = false;
3 nishi 119
 
120
	struct pollfd pollfds[1];
121
	pollfds[0].fd = ok_sock;
122
	pollfds[0].events = POLLIN | POLLPRI;
123
 
11 nishi 124
	while(1) {
3 nishi 125
		int r = poll(pollfds, 1, 100);
11 nishi 126
		if(!(r > 0 && pollfds[0].revents & POLLIN)) {
3 nishi 127
			/* 100ms sleep, technically */
128
			uint64_t count = 0;
129
			FILE* f = fopen(nntpcount, "rb");
11 nishi 130
			if(f != NULL) {
3 nishi 131
				fread(&count, sizeof(count), 1, f);
132
				fclose(f);
133
			}
134
			struct dirent** list;
135
			int n = scandir(nntppath, &list, nodots, namesort);
11 nishi 136
			if(n >= 0) {
3 nishi 137
				int i;
15 nishi 138
				uint64_t last = 0;
11 nishi 139
				for(i = 0; i < n; i++) {
15 nishi 140
					if(!sendable || strcmp(list[i]->d_name, ".") == 0 || strcmp(list[i]->d_name, "..") == 0) {
4 nishi 141
						free(list[i]);
142
						continue;
143
					}
11 nishi 144
					if(count <= atoi(list[i]->d_name)) {
3 nishi 145
						sprintf(construct, "%s/%s", nntppath, list[i]->d_name);
11 nishi 146
						if(ok_news_read(construct) == 0) {
147
							if(strcmp(news_entry.from, nntpfrom) != 0) {
148
								char* tmp = ok_strcat3("PRIVMSG ", ircchan,
149
										       " :\x03"
150
										       "07[USENET] ~ ");
8 nishi 151
								char* temp = ok_strcat3(tmp, news_entry.from, "\x03 ");
152
								free(tmp);
153
								int j;
154
								int incr = 0;
11 nishi 155
								for(j = 0;; j++) {
156
									if(news_entry.content[j] == 0 || news_entry.content[j] == '\n') {
8 nishi 157
										char* line = malloc(j - incr + 1);
158
										line[j - incr] = 0;
159
										memcpy(line, news_entry.content + incr, j - incr);
11 nishi 160
 
161
										if(strlen(line) > 0) {
8 nishi 162
											char* msg = ok_strcat(temp, line);
163
											ircfw_socket_send_cmd(ok_sock, NULL, msg);
164
											free(msg);
165
											usleep(1000 * 100); /* Sleep for 100ms */
166
										}
11 nishi 167
 
8 nishi 168
										free(line);
169
										incr = j + 1;
170
										if(news_entry.content[j] == 0) break;
3 nishi 171
									}
172
								}
8 nishi 173
								free(temp);
3 nishi 174
							}
11 nishi 175
						} else {
3 nishi 176
							fprintf(stderr, "Could not read %s\n", construct);
177
						}
178
					}
15 nishi 179
					last = atoi(list[i]->d_name) + 1;
3 nishi 180
					free(list[i]);
181
				}
15 nishi 182
				count = last;
3 nishi 183
				free(list);
184
			}
11 nishi 185
			if(sendable) {
7 nishi 186
				f = fopen(nntpcount, "wb");
11 nishi 187
				if(f != NULL) {
7 nishi 188
					fwrite(&count, sizeof(count), 1, f);
189
					fclose(f);
190
				}
3 nishi 191
			}
192
			continue;
193
		}
194
		int st = ircfw_socket_read_cmd(ok_sock);
11 nishi 195
		if(st != 0) {
3 nishi 196
			fprintf(stderr, "Bad response\n");
197
			return;
198
		}
11 nishi 199
		if(strlen(ircfw_message.command) == 3 && ok_is_number(ircfw_message.command)) {
3 nishi 200
			int res = atoi(ircfw_message.command);
11 nishi 201
			if(!is_in && 400 <= res && res <= 599) {
3 nishi 202
				fprintf(stderr, "Bad response\n");
203
				return;
11 nishi 204
			} else if(400 <= res && res <= 599) {
3 nishi 205
				fprintf(stderr, "Ignored error: %d\n", res);
206
				continue;
207
			}
11 nishi 208
			if(res == 376) {
3 nishi 209
				is_in = true;
210
				fprintf(stderr, "Login successful\n");
211
				sprintf(construct, "JOIN :%s", ircchan);
212
				ircfw_socket_send_cmd(ok_sock, NULL, construct);
15 nishi 213
			} else if(res == 366) {
4 nishi 214
				sendable = true;
3 nishi 215
			}
11 nishi 216
		} else {
217
			if(strcasecmp(ircfw_message.command, "PING") == 0) {
3 nishi 218
				fprintf(stderr, "Ping request\n");
219
				sprintf(construct, "PONG :%s", ok_null(ircfw_message.prefix));
220
				ircfw_socket_send_cmd(ok_sock, NULL, construct);
11 nishi 221
			} else if(strcasecmp(ircfw_message.command, "PRIVMSG") == 0) {
3 nishi 222
				char* prefix = ircfw_message.prefix;
223
				char** params = ircfw_message.params;
224
 
225
				int len = 0;
226
				if(params != NULL) {
227
					int i;
11 nishi 228
					for(i = 0; params[i] != NULL; i++)
229
						;
3 nishi 230
					len = i;
231
				}
232
				if(prefix != NULL && len == 2) {
233
					char* sentin = params[0];
234
					char* msg = params[1];
235
					char* nick = ok_strdup(prefix);
236
					int i;
237
					for(i = 0; nick[i] != 0; i++) {
238
						if(nick[i] == '!') {
239
							nick[i] = 0;
240
							break;
241
						}
242
					}
11 nishi 243
					if(msg[0] == 1 && msg[strlen(msg) - 1] == 1) {
3 nishi 244
						/* CTCP */
11 nishi 245
						if(strcasecmp(msg, "\x01VERSION\x01") == 0) {
3 nishi 246
							sprintf(construct, "NOTICE %s :\x01VERSION Okuu %s / IRC Frameworks %s: http://nishi.boats/okuu\x01", nick, OKUU_VERSION, IRCFW_VERSION);
247
							ircfw_socket_send_cmd(ok_sock, NULL, construct);
248
						}
11 nishi 249
					} else if(sentin[0] == '#') {
3 nishi 250
						/* This was sent in channel */
13 nishi 251
						pid_t pid = fork();
252
						int code;
253
						if(pid == 0){
14 nishi 254
							close(ok_sock);
13 nishi 255
							_exit(ok_news_write(nick, msg));
256
						}else{
257
							int status;
258
							waitpid(pid, &status, 0);
259
							code = WEXITSTATUS(status);
260
						}
261
						if(code != 0) {
5 nishi 262
							sprintf(construct, "PRIVMSG %s :Could not send the message to the USENET", sentin);
263
							ircfw_socket_send_cmd(ok_sock, NULL, construct);
264
						}
3 nishi 265
					}
266
 
267
					free(nick);
268
				}
11 nishi 269
			} else if(strcasecmp(ircfw_message.command, "ERROR") == 0) {
270
				if(ircfw_message.params != NULL) {
3 nishi 271
					int i;
272
					for(i = 0; ircfw_message.params[i] != NULL; i++) fprintf(stderr, "ERROR: %s\n", ircfw_message.params[i]);
273
				}
274
			}
275
		}
276
	}
277
 
278
	ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown");
279
 
280
	free(construct);
281
}