~ubuntu-branches/ubuntu/maverick/znc/maverick

« back to all changes in this revision

Viewing changes to znc.h

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2010-05-24 18:05:44 UTC
  • mfrom: (1.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100524180544-8e4s3f4nj0lhyw7n
Tags: 0.090~rc1-1
* New upstream release candidate.
  - Drop znc-webadmin package. It is now provided in the core source code.
  - Rename discon_kick module to disconkick.
  - Add charset and notes module.
* Add missing dependency on libc-ares-dev to znc-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
#include "Client.h"
13
13
#include "FileUtils.h"
14
 
#ifdef _MODULES
15
14
#include "Modules.h"
16
 
#endif
17
15
#include "Socket.h"
18
16
#include <map>
19
17
 
47
45
        bool AllowConnectionFrom(const CString& sIP) const;
48
46
        void InitDirs(const CString& sArgvPath, const CString& sDataDir);
49
47
        bool OnBoot();
50
 
        CString ExpandConfigPath(const CString& sConfigFile);
 
48
        CString ExpandConfigPath(const CString& sConfigFile, bool bAllowMkDir = true);
51
49
        bool WriteNewConfig(const CString& sConfigFile);
52
50
        bool WriteConfig();
53
51
        bool ParseConfig(const CString& sConfig);
82
80
 
83
81
        // Setters
84
82
        void SetConfigState(enum ConfigState e) { m_eConfigState = e; }
 
83
        void SetSkinName(const CString& s) { m_sSkinName = s; }
85
84
        void SetStatusPrefix(const CString& s) { m_sStatusPrefix = (s.empty()) ? "*" : s; }
86
85
        void SetISpoofFile(const CString& s) { m_sISpoofFile = s; }
87
86
        void SetISpoofFormat(const CString& s) { m_sISpoofFormat = (s.empty()) ? "global { reply \"%\" }" : s; }
91
90
        enum ConfigState GetConfigState() const { return m_eConfigState; }
92
91
        CSockManager& GetManager() { return m_Manager; }
93
92
        const CSockManager& GetManager() const { return m_Manager; }
94
 
#ifdef _MODULES
95
93
        CGlobalModules& GetModules() { return *m_pModules; }
96
94
        size_t FilterUncommonModules(set<CModInfo>& ssModules);
97
 
#endif
 
95
        CString GetSkinName() const { return m_sSkinName; }
98
96
        const CString& GetStatusPrefix() const { return m_sStatusPrefix; }
99
97
        const CString& GetCurPath() const { if (!CFile::Exists(m_sCurPath)) { CDir::MakeDir(m_sCurPath); } return m_sCurPath; }
100
98
        const CString& GetHomePath() const { if (!CFile::Exists(m_sHomePath)) { CDir::MakeDir(m_sHomePath); } return m_sHomePath; }
101
99
        const CString& GetZNCPath() const { if (!CFile::Exists(m_sZNCPath)) { CDir::MakeDir(m_sZNCPath); } return m_sZNCPath; }
102
 
        CString GetConfPath() const;
 
100
        CString GetConfPath(bool bAllowMkDir = true) const;
103
101
        CString GetUserPath() const;
104
102
        CString GetModPath() const;
105
103
        CString GetPemLocation() const { return GetZNCPath() + "/znc.pem"; }
115
113
        // Static allocator
116
114
        static CZNC& Get();
117
115
        CUser* FindUser(const CString& sUsername);
 
116
        CModule* FindModule(const CString& sModName, const CString& sUsername);
 
117
        CModule* FindModule(const CString& sModName, CUser* pUser);
118
118
        bool DeleteUser(const CString& sUsername);
119
119
        bool AddUser(CUser* pUser, CString& sErrorRet);
120
120
        const map<CString,CUser*> & GetUserMap() const { return(m_msUsers); }
121
121
 
 
122
        // Listener yummy
 
123
        CListener* FindListener(u_short uPort, const CString& BindHost, EAddrType eAddr);
 
124
        bool AddListener(CListener*);
 
125
        bool DelListener(CListener*);
 
126
 
122
127
        // Message of the Day
123
128
        void SetMotd(const CString& sMessage) { ClearMotd(); AddMotd(sMessage); }
124
129
        void AddMotd(const CString& sMessage) { if (!sMessage.empty()) { m_vsMotd.push_back(sMessage); } }
136
141
        void LeakConnectUser(CConnectUserTimer *pTimer);
137
142
 
138
143
private:
 
144
        CFile* InitPidFile();
139
145
        bool DoRehash(CString& sError);
140
146
        // Returns true if something was done
141
147
        bool HandleUserDeletion();
142
148
 
143
149
protected:
144
 
        time_t                          m_TimeStarted;
145
 
 
146
 
        enum ConfigState                m_eConfigState;
147
 
        vector<CListener*>              m_vpListeners;
148
 
        map<CString,CUser*>             m_msUsers;
149
 
        map<CString,CUser*>             m_msDelUsers;
150
 
        CSockManager                    m_Manager;
151
 
 
152
 
        CString                                 m_sCurPath;
153
 
        CString                                 m_sHomePath;
154
 
        CString                                 m_sZNCPath;
155
 
 
156
 
        CString                                 m_sConfigFile;
157
 
        CString                                 m_sStatusPrefix;
158
 
        CString                                 m_sISpoofFile;
159
 
        CString                                 m_sOrigISpoof;
160
 
        CString                                 m_sISpoofFormat;
161
 
        CString                                 m_sPidFile;
162
 
        VCString                                m_vsVHosts;
163
 
        VCString                                m_vsMotd;
164
 
        CFile                                   m_LockFile;
165
 
        CFile*                                  m_pISpoofLockFile;
166
 
        unsigned int                            m_uiConnectDelay;
167
 
        unsigned int                            m_uiAnonIPLimit;
168
 
#ifdef _MODULES
169
 
        CGlobalModules*                 m_pModules;
170
 
#endif
171
 
        unsigned long long              m_uBytesRead;
172
 
        unsigned long long              m_uBytesWritten;
173
 
        CConnectUserTimer               *m_pConnectUserTimer;
174
 
        TCacheMap<CString>              m_sConnectThrottle;
175
 
};
176
 
 
177
 
class CRealListener : public CZNCSock {
178
 
public:
179
 
        CRealListener() : CZNCSock() {}
180
 
        virtual ~CRealListener() {}
181
 
 
182
 
        virtual bool ConnectionFrom(const CString& sHost, unsigned short uPort) {
183
 
                DEBUG(GetSockName() << " == ConnectionFrom(" << sHost << ", " << uPort << ")");
184
 
                return CZNC::Get().IsHostAllowed(sHost);
185
 
        }
186
 
 
187
 
        virtual Csock* GetSockObj(const CString& sHost, unsigned short uPort) {
188
 
                CClient *pClient = new CClient(sHost, uPort);
189
 
                if (CZNC::Get().AllowConnectionFrom(sHost)) {
190
 
#ifdef _MODULES
191
 
                        CZNC::Get().GetModules().OnClientConnect(pClient, sHost, uPort);
192
 
#endif
193
 
                } else {
194
 
                        pClient->RefuseLogin("Too many anonymous connections from your IP");
195
 
#ifdef _MODULES
196
 
                        CZNC::Get().GetModules().OnFailedLogin("", sHost);
197
 
#endif
198
 
                }
199
 
                return pClient;
200
 
        }
201
 
 
202
 
        virtual void SockError(int iErrno) {
203
 
                DEBUG(GetSockName() << " == SockError(" << strerror(iErrno) << ")");
204
 
                if (iErrno == EMFILE) {
205
 
                        // We have too many open fds, let's close this listening port to be able to continue
206
 
                        // to work, next rehash will (try to) reopen it.
207
 
                        Close();
208
 
                }
209
 
        }
210
 
};
211
 
 
212
 
class CListener {
213
 
public:
214
 
        CListener(unsigned short uPort, const CString& sBindHost, bool bSSL, bool bIPV6) {
215
 
                m_uPort = uPort;
216
 
                m_sBindHost = sBindHost;
217
 
                m_bSSL = bSSL;
218
 
                m_bIPV6 = bIPV6;
219
 
                m_pListener = NULL;
220
 
        }
221
 
 
222
 
        ~CListener() {
223
 
                if (m_pListener)
224
 
                        CZNC::Get().GetManager().DelSockByAddr(m_pListener);
225
 
        }
226
 
 
227
 
        // Setters
228
 
        void SetSSL(bool b) { m_bSSL = b; }
229
 
        void SetIPV6(bool b) { m_bIPV6 = b; }
230
 
        void SetPort(unsigned short u) { m_uPort = u; }
231
 
        void SetBindHost(const CString& s) { m_sBindHost = s; }
232
 
        // !Setters
233
 
 
234
 
        // Getters
235
 
        bool IsSSL() const { return m_bSSL; }
236
 
        bool IsIPV6() const { return m_bIPV6; }
237
 
        unsigned short GetPort() const { return m_uPort; }
238
 
        const CString& GetBindHost() const { return m_sBindHost; }
239
 
        CRealListener* GetRealListener() const { return m_pListener; }
240
 
        // !Getters
241
 
 
242
 
        bool Listen() {
243
 
                if (!m_uPort || m_pListener) {
244
 
                        return false;
245
 
                }
246
 
 
247
 
                m_pListener = new CRealListener;
248
 
 
249
 
                bool bSSL = false;
250
 
#ifdef HAVE_LIBSSL
251
 
                if (IsSSL()) {
252
 
                        bSSL = true;
253
 
                        m_pListener->SetPemLocation(CZNC::Get().GetPemLocation());
254
 
                }
255
 
#endif
256
 
 
257
 
                return CZNC::Get().GetManager().ListenHost(m_uPort, "_LISTENER", m_sBindHost, bSSL, SOMAXCONN,
258
 
                                m_pListener, 0, m_bIPV6);
259
 
        }
260
 
private:
261
 
protected:
262
 
        bool                    m_bSSL;
263
 
        bool                    m_bIPV6;
264
 
        unsigned short  m_uPort;
265
 
        CString                 m_sBindHost;
266
 
        CRealListener*          m_pListener;
 
150
        time_t                 m_TimeStarted;
 
151
 
 
152
        enum ConfigState       m_eConfigState;
 
153
        vector<CListener*>     m_vpListeners;
 
154
        map<CString,CUser*>    m_msUsers;
 
155
        map<CString,CUser*>    m_msDelUsers;
 
156
        CSockManager           m_Manager;
 
157
 
 
158
        CString                m_sCurPath;
 
159
        CString                m_sHomePath;
 
160
        CString                m_sZNCPath;
 
161
 
 
162
        CString                m_sConfigFile;
 
163
        CString                m_sSkinName;
 
164
        CString                m_sStatusPrefix;
 
165
        CString                m_sISpoofFile;
 
166
        CString                m_sOrigISpoof;
 
167
        CString                m_sISpoofFormat;
 
168
        CString                m_sPidFile;
 
169
        VCString               m_vsVHosts;
 
170
        VCString               m_vsMotd;
 
171
        CFile                  m_LockFile;
 
172
        CFile*                 m_pISpoofLockFile;
 
173
        unsigned int           m_uiConnectDelay;
 
174
        unsigned int           m_uiAnonIPLimit;
 
175
        CGlobalModules*        m_pModules;
 
176
        unsigned long long     m_uBytesRead;
 
177
        unsigned long long     m_uBytesWritten;
 
178
        CConnectUserTimer     *m_pConnectUserTimer;
 
179
        TCacheMap<CString>     m_sConnectThrottle;
267
180
};
268
181
 
269
182
#endif // !_ZNC_H