1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-19 03:57:14 +01:00

Update the IRC module to work with the modified API.

Document code properly and also various code fixes.
This commit is contained in:
Sandu Liviu Catalin 2016-06-03 21:27:53 +03:00
parent 0c92601362
commit 40ab83743c
7 changed files with 234 additions and 594 deletions

View File

@ -435,9 +435,12 @@
<Unit filename="../modules/irc/Common.cpp" /> <Unit filename="../modules/irc/Common.cpp" />
<Unit filename="../modules/irc/Common.hpp" /> <Unit filename="../modules/irc/Common.hpp" />
<Unit filename="../modules/irc/Module.cpp" /> <Unit filename="../modules/irc/Module.cpp" />
<Unit filename="../modules/irc/Module.hpp" />
<Unit filename="../modules/irc/Session.cpp" /> <Unit filename="../modules/irc/Session.cpp" />
<Unit filename="../modules/irc/Session.hpp" /> <Unit filename="../modules/irc/Session.hpp" />
<Unit filename="../shared/Base/Buffer.cpp" />
<Unit filename="../shared/Base/Buffer.hpp" />
<Unit filename="../shared/Base/Utility.cpp" />
<Unit filename="../shared/Base/Utility.hpp" />
<Unit filename="../shared/SqMod.cpp" /> <Unit filename="../shared/SqMod.cpp" />
<Extensions> <Extensions>
<code_completion /> <code_completion />

View File

@ -1,228 +1,121 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "Common.hpp" #include "Common.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <cstring> #include <cstdlib>
#include <cstdarg>
// ------------------------------------------------------------------------------------------------
#include <sqrat.h>
// ------------------------------------------------------------------------------------------------
#include <libircclient.h>
#include <libirc_rfcnumeric.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static SQChar g_Buffer[4096]; // Common buffer to reduce memory allocations. SQInteger GetNick(HSQUIRRELVM vm)
// ------------------------------------------------------------------------------------------------
SStr GetTempBuff()
{ {
return g_Buffer; // Attempt to retrieve the value from the stack as a string
} StackStrF val(vm, 2);
// Have we failed to retrieve the string?
// ------------------------------------------------------------------------------------------------ if (SQ_FAILED(val.mRes))
Uint32 GetTempBuffSize()
{
return sizeof(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
void SqThrowF(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{ {
std::strcpy(g_Buffer, "Unknown error has occurred"); return val.mRes; // Propagate the error!
} }
// Release the argument list
va_end(args);
// Throw the exception with the resulted message
throw Sqrat::Exception(g_Buffer);
}
// ------------------------------------------------------------------------------------------------
CSStr FmtStr(CSStr str, ...)
{
// Initialize the argument list
va_list args;
va_start (args, str);
// Write the requested contents
if (std::vsnprintf(g_Buffer, sizeof(g_Buffer), str, args) < 0)
{
g_Buffer[0] = 0; // Make sure the string is terminated
}
// Release the argument list
va_end(args);
// Return the data from the buffer
return g_Buffer;
}
// ------------------------------------------------------------------------------------------------
StackGuard::StackGuard()
: m_VM(_SqVM), m_Top(sq_gettop(m_VM))
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
StackGuard::StackGuard(HSQUIRRELVM vm)
: m_VM(vm), m_Top(sq_gettop(vm))
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
StackGuard::~StackGuard()
{
sq_pop(m_VM, sq_gettop(m_VM) - m_Top);
}
// --------------------------------------------------------------------------------------------
StackStrF::StackStrF(HSQUIRRELVM vm, SQInteger idx, bool fmt)
: mPtr(nullptr)
, mLen(-1)
, mRes(SQ_OK)
, mObj()
, mVM(vm)
{
const Int32 top = sq_gettop(vm);
// Reset the converted value object
sq_resetobject(&mObj);
// Was the string or value specified?
if (top <= (idx - 1))
{
mRes = sq_throwerror(vm, "Missing string or value");
}
// Do we have enough values to call the format function and are we allowed to?
else if (top > idx && fmt)
{
// Pointer to the generated string
SStr str = nullptr;
// Attempt to generate the specified string format
mRes = sqstd_format(vm, idx, &mLen, &str);
// Did the format succeeded but ended up with a null string pointer?
if (SQ_SUCCEEDED(mRes) && !str)
{
mRes = sq_throwerror(vm, "Unable to generate the string");
}
else
{
mPtr = const_cast< CSStr >(str);
}
}
// Is the value on the stack an actual string?
else if (sq_gettype(vm, idx) == OT_STRING)
{
// Obtain a reference to the string object
mRes = sq_getstackobj(vm, idx, &mObj);
// Could we retrieve the object from the stack?
if (SQ_SUCCEEDED(mRes))
{
// Keep a strong reference to the object
sq_addref(vm, &mObj);
// Attempt to retrieve the string value from the stack
mRes = sq_getstring(vm, idx, &mPtr);
}
// Did the retrieval succeeded but ended up with a null string pointer?
if (SQ_SUCCEEDED(mRes) && !mPtr)
{
mRes = sq_throwerror(vm, "Unable to retrieve the string");
}
}
// We have to try and convert it to string
else
{
// Attempt to convert the value from the stack to a string
mRes = sq_tostring(vm, idx);
// Could we convert the specified value to string?
if (SQ_SUCCEEDED(mRes))
{
// Obtain a reference to the resulted object
mRes = sq_getstackobj(vm, -1, &mObj);
// Could we retrieve the object from the stack?
if (SQ_SUCCEEDED(mRes))
{
// Keep a strong reference to the object
sq_addref(vm, &mObj);
// Attempt to obtain the string pointer
mRes = sq_getstring(vm, -1, &mPtr);
}
}
// Pop a value from the stack regardless of the result
sq_pop(vm, 1);
// Did the retrieval succeeded but ended up with a null string pointer?
if (SQ_SUCCEEDED(mRes) && !mPtr)
{
mRes = sq_throwerror(vm, "Unable to retrieve the value");
}
}
}
// ------------------------------------------------------------------------------------------------
StackStrF::~StackStrF()
{
if (mVM && !sq_isnull(mObj))
{
sq_release(mVM, &mObj);
}
}
// ------------------------------------------------------------------------------------------------
Object & NullObject()
{
static Object o;
o.Release();
return o;
}
// ------------------------------------------------------------------------------------------------
Table & NullTable()
{
static Table t;
t.Release();
return t;
}
// ------------------------------------------------------------------------------------------------
Array & NullArray()
{
static Array a;
a.Release();
return a;
}
// ------------------------------------------------------------------------------------------------
Function & NullFunction()
{
static Function f;
f.Release();
return f;
}
// ------------------------------------------------------------------------------------------------
CSStr GetNick(CSStr origin)
{
// Attempt to retrieve the nickname // Attempt to retrieve the nickname
irc_target_get_nick(origin, g_Buffer, sizeof(g_Buffer)); irc_target_get_nick(val.mPtr, GetTempBuff(), GetTempBuffSize());
// Return the nickname that could be retrieved // Push the resulted value on the stack
return g_Buffer; sq_pushstring(vm, GetTempBuff(), -1);
// Specify that this function returned a value
return 1;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
CSStr GetHost(CSStr target) SQInteger GetHost(HSQUIRRELVM vm)
{ {
// Attempt to retrieve the value from the stack as a string
StackStrF val(vm, 2);
// Have we failed to retrieve the string?
if (SQ_FAILED(val.mRes))
{
return val.mRes; // Propagate the error!
}
// Attempt to retrieve the host // Attempt to retrieve the host
irc_target_get_host(target, g_Buffer, sizeof(g_Buffer)); irc_target_get_host(val.mPtr, GetTempBuff(), GetTempBuffSize());
// Return the host that could be retrieved // Push the resulted value on the stack
return g_Buffer; sq_pushstring(vm, GetTempBuff(), -1);
// Specify that this function returned a value
return 1;
}
// ------------------------------------------------------------------------------------------------
SQInteger StripColorFromMIRC(HSQUIRRELVM vm)
{
// Attempt to retrieve the value from the stack as a string
StackStrF val(vm, 2);
// Have we failed to retrieve the string?
if (SQ_FAILED(val.mRes))
{
return val.mRes; // Propagate the error!
}
// Attempt to strip the colors
CStr str = irc_color_strip_from_mirc(val.mPtr);
// Could the IRC library allocate memory?
if (!str)
{
return sq_throwerror(vm, _SC("Unable to allocate memory"));
}
// Push the resulted value on the stack
sq_pushstring(vm, str, -1);
// Free the memory allocated by the IRC library
std::free(str);
// Specify that this function returned a value
return 1;
}
// ------------------------------------------------------------------------------------------------
SQInteger ConvertColorFromMIRC(HSQUIRRELVM vm)
{
// Attempt to retrieve the value from the stack as a string
StackStrF val(vm, 2);
// Have we failed to retrieve the string?
if (SQ_FAILED(val.mRes))
{
return val.mRes; // Propagate the error!
}
// Attempt to convert the colors
CStr str = irc_color_convert_from_mirc(val.mPtr);
// Could the IRC library allocate memory?
if (!str)
{
return sq_throwerror(vm, _SC("Unable to allocate memory"));
}
// Push the resulted value on the stack
sq_pushstring(vm, str, -1);
// Free the memory allocated by the IRC library
std::free(str);
// Specify that this function returned a value
return 1;
}
// ------------------------------------------------------------------------------------------------
SQInteger ConvertColorToMIRC(HSQUIRRELVM vm)
{
// Attempt to retrieve the value from the stack as a string
StackStrF val(vm, 2);
// Have we failed to retrieve the string?
if (SQ_FAILED(val.mRes))
{
return val.mRes; // Propagate the error!
}
// Attempt to convert the colors
CStr str = irc_color_convert_to_mirc(val.mPtr);
// Could the IRC library allocate memory?
if (!str)
{
return sq_throwerror(vm, _SC("Unable to allocate memory"));
}
// Push the resulted value on the stack
sq_pushstring(vm, str, -1);
// Free the memory allocated by the IRC library
std::free(str);
// Specify that this function returned a value
return 1;
} }
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -2,10 +2,11 @@
#define _SQIRC_COMMON_HPP_ #define _SQIRC_COMMON_HPP_
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "ModBase.hpp" #include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <squirrel.h> #include <libircclient.h>
#include <libirc_rfcnumeric.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -23,123 +24,6 @@ namespace SqMod {
#define SQIRC_VERSION_MINOR 0 #define SQIRC_VERSION_MINOR 0
#define SQIRC_VERSION_PATCH 1 #define SQIRC_VERSION_PATCH 1
/* ------------------------------------------------------------------------------------------------
* Forward declarations.
*/
class Session;
/* ------------------------------------------------------------------------------------------------
* Retrieve the temporary buffer.
*/
SStr GetTempBuff();
/* ------------------------------------------------------------------------------------------------
* Retrieve the size of the temporary buffer.
*/
Uint32 GetTempBuffSize();
/* ------------------------------------------------------------------------------------------------
* Throw a formatted exception.
*/
void SqThrowF(CSStr str, ...);
/* ------------------------------------------------------------------------------------------------
* Generate a formatted string.
*/
CSStr FmtStr(CSStr str, ...);
/* ------------------------------------------------------------------------------------------------
* Implements RAII to restore the VM stack to it's initial size on function exit.
*/
struct StackGuard
{
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
StackGuard();
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
StackGuard(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~StackGuard();
private:
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
StackGuard(const StackGuard &);
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
StackGuard(StackGuard &&);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
StackGuard & operator = (const StackGuard &);
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
StackGuard & operator = (StackGuard &&);
private:
// --------------------------------------------------------------------------------------------
HSQUIRRELVM m_VM; // The VM where the stack should be restored.
Int32 m_Top; // The top of the stack when this instance was created.
};
/* ------------------------------------------------------------------------------------------------
* Helper structure for retrieving a value from the stack as a string or a formatted string.
*/
struct StackStrF
{
// --------------------------------------------------------------------------------------------
CSStr mPtr; // Pointer to the C string that was retrieved.
SQInteger mLen; // The string length if it could be retrieved.
SQRESULT mRes; // The result of the retrieval attempts.
HSQOBJECT mObj; // Strong reference to the string object.
HSQUIRRELVM mVM; // The associated virtual machine.
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
StackStrF(HSQUIRRELVM vm, SQInteger idx, bool fmt = true);
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackStrF(const StackStrF & o) = delete;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackStrF(StackStrF && o) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~StackStrF();
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackStrF & operator = (const StackStrF & o) = delete;
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
StackStrF & operator = (StackStrF && o) = delete;
};
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Types of events that the session emits. * Types of events that the session emits.
*/ */
@ -170,34 +54,34 @@ enum SessionEvent
}; };
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script object. * Forward declarations.
*/ */
Object & NullObject(); class Session;
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null/empty script table.
*/
Table & NullTable();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null/empty script array.
*/
Array & NullArray();
/* ------------------------------------------------------------------------------------------------
* Retrieve a reference to a null script function.
*/
Function & NullFunction();
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Extract the name from the specified origin. * Extract the name from the specified origin.
*/ */
CSStr GetNick(CSStr origin); SQInteger GetNick(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Extract the host from the specified origin. * Extract the host from the specified origin.
*/ */
CSStr GetHost(CSStr target); SQInteger GetHost(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* Returns a new plain text message with stripped mIRC color codes.
*/
SQInteger StripColorFromMIRC(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* Returns a new message with converted mIRC color codes and format options.
*/
SQInteger ConvertColorFromMIRC(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* Returns a new message with converted color codes and format options.
*/
SQInteger ConvertColorToMIRC(HSQUIRRELVM vm);
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -1,36 +1,21 @@
// -------------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------------------
#include "Module.hpp"
#include "Common.hpp" #include "Common.hpp"
#include "Session.hpp" #include "Session.hpp"
// -------------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------------------
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstdarg>
// -------------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------------------
#include <sqrat.h> #ifdef SQMOD_OS_WINDOWS
// --------------------------------------------------------------------------------------------
#if defined(WIN32) || defined(_WIN32)
#include <Windows.h>
#include <Winsock2.h> #include <Winsock2.h>
#endif #endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
// --------------------------------------------------------------------------------------------
PluginFuncs* _Func = nullptr;
PluginCallbacks* _Clbk = nullptr;
PluginInfo* _Info = nullptr;
// --------------------------------------------------------------------------------------------
HSQAPI _SqAPI = nullptr;
HSQEXPORTS _SqMod = nullptr;
HSQUIRRELVM _SqVM = nullptr;
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Bind speciffic functions to certain server events. * Bind specific functions to certain server events.
*/ */
void BindCallbacks(); void BindCallbacks();
@ -39,22 +24,22 @@ void BindCallbacks();
*/ */
void UnbindCallbacks(); void UnbindCallbacks();
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* Register the module API under the specified virtual machine. * Register the module API under the specified virtual machine.
*/ */
void RegisterAPI(HSQUIRRELVM vm); void RegisterAPI(HSQUIRRELVM vm);
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* Initialize the plugin by obtaining the API provided by the host plugin. * Initialize the plug-in by obtaining the API provided by the host plug-in.
*/ */
void OnSquirrelInitialize() void OnSquirrelInitialize()
{ {
// Attempt to import the plugin API exported by the host plugin // Attempt to import the plug-in API exported by the host plug-in
_SqMod = sq_api_import(_Func); _SqMod = sq_api_import(_Func);
// Did we failed to obtain the plugin exports? // Did we failed to obtain the plug-in exports?
if(!_SqMod) if (!_SqMod)
{ {
OutputError("Failed to attach [%s] on host plugin.", SQIRC_NAME); OutputError("Failed to attach [%s] on host plug-in.", SQIRC_NAME);
} }
else else
{ {
@ -65,12 +50,12 @@ void OnSquirrelInitialize()
} }
} }
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* Load the module on the virtual machine provided by the host module. * Load the module on the virtual machine provided by the host module.
*/ */
void OnSquirrelLoad() void OnSquirrelLoad()
{ {
// Make sure that we have a valid plugin API // Make sure that we have a valid plug-in API
if (!_SqMod) if (!_SqMod)
{ {
return; // Unable to proceed! return; // Unable to proceed!
@ -90,7 +75,7 @@ void OnSquirrelLoad()
OutputMessage("Registered: %s", SQIRC_NAME); OutputMessage("Registered: %s", SQIRC_NAME);
} }
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* The virtual machine is about to be terminated and script resources should be released. * The virtual machine is about to be terminated and script resources should be released.
*/ */
void OnSquirrelTerminate() void OnSquirrelTerminate()
@ -107,13 +92,13 @@ void OnSquirrelTerminate()
DefaultVM::Set(nullptr); DefaultVM::Set(nullptr);
} }
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* Validate the module API to make sure we don't run into issues. * Validate the module API to make sure we don't run into issues.
*/ */
bool CheckAPIVer(CCStr ver) bool CheckAPIVer(CCStr ver)
{ {
// Obtain the numeric representation of the API version // Obtain the numeric representation of the API version
long vernum = std::strtol(ver, nullptr, 10); const LongI vernum = std::strtol(ver, nullptr, 10);
// Check against version mismatch // Check against version mismatch
if (vernum == SQMOD_API_VER) if (vernum == SQMOD_API_VER)
{ {
@ -126,8 +111,8 @@ bool CheckAPIVer(CCStr ver)
return false; return false;
} }
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* React to command sent by other plugins. * React to command sent by other plug-ins.
*/ */
static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message) static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
{ {
@ -150,8 +135,8 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
return 1; return 1;
} }
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* The server was initialized and this plugin was loaded successfully. * The server was initialized and this plug-in was loaded successfully.
*/ */
static uint8_t OnServerInitialise() static uint8_t OnServerInitialise()
{ {
@ -164,8 +149,8 @@ static void OnServerShutdown(void)
UnbindCallbacks(); UnbindCallbacks();
} }
/* -------------------------------------------------------------------------------------------- /* ------------------------------------------------------------------------------------------------
* Update the plugin on each frame. * Update the plug-in on each frame.
*/ */
static void OnServerFrame(float /*delta*/) static void OnServerFrame(float /*delta*/)
{ {
@ -191,7 +176,7 @@ void UnbindCallbacks()
_Clbk->OnPluginCommand = nullptr; _Clbk->OnPluginCommand = nullptr;
} }
// -------------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------------------
void RegisterAPI(HSQUIRRELVM vm) void RegisterAPI(HSQUIRRELVM vm)
{ {
Table ircns(vm); Table ircns(vm);
@ -199,7 +184,7 @@ void RegisterAPI(HSQUIRRELVM vm)
ircns.Bind(_SC("Session"), Class< Session, NoCopy< Session > >(vm, _SC("SqIrcSession")) ircns.Bind(_SC("Session"), Class< Session, NoCopy< Session > >(vm, _SC("SqIrcSession"))
// Constructors // Constructors
.Ctor() .Ctor()
// Core Metamethods // Core Meta-methods
.Func(_SC("_cmp"), &Session::Cmp) .Func(_SC("_cmp"), &Session::Cmp)
.SquirrelFunc(_SC("_typename"), &Session::Typename) .SquirrelFunc(_SC("_typename"), &Session::Typename)
.Func(_SC("_tostring"), &Session::ToString) .Func(_SC("_tostring"), &Session::ToString)
@ -276,9 +261,12 @@ void RegisterAPI(HSQUIRRELVM vm)
.SquirrelFunc(_SC("CmdNoticeF"), &Session::CmdNoticeF) .SquirrelFunc(_SC("CmdNoticeF"), &Session::CmdNoticeF)
); );
ircns.Func(_SC("GetNick"), &GetNick);
ircns.Func(_SC("GetHost"), &GetHost);
ircns.Func(_SC("GetErrStr"), &irc_strerror); ircns.Func(_SC("GetErrStr"), &irc_strerror);
ircns.SquirrelFunc(_SC("GetNick"), &GetNick);
ircns.SquirrelFunc(_SC("GetHost"), &GetHost);
ircns.SquirrelFunc(_SC("StripColorFromMIRC"), &StripColorFromMIRC);
ircns.SquirrelFunc(_SC("ConvertColorFromMIRC"), &ConvertColorFromMIRC);
ircns.SquirrelFunc(_SC("ConvertColorToMIRC"), &ConvertColorToMIRC);
RootTable(vm).Bind(_SC("SqIRC"), ircns); RootTable(vm).Bind(_SC("SqIRC"), ircns);
@ -477,144 +465,59 @@ void RegisterAPI(HSQUIRRELVM vm)
); );
} }
// --------------------------------------------------------------------------------------------
void OutputMessageImpl(const char * msg, va_list args)
{
#if defined(WIN32) || defined(_WIN32)
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("%c[0;32m[SQMOD]%c[0m", 27, 27);
std::vprintf(msg, args);
std::puts("");
#endif
}
// --------------------------------------------------------------------------------------------
void OutputErrorImpl(const char * msg, va_list args)
{
#if defined(WIN32) || defined(_WIN32)
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csb_before;
GetConsoleScreenBufferInfo( hstdout, &csb_before);
SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_INTENSITY);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("%c[0;91m[SQMOD]%c[0m", 27, 27);
std::vprintf(msg, args);
std::puts("");
#endif
}
// --------------------------------------------------------------------------------------------
void OutputDebug(const char * msg, ...)
{
#ifdef _DEBUG
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
#else
SQMOD_UNUSED_VAR(msg);
#endif
}
// --------------------------------------------------------------------------------------------
void OutputMessage(const char * msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputMessageImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
// --------------------------------------------------------------------------------------------
void OutputError(const char * msg, ...)
{
// Initialize the arguments list
va_list args;
va_start(args, msg);
// Call the output function
OutputErrorImpl(msg, args);
// Finalize the arguments list
va_end(args);
}
} // Namespace:: SqMod } // Namespace:: SqMod
// -------------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------------------
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs* functions, PluginCallbacks* callbacks, PluginInfo* info) SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCallbacks * callbacks, PluginInfo * info)
{ {
using namespace SqMod; using namespace SqMod;
// Output plugin header // Output plug-in header
puts(""); puts("");
OutputMessage("--------------------------------------------------------------------"); OutputMessage("--------------------------------------------------------------------");
OutputMessage("Plugin: %s", SQIRC_NAME); OutputMessage("Plug-in: %s", SQIRC_NAME);
OutputMessage("Author: %s", SQIRC_AUTHOR); OutputMessage("Author: %s", SQIRC_AUTHOR);
OutputMessage("Legal: %s", SQIRC_COPYRIGHT); OutputMessage("Legal: %s", SQIRC_COPYRIGHT);
OutputMessage("--------------------------------------------------------------------"); OutputMessage("--------------------------------------------------------------------");
puts(""); puts("");
// Attempt to find the host plugin ID // Attempt to find the host plug-in ID
int host_plugin_id = functions->FindPlugin((char *)(SQMOD_HOST_NAME)); const int host_plugin_id = functions->FindPlugin(SQMOD_HOST_NAME);
// See if our plugin was loaded after the host plugin // See if our plug-in was loaded after the host plug-in
if (host_plugin_id < 0) if (host_plugin_id < 0)
{ {
OutputError("%s could find the host plugin", SQIRC_NAME); OutputError("%s could find the host plug-in", SQIRC_NAME);
// Don't load! // Don't load!
return SQMOD_FAILURE; return SQMOD_FAILURE;
} }
// Should never reach this point but just in case // Should never reach this point but just in case
else if (static_cast< Uint32 >(host_plugin_id) > info->pluginId) else if (static_cast< Uint32 >(host_plugin_id) > info->pluginId)
{ {
OutputError("%s loaded after the host plugin", SQIRC_NAME); OutputError("%s loaded after the host plug-in", SQIRC_NAME);
// Don't load! // Don't load!
return SQMOD_FAILURE; return SQMOD_FAILURE;
} }
#if defined(_WIN32) #ifdef SQMOD_OS_WINDOWS
WSADATA wsa_data; WSADATA wsa_data;
// Initialize the sockets on windows // Initialize the sockets on windows
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
{ {
OutputError("Unable to start because the windows sockets could not be initialized"); OutputError("Unable to initialize the windows sockets");
// Don't load!
return SQMOD_FAILURE; return SQMOD_FAILURE;
} }
#endif // _WIN32 #endif // SQMOD_OS_WINDOWS
// Store server proxies // Store server proxies
_Func = functions; _Func = functions;
_Clbk = callbacks; _Clbk = callbacks;
_Info = info; _Info = info;
// Assign plugin version // Assign plug-in version
_Info->pluginVersion = SQIRC_VERSION; _Info->pluginVersion = SQIRC_VERSION;
_Info->apiMajorVersion = PLUGIN_API_MAJOR; _Info->apiMajorVersion = PLUGIN_API_MAJOR;
_Info->apiMinorVersion = PLUGIN_API_MINOR; _Info->apiMinorVersion = PLUGIN_API_MINOR;
// Assign the plugin name // Assign the plug-in name
std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQIRC_HOST_NAME); std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQIRC_HOST_NAME);
// Bind callbacks // Bind callbacks
BindCallbacks(); BindCallbacks();
// Notify that the plugin was successfully loaded // Notify that the plug-in was successfully loaded
OutputMessage("Successfully loaded %s", SQIRC_NAME); OutputMessage("Successfully loaded %s", SQIRC_NAME);
// Dummy spacing // Dummy spacing
puts(""); puts("");

View File

@ -1,41 +0,0 @@
#ifndef _SQIRC_MODULE_HPP_
#define _SQIRC_MODULE_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqMod.h"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the server.
*/
extern PluginFuncs* _Func;
extern PluginCallbacks* _Clbk;
extern PluginInfo* _Info;
/* ------------------------------------------------------------------------------------------------
* Proxies to comunicate with the Squirrel plugin.
*/
extern HSQAPI _SqAPI;
extern HSQEXPORTS _SqMod;
extern HSQUIRRELVM _SqVM;
/* ------------------------------------------------------------------------------------------------
* Output a message only if the _DEBUG was defined.
*/
void OutputDebug(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted user message to the console.
*/
void OutputMessage(const char * msg, ...);
/* ------------------------------------------------------------------------------------------------
* Output a formatted error message to the console.
*/
void OutputError(const char * msg, ...);
} // Namespace:: SqMod
#endif // _SQIRC_MODULE_HPP_

View File

@ -1,11 +1,8 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "Session.hpp" #include "Session.hpp"
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <cstring> #include <cstring>
// ------------------------------------------------------------------------------------------------
#include <algorithm> #include <algorithm>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -7,13 +7,6 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <vector> #include <vector>
// ------------------------------------------------------------------------------------------------
#include <libircclient.h>
#include <libirc_rfcnumeric.h>
// ------------------------------------------------------------------------------------------------
#include <sqrat.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -111,47 +104,39 @@ protected:
private: private:
/* -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
* Copy constructor. (disabled) irc_session_t* m_Session; // The managed IRC session structure.
*/
Session(const Session &);
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
Session & operator = (const Session &);
private:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
irc_session_t* m_Session; /* The managed IRC session structure. */ String m_Server; // Server address.
String m_Passwd; // Account password.
String m_Nick; // Nickname.
String m_User; // User name.
String m_Name; // Real name.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
String m_Server; /* Server address. */ Int32 m_Port; // Server port.
String m_Passwd; /* Account password. */
String m_Nick; /* Nickname. */
String m_User; /* User name. */
String m_Name; /* Real name. */
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
Int32 m_Port; /* Server port. */ Int32 m_LastCode; // Last error code that could not be returned directly.
Uint32 m_PoolTime; // How much time to wait when pooling for session events.
Uint32 m_Tries; // How many times to retry connection.
Uint32 m_Wait; // How many milliseconds to wait between each try.
Uint32 m_LeftTries; // How many tries are left.
Int64 m_NextTry; // When should the session attempt to connect again.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
Int32 m_LastCode; /* Last error code that could not be returned directly. */ Int64 m_SessionTime; // The time when the session was created.
Uint32 m_PoolTime; /* How much time to wait when pooling for session events. */
Uint32 m_Tries; /* How many times to retry connection. */
Uint32 m_Wait; /* How many milliseconds to wait between each try. */
Uint32 m_LeftTries; /* How many tries are left. */
Int64 m_NextTry; /* When should the session attempt to connect again. */
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
Int64 m_SessionTime; /* The time when the session was created. */ bool m_Reconnect; // Whether the session should try to reconnect.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
bool m_Reconnect; /* Whether the session should try to reconnect. */ bool m_IPv6; // Whether the session was connected to an ipv6 address.
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
bool m_IPv6; /* Whether the session was connected to an ipv6 address. */ String m_Tag; // User tag.
Object m_Data; // User data.
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Script callbacks. * Script callbacks.
@ -178,10 +163,6 @@ private:
Function m_OnDccChatReq; Function m_OnDccChatReq;
Function m_OnDccSendReq; Function m_OnDccSendReq;
// --------------------------------------------------------------------------------------------
String m_Tag; /* User tag. */
Object m_Data; /* User data. */
public: public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -189,11 +170,31 @@ public:
*/ */
Session(); Session();
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
Session(const Session & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
Session(Session && o) = delete;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Destructor. * Destructor.
*/ */
~Session(); ~Session();
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
Session & operator = (const Session & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
Session & operator = (Session && o) = delete;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type. * Used by the script engine to compare two instances of this type.
*/ */
@ -953,107 +954,107 @@ protected:
CCStr addr, CCStr filename, Ulong size, irc_dcc_t dccid); CCStr addr, CCStr filename, Ulong size, irc_dcc_t dccid);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnConnect event to the script callback.
*/ */
static void OnConnect(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnConnect(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnNick event to the script callback.
*/ */
static void OnNick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnNick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnQuit event to the script callback.
*/ */
static void OnQuit(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnQuit(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnJoin event to the script callback.
*/ */
static void OnJoin(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnJoin(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnPart event to the script callback.
*/ */
static void OnPart(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnPart(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnMode event to the script callback.
*/ */
static void OnMode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnMode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnUmode event to the script callback.
*/ */
static void OnUmode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnUmode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnTopic event to the script callback.
*/ */
static void OnTopic(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnTopic(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnKick event to the script callback.
*/ */
static void OnKick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnKick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnChannel event to the script callback.
*/ */
static void OnChannel(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnChannel(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnPrivMsg event to the script callback.
*/ */
static void OnPrivMsg(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnPrivMsg(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnNotice event to the script callback.
*/ */
static void OnNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnChannelNotice event to the script callback.
*/ */
static void OnChannelNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnChannelNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnInvite event to the script callback.
*/ */
static void OnInvite(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnInvite(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnCtcpReq event to the script callback.
*/ */
static void OnCtcpReq(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnCtcpReq(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnCtcpRep event to the script callback.
*/ */
static void OnCtcpRep(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnCtcpRep(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnCtcpAction event to the script callback.
*/ */
static void OnCtcpAction(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnCtcpAction(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnUnknown event to the script callback.
*/ */
static void OnUnknown(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count); static void OnUnknown(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnNumeric event to the script callback.
*/ */
static void OnNumeric(irc_session_t * session, Uint32 event, CCStr origin, CCStr * params, Uint32 count); static void OnNumeric(irc_session_t * session, Uint32 event, CCStr origin, CCStr * params, Uint32 count);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnDccChatReq event to the script callback.
*/ */
static void OnDccChatReq(irc_session_t * session, CCStr nick, CCStr addr, irc_dcc_t dccid); static void OnDccChatReq(irc_session_t * session, CCStr nick, CCStr addr, irc_dcc_t dccid);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* ... * Forward the OnDccSendReq event to the script callback.
*/ */
static void OnDccSendReq(irc_session_t * session, CCStr nick, CCStr addr, CCStr filename, Ulong size, irc_dcc_t dccid); static void OnDccSendReq(irc_session_t * session, CCStr nick, CCStr addr, CCStr filename, Ulong size, irc_dcc_t dccid);
}; };