ZNC  trunk
Chan.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2024 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_CHAN_H
18 #define ZNC_CHAN_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Nick.h>
22 #include <znc/ZNCString.h>
23 #include <znc/Buffer.h>
24 #include <znc/Translation.h>
25 #include <map>
26 
27 // Forward Declarations
28 class CUser;
29 class CIRCNetwork;
30 class CClient;
31 class CConfig;
32 class CFile;
33 // !Forward Declarations
34 
35 class CChan : private CCoreTranslationMixin {
36  public:
37  typedef enum {
38  Voice = '+',
39  HalfOp = '%',
40  Op = '@',
41  Admin = '!',
42  Owner = '*'
44 
45  typedef enum {
46  M_Private = 'p',
47  M_Secret = 's',
48  M_Moderated = 'm',
49  M_InviteOnly = 'i',
50  M_NoMessages = 'n',
51  M_OpTopic = 't',
52  M_Limit = 'l',
53  M_Key = 'k',
54  M_Op = 'o',
55  M_Voice = 'v',
56  M_Ban = 'b',
57  M_Except = 'e'
58  } EModes;
59 
60  CChan(const CString& sName, CIRCNetwork* pNetwork, bool bInConfig,
61  CConfig* pConfig = nullptr);
62  ~CChan();
63 
64  CChan(const CChan&) = delete;
65  CChan& operator=(const CChan&) = delete;
66 
67  void Reset();
68  CConfig ToConfig() const;
69  void Clone(CChan& chan);
70  void Cycle() const;
71  void JoinUser(const CString& sKey = "");
72  void AttachUser(CClient* pClient = nullptr);
73  void DetachUser();
74 
75  void OnWho(const CString& sNick, const CString& sIdent,
76  const CString& sHost);
77 
78  // Modes
80  void SetModes(const CString& s);
86  void SetModes(const CString& sModes, const VCString& vsModeParams);
88  void ModeChange(const CString& sModes, const CNick* OpNick = nullptr);
94  void ModeChange(const CString& sModes,const VCString& vsModeParams, const CNick* OpNick = nullptr);
95  bool AddMode(char cMode, const CString& sArg);
96  bool RemMode(char cMode);
98  CString GetModeArg(CString& sArgs) const;
100  // !Modes
101 
102  // Nicks
103  void ClearNicks();
104  const CNick* FindNick(const CString& sNick) const;
105  CNick* FindNick(const CString& sNick);
106  int AddNicks(const CString& sNicks);
107  bool AddNick(const CString& sNick);
108  bool RemNick(const CString& sNick);
109  bool ChangeNick(const CString& sOldNick, const CString& sNewNick);
110  // !Nicks
111 
112  // Buffer
113  const CBuffer& GetBuffer() const { return m_Buffer; }
114  unsigned int GetBufferCount() const { return m_Buffer.GetLineCount(); }
115  bool SetBufferCount(unsigned int u, bool bForce = false) {
116  m_bHasBufferCountSet = true;
117  return m_Buffer.SetLineCount(u, bForce);
118  }
119  void InheritBufferCount(unsigned int u, bool bForce = false) {
120  if (!m_bHasBufferCountSet) m_Buffer.SetLineCount(u, bForce);
121  }
123  size_t AddBuffer(const CMessage& Format, const CString& sText = "") {
124  return m_Buffer.AddLine(Format, sText);
125  }
127  size_t AddBuffer(const CString& sFormat, const CString& sText = "",
128  const timeval* ts = nullptr,
129  const MCString& mssTags = MCString::EmptyMap) {
130  return m_Buffer.AddLine(sFormat, sText, ts, mssTags);
131  }
132  void ClearBuffer() { m_Buffer.Clear(); }
133  void SendBuffer(CClient* pClient);
134  void SendBuffer(CClient* pClient, const CBuffer& Buffer);
135  // !Buffer
136 
137  // m_Nick wrappers
139  CString GetPermStr() const { return m_Nick.GetPermStr(); }
141  bool HasPerm(char cPerm) const { return m_Nick.HasPerm(cPerm); }
143  bool AddPerm(char cPerm) { return m_Nick.AddPerm(cPerm); }
145  bool RemPerm(char cPerm) { return m_Nick.RemPerm(cPerm); }
146  // !wrappers
147 
148  // Setters
149  void SetModeKnown(bool b) { m_bModeKnown = b; }
150  void SetIsOn(bool b) {
151  m_bIsOn = b;
152  if (!b) {
153  Reset();
154  }
155  }
156  void SetKey(const CString& s);
157  void SetTopic(const CString& s) { m_sTopic = s; }
158  void SetTopicOwner(const CString& s) { m_sTopicOwner = s; }
159  void SetTopicDate(unsigned long u) { m_ulTopicDate = u; }
160  void SetDefaultModes(const CString& s) { m_sDefaultModes = s; }
164  void SetDetached(bool b = true) { m_bDetached = b; }
165  void SetInConfig(bool b);
166  void SetCreationDate(unsigned long u) { m_ulCreationDate = u; }
167  void Disable() { m_bDisabled = true; }
168  void Enable();
170  void ResetJoinTries() { m_uJoinTries = 0; }
171  // !Setters
172 
173  // Getters
174  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
175  bool IsModeKnown() const { return m_bModeKnown; }
176  bool HasMode(char cMode) const;
178  CString GetModeArg(char cMode) const;
179  std::map<char, unsigned int> GetPermCounts() const;
180  bool IsOn() const { return m_bIsOn; }
181  const CString& GetName() const { return m_sName; }
182  const std::map<char, CString>& GetModes() const {
183  return m_mcsModes;
184  }
185  const CString& GetKey() const { return m_sKey; }
186  const CString& GetTopic() const { return m_sTopic; }
187  const CString& GetTopicOwner() const { return m_sTopicOwner; }
188  unsigned long GetTopicDate() const { return m_ulTopicDate; }
189  const CString& GetDefaultModes() const { return m_sDefaultModes; }
190  const std::map<CString, CNick>& GetNicks() const { return m_msNicks; }
191  size_t GetNickCount() const { return m_msNicks.size(); }
193  bool IsDetached() const { return m_bDetached; }
194  bool InConfig() const { return m_bInConfig; }
195  unsigned long GetCreationDate() const { return m_ulCreationDate; }
196  bool IsDisabled() const { return m_bDisabled; }
197  unsigned int GetJoinTries() const { return m_uJoinTries; }
198  bool HasBufferCountSet() const { return m_bHasBufferCountSet; }
201  }
202  // !Getters
203  private:
204  protected:
206  bool m_bIsOn;
216  unsigned long m_ulTopicDate;
217  unsigned long m_ulCreationDate;
220  unsigned int m_uJoinTries;
222  std::map<CString, CNick> m_msNicks; // Todo: make this caseless (irc style)
224 
226  std::map<char, CString> m_mcsModes;
227 };
228 
229 #endif // !ZNC_CHAN_H
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: Buffer.h:74
bool SetLineCount(unsigned int u, bool bForce=false)
size_type AddLine(const CMessage &Format, const CString &sText="")
unsigned int GetLineCount() const
Definition: Buffer.h:107
void Clear()
Definition: Buffer.h:100
Definition: Chan.h:35
void SetTopic(const CString &s)
Definition: Chan.h:157
bool RemNick(const CString &sNick)
void InheritAutoClearChanBuffer(bool b)
void ClearNicks()
unsigned long GetCreationDate() const
Definition: Chan.h:195
void SetAutoClearChanBuffer(bool b)
CString m_sKey
Definition: Chan.h:213
void ClearBuffer()
Definition: Chan.h:132
void SetTopicDate(unsigned long u)
Definition: Chan.h:159
bool AddMode(char cMode, const CString &sArg)
CChan(const CChan &)=delete
const std::map< char, CString > & GetModes() const
Definition: Chan.h:182
bool InConfig() const
Definition: Chan.h:194
CBuffer m_Buffer
Definition: Chan.h:223
bool m_bHasAutoClearChanBufferSet
Definition: Chan.h:211
void ResetJoinTries()
Definition: Chan.h:170
bool m_bInConfig
Definition: Chan.h:208
const CString & GetTopicOwner() const
Definition: Chan.h:187
CNick m_Nick
Definition: Chan.h:219
CNick * FindNick(const CString &sNick)
void DetachUser()
CConfig ToConfig() const
void SendBuffer(CClient *pClient)
size_t AddBuffer(const CMessage &Format, const CString &sText="")
Definition: Chan.h:123
void IncJoinTries()
Definition: Chan.h:169
size_t GetNickCount() const
Definition: Chan.h:191
bool RemPerm(char cPerm)
e.g. '@' for chanop.
Definition: Chan.h:145
CString m_sTopicOwner
Definition: Chan.h:215
void SetIsOn(bool b)
Definition: Chan.h:150
void SetInConfig(bool b)
bool SetBufferCount(unsigned int u, bool bForce=false)
Definition: Chan.h:115
CString GetModeString() const
bool ChangeNick(const CString &sOldNick, const CString &sNewNick)
void SetDefaultModes(const CString &s)
Definition: Chan.h:160
void SetModes(const CString &s)
const CString & GetName() const
Definition: Chan.h:181
void OnWho(const CString &sNick, const CString &sIdent, const CString &sHost)
void ModeChange(const CString &sModes, const VCString &vsModeParams, const CNick *OpNick=nullptr)
Handle changing the modes on a channel.
bool AddNick(const CString &sNick)
CChan(const CString &sName, CIRCNetwork *pNetwork, bool bInConfig, CConfig *pConfig=nullptr)
const CString & GetKey() const
Definition: Chan.h:185
const CString & GetDefaultModes() const
Definition: Chan.h:189
std::map< char, unsigned int > GetPermCounts() const
void InheritBufferCount(unsigned int u, bool bForce=false)
Definition: Chan.h:119
const CBuffer & GetBuffer() const
Definition: Chan.h:113
void Reset()
void Cycle() const
unsigned int GetBufferCount() const
Definition: Chan.h:114
CString GetModeForNames() const
bool m_bModeKnown
Definition: Chan.h:225
CString GetPermStr() const
e.g. '@' for chanop.
Definition: Chan.h:139
unsigned long m_ulCreationDate
Definition: Chan.h:217
void JoinUser(const CString &sKey="")
EUserPerms
Definition: Chan.h:37
@ Owner
Definition: Chan.h:42
@ Voice
Definition: Chan.h:38
@ Op
Definition: Chan.h:40
@ Admin
Definition: Chan.h:41
@ HalfOp
Definition: Chan.h:39
CString m_sDefaultModes
Definition: Chan.h:221
const CString & GetTopic() const
Definition: Chan.h:186
const CNick * FindNick(const CString &sNick) const
bool AddPerm(char cPerm)
e.g. '@' for chanop.
Definition: Chan.h:143
void Disable()
Definition: Chan.h:167
void SetModeKnown(bool b)
Definition: Chan.h:149
bool IsOn() const
Definition: Chan.h:180
void ModeChange(const CString &sModes, const CNick *OpNick=nullptr)
unsigned int GetJoinTries() const
Definition: Chan.h:197
bool m_bIsOn
Definition: Chan.h:206
void ResetBufferCount()
void ResetAutoClearChanBuffer()
void Clone(CChan &chan)
bool HasMode(char cMode) const
unsigned long GetTopicDate() const
Definition: Chan.h:188
void SetCreationDate(unsigned long u)
Definition: Chan.h:166
int AddNicks(const CString &sNicks)
std::map< char, CString > m_mcsModes
Definition: Chan.h:226
size_t AddBuffer(const CString &sFormat, const CString &sText="", const timeval *ts=nullptr, const MCString &mssTags=MCString::EmptyMap)
Definition: Chan.h:127
CString GetOptions() const
bool IsModeKnown() const
Definition: Chan.h:175
void SetTopicOwner(const CString &s)
Definition: Chan.h:158
bool HasBufferCountSet() const
Definition: Chan.h:198
bool HasAutoClearChanBufferSet() const
Definition: Chan.h:199
std::map< CString, CNick > m_msNicks
Definition: Chan.h:222
EModes
Definition: Chan.h:45
@ M_Key
Definition: Chan.h:53
@ M_Private
Definition: Chan.h:46
@ M_Limit
Definition: Chan.h:52
@ M_Ban
Definition: Chan.h:56
@ M_Op
Definition: Chan.h:54
@ M_OpTopic
Definition: Chan.h:51
@ M_Secret
Definition: Chan.h:47
@ M_NoMessages
Definition: Chan.h:50
@ M_Except
Definition: Chan.h:57
@ M_InviteOnly
Definition: Chan.h:49
@ M_Voice
Definition: Chan.h:55
@ M_Moderated
Definition: Chan.h:48
CString m_sTopic
Definition: Chan.h:214
void SetDetached(bool b=true)
Definition: Chan.h:164
void AttachUser(CClient *pClient=nullptr)
bool RemMode(char cMode)
CString GetModeArg(char cMode) const
bool m_bAutoClearChanBuffer
Definition: Chan.h:207
void SetKey(const CString &s)
CString GetModeArg(CString &sArgs) const
void Enable()
bool m_bDisabled
Definition: Chan.h:209
const std::map< CString, CNick > & GetNicks() const
Definition: Chan.h:190
bool m_bDetached
Definition: Chan.h:205
CChan & operator=(const CChan &)=delete
void SendBuffer(CClient *pClient, const CBuffer &Buffer)
bool IsDetached() const
Definition: Chan.h:193
bool HasPerm(char cPerm) const
e.g. '@' for chanop.
Definition: Chan.h:141
CIRCNetwork * GetNetwork() const
Definition: Chan.h:174
CString m_sName
Definition: Chan.h:212
bool IsDisabled() const
Definition: Chan.h:196
unsigned int m_uJoinTries
Definition: Chan.h:220
void SetModes(const CString &sModes, const VCString &vsModeParams)
Set the current modes for this channel.
bool AutoClearChanBuffer() const
Definition: Chan.h:192
bool m_bHasBufferCountSet
Definition: Chan.h:210
unsigned long m_ulTopicDate
Definition: Chan.h:216
CIRCNetwork * m_pNetwork
Definition: Chan.h:218
Definition: Client.h:99
Definition: Config.h:36
Definition: Translation.h:103
Definition: FileUtils.h:30
Definition: IRCNetwork.h:40
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
Definition: Nick.h:29
bool HasPerm(char cPerm) const
e.g. '@' for chanop.
bool AddPerm(char cPerm)
e.g. '@' for chanop.
CString GetPermStr() const
e.g. '@' for chanop.
bool RemPerm(char cPerm)
e.g. '@' for chanop.
String class that is used inside ZNC.
Definition: ZNCString.h:68
Definition: User.h:38
A dictionary for strings.
Definition: ZNCString.h:595
static const MCString EmptyMap
A static instance of an empty map.
Definition: ZNCString.h:606