6 #include "ClientSocket.h"
14 #define BIND_TYPE_JOIN 0
15 #define BIND_TYPE_NICK 1
16 #define BIND_TYPE_PART 2
17 #define BIND_TYPE_QUIT 3
18 #define BIND_TYPE_KICK 4
19 #define BIND_TYPE_TOPIC 5
20 #define BIND_TYPE_MODE 6
21 #define BIND_TYPE_CHANMSG 7
22 #define BIND_TYPE_PRIVMSG 8
23 #define BIND_TYPE_CHANNOTICE 9
24 #define BIND_TYPE_PRIVNOTICE 10
25 #define BIND_TYPE_CHANCTCP 11
26 #define BIND_TYPE_PRIVCTCP 12
27 #define BIND_TYPE_INVITE 13
28 #define BIND_TYPE_RAW 14
29 #define BIND_TYPE_BOT_READY 15
31 #define TOTAL_BIND_TYPES 16
34 binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
38 struct binding *cbind, *next;
40 for(i = 0; i < TOTAL_BIND_TYPES; i++) {
41 for(cbind = binds[i]; cbind; cbind = next) {
49 static int is_bound(unsigned char type, void *func) {
50 struct binding *cbind;
51 for(cbind = binds[type]; cbind; cbind = cbind->next) {
52 if(cbind->func == func)
58 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
59 int bind_##NAME(FUNCTYPE *func) { \
60 if(!is_bound(TYPE, func)) { \
61 struct binding *cbind = malloc(sizeof(*cbind)); \
63 perror("malloc() failed"); \
67 cbind->next = binds[TYPE]; \
68 binds[TYPE] = cbind; \
74 #define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
75 void unbind_##NAME(FUNCTYPE *func) { \
76 struct binding *cbind, *last = NULL, *next; \
77 for(cbind = binds[TYPE]; cbind; cbind = next) { \
79 if(cbind->func == func) { \
81 last->next = cbind->next; \
83 binds[TYPE] = cbind->next; \
90 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
91 int event_##NAME PDECLARATION { \
92 struct binding *cbind; \
93 for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
94 FUNCTYPE *func = cbind->func; \
102 FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
103 FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
104 FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser))
106 FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
107 FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
108 FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick))
110 FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
111 FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
112 FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason))
114 FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT)
115 FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT)
116 FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason))
118 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
119 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
120 FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
122 FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
123 FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
124 FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
126 FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
127 FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
128 FUNC_EVENT(mode, mode_func_t, BIND_TYPE_MODE, (struct UserNode *user, struct ChanNode *chan, char *modes, char **args, int argc), (user, chan, modes, args, argc))
130 FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
131 FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
132 FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
134 FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
135 FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
136 FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
138 FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
139 FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
140 FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
142 FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
143 FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
144 FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
146 FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
147 FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
148 FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
150 FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
151 FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
152 FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
154 FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
155 FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
156 FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct UserNode *user, char *channel), (user, channel))
158 FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
159 FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
160 FUNC_EVENT(raw, raw_func_t, BIND_TYPE_RAW, (struct ClientSocket *client, char *from, char *cmd, char **argv, int argc), (client, from, cmd, argv, argc))
162 FUNC_BIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
163 FUNC_UNBIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
164 FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocket *client), (client))