implemented bind system
[NeonServV5.git] / IRCEvents.c
1
2 #include "IRCEvents.h"
3 #include "UserNode.h"
4 #include "ChanNode.h"
5 #include "ChanUser.h"
6 #include "ClientSocket.h"
7
8 struct binding {
9     void *func;
10     struct binding *next;
11 }
12
13 static void **binds;
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
30 #define TOTAL_BIND_TYPES     15
31
32 void init_bind() {
33     binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
34 }
35
36 static int is_bound(unsigned char type, void *func) {
37     struct binding *cbind;
38     for(cbind = binds[type]; cbind; cbind = cbind->next) {
39         if(cbind->func == func) 
40             return 1;
41     }
42     return 0;
43 }
44
45 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
46 int bind_NAME(FUNCTYPE *func) { \
47     if(!is_bound(TYPE, func)) { \
48         struct binding *bind = malloc(sizeof(*bind)); \
49         if (!bind) { \
50             perror("malloc() failed"); \
51             return 0; \
52         } \
53         bind->func = func; \
54         bind->next = binds[TYPE]; \
55         binds[TYPE] = bind; \
56         return 1; \
57     } \
58 }
59
60 #define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
61 void unbind_NAME(FUNCTYPE *func) { \
62     struct binding *cbind, *last = NULL, *next; \
63     for(cbind = binds[TYPE]; cbind; cbind = next) { \
64         next = cbind->next; \
65         if(cbind->func == func) { \
66             if(last) \
67                 last->next = cbind->next; \
68             else \
69                 binds[TYPE] = cbind->next \
70             free(cbind); \
71         } else \
72             last = cbind; \
73     } \
74 }
75
76 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
77 int event_NAME(PDECLARATION) { \
78     struct binding *cbind; \
79     for(cbind = binds[TYPE]; cbind; cbind = next) { \
80         FUNCTYPE *func = cbind->func; \
81         func(PLIST); \
82     } \
83     return 1; \
84 }
85
86 //EVENTS
87
88 FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
89 FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
90 FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, struct ChanUser *chanuser, chanuser)
91
92 FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
93 FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
94 FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, newnick))
95
96 FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
97 FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
98 FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason))
99
100 FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT)
101 FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT)
102 FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason))
103
104 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
105 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
106 FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
107
108 FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
109 FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
110 FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
111
112 FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
113 FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
114 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))
115
116 FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
117 FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
118 FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
119
120 FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
121 FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
122 FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
123
124 FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
125 FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
126 FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
127
128 FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
129 FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
130 FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
131
132 FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
133 FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
134 FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
135
136 FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
137 FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
138 FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
139
140 FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
141 FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
142 FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct UserNode *user, char *channel), (user, channel))
143
144 FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
145 FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
146 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))
147