Subversion Repositories Okuu

Rev

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