summaryrefslogtreecommitdiffstats
path: root/src/kvilib/ext/kvi_regusersdb.h
blob: 6d587ba1e76aa3225b1af4076ae4d0ece6c3628c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef _KVI_REGUSERSDB_H_
#define _KVI_REGUSERSDB_H_
//=================================================================================================
//
//   File : kvi_regusersdb.h
//   Creation date : Sat Sep 09 2000 15:30:56 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2000-2004 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=================================================================================================

//
// REGISTERED USERS
//     
//     Here we manage users resigered by mask and their (generic!) properties
//

#include "kvi_settings.h"
#include "kvi_heapobject.h"
#include "kvi_qstring.h"
#include "kvi_ircmask.h"
#include "kvi_debug.h"

#include "kvi_pointerlist.h"
#include "kvi_pointerhashtable.h"
#include <tqobject.h>

class KviRegisteredUserDataBase;

#ifndef _KVI_REGUSERDB_CPP_
	extern KVILIB_API KviRegisteredUserDataBase * g_pRegisteredUserDataBase;
#endif //!_KVI_REGUSERDB_CPP_

//=================================================================================================
//
// KviRegisteredUser
//

class KVILIB_API KviRegisteredUser : public KviHeapObject
{
	friend class KviRegisteredUserDataBase;
public:
	enum IgnoreFlags {
		Channel=1,
		Query=2,
		Notice=4,
		Ctcp=8,
		Invite=16,
		Dcc=32
	};

	KviRegisteredUser(const TQString &name);
	~KviRegisteredUser();
private:
	int                        m_iIgnoreFlags;
	bool                       m_bIgnoreEnabled;
	TQString                    m_szName;
	TQString					   m_szGroup;
	KviPointerHashTable<TQString,TQString>           * m_pPropertyDict;   // owned properties
	KviPointerList<KviIrcMask>     * m_pMaskList;       // owned masks
protected:
	// mask ownership is transferred! (always!) returns false if the mask was already there
	bool addMask(KviIrcMask * mask);
	bool removeMask(KviIrcMask * mask);
	KviIrcMask * findMask(const KviIrcMask &mask);
public:
	int  ignoreFlags()                  { return m_iIgnoreFlags; };
	void setIgnoreFlags(int flags)      {m_iIgnoreFlags=flags; };
	bool ignoreEnagled()                { return m_bIgnoreEnabled; };
	void setIgnoreEnabled(bool enabled) {m_bIgnoreEnabled=enabled;};
	bool isIgnoreEnabledFor(IgnoreFlags flag);

	const TQString &name(){ return m_szName; };
	bool matches(const KviIrcMask &mask);
	bool matchesFixed(const KviIrcMask &mask);
	bool matchesFixed(const TQString &nick,const TQString &user,const TQString &host);

	void setProperty(const TQString &name,const TQString &value);
	void setProperty(const TQString &name,bool value);
	
	void setGroup(const TQString &name) { m_szGroup=name; };
	const TQString &group(){ return m_szGroup; };

	const TQString & getProperty(const TQString &name);       // returns 0 if the property is not there
	bool getProperty(const TQString &name,TQString &value); // returns false if the property is not there
	bool getBoolProperty(const TQString &name,bool def=FALSE);           // returns true if the property is there and is true
	// the propertyDict may be 0!
	KviPointerHashTable<TQString,TQString> * propertyDict(){ return m_pPropertyDict; };
	// this is never zero (but may contain no masks)
	KviPointerList<KviIrcMask> * maskList(){ return m_pMaskList; };
};

//============================================================================================================
//
// KviRegisteredUserGroup
//

class KVILIB_API KviRegisteredUserGroup : public KviHeapObject
{
	friend class KviRegisteredUserDataBase;
public:
	KviRegisteredUserGroup(const TQString &name);
	~KviRegisteredUserGroup();
	
	void setName(const TQString &name) { m_szName=name; };
	const TQString &name(){ return m_szName; };
private:
	TQString                      m_szName;
};
//============================================================================================================
//
// KviRegisteredMask
//

class KVILIB_API KviRegisteredMask
{
private:
	KviRegisteredUser * m_pUser;               // pointer , not owned!
	KviIrcMask        * m_pMask;               // pointer , not owned!
	int                 m_iMaskNonWildChars;
public:
	KviRegisteredMask(KviRegisteredUser * u,KviIrcMask * m);
	~KviRegisteredMask(){};
public:
	int                 nonWildChars(){ return m_iMaskNonWildChars; };
	KviRegisteredUser * user(){ return m_pUser; };
	KviIrcMask        * mask(){ return m_pMask; };
};

typedef KviPointerList<KviRegisteredMask> KviRegisteredMaskList;

//=================================================================================================
//
// KviRegisteredUsersDb
//
//    Manages a set of KviRegisteredUser instances stored in the m_pUserDict dictionary
//    The users are identified by masks stored in m_pMaskDict and m_pWildMaskList
//    m_pMaskDict contains lists of non wild-nick KviRegisteredMask that point to users
//    m_pWildMaskList is a list of wild-nick KviRegisteredMask that point to users
//

class KVILIB_API KviRegisteredUserDataBase : public TQObject
{
	TQ_OBJECT
  
public:
	KviRegisteredUserDataBase();
	~KviRegisteredUserDataBase();
private:
	KviPointerHashTable<TQString,KviRegisteredUser>     * m_pUserDict; // unique namespace, owns the objects, does not copy keys
	KviPointerHashTable<TQString,KviRegisteredMaskList> * m_pMaskDict; // owns the objects, copies the keys
	KviRegisteredMaskList          * m_pWildMaskList; // owns the objects
	KviPointerHashTable<TQString,KviRegisteredUserGroup>* m_pGroupDict;
public:
	void copyFrom(KviRegisteredUserDataBase * db);
	KviRegisteredUser * addUser(const TQString &name); // returns 0 if already there
	KviRegisteredUser * getUser(const TQString &name); // returns existing or adds
	bool removeUser(const TQString &name);
	bool removeGroup(const TQString &name);
	KviRegisteredUser * findUserByName(const TQString &name){ return m_pUserDict->find(name); };
	// mask must be allocated on the heap and the ownership is transferred!
	// returns non zero if there is already an user with this mask (returns the pointer to it!)
	KviRegisteredUser * addMask(KviRegisteredUser * u,KviIrcMask * mask);
	bool removeMaskByPointer(KviIrcMask * mask);
	bool removeMask(const KviIrcMask &mask);
	KviRegisteredUser * findMatchingUser(const TQString &nick,const TQString &user,const TQString &host);
	KviRegisteredUser * findUserWithMask(const KviIrcMask &mask);
	KviRegisteredMask * findExactMask(const KviIrcMask &mask);
	KviRegisteredMask * findMatchingMask(const TQString &nick,const TQString &user,const TQString &host);
	//Only used in few places (actually one) of the code, but lot of times;perfect for inlining...
	//bool isIgnoredUser(const char * nick,const char * user,const char * host);
	void load(const TQString &filename);
	void save(const TQString &filename);

	KviPointerHashTable<TQString,KviRegisteredUser> * userDict(){ return m_pUserDict; };
	KviPointerHashTable<TQString,KviRegisteredUserGroup>* groupDict() { return m_pGroupDict; };
	
	KviRegisteredUserGroup* addGroup(const TQString &name);
signals:
	void userRemoved(const TQString&);
	void userChanged(const TQString&);
	void userAdded  (const TQString&);
	void databaseCleared();
};


#endif //_KVI_REGUSERSDB_H_