ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Message.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2018 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 
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  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  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
107  void SetNetwork(CIRCNetwork* pNetwork) { m_pNetwork = pNetwork; }
108 
109  // ZNC <-> CLI
110  CClient* GetClient() const { return m_pClient; }
111  void SetClient(CClient* pClient) { m_pClient = pClient; }
112 
113  CChan* GetChan() const { return m_pChan; }
114  void SetChan(CChan* pChan) { m_pChan = pChan; }
115 
116  CNick& GetNick() { return m_Nick; }
117  const CNick& GetNick() const { return m_Nick; }
118  void SetNick(const CNick& Nick) { m_Nick = Nick; }
119 
120  const CString& GetCommand() const { return m_sCommand; }
121  void SetCommand(const CString& sCommand);
122 
123  const VCString& GetParams() const { return m_vsParams; }
124  void SetParams(const VCString& vsParams);
125 
127  CString GetParams(unsigned int uIdx, unsigned int uLen = -1) const
128  ZNC_MSG_DEPRECATED("Use GetParamsColon() instead") {
129  return GetParamsColon(uIdx, uLen);
130  }
131  CString GetParamsColon(unsigned int uIdx, unsigned int uLen = -1) const;
132 
133  CString GetParam(unsigned int uIdx) const;
134  void SetParam(unsigned int uIdx, const CString& sParam);
135 
136  const timeval& GetTime() const { return m_time; }
137  void SetTime(const timeval& ts) { m_time = ts; }
138 
139  const MCString& GetTags() const { return m_mssTags; }
140  void SetTags(const MCString& mssTags) { m_mssTags = mssTags; }
141 
142  CString GetTag(const CString& sKey) const;
143  void SetTag(const CString& sKey, const CString& sValue);
144 
145  enum FormatFlags {
146  IncludeAll = 0x0,
149  };
150 
151  CString ToString(unsigned int uFlags = IncludeAll) const;
152  void Parse(CString sMessage);
153 
154 // Implicit and explicit conversion to a subclass reference.
155 #ifndef SWIG
156  template <typename M>
158  static_assert(std::is_base_of<CMessage, M>{},
159  "Must be subclass of CMessage");
160  static_assert(sizeof(M) == sizeof(CMessage),
161  "No data members allowed in CMessage subclasses.");
162  return static_cast<M&>(*this);
163  }
164 
165  template <typename M>
166  const M& As() const ZNC_LVREFQUAL {
167  static_assert(std::is_base_of<CMessage, M>{},
168  "Must be subclass of CMessage");
169  static_assert(sizeof(M) == sizeof(CMessage),
170  "No data members allowed in CMessage subclasses.");
171  return static_cast<const M&>(*this);
172  }
173 
174  template <typename M, typename = typename std::enable_if<
175  std::is_base_of<CMessage, M>{}>::type>
176  operator M&() ZNC_LVREFQUAL {
177  return As<M>();
178  }
179  template <typename M, typename = typename std::enable_if<
180  std::is_base_of<CMessage, M>{}>::type>
181  operator const M&() const ZNC_LVREFQUAL {
182  return As<M>();
183  }
184 // REGISTER_ZNC_MESSAGE allows SWIG to instantiate correct .As<> calls.
185 #define REGISTER_ZNC_MESSAGE(M)
186 #else
187  // SWIG (as of 3.0.7) doesn't parse ref-qualifiers, and doesn't
188  // differentiate constness.
189  template <typename M>
190  M& As();
191 #endif
192 
193  private:
194  void InitTime();
195  void InitType();
196 
197  CNick m_Nick;
198  CString m_sCommand;
199  VCString m_vsParams;
200  MCString m_mssTags;
201  timeval m_time;
202  CIRCNetwork* m_pNetwork = nullptr;
203  CClient* m_pClient = nullptr;
204  CChan* m_pChan = nullptr;
205  Type m_eType = Type::Unknown;
206  bool m_bColon = false;
207 };
208 
209 // For gtest
210 #ifdef GTEST_FAIL
211 template <typename M, typename = typename std::enable_if<
212  std::is_base_of<CMessage, M>{}>::type>
213 inline ::std::ostream& operator<<(::std::ostream& os, const M& msg) {
214  return os << msg.ToString().Escape_n(CString::EDEBUG);
215 }
216 #endif
217 
218 // The various CMessage subclasses are "mutable views" to the data held by
219 // CMessage.
220 // They provide convenient access to message type speficic attributes, but are
221 // not
222 // allowed to hold extra data of their own.
223 class CTargetMessage : public CMessage {
224  public:
225  CString GetTarget() const { return GetParam(0); }
226  void SetTarget(const CString& sTarget) { SetParam(0, sTarget); }
227 };
229 
231  public:
232  CString GetText() const {
233  return GetParam(1).TrimPrefix_n("\001ACTION ").TrimSuffix_n("\001");
234  }
235  void SetText(const CString& sText) {
236  SetParam(1, "\001ACTION " + sText + "\001");
237  }
238 };
240 
241 class CCTCPMessage : public CTargetMessage {
242  public:
243  bool IsReply() const { return GetCommand().Equals("NOTICE"); }
244  CString GetText() const {
245  return GetParam(1).TrimPrefix_n("\001").TrimSuffix_n("\001");
246  }
247  void SetText(const CString& sText) { SetParam(1, "\001" + sText + "\001"); }
248 };
250 
251 class CJoinMessage : public CTargetMessage {
252  public:
253  CString GetKey() const { return GetParam(1); }
254  void SetKey(const CString& sKey) { SetParam(1, sKey); }
255 };
257 
258 class CModeMessage : public CTargetMessage {
259  public:
260  CString GetModes() const { return GetParamsColon(1).TrimPrefix_n(":"); }
261 };
263 
264 class CNickMessage : public CMessage {
265  public:
266  CString GetOldNick() const { return GetNick().GetNick(); }
267  CString GetNewNick() const { return GetParam(0); }
268  void SetNewNick(const CString& sNick) { SetParam(0, sNick); }
269 };
271 
273  public:
274  CString GetText() const { return GetParam(1); }
275  void SetText(const CString& sText) { SetParam(1, sText); }
276 };
278 
279 class CNumericMessage : public CMessage {
280  public:
281  unsigned int GetCode() const { return GetCommand().ToUInt(); }
282 };
284 
285 class CKickMessage : public CTargetMessage {
286  public:
287  CString GetKickedNick() const { return GetParam(1); }
288  void SetKickedNick(const CString& sNick) { SetParam(1, sNick); }
289  CString GetReason() const { return GetParam(2); }
290  void SetReason(const CString& sReason) { SetParam(2, sReason); }
291  CString GetText() const { return GetReason(); }
292  void SetText(const CString& sText) { SetReason(sText); }
293 };
295 
296 class CPartMessage : public CTargetMessage {
297  public:
298  CString GetReason() const { return GetParam(1); }
299  void SetReason(const CString& sReason) { SetParam(1, sReason); }
300  CString GetText() const { return GetReason(); }
301  void SetText(const CString& sText) { SetReason(sText); }
302 };
304 
305 class CQuitMessage : public CMessage {
306  public:
307  CString GetReason() const { return GetParam(0); }
308  void SetReason(const CString& sReason) { SetParam(0, sReason); }
309  CString GetText() const { return GetReason(); }
310  void SetText(const CString& sText) { SetReason(sText); }
311 };
313 
314 class CTextMessage : public CTargetMessage {
315  public:
316  CString GetText() const { return GetParam(1); }
317  void SetText(const CString& sText) { SetParam(1, sText); }
318 };
320 
322  public:
323  CString GetTopic() const { return GetParam(1); }
324  void SetTopic(const CString& sTopic) { SetParam(1, sTopic); }
325  CString GetText() const { return GetTopic(); }
326  void SetText(const CString& sText) { SetTopic(sText); }
327 };
329 
330 #endif // !ZNC_MESSAGE_H
Definition: Message.h:279
CString GetReason() const
Definition: Message.h:298
Definition: Message.h:321
#define ZNC_MSG_DEPRECATED(msg)
Definition: Message.h:36
Definition: Message.h:258
CNick & GetNick()
Definition: Message.h:116
void Clone(const CMessage &Other)
void SetText(const CString &sText)
Definition: Message.h:235
CString ToString(unsigned int uFlags=IncludeAll) const
const VCString & GetParams() const
Definition: Message.h:123
Definition: Message.h:148
#define REGISTER_ZNC_MESSAGE(M)
Definition: Message.h:185
const MCString & GetTags() const
Definition: Message.h:139
CString GetModes() const
Definition: Message.h:260
CString TrimSuffix_n(const CString &sSuffix) const
Trim a given suffix.
void SetParam(unsigned int uIdx, const CString &sParam)
void SetChan(CChan *pChan)
Definition: Message.h:114
unsigned int GetCode() const
Definition: Message.h:281
void SetTarget(const CString &sTarget)
Definition: Message.h:226
Definition: Message.h:223
void SetText(const CString &sText)
Definition: Message.h:301
void SetText(const CString &sText)
Definition: Message.h:326
Definition: Message.h:147
Definition: Client.h:99
void SetParams(const VCString &vsParams)
CString GetText() const
Definition: Message.h:316
#define ZNC_LVREFQUAL
Definition: Message.h:30
Definition: Message.h:296
Definition: Message.h:272
void SetTag(const CString &sKey, const CString &sValue)
const CString & GetNick() const
CString GetKickedNick() const
Definition: Message.h:287
CString GetText() const
Definition: Message.h:274
void SetCommand(const CString &sCommand)
CString GetText() const
Definition: Message.h:309
void SetText(const CString &sText)
Definition: Message.h:310
Definition: Nick.h:29
CString GetKey() const
Definition: Message.h:253
void SetText(const CString &sText)
Definition: Message.h:317
const M & As() const ZNC_LVREFQUAL
Definition: Message.h:166
Definition: ZNCString.h:76
Definition: Message.h:241
void SetTags(const MCString &mssTags)
Definition: Message.h:140
bool IsReply() const
Definition: Message.h:243
CString GetText() const
Definition: Message.h:232
Definition: Message.h:305
void Parse(CString sMessage)
Definition: Message.h:230
const timeval & GetTime() const
Definition: Message.h:136
void SetReason(const CString &sReason)
Definition: Message.h:308
bool Equals(const CMessage &Other) const
Definition: IRCNetwork.h:40
std::vector< CString > VCString
Definition: ZNCString.h:38
CString GetText() const
Definition: Message.h:325
CString GetParamsColon(unsigned int uIdx, unsigned int uLen=-1) const
void SetText(const CString &sText)
Definition: Message.h:275
CString GetReason() const
Definition: Message.h:289
String class that is used inside ZNC.
Definition: ZNCString.h:68
void SetKickedNick(const CString &sNick)
Definition: Message.h:288
CString GetText() const
Definition: Message.h:291
void SetNewNick(const CString &sNick)
Definition: Message.h:268
void SetNetwork(CIRCNetwork *pNetwork)
Definition: Message.h:107
CString GetParam(unsigned int uIdx) const
CString GetOldNick() const
Definition: Message.h:266
FormatFlags
Definition: Message.h:145
void SetTopic(const CString &sTopic)
Definition: Message.h:324
const CString & GetCommand() const
Definition: Message.h:120
static const MCString EmptyMap
A static instance of an empty map.
Definition: ZNCString.h:606
const CNick & GetNick() const
Definition: Message.h:117
unsigned int ToUInt() const
Definition: Message.h:314
CString GetTarget() const
Definition: Message.h:225
void SetKey(const CString &sKey)
Definition: Message.h:254
Definition: Message.h:146
M & As() ZNC_LVREFQUAL
Definition: Message.h:157
A dictionary for strings.
Definition: ZNCString.h:595
void SetReason(const CString &sReason)
Definition: Message.h:290
Definition: Message.h:264
CString GetText() const
Definition: Message.h:244
CChan * GetChan() const
Definition: Message.h:113
void SetText(const CString &sText)
Definition: Message.h:292
Type
Definition: Message.h:77
bool Equals(const CString &s, CaseSensitivity cs=CaseInsensitive) const
Check if this string is equal to some other string.
CString GetTopic() const
Definition: Message.h:323
CString GetText() const
Definition: Message.h:300
CString GetNewNick() const
Definition: Message.h:267
void SetTime(const timeval &ts)
Definition: Message.h:137
void SetText(const CString &sText)
Definition: Message.h:247
CString GetTag(const CString &sKey) const
Definition: Message.h:251
void SetReason(const CString &sReason)
Definition: Message.h:299
CString GetParams(unsigned int uIdx, unsigned int uLen=-1) const ZNC_MSG_DEPRECATED("Use GetParamsColon() instead")
Definition: Message.h:127
void SetClient(CClient *pClient)
Definition: Message.h:111
CMessage(const CString &sMessage="")
Definition: Chan.h:35
Definition: Message.h:285
CIRCNetwork * GetNetwork() const
Definition: Message.h:106
CClient * GetClient() const
Definition: Message.h:110
CString GetReason() const
Definition: Message.h:307
Here is a small explanation of how messages on IRC work, and how you can use this class to get useful...
Definition: Message.h:70
CString TrimPrefix_n(const CString &sPrefix=":") const
Trim a given prefix.
void SetNick(const CNick &Nick)
Definition: Message.h:118
Type GetType() const
Definition: Message.h:100