From da5e83151c7f310611aca03d0e62ca12c0d5626b Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Tue, 24 May 2016 21:36:02 +0300 Subject: [PATCH] Switch to identifier based events in IRC session to be consistent with the main plugin events. --- modules/irc/Common.cpp | 32 ++++ modules/irc/Common.hpp | 49 ++++++ modules/irc/Module.cpp | 84 ++++++----- modules/irc/Session.cpp | 59 +++++++- modules/irc/Session.hpp | 325 ++-------------------------------------- 5 files changed, 196 insertions(+), 353 deletions(-) diff --git a/modules/irc/Common.cpp b/modules/irc/Common.cpp index a1858de2..ecf46590 100644 --- a/modules/irc/Common.cpp +++ b/modules/irc/Common.cpp @@ -175,6 +175,38 @@ StackStrF::~StackStrF() } } +// ------------------------------------------------------------------------------------------------ +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) { diff --git a/modules/irc/Common.hpp b/modules/irc/Common.hpp index 1614cb0e..21f3a8e6 100644 --- a/modules/irc/Common.hpp +++ b/modules/irc/Common.hpp @@ -140,6 +140,55 @@ struct StackStrF StackStrF & operator = (StackStrF && o) = delete; }; +/* ------------------------------------------------------------------------------------------------ + * Types of events that the session emits. +*/ +enum SessionEvent +{ + SET_CONNECT = 0, + SET_NICK, + SET_QUIT, + SET_JOIN, + SET_PART, + SET_MODE, + SET_UMODE, + SET_TOPIC, + SET_KICK, + SET_CHANNEL, + SET_PRIVMSG, + SET_NOTICE, + SET_CHANNELNOTICE, + SET_INVITE, + SET_CTCPREQ, + SET_CTCPREP, + SET_CTCPACTION, + SET_UNKNOWN, + SET_NUMERIC, + SET_DCCCHATREQ, + SET_DCCSENDREQ, + SET_MAX +}; + +/* ------------------------------------------------------------------------------------------------ + * Retrieve a reference to a null script object. +*/ +Object & NullObject(); + +/* ------------------------------------------------------------------------------------------------ + * 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. */ diff --git a/modules/irc/Module.cpp b/modules/irc/Module.cpp index d2d51079..a090aa70 100644 --- a/modules/irc/Module.cpp +++ b/modules/irc/Module.cpp @@ -98,6 +98,11 @@ void OnSquirrelTerminate() OutputMessage("Terminating: %s", SQIRC_NAME); // Terminate all session and release script resources Session::Terminate(); + // Release null objects just in case + NullObject().Release(); + NullTable().Release(); + NullArray().Release(); + NullFunction().ReleaseGently(); // Release the current virtual machine, if any DefaultVM::Set(nullptr); } @@ -243,6 +248,24 @@ void RegisterAPI(HSQUIRRELVM vm) .Prop(_SC("OnDccChatReq"), &Session::GetOnDccChatReq) .Prop(_SC("OnDccSendReq"), &Session::GetOnDccSendReq) // Member Methods + .Func(_SC("Bind"), &Session::BindEvent) + .Func(_SC("Disconnect"), &Session::Disconnect) + .Func(_SC("CmdPart"), &Session::CmdPart) + .Func(_SC("CmdInvite"), &Session::CmdInvite) + .Func(_SC("CmdNames"), &Session::CmdNames) + .Func(_SC("CmdMsg"), &Session::CmdMsg) + .Func(_SC("CmdMe"), &Session::CmdMe) + .Func(_SC("CmdNotice"), &Session::CmdNotice) + .Func(_SC("CmdCtcpRequest"), &Session::CmdCtcpRequest) + .Func(_SC("CmdCtcpReply"), &Session::CmdCtcpReply) + .Func(_SC("CmdNick"), &Session::CmdNick) + .Func(_SC("CmdWhois"), &Session::CmdWhois) + .Func(_SC("SendRaw"), &Session::SendRaw) + .Func(_SC("DestroyDcc"), &Session::DestroyDcc) + .Func(_SC("SetCtcpVersion"), &Session::SetCtcpVersion) + .Func(_SC("SetOption"), &Session::SetOption) + .Func(_SC("ResetOption"), &Session::ResetOption) + // Member Overloads .Overload< Int32 (Session::*)(void) >(_SC("Connect"), &Session::Connect) .Overload< Int32 (Session::*)(CSStr, Uint32, CSStr) >(_SC("Connect"), &Session::Connect) .Overload< Int32 (Session::*)(CSStr, Uint32, CSStr, CSStr) >(_SC("Connect"), &Session::Connect) @@ -253,12 +276,8 @@ void RegisterAPI(HSQUIRRELVM vm) .Overload< Int32 (Session::*)(CSStr, Uint32, CSStr, CSStr) >(_SC("Connect6"), &Session::Connect6) .Overload< Int32 (Session::*)(CSStr, Uint32, CSStr, CSStr, CSStr) >(_SC("Connect6"), &Session::Connect6) .Overload< Int32 (Session::*)(CSStr, Uint32, CSStr, CSStr, CSStr, CSStr) >(_SC("Connect6"), &Session::Connect6) - .Func(_SC("Disconnect"), &Session::Disconnect) .Overload< Int32 (Session::*)(CSStr) >(_SC("CmdJoin"), &Session::CmdJoin) .Overload< Int32 (Session::*)(CSStr, CSStr) >(_SC("CmdJoin"), &Session::CmdJoin) - .Func(_SC("CmdPart"), &Session::CmdPart) - .Func(_SC("CmdInvite"), &Session::CmdInvite) - .Func(_SC("CmdNames"), &Session::CmdNames) .Overload< Int32 (Session::*)(void) >(_SC("CmdList"), &Session::CmdList) .Overload< Int32 (Session::*)(CSStr) >(_SC("CmdList"), &Session::CmdList) .Overload< Int32 (Session::*)(CSStr) >(_SC("CmdTopic"), &Session::CmdTopic) @@ -269,41 +288,8 @@ void RegisterAPI(HSQUIRRELVM vm) .Overload< Int32 (Session::*)(CSStr) >(_SC("CmdUserMode"), &Session::CmdUserMode) .Overload< Int32 (Session::*)(CSStr, CSStr) >(_SC("CmdKick"), &Session::CmdKick) .Overload< Int32 (Session::*)(CSStr, CSStr, CSStr) >(_SC("CmdKick"), &Session::CmdKick) - .Func(_SC("CmdMsg"), &Session::CmdMsg) - .Func(_SC("CmdMe"), &Session::CmdMe) - .Func(_SC("CmdNotice"), &Session::CmdNotice) - .Func(_SC("CmdCtcpRequest"), &Session::CmdCtcpRequest) - .Func(_SC("CmdCtcpReply"), &Session::CmdCtcpReply) - .Func(_SC("CmdNick"), &Session::CmdNick) - .Func(_SC("CmdWhois"), &Session::CmdWhois) .Overload< Int32 (Session::*)(void) >(_SC("CmdQuit"), &Session::CmdQuit) .Overload< Int32 (Session::*)(CSStr) >(_SC("CmdQuit"), &Session::CmdQuit) - .Func(_SC("SendRaw"), &Session::SendRaw) - .Func(_SC("DestroyDcc"), &Session::DestroyDcc) - .Func(_SC("SetCtcpVersion"), &Session::SetCtcpVersion) - .Func(_SC("SetOption"), &Session::SetOption) - .Func(_SC("ResetOption"), &Session::ResetOption) - .Func(_SC("BindConnect"), &Session::BindOnConnect) - .Func(_SC("BindNick"), &Session::BindOnNick) - .Func(_SC("BindQuit"), &Session::BindOnQuit) - .Func(_SC("BindJoin"), &Session::BindOnJoin) - .Func(_SC("BindPart"), &Session::BindOnPart) - .Func(_SC("BindMode"), &Session::BindOnMode) - .Func(_SC("BindUmode"), &Session::BindOnUmode) - .Func(_SC("BindTopic"), &Session::BindOnTopic) - .Func(_SC("BindKick"), &Session::BindOnKick) - .Func(_SC("BindChannel"), &Session::BindOnChannel) - .Func(_SC("BindPrivMsg"), &Session::BindOnPrivMsg) - .Func(_SC("BindNotice"), &Session::BindOnNotice) - .Func(_SC("BindChannelNotice"), &Session::BindOnChannelNotice) - .Func(_SC("BindInvite"), &Session::BindOnInvite) - .Func(_SC("BindCtcpReq"), &Session::BindOnCtcpReq) - .Func(_SC("BindCtcpRep"), &Session::BindOnCtcpRep) - .Func(_SC("BindCtcpAction"), &Session::BindOnCtcpAction) - .Func(_SC("BindUnknown"), &Session::BindOnUnknown) - .Func(_SC("BindNumeric"), &Session::BindOnNumeric) - .Func(_SC("BindDccChatReq"), &Session::BindOnDccChatReq) - .Func(_SC("BindDccSendReq"), &Session::BindOnDccSendReq) // Squirrel Methods .SquirrelFunc(_SC("CmdMsgF"), &Session::CmdMsgF) .SquirrelFunc(_SC("CmdMeF"), &Session::CmdMeF) @@ -316,6 +302,30 @@ void RegisterAPI(HSQUIRRELVM vm) RootTable(vm).Bind(_SC("SqIRC"), ircns); + Sqrat::ConstTable(vm).Enum(_SC("SqIrcEvent"), Sqrat::Enumeration(vm) + .Const(_SC("Connect"), SET_CONNECT) + .Const(_SC("Nick"), SET_NICK) + .Const(_SC("Quit"), SET_QUIT) + .Const(_SC("Join"), SET_JOIN) + .Const(_SC("Part"), SET_PART) + .Const(_SC("Mode"), SET_MODE) + .Const(_SC("Umode"), SET_UMODE) + .Const(_SC("Topic"), SET_TOPIC) + .Const(_SC("Kick"), SET_KICK) + .Const(_SC("Channel"), SET_CHANNEL) + .Const(_SC("PrivMsg"), SET_PRIVMSG) + .Const(_SC("Notice"), SET_NOTICE) + .Const(_SC("ChannelNotice"), SET_CHANNELNOTICE) + .Const(_SC("Invite"), SET_INVITE) + .Const(_SC("CtcpReq"), SET_CTCPREQ) + .Const(_SC("CtcpRep"), SET_CTCPREP) + .Const(_SC("CtcpAction"), SET_CTCPACTION) + .Const(_SC("Unknown"), SET_UNKNOWN) + .Const(_SC("Numeric"), SET_NUMERIC) + .Const(_SC("DccChatReq"), SET_DCCCHATREQ) + .Const(_SC("DccSendReq"), SET_DCCSENDREQ) + ); + Sqrat::ConstTable(vm).Enum(_SC("SqIrcErr"), Sqrat::Enumeration(vm) .Const(_SC("Ok"), LIBIRC_ERR_OK) .Const(_SC("InVal"), LIBIRC_ERR_INVAL) diff --git a/modules/irc/Session.cpp b/modules/irc/Session.cpp index 9c9bea4f..8f6b8aa5 100644 --- a/modules/irc/Session.cpp +++ b/modules/irc/Session.cpp @@ -259,7 +259,7 @@ bool Session::ValidateEventSession(Session * 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"); + _SqMod->LogErr("Cannot forward IRC event without a session instance"); // Invalid session instance return false; } @@ -332,6 +332,63 @@ Session::~Session() } } +// ------------------------------------------------------------------------------------------------ +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()); + } +} + // ------------------------------------------------------------------------------------------------ void Session::SetNick(CSStr nick) { diff --git a/modules/irc/Session.hpp b/modules/irc/Session.hpp index c6aec67a..7ad6bb3d 100644 --- a/modules/irc/Session.hpp +++ b/modules/irc/Session.hpp @@ -269,6 +269,16 @@ public: m_Data = data; } + /* -------------------------------------------------------------------------------------------- + * Retrieve an event supported by this session. + */ + Function & GetEvent(Int32 evid); + + /* -------------------------------------------------------------------------------------------- + * Bind to an event supported by this session. + */ + void BindEvent(Int32 evid, Object & env, Function & func); + /* -------------------------------------------------------------------------------------------- * Retrieve the server address. */ @@ -1054,21 +1064,6 @@ public: return m_OnConnect; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnConnect(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnConnect.ReleaseGently(); - } - else - { - m_OnConnect = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1077,21 +1072,6 @@ public: return m_OnNick; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnNick(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnNick.ReleaseGently(); - } - else - { - m_OnNick = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1100,21 +1080,6 @@ public: return m_OnQuit; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnQuit(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnQuit.ReleaseGently(); - } - else - { - m_OnQuit = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1123,21 +1088,6 @@ public: return m_OnJoin; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnJoin(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnJoin.ReleaseGently(); - } - else - { - m_OnJoin = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1146,21 +1096,6 @@ public: return m_OnPart; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnPart(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnPart.ReleaseGently(); - } - else - { - m_OnPart = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1169,21 +1104,6 @@ public: return m_OnMode; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnMode(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnMode.ReleaseGently(); - } - else - { - m_OnMode = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1192,21 +1112,6 @@ public: return m_OnUmode; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnUmode(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnUmode.ReleaseGently(); - } - else - { - m_OnUmode = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1215,21 +1120,6 @@ public: return m_OnTopic; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnTopic(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnTopic.ReleaseGently(); - } - else - { - m_OnTopic = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1238,21 +1128,6 @@ public: return m_OnKick; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnKick(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnKick.ReleaseGently(); - } - else - { - m_OnKick = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1261,21 +1136,6 @@ public: return m_OnChannel; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnChannel(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnChannel.ReleaseGently(); - } - else - { - m_OnChannel = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1284,21 +1144,6 @@ public: return m_OnPrivMsg; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnPrivMsg(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnPrivMsg.ReleaseGently(); - } - else - { - m_OnPrivMsg = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1307,21 +1152,6 @@ public: return m_OnNotice; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnNotice(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnNotice.ReleaseGently(); - } - else - { - m_OnNotice = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1330,21 +1160,6 @@ public: return m_OnChannelNotice; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnChannelNotice(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnChannelNotice.ReleaseGently(); - } - else - { - m_OnChannelNotice = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1353,21 +1168,6 @@ public: return m_OnInvite; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnInvite(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnInvite.ReleaseGently(); - } - else - { - m_OnInvite = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1376,21 +1176,6 @@ public: return m_OnCtcpReq; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnCtcpReq(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnCtcpReq.ReleaseGently(); - } - else - { - m_OnCtcpReq = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1399,21 +1184,6 @@ public: return m_OnCtcpRep; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnCtcpRep(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnCtcpRep.ReleaseGently(); - } - else - { - m_OnCtcpRep = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1422,21 +1192,6 @@ public: return m_OnCtcpAction; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnCtcpAction(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnCtcpAction.ReleaseGently(); - } - else - { - m_OnCtcpAction = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1445,21 +1200,6 @@ public: return m_OnUnknown; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnUnknown(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnUnknown.ReleaseGently(); - } - else - { - m_OnUnknown = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1468,21 +1208,6 @@ public: return m_OnNumeric; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnNumeric(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnNumeric.ReleaseGently(); - } - else - { - m_OnNumeric = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1491,21 +1216,6 @@ public: return m_OnDccChatReq; } - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnDccChatReq(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnDccChatReq.ReleaseGently(); - } - else - { - m_OnDccChatReq = Function(env.GetVM(), env, func.GetFunc()); - } - } - /* -------------------------------------------------------------------------------------------- * ... */ @@ -1513,21 +1223,6 @@ public: { return m_OnDccSendReq; } - - /* -------------------------------------------------------------------------------------------- - * ... - */ - void BindOnDccSendReq(Object & env, Function & func) - { - if (func.IsNull()) - { - m_OnDccSendReq.ReleaseGently(); - } - else - { - m_OnDccSendReq = Function(env.GetVM(), env, func.GetFunc()); - } - } }; } // Namespace:: SqMod