we shouldn't parse args if there are no args
[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 void free_bind() {
37     struct binding *cbind, *next;
38     int i;
39     for(i = 0; i < TOTAL_BIND_TYPES; i++) {
40         for(cbind = binds[i]; cbind; cbind = next) {
41             next = cbind->next;
42             free(cbind);
43         }
44     }
45     free(binds);
46 }
47
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) 
52             return 1;
53     }
54     return 0;
55 }
56
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)); \
61         if (!cbind) { \
62             perror("malloc() failed"); \
63             return 0; \
64         } \
65         cbind->func = func; \
66         cbind->next = binds[TYPE]; \
67         binds[TYPE] = cbind; \
68         return 1; \
69     } \
70     return 0; \
71 }
72
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) { \
77         next = cbind->next; \
78         if(cbind->func == func) { \
79             if(last) \
80                 last->next = cbind->next; \
81             else \
82                 binds[TYPE] = cbind->next; \
83             free(cbind); \
84         } else \
85             last = cbind; \
86     } \
87 }
88
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; \
94         func PLIST; \
95     } \
96     return 1; \
97 }
98
99 //EVENTS
100
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))
104
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))
108
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))
112
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))
116
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))
120
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))
124
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))
128
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))
132
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))
136
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))
140
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))
144
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))
148
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))
152
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))
156
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))
160