ZNC trunk
Loading...
Searching...
No Matches
Message.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 ZNC, see the NOTICE file for details.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ZNC_MESSAGE_H
18#define ZNC_MESSAGE_H
19
20#ifdef SWIG
21#define ZNC_MSG_DEPRECATED(msg)
22#else
23#define ZNC_MSG_DEPRECATED(msg) __attribute__((deprecated(msg)))
24#endif
25
26#include <znc/zncconfig.h>
27#include <znc/ZNCString.h>
28#include <znc/Nick.h>
29#include <sys/time.h>
30
31class CChan;
32class CClient;
33class CIRCNetwork;
34
57class CMessage {
58 public:
59 explicit CMessage(const CString& sMessage = "");
60 CMessage(const CNick& Nick, const CString& sCommand,
61 const VCString& vsParams = VCString(),
62 const MCString& mssTags = MCString::EmptyMap);
63
64 enum class Type {
65 Unknown,
66 Account,
67 Action,
69 Away,
71 ChgHost,
72 CTCP,
73 Error,
74 Invite,
75 Join,
76 Kick,
77 Mode,
78 Nick,
79 Notice,
80 Numeric,
81 Part,
82 Ping,
83 Pong,
84 Quit,
85 TagMsg,
86 Text,
87 Topic,
88 Wallops,
89 };
90 Type GetType() const { return m_eType; }
91
92 bool Equals(const CMessage& Other) const;
93 void Clone(const CMessage& Other);
94
95 // ZNC <-> IRC
96 CIRCNetwork* GetNetwork() const { return m_pNetwork; }
97 void SetNetwork(CIRCNetwork* pNetwork) { m_pNetwork = pNetwork; }
98
99 // ZNC <-> CLI
100 CClient* GetClient() const { return m_pClient; }
101 void SetClient(CClient* pClient) { m_pClient = pClient; }
102
103 CChan* GetChan() const { return m_pChan; }
104 void SetChan(CChan* pChan) { m_pChan = pChan; }
105
106 CNick& GetNick() { return m_Nick; }
107 const CNick& GetNick() const { return m_Nick; }
108 void SetNick(const CNick& Nick) { m_Nick = Nick; }
109
110 const CString& GetCommand() const { return m_sCommand; }
111 void SetCommand(const CString& sCommand);
112
113 const VCString& GetParams() const { return m_vsParams; }
114
125 VCString GetParamsSplit(unsigned int uIdx, unsigned int uLen = -1) const;
126 void SetParams(const VCString& vsParams);
127 void SetParams(VCString&& vsParams);
128
130 CString GetParams(unsigned int uIdx, unsigned int uLen = -1) const
131 ZNC_MSG_DEPRECATED("Use GetParamsColon() instead") {
132 return GetParamsColon(uIdx, uLen);
133 }
134 CString GetParamsColon(unsigned int uIdx, unsigned int uLen = -1) const;
135
136 CString GetParam(unsigned int uIdx) const;
137 void SetParam(unsigned int uIdx, const CString& sParam);
138
139 const timeval& GetTime() const { return m_time; }
140 void SetTime(const timeval& ts) { m_time = ts; }
141
142 const MCString& GetTags() const { return m_mssTags; }
143 MCString& GetTags() { return m_mssTags; }
144 void SetTags(const MCString& mssTags) { m_mssTags = mssTags; }
145
146 CString GetTag(const CString& sKey) const;
147 void SetTag(const CString& sKey, const CString& sValue);
148
154
155 CString ToString(unsigned int uFlags = IncludeAll) const;
156 void Parse(const CString& sMessage);
157
158// Implicit and explicit conversion to a subclass reference.
159#ifndef SWIG
160 template <typename M>
161 M& As() & {
162 static_assert(std::is_base_of<CMessage, M>{},
163 "Must be subclass of CMessage");
164 static_assert(sizeof(M) == sizeof(CMessage),
165 "No data members allowed in CMessage subclasses.");
166 return static_cast<M&>(*this);
167 }
168
169 template <typename M>
170 const M& As() const& {
171 static_assert(std::is_base_of<CMessage, M>{},
172 "Must be subclass of CMessage");
173 static_assert(sizeof(M) == sizeof(CMessage),
174 "No data members allowed in CMessage subclasses.");
175 return static_cast<const M&>(*this);
176 }
177
178 template <typename M, typename = typename std::enable_if<
179 std::is_base_of<CMessage, M>{}>::type>
180 operator M&() & {
181 return As<M>();
182 }
183 template <typename M, typename = typename std::enable_if<
184 std::is_base_of<CMessage, M>{}>::type>
185 operator const M&() const& {
186 return As<M>();
187 }
188// REGISTER_ZNC_MESSAGE allows SWIG to instantiate correct .As<> calls.
189#define REGISTER_ZNC_MESSAGE(M)
190#else
191 // SWIG (as of 3.0.7) doesn't parse ref-qualifiers, and doesn't
192 // differentiate constness.
193 template <typename M>
194 M& As();
195#endif
196
197 private:
198 void InitTime();
199 void InitType();
200
201 CNick m_Nick;
202 CString m_sCommand;
203 VCString m_vsParams;
204 MCString m_mssTags;
205 timeval m_time;
206 CIRCNetwork* m_pNetwork = nullptr;
207 CClient* m_pClient = nullptr;
208 CChan* m_pChan = nullptr;
209 Type m_eType = Type::Unknown;
210 bool m_bColon = false;
211};
212
213// For gtest
214#ifdef GTEST_FAIL
215template <typename M, typename = typename std::enable_if<
216 std::is_base_of<CMessage, M>{}>::type>
217inline ::std::ostream& operator<<(::std::ostream& os, const M& msg) {
218 return os << msg.ToString().Escape_n(CString::EDEBUG);
219}
220#endif
221
222// The various CMessage subclasses are "mutable views" to the data held by
223// CMessage.
224// They provide convenient access to message type speficic attributes, but are
225// not
226// allowed to hold extra data of their own.
227class CTargetMessage : public CMessage {
228 public:
229 CString GetTarget() const { return GetParam(0); }
230 void SetTarget(const CString& sTarget) { SetParam(0, sTarget); }
231};
233
235 public:
236 CString GetText() const {
237 return GetParam(1).TrimPrefix_n("\001ACTION ").TrimSuffix_n("\001");
238 }
239 void SetText(const CString& sText) {
240 SetParam(1, "\001ACTION " + sText + "\001");
241 }
242};
244
246 public:
247 CString GetText() const { return GetParam(0); }
248 void SetText(const CString& sText) { SetParam(0, sText); }
249};
251
253 public:
254 bool IsReply() const { return GetCommand().Equals("NOTICE"); }
255 CString GetText() const {
256 return GetParam(1).TrimPrefix_n("\001").TrimSuffix_n("\001");
257 }
258 void SetText(const CString& sText) { SetParam(1, "\001" + sText + "\001"); }
259};
261
263 public:
264 CString GetKey() const { return GetParam(1); }
265 void SetKey(const CString& sKey) { SetParam(1, sKey); }
266};
268
270 public:
272 CString GetModes() const { return GetParamsColon(1).TrimPrefix_n(":"); }
273
274 CString GetModeList() const { return GetParam(1); };
275
276 VCString GetModeParams() const { return GetParamsSplit(2); };
277
278 bool HasModes() const { return !GetModeList().empty(); };
279};
281
282class CNickMessage : public CMessage {
283 public:
284 CString GetOldNick() const { return GetNick().GetNick(); }
285 CString GetNewNick() const { return GetParam(0); }
286 void SetNewNick(const CString& sNick) { SetParam(0, sNick); }
287};
289
291 public:
292 CString GetText() const { return GetParam(1); }
293 void SetText(const CString& sText) { SetParam(1, sText); }
294};
296
297class CNumericMessage : public CMessage {
298 public:
299 unsigned int GetCode() const { return GetCommand().ToUInt(); }
300};
302
304 public:
305 CString GetKickedNick() const { return GetParam(1); }
306 void SetKickedNick(const CString& sNick) { SetParam(1, sNick); }
307 CString GetReason() const { return GetParam(2); }
308 void SetReason(const CString& sReason) { SetParam(2, sReason); }
309 CString GetText() const { return GetReason(); }
310 void SetText(const CString& sText) { SetReason(sText); }
311};
313
314class CInviteMessage : public CMessage {
315 public:
316 CString GetInvitedNick() const { return GetParam(0); }
317 void SetInvitedNick(const CString& sNick) { SetParam(0, sNick); }
318 CString GetChannel() const { return GetParam(1); }
319 void SetChannel(const CString& sChannel) { SetParam(1, sChannel); }
320};
322
324 public:
325 CString GetReason() const { return GetParam(1); }
326 void SetReason(const CString& sReason) { SetParam(1, sReason); }
327 CString GetText() const { return GetReason(); }
328 void SetText(const CString& sText) { SetReason(sText); }
329};
331
332class CQuitMessage : public CMessage {
333 public:
334 CString GetReason() const { return GetParam(0); }
335 void SetReason(const CString& sReason) { SetParam(0, sReason); }
336 CString GetText() const { return GetReason(); }
337 void SetText(const CString& sText) { SetReason(sText); }
338};
340
342 public:
343 CString GetText() const { return GetParam(1); }
344 void SetText(const CString& sText) { SetParam(1, sText); }
345};
347
349 public:
350 CString GetTopic() const { return GetParam(1); }
351 void SetTopic(const CString& sTopic) { SetParam(1, sTopic); }
352 CString GetText() const { return GetTopic(); }
353 void SetText(const CString& sText) { SetTopic(sText); }
354};
356
357class CChgHostMessage : public CMessage {
358 public:
359 CString GetNewIdent() const { return GetParam(0); }
360 void SetNewIdent(const CString& sIdent) { SetParam(0, sIdent); }
361 CString GetNewHost() const { return GetParam(1); }
362 void SetNewHost(const CString& sHost) { SetParam(1, sHost); }
363};
365
366#endif // !ZNC_MESSAGE_H
#define REGISTER_ZNC_MESSAGE(M)
Definition Message.h:189
#define ZNC_MSG_DEPRECATED(msg)
Definition Message.h:23
std::vector< CString > VCString
Definition ZNCString.h:38
Definition Message.h:234
CString GetText() const
Definition Message.h:236
void SetText(const CString &sText)
Definition Message.h:239
Definition Message.h:245
void SetText(const CString &sText)
Definition Message.h:248
CString GetText() const
Definition Message.h:247
Definition Message.h:252
bool IsReply() const
Definition Message.h:254
CString GetText() const
Definition Message.h:255
void SetText(const CString &sText)
Definition Message.h:258
Definition Chan.h:35
Definition Message.h:357
void SetNewIdent(const CString &sIdent)
Definition Message.h:360
CString GetNewIdent() const
Definition Message.h:359
void SetNewHost(const CString &sHost)
Definition Message.h:362
CString GetNewHost() const
Definition Message.h:361
Definition Client.h:99
Definition IRCNetwork.h:40
Definition Message.h:314
void SetChannel(const CString &sChannel)
Definition Message.h:319
void SetInvitedNick(const CString &sNick)
Definition Message.h:317
CString GetInvitedNick() const
Definition Message.h:316
CString GetChannel() const
Definition Message.h:318
Definition Message.h:262
void SetKey(const CString &sKey)
Definition Message.h:265
CString GetKey() const
Definition Message.h:264
Definition Message.h:303
CString GetReason() const
Definition Message.h:307
void SetKickedNick(const CString &sNick)
Definition Message.h:306
CString GetKickedNick() const
Definition Message.h:305
void SetText(const CString &sText)
Definition Message.h:310
void SetReason(const CString &sReason)
Definition Message.h:308
CString GetText() const
Definition Message.h:309
Here is a small explanation of how messages on IRC work, and how you can use this class to get useful...
Definition Message.h:57
void SetTag(const CString &sKey, const CString &sValue)
MCString & GetTags()
Definition Message.h:143
FormatFlags
Definition Message.h:149
@ ExcludePrefix
Definition Message.h:151
@ ExcludeTags
Definition Message.h:152
@ IncludeAll
Definition Message.h:150
Type
Definition Message.h:64
const CNick & GetNick() const
Definition Message.h:107
void SetNick(const CNick &Nick)
Definition Message.h:108
void Clone(const CMessage &Other)
const VCString & GetParams() const
Definition Message.h:113
void SetClient(CClient *pClient)
Definition Message.h:101
CNick & GetNick()
Definition Message.h:106
void SetTime(const timeval &ts)
Definition Message.h:140
CIRCNetwork * GetNetwork() const
Definition Message.h:96
const timeval & GetTime() const
Definition Message.h:139
Type GetType() const
Definition Message.h:90
CString ToString(unsigned int uFlags=IncludeAll) const
const MCString & GetTags() const
Definition Message.h:142
CClient * GetClient() const
Definition Message.h:100
CString GetParamsColon(unsigned int uIdx, unsigned int uLen=-1) const
VCString GetParamsSplit(unsigned int uIdx, unsigned int uLen=-1) const
Get a subset of the message parameters.
bool Equals(const CMessage &Other) const
const M & As() const &
Definition Message.h:170
CString GetParam(unsigned int uIdx) const
CString GetParams(unsigned int uIdx, unsigned int uLen=-1) const ZNC_MSG_DEPRECATED("Use GetParamsColon() instead")
Definition Message.h:130
CString GetTag(const CString &sKey) const
void SetChan(CChan *pChan)
Definition Message.h:104
void SetCommand(const CString &sCommand)
CMessage(const CNick &Nick, const CString &sCommand, const VCString &vsParams=VCString(), const MCString &mssTags=MCString::EmptyMap)
const CString & GetCommand() const
Definition Message.h:110
void SetParams(VCString &&vsParams)
M & As() &
Definition Message.h:161
void SetTags(const MCString &mssTags)
Definition Message.h:144
CMessage(const CString &sMessage="")
void SetNetwork(CIRCNetwork *pNetwork)
Definition Message.h:97
void SetParams(const VCString &vsParams)
void SetParam(unsigned int uIdx, const CString &sParam)
void Parse(const CString &sMessage)
CChan * GetChan() const
Definition Message.h:103
Definition Message.h:269
bool HasModes() const
Definition Message.h:278
VCString GetModeParams() const
Definition Message.h:276
CString GetModeList() const
Definition Message.h:274
CString GetModes() const
Definition Message.h:272
Definition Message.h:282
void SetNewNick(const CString &sNick)
Definition Message.h:286
CString GetNewNick() const
Definition Message.h:285
CString GetOldNick() const
Definition Message.h:284
Definition Nick.h:29
const CString & GetNick() const
Definition Message.h:290
CString GetText() const
Definition Message.h:292
void SetText(const CString &sText)
Definition Message.h:293
Definition Message.h:297
unsigned int GetCode() const
Definition Message.h:299
Definition Message.h:323
void SetText(const CString &sText)
Definition Message.h:328
void SetReason(const CString &sReason)
Definition Message.h:326
CString GetReason() const
Definition Message.h:325
CString GetText() const
Definition Message.h:327
Definition Message.h:332
CString GetReason() const
Definition Message.h:334
void SetText(const CString &sText)
Definition Message.h:337
CString GetText() const
Definition Message.h:336
void SetReason(const CString &sReason)
Definition Message.h:335
String class that is used inside ZNC.
Definition ZNCString.h:68
unsigned int ToUInt() const
@ EDEBUG
Definition ZNCString.h:76
CString TrimSuffix_n(const CString &sSuffix) const
Trim a given suffix.
CString TrimPrefix_n(const CString &sPrefix=":") const
Trim a given prefix.
bool Equals(const CString &s, CaseSensitivity cs=CaseInsensitive) const
Check if this string is equal to some other string.
Definition Message.h:227
CString GetTarget() const
Definition Message.h:229
void SetTarget(const CString &sTarget)
Definition Message.h:230
Definition Message.h:341
void SetText(const CString &sText)
Definition Message.h:344
CString GetText() const
Definition Message.h:343
Definition Message.h:348
void SetTopic(const CString &sTopic)
Definition Message.h:351
CString GetTopic() const
Definition Message.h:350
void SetText(const CString &sText)
Definition Message.h:353
CString GetText() const
Definition Message.h:352
A dictionary for strings.
Definition ZNCString.h:595
static const MCString EmptyMap
A static instance of an empty map.
Definition ZNCString.h:606