Browse Source

Copied GenericCommand from 'Jupiter Bot' with some modifcation

Plugin: Added events related to GenericCommand
release/0.19
Jessica James 9 years ago
parent
commit
63ac73dc04
  1. 74
      Jupiter/GenericCommand.cpp
  2. 128
      Jupiter/GenericCommand.h
  3. 6
      Jupiter/Jupiter.vcxproj
  4. 18
      Jupiter/Jupiter.vcxproj.filters
  5. 10
      Jupiter/Plugin.cpp
  6. 13
      Jupiter/Plugin.h
  7. BIN
      Release/Jupiter.lib

74
Jupiter/GenericCommand.cpp

@ -0,0 +1,74 @@
/**
* Copyright (C) 2015-2016 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#include "GenericCommand.h"
#include "Plugin.h"
Jupiter::ArrayList<Jupiter::GenericCommand> o_genericCommands;
Jupiter::ArrayList<Jupiter::GenericCommand> *Jupiter::g_generic_commands = &o_genericCommands;
/** GenericCommand */
Jupiter::GenericCommand::GenericCommand()
{
o_genericCommands.add(this);
for (size_t index = 0; index != Jupiter::plugins->size(); ++index)
Jupiter::plugins->get(index)->OnGenericCommandAdd(*this);
}
Jupiter::GenericCommand::~GenericCommand()
{
size_t count = o_genericCommands.size();
while (count != 0)
if (o_genericCommands.get(--count) == this)
{
o_genericCommands.remove(count);
break;
}
for (size_t index = 0; index != Jupiter::plugins->size(); ++index)
Jupiter::plugins->get(index)->OnGenericCommandRemove(*this);
}
/** GenericCommand::ResponseLine */
Jupiter::GenericCommand::ResponseLine::ResponseLine(const Jupiter::ReadableString &response_, GenericCommand::DisplayType type_)
{
GenericCommand::ResponseLine::response = response_;
GenericCommand::ResponseLine::type = type_;
}
Jupiter::GenericCommand::ResponseLine *Jupiter::GenericCommand::ResponseLine::set(const Jupiter::ReadableString &response_, GenericCommand::DisplayType type_)
{
GenericCommand::ResponseLine::response = response_;
GenericCommand::ResponseLine::type = type_;
return this;
}
Jupiter::GenericCommand *Jupiter::getGenericCommand(const Jupiter::ReadableString &trigger)
{
size_t count = o_genericCommands.size();
while (count != 0)
{
Jupiter::GenericCommand *cmd = o_genericCommands.get(--count);
if (cmd->matches(trigger))
return cmd;
}
return nullptr;
}

128
Jupiter/GenericCommand.h

@ -0,0 +1,128 @@
/**
* Copyright (C) 2015-2016 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#if !defined _GENERICCOMMAND_H_HEADER
#define _GENERICCOMMAND_H_HEADER
/**
* @file GenericCommand.h
* @brief Provides an extendable command system.
*/
#include "Command.h"
#include "String.h"
/** DLL Linkage Nagging */
#if defined _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251)
#endif
namespace Jupiter
{
/**
* @brief Provides the base for generic commands.
*/
class JUPITER_API GenericCommand : public Jupiter::Command
{
public:
/** Enumerated class to guide output in generic command interpreters */
enum class DisplayType
{
PublicSuccess,
PrivateSuccess,
PublicError,
PrivateError,
};
/** Data entry returned by trigger() */
struct JUPITER_API ResponseLine
{
Jupiter::StringS response;
GenericCommand::DisplayType type;
ResponseLine *next = nullptr;
/**
* @brief Sets the response and type of the ResponseLine.
*
* @param in_response Value to set response to.
* @param in_type Value to set type to.
* @return This.
*/
ResponseLine *set(const Jupiter::ReadableString &response, GenericCommand::DisplayType type);
ResponseLine() = default;
ResponseLine(const Jupiter::ReadableString &response, GenericCommand::DisplayType type);
};
/**
* @brief Called when the command is to be executed.
*
* @param input Parameters passed to the command by the user.
*/
virtual ResponseLine *trigger(const Jupiter::ReadableString &input) = 0;
/**
* @brief Default constructor for the generic command class.
*/
GenericCommand();
/**
* @brief Destructor for the generic command class.
*/
virtual ~GenericCommand();
};
/**
* @brief Fetches a generic command based on its trigger.
*
* @param trigger Trigger of the command to fetch.
* @return A generic command if it exists, nullptr otherwise.
*/
JUPITER_API extern GenericCommand *getGenericCommand(const Jupiter::ReadableString &trigger);
/** Generic command list */
JUPITER_API extern Jupiter::ArrayList<GenericCommand> *g_generic_commands;
}
/** Generic Command Macros */
/** Defines the core of a generic command's declaration. This should be included in every generic command. */
#define BASE_GENERIC_COMMAND(CLASS) \
public: \
CLASS(); \
GenericCommand::ResponseLine *trigger(const Jupiter::ReadableString &parameters); \
const Jupiter::ReadableString &getHelp(const Jupiter::ReadableString &parameters); \
static CLASS instance;
/** Expands to become the entire declaration for a generic command. In most cases, this will be sufficient. */
#define GENERIC_GENERIC_COMMAND(CLASS) \
class CLASS : public GenericCommand { \
BASE_GENERIC_COMMAND(CLASS) \
};
/** Instantiates a generic command. */
#define GENERIC_COMMAND_INIT(CLASS) \
CLASS CLASS :: instance = CLASS (); \
CLASS & CLASS ## _instance = CLASS :: instance;
/** Re-enable warnings */
#if defined _MSC_VER
#pragma warning(pop)
#endif
#endif // _GENERICCOMMAND_H_HEADER

6
Jupiter/Jupiter.vcxproj

@ -182,13 +182,13 @@
<ClCompile Include="DataBuffer.cpp" /> <ClCompile Include="DataBuffer.cpp" />
<ClCompile Include="File.cpp" /> <ClCompile Include="File.cpp" />
<ClCompile Include="Functions.c" /> <ClCompile Include="Functions.c" />
<ClCompile Include="GenericCommand.cpp" />
<ClCompile Include="Hash.c" /> <ClCompile Include="Hash.c" />
<ClCompile Include="HTTP_Server.cpp" /> <ClCompile Include="HTTP_Server.cpp" />
<ClCompile Include="INIFile.cpp" /> <ClCompile Include="INIFile.cpp" />
<ClCompile Include="IRC.cpp" /> <ClCompile Include="IRC.cpp" />
<ClCompile Include="IRC_Client.cpp" /> <ClCompile Include="IRC_Client.cpp" />
<ClCompile Include="IRC_Server.cpp" /> <ClCompile Include="IRC_Server.cpp" />
<ClCompile Include="JSON.cpp" />
<ClCompile Include="Jupiter.cpp" /> <ClCompile Include="Jupiter.cpp" />
<ClCompile Include="Plugin.cpp" /> <ClCompile Include="Plugin.cpp" />
<ClCompile Include="Queue.cpp" /> <ClCompile Include="Queue.cpp" />
@ -198,9 +198,6 @@
<ClCompile Include="TCPSocket.cpp" /> <ClCompile Include="TCPSocket.cpp" />
<ClCompile Include="Timer.cpp" /> <ClCompile Include="Timer.cpp" />
<ClCompile Include="UDPSocket.cpp" /> <ClCompile Include="UDPSocket.cpp" />
<ClCompile Include="XINI.cpp" />
<ClCompile Include="XML.cpp" />
<ClCompile Include="YAML.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ArrayList.h" /> <ClInclude Include="ArrayList.h" />
@ -213,6 +210,7 @@
<ClInclude Include="DataBuffer_Imp.h" /> <ClInclude Include="DataBuffer_Imp.h" />
<ClInclude Include="DLList.h" /> <ClInclude Include="DLList.h" />
<ClInclude Include="File.h" /> <ClInclude Include="File.h" />
<ClInclude Include="GenericCommand.h" />
<ClInclude Include="Hash.h" /> <ClInclude Include="Hash.h" />
<ClInclude Include="HTTP.h" /> <ClInclude Include="HTTP.h" />
<ClInclude Include="HTTP_QueryString.h" /> <ClInclude Include="HTTP_QueryString.h" />

18
Jupiter/Jupiter.vcxproj.filters

@ -111,21 +111,9 @@
<ClCompile Include="IRC_Client.cpp"> <ClCompile Include="IRC_Client.cpp">
<Filter>Source Files\IRC</Filter> <Filter>Source Files\IRC</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="XML.cpp">
<Filter>Source Files\Files</Filter>
</ClCompile>
<ClCompile Include="Config.cpp"> <ClCompile Include="Config.cpp">
<Filter>Source Files\Files</Filter> <Filter>Source Files\Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="JSON.cpp">
<Filter>Source Files\Files</Filter>
</ClCompile>
<ClCompile Include="YAML.cpp">
<Filter>Source Files\Files</Filter>
</ClCompile>
<ClCompile Include="XINI.cpp">
<Filter>Source Files\Files</Filter>
</ClCompile>
<ClCompile Include="Hash.c"> <ClCompile Include="Hash.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -138,6 +126,9 @@
<ClCompile Include="HTTP_Server.cpp"> <ClCompile Include="HTTP_Server.cpp">
<Filter>Source Files\HTTP</Filter> <Filter>Source Files\HTTP</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="GenericCommand.cpp">
<Filter>Source Files\Object Extensions</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Functions.h"> <ClInclude Include="Functions.h">
@ -272,6 +263,9 @@
<ClInclude Include="HTTP_QueryString.h"> <ClInclude Include="HTTP_QueryString.h">
<Filter>Header Files\HTTP</Filter> <Filter>Header Files\HTTP</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GenericCommand.h">
<Filter>Header Files\Object Extensions</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="Jupiter.rc"> <ResourceCompile Include="Jupiter.rc">

10
Jupiter/Plugin.cpp

@ -392,4 +392,12 @@ void Jupiter::Plugin::OnMode(Jupiter::IRC::Client *, const Jupiter::ReadableStri
void Jupiter::Plugin::OnThink(Jupiter::IRC::Client *) void Jupiter::Plugin::OnThink(Jupiter::IRC::Client *)
{ {
return; return;
} }
void Jupiter::Plugin::OnGenericCommandAdd(Jupiter::GenericCommand &)
{
}
void Jupiter::Plugin::OnGenericCommandRemove(Jupiter::GenericCommand &)
{
}

13
Jupiter/Plugin.h

@ -40,6 +40,7 @@ namespace Jupiter
{ {
/** Forward declaration */ /** Forward declaration */
namespace IRC { class Client; } namespace IRC { class Client; }
class GenericCommand;
/** /**
* @brief Provides the basis for plugin interfacing. * @brief Provides the basis for plugin interfacing.
@ -105,6 +106,8 @@ namespace Jupiter
*/ */
virtual bool initialize(); virtual bool initialize();
/** IRC Listeners */
/** /**
* @brief This is called when a connection has been successfully established. * @brief This is called when a connection has been successfully established.
* The current implementation calls this after the MOTD. * The current implementation calls this after the MOTD.
@ -262,6 +265,16 @@ namespace Jupiter
*/ */
virtual void OnThink(Jupiter::IRC::Client *server); virtual void OnThink(Jupiter::IRC::Client *server);
/**
* @brief This is called when a GenericCommand is instantiated.
*/
virtual void OnGenericCommandAdd(Jupiter::GenericCommand &command);
/**
* @brief This is called when a GenericCommand is deleted.
*/
virtual void OnGenericCommandRemove(Jupiter::GenericCommand &command);
/** /**
* @brief Constructor for the Plugin class. * @brief Constructor for the Plugin class.
*/ */

BIN
Release/Jupiter.lib

Binary file not shown.
Loading…
Cancel
Save