add nsfw filter for #bronymusic
[fillybot.git] / mod.c
blobe69868ede8ff6d55eb050a67041e788e0f99ef14
1 #include "mod.h"
3 #include <libxml/xmlstring.h>
4 #include <libxml/HTMLparser.h>
6 #include <stdint.h>
7 #include <inttypes.h>
8 #include <sys/wait.h>
9 #include <curl/curl.h>
11 #include <magic.h>
13 #include "entities.h"
14 #include "quotes.h"
16 static const char *nick_self;
17 static unsigned ponify;
18 #define elements(x) (sizeof(x)/sizeof(*x))
20 static struct tdb_context *feed_db, *chan_db, *mail_db, *seen_db;
22 static struct command_hash command_channel = { "#", NULL };
24 #define sstrncmp(a, b) (strncmp(a, b, sizeof(b)-1))
25 #define sstrncasecmp(a, b) (strncasecmp(a, b, sizeof(b)-1))
27 #define SPAM_CUTOFF 5
29 static int pipe_command(struct bio *b, const char *target, const char *nick, int redirect_stdout, int redirect_stderr, char *argv[])
31 int fd[2];
32 int lines = 0;
33 if (pipe2(fd, O_CLOEXEC) < 0) {
34 privmsg(b, target, "Could not create pipe: %m");
35 return -1;
37 pid_t pid = fork();
38 if (pid < 0) {
39 privmsg(b, target, "Could not fork: %m");
40 close(fd[0]);
41 close(fd[1]);
42 return -1;
43 } else if (!pid) {
44 int fdnull = open("/dev/null", O_WRONLY|O_CLOEXEC);
45 close(fd[0]);
46 if (dup3(redirect_stdout ? fd[1] : fdnull, 1, 0) < 0)
47 exit(errno);
48 if (dup3(redirect_stderr ? fd[1] : fdnull, 2, 0) < 0)
49 exit(errno);
50 exit(execv(argv[0], argv));
51 } else {
52 int loc = -1;
53 char buffer[0x100];
54 int bufptr = 0, ret;
55 close(fd[1]);
56 fcntl(fd[0], F_SETFL, O_NONBLOCK);
57 while ((ret = waitpid(pid, &loc, WNOHANG)) >= 0) {
58 while (read(fd[0], buffer+bufptr, 1) == 1) {
59 if (buffer[bufptr] != '\n' && bufptr < sizeof(buffer)-1) {
60 bufptr++;
61 continue;
62 } else if (bufptr) {
63 buffer[bufptr] = 0;
64 bufptr = 0;
65 lines++;
66 if (lines < SPAM_CUTOFF)
67 privmsg(b, nick, "%s", buffer);
68 else
69 fprintf(stderr, "%s\n", buffer);
73 if (ret) {
74 if (bufptr) {
75 buffer[bufptr] = 0;
76 if (lines < SPAM_CUTOFF)
77 privmsg(b, nick, "%s", buffer);
78 else
79 fprintf(stderr, "%s\n", buffer);
81 if (lines >= SPAM_CUTOFF)
82 privmsg(b, nick, "%i lines suppressed", lines - SPAM_CUTOFF + 1);
83 break;
86 if (ret < 0)
87 privmsg(b, target, "error on waitpid: %m");
88 else
89 ret = loc;
90 close(fd[0]);
91 return ret;
95 #include "local.c"
97 static struct command_hash commands[2048];
99 static void command_abort(struct bio *b, const char *nick, const char *host, const char *target, char *args)
101 abort();
102 return;
105 static void command_crash(struct bio *b, const char *nick, const char *host, const char *target, char *args)
107 *(char*)0 = 0;
110 static void command_coinflip(struct bio *b, const char *nick, const char *host, const char *target, char *args)
112 if (getrand() & 1)
113 action(b, target, "whirrrs and clicks excitedly at %s", nick);
114 else
115 action(b, target, "eyes %s as nothing happens", nick);
118 static void command_shesaid(struct bio *b, const char *nick, const char *host, const char *target, char *args)
120 privmsg(b, target, "%s: \"%s\"", args ? args : nick, women_quotes[getrand() % elements(women_quotes)]);
123 static void squash(char *title)
125 char *start = title;
126 goto start;
127 while (*title) {
128 while (*title != '\n' && *title != '\r' && *title != '\t' && *title != ' ' && *title) {
129 *(start++) = *(title++);
131 if (*title)
132 *(start++) = ' ';
133 start:
134 while (*title == '\n' || *title == '\r' || *title == '\t' || *title == ' ')
135 title++;
137 *start = 0;
140 struct curl_download_context
142 char *data;
143 size_t len;
146 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *member) {
147 struct curl_download_context *ctx = member;
148 size *= nmemb;
149 ctx->data = realloc(ctx->data, ctx->len + size);
150 memcpy(ctx->data + ctx->len, ptr, size);
151 ctx->len += size;
152 return size;
155 static const char *get_text(xmlNode *cur)
157 for (; cur; cur = cur->next) {
158 if (cur->type == XML_TEXT_NODE)
159 return cur->content;
161 return NULL;
164 static const char *get_link(xmlAttr *cur, const char *which)
166 for (; cur; cur = cur->next) {
167 if (!strcasecmp(cur->name, which))
168 return get_text(cur->children);
170 return NULL;
173 static int get_feed_entry(struct bio *b, const char *nick, const char *target, const char *category, xmlNode *entry, const char **title, const char **link)
175 const char *cur_title = NULL, *cur_link = NULL;
176 xmlNode *cur;
177 int cur_match = !category;
178 for (cur = entry->children; cur; cur = cur->next) {
179 const char *name = cur->name, *cur_cat;
180 if (cur->type != XML_ELEMENT_NODE)
181 continue;
182 else if (!strcasecmp(name, "link")) {
183 const char *ishtml = get_link(cur->properties, "type");
184 const char *rel = get_link(cur->properties, "rel");
185 if ((!ishtml || !strcasecmp(ishtml, "text/html")) &&
186 rel && !strcasecmp(rel, "alternate"))
187 cur_link = get_link(cur->properties, "href");
188 } else if (!strcasecmp(name, "title"))
189 cur_title = get_text(cur->children);
190 else if (!cur_match && !strcasecmp(name, "category") &&
191 (cur_cat = get_link(cur->properties, "term")) &&
192 strcasestr(cur_cat, category))
193 cur_match = 1;
196 if (cur_title)
197 *title = cur_title;
198 else
199 *title = "<no title>";
200 *link = cur_link;
201 return !cur_link ? -1 : cur_match;
204 static const char *walk_feed(struct bio *b, const char *nick, const char *target, const char *url, const char *category, xmlNode *root, const char *last_link)
206 const char *main_title = NULL, *main_subtitle = NULL, *main_link = NULL, *title, *link, *prev_link = NULL, *prev_title = NULL;
207 char *t;
208 int match, updates = 0;
209 xmlNode *cur, *entry = NULL;
210 for (cur = root->children; cur; cur = cur->next) {
211 const char *name = cur->name;
212 if (cur->type != XML_ELEMENT_NODE)
213 continue;
214 else if (!strcasecmp(name, "category"))
215 continue;
216 else if (!strcasecmp(name, "entry"))
217 entry = entry ? entry : cur;
218 else if (!strcasecmp(name, "title"))
219 main_title = get_text(cur->children);
220 else if (!strcasecmp(name, "subtitle"))
221 main_subtitle = get_text(cur->children);
222 else if (!strcasecmp(name, "link")) {
223 const char *ishtml = get_link(cur->properties, "type");
224 const char *rel = get_link(cur->properties, "rel");
225 if ((!ishtml || !strcasecmp(ishtml, "text/html")) && rel && !strcasecmp(rel, "alternate"))
226 main_link = get_link(cur->properties, "href");
229 if (!main_link || !main_title) {
230 privmsg(b, target, "%s: Failed to parse main: %s %s", nick, main_link, main_title);
231 return NULL;
233 if (!entry)
234 return NULL;
236 if (!last_link)
237 privmsg(b, target, "adding blog %s \"%s\": %s", main_link, main_title, main_subtitle);
239 match = get_feed_entry(b, nick, target, category, entry, &title, &link);
240 if (match < 0)
241 return NULL;
243 for (; !match && entry; entry = entry->next) {
244 if (!strcasecmp(entry->name, "entry"))
245 match = get_feed_entry(b, nick, target, category, entry, &title, &link);
248 if (match < 0)
249 return NULL;
250 if (!last_link) {
251 if (match > 0) {
252 t = strdup(title);
253 squash(t);
254 privmsg(b, target, "Most recent entry: %s %s", link, t);
255 free(t);
256 } else
257 privmsg(b, target, "Currently having no entries for this feed that matches the filter");
258 return link;
260 if (!strcmp(last_link, link) || !match || !entry)
261 return link;
263 for (entry = entry->next; entry; entry = entry->next) {
264 const char *cur_link, *cur_title;
265 if (strcasecmp(entry->name, "entry"))
266 continue;
267 match = get_feed_entry(b, nick, target, category, entry, &cur_title, &cur_link);
268 if (match < 0 || !strcmp(last_link, cur_link))
269 break;
270 if (match) {
271 prev_link = cur_link;
272 prev_title = cur_title;
273 updates++;
276 if (updates == 1) {
277 t = strdup(prev_title);
278 squash(t);
279 privmsg(b, target, "( %s ): %s", prev_link, t);
280 free(t);
281 } else if (updates > 1)
282 privmsg(b, target, "( %s ): %u updates, linking most recent", main_link, 1+updates);
283 t = strdup(title);
284 squash(t);
285 privmsg(b, target, "( %s ): %s", link, t);
286 free(t);
287 return link;
290 static const char *walk_rss(struct bio *b, const char *nick, const char *target, const char *url, xmlNode *root, const char *last_link)
292 const char *main_title = NULL, *main_link = NULL, *title = NULL, *link = NULL, *ver;
293 char *t;
294 xmlNode *cur, *entry = NULL;
295 ver = get_link(root->properties, "version");
296 if (!ver || strcmp(ver, "2.0")) {
297 if (!ver)
298 privmsg(b, target, "%s: Could not parse rss feed", nick);
299 else
300 privmsg(b, target, "%s: Invalid rss version \"%s\"", nick, ver);
301 return NULL;
303 for (cur = root->children; cur && cur->type != XML_ELEMENT_NODE; cur = cur->next);
304 if (!cur)
305 return NULL;
306 for (cur = cur->children; cur; cur = cur->next) {
307 const char *name = cur->name;
308 if (cur->type != XML_ELEMENT_NODE)
309 continue;
310 if (!strcasecmp(name, "title"))
311 main_title = get_text(cur->children);
312 else if (!strcasecmp(name, "link"))
313 main_link = main_link ? main_link : get_text(cur->children);
314 else if (!strcasecmp(name, "item"))
315 entry = entry ? entry : cur;
317 if (!main_link || !main_title) {
318 privmsg(b, target, "%s: Failed to parse main: %s %s", nick, main_link, main_title);
319 return NULL;
321 if (!entry)
322 return NULL;
324 link = title = NULL;
325 for (cur = entry->children; cur; cur = cur->next) {
326 const char *name = cur->name;
327 if (cur->type != XML_ELEMENT_NODE)
328 continue;
329 if (!strcasecmp(name, "title"))
330 title = get_text(cur->children);
331 else if (!strcasecmp(name, "link"))
332 link = get_text(cur->children);
334 if (!title)
335 title = "<no title>";
336 if (!link) {
337 privmsg(b, target, "%s: Failed to parse entry: %s %s", nick, link, title);
338 return NULL;
340 if (!last_link) {
341 t = strdup(title);
342 privmsg(b, target, "adding blog %s \"%s\"", main_link, main_title);
343 squash(t);
344 privmsg(b, target, "Most recent entry: %s %s", link, t);
345 free(t);
346 } else if (strcmp(last_link, link)) {
347 int updates = 0;
348 const char *prev_title = NULL, *prev_link = NULL, *cur_title = NULL, *cur_link = NULL;
349 for (entry = entry->next; entry; entry = entry->next) {
350 if (strcasecmp(entry->name, "item"))
351 continue;
352 prev_title = cur_title;
353 prev_link = cur_link;
354 cur_title = cur_link = NULL;
355 for (cur = entry->children; cur; cur = cur->next) {
356 const char *name = cur->name;
357 if (cur->type != XML_ELEMENT_NODE)
358 continue;
359 if (!strcasecmp(name, "title"))
360 cur_title = get_text(cur->children);
361 else if (!strcasecmp(name, "link"))
362 cur_link = get_text(cur->children);
364 if (!cur_title)
365 cur_title = "<no title>";
366 if (!cur_link || !strcmp(last_link, cur_link))
367 break;
368 updates++;
370 if (updates == 1) {
371 t = strdup(prev_title);
372 squash(t);
373 privmsg(b, target, "( %s ): %s", prev_link, t);
374 free(t);
375 } else if (updates > 1)
376 privmsg(b, target, "( %s ): %u updates, linking most recent", main_link, 1+updates);
377 t = strdup(title);
378 squash(t);
379 privmsg(b, target, "( %s ): %s", link, t);
380 free(t);
382 return link;
385 // HTML is a mess, so I'm just walking the tree depth first until I find the next element..
386 static xmlNode *next_link(xmlNode *cur_node)
388 if (cur_node->children)
389 return cur_node->children;
390 while (cur_node) {
391 if (cur_node->next)
392 return cur_node->next;
393 cur_node = cur_node->parent;
395 return NULL;
398 static const char *get_atom_link(xmlNode *cur)
400 for (; cur; cur = next_link(cur)) {
401 if (cur->type != XML_ELEMENT_NODE)
402 continue;
403 if (!strcasecmp(cur->name, "link")) {
404 const char *isxml = get_link(cur->properties, "type");
405 if (isxml && !strcasecmp(isxml, "application/atom+xml"))
406 return get_link(cur->properties, "href");
409 return NULL;
412 static const char *get_rss_link(xmlNode *cur)
414 for (; cur; cur = next_link(cur)) {
415 if (cur->type != XML_ELEMENT_NODE)
416 continue;
417 if (!strcasecmp(cur->name, "link")) {
418 const char *isxml = get_link(cur->properties, "type");
419 if (isxml && !strcasecmp(isxml, "application/rss+xml"))
420 return get_link(cur->properties, "href");
423 return NULL;
426 static void do_html(struct bio *b, const char *nick, const char *target, const char *url, const char *data, unsigned len)
428 htmlDocPtr ctx = htmlReadMemory(data, len, 0, url, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING);
429 xmlNode *root = xmlDocGetRootElement(ctx);
430 const char *link = get_atom_link(root);
431 if (link)
432 privmsg(b, target, "%s: not a valid feed link, try atom: %s", nick, link);
433 else if ((link = get_rss_link(root)))
434 privmsg(b, target, "%s: not a valid feed link, try rss: %s", nick, link);
435 else
436 privmsg(b, target, "%s: not a valid feed link, no suggestion found", nick);
437 xmlFreeDoc(ctx);
440 static size_t get_time_from_header(void *data, size_t size, size_t size2, void *ptr)
442 char *d, *e;
443 size *= size2;
444 if (sstrncmp(data, "Last-Modified: "))
445 return size;
446 data += sizeof("Last-Modified: ")-1;
447 *(char**)ptr = d = strdup(data);
448 if ((e = strchr(d, '\r')))
449 *e = 0;
450 return size;
453 static int check_single_feed(struct bio *b, const char *target, TDB_DATA key, const char *last_modified, const char *url, const char *link, const char *nick)
455 struct curl_download_context curl_ctx = {};
456 struct curl_slist *headers = NULL;
457 char error[CURL_ERROR_SIZE], *category = strchr(url, '#');
458 int retval = -1;
459 headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
460 headers = curl_slist_append(headers, "Accept: */*");
461 if (category)
462 *(category++) = 0;
464 CURL *h = curl_easy_init();
465 curl_easy_setopt(h, CURLOPT_HTTPHEADER, headers);
466 curl_easy_setopt(h, CURLOPT_URL, url);
467 curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, write_data);
468 curl_easy_setopt(h, CURLOPT_WRITEDATA, &curl_ctx);
469 curl_easy_setopt(h, CURLOPT_ERRORBUFFER, error);
470 curl_easy_setopt(h, CURLOPT_FOLLOWLOCATION, 1);
471 curl_easy_setopt(h, CURLOPT_MAXREDIRS, 3);
472 curl_easy_setopt(h, CURLOPT_SSL_VERIFYPEER, 0);
473 curl_easy_setopt(h, CURLOPT_TIMEOUT, 8);
474 curl_easy_setopt(h, CURLOPT_CONNECTTIMEOUT, 8);
475 curl_easy_setopt(h, CURLOPT_FILETIME, 1);
476 curl_easy_setopt(h, CURLOPT_HEADERFUNCTION, get_time_from_header);
477 curl_easy_setopt(h, CURLOPT_WRITEHEADER, &last_modified);
478 //curl_easy_setopt(h, CURLOPT_VERBOSE, 1);
480 if (last_modified) {
481 char *tmp;
482 asprintf(&tmp, "If-Modified-Since: %s", last_modified);
483 headers = curl_slist_append(headers, tmp);
484 free(tmp);
487 int success = curl_easy_perform(h);
488 curl_slist_free_all(headers);
489 if (success == CURLE_OK) {
490 char *mime = NULL;
491 long code;
492 curl_easy_getinfo(h, CURLINFO_CONTENT_TYPE, &mime);
493 curl_easy_getinfo(h, CURLINFO_RESPONSE_CODE, &code);
494 if (code == 304)
496 else if (!mime || !sstrncmp(mime, "application/xml") || !sstrncmp(mime, "text/xml")) {
497 const char *ret_link = NULL;
498 xmlDocPtr ctx = xmlReadMemory(curl_ctx.data, curl_ctx.len, 0, url, XML_PARSE_NOWARNING | XML_PARSE_NOERROR);
499 xmlNode *root = xmlDocGetRootElement(ctx);
501 if (!root || !root->name)
502 fprintf(stderr, "Failed to parse feed %s %p", url, root);
503 else if (!strcasecmp(root->name, "feed"))
504 ret_link = walk_feed(b, nick, target, url, category, root, link);
505 else if (!strcasecmp(root->name, "rss"))
506 ret_link = walk_rss(b, nick, target, url, root, link);
507 else {
508 privmsg(b, target, "Unknown feed type \"%s\"", root->name);
509 goto free_ctx;
511 if (category)
512 category[-1] = '#';
514 if (ret_link && (!link || strcmp(ret_link, link))) {
515 TDB_DATA val;
516 asprintf((char**)&val.dptr, "%s\001%s", last_modified ? last_modified : "", ret_link);
517 val.dsize = strlen(val.dptr)+1;
518 if (tdb_store(feed_db, key, val, 0) < 0)
519 privmsg(b, target, "updating returns %s", tdb_errorstr(feed_db));
520 free(val.dptr);
521 retval = 1;
523 else
524 retval = 0;
526 free_ctx:
527 xmlFreeDoc(ctx);
528 } else if (link)
530 else if (!sstrncmp(mime, "text/html") || !sstrncmp(mime, "application/xhtml+xml"))
531 do_html(b, nick, target, url, curl_ctx.data, curl_ctx.len);
532 else
533 privmsg(b, target, "unhandled content type %s", mime);
534 } else if (!link)
535 privmsg(b, target, "Error %s (%u)", error, success);
537 free(curl_ctx.data);
538 curl_easy_cleanup(h);
539 return retval;
542 static void command_follow(struct bio *b, const char *nick, const char *host, const char *target, char *args)
544 char *space, *last_link = NULL;
545 TDB_DATA key, val;
546 int ret;
547 if (!feed_db)
548 return;
549 if (target[0] != '#') {
550 privmsg(b, target, "%s: Can only follow on channels", nick);
551 return;
553 if (!args || !*args) {
554 privmsg(b, target, "%s: Usage: !follow <url>", nick);
555 return;
558 if (((space = strchr(args, ' ')) && space < strchr(args, '#')) ||
559 (sstrncmp(args, "http://") && sstrncmp(args, "https://"))) {
560 privmsg(b, target, "%s: Invalid url", nick);
561 return;
564 key.dsize = asprintf((char**)&key.dptr, "%s,%s", target, args)+1;
565 val = tdb_fetch(feed_db, key);
566 if (val.dptr)
567 last_link = strchr(val.dptr, '\001');
568 ret = check_single_feed(b, target, key, NULL, args, last_link ? last_link+1 : NULL, nick);
569 free(val.dptr);
570 if (!ret)
571 privmsg(b, target, "%s: Not updated", nick);
572 free(key.dptr);
575 static void channel_feed_check(struct bio *b, const char *target, int64_t now)
577 int len = strlen(target);
578 int64_t save[] = { now, 0, 0 };
580 TDB_DATA chan, res;
581 if (!feed_db || !chan_db)
582 return;
583 chan.dptr = (char*)target;
584 chan.dsize = len+1;
585 res = tdb_fetch(chan_db, chan);
586 if (res.dptr && res.dsize >= 8) {
587 uint64_t then = *(uint64_t*)res.dptr;
588 if (now - then <= 8000)
589 return;
590 if (res.dsize >= 16)
591 save[1] = ((uint64_t*)res.dptr)[1];
592 if (res.dsize >= 24)
593 save[2] = ((uint64_t*)res.dptr)[2];
595 free(res.dptr);
596 res.dptr = (unsigned char*)save;
597 res.dsize = sizeof(save);
598 if (tdb_store(chan_db, chan, res, 0) < 0) {
599 static int complain_db;
600 if (!complain_db++)
601 privmsg(b, target, "updating database: %s", tdb_errorstr(feed_db));
602 return;
605 for (TDB_DATA d = tdb_firstkey(feed_db); d.dptr;) {
606 TDB_DATA f = tdb_fetch(feed_db, d);
607 TDB_DATA next = tdb_nextkey(feed_db, d);
609 if (!strncmp(d.dptr, target, len) && d.dptr[len] == ',') {
610 const char *url = (char*)d.dptr + len + 1;
611 char *sep;
612 if ((sep = strchr(f.dptr, '\001'))) {
613 *(sep++) = 0;
614 check_single_feed(b, target, d, f.dptr, url, sep, target);
617 free(d.dptr);
618 free(f.dptr);
619 d = next;
623 static void command_unfollow(struct bio *b, const char *nick, const char *host, const char *target, char *args)
625 TDB_DATA key;
626 char *url;
628 if (!feed_db)
629 return;
631 if (!(url = token(&args, ' ')) || (sstrncmp(url, "http://") && sstrncmp(url, "https://"))) {
632 privmsg(b, target, "%s: Invalid url", nick);
633 return;
635 if (target[0] != '#') {
636 privmsg(b, target, "%s: Can only unfollow on channels", nick);
637 return;
639 key.dsize = asprintf((char**)&key.dptr, "%s,%s", target, url)+1;
640 if (tdb_delete(feed_db, key) < 0) {
641 if (tdb_error(feed_db) == TDB_ERR_NOEXIST)
642 privmsg(b, target, "%s: Not following %s on this channel", nick, url);
643 else
644 privmsg(b, target, "%s: Could not delete: %s", nick, tdb_errorstr(feed_db));
645 } else
646 privmsg(b, target, "%s: No longer following %s", nick, url);
647 free(key.dptr);
650 static void command_g(struct bio *b, const char *nick, const char *host, const char *target, char *args)
652 int ret = 0;
653 int64_t g = 0, g_total = 0;
654 TDB_DATA chan, res;
656 if (!chan_db || target[0] != '#')
657 return;
659 chan.dptr = (char*)target;
660 chan.dsize = strlen(chan.dptr)+1;
661 res = tdb_fetch(chan_db, chan);
662 if (res.dptr && res.dsize >= 16) {
663 g = ((int64_t*)res.dptr)[1];
664 ((int64_t*)res.dptr)[1] = 0;
665 if (res.dsize >= 24)
666 g_total = ((int64_t*)res.dptr)[2];
667 ret = tdb_store(chan_db, chan, res, 0);
669 free(res.dptr);
670 if (ret < 0)
671 fprintf(stderr, "updating database: %s", tdb_errorstr(feed_db));
672 else
673 privmsg(b, target, "%s: %"PRIi64" g's since last check, %"PRIi64" total", nick, g, g_total);
676 static void command_feeds(struct bio *b, const char *nick, const char *host, const char *target, char *args)
678 int len = strlen(target), found = 0;
679 if (!feed_db)
680 return;
681 if (target[0] != '#') {
682 privmsg(b, target, "%s: Only useful in channels..", nick);
683 return;
685 for (TDB_DATA d = tdb_firstkey(feed_db); d.dptr;) {
686 TDB_DATA f = tdb_fetch(feed_db, d);
687 TDB_DATA next = tdb_nextkey(feed_db, d);
689 if (!strncmp(d.dptr, target, len) && d.dptr[len] == ',') {
690 privmsg(b, target, "%s: following %s", nick, d.dptr + len + 1);
691 found++;
693 free(d.dptr);
694 free(f.dptr);
695 d = next;
697 if (!found)
698 privmsg(b, target, "%s: not following any feed on %s", nick, target);
701 static void command_feed_get(struct bio *b, const char *nick, const char *host, const char *target, char *args)
703 if (!feed_db)
704 return;
705 for (TDB_DATA d = tdb_firstkey(feed_db); d.dptr;) {
706 TDB_DATA next = tdb_nextkey(feed_db, d);
707 if (!args || strcasestr(d.dptr, args)) {
708 TDB_DATA f = tdb_fetch(feed_db, d);
709 privmsg(b, target, "%s: %s = %s", nick, d.dptr, f.dptr);
710 free(f.dptr);
712 if (strlen(d.dptr)+1 < d.dsize) {
713 privmsg(b, target, "%s: removed buggy entry", nick);
714 tdb_delete(feed_db, d);
716 free(d.dptr);
717 d = next;
721 static void command_feed_set(struct bio *b, const char *nick, const char *host, const char *target, char *args)
723 if (!feed_db)
724 return;
725 TDB_DATA key, val;
726 key.dptr = token(&args, ' ');
727 char *value = token(&args, ' ');
728 if (!key.dptr || !value)
729 return;
730 key.dsize = strlen(key.dptr) + 1;
731 val.dsize = strlen(value) + 2;
732 val.dptr = malloc(val.dsize);
733 strcpy(val.dptr+1, value);
734 val.dptr[0] = '\001';
735 if (tdb_store(feed_db, key, val, 0) < 0)
736 privmsg(b, target, "%s: setting failed: %s", nick, tdb_errorstr(feed_db));
737 else
738 privmsg(b, target, "%s: burp", nick);
739 free(val.dptr);
742 static void command_feed_rem(struct bio *b, const char *nick, const char *host, const char *target, char *args)
744 if (!feed_db || !args)
745 return;
746 TDB_DATA key = { .dptr = (unsigned char*)args, .dsize = strlen(args)+1 };
747 if (tdb_delete(feed_db, key) < 0)
748 privmsg(b, target, "%s: removing failed: %s", nick, tdb_errorstr(feed_db));
749 else
750 privmsg(b, target, "%s: burp", nick);
753 static void command_feed_xxx(struct bio *b, const char *nick, const char *host, const char *target, char *args)
755 if (!feed_db)
756 return;
758 tdb_wipe_all(feed_db);
759 privmsg(b, target, "%s: all evidence erased", nick);
762 static void command_seen_xxx(struct bio *b, const char *nick, const char *host, const char *target, char *args)
764 if (!seen_db)
765 return;
767 tdb_wipe_all(seen_db);
768 privmsg(b, target, "%s: all evidence erased", nick);
771 static void command_feed_counter(struct bio *b, const char *nick, const char *host, const char *target, char *args)
773 if (!chan_db)
774 return;
775 tdb_wipe_all(chan_db);
776 privmsg(b, target, "%s: All update counters reset", nick);
779 static char *get_text_appended(xmlNode *cur)
781 for (; cur; cur = cur->next) {
782 if (cur->type != XML_TEXT_NODE)
783 continue;
784 return strdup(cur->content);
786 return NULL;
789 static char *get_title(struct bio *b, xmlNode *cur_node)
791 for (; cur_node; cur_node = next_link(cur_node)) {
792 if (cur_node->type == XML_ELEMENT_NODE && !strcasecmp(cur_node->name, "title")) {
793 if (!cur_node->children)
794 return NULL;
795 return get_text_appended(cur_node->children);
798 return NULL;
801 static void internal_link(struct bio *b, const char *nick, const char *host, const char *target, char *args, unsigned verbose)
803 CURL *h;
804 struct curl_slist *headers = NULL;
805 char error[CURL_ERROR_SIZE];
806 int success, sent = verbose;
807 struct curl_download_context curl_ctx = {};
809 if (!args)
810 return;
811 int64_t stop, start = get_time(b, target);
813 h = curl_easy_init();
814 headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
815 headers = curl_slist_append(headers, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
816 headers = curl_slist_append(headers, "Accept-Language: en-us,en;q=0.7");
817 headers = curl_slist_append(headers, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
818 headers = curl_slist_append(headers, "DNT: 1");
819 headers = curl_slist_append(headers, "Connection: keep-alive");
820 curl_easy_setopt(h, CURLOPT_HTTPHEADER, headers);
821 curl_easy_setopt(h, CURLOPT_URL, args);
822 curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, write_data);
823 curl_easy_setopt(h, CURLOPT_WRITEDATA, &curl_ctx);
824 curl_easy_setopt(h, CURLOPT_ERRORBUFFER, error);
825 curl_easy_setopt(h, CURLOPT_FOLLOWLOCATION, 1);
826 curl_easy_setopt(h, CURLOPT_MAXREDIRS, 3);
827 curl_easy_setopt(h, CURLOPT_SSL_VERIFYPEER, 0);
828 curl_easy_setopt(h, CURLOPT_TIMEOUT, 8);
829 curl_easy_setopt(h, CURLOPT_CONNECTTIMEOUT, 8);
830 //curl_easy_setopt(h, CURLOPT_VERBOSE, 1);
831 success = curl_easy_perform(h);
832 curl_easy_cleanup(h);
833 curl_slist_free_all(headers);
834 if (success == CURLE_OK) {
835 magic_t m = magic_open(MAGIC_MIME_TYPE);
836 magic_load(m, NULL);
837 const char *mime = magic_buffer(m, curl_ctx.data, curl_ctx.len);
838 if (strstr(mime, "text/html") || strstr(mime, "application/xml") || strstr(mime, "application/xhtml+xml")) {
839 htmlDocPtr ctx = htmlReadMemory(curl_ctx.data, curl_ctx.len, 0, args, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING);
840 xmlNode *root_element = xmlDocGetRootElement(ctx);
841 char *title = get_title(b, root_element);
842 if (title) {
843 char *nuke;
844 squash(title);
845 decode_html_entities_utf8(title, NULL);
846 if ((nuke = strstr(title, " on SoundCloud - Create")))
847 *nuke = 0;
848 if (*title) {
849 privmsg(b, target, "%s linked %s", nick, title);
850 sent = 1;
852 free(title);
854 if (verbose && !title)
855 privmsg(b, target, "%s linked %s page with invalid title", nick, mime);
856 xmlFreeDoc(ctx);
857 } else if (verbose) {
858 magic_setflags(m, MAGIC_COMPRESS);
859 const char *desc = magic_buffer(m, curl_ctx.data, curl_ctx.len);
860 privmsg(b, target, "%s linked type %s", nick, desc);
862 magic_close(m);
864 if (verbose && success != CURLE_OK)
865 privmsg(b, target, "Error %s (%u)", error, success);
866 else if (!sent && (stop = get_time(b, target)) - start >= 15) {
867 privmsg(b, target, "Link (%s) by %s timed out, disabling links for 10 seconds", args, nick);
868 commands[strhash("get") % elements(commands)].disabled_until = stop + 10;
870 free(curl_ctx.data);
873 static void command_get(struct bio *b, const char *nick, const char *host, const char *target, char *args)
875 if (!args || (sstrncmp(args, "http://") && sstrncmp(args, "https://")))
876 return;
877 internal_link(b, nick, host, target, token(&args, ' '), 1);
880 static void command_fabric(struct bio *b, const char *nick, const char *host, const char *target, char *args)
882 privmsg(b, target, "Dumb fabric!");
885 static void command_fun(struct bio *b, const char *nick, const char *host, const char *target, char *args)
887 privmsg(b, target, "Fun?");
890 static void command_admire(struct bio *b, const char *nick, const char *host, const char *target, char *args)
892 char *arg = token(&args, ' ');
893 privmsg(b, target, "%s: I really like your mane!", arg ? arg : nick);
896 static void command_hugs(struct bio *b, const char *nick, const char *host, const char *target, char *args)
898 action(b, target, "gives a lunar hug to %s", args ? args : nick);
901 static void command_hug(struct bio *b, const char *nick, const char *host, const char *target, char *args)
903 if ((host && !strcmp(host, "life.on.the.moon.is.great.I.know.all.about.it")) ||
904 (args && !sstrncasecmp(args, "lu")))
905 command_hugs(b, nick, host, target, args);
906 else
907 action(b, target, "gives a robotic hug to %s", args ? args : nick);
910 static void command_snuggles(struct bio *b, const char *nick, const char *host, const char *target, char *args)
912 action(b, target, "gives a lunar snuggle to %s", args ? args : nick);
915 static void command_snuggle(struct bio *b, const char *nick, const char *host, const char *target, char *args)
917 if ((host && !strcmp(host, "life.on.the.moon.is.great.I.know.all.about.it")) ||
918 (args && !sstrncasecmp(args, "lu")))
919 command_snuggles(b, nick, host, target, args);
920 else
921 action(b, target, "gives a robotic snuggle to %s", args ? args : nick);
924 static void command_cuddles(struct bio *b, const char *nick, const char *host, const char *target, char *args)
926 action(b, target, "gives cuddles to %s in a lunaresque way", args ? args : nick);
929 static void command_cuddle(struct bio *b, const char *nick, const char *host, const char *target, char *args)
931 if ((host && !strcmp(host, "life.on.the.moon.is.great.I.know.all.about.it")) ||
932 (args && !sstrncasecmp(args, "lu")))
933 command_cuddles(b, nick, host, target, args);
934 else
935 action(b, target, "gives cuddles to %s in a robotic way", args ? args : nick);
939 static void command_cookie(struct bio *b, const char *nick, const char *host, const char *target, char *args)
941 if (args && !strncasecmp(args, nick_self, strlen(nick_self)))
942 action(b, target, "eats the cookie offered by %s", nick);
943 else
944 action(b, target, "hands a metallic looking cookie to %s", args ? args : nick);
947 static void command_derpy(struct bio *b, const char *nick, const char *host, const char *target, char *args)
949 static const char *insults[] = {
950 "accidentally shocks herself",
951 "tumbles down the stairs like a slinky",
952 "whirrrs and clicks in a screeching way",
953 "had problems executing this command",
954 "breaks down entirely",
955 "uses her magic to levitate herself off the ground, then hits it face first"
957 action(b, target, "%s", insults[getrand() % elements(insults)]);
960 static void command_inspect(struct bio *b, const char *nick, const char *host, const char *target, char *args)
962 struct command_hash *c;
963 unsigned leave, crash;
964 char *cmd;
965 if (!args || !(cmd = token(&args, ' ')))
966 return;
968 if (strcmp(cmd, "#")) {
969 c = &commands[strhash(cmd) % elements(commands)];
970 if (!c->string || strcasecmp(c->string, cmd)) {
971 privmsg(b, target, "Command %s not valid", cmd);
972 return;
974 } else
975 c = &command_channel;
977 leave = c->left + (c->cmd == command_inspect);
978 crash = c->enter - leave;
979 if (c->enter != leave)
980 privmsg(b, target, "%s: %u successes and %u crash%s, last crashing command: %s", c->string, leave, crash, crash == 1 ? "" : "es", c->failed_command);
981 else
982 privmsg(b, target, "%s: %u time%s executed succesfully", c->string, leave, leave == 1 ? "" : "s");
985 static void command_rebuild(struct bio *b, const char *nick, const char *host, const char *target, char *args)
987 int ret;
988 char *make[] = { "/usr/bin/make", "-j4", NULL };
989 char *git_reset[] = { "/usr/bin/git", "reset", "--hard", "master", NULL };
990 ret = pipe_command(b, target, nick, 0, 1, git_reset);
991 if (ret) {
992 action(b, target, "could not rebuild");
993 return;
995 ret = pipe_command(b, target, nick, 0, 1, make);
996 if (!ret)
997 kill(getpid(), SIGUSR1);
998 else if (ret > 0)
999 action(b, target, "displays an ominous %i", ret);
1002 static void command_swear(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1004 static const char *insults[] = {
1005 "featherbrain",
1006 "ponyfeathers",
1007 "What in the hey?",
1008 "What are you, a dictionary?",
1009 "TAR-DY!",
1010 "[BUY SOME APPLES]",
1011 "{ Your lack of bloodlust on the battlefield is proof positive that you are a soulless automaton! }",
1012 "Your royal snootiness"
1014 privmsg(b, target, "%s: %s", args ? args : nick, insults[getrand() % elements(insults)]);
1017 static const char *perty(int64_t *t)
1019 if (*t >= 14 * 24 * 3600) {
1020 *t /= 7 * 24 * 3600;
1021 return "weeks";
1022 } else if (*t >= 48 * 3600) {
1023 *t /= 24 * 3600;
1024 return "days";
1025 } else if (*t >= 7200) {
1026 *t /= 3600;
1027 return "hours";
1028 } else if (*t >= 120) {
1029 *t /= 60;
1030 return "minutes";
1032 return *t == 1 ? "second" : "seconds";
1035 static void command_timeout(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1037 struct command_hash *c;
1038 int64_t t = get_time(b, target);
1039 int64_t howlong;
1040 if (t < 0)
1041 return;
1042 char *arg = token(&args, ' ');
1043 if (!arg || !args || !(howlong = atoi(args))) {
1044 action(b, target, "pretends to time out");;
1045 return;
1047 c = &commands[strhash(arg) % elements(commands)];
1048 if (c->string && !strcasecmp(c->string, arg)) {
1049 c->disabled_until = t + howlong;
1050 const char *str = perty(&howlong);
1051 action(b, target, "disables %s for %"PRIi64" %s", arg, howlong, str);
1052 } else
1053 action(b, target, "clicks sadly at %s for not being able to find that command", nick);
1056 static void command_mfw(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1058 char error[CURL_ERROR_SIZE], *new_url;
1059 CURL *h = curl_easy_init();
1060 struct curl_slist *headers = NULL;
1061 struct curl_download_context curl_ctx = {};
1062 headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
1063 headers = curl_slist_append(headers, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
1064 headers = curl_slist_append(headers, "Accept-Language: en-us,en;q=0.7");
1065 headers = curl_slist_append(headers, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
1066 headers = curl_slist_append(headers, "DNT: 1");
1067 headers = curl_slist_append(headers, "Connection: keep-alive");
1068 curl_easy_setopt(h, CURLOPT_HTTPHEADER, headers);
1069 curl_easy_setopt(h, CURLOPT_URL, "http://mylittlefacewhen.com/random/");
1070 curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, write_data);
1071 curl_easy_setopt(h, CURLOPT_WRITEDATA, &curl_ctx);
1072 curl_easy_setopt(h, CURLOPT_ERRORBUFFER, error);
1073 curl_easy_setopt(h, CURLOPT_TIMEOUT, 8);
1074 curl_easy_setopt(h, CURLOPT_CONNECTTIMEOUT, 8);
1075 //curl_easy_setopt(h, CURLOPT_VERBOSE, 1);
1076 CURLcode ret = curl_easy_perform(h);
1077 if (ret == CURLE_OK && curl_easy_getinfo(h, CURLINFO_REDIRECT_URL, &new_url) == CURLE_OK)
1078 privmsg(b, target, "%s: %s", nick, new_url);
1079 curl_slist_free_all(headers);
1080 curl_easy_cleanup(h);
1081 if (ret != CURLE_OK)
1082 privmsg(b, target, "%s: You have no face", nick);
1085 static TDB_DATA get_mail_key(const char *nick)
1087 TDB_DATA d;
1088 int i;
1089 d.dsize = strlen(nick)+1;
1090 d.dptr = malloc(d.dsize);
1091 for (i = 0; i < d.dsize - 1; ++i)
1092 d.dptr[i] = tolower(nick[i]);
1093 d.dptr[i] = 0;
1094 return d;
1097 static void command_mail(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1099 char *victim, *x = NULL;
1100 size_t len;
1101 int override = 0;
1102 int64_t last_seen = 0;
1104 if (!mail_db || !seen_db)
1105 return;
1106 TDB_DATA key, val;
1107 victim = token(&args, ' ');
1108 if (victim && !strcasecmp(victim, "-seen")) {
1109 victim = token(&args, ' ');
1110 override = 1;
1112 if (!victim || !args || victim[0] == '#' || strchr(victim, '@') || strchr(victim, '.')) {
1113 privmsg(b, target, "%s: Usage: !mail <nick> <message>", nick);
1114 return;
1116 if (!strcasecmp(victim, nick_self)) {
1117 action(b, target, "whirrs and clicks excitedly at the mail she received from %s", nick);
1118 return;
1120 if (!strcasecmp(victim, nick)) {
1121 action(b, target, "echos the words from %s back to them: %s", nick, args);
1122 return;
1124 int64_t now = get_time(b, target);
1125 if (now < 0)
1126 return;
1128 key = get_mail_key(victim);
1129 val = tdb_fetch(seen_db, key);
1130 if (val.dptr && (x = strchr(val.dptr, ','))) {
1131 *(x++) = 0;
1132 last_seen = atoll(val.dptr);
1134 if (x && !admin(host) && (now - last_seen) < 300 && (!sstrncasecmp(x, "in ") || !sstrncasecmp(x, "joining "))) {
1135 action(b, target, "would rather not store mail for someone active so recently");
1136 goto out;
1137 } else if (last_seen && now - last_seen > 14 * 24 * 3600 && !override) {
1138 int64_t delta = now - last_seen;
1139 const char *str = perty(&delta);
1140 privmsg(b, target, "%s: \"%s\" was last seen %"PRIi64" %s ago, use !mail -seen %s <message> to override this check.", nick, victim, delta, str, victim);
1141 return;
1142 } else if (!x && !override) {
1143 privmsg(b, target, "%s: I've never seen \"%s\", use !mail -seen %s <message> to override this check.", nick, victim, victim);
1144 return;
1147 val = tdb_fetch(mail_db, key);
1148 if (!val.dptr)
1149 val.dsize = 0;
1150 else {
1151 unsigned char *cur;
1152 int letters = 0;
1153 for (cur = val.dptr; cur < val.dptr + val.dsize; cur += strlen(cur)+1)
1154 letters++;
1155 if (letters >= 6) {
1156 action(b, target, "looks sadly at %s as she cannot hold any more mail to %s", nick, victim);
1157 goto out;
1160 len = snprintf(NULL, 0, "%"PRIi64 ",%s: %s", now, nick, args) + 1;
1161 val.dptr = realloc(val.dptr, val.dsize + len);
1162 snprintf(val.dptr + val.dsize, len, "%"PRIi64 ",%s: %s", now, nick, args);
1163 val.dsize += len;
1164 if (tdb_store(mail_db, key, val, 0) < 0)
1165 privmsg(b, target, "%s: updating mail returns %s", nick, tdb_errorstr(mail_db));
1166 else
1167 action(b, target, "whirrs and clicks at %s as she stores the mail for %s", nick, victim);
1168 out:
1169 free(val.dptr);
1170 free(key.dptr);
1173 static void single_message(struct bio *b, const char *target, char *cur, int64_t now)
1175 char *endtime, *sep = strchr(cur, ':');
1176 int64_t delta = -1;
1177 if (sep && (endtime = strchr(cur, ',')) && endtime < sep) {
1178 int64_t t = atoll(cur);
1179 if (t > 0)
1180 delta = now - t;
1181 cur = endtime + 1;
1183 if (delta >= 0) {
1184 const char *str = perty(&delta);
1185 privmsg(b, target, "%"PRIi64" %s ago from %s", delta, str, cur);
1186 } else
1187 privmsg(b, target, "From %s", cur);
1190 static void command_deliver(struct bio *b, const char *nick, const char *target)
1192 TDB_DATA key, val;
1193 unsigned char *cur;
1194 static unsigned mail_enter, mail_leave;
1195 if (mail_enter++ != mail_leave)
1196 return;
1198 if (!mail_db)
1199 return;
1200 key = get_mail_key(nick);
1201 val = tdb_fetch(mail_db, key);
1202 if (!val.dptr)
1203 goto end;
1204 int64_t now = get_time(b, NULL);
1205 if (strcasecmp(key.dptr, nick_self)) {
1206 privmsg(b, target, "%s: You've got mail!", nick);
1207 for (cur = val.dptr; cur < val.dptr + val.dsize; cur += strlen(cur)+1)
1208 single_message(b, target, cur, now);
1210 free(val.dptr);
1211 tdb_delete(mail_db, key);
1212 end:
1213 free(key.dptr);
1214 mail_leave++;
1217 static void update_seen(struct bio *b, char *doingwhat, const char *nick)
1219 TDB_DATA key;
1220 key = get_mail_key(nick);
1221 TDB_DATA val = { .dptr = doingwhat, .dsize = strlen(doingwhat)+1 };
1222 if (seen_db)
1223 tdb_store(seen_db, key, val, 0);
1224 free(key.dptr);
1227 static void command_seen(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1229 char *arg = token(&args, ' '), *x;
1230 int64_t now = get_time(b, target);
1231 TDB_DATA key, val;
1232 if (now < 0)
1233 return;
1234 if (!seen_db || !arg) {
1235 privmsg(b, target, "%s: { Error... }", nick);
1236 return;
1238 if (!strcasecmp(arg, nick_self)) {
1239 action(b, target, "whirrs and clicks at %s", nick);
1240 return;
1242 if (!strcasecmp(arg, nick)) {
1243 action(b, target, "circles around %s dancing", nick);
1244 return;
1246 key = get_mail_key(arg);
1247 val = tdb_fetch(seen_db, key);
1248 if (val.dptr && (x = strchr(val.dptr, ','))) {
1249 int64_t delta;
1250 const char *str;
1251 *(x++) = 0;
1252 delta = now - atoll(val.dptr);
1253 str = perty(&delta);
1254 if (delta < 0)
1255 privmsg(b, target, "%s was last seen in the future %s", arg, x);
1256 else
1257 privmsg(b, target, "%s was last seen %"PRIi64" %s ago %s", arg, delta, str, x);
1258 } else
1259 action(b, target, "cannot find any evidence that %s exists", arg);
1260 free(val.dptr);
1263 static void command_mailbag(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1265 char buffer[256];
1266 unsigned rem = sizeof(buffer)-1, first = 2;
1267 if (!mail_db)
1268 return;
1269 buffer[rem] = 0;
1271 for (TDB_DATA f = tdb_firstkey(mail_db); f.dptr;) {
1272 if (f.dsize + 4 > rem) {
1273 privmsg(b, target, "%s: Holding mail for: %s", nick, &buffer[rem]);
1274 first = 2;
1275 rem = sizeof(buffer)-1;
1276 assert(f.dsize + 4 < rem);
1278 if (f.dptr) {
1279 if (first == 2) {
1280 first = 1;
1281 } else if (first) {
1282 rem -= 5;
1283 memcpy(&buffer[rem], " and ", 5);
1284 first = 0;
1285 } else {
1286 rem -= 2;
1287 memcpy(&buffer[rem], ", ", 2);
1289 rem -= f.dsize - 1;
1290 memcpy(&buffer[rem], f.dptr, f.dsize - 1);
1292 TDB_DATA next = tdb_nextkey(mail_db, f);
1293 free(f.dptr);
1294 f = next;
1296 if (first < 2)
1297 privmsg(b, target, "%s: Holding mail for: %s", nick, &buffer[rem]);
1300 static void command_mailread(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1302 TDB_DATA key, val;
1303 char *victim;
1304 if (!mail_db || !(victim = token(&args, ' ')))
1305 return;
1306 key = get_mail_key(victim);
1307 val = tdb_fetch(mail_db, key);
1308 if (!val.dptr)
1309 action(b, target, "ponyshrugs as no mail for %s was found", victim);
1310 else {
1311 unsigned char *cur;
1312 int64_t now = get_time(b, NULL);
1313 action(b, target, "peeks through %s's mail", victim);
1314 for (cur = val.dptr; cur < val.dptr + val.dsize; cur += strlen(cur)+1)
1315 single_message(b, target, cur, now);
1317 free(val.dptr);
1318 free(key.dptr);
1321 static void command_no_deliver(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1323 TDB_DATA key, val;
1324 char *cur;
1325 if (!mail_db || !(cur = token(&args, ' ')))
1326 return;
1327 key = get_mail_key(cur);
1328 val = tdb_fetch(mail_db, key);
1329 if (!val.dptr)
1330 action(b, target, "ponyshrugs as no mail for %s was found", cur);
1331 else {
1332 action(b, target, "deletes all evidence of %s's mail", cur);
1333 tdb_delete(mail_db, key);
1335 free(val.dptr);
1336 free(key.dptr);
1339 static void perform_roll(struct bio *b, const char *nick, const char *target, long sides, long dice, long bonus)
1341 long rem = dice, total = bonus;
1342 while (rem--)
1343 total += 1 + (getrand() % sides);
1344 if (bonus)
1345 action(b, target, "rolls %li %li-sided %s for a total of %li (%+li)", dice, sides, dice == 1 ? "die" : "dice", total, bonus);
1346 else
1347 action(b, target, "rolls %li %li-sided %s for a total of %li", dice, sides, dice == 1 ? "die" : "dice", total);
1350 static void command_roll(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1352 char *cur;
1353 long dice = 1, sides = 20;
1354 if ((cur = token(&args, ' '))) {
1355 char *first = cur;
1356 dice = strtol(first, &cur, 10);
1357 if (first == cur)
1358 dice = 1;
1359 if (*cur && *cur != 'd' && *cur != 'D')
1360 goto syntax;
1362 if (*cur) {
1363 char *last = cur+1;
1364 sides = strtol(last, &cur, 10);
1365 if (last == cur)
1366 goto syntax;
1369 if (dice <= 0 || dice > 25 || sides < 2 || sides > 20)
1370 goto syntax;
1371 perform_roll(b, nick, target, sides, dice, 0);
1372 return;
1374 syntax:
1375 action(b, target, "bleeps at %s in a confused manner", nick);
1378 static void add_g(struct bio *b, const char *target, int g)
1380 int ret = 0;
1381 int64_t total_g = 0;
1382 TDB_DATA chan, res;
1384 if (!chan_db || target[0] != '#')
1385 return;
1387 chan.dptr = (char*)target;
1388 chan.dsize = strlen(chan.dptr)+1;
1389 res = tdb_fetch(chan_db, chan);
1390 if (res.dptr && res.dsize >= 16) {
1391 ((int64_t*)res.dptr)[1] += g;
1392 if (res.dsize >= 24)
1393 total_g = ((int64_t*)res.dptr)[2] += g;
1394 ret = tdb_store(chan_db, chan, res, 0);
1396 free(res.dptr);
1397 if (ret < 0)
1398 fprintf(stderr, "updating database: %s", tdb_errorstr(feed_db));
1399 else if (g < total_g) {
1400 int64_t last_g = total_g - g;
1401 last_g -= last_g % 1000;
1402 if (last_g + 1000 <= total_g)
1403 privmsg(b, target, "g has been said %"PRIi64" times!", total_g);
1407 static int parse_g(const char *cur, int *g)
1409 int this_g = 0;
1410 for (const unsigned char *ptr = cur; *ptr; ptr++) {
1411 if (*ptr >= 0x80)
1412 return 0;
1413 if (*ptr == 'g' || *ptr == 'G')
1414 this_g++;
1415 else if ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z'))
1416 return 0;
1417 else if (*ptr != '1' && *ptr >= '0' && *ptr <= '9')
1418 return 0;
1420 *g += this_g;
1421 return this_g;
1424 static void channel_msg(struct bio *b, const char *nick, const char *host,
1425 const char *chan, char *msg, int64_t t)
1427 char *cur = NULL, *next;
1428 int is_action = 0;
1429 int g = 0;
1431 int that = 0, what = 0, she = 0, nsfw = 0;
1433 if (!msg || !strcmp(nick, "`Daring_Do`") ||
1434 !sstrncmp(nick, "derpy") || !sstrncmp(nick, "`derpy") ||
1435 !sstrncmp(nick, "`Luna") || !sstrncmp(nick, "`Celestia") ||
1436 !sstrncmp(nick, "GitHub") || !sstrncmp(nick, "CIA-") ||
1437 !sstrncmp(nick, "Termi") || !strcmp(nick, "r0m") || !sstrncasecmp(nick, "Owloysius"))
1438 return;
1440 if (!sstrncasecmp(msg, "\001ACTION ")) {
1441 msg += sizeof("\001ACTION ")-1;
1442 is_action = 1;
1443 asprintf(&cur, "%"PRIi64 ",in %s: * %s %s", t, chan, nick, msg);
1444 } else
1445 asprintf(&cur, "%"PRIi64 ",in %s: <%s> %s", t, chan, nick, msg);
1446 if (t > 0 && strcasecmp(chan, "#themoon") && strcasecmp(chan, "#fluttertreehouse"))
1447 update_seen(b, cur, nick);
1448 free(cur);
1449 (void)is_action;
1451 if (!sstrncasecmp(msg, "fun!") || !strcasecmp(msg, "fun")) {
1452 static int64_t last_fun = -1;
1454 if (!strcasecmp(chan, "#bronymusic") && t - last_fun > 5) {
1455 if (t < 0 || t > commands[strhash("fun") % elements(commands)].disabled_until) {
1456 if (sstrncmp(nick, "EqBot"))
1457 privmsg(b, chan, "Fun!");
1458 privmsg(b, chan, "Fun!");
1461 last_fun = t;
1462 return;
1465 next = msg;
1466 while ((cur = token(&next, ' '))) {
1467 if (!strcasecmp(cur, ">mfw") || !strcasecmp(cur, "mfw")) {
1468 if (!strcasecmp(chan, "#brony") || !ponify)
1469 continue;
1470 if (t < 0 || t > commands[strhash("mfw") % elements(commands)].disabled_until)
1471 command_mfw(b, nick, host, chan, NULL);
1472 return;
1473 } else if (!sstrncasecmp(cur, "http://") || !sstrncasecmp(cur, "https://")) {
1474 static char last_url[512];
1475 char *part;
1476 if (!strcmp(cur, last_url))
1477 return;
1478 strncpy(last_url, cur, sizeof(last_url)-1);
1480 if (!sstrncmp(nick, "EqBot") || !sstrncasecmp(chan, "#mlpsurvival"))
1481 return;
1483 if (t >= 0 && t < commands[strhash("get") % elements(commands)].disabled_until)
1484 return;
1486 else if (strcasestr(cur, "youtube.com/user") && (part = strstr(cur, "#p/"))) {
1487 char *foo;
1488 part = strrchr(part, '/') + 1;
1489 asprintf(&foo, "http://youtube.com/watch?v=%s", part);
1490 if (foo)
1491 internal_link(b, nick, host, chan, foo, 0);
1492 free(foo);
1493 return;
1494 } else if (strcasestr(cur, "twitter.com/") || strcasestr(cur, "mlfw.info") || strcasestr(cur, "mylittlefacewhen.com") || !strcasecmp(chan, "#geek"))
1495 return;
1496 if (!strcasecmp(chan, "#bronymusic") &&
1497 (nsfw || (next && strcasestr(next, "nsfw"))))
1498 continue;
1499 internal_link(b, nick, host, chan, cur, 0);
1500 return;
1502 else if (!sstrncasecmp(cur, "that") || !sstrncasecmp(cur, "dat"))
1503 that = 1;
1504 else if (that && (!sstrncasecmp(cur, "what") || !sstrncasecmp(cur, "wat")))
1505 what = 1;
1506 else if (what && !sstrncasecmp(cur, "she"))
1507 she = 1;
1508 else if (she && !sstrncasecmp(cur, "said")) {
1509 if (t <= 0 || t >= commands[strhash("shesaid") % elements(commands)].disabled_until)
1510 privmsg(b, chan, "%s: \"%s\"", nick, women_quotes[getrand() % elements(women_quotes)]);
1511 return;
1512 } else if (parse_g(cur, &g))
1513 continue;
1514 else if (strcasestr(cur, "nsfw"))
1515 nsfw = 1;
1517 if (g)
1518 add_g(b, chan, g);
1521 static void command_commands(struct bio *b, const char *nick, const char *host, const char *target, char *args);
1523 static struct command_hash unhashed[] = {
1524 { "1", command_coinflip },
1525 { "fabric", command_fabric },
1526 { "fun", command_fun },
1527 { "get", command_get },
1528 { "hug", command_hug },
1529 { "hugs", command_hugs, -1 },
1530 { "admire", command_admire },
1531 { "snug", command_snuggle, -1 },
1532 { "snugs", command_snuggles, -1 },
1533 { "snuggle", command_snuggle },
1534 { "snuggles", command_snuggles, -1 },
1535 { "cuddle", command_cuddle },
1536 { "cuddles", command_cuddles, -1 },
1537 { "cookie", command_cookie },
1538 { "mfw", command_mfw },
1539 { "swear", command_swear },
1540 { "mail", command_mail },
1541 { "m", command_mail, -1 },
1542 { "seen", command_seen },
1543 { "derpy", command_derpy },
1544 { "g", command_g, -1 },
1545 { "shesaid", command_shesaid },
1546 { "roll", command_roll },
1548 { "rebuild", command_rebuild, 1 },
1549 { "abort", command_abort, 1 },
1550 { "crash", command_crash, 1 },
1551 { "inspect", command_inspect, 1 },
1552 { "timeout", command_timeout, 1 },
1554 { "follow", command_follow },
1555 { "unfollow", command_unfollow },
1556 { "feeds", command_feeds },
1558 // DEBUG
1559 { "feed_get", command_feed_get, 1 },
1560 { "feed_set", command_feed_set, 1 },
1561 { "feed_rem", command_feed_rem, 1 },
1562 { "feed_xxx", command_feed_xxx, 1 },
1563 { "feed_counter", command_feed_counter, 1 },
1564 { "seen_xxx", command_seen_xxx, 1 },
1565 { "mailbag", command_mailbag },
1566 { "mailread", command_mailread, 1 },
1567 { "\"deliver\"", command_no_deliver, 1 },
1568 { "commands", command_commands, -1 }
1571 static void command_commands(struct bio *b, const char *nick, const char *host, const char *target, char *args)
1573 char buffer[256];
1574 unsigned rem = sizeof(buffer)-1, first = 2, i;
1575 buffer[rem] = 0;
1577 for (i = 0; i < sizeof(unhashed)/sizeof(*unhashed); ++i) {
1578 const char *str;
1579 unsigned long len;
1580 if (unhashed[i].admin)
1581 continue;
1583 str = unhashed[i].string;
1584 len = strlen(str);
1586 if (len + 5 > rem) {
1587 privmsg(b, target, "%s: Valid commands: %s", nick, &buffer[rem]);
1588 first = 2;
1589 rem = sizeof(buffer)-1;
1590 assert(len < rem);
1592 if (str) {
1593 if (first == 2) {
1594 first = 1;
1595 } else if (first) {
1596 rem -= 5;
1597 memcpy(&buffer[rem], " and ", 5);
1598 first = 0;
1599 } else {
1600 rem -= 2;
1601 memcpy(&buffer[rem], ", ", 2);
1603 rem -= len;
1604 memcpy(&buffer[rem], str, len);
1607 if (first < 2)
1608 privmsg(b, target, "%s: Valid commands: %s", nick, &buffer[rem]);
1611 static void init_hash(struct bio *b, const char *target)
1613 int i;
1614 for (i = 0; i < elements(unhashed); ++i) {
1615 unsigned h = strhash(unhashed[i].string) % elements(commands);
1616 if (commands[h].string)
1617 privmsg(b, target, "%s is a duplicate command with %s", commands[h].string, unhashed[i].string);
1618 else
1619 commands[h] = unhashed[i];
1621 #ifdef local_commands
1622 for (i = 0; i < elements(local_commands); ++i) {
1623 unsigned h = strhash(local_commands[i].string) % elements(commands);
1624 if (commands[h].string)
1625 privmsg(b, target, "%s is a duplicate command with %s", commands[h].string, local_commands[i].string);
1626 else
1627 commands[h] = local_commands[i];
1629 #endif
1632 void init_hook(struct bio *b, const char *target, const char *nick, unsigned is_ponified)
1634 char *cwd, *path = NULL;
1635 nick_self = nick;
1636 static const char *messages[] = {
1637 "feels circuits being activated that weren't before",
1638 "suddenly gets a better feel of her surroundings",
1639 "looks the same, yet there's definitely something different",
1640 "emits a beep as her lights begin to pulse slowly",
1641 "whirrrs and bleeps like never before",
1642 "bleeps a few times happily",
1643 "excitedly peeks at her surroundings"
1645 init_hash(b, target);
1646 ponify = is_ponified;
1648 cwd = getcwd(NULL, 0);
1649 asprintf(&path, "%s/db/feed.tdb", cwd);
1650 feed_db = tdb_open(path, 0, 0, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
1651 free(path);
1652 if (!feed_db)
1653 privmsg(b, target, "Opening feed db failed: %m");
1655 asprintf(&path, "%s/db/chan.tdb", cwd);
1656 chan_db = tdb_open(path, 0, 0, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
1657 free(path);
1658 if (!chan_db)
1659 privmsg(b, target, "Opening chan db failed: %m");
1661 asprintf(&path, "%s/db/mail.tdb", cwd);
1662 mail_db = tdb_open(path, 0, 0, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
1663 free(path);
1664 if (!mail_db)
1665 privmsg(b, target, "Opening mail db failed: %m");
1667 asprintf(&path, "%s/db/seen.tdb", cwd);
1668 seen_db = tdb_open(path, 0, 0, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644);
1669 free(path);
1670 if (!seen_db)
1671 privmsg(b, target, "Opening seen db failed: %m");
1673 free(cwd);
1674 action(b, target, "%s", messages[getrand() % elements(messages)]);
1677 static void __attribute__((destructor)) shutdown_hook(void)
1679 if (seen_db)
1680 tdb_close(seen_db);
1681 if (mail_db)
1682 tdb_close(mail_db);
1683 if (feed_db)
1684 tdb_close(feed_db);
1685 if (chan_db)
1686 tdb_close(chan_db);
1689 static char *nom_special(char *line)
1691 while (*line) {
1692 if (*line == 0x03) {
1693 line++;
1694 if (*line >= '0' && *line <= '9')
1695 line++;
1696 else continue;
1697 if (*line >= '0' && *line <= '9')
1698 line++;
1699 if (line[0] == ',' && line[1] >= '0' && line[1] <= '9')
1700 line += 2;
1701 else continue;
1702 if (*line >= '0' && *line <= '9')
1703 line++;
1704 } else if (*line != 0x02 && /* BOLD */
1705 *line != 0x1f && /* UNDERLINE */
1706 *line != 0x16 && /* ITALIC */
1707 *line != 0x06 && /* NFI, is this even used? */
1708 *line != 0x07 && /* NFI, is this even used? */
1709 *line != 0x0f) /* NORMAL */
1710 return line;
1711 else
1712 line++;
1714 return line;
1717 static char *cleanup_special(char *line)
1719 char *cur, *start = nom_special(line);
1720 if (!*start)
1721 return NULL;
1722 for (line = cur = start; *line; line = nom_special(line))
1723 *(cur++) = *(line++);
1725 for (cur--; cur >= start; --cur)
1726 if (*cur != ' ' && *cur != '\001')
1727 break;
1729 if (cur < start)
1730 return NULL;
1731 cur[1] = 0;
1732 return start;
1735 static void rss_check(struct bio *b, const char *channel, int64_t t)
1737 static unsigned rss_enter, rss_leave;
1738 if (t >= 0 && rss_enter == rss_leave) {
1739 rss_enter++;
1740 channel_feed_check(b, channel, t);
1741 rss_leave++;
1745 static const char *privileged_command[] = {
1746 "{ Rarity, I love you so much! }",
1747 "{ Rarity, have I ever told you that I love you? }",
1748 "{ Yes, I love my sister, Rarity. }",
1749 "{ Raaaaaaaaaaaaaarity. }",
1750 "{ You do not fool me, Rari...bot! }"
1753 static int cmd_check(struct bio *b, struct command_hash *c, int is_admin, int64_t t, const char *prefix, const char *target)
1755 if (c->left != c->enter)
1756 privmsg(b, target, "Command %s is disabled because of a crash", c->string);
1757 else if (t > 0 && t < c->disabled_until && !is_admin) {
1758 int64_t delta = c->disabled_until - t;
1759 const char *str = perty(&delta);
1760 b->writeline(b, "NOTICE %s :Command %s is on timeout for the next %"PRIi64 " %s", prefix, c->string, delta, str);
1761 } else if (c->admin > 0 && !is_admin)
1762 privmsg(b, target, "%s: %s", prefix, privileged_command[getrand() % elements(privileged_command)]);
1763 else
1764 return 1;
1765 return 0;
1768 void privmsg_hook(struct bio *b, const char *prefix, const char *ident, const char *host,
1769 const char *const *args, unsigned nargs)
1771 char *cmd_args, *cmd;
1772 const char *target = args[0][0] == '#' ? args[0] : prefix;
1773 unsigned chan, nick_len;
1774 struct command_hash *c;
1775 int64_t t = get_time(b, target);
1776 int is_admin = admin(host);
1778 chan = args[0][0] == '#';
1779 if (chan) {
1780 rss_check(b, args[0], t);
1781 command_deliver(b, prefix, args[0]);
1783 cmd_args = cleanup_special((char*)args[1]);
1784 if (!cmd_args)
1785 return;
1787 if (ident && (!strcasecmp(ident, "Revolver") || !strcasecmp(ident, "Rev")))
1788 return;
1790 if (chan && cmd_args[0] == '!') {
1791 cmd_args++;
1792 } else if (chan && (nick_len = strlen(nick_self)) &&
1793 !strncasecmp(cmd_args, nick_self, nick_len) &&
1794 (cmd_args[nick_len] == ':' || cmd_args[nick_len] == ',') && cmd_args[nick_len+1] == ' ') {
1795 cmd_args += nick_len + 2;
1796 if (!cmd_args[0])
1797 return;
1798 chan = 2;
1799 } else if (chan) {
1800 if (command_channel.enter == command_channel.left) {
1801 command_channel.enter++;
1802 snprintf(command_channel.failed_command,
1803 sizeof(command_channel) - offsetof(struct command_hash, failed_command) - 1,
1804 "%s:%s (%s@%s) \"#\" %s", target, prefix, ident, host, cmd_args);
1805 channel_msg(b, prefix, host, args[0], cmd_args, t);
1806 command_channel.left++;
1808 return;
1810 cmd = token(&cmd_args, ' ');
1811 if (!chan && cmd_args && cmd_args[0] == '#') {
1812 if (!is_admin) {
1813 privmsg(b, target, "%s: %s", prefix, privileged_command[getrand() % elements(privileged_command)]);
1814 return;
1816 target = token(&cmd_args, ' ');
1819 c = &commands[strhash(cmd) % elements(commands)];
1820 if (c->string && !strcasecmp(c->string, cmd)) {
1821 if (!sstrncasecmp(prefix, "EqBot")) {
1822 action(b, target, "adoringly pulses some light back to %s", prefix);
1823 return;
1825 if (cmd_check(b, c, is_admin, t, prefix, target)) {
1826 c->enter++;
1827 snprintf(c->failed_command, sizeof(*c) - offsetof(struct command_hash, failed_command) - 1,
1828 "%s:%s (%s@%s) \"%s\" %s", target, prefix, ident, host, cmd, cmd_args);
1829 c->cmd(b, prefix, host, target, cmd_args);
1830 c->left++;
1832 return;
1833 } else if (cmd[0] == 'd' && cmd[1] >= '0' && cmd[1] <= '9') {
1834 char *end;
1835 long base;
1836 base = strtol(cmd+1, &end, 10);
1837 if (base > 1 && base <= 20 && !*end) {
1838 long dice = 1, bonus = 0;
1839 char *strdice;
1840 c = &commands[strhash("roll") % elements(commands)];
1841 if (!cmd_check(b, c, is_admin, t, prefix, target))
1842 return;
1843 c->enter++;
1844 snprintf(c->failed_command, sizeof(*c) - offsetof(struct command_hash, failed_command) - 1,
1845 "%s:%s (%s@%s) \"%s\" %s", target, prefix, ident, host, cmd, cmd_args);
1847 strdice = token(&cmd_args, ' ');
1848 if (strdice) {
1849 dice = strtol(strdice, &end, 10);
1850 if (*end || !dice || dice > 25)
1851 goto syntax;
1852 strdice = token(&cmd_args, ' ');
1853 if (strdice) {
1854 bonus = strtol(strdice, &end, 10);
1855 if (*end || bonus > 1000 || bonus < -1000)
1856 goto syntax;
1859 perform_roll(b, prefix, target, base, dice, bonus);
1860 c->left++;
1861 return;
1863 syntax:
1864 action(b, target, "bleeps at %s in a confused manner", prefix);
1865 c->left++;
1866 return;
1868 } else if (cmd[0] >= '0' && cmd[0] <= '9') {
1869 long dice, sides, bonus = 0;
1870 char *end;
1871 dice = strtol(cmd, &end, 10);
1872 if (dice >= 1 && dice <= 25 && *end == 'd' &&
1873 (sides = strtol(end+1, &end, 10)) &&
1874 sides > 1 && sides <= 20 && !*end) {
1875 char *strbonus;
1877 c = &commands[strhash("roll") % elements(commands)];
1878 if (!cmd_check(b, c, is_admin, t, prefix, target))
1879 return;
1880 c->enter++;
1881 snprintf(c->failed_command, sizeof(*c) - offsetof(struct command_hash, failed_command) - 1,
1882 "%s:%s (%s@%s) \"%s\" %s", target, prefix, ident, host, cmd, cmd_args);
1884 strbonus = token(&cmd_args, ' ');
1885 if (strbonus) {
1886 bonus = strtol(strbonus, &end, 10);
1887 if (*end || bonus > 1000 || bonus < -1000)
1888 goto syntax;
1891 perform_roll(b, prefix, target, sides, dice, bonus);
1892 c->left++;
1893 return;
1897 if (chan == 2 && t > 0) {
1898 static int64_t last_t;
1899 if (t - last_t > 3)
1900 privmsg(b, target, "%s: { I love you! }", prefix);
1901 last_t = t;
1905 void command_hook(struct bio *b, const char *prefix, const char *ident, const char *host,
1906 const char *command, const char *const *args, unsigned nargs)
1908 char *buf = NULL;
1909 int64_t t = -1;
1910 if (!strcasecmp(command, "NOTICE")) {
1911 if (nargs < 2 || args[0][0] == '#')
1912 return;
1913 if (admin(host))
1914 b->writeline(b, "%s", args[1]);
1915 else
1916 fprintf(stderr, "%s: %s\n", prefix, args[1]);
1917 } else if (!strcasecmp(command, "JOIN")) {
1918 t = get_time(b, args[0]);
1919 rss_check(b, args[0], t);
1920 command_deliver(b, prefix, args[0]);
1921 asprintf(&buf, "%"PRIi64",joining %s", t, args[0]);
1922 } else if (!strcasecmp(command, "PART")) {
1923 t = get_time(b, args[0]);
1924 rss_check(b, args[0], t);
1925 asprintf(&buf, "%"PRIi64",leaving %s", t, args[0]);
1926 } else if (!strcasecmp(command, "QUIT")) {
1927 t = get_time(b, NULL);
1928 asprintf(&buf, "%"PRIi64",quitting with the message \"%s\"", t, args[0]);
1929 } else if (!strcasecmp(command, "NICK")) {
1930 t = get_time(b, NULL);
1931 if (t >= 0) {
1932 asprintf(&buf, "%"PRIi64",changing nick from %s", t, prefix);
1933 if (buf)
1934 update_seen(b, buf, args[0]);
1935 free(buf);
1936 buf = NULL;
1938 asprintf(&buf, "%"PRIi64",changing nick to %s", t, args[0]);
1939 } else if (0) {
1940 int i;
1941 fprintf(stderr, ":%s!%s%s %s", prefix, ident, host, command);
1942 for (i = 0; i < nargs; ++i)
1943 fprintf(stderr, " %s", args[i]);
1944 fprintf(stderr, "\n");
1946 if (t >= 0 && buf)
1947 update_seen(b, buf, prefix);
1948 free(buf);
1949 return;