Subversion Repositories Okuu

Rev

Rev 4 | Rev 6 | 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 5 2024-09-11 00:26:25Z 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;
32
 
33
extern char* ircserver;
34
extern char* ircchan;
35
extern char* ircpass;
36
extern char* ircuser;
37
extern char* ircnick;
38
extern char* ircreal;
39
extern int ircport;
40
 
41
int ok_sock;
42
struct sockaddr_in ok_addr;
43
 
44
void ok_close(int sock){
45
	close(sock);
46
}
47
 
48
void ok_bot_kill(int sig){
49
	fprintf(stderr, "Shutdown\n");
50
	ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown (Signal)");
51
	exit(1);
52
}
53
 
54
bool ok_is_number(const char* str) {
55
	int i;
56
	for(i = 0; str[i] != 0; i++) {
57
		if(!('0' <= str[i] && str[i] <= '9')) return false;
58
	}
59
	return true;
60
}
61
 
62
char* ok_null(const char* str){
63
	return str == NULL ? "(null)" : (char*)str;
64
}
65
 
66
int namesort(const struct dirent** a_, const struct dirent** b_){
67
	const struct dirent* a = *a_;
68
	const struct dirent* b = *b_;
69
	return atoi(a->d_name) - atoi(b->d_name);
70
}
71
 
72
int nodots(const struct dirent* d){
73
	return (strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) ? 0 : 1;
74
}
75
 
76
void ok_bot(void){
77
	if((ok_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0){
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
	}
3 nishi 94
 
95
	if(connect(ok_sock, (struct sockaddr*)&ok_addr, sizeof(ok_addr)) < 0){
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
 
106
	if(ircpass != NULL && strlen(ircpass) > 0){
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
 
124
	while(1){
125
		int r = poll(pollfds, 1, 100);
126
		if(!(r > 0 && pollfds[0].revents & POLLIN)){
127
			/* 100ms sleep, technically */
128
			uint64_t count = 0;
129
			FILE* f = fopen(nntpcount, "rb");
130
			if(f != NULL){
131
				fread(&count, sizeof(count), 1, f);
132
				fclose(f);
133
			}
134
			struct dirent** list;
135
			int n = scandir(nntppath, &list, nodots, namesort);
136
			if(n >= 0){
137
				int i;
138
				for(i = 0; i < n; i++){
4 nishi 139
					if(!sendable){
140
						free(list[i]);
141
						continue;
142
					}
3 nishi 143
					if(count <= atoi(list[i]->d_name)){
144
						sprintf(construct, "%s/%s", nntppath, list[i]->d_name);
145
						if(ok_news_read(construct) == 0){
146
							char* tmp = ok_strcat3("PRIVMSG ", ircchan, " :\x03" "07[USENET] ~ ");
147
							char* temp = ok_strcat3(tmp, news_entry.from, "\x03 ");
148
							free(tmp);
149
							int j;
150
							int incr = 0;
151
							for(j = 0;; j++){
152
								if(news_entry.content[j] == 0 || news_entry.content[j] == '\n'){
153
									char* line = malloc(j - incr + 1);
154
									line[j - incr] = 0;
155
									memcpy(line, news_entry.content + incr, j - incr);
156
 
157
									if(strlen(line) > 0){
158
										char* msg = ok_strcat(temp, line);
159
										ircfw_socket_send_cmd(ok_sock, NULL, msg);
160
										free(msg);
161
									}
162
 
163
									free(line);
164
									incr = j + 1;
165
									if(news_entry.content[j] == 0) break;
166
								}
167
							}
168
							free(temp);
169
						}else{
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
			}
178
			f = fopen(nntpcount, "wb");
179
			if(f != NULL){
180
				fwrite(&count, sizeof(count), 1, f);
181
				fclose(f);
182
			}
183
			continue;
184
		}
185
		int st = ircfw_socket_read_cmd(ok_sock);
186
		if(st != 0){
187
			fprintf(stderr, "Bad response\n");
188
			return;
189
		}
190
		if(strlen(ircfw_message.command) == 3 && ok_is_number(ircfw_message.command)){
191
			int res = atoi(ircfw_message.command);
192
			if(!is_in && 400 <= res && res <= 599){
193
				fprintf(stderr, "Bad response\n");
194
				return;
195
			}else if(400 <= res && res <= 599){
196
				fprintf(stderr, "Ignored error: %d\n", res);
197
				continue;
198
			}
199
			if(res == 376){
200
				is_in = true;
201
				fprintf(stderr, "Login successful\n");
202
				sprintf(construct, "JOIN :%s", ircchan);
203
				ircfw_socket_send_cmd(ok_sock, NULL, construct);
4 nishi 204
			}else if(res == 331 || res == 332){
205
				sendable = true;
3 nishi 206
			}
207
		}else{
208
			if(strcasecmp(ircfw_message.command, "PING") == 0){
209
				fprintf(stderr, "Ping request\n");
210
				sprintf(construct, "PONG :%s", ok_null(ircfw_message.prefix));
211
				ircfw_socket_send_cmd(ok_sock, NULL, construct);
212
			}else if(strcasecmp(ircfw_message.command, "PRIVMSG") == 0){
213
				char* prefix = ircfw_message.prefix;
214
				char** params = ircfw_message.params;
215
 
216
				int len = 0;
217
				if(params != NULL) {
218
					int i;
219
					for(i = 0; params[i] != NULL; i++);
220
					len = i;
221
				}
222
				if(prefix != NULL && len == 2) {
223
					char* sentin = params[0];
224
					char* msg = params[1];
225
					char* nick = ok_strdup(prefix);
226
					int i;
227
					for(i = 0; nick[i] != 0; i++) {
228
						if(nick[i] == '!') {
229
							nick[i] = 0;
230
							break;
231
						}
232
					}
233
					if(msg[0] == 1 && msg[strlen(msg) - 1] == 1){
234
						/* CTCP */
235
						if(strcasecmp(msg, "\x01VERSION\x01") == 0){
236
							sprintf(construct, "NOTICE %s :\x01VERSION Okuu %s / IRC Frameworks %s: http://nishi.boats/okuu\x01", nick, OKUU_VERSION, IRCFW_VERSION);
237
							ircfw_socket_send_cmd(ok_sock, NULL, construct);
238
						}
239
					}else if(sentin[0] == '#'){
240
						/* This was sent in channel */
5 nishi 241
						if(ok_news_write(nick, msg) != 0){
242
							sprintf(construct, "PRIVMSG %s :Could not send the message to the USENET", sentin);
243
							ircfw_socket_send_cmd(ok_sock, NULL, construct);
244
						}
3 nishi 245
					}
246
 
247
					free(nick);
248
				}
249
			}else if(strcasecmp(ircfw_message.command, "ERROR") == 0){
250
				if(ircfw_message.params != NULL){
251
					int i;
252
					for(i = 0; ircfw_message.params[i] != NULL; i++) fprintf(stderr, "ERROR: %s\n", ircfw_message.params[i]);
253
				}
254
			}
255
		}
256
	}
257
 
258
	ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown");
259
 
260
	free(construct);
261
}