2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 10:57:10 +01:00
|
|
|
#include "Session.hpp"
|
|
|
|
#include "Module.hpp"
|
2016-02-23 04:23:56 +01:00
|
|
|
|
2016-02-23 05:47:02 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-02 11:49:32 +02:00
|
|
|
#include <cstring>
|
2016-02-23 05:47:02 +01:00
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
irc_callbacks_t Session::s_Callbacks;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Session::s_Initialized = false;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-02 11:49:32 +02:00
|
|
|
Session* Session::s_Session = nullptr;
|
2016-02-23 04:23:56 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Session::Sessions Session::s_Sessions;
|
|
|
|
|
2016-02-28 15:17:59 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Session::Typename(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-05-22 21:34:27 +02:00
|
|
|
static const SQChar name[] = _SC("SqIrcSession");
|
2016-02-28 15:17:59 +01:00
|
|
|
sq_pushstring(vm, name, sizeof(name));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::Process()
|
|
|
|
{
|
|
|
|
// Do we only have one IRC session?
|
|
|
|
if (s_Session)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
s_Session->Update();
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Do we have multiple sessions?
|
|
|
|
else if (!s_Sessions.empty())
|
|
|
|
{
|
|
|
|
for (Sessions::iterator itr = s_Sessions.begin(); itr != s_Sessions.end(); ++itr)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
(*itr)->Update();
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::Terminate()
|
|
|
|
{
|
|
|
|
// Do we only have one IRC session?
|
|
|
|
if (s_Session)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
s_Session->Destroy(); /* This should do the job. */
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Do we have multiple sessions?
|
|
|
|
else if (!s_Sessions.empty())
|
|
|
|
{
|
|
|
|
for (Sessions::iterator itr = s_Sessions.begin(); itr != s_Sessions.end(); ++itr)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
(*itr)->Destroy();
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
irc_callbacks_t * Session::GetCallbacks()
|
|
|
|
{
|
|
|
|
// See if the callbacks structure was initialized before
|
|
|
|
if (!s_Initialized)
|
|
|
|
{
|
|
|
|
// Initialize the callbacks
|
|
|
|
s_Callbacks.event_connect = &OnConnect;
|
|
|
|
s_Callbacks.event_nick = &OnNick;
|
|
|
|
s_Callbacks.event_quit = &OnQuit;
|
|
|
|
s_Callbacks.event_join = &OnJoin;
|
|
|
|
s_Callbacks.event_part = &OnPart;
|
|
|
|
s_Callbacks.event_mode = &OnMode;
|
|
|
|
s_Callbacks.event_umode = &OnUmode;
|
|
|
|
s_Callbacks.event_topic = &OnTopic;
|
|
|
|
s_Callbacks.event_kick = &OnKick;
|
|
|
|
s_Callbacks.event_channel = &OnChannel;
|
|
|
|
s_Callbacks.event_privmsg = &OnPrivMsg;
|
|
|
|
s_Callbacks.event_notice = &OnNotice;
|
|
|
|
s_Callbacks.event_channel_notice = &OnChannelNotice;
|
|
|
|
s_Callbacks.event_invite = &OnInvite;
|
|
|
|
s_Callbacks.event_ctcp_req = &OnCtcpReq;
|
|
|
|
s_Callbacks.event_ctcp_rep = &OnCtcpRep;
|
|
|
|
s_Callbacks.event_ctcp_action = &OnCtcpAction;
|
|
|
|
s_Callbacks.event_unknown = &OnUnknown;
|
|
|
|
s_Callbacks.event_numeric = &OnNumeric;
|
|
|
|
s_Callbacks.event_dcc_chat_req = &OnDccChatReq;
|
|
|
|
s_Callbacks.event_dcc_send_req = &OnDccSendReq;
|
|
|
|
// Specify that the callbacks were initialized
|
|
|
|
s_Initialized = true;
|
|
|
|
}
|
|
|
|
// Return the callbacks structure
|
|
|
|
return &s_Callbacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::Update()
|
|
|
|
{
|
|
|
|
// Make sure we even have a session
|
|
|
|
if (!m_Session)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
return;
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Make sure that the IRC session is connected
|
|
|
|
else if (!irc_is_connected(m_Session))
|
|
|
|
{
|
|
|
|
// Do we meet the condition to attempt to reconnect?
|
2016-02-27 10:57:10 +01:00
|
|
|
if (m_Reconnect && (m_LeftTries != 0) && (m_NextTry <= _SqMod->GetEpochTimeMicro()))
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
|
|
|
// Take out one try
|
|
|
|
--m_LeftTries;
|
|
|
|
// Update the time-point for the next try
|
2016-02-27 10:57:10 +01:00
|
|
|
m_NextTry = (_SqMod->GetEpochTimeMicro() + (m_Wait * 1000LL));
|
2016-02-23 04:23:56 +01:00
|
|
|
// Attempt to reconnect
|
|
|
|
if (m_IPv6)
|
|
|
|
{
|
|
|
|
m_LastCode = irc_connect6(m_Session, m_Server.c_str(), m_Port,
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
|
2016-02-23 04:23:56 +01:00
|
|
|
m_Nick.c_str(),
|
2016-04-02 11:49:32 +02:00
|
|
|
m_User.empty() ? nullptr : m_User.c_str(),
|
|
|
|
m_Name.empty() ? nullptr : m_Name.c_str()
|
2016-02-23 04:23:56 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_LastCode = irc_connect(m_Session, m_Server.c_str(), m_Port,
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
|
2016-02-23 04:23:56 +01:00
|
|
|
m_Nick.c_str(),
|
2016-04-02 11:49:32 +02:00
|
|
|
m_User.empty() ? nullptr : m_User.c_str(),
|
|
|
|
m_Name.empty() ? nullptr : m_Name.c_str()
|
2016-02-23 04:23:56 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We're done for now
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Create the structures for select()
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set in_set, out_set;
|
|
|
|
int maxfd = 0;
|
|
|
|
// Pool for the specified amount of time
|
|
|
|
tv.tv_usec = (m_PoolTime * 1000L);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
// Initialize the sets
|
2016-04-02 11:49:32 +02:00
|
|
|
std::memset(&in_set, 0, sizeof(fd_set));
|
|
|
|
std::memset(&out_set, 0, sizeof(fd_set));
|
2016-02-23 04:23:56 +01:00
|
|
|
// Add the IRC session descriptors
|
|
|
|
irc_add_select_descriptors(m_Session, &in_set, &out_set, &maxfd);
|
|
|
|
// Call select()
|
|
|
|
if (select(maxfd + 1, &in_set, &out_set, 0, &tv) < 0)
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
_SqMod->LogErr("Unable to select() on IRC session");
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
// Call irc_process_select_descriptors() for the session
|
|
|
|
if (irc_process_select_descriptors (m_Session, &in_set, &out_set))
|
|
|
|
{
|
|
|
|
// @TODO: The connection failed, or the server disconnected. Handle it!
|
2016-02-27 10:57:10 +01:00
|
|
|
_SqMod->LogWrn("The IRC connection failed, or the server disconnected.");
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::Release()
|
|
|
|
{
|
|
|
|
// Release the reference to the specified callbacks
|
|
|
|
m_OnConnect.ReleaseGently();
|
|
|
|
m_OnNick.ReleaseGently();
|
|
|
|
m_OnQuit.ReleaseGently();
|
|
|
|
m_OnJoin.ReleaseGently();
|
|
|
|
m_OnPart.ReleaseGently();
|
|
|
|
m_OnMode.ReleaseGently();
|
|
|
|
m_OnUmode.ReleaseGently();
|
|
|
|
m_OnTopic.ReleaseGently();
|
|
|
|
m_OnKick.ReleaseGently();
|
|
|
|
m_OnChannel.ReleaseGently();
|
|
|
|
m_OnPrivMsg.ReleaseGently();
|
|
|
|
m_OnNotice.ReleaseGently();
|
|
|
|
m_OnChannelNotice.ReleaseGently();
|
|
|
|
m_OnInvite.ReleaseGently();
|
|
|
|
m_OnCtcpReq.ReleaseGently();
|
|
|
|
m_OnCtcpRep.ReleaseGently();
|
|
|
|
m_OnCtcpAction.ReleaseGently();
|
|
|
|
m_OnUnknown.ReleaseGently();
|
|
|
|
m_OnNumeric.ReleaseGently();
|
|
|
|
m_OnDccChatReq.ReleaseGently();
|
|
|
|
m_OnDccSendReq.ReleaseGently();
|
|
|
|
// Release the reference to the specified user data
|
|
|
|
m_Data.Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::Destroy()
|
|
|
|
{
|
|
|
|
// Make sure there's even a session to release
|
|
|
|
if (!m_Session)
|
|
|
|
return;
|
|
|
|
// Disconnect the session
|
|
|
|
Disconnect();
|
|
|
|
// Break the association with this instance (paranoia)
|
2016-04-02 11:49:32 +02:00
|
|
|
irc_set_ctx(m_Session, nullptr);
|
2016-02-23 04:23:56 +01:00
|
|
|
// Destroy the IRC session structure
|
|
|
|
irc_destroy_session(m_Session);
|
|
|
|
// Explicitly make sure no further calls can be made to this session (again... paranoia)
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Session = nullptr;
|
2016-02-23 04:23:56 +01:00
|
|
|
// Release resources
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:57:10 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-28 15:17:59 +01:00
|
|
|
void Session::Validate() const
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Do we have a valid session handle?
|
|
|
|
if (!m_Session)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Invalid IRC session");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-28 15:17:59 +01:00
|
|
|
void Session::ValidateConnection() const
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Do we have a valid session handle?
|
2016-02-27 10:57:10 +01:00
|
|
|
if (!m_Session)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Invalid IRC session");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Is the session connected?
|
2016-02-27 10:57:10 +01:00
|
|
|
else if (!irc_is_connected(m_Session))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Session is not connected");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-28 15:17:59 +01:00
|
|
|
void Session::IsNotConnected() const
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Do we have a session that is not connected or trying to connect?
|
|
|
|
if (m_Session && (irc_is_connected(m_Session) || m_Reconnect))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Already connected or trying connect to IRC server");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Session::ValidateEventSession(Session * ptr)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Is the session instance valid?
|
2016-02-27 10:57:10 +01:00
|
|
|
if (ptr)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return true;
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// We can't throw an error here so we simply log it
|
2016-05-24 20:36:02 +02:00
|
|
|
_SqMod->LogErr("Cannot forward IRC event without a session instance");
|
2016-02-28 15:17:59 +01:00
|
|
|
// Invalid session instance
|
2016-02-27 10:57:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Session::Session()
|
|
|
|
: m_Session(irc_create_session(GetCallbacks()))
|
|
|
|
, m_Server(_SC(""))
|
|
|
|
, m_Passwd(_SC(""))
|
|
|
|
, m_Nick(_SC(""))
|
|
|
|
, m_User(_SC(""))
|
|
|
|
, m_Name(_SC(""))
|
|
|
|
, m_Port(6667)
|
|
|
|
, m_LastCode(0)
|
|
|
|
, m_PoolTime(4)
|
|
|
|
, m_Tries(3)
|
|
|
|
, m_Wait(5000)
|
|
|
|
, m_LeftTries(0)
|
|
|
|
, m_NextTry(0)
|
|
|
|
, m_SessionTime(0)
|
|
|
|
, m_Reconnect(false)
|
|
|
|
, m_IPv6(false)
|
|
|
|
{
|
|
|
|
if (!m_Session)
|
|
|
|
{
|
|
|
|
// Explicitly make sure no further calls can be made to this session
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Session = nullptr;
|
2016-02-28 15:17:59 +01:00
|
|
|
// Now it's safe to throw the error
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Unable to create an IRC session");
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Associate this wrapper instance with the session structure
|
|
|
|
irc_set_ctx(m_Session, this);
|
|
|
|
// Is this the only session instance?
|
|
|
|
if (!s_Session && s_Sessions.empty())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
s_Session = this;
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Is this the second session instance?
|
2016-02-23 04:23:56 +01:00
|
|
|
else if (s_Sessions.empty())
|
|
|
|
{
|
|
|
|
s_Sessions.push_back(s_Session);
|
2016-04-02 11:49:32 +02:00
|
|
|
s_Session = nullptr;
|
2016-02-23 04:23:56 +01:00
|
|
|
s_Sessions.push_back(this);
|
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// This is part of multiple session instances
|
2016-02-23 04:23:56 +01:00
|
|
|
else
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
s_Sessions.push_back(this);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Session::~Session()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
// Attempt to find our self in the session pool
|
|
|
|
Sessions::iterator itr = std::find(s_Sessions.begin(), s_Sessions.end(), this);
|
|
|
|
// Are we in the pool?
|
|
|
|
if (itr != s_Sessions.end())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
s_Sessions.erase(itr); /* Remove our self from the pool. */
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Is there a single session and that's us?
|
|
|
|
if (s_Session == this)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
|
|
|
s_Session = nullptr;
|
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
2016-05-24 20:36:02 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Function & Session::GetEvent(Int32 evid)
|
|
|
|
{
|
|
|
|
// Identify the requested event type
|
|
|
|
switch (evid)
|
|
|
|
{
|
|
|
|
case SET_CONNECT: return m_OnConnect;
|
|
|
|
case SET_NICK: return m_OnNick;
|
|
|
|
case SET_QUIT: return m_OnQuit;
|
|
|
|
case SET_JOIN: return m_OnJoin;
|
|
|
|
case SET_PART: return m_OnPart;
|
|
|
|
case SET_MODE: return m_OnMode;
|
|
|
|
case SET_UMODE: return m_OnUmode;
|
|
|
|
case SET_TOPIC: return m_OnTopic;
|
|
|
|
case SET_KICK: return m_OnKick;
|
|
|
|
case SET_CHANNEL: return m_OnChannel;
|
|
|
|
case SET_PRIVMSG: return m_OnPrivMsg;
|
|
|
|
case SET_NOTICE: return m_OnNotice;
|
|
|
|
case SET_CHANNELNOTICE: return m_OnChannelNotice;
|
|
|
|
case SET_INVITE: return m_OnInvite;
|
|
|
|
case SET_CTCPREQ: return m_OnCtcpReq;
|
|
|
|
case SET_CTCPREP: return m_OnCtcpRep;
|
|
|
|
case SET_CTCPACTION: return m_OnCtcpAction;
|
|
|
|
case SET_UNKNOWN: return m_OnUnknown;
|
|
|
|
case SET_NUMERIC: return m_OnNumeric;
|
|
|
|
case SET_DCCCHATREQ: return m_OnDccChatReq;
|
|
|
|
case SET_DCCSENDREQ: return m_OnDccSendReq;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
// Default to a null function
|
|
|
|
return NullFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::BindEvent(Int32 evid, Object & env, Function & func)
|
|
|
|
{
|
|
|
|
// Validate the handle
|
|
|
|
Validate();
|
|
|
|
// Obtain the function instance called for this event
|
|
|
|
Function & event = GetEvent(evid);
|
|
|
|
// Is the specified callback function null?
|
|
|
|
if (func.IsNull())
|
|
|
|
{
|
|
|
|
event.ReleaseGently(); // Then release the current callback
|
|
|
|
}
|
|
|
|
// Does this function need a custom environment?
|
|
|
|
else if (env.IsNull())
|
|
|
|
{
|
|
|
|
event = func;
|
|
|
|
}
|
|
|
|
// Assign the specified environment and function
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:57:10 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::SetNick(CSStr nick)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Validate the handle
|
|
|
|
Validate();
|
|
|
|
// Validate the specified nick name
|
2016-02-27 10:57:10 +01:00
|
|
|
if (!nick || strlen(nick) <= 0)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Invalid IRC nickname");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Do we have to issue a nickname command?
|
2016-02-27 10:57:10 +01:00
|
|
|
else if (Connected())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
irc_cmd_nick(m_Session, nick);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Simply save the specified nickname
|
|
|
|
else
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
m_Nick.assign(nick);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::SetPort(Uint32 num)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// The port cannot be changed once connected!
|
|
|
|
IsNotConnected();
|
|
|
|
// Validate the specified port number
|
2016-02-27 10:57:10 +01:00
|
|
|
if (num > 0xFFFF)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Port number is out of range: %u > %u", num, 0xFFFF);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Assign the specified port number
|
|
|
|
m_Port = num;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Session::CmdNick(CSStr nick)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Make sure the session is connected
|
|
|
|
ValidateConnection();
|
|
|
|
// Validate the specified nick name
|
2016-02-27 10:57:10 +01:00
|
|
|
if (!nick || strlen(nick) <= 0)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Invalid IRC nickname");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Issue the command and return the result
|
|
|
|
return irc_cmd_nick(m_Session, nick);
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object Session::GetNextTry() const
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
2016-02-28 15:17:59 +01:00
|
|
|
const StackGuard sg(_SqVM);
|
2016-02-27 10:57:10 +01:00
|
|
|
// Attempt to push a time-stamp instance on the stack
|
|
|
|
_SqMod->PushTimestamp(_SqVM, m_NextTry);
|
2016-02-28 15:17:59 +01:00
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return Var< Object >(_SqVM, -1).value;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::SetNextTry(Object & tm)
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
2016-02-28 15:17:59 +01:00
|
|
|
const StackGuard sg(_SqVM);
|
2016-02-27 10:57:10 +01:00
|
|
|
// Push the specified object onto the stack
|
|
|
|
Var< Object >::push(_SqVM, tm);
|
|
|
|
// The resulted times-tamp value
|
|
|
|
Int64 microseconds = 0;
|
|
|
|
// Attempt to get the numeric value inside the specified object
|
|
|
|
if (SQ_FAILED(_SqMod->GetTimestamp(_SqVM, -1, µseconds)))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Invalid time-stamp specified");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Assign the specified timestamp value
|
|
|
|
m_NextTry = microseconds;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object Session::GetSessionTime() const
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
2016-02-28 15:17:59 +01:00
|
|
|
const StackGuard sg(_SqVM);
|
2016-02-27 10:57:10 +01:00
|
|
|
// Attempt to push a time-stamp instance on the stack
|
|
|
|
if (m_SessionTime)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
_SqMod->PushTimestamp(_SqVM, _SqMod->GetEpochTimeMicro() - m_SessionTime);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// This session was not connected yet
|
2016-02-27 10:57:10 +01:00
|
|
|
else
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
_SqMod->PushTimestamp(_SqVM, 0);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Obtain the object from the stack and return it
|
|
|
|
return Var< Object >(_SqVM, -1).value;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Session::Connect()
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Validate the handle
|
|
|
|
Validate();
|
|
|
|
// Make sure we are allowed to connect
|
|
|
|
IsNotConnected();
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified server
|
2016-02-28 15:17:59 +01:00
|
|
|
if (!m_Server.empty())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a server");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified nickname
|
|
|
|
else if (!m_Nick.empty())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a nickname");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Enable the reconnection system
|
|
|
|
m_Reconnect = true;
|
|
|
|
// Reset the number of tries
|
|
|
|
m_LeftTries = m_Tries;
|
|
|
|
// Set the time-point for the next try
|
|
|
|
m_NextTry = (_SqMod->GetEpochTimeMicro() + (m_Wait * 1000LL));
|
|
|
|
// This is not an IPv6 connection
|
|
|
|
m_IPv6 = false;
|
|
|
|
// Attempt to connect the session and return the result
|
|
|
|
return irc_connect(m_Session, m_Server.c_str(), m_Port,
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
|
2016-02-28 15:17:59 +01:00
|
|
|
m_Nick.c_str(),
|
2016-04-02 11:49:32 +02:00
|
|
|
m_User.empty() ? nullptr : m_User.c_str(),
|
|
|
|
m_Name.empty() ? nullptr : m_Name.c_str()
|
2016-02-28 15:17:59 +01:00
|
|
|
);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Session::Connect(CSStr server, Uint32 port, CSStr nick, CSStr passwd, CSStr user, CSStr name)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Validate the handle
|
|
|
|
Validate();
|
|
|
|
// Make sure we are allowed to connect
|
|
|
|
IsNotConnected();
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified port
|
2016-02-28 15:17:59 +01:00
|
|
|
if (port > 0xFFFF)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Port number is out of range: %u > %u", port, 0xFFFF);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified server
|
|
|
|
else if (!server || strlen(server) <= 0)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a server");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified nickname
|
|
|
|
else if (!nick || strlen(nick) <= 0)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a nickname");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Save the specified port
|
|
|
|
m_Port = port;
|
|
|
|
// Save the specified server
|
|
|
|
m_Server.assign(server);
|
|
|
|
// Save the specified nickname
|
|
|
|
m_Nick.assign(nick);
|
|
|
|
// Save the specified password
|
|
|
|
m_Passwd.assign(passwd ? passwd : _SC(""));
|
|
|
|
// Save the specified user
|
|
|
|
m_User.assign(user ? user : _SC(""));
|
|
|
|
// Save the specified name
|
|
|
|
m_Name.assign(name ? name : _SC(""));
|
|
|
|
// Enable the reconnection system
|
|
|
|
m_Reconnect = true;
|
|
|
|
// Reset the number of tries
|
|
|
|
m_LeftTries = m_Tries;
|
|
|
|
// Set the time-point for the next connection try
|
|
|
|
m_NextTry = (_SqMod->GetEpochTimeMicro() + (m_Wait * 1000LL));
|
|
|
|
// This is not an IPv6 connection
|
|
|
|
m_IPv6 = false;
|
|
|
|
// Attempt to connect the session and return the result
|
|
|
|
return irc_connect(m_Session, m_Server.c_str(), m_Port,
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
|
2016-02-28 15:17:59 +01:00
|
|
|
m_Nick.c_str(),
|
2016-04-02 11:49:32 +02:00
|
|
|
m_User.empty() ? nullptr : m_User.c_str(),
|
|
|
|
m_Name.empty() ? nullptr : m_Name.c_str()
|
2016-02-28 15:17:59 +01:00
|
|
|
);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Session::Connect6()
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Validate the handle
|
|
|
|
Validate();
|
|
|
|
// Make sure we are allowed to connect
|
|
|
|
IsNotConnected();
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified server
|
2016-02-28 15:17:59 +01:00
|
|
|
if (!m_Server.empty())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a server");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified nickname
|
|
|
|
else if (!m_Nick.empty())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a nickname");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Enable the reconnection system
|
|
|
|
m_Reconnect = true;
|
|
|
|
// Reset the number of tries
|
|
|
|
m_LeftTries = m_Tries;
|
|
|
|
// Set the time-point for the next try
|
|
|
|
m_NextTry = (_SqMod->GetEpochTimeMicro() + (m_Wait * 1000LL));
|
|
|
|
// This is an IPv6 connection
|
|
|
|
m_IPv6 = true;
|
|
|
|
// Attempt to connect the session and return the result
|
|
|
|
return irc_connect6(m_Session, m_Server.c_str(), m_Port,
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
|
2016-02-28 15:17:59 +01:00
|
|
|
m_Nick.c_str(),
|
2016-04-02 11:49:32 +02:00
|
|
|
m_User.empty() ? nullptr : m_User.c_str(),
|
|
|
|
m_Name.empty() ? nullptr : m_Name.c_str()
|
2016-02-28 15:17:59 +01:00
|
|
|
);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 Session::Connect6(CSStr server, Uint32 port, CSStr nick, CSStr passwd, CSStr user, CSStr name)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Validate the handle
|
|
|
|
Validate();
|
|
|
|
// Make sure we are allowed to connect
|
|
|
|
IsNotConnected();
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified port
|
2016-02-28 15:17:59 +01:00
|
|
|
if (port > 0xFFFF)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Port number is out of range: %u > %u", port, 0xFFFF);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified server
|
|
|
|
else if (!server || strlen(server) <= 0)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a server");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Validate the specified nickname
|
|
|
|
else if (!nick || strlen(nick) <= 0)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-03-22 23:26:26 +01:00
|
|
|
STHROWF("Attempting to connect IRC without specifying a nickname");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-28 15:17:59 +01:00
|
|
|
// Save the specified port
|
|
|
|
m_Port = port;
|
|
|
|
// Save the specified server
|
|
|
|
m_Server.assign(server);
|
|
|
|
// Save the specified nickname
|
|
|
|
m_Nick.assign(nick);
|
|
|
|
// Save the specified password
|
|
|
|
m_Passwd.assign(passwd ? passwd : _SC(""));
|
|
|
|
// Save the specified user
|
|
|
|
m_User.assign(user ? user : _SC(""));
|
|
|
|
// Save the specified name
|
|
|
|
m_Name.assign(name ? name : _SC(""));
|
|
|
|
// Enable the reconnection system
|
|
|
|
m_Reconnect = true;
|
|
|
|
// Reset the number of tries
|
|
|
|
m_LeftTries = m_Tries;
|
|
|
|
// Set the time-point for the next connection try
|
|
|
|
m_NextTry = (_SqMod->GetEpochTimeMicro() + (m_Wait * 1000LL));
|
|
|
|
// This is an IPv6 connection
|
|
|
|
m_IPv6 = true;
|
|
|
|
// Attempt to connect the session and return the result
|
|
|
|
return irc_connect6(m_Session, m_Server.c_str(), m_Port,
|
2016-04-02 11:49:32 +02:00
|
|
|
m_Passwd.empty() ? nullptr : m_Passwd.c_str(),
|
2016-02-28 15:17:59 +01:00
|
|
|
m_Nick.c_str(),
|
2016-04-02 11:49:32 +02:00
|
|
|
m_User.empty() ? nullptr : m_User.c_str(),
|
|
|
|
m_Name.empty() ? nullptr : m_Name.c_str()
|
2016-02-28 15:17:59 +01:00
|
|
|
);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::Disconnect()
|
|
|
|
{
|
|
|
|
if (Connected())
|
|
|
|
{
|
|
|
|
// Update one last time to catch remaining events
|
|
|
|
Update();
|
|
|
|
// Disable the reconnection system
|
|
|
|
m_Reconnect = false;
|
|
|
|
// Attempt to disconnect
|
|
|
|
irc_disconnect(m_Session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-24 19:51:34 +02:00
|
|
|
void Session::ForwardEvent(Function & listener, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
|
|
|
// Is there anyone even listening to this event?
|
|
|
|
if (listener.IsNull())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
|
|
|
return; // No point in going forward
|
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Make sure that the origin can't be a null pointer
|
|
|
|
else if (!origin)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
origin = _SC("");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Each event must have an array of parameters (empty or not)
|
2016-02-28 15:17:59 +01:00
|
|
|
Array parameters(_SqVM, count);
|
2016-02-23 04:23:56 +01:00
|
|
|
// Are the any parameters?
|
|
|
|
if (params && count > 0)
|
|
|
|
{
|
|
|
|
// Transform the parameters into a squirrel array
|
|
|
|
for (Uint32 i = 0; i < count; ++i)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
parameters.SetValue(i, params[i]);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
2016-05-24 19:57:28 +02:00
|
|
|
// Attempt top execute the callback with the obtained values
|
|
|
|
try
|
|
|
|
{
|
|
|
|
listener.Execute< CSStr, CSStr, Array & >(event, origin, parameters);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
_SqMod->LogErr("IRC event [%s] => Squirrel error [%s]", event, e.Message().c_str());
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
_SqMod->LogErr("IRC event [%s] => Program error [%s]", event, e.what());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
_SqMod->LogErr("IRC event [%s] => Unknown error", event);
|
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-24 19:51:34 +02:00
|
|
|
void Session::ForwardEvent(Function & listener, Uint32 event,
|
2016-02-23 04:23:56 +01:00
|
|
|
CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
|
|
|
// Is there anyone even listening to this event?
|
|
|
|
if (listener.IsNull())
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
|
|
|
return; // No point in going forward
|
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Make sure that the origin can't be a null pointer
|
|
|
|
else if (!origin)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
origin = _SC("");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
// Each event must have an array of parameters (empty or not)
|
2016-02-28 15:17:59 +01:00
|
|
|
Array parameters(_SqVM, count);
|
2016-02-23 04:23:56 +01:00
|
|
|
// Are the any parameters?
|
|
|
|
if (params && count > 0)
|
|
|
|
{
|
|
|
|
// Transform the parameters into a squirrel array
|
|
|
|
for (unsigned int i = 0; i < count; ++i)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
parameters.SetValue(i, params[i]);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
2016-05-24 19:57:28 +02:00
|
|
|
// Attempt top execute the callback with the obtained values
|
|
|
|
try
|
|
|
|
{
|
|
|
|
listener.Execute< Uint32, CSStr, Array & >(event, origin, parameters);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
_SqMod->LogErr("IRC event [%s] => Squirrel error [%s]", event, e.Message().c_str());
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
_SqMod->LogErr("IRC event [%s] => Program error [%s]", event, e.what());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
_SqMod->LogErr("IRC event [%s] => Unknown error", event);
|
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-24 19:51:34 +02:00
|
|
|
void Session::ForwardEvent(Function & /*listener*/, CCStr /*nick*/,
|
2016-02-23 04:23:56 +01:00
|
|
|
CCStr /*addr*/, irc_dcc_t /*dccid*/)
|
|
|
|
{
|
|
|
|
/* TODO! */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-24 19:51:34 +02:00
|
|
|
void Session::ForwardEvent(Function & /*listener*/, CCStr /*nick*/,
|
2016-02-23 04:23:56 +01:00
|
|
|
CCStr /*addr*/, CCStr /*filename*/, Ulong /*size*/, irc_dcc_t /*dccid*/)
|
|
|
|
{
|
|
|
|
/* TODO! */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnConnect(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
|
|
|
{
|
|
|
|
// Prevent any attempts to reconnect now
|
|
|
|
inst->m_Reconnect = false;
|
|
|
|
// Save the connection time-stamp to calculate session uptime
|
2016-02-27 10:57:10 +01:00
|
|
|
inst->m_SessionTime = _SqMod->GetEpochTimeMicro();
|
2016-02-23 04:23:56 +01:00
|
|
|
// Now forward event
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnConnect, event, origin, params, count);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnNick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
|
|
|
{
|
|
|
|
if (params && params[0])
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 04:23:56 +01:00
|
|
|
inst->m_Nick.assign(params[0]);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnNick, event, origin, params, count);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnQuit(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnQuit, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnJoin(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnJoin, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnPart(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnPart, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnMode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnMode, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnUmode(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnUmode, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnTopic(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnTopic, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnKick(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnKick, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnChannel(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnChannel, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnPrivMsg(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnPrivMsg, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnNotice, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnChannelNotice(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnChannelNotice, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnInvite(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnInvite, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnCtcpReq(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnCtcpReq, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnCtcpRep(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnCtcpRep, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnCtcpAction(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnCtcpAction, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnUnknown(irc_session_t * session, CCStr event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnUnknown, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnNumeric(irc_session_t * session, Uint32 event, CCStr origin, CCStr * params, Uint32 count)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnNumeric, event, origin, params, count);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnDccChatReq(irc_session_t * session, CCStr nick, CCStr addr, irc_dcc_t dccid)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnDccChatReq, nick, addr, dccid);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Session::OnDccSendReq(irc_session_t * session, CCStr nick, CCStr addr, CCStr filename, Ulong size, irc_dcc_t dccid)
|
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
Session * inst = reinterpret_cast< Session * >(irc_get_ctx(session));
|
2016-02-23 04:23:56 +01:00
|
|
|
if (ValidateEventSession(inst))
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-05-24 19:51:34 +02:00
|
|
|
ForwardEvent(inst->m_OnDccSendReq, nick, addr, filename, size, dccid);
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 05:47:02 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Session::CmdMsgF(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Obtain the initial stack size
|
2016-02-23 05:47:02 +01:00
|
|
|
const Int32 top = sq_gettop(vm);
|
2016-04-02 11:49:32 +02:00
|
|
|
// Do we have a target value?
|
2016-02-23 05:47:02 +01:00
|
|
|
if (top <= 1)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Missing the message target");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 05:47:02 +01:00
|
|
|
// Do we have a message value?
|
|
|
|
else if (top <= 2)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Missing the message value");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
|
|
|
// 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());
|
|
|
|
}
|
2016-02-23 05:47:02 +01:00
|
|
|
// Do we have a valid session instance?
|
2016-04-02 11:49:32 +02:00
|
|
|
if (!session)
|
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Invalid session instance");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Do we have a valid session?
|
2016-04-02 11:49:32 +02:00
|
|
|
else if (!session->m_Session)
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return sq_throwerror(vm, "Invalid IRC session");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Is the session connected?
|
2016-04-02 11:49:32 +02:00
|
|
|
else if (!session->Connected())
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return sq_throwerror(vm, "Session is not connected");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
|
|
|
// 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))
|
2016-02-23 05:47:02 +01:00
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
return target.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// 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);
|
2016-02-23 05:47:02 +01:00
|
|
|
// Push the obtained code onto the stack
|
|
|
|
sq_pushinteger(vm, code);
|
|
|
|
// We have a value on the stack
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Session::CmdMeF(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Obtain the initial stack size
|
2016-02-23 05:47:02 +01:00
|
|
|
const Int32 top = sq_gettop(vm);
|
2016-04-02 11:49:32 +02:00
|
|
|
// Do we have a target value?
|
2016-02-23 05:47:02 +01:00
|
|
|
if (top <= 1)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Missing the message target");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 05:47:02 +01:00
|
|
|
// Do we have a message value?
|
|
|
|
else if (top <= 2)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Missing the message value");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
|
|
|
// 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());
|
|
|
|
}
|
2016-02-23 05:47:02 +01:00
|
|
|
// Do we have a valid session instance?
|
2016-04-02 11:49:32 +02:00
|
|
|
if (!session)
|
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Invalid session instance");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Do we have a valid session?
|
2016-04-02 11:49:32 +02:00
|
|
|
else if (!session->m_Session)
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return sq_throwerror(vm, "Invalid IRC session");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Is the session connected?
|
2016-04-02 11:49:32 +02:00
|
|
|
else if (!session->Connected())
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return sq_throwerror(vm, "Session is not connected");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
|
|
|
// 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))
|
2016-02-23 05:47:02 +01:00
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
return target.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// 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);
|
2016-02-23 05:47:02 +01:00
|
|
|
// Push the obtained code onto the stack
|
|
|
|
sq_pushinteger(vm, code);
|
|
|
|
// We have a value on the stack
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Session::CmdNoticeF(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-02-28 15:17:59 +01:00
|
|
|
// Obtain the initial stack size
|
2016-02-23 05:47:02 +01:00
|
|
|
const Int32 top = sq_gettop(vm);
|
2016-04-02 11:49:32 +02:00
|
|
|
// Do we have a target value?
|
2016-02-23 05:47:02 +01:00
|
|
|
if (top <= 1)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Missing the message target");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-23 05:47:02 +01:00
|
|
|
// Do we have a message value?
|
|
|
|
else if (top <= 2)
|
2016-04-02 11:49:32 +02:00
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Missing the message value");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
|
|
|
// 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());
|
|
|
|
}
|
2016-02-23 05:47:02 +01:00
|
|
|
// Do we have a valid session instance?
|
2016-04-02 11:49:32 +02:00
|
|
|
if (!session)
|
|
|
|
{
|
2016-02-23 05:47:02 +01:00
|
|
|
return sq_throwerror(vm, "Invalid session instance");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Do we have a valid session?
|
2016-04-02 11:49:32 +02:00
|
|
|
else if (!session->m_Session)
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return sq_throwerror(vm, "Invalid IRC session");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Is the session connected?
|
2016-04-02 11:49:32 +02:00
|
|
|
else if (!session->Connected())
|
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return sq_throwerror(vm, "Session is not connected");
|
2016-04-02 11:49:32 +02:00
|
|
|
}
|
|
|
|
// 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))
|
2016-02-23 05:47:02 +01:00
|
|
|
{
|
2016-04-02 11:49:32 +02:00
|
|
|
return target.mRes; // Propagate the error!
|
|
|
|
}
|
|
|
|
// 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);
|
2016-02-23 05:47:02 +01:00
|
|
|
// Push the obtained code onto the stack
|
|
|
|
sq_pushinteger(vm, code);
|
|
|
|
// We have a value on the stack
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
} // Namespace:: SqMod
|