1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-09-15 08:47:17 +02:00

Fixed excpetion throwing in IRC document to that generated corrupted messages because snprintf was used instead of vsnprintf.

Revised most of the IRC plugin and cleaned code.
This commit is contained in:
Sandu Liviu Catalin
2016-04-02 12:49:32 +03:00
parent 522f6cfd01
commit 27fb281805
5 changed files with 581 additions and 224 deletions

View File

@@ -3,8 +3,7 @@
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include <math.h>
#include <limits.h>
#include <cstring>
// ------------------------------------------------------------------------------------------------
#include <algorithm>
@@ -19,7 +18,7 @@ irc_callbacks_t Session::s_Callbacks;
bool Session::s_Initialized = false;
// ------------------------------------------------------------------------------------------------
Session* Session::s_Session = NULL;
Session* Session::s_Session = nullptr;
// ------------------------------------------------------------------------------------------------
Session::Sessions Session::s_Sessions;
@@ -37,12 +36,16 @@ void Session::Process()
{
// Do we only have one IRC session?
if (s_Session)
{
s_Session->Update();
}
// Do we have multiple sessions?
else if (!s_Sessions.empty())
{
for (Sessions::iterator itr = s_Sessions.begin(); itr != s_Sessions.end(); ++itr)
{
(*itr)->Update();
}
}
}
@@ -51,12 +54,16 @@ void Session::Terminate()
{
// Do we only have one IRC session?
if (s_Session)
{
s_Session->Destroy(); /* This should do the job. */
}
// Do we have multiple sessions?
else if (!s_Sessions.empty())
{
for (Sessions::iterator itr = s_Sessions.begin(); itr != s_Sessions.end(); ++itr)
{
(*itr)->Destroy();
}
}
}
@@ -100,7 +107,9 @@ void Session::Update()
{
// Make sure we even have a session
if (!m_Session)
{
return;
}
// Make sure that the IRC session is connected
else if (!irc_is_connected(m_Session))
{
@@ -115,19 +124,19 @@ void Session::Update()
if (m_IPv6)
{
m_LastCode = irc_connect6(m_Session, m_Server.c_str(), m_Port,
m_Passwd.empty() ? NULL : m_Passwd.c_str(),
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
m_Nick.c_str(),
m_User.empty() ? NULL : m_User.c_str(),
m_Name.empty() ? NULL : m_Name.c_str()
m_User.empty() ? nullptr : m_User.c_str(),
m_Name.empty() ? nullptr : m_Name.c_str()
);
}
else
{
m_LastCode = irc_connect(m_Session, m_Server.c_str(), m_Port,
m_Passwd.empty() ? NULL : m_Passwd.c_str(),
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
m_Nick.c_str(),
m_User.empty() ? NULL : m_User.c_str(),
m_Name.empty() ? NULL : m_Name.c_str()
m_User.empty() ? nullptr : m_User.c_str(),
m_Name.empty() ? nullptr : m_Name.c_str()
);
}
}
@@ -142,8 +151,8 @@ void Session::Update()
tv.tv_usec = (m_PoolTime * 1000L);
tv.tv_sec = 0;
// Initialize the sets
memset(&in_set, 0, sizeof(fd_set));
memset(&out_set, 0, sizeof(fd_set));
std::memset(&in_set, 0, sizeof(fd_set));
std::memset(&out_set, 0, sizeof(fd_set));
// Add the IRC session descriptors
irc_add_select_descriptors(m_Session, &in_set, &out_set, &maxfd);
// Call select()
@@ -197,11 +206,11 @@ void Session::Destroy()
// Disconnect the session
Disconnect();
// Break the association with this instance (paranoia)
irc_set_ctx(m_Session, NULL);
irc_set_ctx(m_Session, nullptr);
// Destroy the IRC session structure
irc_destroy_session(m_Session);
// Explicitly make sure no further calls can be made to this session (again... paranoia)
m_Session = NULL;
m_Session = nullptr;
// Release resources
Release();
}
@@ -211,7 +220,9 @@ void Session::Validate() const
{
// Do we have a valid session handle?
if (!m_Session)
{
STHROWF("Invalid IRC session");
}
}
// ------------------------------------------------------------------------------------------------
@@ -219,10 +230,14 @@ void Session::ValidateConnection() const
{
// Do we have a valid session handle?
if (!m_Session)
{
STHROWF("Invalid IRC session");
}
// Is the session connected?
else if (!irc_is_connected(m_Session))
{
STHROWF("Session is not connected");
}
}
// ------------------------------------------------------------------------------------------------
@@ -230,7 +245,9 @@ void Session::IsNotConnected() const
{
// Do we have a session that is not connected or trying to connect?
if (m_Session && (irc_is_connected(m_Session) || m_Reconnect))
{
STHROWF("Already connected or trying connect to IRC server");
}
}
// ------------------------------------------------------------------------------------------------
@@ -238,7 +255,9 @@ bool Session::ValidateEventSession(Session * ptr)
{
// Is the session instance valid?
if (ptr)
{
return true;
}
// We can't throw an error here so we simply log it
_SqMod->LogErr("Cannot forward IRC event without a session container");
// Invalid session instance
@@ -267,7 +286,7 @@ Session::Session()
if (!m_Session)
{
// Explicitly make sure no further calls can be made to this session
m_Session = NULL;
m_Session = nullptr;
// Now it's safe to throw the error
STHROWF("Unable to create an IRC session");
}
@@ -277,17 +296,21 @@ Session::Session()
irc_set_ctx(m_Session, this);
// Is this the only session instance?
if (!s_Session && s_Sessions.empty())
{
s_Session = this;
}
// Is this the second session instance?
else if (s_Sessions.empty())
{
s_Sessions.push_back(s_Session);
s_Session = NULL;
s_Session = nullptr;
s_Sessions.push_back(this);
}
// This is part of multiple session instances
else
{
s_Sessions.push_back(this);
}
}
}
@@ -299,10 +322,14 @@ Session::~Session()
Sessions::iterator itr = std::find(s_Sessions.begin(), s_Sessions.end(), this);
// Are we in the pool?
if (itr != s_Sessions.end())
{
s_Sessions.erase(itr); /* Remove our self from the pool. */
}
// Is there a single session and that's us?
if (s_Session == this)
s_Session = NULL;
{
s_Session = nullptr;
}
}
// ------------------------------------------------------------------------------------------------
@@ -312,13 +339,19 @@ void Session::SetNick(CSStr nick)
Validate();
// Validate the specified nick name
if (!nick || strlen(nick) <= 0)
{
STHROWF("Invalid IRC nickname");
}
// Do we have to issue a nickname command?
else if (Connected())
{
irc_cmd_nick(m_Session, nick);
}
// Simply save the specified nickname
else
{
m_Nick.assign(nick);
}
}
// ------------------------------------------------------------------------------------------------
@@ -328,7 +361,9 @@ void Session::SetPort(Uint32 num)
IsNotConnected();
// Validate the specified port number
if (num > 0xFFFF)
{
STHROWF("Port number is out of range: %u > %u", num, 0xFFFF);
}
// Assign the specified port number
m_Port = num;
}
@@ -340,7 +375,9 @@ Int32 Session::CmdNick(CSStr nick)
ValidateConnection();
// Validate the specified nick name
if (!nick || strlen(nick) <= 0)
{
STHROWF("Invalid IRC nickname");
}
// Issue the command and return the result
return irc_cmd_nick(m_Session, nick);
}
@@ -367,7 +404,9 @@ void Session::SetNextTry(Object & tm)
Int64 microseconds = 0;
// Attempt to get the numeric value inside the specified object
if (SQ_FAILED(_SqMod->GetTimestamp(_SqVM, -1, &microseconds)))
{
STHROWF("Invalid time-stamp specified");
}
// Assign the specified timestamp value
m_NextTry = microseconds;
}
@@ -379,10 +418,14 @@ Object Session::GetSessionTime() const
const StackGuard sg(_SqVM);
// Attempt to push a time-stamp instance on the stack
if (m_SessionTime)
{
_SqMod->PushTimestamp(_SqVM, _SqMod->GetEpochTimeMicro() - m_SessionTime);
}
// This session was not connected yet
else
{
_SqMod->PushTimestamp(_SqVM, 0);
}
// Obtain the object from the stack and return it
return Var< Object >(_SqVM, -1).value;
}
@@ -396,10 +439,14 @@ Int32 Session::Connect()
IsNotConnected();
// Validate the specified server
if (!m_Server.empty())
{
STHROWF("Attempting to connect IRC without specifying a server");
}
// Validate the specified nickname
else if (!m_Nick.empty())
{
STHROWF("Attempting to connect IRC without specifying a nickname");
}
// Enable the reconnection system
m_Reconnect = true;
// Reset the number of tries
@@ -410,10 +457,10 @@ Int32 Session::Connect()
m_IPv6 = false;
// Attempt to connect the session and return the result
return irc_connect(m_Session, m_Server.c_str(), m_Port,
m_Passwd.empty() ? NULL : m_Passwd.c_str(),
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
m_Nick.c_str(),
m_User.empty() ? NULL : m_User.c_str(),
m_Name.empty() ? NULL : m_Name.c_str()
m_User.empty() ? nullptr : m_User.c_str(),
m_Name.empty() ? nullptr : m_Name.c_str()
);
}
@@ -426,13 +473,19 @@ Int32 Session::Connect(CSStr server, Uint32 port, CSStr nick, CSStr passwd, CSSt
IsNotConnected();
// Validate the specified port
if (port > 0xFFFF)
{
STHROWF("Port number is out of range: %u > %u", port, 0xFFFF);
}
// Validate the specified server
else if (!server || strlen(server) <= 0)
{
STHROWF("Attempting to connect IRC without specifying a server");
}
// Validate the specified nickname
else if (!nick || strlen(nick) <= 0)
{
STHROWF("Attempting to connect IRC without specifying a nickname");
}
// Save the specified port
m_Port = port;
// Save the specified server
@@ -455,10 +508,10 @@ Int32 Session::Connect(CSStr server, Uint32 port, CSStr nick, CSStr passwd, CSSt
m_IPv6 = false;
// Attempt to connect the session and return the result
return irc_connect(m_Session, m_Server.c_str(), m_Port,
m_Passwd.empty() ? NULL : m_Passwd.c_str(),
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
m_Nick.c_str(),
m_User.empty() ? NULL : m_User.c_str(),
m_Name.empty() ? NULL : m_Name.c_str()
m_User.empty() ? nullptr : m_User.c_str(),
m_Name.empty() ? nullptr : m_Name.c_str()
);
}
@@ -471,10 +524,14 @@ Int32 Session::Connect6()
IsNotConnected();
// Validate the specified server
if (!m_Server.empty())
{
STHROWF("Attempting to connect IRC without specifying a server");
}
// Validate the specified nickname
else if (!m_Nick.empty())
{
STHROWF("Attempting to connect IRC without specifying a nickname");
}
// Enable the reconnection system
m_Reconnect = true;
// Reset the number of tries
@@ -485,10 +542,10 @@ Int32 Session::Connect6()
m_IPv6 = true;
// Attempt to connect the session and return the result
return irc_connect6(m_Session, m_Server.c_str(), m_Port,
m_Passwd.empty() ? NULL : m_Passwd.c_str(),
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
m_Nick.c_str(),
m_User.empty() ? NULL : m_User.c_str(),
m_Name.empty() ? NULL : m_Name.c_str()
m_User.empty() ? nullptr : m_User.c_str(),
m_Name.empty() ? nullptr : m_Name.c_str()
);
}
@@ -501,13 +558,19 @@ Int32 Session::Connect6(CSStr server, Uint32 port, CSStr nick, CSStr passwd, CSS
IsNotConnected();
// Validate the specified port
if (port > 0xFFFF)
{
STHROWF("Port number is out of range: %u > %u", port, 0xFFFF);
}
// Validate the specified server
else if (!server || strlen(server) <= 0)
{
STHROWF("Attempting to connect IRC without specifying a server");
}
// Validate the specified nickname
else if (!nick || strlen(nick) <= 0)
{
STHROWF("Attempting to connect IRC without specifying a nickname");
}
// Save the specified port
m_Port = port;
// Save the specified server
@@ -530,10 +593,10 @@ Int32 Session::Connect6(CSStr server, Uint32 port, CSStr nick, CSStr passwd, CSS
m_IPv6 = true;
// Attempt to connect the session and return the result
return irc_connect6(m_Session, m_Server.c_str(), m_Port,
m_Passwd.empty() ? NULL : m_Passwd.c_str(),
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
m_Nick.c_str(),
m_User.empty() ? NULL : m_User.c_str(),
m_Name.empty() ? NULL : m_Name.c_str()
m_User.empty() ? nullptr : m_User.c_str(),
m_Name.empty() ? nullptr : m_Name.c_str()
);
}
@@ -557,10 +620,14 @@ void Session::ForwardEvent(Session * session, Function & listener, CCStr event,
{
// Is there anyone even listening to this event?
if (listener.IsNull())
return; /* No point in going forward */
{
return; // No point in going forward
}
// Make sure that the origin can't be a null pointer
else if (!origin)
{
origin = _SC("");
}
// Each event must have an array of parameters (empty or not)
Array parameters(_SqVM, count);
// Are the any parameters?
@@ -568,7 +635,9 @@ void Session::ForwardEvent(Session * session, Function & listener, CCStr event,
{
// Transform the parameters into a squirrel array
for (Uint32 i = 0; i < count; ++i)
{
parameters.SetValue(i, params[i]);
}
}
// Obtain the initial stack size
const StackGuard sg(_SqVM);
@@ -586,10 +655,14 @@ void Session::ForwardEvent(Session * session, Function & listener, Uint32 event,
{
// Is there anyone even listening to this event?
if (listener.IsNull())
return; /* No point in going forward */
{
return; // No point in going forward
}
// Make sure that the origin can't be a null pointer
else if (!origin)
{
origin = _SC("");
}
// Each event must have an array of parameters (empty or not)
Array parameters(_SqVM, count);
// Are the any parameters?
@@ -597,7 +670,9 @@ void Session::ForwardEvent(Session * session, Function & listener, Uint32 event,
{
// Transform the parameters into a squirrel array
for (unsigned int i = 0; i < count; ++i)
{
parameters.SetValue(i, params[i]);
}
}
// Obtain the initial stack size
const StackGuard sg(_SqVM);
@@ -626,7 +701,7 @@ void Session::ForwardEvent(Session * /*session*/, Function & /*listener*/, CCStr
// ------------------------------------------------------------------------------------------------
void Session::OnConnect(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
// Prevent any attempts to reconnect now
@@ -641,11 +716,13 @@ void Session::OnConnect(irc_session_t * session, CCStr event, CCStr origin, CCSt
// ------------------------------------------------------------------------------------------------
void Session::OnNick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
if (params && params[0])
{
inst->m_Nick.assign(params[0]);
}
ForwardEvent(inst, inst->m_OnNick, event, origin, params, count);
}
}
@@ -653,154 +730,192 @@ void Session::OnNick(irc_session_t * session, CCStr event, CCStr origin, CCStr *
// ------------------------------------------------------------------------------------------------
void Session::OnQuit(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnQuit, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnJoin(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnJoin, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnPart(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnPart, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnMode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnMode, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnUmode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnUmode, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnTopic(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnTopic, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnKick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnKick, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnChannel(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnChannel, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnPrivMsg(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnPrivMsg, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnNotice, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnChannelNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnChannelNotice, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnInvite(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnInvite, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnCtcpReq(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnCtcpReq, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnCtcpRep(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnCtcpRep, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnCtcpAction(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnCtcpAction, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnUnknown(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnUnknown, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnNumeric(irc_session_t * session, Uint32 event, CCStr origin, CCStr * params, Uint32 count)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnNumeric, event, origin, params, count);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnDccChatReq(irc_session_t * session, CCStr nick, CCStr addr, irc_dcc_t dccid)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnDccChatReq, nick, addr, dccid);
}
}
// ------------------------------------------------------------------------------------------------
void Session::OnDccSendReq(irc_session_t * session, CCStr nick, CCStr addr, CCStr filename, Ulong size, irc_dcc_t dccid)
{
Session * inst = (Session *)irc_get_ctx(session);
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
if (ValidateEventSession(inst))
{
ForwardEvent(inst, inst->m_OnDccSendReq, nick, addr, filename, size, dccid);
}
}
// ------------------------------------------------------------------------------------------------
@@ -808,63 +923,59 @@ SQInteger Session::CmdMsgF(HSQUIRRELVM vm)
{
// Obtain the initial stack size
const Int32 top = sq_gettop(vm);
// Do we have a target?
// Do we have a target value?
if (top <= 1)
{
return sq_throwerror(vm, "Missing the message target");
// Do we have a valid target?
else if (sq_gettype(vm, 2) != OT_STRING)
return sq_throwerror(vm, "Invalid target specified");
}
// Do we have a message value?
else if (top <= 2)
{
return sq_throwerror(vm, "Missing the message value");
// Obtain the instance and target
Var< Session * > inst(vm, 1);
Var< String > target(vm, 2);
// The value returned by the command
Int32 code = -1;
}
// The session instance
Session * session = nullptr;
// Attempt to extract the argument values
try
{
session = Var< Session * >(vm, 1).value;
}
catch (const Sqrat::Exception & e)
{
// Propagate the error
return sq_throwerror(vm, e.Message().c_str());
}
// Do we have a valid session instance?
if (!inst.value)
if (!session)
{
return sq_throwerror(vm, "Invalid session instance");
}
// Do we have a valid session?
else if (!inst.value->m_Session)
else if (!session->m_Session)
{
return sq_throwerror(vm, "Invalid IRC session");
}
// Is the session connected?
else if (!inst.value->Connected())
else if (!session->Connected())
{
return sq_throwerror(vm, "Session is not connected");
// Is the specified message value a string or something convertible to string?
else if (top == 3 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
CSStr msg = NULL;
// Attempt to retrieve the string from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
{
// If the value was converted to a string then pop the string
sq_pop(vm, sq_gettop(vm) - top);
// Now we can throw the error message
return sq_throwerror(vm, "Unable to retrieve the message");
}
// Forward the resulted string value
code = irc_cmd_msg(inst.value->m_Session, target.value.c_str(), msg);
// If the value was converted to a string then pop the string
sq_pop(vm, sq_gettop(vm) - top);
}
// Do we have enough values to call the format function?
else if (top > 3)
// Attempt to retrieve the target from the stack as a string
StackStrF target(vm, 2, false);
// Have we failed to retrieve the string?
if (SQ_FAILED(target.mRes))
{
SStr msg = NULL;
SQInteger len = 0;
// Attempt to generate the specified string format
SQRESULT ret = sqstd_format(vm, 3, &len, &msg);
// Did the format failed?
if (SQ_FAILED(ret))
return ret;
// Forward the resulted string value
else if (msg)
code = irc_cmd_msg(inst.value->m_Session, target.value.c_str(), msg);
return target.mRes; // Propagate the error!
}
// All methods of retrieving the message value failed
else
return sq_throwerror(vm, "Unable to extract the player message");
// Attempt to retrieve the value from the stack as a string
StackStrF message(vm, 3);
// Have we failed to retrieve the string?
if (SQ_FAILED(message.mRes))
{
return message.mRes; // Propagate the error!
}
// Forward the resulted string value and save the returned result code
const Int32 code = irc_cmd_msg(session->m_Session, target.mPtr, message.mPtr);
// Push the obtained code onto the stack
sq_pushinteger(vm, code);
// We have a value on the stack
@@ -876,63 +987,59 @@ SQInteger Session::CmdMeF(HSQUIRRELVM vm)
{
// Obtain the initial stack size
const Int32 top = sq_gettop(vm);
// Do we have a target?
// Do we have a target value?
if (top <= 1)
{
return sq_throwerror(vm, "Missing the message target");
// Do we have a valid target?
else if (sq_gettype(vm, 2) != OT_STRING)
return sq_throwerror(vm, "Invalid target specified");
}
// Do we have a message value?
else if (top <= 2)
{
return sq_throwerror(vm, "Missing the message value");
// Obtain the instance and target
Var< Session * > inst(vm, 1);
Var< String > target(vm, 2);
// The value returned by the command
Int32 code = -1;
}
// The session instance
Session * session = nullptr;
// Attempt to extract the argument values
try
{
session = Var< Session * >(vm, 1).value;
}
catch (const Sqrat::Exception & e)
{
// Propagate the error
return sq_throwerror(vm, e.Message().c_str());
}
// Do we have a valid session instance?
if (!inst.value)
if (!session)
{
return sq_throwerror(vm, "Invalid session instance");
}
// Do we have a valid session?
else if (!inst.value->m_Session)
else if (!session->m_Session)
{
return sq_throwerror(vm, "Invalid IRC session");
}
// Is the session connected?
else if (!inst.value->Connected())
else if (!session->Connected())
{
return sq_throwerror(vm, "Session is not connected");
// Is the specified message value a string or something convertible to string?
else if (top == 3 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
CSStr msg = NULL;
// Attempt to retrieve the string from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
{
// If the value was converted to a string then pop the string
sq_pop(vm, sq_gettop(vm) - top);
// Now we can throw the error message
return sq_throwerror(vm, "Unable to retrieve the message");
}
// Forward the resulted string value
code = irc_cmd_me(inst.value->m_Session, target.value.c_str(), msg);
// If the value was converted to a string then pop the string
sq_pop(vm, sq_gettop(vm) - top);
}
// Do we have enough values to call the format function?
else if (top > 3)
// Attempt to retrieve the target from the stack as a string
StackStrF target(vm, 2, false);
// Have we failed to retrieve the string?
if (SQ_FAILED(target.mRes))
{
SStr msg = NULL;
SQInteger len = 0;
// Attempt to generate the specified string format
SQRESULT ret = sqstd_format(vm, 3, &len, &msg);
// Did the format failed?
if (SQ_FAILED(ret))
return ret;
// Forward the resulted string value
else if (msg)
code = irc_cmd_me(inst.value->m_Session, target.value.c_str(), msg);
return target.mRes; // Propagate the error!
}
// All methods of retrieving the message value failed
else
return sq_throwerror(vm, "Unable to extract the player message");
// Attempt to retrieve the value from the stack as a string
StackStrF message(vm, 3);
// Have we failed to retrieve the string?
if (SQ_FAILED(message.mRes))
{
return message.mRes; // Propagate the error!
}
// Forward the resulted string value and save the returned result code
const Int32 code = irc_cmd_me(session->m_Session, target.mPtr, message.mPtr);
// Push the obtained code onto the stack
sq_pushinteger(vm, code);
// We have a value on the stack
@@ -944,63 +1051,59 @@ SQInteger Session::CmdNoticeF(HSQUIRRELVM vm)
{
// Obtain the initial stack size
const Int32 top = sq_gettop(vm);
// Do we have a target?
// Do we have a target value?
if (top <= 1)
{
return sq_throwerror(vm, "Missing the message target");
// Do we have a valid target?
else if (sq_gettype(vm, 2) != OT_STRING)
return sq_throwerror(vm, "Invalid target specified");
}
// Do we have a message value?
else if (top <= 2)
{
return sq_throwerror(vm, "Missing the message value");
// Obtain the instance and target
Var< Session * > inst(vm, 1);
Var< String > target(vm, 2);
// The value returned by the command
Int32 code = -1;
}
// The session instance
Session * session = nullptr;
// Attempt to extract the argument values
try
{
session = Var< Session * >(vm, 1).value;
}
catch (const Sqrat::Exception & e)
{
// Propagate the error
return sq_throwerror(vm, e.Message().c_str());
}
// Do we have a valid session instance?
if (!inst.value)
if (!session)
{
return sq_throwerror(vm, "Invalid session instance");
}
// Do we have a valid session?
else if (!inst.value->m_Session)
else if (!session->m_Session)
{
return sq_throwerror(vm, "Invalid IRC session");
}
// Is the session connected?
else if (!inst.value->Connected())
else if (!session->Connected())
{
return sq_throwerror(vm, "Session is not connected");
// Is the specified message value a string or something convertible to string?
else if (top == 3 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
CSStr msg = NULL;
// Attempt to retrieve the string from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
{
// If the value was converted to a string then pop the string
sq_pop(vm, sq_gettop(vm) - top);
// Now we can throw the error message
return sq_throwerror(vm, "Unable to retrieve the message");
}
// Forward the resulted string value
code = irc_cmd_notice(inst.value->m_Session, target.value.c_str(), msg);
// If the value was converted to a string then pop the string
sq_pop(vm, sq_gettop(vm) - top);
}
// Do we have enough values to call the format function?
else if (top > 3)
// Attempt to retrieve the target from the stack as a string
StackStrF target(vm, 2, false);
// Have we failed to retrieve the string?
if (SQ_FAILED(target.mRes))
{
SStr msg = NULL;
SQInteger len = 0;
// Attempt to generate the specified string format
SQRESULT ret = sqstd_format(vm, 3, &len, &msg);
// Did the format failed?
if (SQ_FAILED(ret))
return ret;
// Forward the resulted string value
else if (msg)
code = irc_cmd_notice(inst.value->m_Session, target.value.c_str(), msg);
return target.mRes; // Propagate the error!
}
// All methods of retrieving the message value failed
else
return sq_throwerror(vm, "Unable to extract the player message");
// Attempt to retrieve the value from the stack as a string
StackStrF message(vm, 3);
// Have we failed to retrieve the string?
if (SQ_FAILED(message.mRes))
{
return message.mRes; // Propagate the error!
}
// Forward the resulted string value and save the returned result code
const Int32 code = irc_cmd_notice(session->m_Session, target.mPtr, message.mPtr);
// Push the obtained code onto the stack
sq_pushinteger(vm, code);
// We have a value on the stack