Ignore links by EqBot
[fillybot.git] / mod.h
blobac810c04676cb4d0c8309371ed91d1bde2609c43
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 fprintf(stderr, "rolled a %u (%x)\n", r, r);
29 return r;
31 fprintf(stderr, "random number generator error: %s", ERR_error_string(ERR_peek_last_error(), NULL));
32 while (ERR_get_error());
33 return random();
36 struct command_hash
38 char *string;
39 void (*cmd)(struct bio *b, const char *nick, const char *host, const char *target, char *args);
40 int admin;
41 int enter, left;
42 int64_t disabled_until;
43 char failed_command[];
44 } __attribute__((aligned(256)));
46 static unsigned strhash(const char *h)
48 TDB_DATA in = { .dptr = (char*)h, .dsize = strlen(h) };
49 return tdb_jenkins_hash(&in);
52 static void __attribute__ ((__format__(__printf__, 3, 4))) privmsg(struct bio *b, const char *who, const char *fmt, ...)
54 char *buffer = NULL;
55 va_list va;
57 va_start(va, fmt);
58 vasprintf(&buffer, fmt, va);
59 va_end(va);
60 if (strchr(buffer, '\r') || strchr(buffer, '\n')) {
61 fprintf(stderr, "Offending message: %s\n", buffer);
62 free(buffer);
63 assert(!"Invalid character in privmsg reply");
64 } else {
65 b->writeline(b, "PRIVMSG %s :%s", who, buffer);
66 free(buffer);
70 #define BUILD_BUG_ON(x) ((void)sizeof(char[1 - 2*!!(x)]))
71 #define privmsg(b, target, fmt, args...) do { \
72 BUILD_BUG_ON(__builtin_strrchr( "\n" fmt, '\n') != __builtin_strchr("\n" fmt, '\n')); \
73 privmsg(b, target, fmt, ##args); \
74 } while (0)
76 static void __attribute__ ((__format__(__printf__, 3, 4))) action(struct bio *b, const char *who, const char *fmt, ...)
78 char *buffer = NULL;
79 va_list va;
81 va_start(va, fmt);
82 vasprintf(&buffer, fmt, va);
83 va_end(va);
84 if (strchr(buffer, '\r') || strchr(buffer, '\n')) {
85 fprintf(stderr, "Offending message: %s\n", buffer);
86 free(buffer);
87 assert(!"Invalid character in action reply");
88 } else {
89 b->writeline(b, "PRIVMSG %s :\001ACTION %s\001", who, buffer);
90 free(buffer);
93 #define action(b, who, fmt, args...) do { \
94 BUILD_BUG_ON(__builtin_strrchr( "\n" fmt, '\n') != __builtin_strchr("\n" fmt, '\n')); \
95 action(b, who, fmt, ##args); \
96 } while (0)
98 static int64_t get_time(struct bio *b, const char *target)
100 struct timeval tv;
101 if (gettimeofday(&tv, NULL) < 0) {
102 static int complain_time;
103 if (target && !complain_time++)
104 privmsg(b, target, "Could not get time: %m");
105 return -1;
106 } else
107 return tv.tv_sec;