/* Copyright 2011 sss * This file is part of evil_core. * * evil_core is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * evil_core is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with evil_core. If not, see .*/ #include "commonheaders.h" const int evt_handler::getType() { return evt_type; } evt_handler::evt_handler(int type) { evt_type = type; } void evt_handler::AddHandler(EVENT_HANDLER add_handler) { handler.connect(add_handler); } void* evt_handler::Execute(void* data) { return handler(data); } int RegisterEventHandler(int type, EVENT_HANDLER func) { if(!event_handlers.empty()) { event_handlers_mutex.lock(); std::list::iterator end = event_handlers.end(); for(std::list::iterator i = event_handlers.begin(); i != end; ++i) { if((*i)->getType() == type) { (*i)->AddHandler(func); logger.log(LM_DEBUG, "succesfuly added handler for event %d.\n", type); event_handlers_mutex.unlock(); return 0; } } event_handlers_mutex.unlock(); } logger.log(LM_DEBUG, "failed to add handler for event %d, event %d not registerd to any plugin or core", type,type); return 1; } int RegisterEventType(int type, int specialid) { plugin *p = NULL; if(!plugins.empty()) { plugin_list_mutex.lock(); std::list::iterator end = plugins.end(); for(std::list::iterator i = plugins.begin(); i!= end; ++i) { if((*i)->get_plugin_id() == specialid) { p = *i; logger.log(LM_DEBUG, "special id %d is right, plugin \"%s\" is in plugin list.\n", specialid, toUTF8(p->get_plugininfo()->name).c_str()); break; } } plugin_list_mutex.unlock(); if(!p) { logger.log(LM_DEBUG, "event registration for plugin: \"%s\" failed, trying to register event with wrong special id %d, right special id for this plugin is %d.\n", toUTF8(p->get_plugininfo()->name).c_str(), specialid, p->get_plugininfo()->pluginid); return 1; } } else { logger.log(LM_DEBUG, "event registration failed, something very bad happened, we have not plugin list\n"); return 1; } if(!event_handlers.empty()) { event_handlers_mutex.lock(); std::list::iterator end = event_handlers.end(); for(std::list::iterator i = event_handlers.begin(); i != end; ++i) { if((*i)->getType() == type) { logger.log(LM_DEBUG, "event registration for plugin \"%s\" failed, event already registered", toUTF8(p->get_plugininfo()->name).c_str()); event_handlers_mutex.unlock(); return 1; } } event_handlers_mutex.unlock(); } event_handlers.push_back(new evt_handler(type)); p->register_event(type); logger.log(LM_DEBUG, "event %d registration for plugin \"%s\" succesful.\n", type, toUTF8(p->get_plugininfo()->name).c_str()); return 0; } void* ExecuteEvent(int type, int pluginid, void* data) { if(!event_handlers.empty()) { std::list::iterator end = event_handlers.end(); for(std::list::iterator i = event_handlers.begin(); i != end; ++i) { if((*i)->getType() == type) { logger.log(LM_DEBUG, "event %d found, executing...\n", type); return (*i)->Execute(data); } } } logger.log(LM_DEBUG, "event %d not found...\n", type); return 0; }