Subversion Repositories IRC-Archiver

Rev

Rev 19 | Rev 23 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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