File: Nick.cpp

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (488 lines) | stat: -rw-r--r-- 9,524 bytes parent folder | download | duplicates (3)
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
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 Gunnar Beutner                                      *
 *                                                                             *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

#include "StdAfx.h"

/**
 * CNick
 *
 * Constructs a new nick object.
 *
 * @param Nick the nickname of the user
 * @param Owner the owning channel of this nick object
 */
CNick::CNick(const char *Nick, CChannel *Owner) {
	assert(Nick != NULL);

	SetOwner(Owner);

	m_Nick = ustrdup(Nick);

	CHECK_ALLOC_RESULT(m_Nick, ustrdup) { } CHECK_ALLOC_RESULT_END;

	m_Prefixes = NULL;
	m_Site = NULL;
	m_Realname = NULL;
	m_Server = NULL;
	m_Creation = g_CurrentTime;
	m_IdleSince = m_Creation;
}

/**
 * ~CNick
 *
 * Destroys a nick object.
 */
CNick::~CNick() {
	ufree(m_Nick);
	ufree(m_Prefixes);
	ufree(m_Site);
	ufree(m_Realname);
	ufree(m_Server);

	for (unsigned int i = 0; i < m_Tags.GetLength(); i++) {
		ufree(m_Tags[i].Name);
		ufree(m_Tags[i].Value);
	}
}

/**
 * SetNick
 *
 * Sets the user's nickname.
 *
 * @param Nick the new nickname
 */
bool CNick::SetNick(const char *Nick) {
	char *NewNick;

	assert(Nick != NULL);

	NewNick = ustrdup(Nick);

	CHECK_ALLOC_RESULT(m_Nick, ustrdup) {
		return false;
	} CHECK_ALLOC_RESULT_END;

	ufree(m_Nick);
	m_Nick = NewNick;

	return true;
}

/**
 * GetNick
 *
 * Returns the current nick of the user.
 */
const char *CNick::GetNick(void) const {
	return m_Nick;
}

/**
 * IsOp
 *
 * Returns whether the user is an op.
 */
bool CNick::IsOp(void) const {
	return HasPrefix('@');
}

/**
 * IsVoice
 *
 * Returns whether the user is voiced.
 */
bool CNick::IsVoice(void) const {
	return HasPrefix('+');
}

/**
 * IsHalfop
 *
 * Returns whether the user is a half-op.
 */
bool CNick::IsHalfop(void) const {
	return HasPrefix('%');
}

/**
 * HasPrefix
 *
 * Returns whether the user has the given prefix.
 *
 * @param Prefix the prefix (e.g. @, +)
 */
bool CNick::HasPrefix(char Prefix) const {
	if (m_Prefixes != NULL && strchr(m_Prefixes, Prefix) != NULL) {
		return true;
	} else {
		return false;
	}
}

/**
 * AddPrefix
 *
 * Adds a prefix to a user.
 *
 * @param Prefix the new prefix
 */
bool CNick::AddPrefix(char Prefix) {
	char *Prefixes;
	size_t LengthPrefixes = m_Prefixes ? strlen(m_Prefixes) : 0;

	Prefixes = (char *)urealloc(m_Prefixes, LengthPrefixes + 2);

	CHECK_ALLOC_RESULT(Prefixes, realloc) {
		return false;
	} CHECK_ALLOC_RESULT_END;

	m_Prefixes = Prefixes;
	m_Prefixes[LengthPrefixes] = Prefix;
	m_Prefixes[LengthPrefixes + 1] = '\0';

	return true;
}

/**
 * RemovePrefix
 *
 * Removes the specified prefix from the user.
 *
 * @param Prefix the prefix
 */
bool CNick::RemovePrefix(char Prefix) {
	int a = 0;
	size_t LengthPrefixes;

	if (m_Prefixes == NULL) {
		return true;
	}

	LengthPrefixes = strlen(m_Prefixes);

	char *Copy = (char *)umalloc(LengthPrefixes + 1);

	CHECK_ALLOC_RESULT(Copy, umalloc) {
		return false;
	} CHECK_ALLOC_RESULT_END;

	for (unsigned int i = 0; i < LengthPrefixes; i++) {
		if (m_Prefixes[i] != Prefix) {
			Copy[a++] = m_Prefixes[i];
		}
	}

	Copy[a] = '\0';

	ufree(m_Prefixes);
	m_Prefixes = Copy;

	return true;
}

/**
 * SetPrefixes
 *
 * Sets the prefixes for a user.
 *
 * @param Prefixes the new prefixes
 */
bool CNick::SetPrefixes(const char *Prefixes) {
	char *dupPrefixes;

	if (Prefixes) {
		dupPrefixes = ustrdup(Prefixes);

		CHECK_ALLOC_RESULT(dupPrefixes, ustrdup) {
			return false;
		} CHECK_ALLOC_RESULT_END;
	} else {
		dupPrefixes = NULL;
	}

	ufree(m_Prefixes);
	m_Prefixes = dupPrefixes;

	return true;
}

/**
 * GetPrefixes
 *
 * Returns all prefixes for a user.
 */
const char *CNick::GetPrefixes(void) const {
	return m_Prefixes;
}

/**
 * IMPL_NICKSET
 *
 * Implements a Set*() function
 *
 * @param Name the name of the attribute
 * @param NewValue the new value
 * @param Static indicates whether the attribute can be modified
 *		  once its initial value has been set
 */
#define IMPL_NICKSET(Name, NewValue, Static) \
	char *DuplicateValue; \
\
	if ((Static && Name != NULL) || NewValue == NULL) { \
		return false; \
	} \
\
	DuplicateValue = ustrdup(NewValue); \
\
	if (DuplicateValue == NULL) { \
		LOGERROR("ustrdup() failed. New " #Name " was lost (%s, %s).", m_Nick, NewValue); \
\
		return false; \
	} else { \
		ufree(Name); \
		Name = DuplicateValue; \
\
		return true; \
	}

/**
 * SetSite
 *
 * Sets the site (ident\@host) for a user.
 *
 * @param Site the user's new site
 */
bool CNick::SetSite(const char *Site) {
	IMPL_NICKSET(m_Site, Site, false);
}

/**
 * SetRealname
 *
 * Sets the user's realname.
 *
 * @param Realname the new realname
 */
bool CNick::SetRealname(const char *Realname) {
	IMPL_NICKSET(m_Realname, Realname, true);
}

/**
 * SetServer
 *
 * Sets the server for a user.
 *
 * @param Server the server which the user is using
 */
bool CNick::SetServer(const char *Server) {
	IMPL_NICKSET(m_Server, Server, true);
}

/**
 * InternalGetSite
 *
 * Returns the user's site without querying other nick
 * objects if the information is not available.
 */
const char *CNick::InternalGetSite(void) const {
	if (m_Site == NULL) {
		return NULL;
	}

	char *Host = strstr(m_Site, "!");

	if (Host) {
		return Host + 1;
	} else {
		return m_Site;
	}
}

/**
 * InternalGetRealname
 *
 * Returns the user's realname without querying other nick
 * objects if the information is not available.
 */
const char *CNick::InternalGetRealname(void) const {
	return m_Realname;
}

/**
 * InternalGetServer
 *
 * Returns the user's server without querying other nick
 * objects if the information is not available.
 */
const char *CNick::InternalGetServer(void) const {
	return m_Server;
}

/**
 * IMPL_NICKACCESSOR
 *
 * Implements a Get*() function
 *
 * @param Name the name of the attribute
 */
#define IMPL_NICKACCESSOR(Name) \
	const char *Value; \
	int a = 0; \
\
	if ((Value = Name()) != NULL) { \
		return Value; \
	} \
\
	while (hash_t<CChannel *> *Chan = GetOwner()->GetOwner()->GetChannels()->Iterate(a++)) { \
		if (!Chan->Value->HasNames()) \
			continue; \
\
		CNick *NickObj = Chan->Value->GetNames()->Get(m_Nick); \
\
		if (NickObj && strcasecmp(NickObj->GetNick(), m_Nick) == 0 && NickObj->Name() != NULL) \
			return NickObj->Name(); \
	} \
\
	return NULL;

/**
 * GetSite
 *
 * Returns the user's site.
 */
const char *CNick::GetSite(void) const {
	IMPL_NICKACCESSOR(InternalGetSite)
}

/**
 * GetRealname
 *
 * Returns the user's realname.
 */
const char *CNick::GetRealname(void) const {
	IMPL_NICKACCESSOR(InternalGetRealname)
}

/**
 * GetServer
 *
 * Returns the user's server.
 */
const char *CNick::GetServer(void) const {
	IMPL_NICKACCESSOR(InternalGetServer)
}

/**
 * GetChanJoin
 *
 * Returns a timestamp which determines when
 * the user joined the channel.
 */
time_t CNick::GetChanJoin(void) const {
	return m_Creation;
}

/**
 * GetIdleSince
 *
 * Returns a timestamp which determines when
 * the user last said something.
 */
time_t CNick::GetIdleSince(void) const {
	return m_IdleSince;
}

/**
 * SetIdleSince
 *
 * Sets the timestamp of the user's last channel PRIVMSG.
 *
 * @param Time the new timestamp
 */
bool CNick::SetIdleSince(time_t Time) {
	m_IdleSince = Time;

	return true;
}

/**
 * GetTag
 *
 * Returns the value of a user-specific tag.
 *
 * @param Name the name of the tag
 */
const char *CNick::GetTag(const char *Name) const {
	for (unsigned int i = 0; i < m_Tags.GetLength(); i++) {
		if (strcasecmp(m_Tags[i].Name, Name) == 0) {
			return m_Tags[i].Value;
		}
	}

	return NULL;
}

/**
 * SetTag
 *
 * Sets a user-specific tag.
 *
 * @param Name the name of the tag
 * @param Value the value of the tag
 */
bool CNick::SetTag(const char *Name, const char *Value) {
	nicktag_t NewTag;

	if (Name == NULL) {
		return false;
	}

	for (unsigned int i = 0; i < m_Tags.GetLength(); i++) {
		if (strcasecmp(m_Tags[i].Name, Name) == 0) {
			ufree(m_Tags[i].Name);
			ufree(m_Tags[i].Value);

			m_Tags.Remove(i);

			break;
		}
	}

	if (Value == NULL) {
		return true;
	}

	NewTag.Name = ustrdup(Name);

	CHECK_ALLOC_RESULT(NewTag.Name, ustrdup) {
		return false;
	} CHECK_ALLOC_RESULT_END;

	NewTag.Value = ustrdup(Value);

	CHECK_ALLOC_RESULT(NewTag.Value, ustrdup) {
		ufree(NewTag.Name);

		return false;
	} CHECK_ALLOC_RESULT_END;

	return m_Tags.Insert(NewTag);
}