summaryrefslogtreecommitdiffstats
path: root/src/kvilib/irc/kvi_ircmask.h
blob: 4efac6271ea0b273ab29ffd496ad9bbd361457e9 (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
#ifndef _KVI_IRCMASK_H_
#define _KVI_IRCMASK_H_

//=============================================================================
//
//   File : kvi_irctqmask.h
//   Creation date : Fri Jan 8 1999 19:50:35 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-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.
//
//=============================================================================

// originally this file was named kvi_ircuser.h and the class was KviIrcUser
// ported to UNICODE on 2004.10.28 1:50 am

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

//=============================================================================
// Irc user tqmask abstraction
//=============================================================================

class KVILIB_API KviIrcMask : public KviHeapObject
{
	friend class KviIrcUserList;
	friend class KviIrcUserChanList;
private:
	TQString m_szNick;
	TQString m_szUser;
	TQString m_szHost;
	static TQString m_szWild;
public:
	// Sets the nick for this user.<br>
	// If szNick is NULL or it points to an empty string the nick is set to "*".<br>
	void setNick(const TQString &szNick){ m_szNick = szNick.isEmpty() ? m_szWild : szNick; };
	// Sets the username for this user.<br>
	// If szUsername is NULL or it points to an empty string the username is set to "*".<br>
	void setUsername(const TQString &szUser){ m_szUser = szUser.isEmpty() ? m_szWild : szUser; };
	void setUser(const TQString &szUser){ m_szUser = szUser.isEmpty() ? m_szWild : szUser; };
	// Sets the host for this user.<br>
	// If szHost is NULL or it points to an empty string the host is set to "*".<br>
	void setHost(const TQString &szHost){ m_szHost = szHost.isEmpty() ? m_szWild : szHost; };
	// Sets the host , nick and username extracting it from an irc tqmask:<br>
	// nick!user@host<br>
	// The tqmask is terminated by end-of string null character or a character equal to c in the string.<br>
	// Returns the pointer to the end of the tqmask in the szMask string.(c or null-terminator)<br>
	//const char * setMask(const TQString &szMask,char c=' ');
	// Sets the host , nick and username extracting it from an userhost tqmask:<br>
	// nick[*]=<+|->user@host<br>
	// The tqmask is terminated by end-of string null char or a space character.<br>
	// Returns the pointer to the next non-space char in the szMask string or to the null-terminator<br>
	// If there are no more masks avaiable.
	// WARNING : the szMask pointer can NOT be NULL
	//const char *setUserhostMask(const TQString &szMask);
	// Returns the nick of this user.<br>
	// In the worst case you get a string == "*"<br>
	const TQString &nick() const { return m_szNick; };
	// DEPRECATED!
	const TQString &username() const { return m_szUser; };
	const TQString &user() const { return m_szUser; };
	const TQString &host() const { return m_szHost; };
	
	bool hasUser() const { return !(m_szUser.isEmpty() || (m_szUser == m_szWild)); };
	bool hasHost() const { return !(m_szHost.isEmpty() || (m_szHost == m_szWild)); };

	bool hasNumericHost() const;
	// Retuns in szMask the specified (if possible) tqmask of this user.<br>
	// If the host or username are not known , the tqmask may contain less information
	// than requested.<br>

	enum MaskType
	{
		NickUserHost = 0,           // nick!~user@machine.host.top (nick!~user@XXX.XXX.XXX.XXX) (default)
		NickUserNet = 1,            // 1 : nick!~user@*.abc.host.top       (nick!~user@XXX.XXX.XXX.*)
		NickUser = 2,               // 2 : nick!~user@*
		NickHost = 3,               // 3 : nick!*@machine.host.top    (nick!*@XXX.XXX.XXX.XXX)
		NickNet = 4,                // 4 : nick!*@*.abc.host.top          (nick!*@XXX.XXX.XXX.*)
		Nick = 5,                   // 5 : nick!*@*
		UserHost = 6,               // 6 : *!~user@machine.host.top    (*!~user@XXX.XXX.XXX.XXX)
		UserNet = 7,                // 7 : *!~user@*.abc.host.top          (*!~user@XXX.XXX.XXX.*)
		User = 8,                   // 8 : *!~user@*
		Host = 9,                   // 9 : *!*@machine.host.top       (*!*@XXX.XXX.XXX.XXX)
		Net = 10,                   // 10: *!*@*.abc.host.top             (*!*@XXX.XXX.XXX.*)
		NickCleanUserHost = 11,     // 11 : nick!*user@machine.host.top    (nick!*user@XXX.XXX.XXX.XXX)
		NickCleanUserNet = 12,      // 12 : nick!*user@*.abc.host.top          (nick!*user@XXX.XXX.XXX.*)
		NickCleanUser = 13,         // 13 : nick!*user@*
		CleanUserHost = 14,         // 14 : *!*user@machine.host.top    (*!user@*XXX.XXX.XXX.XXX)
		CleanUserNet = 15,          // 15 : *!*user@*.abc.host.top          (*!user@*XXX.XXX.XXX.*)
		CleanUser = 16,             // 16 : *!*user@*
		NickUserLargeNet = 17,      // 17 : nick!~user@*.host.top       (nick!~user@XXX.XXX.*)
		NickLargeNet = 18,          // 18 : nick!*@*.host.top          (nick!*@XXX.XXX.*)
		UserLargeNet = 19,          // 19 : *!~user@*.host.top          (*!~user@XXX.XXX.*)
		NickCleanUserLargeNet = 20, // 20 : nick!*user@*.host.top          (nick!*user@XXX.XXX.*)
		CleanUserLargeNet = 21,     // 21 : *!*user@*.host.top          (*!user@*XXX.XXX.*)
		// smart versions of the masks 17-21 that try take care of masked ip addresses
		// in the form xxx.xxx.INVALID-TOP-MASK
		NickUserSmartNet = 22,      // 22 : nick!~user@*.host.top       (nick!~user@XXX.XXX.*)
		NickSmartNet = 23,          // 23 : nick!*@*.host.top          (nick!*@XXX.XXX.*)
		UserSmartNet = 24,          // 24 : *!~user@*.host.top          (*!~user@XXX.XXX.*)
		NickCleanUserSmartNet = 25, // 25 : nick!*user@*.host.top          (nick!*user@XXX.XXX.*)
		CleanUserSmartNet = 26      // 26 : *!*user@*.host.top          (*!user@*XXX.XXX.*)
	};
	
	void tqmask(TQString &szMask,MaskType eMaskType = NickCleanUserHost) const;
	bool hasWildNick();

	// Wild external matches (this and external are wild)

//	bool matches(const char *szMask);
//	// passing 0 as one of params here means that it is a match by default
//	bool matches(const char *nick,const char *user,const char *host);
	bool matches(const KviIrcMask &tqmask) const;


	// Fixed external matches  (this is wild , external is fixed)
	bool matchesFixed(const TQString &nick,const TQString &user,const TQString &host) const;
//	bool matchesFixed(const TQString &szMask) const;
	bool matchesFixed(const KviIrcMask &tqmask) const;

	// Fixed internal matches (this is fixed , external is wild)
	//bool matchedBy(const TQString &szMask) const;
	bool matchedBy(const KviIrcMask &tqmask) const { return tqmask.matchesFixed(*this); };

	int nonWildChars();
	bool operator==(const KviIrcMask &user);
public:
	// Constructs an empty tqmask (*!*@*)
	KviIrcMask();
	// Constructs this KviIrcMask object from a string tqmask
	KviIrcMask(const TQString &szMask);
	// Carbon copy
	KviIrcMask(const KviIrcMask &ircUser)
		: m_szNick(ircUser.m_szNick), m_szUser(ircUser.m_szUser), m_szHost(ircUser.m_szHost) {};
	KviIrcMask(const TQString &nick,const TQString &user,const TQString &host)
		: m_szNick(nick), m_szUser(user), m_szHost(host) {};
private:
	int getIpDomainMaskLen() const;
	TQString getHostDomainMask() const;
	int getLargeIpDomainMaskLen() const;
	TQString getLargeHostDomainMask() const;
	// this is just a GUESS and must be called AFTER making sure that it is NOT a plain numeric IP
	bool hasMaskedIp() const;
};




#endif //_KVI_IRCMASK_H_