add ident to admin checking, and add hope
[fillybot.git] / mod.h
blob2c56e58a707c10ac56922ae041f57691a3c353f9
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_bytes((unsigned char*)&r, sizeof(r)) > 0)
28 return r;
30 fprintf(stderr, "random number generator error: %s", ERR_error_string(ERR_peek_last_error(), NULL));
31 while (ERR_get_error());
32 return random();
35 struct command_hash
37 char *string;
38 void (*cmd)(struct bio *b, const char *nick, const char *host, const char *target, char *args);
39 int admin;
40 int enter, left;
41 int64_t disabled_until;
42 char failed_command[];
43 } __attribute__((aligned(256)));
45 static unsigned strhash(const char *h)
47 TDB_DATA in = { .dptr = (char*)h, .dsize = strlen(h) };
48 return tdb_jenkins_hash(&in);
51 static void __attribute__ ((__format__(__printf__, 3, 4))) privmsg(struct bio *b, const char *who, const char *fmt, ...)
53 char *buffer = NULL;
54 va_list va;
56 va_start(va, fmt);
57 vasprintf(&buffer, fmt, va);
58 va_end(va);
59 if (strchr(buffer, '\r') || strchr(buffer, '\n')) {
60 fprintf(stderr, "Offending message: %s\n", buffer);
61 free(buffer);
62 assert(!"Invalid character in privmsg reply");
63 } else {
64 b->writeline(b, "PRIVMSG %s :%s", who, buffer);
65 free(buffer);
69 #define BUILD_BUG_ON(x) ((void)sizeof(char[1 - 2*!!(x)]))
70 #define privmsg(b, target, fmt, args...) do { \
71 BUILD_BUG_ON(__builtin_strrchr( "\n" fmt, '\n') != __builtin_strchr("\n" fmt, '\n')); \
72 privmsg(b, target, fmt, ##args); \
73 } while (0)
75 static void __attribute__ ((__format__(__printf__, 3, 4))) action(struct bio *b, const char *who, const char *fmt, ...)
77 char *buffer = NULL;
78 va_list va;
80 va_start(va, fmt);
81 vasprintf(&buffer, fmt, va);
82 va_end(va);
83 if (strchr(buffer, '\r') || strchr(buffer, '\n')) {
84 fprintf(stderr, "Offending message: %s\n", buffer);
85 free(buffer);
86 assert(!"Invalid character in action reply");
87 } else {
88 b->writeline(b, "PRIVMSG %s :\001ACTION %s\001", who, buffer);
89 free(buffer);
92 #define action(b, who, fmt, args...) do { \
93 BUILD_BUG_ON(__builtin_strrchr( "\n" fmt, '\n') != __builtin_strchr("\n" fmt, '\n')); \
94 action(b, who, fmt, ##args); \
95 } while (0)
97 static int64_t get_time(struct bio *b, const char *target)
99 struct timeval tv;
100 if (gettimeofday(&tv, NULL) < 0) {
101 static int complain_time;
102 if (target && !complain_time++)
103 privmsg(b, target, "Could not get time: %m");
104 return -1;
105 } else
106 return tv.tv_sec;