Coverage Report

Created: 2023-09-25 06:17

/src/znc/include/znc/Message.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2004-2023 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
// Remove this after Feb 2016 when Debian 7 is EOL
21
#if __cpp_ref_qualifiers >= 200710
22
#define ZNC_LVREFQUAL &
23
#elif defined(__clang__)
24
#define ZNC_LVREFQUAL &
25
#elif __GNUC__ > 4 ||                       \
26
    __GNUC__ == 4 && (__GNUC_MINOR__ > 8 || \
27
                      __GNUC_MINOR__ == 8 && __GNUC_PATCHLEVEL__ >= 1)
28
#define ZNC_LVREFQUAL &
29
#else
30
#define ZNC_LVREFQUAL
31
#endif
32
33
#ifdef SWIG
34
#define ZNC_MSG_DEPRECATED(msg)
35
#else
36
#define ZNC_MSG_DEPRECATED(msg) __attribute__((deprecated(msg)))
37
#endif
38
39
#include <znc/zncconfig.h>
40
#include <znc/ZNCString.h>
41
#include <znc/Nick.h>
42
#include <sys/time.h>
43
44
class CChan;
45
class CClient;
46
class CIRCNetwork;
47
48
/**
49
 * Here is a small explanation of how messages on IRC work, and how you can use
50
 * this class to get useful information from the parsed message. The output
51
 * varies greatly and this advice may not apply to every message type, but this
52
 * will hopefully help you understand how it works more accurately.
53
 *
54
 * @t=some-tag :server.network.net 366 something #channel :End of /NAMES list.
55
 * tags         nick               cmd 0         1         2
56
 *
57
 * - `tags` is the IRCv3 tags associated with the message, obtained with
58
 *   GetTag("t").  the @time tag can also be obtained with GetTime(), some
59
 *   messages have other tags with them. Tags may not always be present. Refer
60
 *   to IRCv3 for documentation on tags.
61
 * - `nick` is the sender, which can be obtained with GetNick()
62
 * - `cmd` is command, which is obtained via GetCommand()
63
 * - `0`, `1`, ... are parameters, available via GetParam(n), which removes the
64
 *   leading colon (:). If you don't want to remove the colon, use
65
 *   GetParamsColon().
66
 *
67
 * For certain events, like a PRIVMSG, convienience commands like GetChan() and
68
 * GetNick() are available, this is not true for all CMessage extensions.
69
 */
70
class CMessage {
71
  public:
72
    explicit CMessage(const CString& sMessage = "");
73
    CMessage(const CNick& Nick, const CString& sCommand,
74
             const VCString& vsParams = VCString(),
75
             const MCString& mssTags = MCString::EmptyMap);
76
77
    enum class Type {
78
        Unknown,
79
        Account,
80
        Action,
81
        Away,
82
        Capability,
83
        CTCP,
84
        Error,
85
        Invite,
86
        Join,
87
        Kick,
88
        Mode,
89
        Nick,
90
        Notice,
91
        Numeric,
92
        Part,
93
        Ping,
94
        Pong,
95
        Quit,
96
        Text,
97
        Topic,
98
        Wallops,
99
    };
100
0
    Type GetType() const { return m_eType; }
101
102
    bool Equals(const CMessage& Other) const;
103
    void Clone(const CMessage& Other);
104
105
    // ZNC <-> IRC
106
0
    CIRCNetwork* GetNetwork() const { return m_pNetwork; }
107
0
    void SetNetwork(CIRCNetwork* pNetwork) { m_pNetwork = pNetwork; }
108
109
    // ZNC <-> CLI
110
0
    CClient* GetClient() const { return m_pClient; }
111
0
    void SetClient(CClient* pClient) { m_pClient = pClient; }
112
113
0
    CChan* GetChan() const { return m_pChan; }
114
0
    void SetChan(CChan* pChan) { m_pChan = pChan; }
115
116
0
    CNick& GetNick() { return m_Nick; }
117
0
    const CNick& GetNick() const { return m_Nick; }
118
0
    void SetNick(const CNick& Nick) { m_Nick = Nick; }
119
120
0
    const CString& GetCommand() const { return m_sCommand; }
121
    void SetCommand(const CString& sCommand);
122
123
1.90k
    const VCString& GetParams() const { return m_vsParams; }
124
125
    /**
126
     * Get a subset of the message parameters
127
     *
128
     * This allows accessing a vector of a specific range of parameters,
129
     * allowing easy inline use, such as `pChan->SetModes(Message.GetParam(2), Message.GetParamsSplit(3));`
130
     *
131
     * @param uIdx The index of the first parameter to retrieve
132
     * @param uLen How many parameters to retrieve
133
     * @return A VCString containing the retrieved parameters
134
     */
135
    VCString GetParamsSplit(unsigned int uIdx, unsigned int uLen = -1) const;
136
    void SetParams(const VCString& vsParams);
137
138
    /// @deprecated use GetParamsColon() instead.
139
    CString GetParams(unsigned int uIdx, unsigned int uLen = -1) const
140
0
        ZNC_MSG_DEPRECATED("Use GetParamsColon() instead") {
141
0
        return GetParamsColon(uIdx, uLen);
142
0
    }
143
    CString GetParamsColon(unsigned int uIdx, unsigned int uLen = -1) const;
144
145
    CString GetParam(unsigned int uIdx) const;
146
    void SetParam(unsigned int uIdx, const CString& sParam);
147
148
0
    const timeval& GetTime() const { return m_time; }
149
0
    void SetTime(const timeval& ts) { m_time = ts; }
150
151
0
    const MCString& GetTags() const { return m_mssTags; }
152
0
    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
157
    enum FormatFlags {
158
        IncludeAll = 0x0,
159
        ExcludePrefix = 0x1,
160
        ExcludeTags = 0x2
161
    };
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
0
    M& As() ZNC_LVREFQUAL {
170
0
        static_assert(std::is_base_of<CMessage, M>{},
171
0
                      "Must be subclass of CMessage");
172
0
        static_assert(sizeof(M) == sizeof(CMessage),
173
0
                      "No data members allowed in CMessage subclasses.");
174
0
        return static_cast<M&>(*this);
175
0
    }
Unexecuted instantiation: CActionMessage& CMessage::As<CActionMessage>() &
Unexecuted instantiation: CCTCPMessage& CMessage::As<CCTCPMessage>() &
Unexecuted instantiation: CJoinMessage& CMessage::As<CJoinMessage>() &
Unexecuted instantiation: CKickMessage& CMessage::As<CKickMessage>() &
Unexecuted instantiation: CModeMessage& CMessage::As<CModeMessage>() &
Unexecuted instantiation: CNickMessage& CMessage::As<CNickMessage>() &
Unexecuted instantiation: CNoticeMessage& CMessage::As<CNoticeMessage>() &
Unexecuted instantiation: CNumericMessage& CMessage::As<CNumericMessage>() &
Unexecuted instantiation: CPartMessage& CMessage::As<CPartMessage>() &
Unexecuted instantiation: CQuitMessage& CMessage::As<CQuitMessage>() &
Unexecuted instantiation: CTextMessage& CMessage::As<CTextMessage>() &
Unexecuted instantiation: CTopicMessage& CMessage::As<CTopicMessage>() &
176
177
    template <typename M>
178
    const M& As() const ZNC_LVREFQUAL {
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
0
    operator M&() ZNC_LVREFQUAL {
189
0
        return As<M>();
190
0
    }
Unexecuted instantiation: CMessage::operator CActionMessage&<CActionMessage, void>() &
Unexecuted instantiation: CMessage::operator CCTCPMessage&<CCTCPMessage, void>() &
Unexecuted instantiation: CMessage::operator CJoinMessage&<CJoinMessage, void>() &
Unexecuted instantiation: CMessage::operator CKickMessage&<CKickMessage, void>() &
Unexecuted instantiation: CMessage::operator CModeMessage&<CModeMessage, void>() &
Unexecuted instantiation: CMessage::operator CNickMessage&<CNickMessage, void>() &
Unexecuted instantiation: CMessage::operator CNoticeMessage&<CNoticeMessage, void>() &
Unexecuted instantiation: CMessage::operator CNumericMessage&<CNumericMessage, void>() &
Unexecuted instantiation: CMessage::operator CPartMessage&<CPartMessage, void>() &
Unexecuted instantiation: CMessage::operator CQuitMessage&<CQuitMessage, void>() &
Unexecuted instantiation: CMessage::operator CTextMessage&<CTextMessage, void>() &
Unexecuted instantiation: CMessage::operator CTopicMessage&<CTopicMessage, void>() &
191
    template <typename M, typename = typename std::enable_if<
192
                              std::is_base_of<CMessage, M>{}>::type>
193
    operator const M&() const ZNC_LVREFQUAL {
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
223
template <typename M, typename = typename std::enable_if<
224
                          std::is_base_of<CMessage, M>{}>::type>
225
inline ::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.
235
class CTargetMessage : public CMessage {
236
  public:
237
0
    CString GetTarget() const { return GetParam(0); }
238
0
    void SetTarget(const CString& sTarget) { SetParam(0, sTarget); }
239
};
240
REGISTER_ZNC_MESSAGE(CTargetMessage);
241
242
class CActionMessage : public CTargetMessage {
243
  public:
244
0
    CString GetText() const {
245
0
        return GetParam(1).TrimPrefix_n("\001ACTION ").TrimSuffix_n("\001");
246
0
    }
247
0
    void SetText(const CString& sText) {
248
0
        SetParam(1, "\001ACTION " + sText + "\001");
249
0
    }
250
};
251
REGISTER_ZNC_MESSAGE(CActionMessage);
252
253
class CCTCPMessage : public CTargetMessage {
254
  public:
255
0
    bool IsReply() const { return GetCommand().Equals("NOTICE"); }
256
0
    CString GetText() const {
257
0
        return GetParam(1).TrimPrefix_n("\001").TrimSuffix_n("\001");
258
0
    }
259
0
    void SetText(const CString& sText) { SetParam(1, "\001" + sText + "\001"); }
260
};
261
REGISTER_ZNC_MESSAGE(CCTCPMessage);
262
263
class CJoinMessage : public CTargetMessage {
264
  public:
265
0
    CString GetKey() const { return GetParam(1); }
266
0
    void SetKey(const CString& sKey) { SetParam(1, sKey); }
267
};
268
REGISTER_ZNC_MESSAGE(CJoinMessage);
269
270
class CModeMessage : public CTargetMessage {
271
  public:
272
    /// @deprecated Use GetModeList() and GetModeParams()
273
0
    CString GetModes() const { return GetParamsColon(1).TrimPrefix_n(":"); }
274
275
0
    CString GetModeList() const { return GetParam(1); };
276
277
0
    VCString GetModeParams() const { return GetParamsSplit(2); };
278
279
0
    bool HasModes() const { return !GetModeList().empty(); };
280
};
281
REGISTER_ZNC_MESSAGE(CModeMessage);
282
283
class CNickMessage : public CMessage {
284
  public:
285
0
    CString GetOldNick() const { return GetNick().GetNick(); }
286
0
    CString GetNewNick() const { return GetParam(0); }
287
0
    void SetNewNick(const CString& sNick) { SetParam(0, sNick); }
288
};
289
REGISTER_ZNC_MESSAGE(CNickMessage);
290
291
class CNoticeMessage : public CTargetMessage {
292
  public:
293
0
    CString GetText() const { return GetParam(1); }
294
0
    void SetText(const CString& sText) { SetParam(1, sText); }
295
};
296
REGISTER_ZNC_MESSAGE(CNoticeMessage);
297
298
class CNumericMessage : public CMessage {
299
  public:
300
0
    unsigned int GetCode() const { return GetCommand().ToUInt(); }
301
};
302
REGISTER_ZNC_MESSAGE(CNumericMessage);
303
304
class CKickMessage : public CTargetMessage {
305
  public:
306
0
    CString GetKickedNick() const { return GetParam(1); }
307
0
    void SetKickedNick(const CString& sNick) { SetParam(1, sNick); }
308
0
    CString GetReason() const { return GetParam(2); }
309
0
    void SetReason(const CString& sReason) { SetParam(2, sReason); }
310
0
    CString GetText() const { return GetReason(); }
311
0
    void SetText(const CString& sText) { SetReason(sText); }
312
};
313
REGISTER_ZNC_MESSAGE(CKickMessage);
314
315
class CPartMessage : public CTargetMessage {
316
  public:
317
0
    CString GetReason() const { return GetParam(1); }
318
0
    void SetReason(const CString& sReason) { SetParam(1, sReason); }
319
0
    CString GetText() const { return GetReason(); }
320
0
    void SetText(const CString& sText) { SetReason(sText); }
321
};
322
REGISTER_ZNC_MESSAGE(CPartMessage);
323
324
class CQuitMessage : public CMessage {
325
  public:
326
0
    CString GetReason() const { return GetParam(0); }
327
0
    void SetReason(const CString& sReason) { SetParam(0, sReason); }
328
0
    CString GetText() const { return GetReason(); }
329
0
    void SetText(const CString& sText) { SetReason(sText); }
330
};
331
REGISTER_ZNC_MESSAGE(CQuitMessage);
332
333
class CTextMessage : public CTargetMessage {
334
  public:
335
0
    CString GetText() const { return GetParam(1); }
336
0
    void SetText(const CString& sText) { SetParam(1, sText); }
337
};
338
REGISTER_ZNC_MESSAGE(CTextMessage);
339
340
class CTopicMessage : public CTargetMessage {
341
  public:
342
0
    CString GetTopic() const { return GetParam(1); }
343
0
    void SetTopic(const CString& sTopic) { SetParam(1, sTopic); }
344
0
    CString GetText() const { return GetTopic(); }
345
0
    void SetText(const CString& sText) { SetTopic(sText); }
346
};
347
REGISTER_ZNC_MESSAGE(CTopicMessage);
348
349
#endif  // !ZNC_MESSAGE_H