58efd37cc8132351a8a2fbf9c283a88b764eb161
[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 #include "mysqlConn.h"
8
9 struct binding {
10     void *func;
11     struct binding *next;
12 };
13
14 static void **binds;
15 #define BIND_TYPE_JOIN       0
16 #define BIND_TYPE_NICK       1
17 #define BIND_TYPE_PART       2
18 #define BIND_TYPE_QUIT       3
19 #define BIND_TYPE_KICK       4
20 #define BIND_TYPE_TOPIC      5
21 #define BIND_TYPE_MODE       6
22 #define BIND_TYPE_CHANMSG    7
23 #define BIND_TYPE_PRIVMSG    8
24 #define BIND_TYPE_CHANNOTICE 9
25 #define BIND_TYPE_PRIVNOTICE 10
26 #define BIND_TYPE_CHANCTCP   11
27 #define BIND_TYPE_PRIVCTCP   12
28 #define BIND_TYPE_INVITE     13
29 #define BIND_TYPE_RAW        14
30 #define BIND_TYPE_BOT_READY  15
31
32 #define TOTAL_BIND_TYPES     16
33
34 void init_bind() {
35     binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds));
36 }
37
38 void free_bind() {
39     struct binding *cbind, *next;
40     int i;
41     for(i = 0; i < TOTAL_BIND_TYPES; i++) {
42         for(cbind = binds[i]; cbind; cbind = next) {
43             next = cbind->next;
44             free(cbind);
45         }
46     }
47     free(binds);
48 }
49
50 static int is_bound(unsigned char type, void *func) {
51     struct binding *cbind;
52     for(cbind = binds[type]; cbind; cbind = cbind->next) {
53         if(cbind->func == func) 
54             return 1;
55     }
56     return 0;
57 }
58
59 #define FUNC_BIND(NAME,FUNCTYPE,TYPE) \
60 int bind_##NAME(FUNCTYPE *func) { \
61     if(!is_bound(TYPE, func)) { \
62         struct binding *cbind = malloc(sizeof(*cbind)); \
63         if (!cbind) { \
64             perror("malloc() failed"); \
65             return 0; \
66         } \
67         cbind->func = func; \
68         cbind->next = binds[TYPE]; \
69         binds[TYPE] = cbind; \
70         return 1; \
71     } \
72     return 0; \
73 }
74
75 #define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \
76 void unbind_##NAME(FUNCTYPE *func) { \
77     struct binding *cbind, *last = NULL, *next; \
78     for(cbind = binds[TYPE]; cbind; cbind = next) { \
79         next = cbind->next; \
80         if(cbind->func == func) { \
81             if(last) \
82                 last->next = cbind->next; \
83             else \
84                 binds[TYPE] = cbind->next; \
85             free(cbind); \
86         } else \
87             last = cbind; \
88     } \
89 }
90
91 #define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \
92 int event_##NAME PDECLARATION { \
93     struct binding *cbind; \
94     pre_event(TYPE); \
95     for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \
96         FUNCTYPE *func = cbind->func; \
97         func PLIST; \
98     } \
99     post_event(TYPE); \
100     return 1; \
101 }
102
103 void pre_event(UNUSED_ARG(int type)) {
104
105 }
106
107 void post_event(UNUSED_ARG(int type)) {
108     mysql_free();
109 }
110
111 //EVENTS
112
113 FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN)
114 FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN)
115 FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser))
116
117 FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK)
118 FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK)
119 FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick))
120
121 FUNC_BIND(part, part_func_t, BIND_TYPE_PART)
122 FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART)
123 FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason))
124
125 FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT)
126 FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT)
127 FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason))
128
129 FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK)
130 FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK)
131 FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason))
132
133 FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC)
134 FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC)
135 FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic))
136
137 FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE)
138 FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE)
139 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))
140
141 FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
142 FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG)
143 FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
144
145 FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
146 FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG)
147 FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
148
149 FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
150 FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE)
151 FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message))
152
153 FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
154 FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE)
155 FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message))
156
157 FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
158 FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP)
159 FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text))
160
161 FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
162 FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP)
163 FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text))
164
165 FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE)
166 FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE)
167 FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct ClientSocket *client, struct UserNode *user, char *channel), (client, user, channel))
168
169 FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW)
170 FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW)
171 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))
172
173 FUNC_BIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
174 FUNC_UNBIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY)
175 FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocket *client), (client))