Subversion Repositories Okuu

Rev

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