Subversion Repositories Okuu

Rev

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