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 };
98 Type GetType() const { return m_eType; }
99
100 bool Equals(const CMessage& Other) const;
101 void Clone(const CMessage& Other);
102
103 // ZNC <-> IRC
104 CIRCNetwork* GetNetwork() const { return m_pNetwork; }
105 void SetNetwork(CIRCNetwork* pNetwork) { m_pNetwork = pNetwork; }
106
107 // ZNC <-> CLI
108 CClient* GetClient() const { return m_pClient; }
109 void SetClient(CClient* pClient) { m_pClient = pClient; }
110
111 CChan* GetChan() const { return m_pChan; }
112 void SetChan(CChan* pChan) { m_pChan = pChan; }
113
114 CNick& GetNick() { return m_Nick; }
115 const CNick& GetNick() const { return m_Nick; }
116 void SetNick(const CNick& Nick) { m_Nick = Nick; }
117
118 const CString& GetCommand() const { return m_sCommand; }
119 void SetCommand(const CString& sCommand);
120
121 const VCString& GetParams() const { return m_vsParams; }
122
133 VCString GetParamsSplit(unsigned int uIdx, unsigned int uLen = -1) const;
134 void SetParams(const VCString& vsParams);
135 void SetParams(VCString&& vsParams);
136
138 CString GetParams(unsigned int uIdx, unsigned int uLen = -1) const
139 ZNC_MSG_DEPRECATED("Use GetParamsColon() instead") {
140 return GetParamsColon(uIdx, uLen);
141 }
142 CString GetParamsColon(unsigned int uIdx, unsigned int uLen = -1) const;
143
144 CString GetParam(unsigned int uIdx) const;
145 void SetParam(unsigned int uIdx, const CString& sParam);
146
147 const timeval& GetTime() const { return m_time; }
148 void SetTime(const timeval& ts) { m_time = ts; }
149
150 const MCString& GetTags() const { return m_mssTags; }
151 MCString& GetTags() { return m_mssTags; }
152 void SetTags(const MCString& mssTags) { m_mssTags = mssTags; }
153
154 CString GetTag(const CString& sKey) const;
155 void SetTag(const CString& sKey, const CString& sValue);
156
162
163 CString ToString(unsigned int uFlags = IncludeAll) const;
164 void Parse(const CString& sMessage);
165
166// Implicit and explicit conversion to a subclass reference.
167#ifndef SWIG
168 template <typename M>
169 M& As() & {
170 static_assert(std::is_base_of<CMessage, M>{},
171 "Must be subclass of CMessage");
172 static_assert(sizeof(M) == sizeof(CMessage),
173 "No data members allowed in CMessage subclasses.");
174 return static_cast<M&>(*this);
175 }
176
177 template <typename M>
178 const M& As() const& {
179 static_assert(std::is_base_of<CMessage, M>{},
180 "Must be subclass of CMessage");
181 static_assert(sizeof(M) == sizeof(CMessage),
182 "No data members allowed in CMessage subclasses.");
183 return static_cast<const M&>(*this);
184 }
185
186 template <typename M, typename = typename std::enable_if<
187 std::is_base_of<CMessage, M>{}>::type>
188 operator M&() & {
189 return As<M>();
190 }
191 template <typename M, typename = typename std::enable_if<
192 std::is_base_of<CMessage, M>{}>::type>
193 operator const M&() const& {
194 return As<M>();
195 }
196// REGISTER_ZNC_MESSAGE allows SWIG to instantiate correct .As<> calls.
197#define REGISTER_ZNC_MESSAGE(M)
198#else
199 // SWIG (as of 3.0.7) doesn't parse ref-qualifiers, and doesn't
200 // differentiate constness.
201 template <typename M>
202 M& As();
203#endif
204
205 private:
206 void InitTime();
207 void InitType();
208
209 CNick m_Nick;
210 CString m_sCommand;
211 VCString m_vsParams;
212 MCString m_mssTags;
213 timeval m_time;
214 CIRCNetwork* m_pNetwork = nullptr;
215 CClient* m_pClient = nullptr;
216 CChan* m_pChan = nullptr;
217 Type m_eType = Type::Unknown;
218 bool m_bColon = false;
219};
220
221// For gtest
222#ifdef GTEST_FAIL
223template <typename M, typename = typename std::enable_if<
224 std::is_base_of<CMessage, M>{}>::type>
225inline ::std::ostream& operator<<(::std::ostream& os, const M& msg) {
226 return os << msg.ToString().Escape_n(CString::EDEBUG);
227}
228#endif
229
230// The various CMessage subclasses are "mutable views" to the data held by
231// CMessage.
232// They provide convenient access to message type speficic attributes, but are
233// not
234// allowed to hold extra data of their own.
235class CTargetMessage : public CMessage {
236 public:
237 CString GetTarget() const { return GetParam(0); }
238 void SetTarget(const CString& sTarget) { SetParam(0, sTarget); }
239};
241
243 public:
244 CString GetText() const {
245 return GetParam(1).TrimPrefix_n("\001ACTION ").TrimSuffix_n("\001");
246 }
247 void SetText(const CString& sText) {
248 SetParam(1, "\001ACTION " + sText + "\001");
249 }
250};
252
254 public:
255 CString GetText() const { return GetParam(0); }
256 void SetText(const CString& sText) { SetParam(0, sText); }
257};
259
261 public:
262 bool IsReply() const { return GetCommand().Equals("NOTICE"); }
263 CString GetText() const {
264 return GetParam(1).TrimPrefix_n("\001").TrimSuffix_n("\001");
265 }
266 void SetText(const CString& sText) { SetParam(1, "\001" + sText + "\001"); }
267};
269
271 public:
272 CString GetKey() const { return GetParam(1); }
273 void SetKey(const CString& sKey) { SetParam(1, sKey); }
274};
276
278 public:
280 CString GetModes() const { return GetParamsColon(1).TrimPrefix_n(":"); }
281
282 CString GetModeList() const { return GetParam(1); };
283
284 VCString GetModeParams() const { return GetParamsSplit(2); };
285
286 bool HasModes() const { return !GetModeList().empty(); };
287};
289
290class CNickMessage : public CMessage {
291 public:
292 CString GetOldNick() const { return GetNick().GetNick(); }
293 CString GetNewNick() const { return GetParam(0); }
294 void SetNewNick(const CString& sNick) { SetParam(0, sNick); }
295};
297
299 public:
300 CString GetText() const { return GetParam(1); }
301 void SetText(const CString& sText) { SetParam(1, sText); }
302};
304
305class CNumericMessage : public CMessage {
306 public:
307 unsigned int GetCode() const { return GetCommand().ToUInt(); }
308};
310
312 public:
313 CString GetKickedNick() const { return GetParam(1); }
314 void SetKickedNick(const CString& sNick) { SetParam(1, sNick); }
315 CString GetReason() const { return GetParam(2); }
316 void SetReason(const CString& sReason) { SetParam(2, sReason); }
317 CString GetText() const { return GetReason(); }
318 void SetText(const CString& sText) { SetReason(sText); }
319};
321
322class CInviteMessage : public CMessage {
323 public:
324 CString GetInvitedNick() const { return GetParam(0); }
325 void SetInvitedNick(const CString& sNick) { SetParam(0, sNick); }
326 CString GetChannel() const { return GetParam(1); }
327 void SetChannel(const CString& sChannel) { SetParam(1, sChannel); }
328};
330
332 public:
333 CString GetReason() const { return GetParam(1); }
334 void SetReason(const CString& sReason) { SetParam(1, sReason); }
335 CString GetText() const { return GetReason(); }
336 void SetText(const CString& sText) { SetReason(sText); }
337};
339
340class CQuitMessage : public CMessage {
341 public:
342 CString GetReason() const { return GetParam(0); }
343 void SetReason(const CString& sReason) { SetParam(0, sReason); }
344 CString GetText() const { return GetReason(); }
345 void SetText(const CString& sText) { SetReason(sText); }
346};
348
350 public:
351 CString GetText() const { return GetParam(1); }
352 void SetText(const CString& sText) { SetParam(1, sText); }
353};
355
357 public:
358 CString GetTopic() const { return GetParam(1); }
359 void SetTopic(const CString& sTopic) { SetParam(1, sTopic); }
360 CString GetText() const { return GetTopic(); }
361 void SetText(const CString& sText) { SetTopic(sText); }
362};
364
365class CChgHostMessage : public CMessage {
366 public:
367 CString GetNewIdent() const { return GetParam(0); }
368 void SetNewIdent(const CString& sIdent) { SetParam(0, sIdent); }
369 CString GetNewHost() const { return GetParam(1); }
370 void SetNewHost(const CString& sHost) { SetParam(1, sHost); }
371};
373
374#endif // !ZNC_MESSAGE_H
#define REGISTER_ZNC_MESSAGE(M)
Definition Message.h:197
#define ZNC_MSG_DEPRECATED(msg)
Definition Message.h:23
std::vector< CString > VCString
Definition ZNCString.h:38
Definition Message.h:242
CString GetText() const
Definition Message.h:244
void SetText(const CString &sText)
Definition Message.h:247
Definition Message.h:253
void SetText(const CString &sText)
Definition Message.h:256
CString GetText() const
Definition Message.h:255
Definition Message.h:260
bool IsReply() const
Definition Message.h:262
CString GetText() const
Definition Message.h:263
void SetText(const CString &sText)
Definition Message.h:266
Definition Chan.h:35
Definition Message.h:365
void SetNewIdent(const CString &sIdent)
Definition Message.h:368
CString GetNewIdent() const
Definition Message.h:367
void SetNewHost(const CString &sHost)
Definition Message.h:370
CString GetNewHost() const
Definition Message.h:369
Definition Client.h:99
Definition IRCNetwork.h:40
Definition Message.h:322
void SetChannel(const CString &sChannel)
Definition Message.h:327
void SetInvitedNick(const CString &sNick)
Definition Message.h:325
CString GetInvitedNick() const
Definition Message.h:324
CString GetChannel() const
Definition Message.h:326
Definition Message.h:270
void SetKey(const CString &sKey)
Definition Message.h:273
CString GetKey() const
Definition Message.h:272
Definition Message.h:311
CString GetReason() const
Definition Message.h:315
void SetKickedNick(const CString &sNick)
Definition Message.h:314
CString GetKickedNick() const
Definition Message.h:313
void SetText(const CString &sText)
Definition Message.h:318
void SetReason(const CString &sReason)
Definition Message.h:316
CString GetText() const
Definition Message.h:317
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:151
FormatFlags
Definition Message.h:157
@ ExcludePrefix
Definition Message.h:159
@ ExcludeTags
Definition Message.h:160
@ IncludeAll
Definition Message.h:158
Type
Definition Message.h:64
const CNick & GetNick() const
Definition Message.h:115
void SetNick(const CNick &Nick)
Definition Message.h:116
void Clone(const CMessage &Other)
const VCString & GetParams() const
Definition Message.h:121
void SetClient(CClient *pClient)
Definition Message.h:109
CNick & GetNick()
Definition Message.h:114
void SetTime(const timeval &ts)
Definition Message.h:148
CIRCNetwork * GetNetwork() const
Definition Message.h:104
const timeval & GetTime() const
Definition Message.h:147
Type GetType() const
Returns which type of message this is.
Definition Message.h:98
CString ToString(unsigned int uFlags=IncludeAll) const
const MCString & GetTags() const
Definition Message.h:150
CClient * GetClient() const
Definition Message.h:108
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:178
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:138
CString GetTag(const CString &sKey) const
void SetChan(CChan *pChan)
Definition Message.h:112
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:118
void SetParams(VCString &&vsParams)
M & As() &
Definition Message.h:169
void SetTags(const MCString &mssTags)
Definition Message.h:152
CMessage(const CString &sMessage="")
void SetNetwork(CIRCNetwork *pNetwork)
Definition Message.h:105
void SetParams(const VCString &vsParams)
void SetParam(unsigned int uIdx, const CString &sParam)
void Parse(const CString &sMessage)
CChan * GetChan() const
Definition Message.h:111
Definition Message.h:277
bool HasModes() const
Definition Message.h:286
VCString GetModeParams() const
Definition Message.h:284
CString GetModeList() const
Definition Message.h:282
CString GetModes() const
Definition Message.h:280
Definition Message.h:290
void SetNewNick(const CString &sNick)
Definition Message.h:294
CString GetNewNick() const
Definition Message.h:293
CString GetOldNick() const
Definition Message.h:292
Definition Nick.h:29
const CString & GetNick() const
Definition Message.h:298
CString GetText() const
Definition Message.h:300
void SetText(const CString &sText)
Definition Message.h:301
Definition Message.h:305
unsigned int GetCode() const
Definition Message.h:307
Definition Message.h:331
void SetText(const CString &sText)
Definition Message.h:336
void SetReason(const CString &sReason)
Definition Message.h:334
CString GetReason() const
Definition Message.h:333
CString GetText() const
Definition Message.h:335
Definition Message.h:340
CString GetReason() const
Definition Message.h:342
void SetText(const CString &sText)
Definition Message.h:345
CString GetText() const
Definition Message.h:344
void SetReason(const CString &sReason)
Definition Message.h:343
String class that is used inside ZNC.
Definition ZNCString.h:50
unsigned int ToUInt() const
@ EDEBUG
Definition ZNCString.h:58
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:235
CString GetTarget() const
Definition Message.h:237
void SetTarget(const CString &sTarget)
Definition Message.h:238
Definition Message.h:349
void SetText(const CString &sText)
Definition Message.h:352
CString GetText() const
Definition Message.h:351
Definition Message.h:356
void SetTopic(const CString &sTopic)
Definition Message.h:359
CString GetTopic() const
Definition Message.h:358
void SetText(const CString &sText)
Definition Message.h:361
CString GetText() const
Definition Message.h:360
A dictionary for strings.
Definition ZNCString.h:577
static const MCString EmptyMap
A static instance of an empty map.
Definition ZNCString.h:588