Subversion Repositories Okuu

Rev

Rev 13 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 nishi 1
/* $Id: bot.c 14 2024-09-12 22:19:34Z 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
 
104
	char* construct = malloc(1025);
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;
11 nishi 138
				for(i = 0; i < n; i++) {
139
					if(!sendable) {
4 nishi 140
						free(list[i]);
141
						continue;
142
					}
11 nishi 143
					if(count <= atoi(list[i]->d_name)) {
3 nishi 144
						sprintf(construct, "%s/%s", nntppath, list[i]->d_name);
11 nishi 145
						if(ok_news_read(construct) == 0) {
146
							if(strcmp(news_entry.from, nntpfrom) != 0) {
147
								char* tmp = ok_strcat3("PRIVMSG ", ircchan,
148
										       " :\x03"
149
										       "07[USENET] ~ ");
8 nishi 150
								char* temp = ok_strcat3(tmp, news_entry.from, "\x03 ");
151
								free(tmp);
152
								int j;
153
								int incr = 0;
11 nishi 154
								for(j = 0;; j++) {
155
									if(news_entry.content[j] == 0 || news_entry.content[j] == '\n') {
8 nishi 156
										char* line = malloc(j - incr + 1);
157
										line[j - incr] = 0;
158
										memcpy(line, news_entry.content + incr, j - incr);
11 nishi 159
 
160
										if(strlen(line) > 0) {
8 nishi 161
											char* msg = ok_strcat(temp, line);
162
											ircfw_socket_send_cmd(ok_sock, NULL, msg);
163
											free(msg);
164
											usleep(1000 * 100); /* Sleep for 100ms */
165
										}
11 nishi 166
 
8 nishi 167
										free(line);
168
										incr = j + 1;
169
										if(news_entry.content[j] == 0) break;
3 nishi 170
									}
171
								}
8 nishi 172
								free(temp);
3 nishi 173
							}
11 nishi 174
						} else {
3 nishi 175
							fprintf(stderr, "Could not read %s\n", construct);
176
						}
177
					}
178
					free(list[i]);
179
				}
180
				count = atoi(list[i - 1]->d_name) + 1;
181
				free(list);
182
			}
11 nishi 183
			if(sendable) {
7 nishi 184
				f = fopen(nntpcount, "wb");
11 nishi 185
				if(f != NULL) {
7 nishi 186
					fwrite(&count, sizeof(count), 1, f);
187
					fclose(f);
188
				}
3 nishi 189
			}
190
			continue;
191
		}
192
		int st = ircfw_socket_read_cmd(ok_sock);
11 nishi 193
		if(st != 0) {
3 nishi 194
			fprintf(stderr, "Bad response\n");
195
			return;
196
		}
11 nishi 197
		if(strlen(ircfw_message.command) == 3 && ok_is_number(ircfw_message.command)) {
3 nishi 198
			int res = atoi(ircfw_message.command);
11 nishi 199
			if(!is_in && 400 <= res && res <= 599) {
3 nishi 200
				fprintf(stderr, "Bad response\n");
201
				return;
11 nishi 202
			} else if(400 <= res && res <= 599) {
3 nishi 203
				fprintf(stderr, "Ignored error: %d\n", res);
204
				continue;
205
			}
11 nishi 206
			if(res == 376) {
3 nishi 207
				is_in = true;
208
				fprintf(stderr, "Login successful\n");
209
				sprintf(construct, "JOIN :%s", ircchan);
210
				ircfw_socket_send_cmd(ok_sock, NULL, construct);
11 nishi 211
			} else if(res == 331 || res == 332) {
4 nishi 212
				sendable = true;
3 nishi 213
			}
11 nishi 214
		} else {
215
			if(strcasecmp(ircfw_message.command, "PING") == 0) {
3 nishi 216
				fprintf(stderr, "Ping request\n");
217
				sprintf(construct, "PONG :%s", ok_null(ircfw_message.prefix));
218
				ircfw_socket_send_cmd(ok_sock, NULL, construct);
11 nishi 219
			} else if(strcasecmp(ircfw_message.command, "PRIVMSG") == 0) {
3 nishi 220
				char* prefix = ircfw_message.prefix;
221
				char** params = ircfw_message.params;
222
 
223
				int len = 0;
224
				if(params != NULL) {
225
					int i;
11 nishi 226
					for(i = 0; params[i] != NULL; i++)
227
						;
3 nishi 228
					len = i;
229
				}
230
				if(prefix != NULL && len == 2) {
231
					char* sentin = params[0];
232
					char* msg = params[1];
233
					char* nick = ok_strdup(prefix);
234
					int i;
235
					for(i = 0; nick[i] != 0; i++) {
236
						if(nick[i] == '!') {
237
							nick[i] = 0;
238
							break;
239
						}
240
					}
11 nishi 241
					if(msg[0] == 1 && msg[strlen(msg) - 1] == 1) {
3 nishi 242
						/* CTCP */
11 nishi 243
						if(strcasecmp(msg, "\x01VERSION\x01") == 0) {
3 nishi 244
							sprintf(construct, "NOTICE %s :\x01VERSION Okuu %s / IRC Frameworks %s: http://nishi.boats/okuu\x01", nick, OKUU_VERSION, IRCFW_VERSION);
245
							ircfw_socket_send_cmd(ok_sock, NULL, construct);
246
						}
11 nishi 247
					} else if(sentin[0] == '#') {
3 nishi 248
						/* This was sent in channel */
13 nishi 249
						pid_t pid = fork();
250
						int code;
251
						if(pid == 0){
14 nishi 252
							close(ok_sock);
13 nishi 253
							_exit(ok_news_write(nick, msg));
254
						}else{
255
							int status;
256
							waitpid(pid, &status, 0);
257
							code = WEXITSTATUS(status);
258
						}
259
						if(code != 0) {
5 nishi 260
							sprintf(construct, "PRIVMSG %s :Could not send the message to the USENET", sentin);
261
							ircfw_socket_send_cmd(ok_sock, NULL, construct);
262
						}
3 nishi 263
					}
264
 
265
					free(nick);
266
				}
11 nishi 267
			} else if(strcasecmp(ircfw_message.command, "ERROR") == 0) {
268
				if(ircfw_message.params != NULL) {
3 nishi 269
					int i;
270
					for(i = 0; ircfw_message.params[i] != NULL; i++) fprintf(stderr, "ERROR: %s\n", ircfw_message.params[i]);
271
				}
272
			}
273
		}
274
	}
275
 
276
	ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown");
277
 
278
	free(construct);
279
}