Subversion Repositories Okuu

Rev

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