Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / IRCEvents.c
1 /* IRCEvents.c - NeonServ v5.3
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     struct binding *next;
28 };
29
30 static void **binds;
31 #define BIND_TYPE_JOIN       0
32 #define BIND_TYPE_NICK       1
33 #define BIND_TYPE_PART       2
34 #define BIND_TYPE_QUIT       3
35 #define BIND_TYPE_KICK       4
36 #define BIND_TYPE_TOPIC      5
37 #define BIND_TYPE_MODE       6
38 #define BIND_TYPE_CHANMSG    7
39 #define BIND_TYPE_PRIVMSG    8
40 #define BIND_TYPE_CHANNOTICE 9
41 #define BIND_TYPE_PRIVNOTICE 10
42 #define BIND_TYPE_CHANCTCP   11
43 #define BIND_TYPE_PRIVCTCP   12
44 #define BIND_TYPE_INVITE     13
45 #define BIND_TYPE_RAW        14
46 #define BIND_TYPE_BOT_READY  15
47 #define BIND_TYPE_REGISTERED 16
48 #define BIND_TYPE_FREEUSER   17
49 #define BIND_TYPE_FREECHAN   18
50
51 #define TOTAL_BIND_TYPES     19
52
53 void init_bind() {
54     binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
55 }
56
57 void free_bind() {
58     struct binding *cbind, *next;
59     int i;
60     for(i = 0; i < TOTAL_BIND_TYPES; i++) {
61         for(cbind = binds[i]; cbind; cbind = next) {
62             next = cbind->next;
63             free(cbind);
64         }
65     }
66     free(binds);
67 }
68
69 static int is_bound(unsigned char type, void *func) {
70     struct binding *cbind;
71     for(cbind = binds[type]; cbind; cbind = cbind->next) {
72         if(cbind->func == func) 
73             return 1;
74     }
75     return 0;
76 }
77
78 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
79 int bind_##NAME(FUNCTYPE *func) { \
80     if(!is_bound(TYPE, func)) { \
81         struct binding *cbind = malloc(sizeof(*cbind)); \
82         if (!cbind) { \
83             perror("malloc() failed"); \
84             return 0; \
85         } \
86         cbind->func = func; \
87         cbind->next = binds[TYPE]; \
88         binds[TYPE] = cbind; \
89         return 1; \
90     } \
91     return 0; \
92 }
93
94 #define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
95 void unbind_##NAME(FUNCTYPE *func) { \
96     struct binding *cbind, *last = NULL, *next; \
97     for(cbind = binds[TYPE]; cbind; cbind = next) { \
98         next = cbind->next; \
99         if(cbind->func == func) { \
100             if(last) \
101                 last->next = cbind->next; \
102             else \
103                 binds[TYPE] = cbind->next; \
104             free(cbind); \
105         } else \
106             last = cbind; \
107     } \
108 }
109
110 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
111 int event_##NAME PDECLARATION { \
112     struct binding *cbind; \
113     pre_event(TYPE); \
114     for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
115         FUNCTYPE *func = cbind->func; \
116         func PLIST; \
117     } \
118     post_event(TYPE); \
119     return 1; \
120 }
121
122 void pre_event(UNUSED_ARG(int type)) {
123
124 }
125
126 void post_event(UNUSED_ARG(int type)) {
127     mysql_free();
128 }
129
130 //EVENTS
131
132 FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
133 FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
134 FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser))
135
136 FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
137 FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
138 FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick))
139
140 FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
141 FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
142 FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason))
143
144 FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT)
145 FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT)
146 FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason))
147
148 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
149 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
150 FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
151
152 FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
153 FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
154 FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
155
156 FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
157 FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
158 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))
159
160 FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
161 FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
162 FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
163
164 FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
165 FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
166 FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
167
168 FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
169 FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
170 FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
171
172 FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
173 FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
174 FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
175
176 FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
177 FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
178 FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
179
180 FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
181 FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
182 FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
183
184 FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
185 FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
186 FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct ClientSocket *client, struct UserNode *user, char *channel), (client, user, channel))
187
188 FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
189 FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
190 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))
191
192 FUNC_BIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
193 FUNC_UNBIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
194 FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocket *client), (client))
195
196 FUNC_BIND(registered, registered_func_t, BIND_TYPE_REGISTERED)
197 FUNC_UNBIND(registered, registered_func_t, BIND_TYPE_REGISTERED)
198 int event_registered(struct UserNode *old_user, struct UserNode *new_user) {
199     struct binding *cbind;
200     int ret = 0;
201     pre_event(BIND_TYPE_REGISTERED);
202     for(cbind = binds[BIND_TYPE_REGISTERED]; cbind; cbind = cbind->next) {
203         registered_func_t *func = cbind->func;
204         ret |= func(old_user, new_user);
205     }
206     post_event(BIND_TYPE_REGISTERED);
207     return ret;
208 }
209
210 FUNC_BIND(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER)
211 FUNC_UNBIND(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER)
212 FUNC_EVENT(freeuser, freeuser_func_t, BIND_TYPE_FREEUSER, (struct UserNode *user), (user))
213
214 FUNC_BIND(freechan, freechan_func_t, BIND_TYPE_FREECHAN)
215 FUNC_UNBIND(freechan, freechan_func_t, BIND_TYPE_FREECHAN)
216 FUNC_EVENT(freechan, freechan_func_t, BIND_TYPE_FREECHAN, (struct ChanNode *chan), (chan))