X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=IRCEvents.c;h=d85dff9b9027c49736d02c66d6fe1d2ec3a41dae;hb=84fb65ee885f3f747b0a07eb07999395c0ae22f9;hp=acf4ff5cb163c66d7cbec54f3df034c16fc81faa;hpb=54e1930bcb43a0f5410ecaac28a3f33efda2debb;p=NeonServV5.git diff --git a/IRCEvents.c b/IRCEvents.c index acf4ff5..d85dff9 100644 --- a/IRCEvents.c +++ b/IRCEvents.c @@ -1,30 +1,175 @@ #include "IRCEvents.h" +#include "UserNode.h" +#include "ChanNode.h" +#include "ChanUser.h" +#include "ClientSocket.h" +#include "mysqlConn.h" -int event_join(struct ChanUser *chanuser) { - return 1; +struct binding { + void *func; + struct binding *next; +}; + +static void **binds; +#define BIND_TYPE_JOIN 0 +#define BIND_TYPE_NICK 1 +#define BIND_TYPE_PART 2 +#define BIND_TYPE_QUIT 3 +#define BIND_TYPE_KICK 4 +#define BIND_TYPE_TOPIC 5 +#define BIND_TYPE_MODE 6 +#define BIND_TYPE_CHANMSG 7 +#define BIND_TYPE_PRIVMSG 8 +#define BIND_TYPE_CHANNOTICE 9 +#define BIND_TYPE_PRIVNOTICE 10 +#define BIND_TYPE_CHANCTCP 11 +#define BIND_TYPE_PRIVCTCP 12 +#define BIND_TYPE_INVITE 13 +#define BIND_TYPE_RAW 14 +#define BIND_TYPE_BOT_READY 15 + +#define TOTAL_BIND_TYPES 16 + +void init_bind() { + binds = calloc(TOTAL_BIND_TYPES, sizeof(*binds)); } -int event_part(struct ChanUser *chanuser, char *reason) { - return 1; +void free_bind() { + struct binding *cbind, *next; + int i; + for(i = 0; i < TOTAL_BIND_TYPES; i++) { + for(cbind = binds[i]; cbind; cbind = next) { + next = cbind->next; + free(cbind); + } + } + free(binds); } -int event_quit(struct UserNode *user, char *reason) { - return 1; +static int is_bound(unsigned char type, void *func) { + struct binding *cbind; + for(cbind = binds[type]; cbind; cbind = cbind->next) { + if(cbind->func == func) + return 1; + } + return 0; } -int event_kick(struct UserNode *user, struct ChanUser *target, char *reason) { - return 1; +#define FUNC_BIND(NAME,FUNCTYPE,TYPE) \ +int bind_##NAME(FUNCTYPE *func) { \ + if(!is_bound(TYPE, func)) { \ + struct binding *cbind = malloc(sizeof(*cbind)); \ + if (!cbind) { \ + perror("malloc() failed"); \ + return 0; \ + } \ + cbind->func = func; \ + cbind->next = binds[TYPE]; \ + binds[TYPE] = cbind; \ + return 1; \ + } \ + return 0; \ } -int event_topic(struct UserNode *user, struct ChanNode *chan, const char *new_topic) { - return 1; +#define FUNC_UNBIND(NAME,FUNCTYPE,TYPE) \ +void unbind_##NAME(FUNCTYPE *func) { \ + struct binding *cbind, *last = NULL, *next; \ + for(cbind = binds[TYPE]; cbind; cbind = next) { \ + next = cbind->next; \ + if(cbind->func == func) { \ + if(last) \ + last->next = cbind->next; \ + else \ + binds[TYPE] = cbind->next; \ + free(cbind); \ + } else \ + last = cbind; \ + } \ } -int event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) { - return 1; +#define FUNC_EVENT(NAME,FUNCTYPE,TYPE,PDECLARATION,PLIST) \ +int event_##NAME PDECLARATION { \ + struct binding *cbind; \ + pre_event(TYPE); \ + for(cbind = binds[TYPE]; cbind; cbind = cbind->next) { \ + FUNCTYPE *func = cbind->func; \ + func PLIST; \ + } \ + post_event(TYPE); \ + return 1; \ +} + +void pre_event(UNUSED_ARG(int type)) { + } -int event_privmsg(struct UserNode *user, struct UserNode *target, char *message) { - return 1; +void post_event(UNUSED_ARG(int type)) { + mysql_free(); } + +//EVENTS + +FUNC_BIND(join, join_func_t, BIND_TYPE_JOIN) +FUNC_UNBIND(join, join_func_t, BIND_TYPE_JOIN) +FUNC_EVENT(join, join_func_t, BIND_TYPE_JOIN, (struct ChanUser *chanuser), (chanuser)) + +FUNC_BIND(nick, nick_func_t, BIND_TYPE_NICK) +FUNC_UNBIND(nick, nick_func_t, BIND_TYPE_NICK) +FUNC_EVENT(nick, nick_func_t, BIND_TYPE_NICK, (struct UserNode *user, char *new_nick), (user, new_nick)) + +FUNC_BIND(part, part_func_t, BIND_TYPE_PART) +FUNC_UNBIND(part, part_func_t, BIND_TYPE_PART) +FUNC_EVENT(part, part_func_t, BIND_TYPE_PART, (struct ChanUser *chanuser, char *reason), (chanuser, reason)) + +FUNC_BIND(quit, quit_func_t, BIND_TYPE_QUIT) +FUNC_UNBIND(quit, quit_func_t, BIND_TYPE_QUIT) +FUNC_EVENT(quit, quit_func_t, BIND_TYPE_QUIT, (struct UserNode *user, char *reason), (user, reason)) + +FUNC_BIND(kick, kick_func_t, BIND_TYPE_KICK) +FUNC_UNBIND(kick, kick_func_t, BIND_TYPE_KICK) +FUNC_EVENT(kick, kick_func_t, BIND_TYPE_KICK, (struct UserNode *user, struct ChanUser *target, char *reason), (user, target, reason)) + +FUNC_BIND(topic, topic_func_t, BIND_TYPE_TOPIC) +FUNC_UNBIND(topic, topic_func_t, BIND_TYPE_TOPIC) +FUNC_EVENT(topic, topic_func_t, BIND_TYPE_TOPIC, (struct UserNode *user, struct ChanNode *chan, const char *new_topic), (user, chan, new_topic)) + +FUNC_BIND(mode, mode_func_t, BIND_TYPE_MODE) +FUNC_UNBIND(mode, mode_func_t, BIND_TYPE_MODE) +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)) + +FUNC_BIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG) +FUNC_UNBIND(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG) +FUNC_EVENT(chanmsg, chanmsg_func_t, BIND_TYPE_CHANMSG, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message)) + +FUNC_BIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG) +FUNC_UNBIND(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG) +FUNC_EVENT(privmsg, privmsg_func_t, BIND_TYPE_PRIVMSG, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message)) + +FUNC_BIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE) +FUNC_UNBIND(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE) +FUNC_EVENT(channotice, channotice_func_t, BIND_TYPE_CHANNOTICE, (struct UserNode *user, struct ChanNode *chan, char *message), (user, chan, message)) + +FUNC_BIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE) +FUNC_UNBIND(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE) +FUNC_EVENT(privnotice, privnotice_func_t, BIND_TYPE_PRIVNOTICE, (struct UserNode *user, struct UserNode *target, char *message), (user, target, message)) + +FUNC_BIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP) +FUNC_UNBIND(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP) +FUNC_EVENT(chanctcp, chanctcp_func_t, BIND_TYPE_CHANCTCP, (struct UserNode *user, struct ChanNode *chan, char *command, char *text), (user, chan, command, text)) + +FUNC_BIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP) +FUNC_UNBIND(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP) +FUNC_EVENT(privctcp, privctcp_func_t, BIND_TYPE_PRIVCTCP, (struct UserNode *user, struct UserNode *target, char *command, char *text), (user, target, command, text)) + +FUNC_BIND(invite, invite_func_t, BIND_TYPE_INVITE) +FUNC_UNBIND(invite, invite_func_t, BIND_TYPE_INVITE) +FUNC_EVENT(invite, invite_func_t, BIND_TYPE_INVITE, (struct UserNode *user, char *channel), (user, channel)) + +FUNC_BIND(raw, raw_func_t, BIND_TYPE_RAW) +FUNC_UNBIND(raw, raw_func_t, BIND_TYPE_RAW) +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)) + +FUNC_BIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY) +FUNC_UNBIND(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY) +FUNC_EVENT(bot_ready, bot_ready_func_t, BIND_TYPE_BOT_READY, (struct ClientSocket *client), (client))