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