diff options
author | Peter Powell <petpow@saberuk.com> | 2017-12-11 19:42:52 +0000 |
---|---|---|
committer | Peter Powell <petpow@saberuk.com> | 2018-01-06 14:18:21 +0000 |
commit | 2fcb5ff4389a9a82d253acdff02a388ddcf14653 (patch) | |
tree | 96feee81599adb7ef02bc35293daccba7071a6de /include | |
parent | 40514d0ba8279309f350a47652fffef745662926 (diff) |
Rework message handling.
- Move all message-related types to their own header to make moving
them to a cross-module events easier.
- Rename OnUserMessage to OnUserPostMessage.
- Rename OnText to OnUserMessage.
- Replace the dest, target_type, and status parameters with the
MessageTarget class.
- Replace the text, exempt_list, and msgtype parameters with the
MessageDetails struct.
- Add echooriginal and originaltext to the MessageDetails struct
to allow spam filtering to not be broken by cap echo-message.
Diffstat (limited to 'include')
-rw-r--r-- | include/inspircd.h | 1 | ||||
-rw-r--r-- | include/message.h | 127 | ||||
-rw-r--r-- | include/modules.h | 92 |
3 files changed, 157 insertions, 63 deletions
diff --git a/include/inspircd.h b/include/inspircd.h index bdbcdb20d..b0446482d 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -88,6 +88,7 @@ struct fakederef #include "socketengine.h" #include "snomasks.h" #include "filelogger.h" +#include "message.h" #include "modules.h" #include "threadengine.h" #include "configreader.h" diff --git a/include/message.h b/include/message.h new file mode 100644 index 000000000..fb9e7619f --- /dev/null +++ b/include/message.h @@ -0,0 +1,127 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2017 Peter Powell <petpow@saberuk.com> + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#pragma once + +/** Whether message was a PRIVMSG or a NOTICE. */ +enum MessageType +{ + /** The message is a PRIVMSG. */ + MSG_PRIVMSG, + + /** The message is a NOTICE. */ + MSG_NOTICE +}; + +class CoreExport MessageDetails +{ + public: + /* Whether to send the original message back to clients with echo-message support. */ + bool echooriginal; + + /** The users who are exempted from receiving this message. */ + CUList exemptions; + + /* The original message as sent by the user. */ + const std::string originaltext; + + /** The message which will be sent to clients. */ + std::string text; + + /** The type of message. */ + const MessageType type; + + MessageDetails(MessageType mt, const std::string& msg) + : echooriginal(false) + , originaltext(msg) + , text(msg) + , type(mt) + { + } +}; + +/** Represents the target of a message (NOTICE, PRIVMSG, etc). */ +class CoreExport MessageTarget +{ + public: + /** An enumeration of possible message target types. */ + enum TargetType + { + /** The target of the message is a user. */ + TYPE_USER, + + /** The target of the message is a channel. */ + TYPE_CHANNEL, + + /** The target of the message is a server. */ + TYPE_SERVER + }; + + private: + /** The target of the message. */ + void* dest; + + public: + /** If type is TYPE_CHANNEL and the user specified a status rank. */ + char status; + + /** The type of the target of the message. If this is TYPE_CHANNEL then dest + * is a Channel*, TYPE_USER then dest is a User*, and TYPE_SERVER then dest is + * a std::string* containing a server glob. + */ + MessageTarget::TargetType type; + + /** Initialises a new channel message target. + * @param channel The channel which is the target of the message. + * @param statuschar The lowest status rank that the message is being sent to. + */ + MessageTarget(Channel* channel, char statuschar) + : dest(channel) + , status(statuschar) + , type(TYPE_CHANNEL) + { + } + + /** Initialises a new user message target. + * @param user The user which is the target of the message. + */ + MessageTarget(User* user) + : dest(user) + , status(0) + , type(TYPE_USER) + { + } + + /** Initialises a new server message target. + * @param server The server glob which is the target of the message. + */ + MessageTarget(std::string* server) + : dest(server) + , status(0) + , type(TYPE_SERVER) + { + } + + /** Retrieves the target of this message. */ + template<typename T> + T* Get() const + { + return static_cast<T*>(dest); + } +}; diff --git a/include/modules.h b/include/modules.h index 5dee6bfb6..c6b10cad9 100644 --- a/include/modules.h +++ b/include/modules.h @@ -54,22 +54,6 @@ enum ModuleFlags VF_OPTCOMMON = 8 }; -/** Used to represent an event type, for user, channel or server - */ -enum TargetTypeFlags { - TYPE_USER = 1, - TYPE_CHANNEL, - TYPE_SERVER, - TYPE_OTHER -}; - -/** Used to represent wether a message was PRIVMSG or NOTICE - */ -enum MessageType { - MSG_PRIVMSG = 0, - MSG_NOTICE = 1 -}; - #define MOD_RES_ALLOW (ModResult(1)) #define MOD_RES_PASSTHRU (ModResult(0)) #define MOD_RES_DENY (ModResult(-1)) @@ -230,7 +214,7 @@ enum Implementation I_OnUserConnect, I_OnUserQuit, I_OnUserDisconnect, I_OnUserJoin, I_OnUserPart, I_OnSendSnotice, I_OnUserPreJoin, I_OnUserPreKick, I_OnUserKick, I_OnOper, I_OnInfo, I_OnUserPreInvite, I_OnUserInvite, I_OnUserPreMessage, I_OnUserPreNick, - I_OnUserMessage, I_OnMode, + I_OnUserPostMessage, I_OnMode, I_OnDecodeMetaData, I_OnAcceptConnection, I_OnUserInit, I_OnChangeHost, I_OnChangeName, I_OnAddLine, I_OnDelLine, I_OnExpireLine, I_OnUserPostNick, I_OnPreMode, I_On005Numeric, I_OnKill, I_OnLoadModule, @@ -241,7 +225,7 @@ enum Implementation I_OnChangeLocalUserGECOS, I_OnUserRegister, I_OnChannelPreDelete, I_OnChannelDelete, I_OnPostOper, I_OnSetAway, I_OnPostCommand, I_OnPostJoin, I_OnBuildNeighborList, I_OnGarbageCollect, I_OnSetConnectClass, - I_OnText, I_OnPassCompare, I_OnNamesListItem, I_OnNumeric, + I_OnUserMessage, I_OnPassCompare, I_OnNamesListItem, I_OnNumeric, I_OnPreRehash, I_OnModuleRehash, I_OnSendWhoLine, I_OnChangeIdent, I_OnSetUserIP, I_OnServiceAdd, I_OnServiceDel, I_END @@ -500,24 +484,16 @@ class CoreExport Module : public classbase, public usecountbase */ virtual void OnUserInvite(User* source, User* dest, Channel* channel, time_t timeout, unsigned int notifyrank, CUList& notifyexcepts); - /** Called whenever a user is about to PRIVMSG A user or a channel, before any processing is done. - * Returning any nonzero value from this function stops the process immediately, causing no - * output to be sent to the user by the core. If you do this you must produce your own numerics, - * notices etc. This is useful for modules which may want to filter or redirect messages. - * target_type can be one of TYPE_USER or TYPE_CHANNEL. If the target_type value is a user, - * you must cast dest to a User* otherwise you must cast it to a Channel*, this is the details - * of where the message is destined to be sent. - * @param user The user sending the message - * @param dest The target of the message (Channel* or User*) - * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL) - * @param text Changeable text being sent by the user - * @param status The status being used, e.g. PRIVMSG @#chan has status== '@', 0 to send to everyone. - * @param exempt_list A list of users not to send to. For channel messages, this will usually contain just the sender. - * It will be ignored for private messages. - * @param msgtype The message type, MSG_PRIVMSG for PRIVMSGs, MSG_NOTICE for NOTICEs - * @return 1 to deny the message, 0 to allow it - */ - virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list, MessageType msgtype); + /** Called before a user sends a message to a channel, a user, or a server glob mask. + * @param user The user sending the message. + * @param target The target of the message. This can either be a channel, a user, or a server + * glob mask. + * @param details Details about the message such as the message text and type. See the + * MessageDetails class for more information. + * @return MOD_RES_ALLOW to explicitly allow the message, MOD_RES_DENY to explicitly deny the + * message, or MOD_RES_PASSTHRU to let another module handle the event. + */ + virtual ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details); /** Called when sending a message to all "neighbors" of a given user - * that is, all users that share a common channel. This is used in @@ -539,33 +515,23 @@ class CoreExport Module : public classbase, public usecountbase */ virtual ModResult OnUserPreNick(LocalUser* user, const std::string& newnick); - /** Called after any PRIVMSG sent from a user. - * The dest variable contains a User* if target_type is TYPE_USER and a Channel* - * if target_type is TYPE_CHANNEL. - * @param user The user sending the message - * @param dest The target of the message - * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL) - * @param text the text being sent by the user - * @param status The status being used, e.g. PRIVMSG @#chan has status== '@', 0 to send to everyone. - * @param exempt_list A list of users to not send to. - * @param msgtype The message type, MSG_PRIVMSG for PRIVMSGs, MSG_NOTICE for NOTICEs - */ - virtual void OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list, MessageType msgtype); - - /** Called immediately before any NOTICE or PRIVMSG sent from a user, local or remote. - * The dest variable contains a User* if target_type is TYPE_USER and a Channel* - * if target_type is TYPE_CHANNEL. - * The difference between this event and OnUserPreMessage is that delivery is gauranteed, - * the message has already been vetted. In the case of the other two methods, a later module may stop your - * message. This also differs from OnUserMessage which occurs AFTER the message has been sent. - * @param user The user sending the message - * @param dest The target of the message - * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL) - * @param text the text being sent by the user - * @param status The status being used, e.g. NOTICE @#chan has status== '@', 0 to send to everyone. - * @param exempt_list A list of users not to send to. For channel messages, this will usually contain just the sender. - */ - virtual void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list); + /** Called immediately after a user sends a message to a channel, a user, or a server glob mask. + * @param user The user sending the message. + * @param target The target of the message. This can either be a channel, a user, or a server + * glob mask. + * @param details Details about the message such as the message text and type. See the + * MessageDetails class for more information. + */ + virtual void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details); + + /** Called immediately before a user sends a message to a channel, a user, or a server glob mask. + * @param user The user sending the message. + * @param target The target of the message. This can either be a channel, a user, or a server + * glob mask. + * @param details Details about the message such as the message text and type. See the + * MessageDetails class for more information. + */ + virtual void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details); /** Called after every MODE command sent from a user * Either the usertarget or the chantarget variable contains the target of the modes, |