]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/message.h
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / include / message.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #pragma once
22
23 /** Whether message was a PRIVMSG or a NOTICE. */
24 enum MessageType
25 {
26         /** The message is a PRIVMSG. */
27         MSG_PRIVMSG,
28
29         /** The message is a NOTICE. */
30         MSG_NOTICE
31 };
32
33 class CoreExport MessageDetails
34 {
35  public:
36         /** Whether to echo the message at all. */
37         bool echo;
38
39         /* Whether to send the original message back to clients with echo-message support. */
40         bool echo_original;
41
42         /** Whether to update the source user's idle time. */
43         bool update_idle;
44
45         /** The users who are exempted from receiving this message. */
46         CUList exemptions;
47
48         /* The original message as sent by the user. */
49         const std::string original_text;
50
51         /** IRCv3 message tags sent to the server by the user. */
52         const ClientProtocol::TagMap tags_in;
53
54         /** IRCv3 message tags sent out to users who get this message. */
55         ClientProtocol::TagMap tags_out;
56
57         /** The message which will be sent to clients. */
58         std::string text;
59
60         /** The type of message. */
61         MessageType type;
62
63         /** Determines whether the specified message is a CTCP. If the specified message
64          * is a CTCP then the CTCP name and CTCP body are extracted and stored in the
65          * name and body references.
66          * @param name The location to store the parsed CTCP name.
67          * @param body The location to store the parsed CTCP body.
68          */
69         virtual bool IsCTCP(std::string& name, std::string& body) const = 0;
70
71         /** Determines whether the specified message is a CTCP. If the specified message
72          * is a CTCP then the CTCP name is extracted and stored in the name reference.
73          * @param name The location to store the parsed CTCP name.
74          */
75         virtual bool IsCTCP(std::string& name) const = 0;
76
77         /** Determines whether the specified message is a CTCP. */
78         virtual bool IsCTCP() const = 0;
79
80  protected:
81         MessageDetails(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
82                 : echo(true)
83                 , echo_original(false)
84                 , update_idle(true)
85                 , original_text(msg)
86                 , tags_in(tags)
87                 , text(msg)
88                 , type(mt)
89         {
90         }
91 };
92
93 /** Represents the target of a message (NOTICE, PRIVMSG, etc). */
94 class CoreExport MessageTarget
95 {
96  public:
97         /** An enumeration of possible message target types. */
98         enum TargetType
99         {
100                 /** The target of the message is a user. */
101                 TYPE_USER,
102
103                 /** The target of the message is a channel. */
104                 TYPE_CHANNEL,
105
106                 /** The target of the message is a server. */
107                 TYPE_SERVER
108         };
109
110  private:
111         /** The target of the message. */
112         void* dest;
113
114  public:
115         /** If type is TYPE_CHANNEL and the user specified a status rank. */
116         char status;
117
118         /** The type of the target of the message. If this is TYPE_CHANNEL then dest
119          * is a Channel*, TYPE_USER then dest is a User*, and TYPE_SERVER then dest is
120          * a std::string* containing a server glob.
121          */
122         MessageTarget::TargetType type;
123
124         /** Initialises a new channel message target.
125          * @param channel The channel which is the target of the message.
126          * @param statuschar The lowest status rank that the message is being sent to.
127          */
128         MessageTarget(Channel* channel, char statuschar)
129                 : dest(channel)
130                 , status(statuschar)
131                 , type(TYPE_CHANNEL)
132         {
133         }
134
135         /** Initialises a new user message target.
136          * @param user The user which is the target of the message.
137          */
138         MessageTarget(User* user)
139                 : dest(user)
140                 , status(0)
141                 , type(TYPE_USER)
142         {
143         }
144
145         /** Initialises a new server message target.
146          * @param server The server glob which is the target of the message.
147          */
148         MessageTarget(std::string* server)
149                 : dest(server)
150                 , status(0)
151                 , type(TYPE_SERVER)
152         {
153         }
154
155         /** Retrieves the target of this message. */
156         template<typename T>
157         T* Get() const
158         {
159                 return static_cast<T*>(dest);
160         }
161
162         /** Retrieves the name of the target of this message. */
163         const std::string& GetName() const
164         {
165                 switch (type)
166                 {
167                         case TYPE_CHANNEL:
168                                 return Get<Channel>()->name;
169                         case TYPE_USER:
170                                 return Get<User>()->nick;
171                         case TYPE_SERVER:
172                                 return *Get<std::string>();
173                 }
174
175                 // We should never reach this point during a normal execution but
176                 // handle it just in case.
177                 static const std::string target = "*";
178                 return target;
179         }
180 };