Subversion Repositories Okuu

Rev

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