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
30 #define TOTAL_BIND_TYPES 15
33 binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
37 struct binding *cbind, *next;
39 for(i = 0; i < TOTAL_BIND_TYPES; i++) {
40 for(cbind = binds[i]; cbind; cbind = next) {
48 static int is_bound(unsigned char type, void *func) {
49 struct binding *cbind;
50 for(cbind = binds[type]; cbind; cbind = cbind->next) {
51 if(cbind->func == func)
57 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
58 int bind_##NAME(FUNCTYPE *func) { \
59 if(!is_bound(TYPE, func)) { \
60 struct binding *cbind = malloc(sizeof(*cbind)); \
62 perror("malloc() failed"); \
66 cbind->next = binds[TYPE]; \
67 binds[TYPE] = cbind; \
73 #define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
74 void unbind_##NAME(FUNCTYPE *func) { \
75 struct binding *cbind, *last = NULL, *next; \
76 for(cbind = binds[TYPE]; cbind; cbind = next) { \
78 if(cbind->func == func) { \
80 last->next = cbind->next; \
82 binds[TYPE] = cbind->next; \
89 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
90 int event_##NAME PDECLARATION { \
91 struct binding *cbind; \
92 for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
93 FUNCTYPE *func = cbind->func; \
101 FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
102 FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
103 FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser))
105 FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
106 FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
107 FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick))
109 FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
110 FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
111 FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason))
113 FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT)
114 FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT)
115 FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason))
117 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
118 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
119 FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
121 FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
122 FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
123 FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
125 FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
126 FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
127 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))
129 FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
130 FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
131 FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
133 FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
134 FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
135 FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
137 FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
138 FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
139 FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
141 FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
142 FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
143 FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
145 FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
146 FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
147 FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
149 FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
150 FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
151 FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
153 FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
154 FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
155 FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct UserNode *user, char *channel), (user, channel))
157 FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
158 FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
159 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))