-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.cpp
More file actions
262 lines (224 loc) · 9.46 KB
/
Copy pathbot.cpp
File metadata and controls
262 lines (224 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
class IRCBot {
private:
int _sockfd;
std::string _buffer;
std::set<std::string> _banwords;
std::set<std::string> _joined_channels;
std::string _nick;
std::string _password;
time_t _last_list_check;
void send_raw(const std::string& msg) {
std::string full = msg + "\r\n";
send(_sockfd, full.c_str(), full.length(), 0);
}
void process_line(const std::string& line) {
// PING/PONG
if (line.find("PING") == 0) {
send_raw("PONG " + line.substr(5));
return;
}
// Check for OPER confirmation
if (line.find("381") != std::string::npos || line.find("IRC operator") != std::string::npos) {
std::cout << "\033[32m[✓]\033[0m Bot is now an IRC operator" << std::endl;
// Request list of all channels to auto-join them
send_raw("LIST");
}
// Check for error on KICK
if (line.find("482") != std::string::npos) {
std::cout << "\033[31m[✗]\033[0m Permission denied - OPER failed" << std::endl;
}
// Parse LIST response (322 = channel list item)
if (line.find(" 322 ") != std::string::npos) {
// Format: :server 322 nick #channel users :topic
size_t chan_start = line.find("#");
if (chan_start != std::string::npos) {
std::string channel = line.substr(chan_start);
channel = channel.substr(0, channel.find(" "));
if (_joined_channels.find(channel) == _joined_channels.end()) {
_joined_channels.insert(channel);
send_raw("JOIN " + channel);
std::cout << "\033[36m[→]\033[0m Joining existing channel " << channel << std::endl;
}
}
}
// JOIN notification - auto-join new channels
if (line.find("JOIN") != std::string::npos) {
size_t pos = line.find("#");
if (pos != std::string::npos) {
std::string channel = line.substr(pos);
channel = channel.substr(0, channel.find(" "));
channel = channel.substr(0, channel.find("\r"));
// Check if it's our own JOIN confirmation
if (line.find(":" + _nick + "!") == 0 || line.find(":" + _nick + " ") == 0) {
std::cout << "\033[36m[→]\033[0m Joined " << channel << std::endl;
}
// Auto-join if it's someone else creating a new channel
else if (_joined_channels.find(channel) == _joined_channels.end()) {
_joined_channels.insert(channel);
std::cout << "\033[36m[→]\033[0m Auto-joining " << channel << std::endl;
send_raw("JOIN " + channel);
}
}
}
// PRIVMSG - check for ban words and commands
if (line.find("PRIVMSG") != std::string::npos) {
// Parse: :nick!user@host PRIVMSG #channel :message
// OR: :nick PRIVMSG #channel :message
if (line[0] != ':') return; // Must start with :
size_t nick_end = line.find('!');
if (nick_end == std::string::npos) {
// No !, try to find space (format: :nick PRIVMSG)
nick_end = line.find(' ');
}
size_t chan_start = line.find("#");
size_t msg_start = line.find(" :", chan_start);
if (nick_end != std::string::npos && chan_start != std::string::npos && msg_start != std::string::npos) {
std::string nick = line.substr(1, nick_end - 1);
std::string channel = line.substr(chan_start);
channel = channel.substr(0, channel.find(" "));
std::string message = line.substr(msg_start + 2);
// Clean message from \r\n
size_t end = message.find("\r");
if (end != std::string::npos) message = message.substr(0, end);
end = message.find("\n");
if (end != std::string::npos) message = message.substr(0, end);
// Ignore our own messages
if (nick == _nick) {
return;
}
// Command: !addban <word>
if (message.find("!addban ") == 0) {
std::string word = message.substr(8);
word = word.substr(0, word.find(" "));
word = word.substr(0, word.find("\r"));
word = word.substr(0, word.find("\n"));
_banwords.insert(word);
send_raw("PRIVMSG " + channel + " :Ban word added: " + word);
std::cout << "\033[33m[+]\033[0m Ban word added: " << word << std::endl;
return;
}
// Command: !listbans
if (message.find("!listbans") == 0) {
std::string list = "Ban words: ";
for (std::set<std::string>::iterator it = _banwords.begin(); it != _banwords.end(); ++it) {
list += *it + " ";
}
send_raw("PRIVMSG " + channel + " :" + list);
return;
}
// Check for ban words
for (std::set<std::string>::iterator it = _banwords.begin(); it != _banwords.end(); ++it) {
if (message.find(*it) != std::string::npos) {
std::cout << "\033[31m[!]\033[0m Kicked " << nick << " from " << channel << " (reason: " << *it << ")" << std::endl;
send_raw("KICK " + channel + " " + nick + " :Ban word detected: " + *it);
return;
}
}
}
}
}
public:
IRCBot(const std::string& nick) : _sockfd(-1), _nick(nick), _last_list_check(0) {
// Ban words par défaut
_banwords.insert("badword");
_banwords.insert("spam");
}
~IRCBot() {
if (_sockfd != -1)
close(_sockfd);
}
bool connect(const std::string& host, int port, const std::string& password) {
_password = password;
_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (_sockfd < 0) {
std::cerr << "Error creating socket" << std::endl;
return false;
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_pton(AF_INET, host.c_str(), &server_addr.sin_addr);
if (::connect(_sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
std::cerr << "Error connecting to server" << std::endl;
return false;
}
// Register and become operator
send_raw("CAP LS");
send_raw("PASS " + password);
send_raw("NICK " + _nick);
send_raw("USER " + _nick + " 0 * :IRC Bot");
send_raw("CAP END");
send_raw("OPER admin admin123");
return true;
}
void join_channel(const std::string& channel) {
_joined_channels.insert(channel);
send_raw("JOIN " + channel);
}
void run() {
struct pollfd pfd;
pfd.fd = _sockfd;
pfd.events = POLLIN;
char buf[4096];
std::cout << "\033[32m=== BotGuard Active ===\033[0m" << std::endl;
std::cout << "Ban words: ";
for (std::set<std::string>::iterator it = _banwords.begin(); it != _banwords.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
while (true) {
// Check for new channels every 10 seconds
time_t now = time(NULL);
if (now - _last_list_check >= 2) {
send_raw("LIST");
_last_list_check = now;
}
int ret = poll(&pfd, 1, 100);
if (ret > 0 && (pfd.revents & POLLIN)) {
int n = recv(_sockfd, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
std::cerr << "Connection closed" << std::endl;
break;
}
buf[n] = '\0';
_buffer += buf;
// Process complete lines
size_t pos;
while ((pos = _buffer.find("\r\n")) != std::string::npos) {
std::string line = _buffer.substr(0, pos);
_buffer = _buffer.substr(pos + 2);
if (!line.empty())
process_line(line);
}
}
}
}
};
int main(int argc, char** argv) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <port> <password>" << std::endl;
return 1;
}
int port = atoi(argv[1]);
std::string password = argv[2];
IRCBot bot("BotGuard");
if (!bot.connect("127.0.0.1", port, password)) {
return 1;
}
bot.run();
return 0;
}