From: <Jef...@us...> - 2007-10-09 22:18:08
|
Revision: 256 http://libirc.svn.sourceforge.net/libirc/?rev=256&view=rev Author: JeffM2501 Date: 2007-10-09 15:18:05 -0700 (Tue, 09 Oct 2007) Log Message: ----------- allow registration of command handlers Modified Paths: -------------- trunk/libirc/include/IRCServer.h trunk/libirc/src/IRCServer.cpp Modified: trunk/libirc/include/IRCServer.h =================================================================== --- trunk/libirc/include/IRCServer.h 2007-10-09 22:04:39 UTC (rev 255) +++ trunk/libirc/include/IRCServer.h 2007-10-09 22:18:05 UTC (rev 256) @@ -88,7 +88,7 @@ class ChannelUserData { public: - virtual ~ChannelUserData(); + virtual ~ChannelUserData(){}; bool voice; bool op; }; @@ -104,23 +104,23 @@ class IRCServerCommandHandler { public: - IRCServerCommandHandler(){return;} - virtual ~IRCServerCommandHandler(){return;} + IRCServerCommandHandler(){return;} + virtual ~IRCServerCommandHandler(){return;} - // called when the system wishes to know the name of this command - virtual std::string getCommandName ( void ){return name;} + // called when the system wishes to know the name of this command + virtual std::string getCommandName ( void ){return name;} - // the send and receve methods return true if the default handler is to be called - // it is recomended that the default ALWAYS be called, as it often sets internal data for other mesages + // the send and receve methods return true if the default handler is to be called + // it is recomended that the default ALWAYS be called, as it often sets internal data for other mesages - // called when the client receves a command of this type - virtual bool receve ( IRCServer *server, IRCServerConnectedClient *client, const std::string &command, const BaseIRCCommandInfo &info ){return true;} + // called when the client receves a command of this type + virtual bool receve ( IRCServer *server, IRCServerConnectedClient *client, const std::string &command, const BaseIRCCommandInfo &info ){return true;} - // called when the user wishes to send a command of this type - virtual bool send ( IRCServer *server, IRCServerConnectedClient *client, const std::string &command, const BaseIRCCommandInfo &info ){return true;} + // called when the user wishes to send a command of this type + virtual bool send ( IRCServer *server, IRCServerConnectedClient *client, const std::string &command, const BaseIRCCommandInfo &info ){return true;} protected: - std::string name; + std::string name; }; class IRCServer : public TCPServerDataPendingListener @@ -169,6 +169,13 @@ // commands virtual bool receveCommand ( const std::string &commandName, IRCServerConnectedClient *client, const BaseIRCCommandInfo &info ); + //command handler methods... for lower level API + virtual bool registerCommandHandler ( IRCServerCommandHandler *handler ); + virtual bool removeCommandHandler ( IRCServerCommandHandler *handler ); + + virtual int listUserHandledCommands ( std::vector<std::string> &commandList ); + virtual int listDefaultHandledCommands ( std::vector<std::string> &commandList ); + protected: friend class IRCServerConnectedClient; Modified: trunk/libirc/src/IRCServer.cpp =================================================================== --- trunk/libirc/src/IRCServer.cpp 2007-10-09 22:04:39 UTC (rev 255) +++ trunk/libirc/src/IRCServer.cpp 2007-10-09 22:18:05 UTC (rev 256) @@ -498,7 +498,79 @@ } +bool IRCServer::registerCommandHandler ( IRCServerCommandHandler *handler ) +{ + if (!handler) + return false; + std::string command = handler->getCommandName(); + + tmCommandHandlerListMap::iterator commandListItr = userCommandHandlers.find(command); + if (commandListItr == userCommandHandlers.end()) + { + std::vector<IRCServerCommandHandler*> handlerList; + handlerList.push_back(handler); + userCommandHandlers[command] = handlerList; + } + else + commandListItr->second.push_back(handler); + + return true; +} + +bool IRCServer::removeCommandHandler ( IRCServerCommandHandler *handler ) +{ + if (!handler) + return false; + + std::string command = handler->getCommandName(); + + tmCommandHandlerListMap::iterator commandListItr = userCommandHandlers.find(command); + if (commandListItr == userCommandHandlers.end()) + return false; + else + { + std::vector<IRCServerCommandHandler*>::iterator itr = commandListItr->second.begin(); + while ( itr != commandListItr->second.end()) + { + if (*itr == handler) + itr = commandListItr->second.erase(itr); + else + itr++; + } + } + return true; +} + +int IRCServer::listUserHandledCommands ( std::vector<std::string> &commandList ) +{ + commandList.clear(); + + tmCommandHandlerListMap::iterator itr = userCommandHandlers.begin(); + + while (itr != userCommandHandlers.end()) + { + commandList.push_back(itr->first); + itr++; + } + return (int)commandList.size(); +} + +int IRCServer::listDefaultHandledCommands ( std::vector<std::string> &commandList ) +{ + commandList.clear(); + + tmCommandHandlerMap::iterator itr = defaultCommandHandlers.begin(); + + while (itr != defaultCommandHandlers.end()) + { + commandList.push_back(itr->first); + itr++; + } + return (int)commandList.size(); +} + + // Local Variables: *** // mode:C++ *** // tab-width: 8 *** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |