fc5053ffa68e4df71a061a3768bd94c3a58076c1
[NeonServV5.git] / src / IRCEvents.c
1 /* IRCEvents.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "IRCEvents.h"
19 #include "UserNode.h"
20 #include "ChanNode.h"
21 #include "ChanUser.h"
22 #include "ClientSocket.h"
23 #include "mysqlConn.h"
24
25 struct binding {
26     void *func;
27     int module_id;
28     struct binding *next;
29 };
30
31 static void **binds = NULL;
32 #define BIND_TYPE_JOIN       0
33 #define BIND_TYPE_NICK       1
34 #define BIND_TYPE_PART       2
35 #define BIND_TYPE_QUIT       3
36 #define BIND_TYPE_KICK       4
37 #define BIND_TYPE_TOPIC      5
38 #define BIND_TYPE_MODE       6
39 #define BIND_TYPE_CHANMSG    7
40 #define BIND_TYPE_PRIVMSG    8
41 #define BIND_TYPE_CHANNOTICE 9
42 #define BIND_TYPE_PRIVNOTICE 10
43 #define BIND_TYPE_CHANCTCP   11
44 #define BIND_TYPE_PRIVCTCP   12
45 #define BIND_TYPE_INVITE     13
46 #define BIND_TYPE_RAW        14
47 #define BIND_TYPE_BOT_READY  15
48 #define BIND_TYPE_REGISTERED 16
49 #define BIND_TYPE_FREEUSER   17
50 #define BIND_TYPE_FREECHAN   18
51 #define BIND_TYPE_RELOAD     19
52
53 #define TOTAL_BIND_TYPES     20
54
55 void init_bind() {
56     if(binds)
57         return;
58     binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
59 }
60
61 void free_bind() {
62     struct binding *cbind, *next;
63     int i;
64     for(i = 0; i < TOTAL_BIND_TYPES; i++) {
65         for(cbind = binds[i]; cbind; cbind = next) {
66             next = cbind->next;
67             free(cbind);
68         }
69     }
70     free(binds);
71     binds = NULL;
72 }
73
74 void unregister_module_events(int module_id) {
75     struct binding *cbind, *next, *prev;
76     int i;
77     for(i = 0; i < TOTAL_BIND_TYPES; i++) {
78         prev = NULL;
79         for(cbind = binds[i]; cbind; cbind = next) {
80             next = cbind->next;
81             if(cbind->module_id == module_id) {
82                 if(prev)
83                     prev->next = next;
84                 else
85                     binds[i] = next;
86                 free(cbind);
87             } else
88                 prev = cbind;
89         }
90     }
91 }
92
93 static int is_bound(unsigned char type, void *func) {
94     struct binding *cbind;
95     for(cbind = binds[type]; cbind; cbind = cbind->next) {
96         if(cbind->func == func) 
97             return 1;
98     }
99     return 0;
100 }
101
102 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
103 int bind_##NAME(FUNCTYPE *func, int module_id) { \
104     if(!is_bound(TYPE, func)) { \
105         struct binding *cbind = malloc(sizeof(*cbind)); \
106         if (!cbind) { \
107             perror("malloc() failed"); \
108             return 0; \
109         } \
110         cbind->func = func; \
111         cbind->module_id = module_id; \
112         cbind->next = binds[TYPE]; \
113         binds[TYPE] = cbind; \
114         return 1; \
115     } \
116     return 0; \
117 }
118
119 #define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
120 void unbind_##NAME(FUNCTYPE *func) { \
121     struct binding *cbind, *last = NULL, *next; \
122     for(cbind = binds[TYPE]; cbind; cbind = next) { \
123         next = cbind->next; \
124         if(cbind->func == func) { \
125             if(last) \
126                 last->next = cbind->next; \
127             else \
128                 binds[TYPE] = cbind->next; \
129             free(cbind); \
130         } else \
131             last = cbind; \
132     } \
133 }
134
135 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
136 int event_##NAME PDECLARATION { \
137     struct binding *cbind; \
138     pre_event(TYPE); \
139     for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
140         FUNCTYPE *func = cbind->func; \
141         func PLIST; \
142     } \
143     post_event(TYPE); \
144     return 1; \
145 }
146
147 void pre_event(UNUSED_ARG(int type)) {
148
149 }
150
151 void post_event(UNUSED_ARG(int type)) {
152
153 }
154
155 //EVENTS
156
157 FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
158 FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
159 FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser))
160
161 FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
162 FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
163 FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick))
164
165 FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
166 FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
167 FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, int quit, char *reason), (chanuser, quit, reason))
168
169 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
170 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
171 FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
172
173 FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
174 FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
175 FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
176
177 FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
178 FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
179 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))
180
181 FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
182 FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
183 FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
184
185 FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
186 FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
187 FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
188
189 FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
190 FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
191 FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
192
193 FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
194 FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
195 FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
196
197 FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
198 FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
199 FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
200
201 FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
202 FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
203 FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
204
205 FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
206 FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
207 FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct ClientSocket *client, struct UserNode *user, char *channel), (client, user, channel))
208
209 FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
210 FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
211 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))
212
213 FUNC_BIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
214 FUNC_UNBIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
215 FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocket *client), (client))
216
217 FUNC_BIND(registered, registered_func_t, BIND_TYPE_REGISTERED)
218 FUNC_UNBIND(registered, registered_func_t, BIND_TYPE_REGISTERED)
219 FUNC_EVENT(registered, registered_func_t, BIND_TYPE_REGISTERED, (struct UserNode *user, char *new_mask), (user, new_mask))
220
221 FUNC_BIND(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER)
222 FUNC_UNBIND(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER)
223 FUNC_EVENT(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER, (struct UserNode *user), (user))
224
225 FUNC_BIND(freechan, freechan_func_t, BIND_TYPE_FREECHAN)
226 FUNC_UNBIND(freechan, freechan_func_t, BIND_TYPE_FREECHAN)
227 FUNC_EVENT(freechan, freechan_func_t, BIND_TYPE_FREECHAN, (struct ChanNode *chan), (chan))
228
229 FUNC_BIND(reload, reload_func_t, BIND_TYPE_RELOAD)
230 FUNC_UNBIND(reload, reload_func_t, BIND_TYPE_RELOAD)
231 FUNC_EVENT(reload, reload_func_t, BIND_TYPE_RELOAD, (int initialization), (initialization))