]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/message.h
Update copyright headers.
[user/henk/code/inspircd.git] / include / message.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2017-2018 Attila Molnar <attilamolnar@hush.com>
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          /** The users who are exempted from receiving this message. */
43         CUList exemptions;
44
45         /* The original message as sent by the user. */
46         const std::string original_text;
47
48         /** IRCv3 message tags sent to the server by the user. */
49         const ClientProtocol::TagMap tags_in;
50
51         /** IRCv3 message tags sent out to users who get this message. */
52         ClientProtocol::TagMap tags_out;
53
54         /** The message which will be sent to clients. */
55         std::string text;
56
57         /** The type of message. */
58         const MessageType type;
59
60         /** Determines whether the specified message is a CTCP. If the specified message
61          * is a CTCP then the CTCP name and CTCP body are extracted and stored in the
62          * name and body references.
63          * @param name The location to store the parsed CTCP name.
64          * @param body The location to store the parsed CTCP body.
65          */
66         virtual bool IsCTCP(std::string& name, std::string& body) const = 0;
67
68         /** Determines whether the specified message is a CTCP. If the specified message
69          * is a CTCP then the CTCP name is extracted and stored in the name reference.
70          * @param name The location to store the parsed CTCP name.
71          */
72         virtual bool IsCTCP(std::string& name) const = 0;
73
74         /** Determines whether the specified message is a CTCP. */
75         virtual bool IsCTCP() const = 0;
76
77  protected:
78         MessageDetails(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
79                 : echo(true)
80                 , echo_original(false)
81                 , original_text(msg)
82                 , tags_in(tags)
83                 , text(msg)
84                 , type(mt)
85         {
86         }
87 };
88
89 /** Represents the target of a message (NOTICE, PRIVMSG, etc). */
90 class CoreExport MessageTarget
91 {
92  public:
93         /** An enumeration of possible message target types. */
94         enum TargetType
95         {
96                 /** The target of the message is a user. */
97                 TYPE_USER,
98
99                 /** The target of the message is a channel. */
100                 TYPE_CHANNEL,
101
102                 /** The target of the message is a server. */
103                 TYPE_SERVER
104         };
105
106  private:
107         /** The target of the message. */
108         void* dest;
109
110  public:
111         /** If type is TYPE_CHANNEL and the user specified a status rank. */
112         char status;
113
114         /** The type of the target of the message. If this is TYPE_CHANNEL then dest
115          * is a Channel*, TYPE_USER then dest is a User*, and TYPE_SERVER then dest is
116          * a std::string* containing a server glob.
117          */
118         MessageTarget::TargetType type;
119
120         /** Initialises a new channel message target.
121          * @param channel The channel which is the target of the message.
122          * @param statuschar The lowest status rank that the message is being sent to.
123          */
124         MessageTarget(Channel* channel, char statuschar)
125                 : dest(channel)
126                 , status(statuschar)
127                 , type(TYPE_CHANNEL)
128         {
129         }
130
131         /** Initialises a new user message target.
132          * @param user The user which is the target of the message.
133          */
134         MessageTarget(User* user)
135                 : dest(user)
136                 , status(0)
137                 , type(TYPE_USER)
138         {
139         }
140
141         /** Initialises a new server message target.
142          * @param server The server glob which is the target of the message.
143          */
144         MessageTarget(std::string* server)
145                 : dest(server)
146                 , status(0)
147                 , type(TYPE_SERVER)
148         {
149         }
150
151         /** Retrieves the target of this message. */
152         template<typename T>
153         T* Get() const
154         {
155                 return static_cast<T*>(dest);
156         }
157
158         /** Retrieves the name of the target of this message. */
159         const std::string& GetName() const
160         {
161                 switch (type)
162                 {
163                         case TYPE_CHANNEL:
164                                 return Get<Channel>()->name;
165                         case TYPE_USER:
166                                 return Get<User>()->nick;
167                         case TYPE_SERVER:
168                                 return *Get<std::string>();
169                 }
170
171                 // We should never reach this point during a normal execution but
172                 // handle it just in case.
173                 static const std::string target = "*";
174                 return target;
175         }
176 };