-
Notifications
You must be signed in to change notification settings - Fork 0
/
prbot.c
421 lines (360 loc) · 11.1 KB
/
prbot.c
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* Copyright 2013 Sean Stangl (sean.stangl@gmail.com)
*
* This file is part of PRBot.
*
* PRBot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PRBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PRBot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sqlite3.h>
#include <regex.h>
#include "irc.h"
#define BUF_LEN 1024
#define DATABASE_NAME "prbot.sqlite3"
#define IRC_HOST "irc.rizon.net"
#define IRC_PORT "6667"
#define IRC_NICK "prbot"
#define IRC_CHANNEL "#prbottest"
static const char INITIALIZE_DB[] =
"CREATE TABLE IF NOT EXISTS prs ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" nick VARCHAR(255) NOT NULL,"
" lift VARCHAR(255) NOT NULL,"
" date INTEGER NOT NULL,"
" sets INTEGER NOT NULL,"
" reps INTEGER NOT NULL,"
" kgs REAL NOT NULL"
");";
static const char TOP_PRS[] =
"SELECT * "
"FROM "
" (SELECT nick, lift, date, sets, reps, kgs"
" FROM prs"
" WHERE nick = ?"
" ORDER BY date DESC) "
"GROUP BY lift, nick "
"ORDER BY lift ASC;";
static const char INSERT_PR[] =
"INSERT INTO prs (nick, lift, date, sets, reps, kgs)"
"VALUES (?, ?, ?, ?, ?, ?)";
static const char *LIFTS[] = {
"bench press",
"overhead press",
"squat",
"front squat",
"power clean"
};
struct prbot_pr {
char *nick;
char *lift;
time_t date;
int sets;
int reps;
double kgs;
};
static double inline
kg2lb(double kgs)
{
return kgs * 2.205;
}
static double inline
lb2kg(double lbs)
{
return lbs / 2.205;
}
// Global database handle ( :( ).
static sqlite3 *db;
static bool
insert_pr(struct prbot_pr *pr)
{
sqlite3_stmt *stmt;
if (sqlite3_prepare(db, INSERT_PR, -1, &stmt, NULL) != SQLITE_OK) {
// Something broke. :(
return false;
}
sqlite3_bind_text(stmt, 1, pr->nick, -1, NULL);
sqlite3_bind_text(stmt, 2, pr->lift, -1, NULL);
sqlite3_bind_int64(stmt, 3, (sqlite3_int64) pr->date);
sqlite3_bind_int(stmt, 4, pr->sets);
sqlite3_bind_int(stmt, 5, pr->reps);
sqlite3_bind_double(stmt, 6, pr->kgs);
if (sqlite3_step(stmt) != SQLITE_DONE) {
// Couldn't run this statement.
return false;
}
if (sqlite3_finalize(stmt)) {
// Couldn't finalize statement.
return false;
}
return true;
}
static const char NEW_PR_PATTERN[] = "^(.+) of ([0-9]+)(\\.[0-9]+)?(kg|lb) ([0-9]+)x([0-9]+)";
static regex_t new_pr_regex;
static bool
tryparse_pr(char *msg, struct prbot_pr *pr)
{
#define NUM_MATCHES 7
// There are 7 match groups:
//
// 0. full string
// 1. lift
// 2. weight (whole part)
// 3. weight (decimal part)
// 4. unit
// 5. sets
// 6. reps
regmatch_t matches[NUM_MATCHES];
if (regexec(&new_pr_regex, msg, NUM_MATCHES, matches, 0)) {
return false;
}
// The regexp will match the full unit, so we can just predicate on the
// first character (l for lb, k for kg).
bool needs_conv_from_lb = msg[matches[4].rm_so] == 'l';
// Null-terminate some parts of the string so they can be parsed.
for (size_t i = 0; i < NUM_MATCHES; ++i) {
if (i == 2) {
// But don't null-terminate the whole weight.
continue;
}
msg[matches[i].rm_eo] = '\0';
}
#undef NUM_MATCHES
pr->lift = msg + matches[1].rm_so;
for (char *c = pr->lift; *c != '\0'; ++c) {
*c = tolower(*c);
}
pr->kgs = atof(msg + matches[2].rm_so);
if (needs_conv_from_lb) {
pr->kgs = lb2kg(pr->kgs);
}
pr->sets = atoi(msg + matches[5].rm_so);
pr->reps = atoi(msg + matches[6].rm_so);
return true;
}
static bool
handle_ping(int fd, struct ircmsg_ping *ping)
{
irc_pong(fd, ping->text);
return true;
}
static bool
handle_part(int fd, struct ircmsg_part *part)
{
return true;
}
static bool
handle_join(int fd, struct ircmsg_join *join)
{
return true;
}
static bool
handle_cmd_record(int fd, struct ircmsg_privmsg *msg, char *head)
{
struct prbot_pr pr;
if (!tryparse_pr(head, &pr)) {
irc_privmsg(fd, msg->chan, "%s: check your syntax, expected: "
"<lift> of <weight><unit> <sets>x<reps>",
msg->name.nick);
return true;
}
bool lift_ok = false;
for (size_t i = 0; i < sizeof LIFTS / sizeof LIFTS[0]; ++i) {
if (strcmp(LIFTS[i], pr.lift) == 0) {
lift_ok = true;
break;
}
}
if (!lift_ok) {
irc_privmsg(fd, msg->chan, "%s: sorry, I don't think \"%s\" is a real lift",
msg->name.nick, pr.lift);
return true;
}
// Normalize nicknames to lowercase, so we don't get duplicates of nicknames.
char nick_lower[BUF_LEN];
strcpy(nick_lower, msg->name.nick);
for (char *c = nick_lower; *c != '\0'; ++c) {
*c = tolower(*c);
}
// TODO: remove me later and use a proper verification thing
if (strcmp(nick_lower, "number1stunna")) {
irc_privmsg(fd, msg->chan, "%s: haha, no.",
msg->name.nick);
return true;
}
pr.nick = nick_lower;
pr.date = time(NULL);
if (!insert_pr(&pr)) {
irc_privmsg(fd, msg->chan, "%s: couldn't record your PR, try again later :(",
msg->name.nick);
return true;
}
irc_privmsg(fd, msg->chan, "%s: recorded your PR for %s of %.2fkg %dx%d",
msg->name.nick, pr.lift, pr.kgs, pr.sets, pr.reps);
return true;
}
static bool
handle_cmd_records(int fd, struct ircmsg_privmsg *msg, char *head) {
// Normalize the nickname to lowercase, because that keeps the database
// consistent (as is done in other places).
for (char *c = head; *c != '\0'; ++c) {
*c = tolower(*c);
// Also turn the trailing CR/LF to NUL so we can use the nickname as a
// null-terminated string.
if (*c == '\r' || *c == '\n') {
*c = '\0';
break;
}
}
sqlite3_stmt *stmt;
char out[BUF_LEN] = "\0";
char *cur = out;
if (sqlite3_prepare(db, TOP_PRS, -1, &stmt, NULL) != SQLITE_OK) {
// Something broke. :(
irc_privmsg(fd, msg->chan, "%s: sorry, couldn't get PRs (prepare)");
return true;
}
sqlite3_bind_text(stmt, 1, head, -1, NULL);
int retval;
do {
switch ((retval = sqlite3_step(stmt))) {
case SQLITE_DONE:
break;
case SQLITE_ROW: {
// There are 6 columns:
//
// 0. nick
// 1. lift
// 2. date
// 3. sets
// 4. reps
// 5. kgs
const char *nick = (const char *) sqlite3_column_text(stmt, 0);
const char *lift = (const char *) sqlite3_column_text(stmt, 1);
long date = (long) sqlite3_column_int64(stmt, 2);
int sets = sqlite3_column_int(stmt, 3);
int reps = sqlite3_column_int(stmt, 4);
double kgs = sqlite3_column_double(stmt, 5);
int n = snprintf(cur, BUF_LEN - (int) (cur - out), "| %s of %.2fkg %dx%d ", lift, kgs, sets, reps);
if (n < 0) {
// TODO: split output over multiple lines, rather than just silencing it
goto finalize;
}
cur += n;
break;
}
default:
// Some error occured during PR retrieval.
irc_privmsg(fd, msg->chan, "%s: sorry, couldn't get PRs (iterate)");
return true;
}
} while (retval == SQLITE_ROW);
finalize:
if (sqlite3_finalize(stmt)) {
// Couldn't finalize statement.
irc_privmsg(fd, msg->chan, "%s: sorry, couldn't get PRs (finalize)");
return true;
}
irc_privmsg(fd, msg->chan, "PRs for %s %s",
head, out[0] == '\0' ? "| none" : out);
return true;
}
static inline bool
BeginsWith(char *s1, char *s2)
{
return (strncasecmp(s1, s2, strlen(s2)) == 0);
}
static bool
handle_privmsg(int fd, struct ircmsg_privmsg *msg)
{
// Only handle messages directed at the bot.
if (strncmp(msg->text, IRC_NICK, strlen(IRC_NICK)) != 0)
return true;
// Only handle messages in a channel.
if (msg->chan[0] != '#')
return true;
char *cmd = msg->text + strlen(IRC_NICK ": ");
if (BeginsWith(cmd, "help"))
return handle_cmd_help(fd, msg, head + 4);
if (BeginsWith(cmd, "record "))
return handle_cmd_record(fd, msg, head + 7);
if (BeginsWith(cmd, "records "))
return handle_cmd_records(fd, msg, head + 8);
irc_privmsg(fd, msg->chan, "%s: shut the fuck up.", msg->name.nick);
return true;
}
static bool
handle_kick(int fd, struct ircmsg_kick *kick)
{
return true;
}
static bool
dispatch_handler(int fd, struct ircmsg *msg)
{
switch (msg->type) {
case IRCMSG_UNKNOWN: return true;
case IRCMSG_PING: return handle_ping(fd, &msg->u.ping);
case IRCMSG_PART: return handle_part(fd, &msg->u.part);
case IRCMSG_JOIN: return handle_join(fd, &msg->u.join);
case IRCMSG_PRIVMSG: return handle_privmsg(fd, &msg->u.privmsg);
case IRCMSG_KICK: return handle_kick(fd, &msg->u.kick);
default: return false;
}
}
int
main(int argc, char *argv[])
{
// Compile some regexes.
if (regcomp(&new_pr_regex, NEW_PR_PATTERN, REG_EXTENDED)) {
fprintf(stderr, "Failed to compile regex.\n");
return 1;
}
// Initialize SQLite gunk.
if (sqlite3_open(DATABASE_NAME, &db)) {
fprintf(stderr, "Failed to open database: %s\n", sqlite3_errmsg(db));
return 1;
}
if (sqlite3_exec(db, INITIALIZE_DB, 0, 0, 0)) {
fprintf(stderr, "Failed to initialize database: %s\n", sqlite3_errmsg(db));
return 1;
}
// Kick off the IRC connection.
char buf[BUF_LEN];
struct ircbuf ircbuf;
ircbuf_init(&ircbuf, buf, BUF_LEN);
int fd = irc_connect(IRC_HOST, IRC_PORT);
if (fd < 0) {
fprintf(stderr, "Failed to open connection.\n");
return 1;
}
irc_nick(fd, IRC_NICK, NULL);
irc_join(fd, IRC_CHANNEL);
char *line;
while ((line = irc_getline(fd, &ircbuf))) {
printf("%s\n", line);
struct ircmsg msg;
irc_parseline(line, &msg);
if (!dispatch_handler(fd, &msg))
return 2;
}
return 0;
}