-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBot.cpp
More file actions
288 lines (264 loc) · 10 KB
/
Copy pathMyBot.cpp
File metadata and controls
288 lines (264 loc) · 10 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "IRCBot.h"
#include "MyBot.h"
#include <sstream>
void MyBot::help(IrcBot* bot, BotFunctArgs arguments) {
stringstream ss;
ss << "Hello! I'm IRCBot. You can get my source from https://github.com/C0DEHERO/IRCBot/" << endl;
ss << "I'm written in C++ and this is what i can do!" << endl;
ss << arguments.trigger+"cookie <username> gives cookies to <username>" << endl;
ss << arguments.trigger+"roll <faces> rolls a die with <faces> faces. Faces defaults to 6" << endl;
ss << arguments.trigger+"channels lists the channels I'm in" << endl;
ss << arguments.trigger+"time prints my local time" << endl;
bot->sendMessage(ss.str(), arguments.msgChannel);
}
void MyBot::info(IrcBot* bot, BotFunctArgs arguments) {
stringstream ss;
ss << "Hello! I'm IRCBot. You can get my source from https://github.com/C0DEHERO/IRCBot/" << endl;
ss << "I'm written in C++ and this is what i can do!" << endl;
bot->sendMessage(ss.str(), arguments.msgChannel);
}
void MyBot::cookie(IrcBot* bot, BotFunctArgs arguments) {
string argNick = arguments.arg;
if(argNick.empty())
argNick = arguments.msgNick;
bot->sendAction("gives " + argNick + " a cookie", arguments.msgChannel);
}
void MyBot::roll(IrcBot* bot, BotFunctArgs arguments) {
int faces = 1;
if(arguments.arg.empty())
faces = 6;
else
{
try {
faces = stoi(arguments.arg);
} catch (std::invalid_argument) {
bot->sendMessage("You need to input a valid number", arguments.msgChannel);
}
}
bot->sendMessage(std::to_string(rand() % faces + 1), arguments.msgChannel);
}
void MyBot::time(IrcBot* bot, BotFunctArgs arguments) {
bot->sendMessage(bot->timeNow(), arguments.msgChannel);
}
void MyBot::note(IrcBot* bot, BotFunctArgs arguments) {
string message = arguments.arg;
size_t found = message.find(" "); // second space
string nickName = message.substr(0, found); // get the nick name, which is the first argument
message = message.substr(found+1); // get the message, which is the second argument
bot->notes[nickName].push_back("note from "+arguments.msgNick+": "+message);
bot->sendMessage("saved note for "+nickName, arguments.msgChannel);
}
void MyBot::say(IrcBot* bot, BotFunctArgs arguments) {
if (bot->isAdmin(arguments.msgNick)) {
bot->sendMessage(arguments.arg, arguments.msgChannel);
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::say_chan(IrcBot* bot, BotFunctArgs arguments) {
if (bot->isAdmin(arguments.msgNick)) {
vector<string> args;
args = bot->splitString(arguments.arg, " ", 2);
bot->sendMessage(args[1], args[0]);
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::add_admin(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick)) {
bot->addAdmin(arguments.arg);
bot->sendMessage(arguments.arg+" was added as an admin", arguments.msgChannel);
}
}
void MyBot::add_admins(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick)) {
for(auto f : arguments.args) {
bot->addAdmin(f);
bot->sendMessage(f+" was added as an admin", arguments.msgChannel);
}
}
}
void MyBot::remove_admin(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick)) {
bot->removeAdmin(arguments.arg);
bot->sendMessage(arguments.arg+" was removed from the admin list", arguments.msgChannel);
}
}
void MyBot::list_admins(IrcBot* bot, BotFunctArgs arguments) {
stringstream ss;
for(auto f : bot->admins) {
ss << f << " ";
}
string adminNames= ss.str();
adminNames.pop_back();
bot->sendMessage("Admins: "+adminNames, arguments.msgChannel);
}
void MyBot::channel(IrcBot* bot, BotFunctArgs arguments) {
bot->sendMessage(arguments.msgChannel, arguments.msgChannel);
}
void MyBot::channels(IrcBot* bot, BotFunctArgs arguments) {
string message;
for(auto f : bot->channels)
{
message += " " + f;
}
bot->sendMessage(message.erase(0,1), arguments.msgChannel);
}
void MyBot::topic(IrcBot* bot, BotFunctArgs arguments) {
if(arguments.msgNick[0] != '@') {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that", arguments.msgChannel);
} else {
bot->sendData("TOPIC " + arguments.msgChannel + " :" + arguments.arg);
}
}
void MyBot::names(IrcBot* bot, BotFunctArgs arguments) {
string channelName = arguments.arg;
if(channelName.empty() || channelName == "#")
channelName = arguments.msgChannel;
if(channelName[0] != '#') {
bot->sendMessage("Channel name has to start with a #", arguments.msgChannel);
return;
}
if(!bot->checkWhitelist(channelName, bot->channelWhitelist)) {
bot->sendMessage("Channel name contains invalid characters.", arguments.msgChannel);
return;
}
bot->sendData("NAMES " + channelName);
bot->awaiting_names = arguments.msgChannel;
}
void MyBot::name_map(IrcBot* bot, BotFunctArgs arguments) {
if(bot->nicks.find(arguments.msgChannel) != bot->nicks.end()) {
string names;
names = "map: ";
for(auto f : bot->nicks.find(arguments.msgChannel)->second) {
names += f+" ";
}
//bot->sendMessage(names, arguments.msgChannel);
bot->sendMessage(names, bot->my_owner);
}
else {
bot->sendMessage("sorry ._.", arguments.msgChannel);
}
}
void MyBot::send_raw(IrcBot* bot, BotFunctArgs arguments) {
if (bot->isAdmin(arguments.msgNick)) {
bot->sendData(arguments.arg);
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::activate(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick)) {
if(bot->activateFunction(arguments.arg)) {
bot->sendMessage(arguments.arg+" was activated.", arguments.msgChannel);
}
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::deactivate(IrcBot* bot, BotFunctArgs arguments) {
if(arguments.arg == "activate") {
bot->sendMessage("The activate function cannot be deactivated.", arguments.msgChannel);
} else if(bot->isAdmin(arguments.msgNick)) {
if(bot->deactivateFunction(arguments.arg)) {
bot->sendMessage(arguments.arg+" was deactivated.", arguments.msgChannel);
}
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::join(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick)) {
int result = bot->join(arguments.arg);
switch(result) {
case 0:
bot->sendMessage("Joining "+arguments.arg+".", arguments.msgChannel);
break;
case 1:
bot->sendMessage("No channel specified.", arguments.msgChannel);
break;
case 2:
bot->sendMessage("Channel name has to start with a #", arguments.msgChannel);
break;
case 3:
bot->sendMessage("Channel name contains invalid characters.", arguments.msgChannel);
break;
case 4:
bot->sendMessage("I'm already in that channel.", arguments.msgChannel);
break;
}
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::leave(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick) || bot->isMod(arguments.msgNick, arguments.msgChannel)) {
int result = bot->leave(arguments.arg);
switch(result) {
case 0:
bot->sendMessage("Leaving "+arguments.arg+".", arguments.msgChannel);
break;
case 1:
bot->sendMessage("No channel specified.", arguments.msgChannel);
break;
case 2:
bot->sendMessage("Channel name has to start with a #", arguments.msgChannel);
break;
case 3:
bot->sendMessage("Channel name contains invalid characters.", arguments.msgChannel);
break;
case 4:
bot->sendMessage("I'm not in that channel.", arguments.msgChannel);
break;
}
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::quit(IrcBot* bot, BotFunctArgs arguments) {
if(bot->isAdmin(arguments.msgNick)) {
bot->sendMessage("Bye!", arguments.msgChannel);
bot->sendData("QUIT :Bye!");
bot->quit = true;
} else {
bot->sendMessage("Sorry " + arguments.msgNick + ", I'm afraid I can't let you do that.", arguments.msgChannel);
}
}
void MyBot::registerFunctions() {
registerFunction("help", MyBot::help);
registerFunction("info", MyBot::info);
registerFunction("cookie", MyBot::cookie);
registerFunction("roll", MyBot::roll);
registerFunction("time", MyBot::time);
registerFunction("note", MyBot::note);
registerFunction("addadmin", MyBot::add_admin);
registerFunction("addadmins", MyBot::add_admins);
registerFunction("say", MyBot::say);
registerFunction("saychan", MyBot::say_chan);
registerFunction("removeadmin", MyBot::remove_admin);
registerFunction("listadmins", MyBot::list_admins);
registerFunction("channel", MyBot::channel);
registerFunction("channels", MyBot::channels);
registerFunction("topic", MyBot::topic);
registerFunction("names", MyBot::names);
registerFunction("namemap", MyBot::name_map);
registerFunction("sendraw", MyBot::send_raw);
registerFunction("activate", MyBot::activate);
registerFunction("deactivate", MyBot::deactivate);
registerFunction("join", MyBot::join);
registerFunction("leave", MyBot::leave);
registerFunction("quit", MyBot::quit);
}
void MyBot::extraHandle(const string &buf, const string &msgChannel, const string &msgNick) {
if (buf.find("huhu " + nick) != string::npos) {
sendMessage("huhu " + msgNick, msgChannel);
}
if (!notes[msgNick].empty() && !msgChannel.empty() && msgNick != nick) {
vector<string>::iterator it = notes[msgNick].begin();
while(!notes[msgNick].empty()) {
sendMessage(msgNick+": "+*it, msgChannel);
it = notes[msgNick].erase(it);
}
return;
}
}