6 static inline char *token(char **foo
, char sep
)
8 char *line
= *foo
, *ret
;
12 while (*line
&& *line
!= sep
)
18 *foo
= *line
? line
: NULL
;
24 static inline unsigned getrand(void)
27 if (RAND_bytes((unsigned char*)&r
, sizeof(r
)) > 0) {
28 fprintf(stderr
, "rolled a %u (%x)\n", r
, r
);
31 fprintf(stderr
, "random number generator error: %s", ERR_error_string(ERR_peek_last_error(), NULL
));
32 while (ERR_get_error());
39 void (*cmd
)(struct bio
*b
, const char *nick
, const char *host
, const char *target
, char *args
);
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
, ...)
58 vasprintf(&buffer
, fmt
, va
);
60 if (strchr(buffer
, '\r') || strchr(buffer
, '\n')) {
61 fprintf(stderr
, "Offending message: %s\n", buffer
);
63 assert(!"Invalid character in privmsg reply");
65 b
->writeline(b
, "PRIVMSG %s :%s", who
, 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); \
76 static void __attribute__ ((__format__(__printf__
, 3, 4))) action(struct bio
*b
, const char *who
, const char *fmt
, ...)
82 vasprintf(&buffer
, fmt
, va
);
84 if (strchr(buffer
, '\r') || strchr(buffer
, '\n')) {
85 fprintf(stderr
, "Offending message: %s\n", buffer
);
87 assert(!"Invalid character in action reply");
89 b
->writeline(b
, "PRIVMSG %s :\001ACTION %s\001", who
, 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); \
98 static int64_t get_time(struct bio
*b
, const char *target
)
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");