-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsamplebot.c
More file actions
369 lines (297 loc) · 10.1 KB
/
Copy pathsamplebot.c
File metadata and controls
369 lines (297 loc) · 10.1 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* A sample IRC bot using libbotty.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <libgen.h>
#include "botapi.h"
#include "commands/mailbox.h"
#include "commands/links.h"
/*=====================================================
* Bot Configuration
*===================================================*/
BotInfo botInfo = {
.info = &(IrcInfo) {},
.useSSL = 1
};
/*=====================================================
* Bot Callback functions
*===================================================*/
/*
* Callback functions can be used for adding
* features or logic to notable responses or events.
*/
static int onConnect(void *data, IrcMsg *msg) {
syslog(LOG_INFO, "BOT HAS CONNECTED!");
return 0;
}
static int onJoin(void *data, IrcMsg *msg) {
botty_say((BotInfo *)data, msg->channel, "Hello, World!");
return 0;
}
static int onMsg(void *data, IrcMsg *msg) {
if (!data || !msg) return -1;
syslog(LOG_DEBUG, "Recieved msg from %s in %s: %s", msg->nick, msg->channel, msg->msg);
MailBox_notifyUser((BotInfo *)data, msg->channel, msg->nick);
links_store(msg->msg);
return 0;
}
static int onUsrJoin(void *data, IrcMsg *msg) {
if (!data || !msg) return -1;
syslog(LOG_INFO, "'%s' has joined the channel", msg->nick);
MailBox_notifyUser((BotInfo *)data, msg->channel, msg->nick);
return 0;
}
static int onUsrPart(void *data, IrcMsg *msg) {
if (!data || !msg) return -1;
syslog(LOG_INFO, "%s has left the channel", msg->nick);
MailBox_resetUserNotification(msg->nick);
return 0;
}
static int onUsrQuit(void *data, IrcMsg *msg) {
if (!data || !msg) return -1;
syslog(LOG_INFO, "%s has disconnected the server", msg->nick);
MailBox_resetUserNotification(msg->nick);
return 0;
}
static int onNickChange(void *data, IrcMsg *msg) {
if (!data || !msg) return -1;
BotInfo *i = (BotInfo *)data;
char *newNick = msg->msg;
syslog(LOG_DEBUG, "OldNick: %s, NewNick: %s: Channel: %s", msg->nick, newNick, msg->channel);
if (botty_msgContainsValidChannel(msg)) {
botty_say(i, msg->channel, "I see what you did there %s... AKA %s!", newNick, msg->nick);
MailBox_notifyUser((BotInfo *) data, msg->channel, newNick);
}
return 0;
}
static int onUsrInvite(void *data, IrcMsg *msg) {
if (!strncmp(msg->nick, botInfo.master, MAX_NICK_LEN))
botty_join((BotInfo *)data, msg->msg);
return 0;
}
static int onServerResp(void *data, IrcMsg *msg) {
if (!data || !msg) return -1;
syslog(LOG_DEBUG, "Received code: %s", msg->action);
for (int i = 0; i < MAX_PARAMETERS; i++) {
if (!msg->msgTok[i]) break;
syslog(LOG_DEBUG, "Parameter %d: %s", i, msg->msgTok[i]);
}
return 0;
}
/*=====================================================
* Bot Command functions
*===================================================*/
/*
* Some fun commands that aren't necessary, but illustrate
* how to use this bot api.
*/
int botcmd_say(CmdData *data, char *args[MAX_BOT_ARGS]) {
char *responseTarget = botcmd_builtin_getTarget(data);
botty_say(data->bot, responseTarget, args[1]);
return 0;
}
/* Hacky roulette game implementation */
int botcmd_roulette(CmdData *data, char *args[MAX_BOT_ARGS]) {
#define BULLETS 6
#define QUOTE "You've got to ask yourself one question: \"do I feel lucky?\" Well do you punk?"
//preserve game state across function calls
typedef struct roulette {
char state:2;
unsigned char shot:3;
unsigned char loop:2;
unsigned char doQuote: 1;
} roulette;
static roulette game = {.shot = 0, .state = -1};
char *responseTarget = botcmd_builtin_getTarget(data);
game.loop = 0;
do {
switch (game.state) {
default: {
//first person to call roulette forces the gun to load
//and then pulls the trigger on themselves.
game.loop++;
}
case 0:
botty_ctcpSend(data->bot, responseTarget, "ACTION", "loads a round and spins the cylinder.");
game.shot = (rand() % BULLETS + 1) + 1;
game.doQuote = (game.shot >= BULLETS);
game.state = 1;
break;
case 1:
if (--game.shot == 0) {
botty_ctcpSend(data->bot, responseTarget, "ACTION", "BANG! %s is dead.", data->msg->nick);
//reload the gun once it has been shot
game.state = 0;
game.loop++;
} else
botty_ctcpSend(data->bot, responseTarget, "ACTION", "Click... %s is safe.", data->msg->nick);
if (game.doQuote && game.shot == 2) botty_say(data->bot, responseTarget, QUOTE);
break;
}
} while (game.loop--);
return 0;
}
int botcmd_roll(CmdData *data, char *args[MAX_BOT_ARGS]) {
#define MAX_DICE 9
char *responseTarget = botcmd_builtin_getTarget(data);
char msg[MAX_MSG_LEN];
int numDice = 0, dieMax = 0, n = 0;
char delim = '\0';
if (!args[1]) {
botty_say(data->bot, responseTarget, "Missing dice information");
return 0;
}
n = sscanf(args[1], "%u%c%u", &numDice, &delim, &dieMax);
if (n < 3) {
botty_say(data->bot, responseTarget, "Invalid roll request: missing parameter");
return 0;
}
else if (numDice > MAX_DICE || numDice < 1) {
botty_say(data->bot, responseTarget, "Invalid roll request: only 1 through 9 dice may be rolled.");
return 0;
}
else if (dieMax < 2) {
botty_say(data->bot, responseTarget, "Invalid roll request: dice must have a max greater than 1");
return 0;
}
int offset = snprintf(msg, MAX_MSG_LEN, "Rolled: ");
for (int i = 0; i < numDice; i++) {
int num = (rand() % dieMax) + 1;
offset += snprintf(msg + offset, MAX_MSG_LEN, "%d ", num);
}
snprintf(msg + offset, MAX_MSG_LEN, "for %s", data->msg->nick);
botty_ctcpSend(data->bot, responseTarget, "ACTION", msg);
return 0;
}
static void printNick(NickListEntry *n, void *data) {
syslog(LOG_INFO, "NICKDUMP: %s", n->nick);
}
int botcmd_dumpnames(CmdData *data, char *args[MAX_BOT_ARGS]) {
char *responseTarget = botcmd_builtin_getTarget(data);
bot_foreachName(data->bot, responseTarget, NULL, &printNick);
return 0;
}
static int _draw_free(void *a) {
FILE *fh = (FILE *)a;
fclose(fh);
return 0;
}
//A sample 'process' function that can be given to the bot
static int _draw(void *b, char *procOwner, BotProcessArgs *args) {
BotInfo *bot = (BotInfo *)b;
FILE *input = (FILE *)args->data;
char *responseTarget = args->target;
char buf[MAX_MSG_LEN];
if (feof(input))
goto _fin;
char *s = fgets(buf, MAX_MSG_LEN, input);
if (!s)
goto _fin;
char *newline = strchr(s, '\n');
if (newline) *newline = '\0';
if (botty_say(bot, responseTarget, ". %s", s) < 0)
goto _fin;
//return 1 to keep the process going
return 1;
_fin:
//return negative value to indicate the process
//is complete
_draw_free(args->data);
return -1;
}
int botcmd_draw(CmdData *data, char *args[MAX_BOT_ARGS]) {
char *caller = data->msg->nick;
char *responseTarget = botty_respondDest(data);
char *script = "draw";
char path[512];
char *file = args[1];
if (!file) {
botty_say(data->bot, responseTarget, "%s: please specify a picture.", caller);
return 0;
}
snprintf(path, sizeof(path), "art/%s.txt", file);
FILE *f = fopen(path, "rb");
if (!f) {
botty_say(data->bot, responseTarget, "File '%s' does not exist!", file);
return 0;
}
BotProcessArgs *sArgs = botty_makeProcessArgs((void *)f, responseTarget, &_draw_free);
if (!sArgs) {
botty_say(data->bot, responseTarget, "There was an error allocating memory to execute command: %s", script);
fclose(f);
return 0;
}
//initialize and start the draw process
//A process will block all input for a given bot until
//it has completed the assigned process.
botty_runProcess(data->bot, &_draw, sArgs, script, caller);
return 0;
}
int test_notice(CmdData *data, char *args[MAX_BOT_ARGS]) {
char *caller = data->msg->nick;
char *responseTarget = caller;
char *msg = args[1];
botty_send(data->bot, responseTarget, NOTICE_ACTION, NULL, "%s", msg);
return 0;
}
int main(int argc, char *argv[]) {
char configPath[MAX_FILEPATH_LEN];
char *cfgPtr = configPath;
openlog(argv[0], LOG_PERROR | LOG_CONS | LOG_PID, LOG_SYSLOG);
if (argc < 2) {
realpath(argv[0], configPath);
char *config = dirname(configPath);
snprintf(configPath, MAX_FILEPATH_LEN - 1, "%s/%s", config, DEFAULT_CONFIG_FILE);
syslog(LOG_NOTICE, "Loading default config file: %s", cfgPtr);
}
else {
cfgPtr = argv[1];
syslog(LOG_NOTICE, "Loading user specified config: %s", cfgPtr);
}
if (botty_loadConfig(&botInfo, cfgPtr)) {
syslog(LOG_CRIT, "Error loading config file: %s", cfgPtr);
return -1;
}
int status = 0;
time_t t;
srand((unsigned) time(&t));
//hook in some callback functions
botty_setCallback(&botInfo, CALLBACK_CONNECT, &onConnect);
botty_setCallback(&botInfo, CALLBACK_JOIN, &onJoin);
botty_setCallback(&botInfo, CALLBACK_MSG, &onMsg);
botty_setCallback(&botInfo, CALLBACK_USRJOIN, &onUsrJoin);
botty_setCallback(&botInfo, CALLBACK_USRPART, &onUsrPart);
botty_setCallback(&botInfo, CALLBACK_USRQUIT, &onUsrQuit);
botty_setCallback(&botInfo, CALLBACK_SERVERCODE, &onServerResp);
botty_setCallback(&botInfo, CALLBACK_USRNICKCHANGE, &onNickChange);
botty_setCallback(&botInfo, CALLBACK_USRINVITE, &onUsrInvite);
if (botty_init(&botInfo, argc, argv, 0))
return -1;
//register some extra commands
botty_addCommand(&botInfo, "say", 0, 2, &botcmd_say);
botty_addCommand(&botInfo,"roll", 0, 2, &botcmd_roll);
botty_addCommand(&botInfo, "roulette", 0, 1, &botcmd_roulette);
botty_addCommand(&botInfo, "nicks", 0, 1, &botcmd_dumpnames);
botty_addCommand(&botInfo, "msg", 0, 3, &botcmd_msg);
botty_addCommand(&botInfo, "mail", 0, 1, &botcmd_mail);
botty_addCommand(&botInfo, "draw", 0, 2, &botcmd_draw);
botty_addCommand(&botInfo, "links", 0, 1, &links_print);
botty_addCommand(&botInfo, "whisper", 0, 2, &test_notice);
//start the bot connection to the irc server
botty_connect(&botInfo);
while (((status = botty_process(&botInfo)) >= 0)) {
//prevent 100% cpu usage
nanosleep(&(struct timespec) {
.tv_sec = 0,
.tv_nsec = ONE_SEC_IN_NS/120
}, NULL);
}
botty_cleanup(&botInfo);
MailBox_destroyAll();
links_purge();
closelog();
return status;
}