1 /*
2  * Copyright (C) 2004-2020 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 = '*'
43     } EUserPerms;
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
79     /// @deprecated Use SetModes(CString, VCString)
80     void SetModes(const CString& s);
81     /**
82      * Set the current modes for this channel
83      * @param sModes The mode characters being changed
84      * @param vsModeParams The parameters for the modes to be set
85      */
86     void SetModes(const CString& sModes, const VCString& vsModeParams);
87     /// @deprecated Use ModeChange(CString, VCString, CNick*)
88     void ModeChange(const CString& sModes, const CNick* OpNick = nullptr);
89     /**
90      * Handle changing the modes on a channel
91      * @param sModes The mode string (eg. +ovbs-pbo)
92      * @param vsModeParams The parameters for the mode string
93      */
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);
97     CString GetModeString() const;
98     CString GetModeArg(CString& sArgs) const;
99     CString GetModeForNames() 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
GetBuffer()113     const CBuffer& GetBuffer() const { return m_Buffer; }
GetBufferCount()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     }
122     void ResetBufferCount();
123     size_t AddBuffer(const CMessage& Format, const CString& sText = "") {
124         return m_Buffer.AddLine(Format, sText);
125     }
126     /// @deprecated
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     }
ClearBuffer()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
138     /// e.g. '@' for chanop.
GetPermStr()139     CString GetPermStr() const { return m_Nick.GetPermStr(); }
140     /// e.g. '@' for chanop.
HasPerm(char cPerm)141     bool HasPerm(char cPerm) const { return m_Nick.HasPerm(cPerm); }
142     /// e.g. '@' for chanop.
AddPerm(char cPerm)143     bool AddPerm(char cPerm) { return m_Nick.AddPerm(cPerm); }
144     /// e.g. '@' for chanop.
RemPerm(char cPerm)145     bool RemPerm(char cPerm) { return m_Nick.RemPerm(cPerm); }
146     // !wrappers
147 
148     // Setters
SetModeKnown(bool b)149     void SetModeKnown(bool b) { m_bModeKnown = b; }
SetIsOn(bool b)150     void SetIsOn(bool b) {
151         m_bIsOn = b;
152         if (!b) {
153             Reset();
154         }
155     }
156     void SetKey(const CString& s);
SetTopic(const CString & s)157     void SetTopic(const CString& s) { m_sTopic = s; }
SetTopicOwner(const CString & s)158     void SetTopicOwner(const CString& s) { m_sTopicOwner = s; }
SetTopicDate(unsigned long u)159     void SetTopicDate(unsigned long u) { m_ulTopicDate = u; }
SetDefaultModes(const CString & s)160     void SetDefaultModes(const CString& s) { m_sDefaultModes = s; }
161     void SetAutoClearChanBuffer(bool b);
162     void InheritAutoClearChanBuffer(bool b);
163     void ResetAutoClearChanBuffer();
164     void SetDetached(bool b = true) { m_bDetached = b; }
165     void SetInConfig(bool b);
SetCreationDate(unsigned long u)166     void SetCreationDate(unsigned long u) { m_ulCreationDate = u; }
Disable()167     void Disable() { m_bDisabled = true; }
168     void Enable();
IncJoinTries()169     void IncJoinTries() { m_uJoinTries++; }
ResetJoinTries()170     void ResetJoinTries() { m_uJoinTries = 0; }
171     // !Setters
172 
173     // Getters
GetNetwork()174     CIRCNetwork* GetNetwork() const { return m_pNetwork; }
IsModeKnown()175     bool IsModeKnown() const { return m_bModeKnown; }
176     bool HasMode(char cMode) const;
177     CString GetOptions() const;
178     CString GetModeArg(char cMode) const;
179     std::map<char, unsigned int> GetPermCounts() const;
IsOn()180     bool IsOn() const { return m_bIsOn; }
GetName()181     const CString& GetName() const { return m_sName; }
GetModes()182     const std::map<char, CString>& GetModes() const {
183         return m_mcsModes;
184     }
GetKey()185     const CString& GetKey() const { return m_sKey; }
GetTopic()186     const CString& GetTopic() const { return m_sTopic; }
GetTopicOwner()187     const CString& GetTopicOwner() const { return m_sTopicOwner; }
GetTopicDate()188     unsigned long GetTopicDate() const { return m_ulTopicDate; }
GetDefaultModes()189     const CString& GetDefaultModes() const { return m_sDefaultModes; }
GetNicks()190     const std::map<CString, CNick>& GetNicks() const { return m_msNicks; }
GetNickCount()191     size_t GetNickCount() const { return m_msNicks.size(); }
AutoClearChanBuffer()192     bool AutoClearChanBuffer() const { return m_bAutoClearChanBuffer; }
IsDetached()193     bool IsDetached() const { return m_bDetached; }
InConfig()194     bool InConfig() const { return m_bInConfig; }
GetCreationDate()195     unsigned long GetCreationDate() const { return m_ulCreationDate; }
IsDisabled()196     bool IsDisabled() const { return m_bDisabled; }
GetJoinTries()197     unsigned int GetJoinTries() const { return m_uJoinTries; }
HasBufferCountSet()198     bool HasBufferCountSet() const { return m_bHasBufferCountSet; }
HasAutoClearChanBufferSet()199     bool HasAutoClearChanBufferSet() const {
200         return m_bHasAutoClearChanBufferSet;
201     }
202     // !Getters
203   private:
204   protected:
205     bool m_bDetached;
206     bool m_bIsOn;
207     bool m_bAutoClearChanBuffer;
208     bool m_bInConfig;
209     bool m_bDisabled;
210     bool m_bHasBufferCountSet;
211     bool m_bHasAutoClearChanBufferSet;
212     CString m_sName;
213     CString m_sKey;
214     CString m_sTopic;
215     CString m_sTopicOwner;
216     unsigned long m_ulTopicDate;
217     unsigned long m_ulCreationDate;
218     CIRCNetwork* m_pNetwork;
219     CNick m_Nick;
220     unsigned int m_uJoinTries;
221     CString m_sDefaultModes;
222     std::map<CString, CNick> m_msNicks;  // Todo: make this caseless (irc style)
223     CBuffer m_Buffer;
224 
225     bool m_bModeKnown;
226     std::map<char, CString> m_mcsModes;
227 };
228 
229 #endif  // !ZNC_CHAN_H
230