have fun
[fillybot.git] / mod.h
blobe47dccb4ddf3c0830cabd130262d2de63e8f8e55
1 #include "shared.h"
2 #include <ctype.h>
3 #include <tdb.h>
4 #include <sys/time.h>
6 static inline char *token(char **foo, char sep)
8 char *line = *foo, *ret;
9 if (!line)
10 return NULL;
11 ret = line;
12 while (*line && *line != sep)
13 line++;
14 if (*line) {
15 *(line++) = 0;
16 while (*line == sep)
17 line++;
18 *foo = *line ? line : NULL;
19 } else
20 *foo = NULL;
21 return ret;
24 static inline unsigned getrand(void)
26 unsigned r;
27 if (RAND_pseudo_bytes((unsigned char*)&r, sizeof(r)) >= 0)
28 return r;
29 fprintf(stderr, "random number generator error: %s", ERR_error_string(ERR_peek_last_error(), NULL));
30 while (ERR_get_error());
31 return random();
34 struct command_hash
36 char *string;
37 void (*cmd)(struct bio *b, const char *nick, const char *host, const char *target, char *args);
38 int admin;
39 int enter, left;
40 int64_t disabled_until;
41 char failed_command[];
42 } __attribute__((aligned(256)));
44 static unsigned strhash(const char *h)
46 TDB_DATA in = { .dptr = (char*)h, .dsize = strlen(h) };
47 return tdb_jenkins_hash(&in);
50 static void __attribute__ ((__format__(__printf__, 3, 4))) privmsg(struct bio *b, const char *who, const char *fmt, ...)
52 char *buffer;
53 va_list va;
55 va_start(va, fmt);
56 vasprintf(&buffer, fmt, va);
57 va_end(va);
59 b->writeline(b, "PRIVMSG %s :%s", who, buffer);
60 free(buffer);
63 static void __attribute__ ((__format__(__printf__, 3, 4))) action(struct bio *b, const char *who, const char *fmt, ...)
65 char *buffer;
66 va_list va;
68 va_start(va, fmt);
69 vasprintf(&buffer, fmt, va);
70 va_end(va);
72 b->writeline(b, "PRIVMSG %s :\001ACTION %s\001", who, buffer);
73 free(buffer);
76 static int64_t get_time(struct bio *b, const char *target)
78 struct timeval tv;
79 if (gettimeofday(&tv, NULL) < 0) {
80 static int complain_time;
81 if (target && !complain_time++)
82 privmsg(b, target, "Could not get time: %m");
83 return -1;
84 } else
85 return tv.tv_sec;