aboutsummaryrefslogtreecommitdiffstats
path: root/src/kvilib/irc/KviIrcMask.cpp
blob: f3ccaf16b8315cadd7188ad80367a1a790ba3e9c (about) (plain) (blame)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//=============================================================================
//
//   File : KviIrcMask.cpp
//   Creation date : Fri Jan 8 1999 20:56:07 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 1999-2010 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 option) 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.
//
//=============================================================================

#include "kvi_debug.h"
#include "KviIrcMask.h"
#include "KviQString.h"
#include "KviRegExp.h"

/*
	@doc: irc_masks
	@title:
		IRC masks
	@type:
		generic
	@short:
		Description of the standard IRC masks
	@keyterms:
		irc masks, nickname, username, hostname, wildcard
	@body:
		[big]Simple masks[/big]
		An IRC mask is a string in a special format that identifies a user on IRC.[br]
		The standard basic format is:[br]
		[b]<nick>!<username>@<host>[/b][br]
		The <nick> part contains the nickname with that the user is widely known across the network.[br]
		The nickname format is generally restricted by the IRC network rules: usually it has a maximum
		length (9 on actual IrcNet servers for example), and can contain only a defined set of characters.
		Just as example, the character '!' obviously can't be included in a nickname.[br]
		The <username> part is the machine username of the remote user: this is usually
		retrieved by the IRC server at connect time by contacting the Ident service on the user's machine.
		Some IRC servers allow specifying this username inside the login messages and do not connect
		to the Ident service at all.[br]
		The <username> often has a special prefix character added by the IRC server:[br]
		this is rather server specific protocol, but the prefixes are somewhat standardized and
		the common meanings of them are:[br]
		[pre]
			noprefix: I line with Ident
			^: I line with OTHER type Ident
			~: I line, no Ident
			+: i line with Ident
			=: i line with OTHER type Ident
			-: i line, no Ident
		[/pre]
		So finally you can find <username> strings like [i]~pragma[/i] or [i]^pragma[/i], where [i]pragma[/i]
		is the system username of the irc-user and ~ and ^ are prefixes.[br]
		The <host> part is the hostname of the remote user.[br]
		In most cases it is the human-readable format of the host name, but sometimes
		it happens to be an IP-address (when the host has no reverse DNS entry).[br]
		The IP address can be either in IPv4 format or in IPv6 format.[br]
		Some (weird from my point of view) servers hide certain parts of the IP address to
		prevent attacks to the user's machine.[br]
		Here are some examples of full irc-masks:[br]
		Pragma!^pragma@staff.kvirc.net[br]
		[jazz]!~jazz@jazz.myhome.com[br]
		luke!=skywalker@212.213.41.12[br]
		HAN!^solo@ff0f:a0a0:1011::ea80:1[br]
		Darth!vader@210.11.12.XXX[br]
		The irc-masks are [b]case insensitive[/b].
		[big]Wildcard masks[/big]
		In some contexts the irc-masks can contain [b]*[/b] and [b]?[/b] wildcards.[br]
		The wild masks are used to [i]match[/i] a user within a set of them.[br]
		[b]*[/b] matches any sequence (eventually empty) of characters and [b]?[/b] matches a single character.[br]
		Wildcards are allowed only in the <nick>, <user> and <host> part: so the
		[i]wildest[/i] mask possible is:[br]
		[b]*!*@*[/b][br]
		that designates [i]any nickname, any username on any host[/i].[br]
		Here are some examples of wild masks:[br]
		Pragma!*pragma@212.101.102.*: matches any user with nickname [i]Pragma[/i], username that ends with [i]pragma[/i] and
		coming from any machine on the 212.101.102 network.[br]
		*!solo@*.starwars.org: matches any nick with username solo (no prefix!) coming from any machine in
		the starwars.org domain.[br]
		Pragma!*@*: matches any user with nickname [i]Pragma[/i].[br]
*/

static unsigned char ucMaskTable[27][3] = {
	{ 0, 0, 0 }, //0 means normal block
	{ 0, 0, 2 }, //2 in the third field means type *.abc.host.top (or XXX.XXX.XXX.*) host mask
	{ 0, 0, 1 }, //2 in the second field means *user (strip prefixes)
	{ 0, 1, 0 }, //1 means *
	{ 0, 1, 2 }, //3 in the third field means type *.host.top (or XXX.XXX.*) host mask
	{ 0, 1, 1 }, //4 in the third field is like 3 but tries to detect masked ip addresses too
	{ 1, 0, 0 },
	{ 1, 0, 2 },
	{ 1, 0, 1 },
	{ 1, 1, 0 },
	{ 1, 1, 2 },
	{ 0, 2, 0 },
	{ 0, 2, 2 },
	{ 0, 2, 1 },
	{ 1, 2, 0 },
	{ 1, 2, 2 },
	{ 1, 2, 1 },
	{ 0, 0, 3 },
	{ 0, 1, 3 },
	{ 1, 0, 3 },
	{ 0, 2, 3 },
	{ 1, 2, 3 },
	{ 0, 0, 4 },
	{ 0, 1, 4 },
	{ 1, 0, 4 },
	{ 0, 2, 4 },
	{ 1, 2, 4 }
};

KviIrcMask::KviIrcMask()
{
	m_szHost = m_szWild;
	m_szUser = m_szWild;
	m_szNick = m_szWild;
}

KviIrcMask::KviIrcMask(const QString & szMask)
{
	static QString szWild("*");
	const QChar * pChar = szMask.constData();
	if(pChar)
	{
		const QChar * pChar2 = pChar;
		while(pChar2->unicode() && (pChar2->unicode() != '!'))
			pChar2++;
		if(pChar2->unicode())
		{
			if(pChar2 != pChar)
				m_szNick.setUnicode(pChar, pChar2 - pChar);
			else
				m_szNick = szWild; // ???
		}
		else
		{
			if(pChar2 != pChar)
				m_szNick.setUnicode(pChar, pChar2 - pChar);
			else
				m_szNick = szWild; // ???
			m_szUser = szWild;
			m_szHost = szWild;
			return;
		}
		pChar2++;
		pChar = pChar2;
		while(pChar2->unicode() && (pChar2->unicode() != '@'))
			pChar2++;
		if(pChar2->unicode())
		{
			if(pChar2 != pChar)
				m_szUser.setUnicode(pChar, pChar2 - pChar);
			else
				m_szUser = szWild; // ???
		}
		else
		{
			if(pChar2 != pChar)
				m_szUser.setUnicode(pChar, pChar2 - pChar);
			else
				m_szUser = szWild; // ???
			m_szHost = szWild;
			return;
		}
		pChar2++;
		pChar = pChar2;
		while(pChar2->unicode())
			pChar2++;
		if(pChar2 != pChar)
			m_szHost.setUnicode(pChar, pChar2 - pChar);
		else
			m_szHost = szWild; // ???
	}
	else
	{
		m_szUser = szWild;
		m_szHost = szWild;
		m_szNick = szWild;
	}
}

QString KviIrcMask::m_szWild("*");

bool KviIrcMask::hasNumericHost() const
{
	const QChar * pChar = m_szHost.constData();
	if(!pChar)
		return false;
	int iPoints = 0;
	int iDoublePoints = 0;
	unsigned short uc;

	if(m_szHost.endsWith("=")) // for CR servers, see ticket #358
		return true;

	while((uc = pChar->unicode()))
	{
		if(uc == '.')
			iPoints++; // ipv6 masks can contain dots too!
		else
		{
			if(uc == ':')
				iDoublePoints++;
			else
			{
				if((uc < '0') || (uc > '9'))
				{
					uc = pChar->toUpper().unicode();
					if((uc < 'A') || (uc > 'F'))
						return false;
				}
			}
		}
		pChar++;
	}
	return ((iPoints == 3) || (iDoublePoints > 1));
}

void KviIrcMask::mask(QString & szMask, MaskType eMaskType) const
{
	if((((int)eMaskType) > 26) || (((int)eMaskType) < 0))
		eMaskType = NickUserHost;
	szMask = ucMaskTable[((int)eMaskType)][0] ? m_szWild : m_szNick;
	szMask.append("!");
	switch(ucMaskTable[((int)eMaskType)][1])
	{
		case 0:
			szMask.append(m_szUser);
			break;
		case 1:
			szMask.append(m_szWild);
			break;
		default:
			if(m_szUser.length() > 0)
			{
				if(m_szUser[0].unicode() != '*')
					szMask.append(m_szWild);
				if((m_szUser[0].unicode() == '~') || (m_szUser[0].unicode() == '^') || (m_szUser[0].unicode() == '+') || (m_szUser[0].unicode() == '-') || (m_szUser[0].unicode() == '='))
					szMask.append(m_szUser.right(m_szUser.length() - 1));
				else
					szMask.append(m_szUser);
			}
			break;
	}
	szMask.append('@');
	switch(ucMaskTable[((int)eMaskType)][2])
	{
		case 0:
			szMask.append(m_szHost);
			break;
		case 1:
			szMask.append(m_szWild);
			break;
		case 2:
			if(m_szHost != m_szWild)
			{
				if(hasNumericHost())
				{
					QString szHost(m_szHost.left(getIpDomainMaskLen()));
					szMask.append(szHost);
					szMask.append(m_szWild);
				}
				else
				{
					szMask.append(m_szWild);
					szMask.append(getHostDomainMask());
				}
			}
			else
			{
				szMask.append(m_szWild);
			}
			break;
		case 3:
			if(m_szHost != m_szWild)
			{
				if(hasNumericHost())
				{
					QString szHost(m_szHost.left(getLargeIpDomainMaskLen()));
					szMask.append(szHost);
					szMask.append(m_szWild);
				}
				else
				{
					szMask.append(m_szWild);
					szMask.append(getLargeHostDomainMask());
				}
			}
			else
			{
				szMask.append(m_szWild);
			}
			break;
		default: // case 4 and others
			if(m_szHost != m_szWild)
			{
				if(hasNumericHost() || hasMaskedIp())
				{
					QString szHost(m_szHost.left(getLargeIpDomainMaskLen()));
					szMask.append(szHost);
					szMask.append(m_szWild);
				}
				else
				{
					szMask.append(m_szWild);
					szMask.append(getLargeHostDomainMask());
				}
			}
			else
			{
				szMask.append(m_szWild);
			}
			break;
	}
}

bool KviIrcMask::matches(const KviIrcMask & mask) const
{
	if(KviQString::matchWildExpressions(m_szNick, mask.m_szNick))
	{
		if(KviQString::matchWildExpressions(m_szUser, mask.m_szUser))
		{
			if(KviQString::matchWildExpressions(m_szHost, mask.m_szHost))
				return true;
		}
	}
	return false;
}

bool KviIrcMask::matchesFixed(const KviIrcMask & mask) const
{
	if(matchWildString(m_szNick, mask.m_szNick))
	{
		if(matchWildString(m_szUser, mask.m_szUser))
		{
			if(matchWildString(m_szHost, mask.m_szHost))
				return true;
		}
	}
	return false;
}

bool KviIrcMask::matchesFixed(const QString & szNick, const QString & szUser, const QString & szHost) const
{
	if(!matchWildString(m_szNick, szNick))
		return false;
	if(!matchWildString(m_szUser, szUser))
		return false;
	if(!matchWildString(m_szHost, szHost))
		return false;
	return true;
}

bool KviIrcMask::matchWildString(const QString & szExp, const QString & szStr) const
{
	QString szWildcard;
	QChar * pPtr = (QChar *)szExp.constData();

	if(!pPtr)
		return false;

	while(pPtr->unicode())
	{
		if((pPtr->unicode() == '[') || (pPtr->unicode() == ']'))
		{
			szWildcard.append("[");
			szWildcard.append(*pPtr);
			szWildcard.append("]");
		}
		else
		{
			szWildcard.append(*pPtr);
		}
		pPtr++;
	}
	KviRegExp re(szWildcard, KviRegExp::CaseInsensitive, KviRegExp::Wildcard);

	return re.exactMatch(szStr);
}

int KviIrcMask::getIpDomainMaskLen() const
{
	int iLen = m_szHost.length();
	const QChar * pChar = m_szHost.unicode();
	const QChar * pChar2 = pChar;
	pChar += iLen;
	if(pChar2 < pChar)
	{
		pChar--;
		while((pChar2 < pChar) && (pChar->unicode() != '.') && (pChar->unicode() != ':'))
			pChar--;
	}
	// 000.000.000.000
	//            pChar
	//
	return (pChar == pChar2) ? 0 : ((pChar - pChar2) + 1);
}

int KviIrcMask::getLargeIpDomainMaskLen() const
{
	int iLen = m_szHost.length();
	const QChar * pChar = m_szHost.unicode();
	const QChar * pChar2 = pChar;
	pChar += iLen;
	if(pChar2 < pChar)
	{
		pChar--;
		while((pChar2 < pChar) && (pChar->unicode() != '.') && (pChar->unicode() != ':'))
			pChar--;
		if(pChar2 < pChar)
		{
			pChar--;
			while((pChar2 < pChar) && (pChar->unicode() != '.') && (pChar->unicode() != ':'))
				pChar--;
		}
	}
	// 000.000.000.000
	//        pChar
	//
	return (pChar == pChar2) ? 0 : ((pChar - pChar2) + 1);
}

QString KviIrcMask::getHostDomainMask() const
{
	int iLen = m_szHost.length();
	const QChar * pChar = m_szHost.constData();
	if(!pChar)
		return QString();
	const QChar * pChar2 = pChar;
	while(pChar->unicode() && pChar->unicode() != '.')
		pChar++;
	QString szRet(pChar, iLen - (pChar - pChar2));
	return szRet;
}

QString KviIrcMask::getLargeHostDomainMask() const
{
	int iLen = m_szHost.length();
	const QChar * pChar = m_szHost.unicode();
	const QChar * pChar2 = pChar;
	pChar += iLen;

	if(pChar2 < pChar)
	{
		pChar--;
		while((pChar2 < pChar) && (pChar->unicode() != '.'))
			pChar--;
		if(pChar2 < pChar)
		{
			pChar--;
			while((pChar2 < pChar) && (pChar->unicode() != '.'))
				pChar--;
		}
	}

	// xyz.klm.abc.host.top
	//            pChar

	QString szRet(pChar, iLen - (pChar - pChar2));
	return szRet;
}

bool KviIrcMask::hasMaskedIp() const
{
	int iLen = m_szHost.length();
	const QChar * pChar = m_szHost.unicode();
	const QChar * pChar2 = pChar;
	if(iLen == 0)
		return false;
	//run to the end
	pChar += iLen;
	const QChar * pChar3 = pChar;
	pChar--;
	while((pChar2 < pChar) && (pChar->unicode() != '.'))
		pChar--;
	return ((pChar3 - pChar) > 4); // at the moment 4 should be enough : the largest top part is "name"
}

bool KviIrcMask::operator==(const KviIrcMask & user) const
{
	if(KviQString::equalCI(m_szNick, user.m_szNick))
	{
		if(KviQString::equalCI(m_szUser, user.m_szUser))
		{
			if(KviQString::equalCI(m_szHost, user.m_szHost))
				return true;
		}
	}
	return false;
}

bool KviIrcMask::hasWildNick()
{
	const QChar * pAux = m_szNick.constData();
	if(!pAux)
		return false;
	unsigned short uc;
	while((uc = pAux->unicode()))
	{
		if((uc == '*') || (uc == '?'))
			return true;
		pAux++;
	}
	return false;
}

int KviIrcMask::nonWildChars()
{
	int iCnt = 0;
	const QChar * pAux = m_szNick.constData();
	if(!pAux)
		return 0;
	unsigned short uc;

	while((uc = pAux->unicode()))
	{
		if((uc != '*') && (uc != '?'))
			iCnt++;
		pAux++;
	}

	pAux = m_szUser.constData();
	while((uc = pAux->unicode()))
	{
		if((uc != '*') && (uc != '?'))
			iCnt++;
		pAux++;
	}

	pAux = m_szHost.constData();
	while((uc = pAux->unicode()))
	{
		if((uc != '*') && (uc != '?'))
			iCnt++;
		pAux++;
	}
	return iCnt;
}