#include #include #include #include "event.h" #include "global.h" #include "plugin.h" #include "plugin_client_header.h" struct plugin_data{ void *handle; plugin_event_func_t *callback; }; static i64 num_plugins=0; static i64 plugins_cap=8; static struct plugin_data *plugins_list=NULL; static void consume_event(const struct event_item *event){ struct plugin_event pe; switch(event->type){ case EVENT_MESSAGE: pe.type=PLUGIN_EVENT_MESSAGE; break; case EVENT_ONLINE: pe.type=PLUGIN_EVENT_ONLINE; break; case EVENT_JOIN: pe.type=PLUGIN_EVENT_JOIN; break; case EVENT_LEAVE: pe.type=PLUGIN_EVENT_LEAVE; break; default: die("Unknown event type %d in plugin.c:consume_event",event->type); } pe.timestamp=event->timestamp; pe.message=event->message; pe.user=event->user; pe.room=event->room; pe.num=event->num; for(i64 i=0;ihandle=dlopen(fname,RTLD_NOW|RTLD_LOCAL); if(data->handle==NULL){ die("Error loading plugin '%s': %s",fname,dlerror()); } plugin_event_func_t* (*init_func)(void)=NULL; dlerror(); *(void**)&init_func=dlsym(data->handle,"plugin_init_func"); char *err=dlerror(); if(err!=NULL){ die("Error reading symbol 'plugin_init_func' from plugin '%s'",fname); } if(init_func==NULL){ die("Plugin '%s' exports NULL init function",fname); } data->callback=init_func(); if(data->callback==NULL){ die("Init function of plugin '%s' returned NULL",fname); } printf("Loaded plugin '%s'\n",fname); }