Subversion Repositories IRC-Archiver

Rev

Rev 23 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: bot.c 24 2024-09-03 16:33:08Z nishi $ */
2
 
3
#include "ia_bot.h"
4
 
5 nishi 5
#include "ia_util.h"
8 nishi 6
#include "ia_db.h"
5 nishi 7
 
11 nishi 8
#include "web_html.h"
9
 
5 nishi 10
#include "ircfw.h"
11
 
12
#include <signal.h>
4 nishi 13
#include <stdbool.h>
5 nishi 14
#include <stdlib.h>
4 nishi 15
#include <stdio.h>
11 nishi 16
#include <time.h>
4 nishi 17
#include <string.h>
17 nishi 18
#include <strings.h>
20 nishi 19
#include <sys/stat.h>
5 nishi 20
#include <unistd.h>
4 nishi 21
 
22
#include <sys/socket.h>
23
#include <netinet/tcp.h>
24
#include <arpa/inet.h>
25
 
23 nishi 26
#ifdef __FreeBSD__
27
#include <netinet/in.h>
28
#endif
29
 
19 nishi 30
#define IRCARC_VERSION "1.01"
6 nishi 31
 
13 nishi 32
const char* ircarc_version = IRCARC_VERSION;
33
 
4 nishi 34
int ia_sock;
5 nishi 35
struct sockaddr_in ia_addr;
4 nishi 36
 
37
bool ia_do_log = false;
38
 
39
void ia_log(const char* txt) {
40
	if(!ia_do_log) return;
41
	fprintf(stderr, "%s\n", txt);
42
}
43
 
44
extern char* host;
6 nishi 45
extern char* admin;
5 nishi 46
extern char* realname;
47
extern char* nickname;
48
extern char* username;
49
extern char* password;
8 nishi 50
extern char* nickserv;
20 nishi 51
extern char* webroot;
5 nishi 52
extern char* channels[];
4 nishi 53
extern int port;
54
 
5 nishi 55
void ia_close(int sock) { close(sock); }
56
 
57
const char* ia_null(const char* str) {
58
	if(str == NULL) return "(null)";
59
	return str;
60
}
61
 
62
bool ia_is_number(const char* str) {
63
	int i;
64
	for(i = 0; str[i] != 0; i++) {
65
		if(!('0' <= str[i] && str[i] <= '9')) return false;
66
	}
67
	return true;
68
}
69
 
70
bool loop = true;
71
 
72
void ia_bot_kill(int sig) {
73
	ia_log("Shutdown");
74
	ircfw_socket_send_cmd(ia_sock, NULL, "QUIT :Shutdown (Signal)");
75
	exit(1);
76
}
77
 
4 nishi 78
void ia_bot_loop(void) {
79
	if((ia_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
80
		ia_log("Socket creation failure");
81
		return;
82
	}
83
 
5 nishi 84
	loop = true;
85
 
4 nishi 86
	int yes = 1;
87
 
88
	if(setsockopt(ia_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(yes)) < 0) {
89
		ia_log("setsockopt failure");
5 nishi 90
		ia_close(ia_sock);
4 nishi 91
		return;
92
	}
93
 
5 nishi 94
	bzero((char*)&ia_addr, sizeof(ia_addr));
95
	ia_addr.sin_family = PF_INET;
96
	ia_addr.sin_addr.s_addr = inet_addr(host);
97
	ia_addr.sin_port = htons(port);
4 nishi 98
 
99
	if(connect(ia_sock, (struct sockaddr*)&ia_addr, sizeof(ia_addr)) < 0) {
100
		ia_log("Connection failure");
5 nishi 101
		ia_close(ia_sock);
102
		return;
4 nishi 103
	}
104
 
5 nishi 105
	signal(SIGINT, ia_bot_kill);
106
	signal(SIGTERM, ia_bot_kill);
107
 
108
	char* construct = malloc(1025);
109
 
110
	if(password != NULL && strlen(password) > 0) {
111
		sprintf(construct, "PASS :%s", password);
112
		ircfw_socket_send_cmd(ia_sock, NULL, construct);
113
	}
114
 
115
	sprintf(construct, "USER %s %s %s :%s", username, username, username, realname);
116
	ircfw_socket_send_cmd(ia_sock, NULL, construct);
117
 
118
	sprintf(construct, "NICK :%s", nickname);
119
	ircfw_socket_send_cmd(ia_sock, NULL, construct);
120
 
121
	bool is_in = false;
122
 
7 nishi 123
	while(loop) {
5 nishi 124
		int st = ircfw_socket_read_cmd(ia_sock);
125
		if(st != 0) {
126
			ia_log("Bad response");
127
			return;
128
		}
129
		if(strlen(ircfw_message.command) == 3 && ia_is_number(ircfw_message.command)) {
130
			int res = atoi(ircfw_message.command);
131
			if(!is_in && 400 <= res && res <= 599) {
132
				ia_log("Bad response");
133
				return;
134
			} else if(400 <= res && res <= 599) {
135
				sprintf(construct, "Ignored error: %d", res);
136
				ia_log(construct);
137
				continue;
138
			}
139
			if(res == 376) {
140
				is_in = true;
141
				ia_log("Login successful");
142
				int i;
143
				for(i = 0; channels[i] != NULL; i++) {
144
					sprintf(construct, "JOIN :%s", channels[i]);
145
					ircfw_socket_send_cmd(ia_sock, NULL, construct);
146
				}
6 nishi 147
				sprintf(construct, "NOTICE %s :IRC-Archiver %s is ready to accept requests", admin, IRCARC_VERSION);
148
				ircfw_socket_send_cmd(ia_sock, NULL, construct);
8 nishi 149
				if(nickserv != NULL) {
150
					sprintf(construct, "PRIVMSG NickServ :%s", nickserv);
151
					ircfw_socket_send_cmd(ia_sock, NULL, construct);
152
				}
5 nishi 153
			}
154
		} else {
155
			if(strcasecmp(ircfw_message.command, "PING") == 0) {
156
				ia_log("Ping request");
157
				sprintf(construct, "PONG :%s", ia_null(ircfw_message.prefix));
158
				ircfw_socket_send_cmd(ia_sock, NULL, construct);
159
			} else if(strcasecmp(ircfw_message.command, "PRIVMSG") == 0) {
160
				char* prefix = ircfw_message.prefix;
161
				char** params = ircfw_message.params;
6 nishi 162
				int i;
163
				int len = 0;
164
				if(params != NULL) {
165
					for(i = 0; params[i] != NULL; i++)
166
						;
167
					len = i;
168
				}
169
				if(prefix != NULL && len == 2) {
170
					char* sentin = params[0];
171
					char* msg = params[1];
5 nishi 172
					char* nick = ia_strdup(prefix);
173
					for(i = 0; nick[i] != 0; i++) {
174
						if(nick[i] == '!') {
175
							nick[i] = 0;
176
							break;
177
						}
178
					}
179
 
6 nishi 180
					if(msg[0] == 1 && msg[strlen(msg) - 1] == 1) {
181
						/* CTCP */
182
						if(strcasecmp(msg, "\x01VERSION\x01") == 0) {
15 nishi 183
							sprintf(construct, "NOTICE %s :\x01VERSION IRC-Archiver %s / IRC Frameworks %s: http://nishi.boats/ircarc\x01", nick, IRCARC_VERSION, IRCFW_VERSION);
6 nishi 184
							ircfw_socket_send_cmd(ia_sock, NULL, construct);
185
						}
186
					} else if(sentin[0] == '#') {
187
						/* This was sent in channel ; log it */
8 nishi 188
						ia_db_put(nick, sentin, msg);
6 nishi 189
					} else {
190
						/* Command, I guess */
7 nishi 191
						int i;
11 nishi 192
 
193
						char* name = NULL;
194
						char* chn = NULL;
195
						char* frm = NULL;
196
						char* to = NULL;
197
 
7 nishi 198
						char* prm = ia_strdup(msg);
199
						int incr = 0;
11 nishi 200
						int t = 0;
7 nishi 201
						for(i = 0;; i++) {
202
							if(prm[i] == ' ' || prm[i] == 0) {
203
								char oldc = prm[i];
204
								prm[i] = 0;
205
 
206
								if(strcasecmp(prm, "help") == 0) {
11 nishi 207
									sprintf(construct, "PRIVMSG %s :HELP   Show this help", nick);
7 nishi 208
									ircfw_socket_send_cmd(ia_sock, NULL, construct);
209
									sprintf(construct, "PRIVMSG %s :SHUTDOWN   Shutdown the bot", nick);
210
									ircfw_socket_send_cmd(ia_sock, NULL, construct);
11 nishi 211
									sprintf(construct, "PRIVMSG %s :ARCHIVE [name] [channel] <yyyy-mm-dd-hh:mm:ss> <yyyy-mm-dd-hh:mm:ss>   Archive the log", nick);
212
									ircfw_socket_send_cmd(ia_sock, NULL, construct);
213
								} else if(strcasecmp(prm, "archive") == 0) {
214
									if(strcmp(admin, nick) == 0) {
215
										if(t <= 4 && t != 0) {
216
											char* p = prm + incr;
217
											if(t == 1) {
218
												name = p;
219
											} else if(t == 2) {
220
												chn = p;
221
											} else if(t == 3) {
222
												frm = p;
223
											} else if(t == 4) {
224
												to = p;
225
											}
226
										}
227
										if(oldc == 0) {
228
											if(chn == NULL) {
229
												sprintf(construct, "PRIVMSG %s :Insufficient arguments", nick);
230
												ircfw_socket_send_cmd(ia_sock, NULL, construct);
18 nishi 231
											} else if(strcmp(name, "index") == 0) {
232
												sprintf(construct, "PRIVMSG %s :You cannot use that name", nick);
233
												ircfw_socket_send_cmd(ia_sock, NULL, construct);
11 nishi 234
											} else {
235
												web_range_t range;
236
												struct tm from_tm;
237
												memset(&from_tm, 0, sizeof(from_tm));
238
												struct tm to_tm;
239
												memset(&to_tm, 0, sizeof(to_tm));
240
												time_t tfrom = 0;
241
												time_t tto = 0;
242
												if(frm != NULL) {
13 nishi 243
													if(strptime(frm, "%Y/%m/%d-%H:%M:%S", &from_tm) == NULL) {
11 nishi 244
														sprintf(construct, "PRIVMSG %s :Date parsing failure", nick);
245
														ircfw_socket_send_cmd(ia_sock, NULL, construct);
246
														break;
247
													}
248
													tfrom = mktime(&from_tm);
249
												}
250
												if(to != NULL) {
13 nishi 251
													if(strptime(to, "%Y/%m/%d-%H:%M:%S", &to_tm) == NULL) {
11 nishi 252
														sprintf(construct, "PRIVMSG %s :Date parsing failure", nick);
253
														ircfw_socket_send_cmd(ia_sock, NULL, construct);
254
														break;
255
													}
256
													tto = mktime(&to_tm);
257
												}
18 nishi 258
 
259
												char* hpath = ia_strcat4(webroot, "/", name, ".html");
260
												struct stat s;
261
												if(stat(hpath, &s) == 0) {
262
													sprintf(construct, "PRIVMSG %s :Archive which has the same name already exists", nick);
263
													ircfw_socket_send_cmd(ia_sock, NULL, construct);
264
													break;
265
												}
266
 
11 nishi 267
												int j;
268
												bool found = false;
269
												for(j = 0; channels[j] != NULL; j++) {
270
													if(strcmp(channels[j], chn) == 0) {
271
														found = true;
272
														break;
273
													}
274
												}
275
												if(!found) {
276
													sprintf(construct, "PRIVMSG %s :I do not know the channel", nick);
277
													ircfw_socket_send_cmd(ia_sock, NULL, construct);
278
													break;
279
												}
280
												range.from = tfrom;
281
												range.to = tto;
282
												range.channel = chn;
283
												web_html_generate(name, range);
24 nishi 284
 
285
												sprintf(construct, "PRIVMSG %s :Archived this channel", chn);
286
												ircfw_socket_send_cmd(ia_sock, NULL, construct);
11 nishi 287
											}
288
										}
289
									} else {
290
										sprintf(construct, "PRIVMSG %s :You must be an admin to use this", nick);
291
										ircfw_socket_send_cmd(ia_sock, NULL, construct);
292
										break;
293
									}
7 nishi 294
								} else if(strcasecmp(prm, "shutdown") == 0) {
295
									if(strcmp(admin, nick) == 0) {
296
										loop = false;
297
									} else {
298
										sprintf(construct, "PRIVMSG %s :You must be an admin to use this", nick);
299
										ircfw_socket_send_cmd(ia_sock, NULL, construct);
300
									}
301
									break;
302
								} else {
303
									sprintf(construct, "PRIVMSG %s :Unknown command `%s'. See HELP.", nick, prm);
304
									ircfw_socket_send_cmd(ia_sock, NULL, construct);
305
									break;
306
								}
307
 
308
								incr = i + 1;
11 nishi 309
								t++;
7 nishi 310
								if(oldc == 0) break;
311
							}
312
						}
313
						free(prm);
6 nishi 314
					}
315
 
5 nishi 316
					free(nick);
317
				}
318
			}
319
		}
4 nishi 320
	}
5 nishi 321
 
322
	ircfw_socket_send_cmd(ia_sock, NULL, "QUIT :Shutdown");
323
 
324
	free(construct);
4 nishi 325
}