File: Channel.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 (744 lines) | stat: -rw-r--r-- 15,670 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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*******************************************************************************
 * 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"

/**
 * CChannel
 *
 * Constructs a new channel object.
 *
 * @param Name the name of the channel
 * @param Owner the owner of the channel object
 */
CChannel::CChannel(const char *Name, CIRCConnection *Owner, safe_box_t Box) {
	SetOwner(Owner);
	SetBox(Box);

	m_Name = ustrdup(Name);
	CHECK_ALLOC_RESULT(m_Name, strdup) { } CHECK_ALLOC_RESULT_END;

	m_Timestamp = g_CurrentTime;
	m_Creation = 0;
	m_Topic = NULL;
	m_TopicNick = NULL;
	m_TopicStamp = 0;
	m_HasTopic = 0;

	m_Nicks.RegisterValueDestructor(DestroyObject<CNick>);

	m_HasNames = false;
	m_ModesValid = false;

	m_HasBans = false;
	m_TempModes = NULL;

	m_Banlist = unew CBanlist(this);

	if (Box != NULL) {
		time_t Temp;
		const char *TempStr;

		safe_set_ro(Box, 1);

		Temp = safe_get_integer(Box, "CreationTimestamp");

		if (Temp != 0) {
			m_Creation = Temp;
		}

		TempStr = safe_get_string(Box, "Topic");

		if (TempStr != NULL) {
			m_Topic = ustrdup(TempStr);
		}

		TempStr = safe_get_string(Box, "TopicNick");

		if (TempStr != NULL) {
			m_TopicNick = ustrdup(TempStr);
		}

		m_TopicStamp = safe_get_integer(Box, "TopicTimestamp");
		m_HasTopic = safe_get_integer(Box, "HasTopic");

		safe_set_ro(Box, 0);
	}
}

/**
 * ~CChannel
 *
 * Destructs a channel object.
 */
CChannel::~CChannel() {
	ufree(m_Name);

	ufree(m_Topic);
	ufree(m_TopicNick);
	ufree(m_TempModes);

	for (unsigned int i = 0; i < m_Modes.GetLength(); i++) {
		ufree(m_Modes[i].Parameter);
	}

	delete m_Banlist;
}

/**
 * GetName
 *
 * Returns the name of the channel.
 */
const char *CChannel::GetName(void) const {
	return m_Name;
}

/**
 * GetChannelModes
 *
 * Returns the channel's modes.
 */
RESULT<const char *> CChannel::GetChannelModes(void) {
	unsigned int i;
	size_t Size;
	char *NewTempModes;
	char ModeString[2];
	int ModeType;

	if (m_TempModes != NULL) {
		RETURN(const char *, m_TempModes);
	}

	Size = m_Modes.GetLength() + 1024;
	m_TempModes = (char *)umalloc(Size);

	CHECK_ALLOC_RESULT(m_TempModes, umalloc) {
		THROW(const char *, Generic_OutOfMemory, "umalloc() failed.");
	} CHECK_ALLOC_RESULT_END;

	strmcpy(m_TempModes, "+", Size);

	for (i = 0; i < m_Modes.GetLength(); i++) {
		ModeType = GetOwner()->RequiresParameter(m_Modes[i].Mode);

		if (m_Modes[i].Mode != '\0' && ModeType != 3) {
			ModeString[0] = m_Modes[i].Mode;
			ModeString[1] = '\0';

			strmcat(m_TempModes, ModeString, Size);
		}
	}

	for (i = 0; i < m_Modes.GetLength(); i++) {
		int ModeType = GetOwner()->RequiresParameter(m_Modes[i].Mode);

		if (m_Modes[i].Mode != '\0' && m_Modes[i].Parameter && ModeType != 3) {
			strmcat(m_TempModes, " ", Size);

			if (strlen(m_TempModes) + strlen(m_Modes[i].Parameter) > Size) {
				Size += strlen(m_Modes[i].Parameter) + 1024;
				NewTempModes = (char *)urealloc(m_TempModes, Size);

				CHECK_ALLOC_RESULT(NewTempModes, urealloc) {
					ufree(m_TempModes);
					m_TempModes = NULL;

					THROW(const char *, Generic_OutOfMemory, "urealloc() failed.");
				} CHECK_ALLOC_RESULT_END;

				m_TempModes = NewTempModes;
			}

			strmcat(m_TempModes, m_Modes[i].Parameter, Size);
		}
	}

	RETURN(const char *, m_TempModes);
}

/**
 * ParseModeChange
 *
 * Parses a mode change and updates the internal structures.
 *
 * @param Source the source of the mode change
 * @param Modes the modified modes
 * @param pargc the number of parameters
 * @param pargv the parameters
 */
void CChannel::ParseModeChange(const char *Source, const char *Modes, int pargc, const char **pargv) {
	bool Flip = true;
	int p = 0;

	/* free any cached chanmodes */
	if (m_TempModes != NULL) {
		ufree(m_TempModes);
		m_TempModes = NULL;
	}

	const CVector<CModule *> *Modules = g_Bouncer->GetModules();

	for (unsigned int i = 0; i < strlen(Modes); i++) {
		char Current = Modes[i];

		if (Current == '+') {
			Flip = true;
			continue;
		} else if (Current == '-') {
			Flip = false;
			continue;
		} else if (GetOwner()->IsNickMode(Current)) {
			if (p >= pargc) {
				return; // should not happen
			}

			CNick *NickObj = m_Nicks.Get(pargv[p]);

			if (NickObj != NULL) {
				if (Flip) {
					NickObj->AddPrefix(GetOwner()->PrefixForChanMode(Current));
				} else {
					NickObj->RemovePrefix(GetOwner()->PrefixForChanMode(Current));
				}
			}

			for (unsigned int i = 0; i < Modules->GetLength(); i++) {
				(*Modules)[i]->SingleModeChange(GetOwner(), m_Name, Source, Flip, Current, pargv[p]);
			}

			if (Flip && Current == 'o' && strcasecmp(pargv[p], GetOwner()->GetCurrentNick()) == 0) {
				// invalidate channel modes so we can get channel-modes which require +o (e.g. +k)
				SetModesValid(false);

				// update modes immediatelly if we don't have a client
				if (GetUser()->GetClientConnectionMultiplexer() == NULL) {
					GetOwner()->WriteLine("MODE %s", m_Name);
				}
			}

			p++;

			continue;
		}

		chanmode_t *Slot = FindSlot(Current);

		int ModeType = GetOwner()->RequiresParameter(Current);

		if (Current == 'b' && m_Banlist != NULL && p < pargc) {
			if (Flip) {
				if (IsError(m_Banlist->SetBan(pargv[p], Source, g_CurrentTime))) {
					m_HasBans = false;
				}
			} else {
				m_Banlist->UnsetBan(pargv[p]);
			}
		}

		if (Current == 'k' && Flip && p < pargc && strcmp(pargv[p], "*") != 0) {
			GetUser()->GetKeyring()->SetKey(m_Name, pargv[p]);
		}

		for (unsigned int i = 0; i < Modules->GetLength(); i++) {
			const char *arg = NULL;

			if (((Flip && ModeType != 0) || (!Flip && ModeType != 0 && ModeType != 1)) && p < pargc) {
				arg = pargv[p];
			}

			(*Modules)[i]->SingleModeChange(GetOwner(), m_Name, Source, Flip, Current, arg);
		}

		if (Flip) {
			if (Slot != NULL) {
				ufree(Slot->Parameter);
			} else {
				Slot = m_Modes.GetNew();
			}

			if (Slot == NULL) {
				if (ModeType) {
					p++;
				}

				continue;
			}

			Slot->Mode = Current;

			if (ModeType != 0 && p < pargc) {
				Slot->Parameter = ustrdup(pargv[p++]);
			} else {
				Slot->Parameter = NULL;
			}
		} else {
			if (Slot != NULL) {
				Slot->Mode = '\0';
				ufree(Slot->Parameter);

				Slot->Parameter = NULL;
			}

			if (ModeType != 0 && ModeType != 1) {
				p++;
			}
		}
	}
}

/**
 * FindSlot
 *
 * Returns the slot for a channelmode.
 *
 * @param Mode the mode
 */
chanmode_t *CChannel::FindSlot(char Mode) {
	for (unsigned int i = 0; i < m_Modes.GetLength(); i++) {
		if (m_Modes[i].Mode == Mode) {
			return &m_Modes[i];
		}
	}

	return NULL;
}

/**
 * GetCreationTime
 *
 * Returns a timestamp which describes when the channel was created.
 */
time_t CChannel::GetCreationTime(void) const {
	return m_Creation;
}

/**
 * SetCreationTime
 *
 * Sets the timestamp when this channel was created.
 */
void CChannel::SetCreationTime(time_t Time) {
	m_Creation = Time;

	if (GetBox() != NULL) {
		safe_put_integer(GetBox(), "CreationTimestamp", Time);
	}
}

/**
 * GetTopic
 *
 * Returns the channel's topic.
 */
const char *CChannel::GetTopic(void) const {
	return m_Topic;
}

/**
 * SetTopic
 *
 * Sets the channel's topic (just internally).
 *
 * @param Topic the channel's topic
 */
void CChannel::SetTopic(const char *Topic) {
	char *NewTopic;

	NewTopic = ustrdup(Topic);

	CHECK_ALLOC_RESULT(NewTopic, strdup) {
		return;
	} CHECK_ALLOC_RESULT_END;

	ufree(m_Topic);
	m_Topic = NewTopic;
	m_HasTopic = 1;

	if (GetBox() != NULL) {
		safe_put_string(GetBox(), "Topic", Topic);
		safe_put_integer(GetBox(), "HasTopic", 1);
	}
}

/**
 * GetTopicNick
 *
 * Returns the nick of the user who set the topic.
 */
const char *CChannel::GetTopicNick(void) const {
	return m_TopicNick;
}

/**
 * SetTopicNick
 *
 * Sets the nick of the user who set the topic.
 *
 * @param Nick the nick of the user
 */
void CChannel::SetTopicNick(const char *Nick) {
	char *NewTopicNick;

	NewTopicNick = ustrdup(Nick);

	CHECK_ALLOC_RESULT(NewTopicNick, strdup) {
		return;
	} CHECK_ALLOC_RESULT_END;

	ufree(m_TopicNick);
	m_TopicNick = NewTopicNick;
	m_HasTopic = 1;

	if (GetBox() != NULL) {
		safe_put_string(GetBox(), "TopicNick", Nick);
		safe_put_integer(GetBox(), "HasTopic", 1);
	}
}

/**
 * GetTopicStamp
 *
 * Returns the timestamp which describes when the topic was set.
 */
time_t CChannel::GetTopicStamp(void) const {
	return m_TopicStamp;
}

/**
 * SetTopicStamp
 *
 * Sets the timestamp which describes when the topic was set.
 *
 * @param Timestamp the timestamp
 */
void CChannel::SetTopicStamp(time_t Timestamp) {
	m_TopicStamp = Timestamp;
	m_HasTopic = 1;

	if (GetBox() != NULL) {
		safe_put_integer(GetBox(), "TopicTimestamp", Timestamp);
		safe_put_integer(GetBox(), "HasTopic", 1);
	}
}

/**
 * HasTopic
 *
 * Checks whether the bouncer knows the channel's topic.
 */
int CChannel::HasTopic(void) const {
	return m_HasTopic;
}

/**
 * SetNoTopic
 *
 * Specifies that no topic is set.
 */
void CChannel::SetNoTopic(void) {
	m_HasTopic = -1;

	if (GetBox() != NULL) {
		safe_put_integer(GetBox(), "HasTopic", -1);
	}
}

/**
 * AddUser
 *
 * Creates a new user for this channel.
 *
 * @param Nick the nick of the user
 * @param ModeChars the mode chars for the user
 */
void CChannel::AddUser(const char *Nick, const char *ModeChars) {
	CNick *NickObj;

	if (GetUser()->GetLeanMode() > 1) {
		return;
	}

	m_Nicks.Remove(Nick);

	NickObj = unew CNick(Nick, this);

	CHECK_ALLOC_RESULT(NickObj, CZone::Allocate) {
		m_HasNames = false;

		return;
	} CHECK_ALLOC_RESULT_END;

	NickObj->SetPrefixes(ModeChars);

	m_Nicks.Add(Nick, NickObj);
}

/**
 * RemoveUser
 *
 * Removes a user from a channel.
 *
 * @param Nick the nick of the user
 */
void CChannel::RemoveUser(const char *Nick) {
	m_Nicks.Remove(Nick);
	
	if (GetBox() != NULL) {
		safe_box_t NicksBox = safe_get_box(GetBox(), "Nicks");

		if (NicksBox != NULL) {
			safe_remove(NicksBox, Nick);
		}
	}
}

/**
 * RenameUser
 *
 * Renames a user for the channel.
 *
 * @param Nick the old nick of the user
 * @param NewNick the new nick of the user
 */
void CChannel::RenameUser(const char *Nick, const char *NewNick) {
	CNick *NickObj;

	NickObj = m_Nicks.Get(Nick);

	if (NickObj == NULL) {
		return;
	}

	m_Nicks.Remove(Nick, true);

	if (GetBox() != NULL) {
		safe_box_t NicksBox = safe_get_box(GetBox(), "Nicks");

		if (NicksBox != NULL) {
			safe_rename(NicksBox, Nick, NewNick);
		}
	}

	NickObj->SetNick(NewNick);
	m_Nicks.Add(NewNick, NickObj);
}

/**
 * HasNames
 *
 * Check whether the bouncer knows the names for the channel.
 */
bool CChannel::HasNames(void) const {
	if (GetUser()->GetLeanMode() > 1) {
		return false;
	} else {
		return m_HasNames;
	}
}

/**
 * SetHasNames
 *
 * Specifies that the names are known for the channel.
 */
void CChannel::SetHasNames(void) {
	m_HasNames = true;
}

/**
 * GetNames
 *
 * Returns a hashtable containing the nicks for the channel.
 */
const CHashtable<CNick *, false, 64> *CChannel::GetNames(void) const {
	return &m_Nicks;
}

/**
 * ClearModes
 *
 * Clears all modes for the channel.
 */
void CChannel::ClearModes(void) {
	for (unsigned int i = 0; i < m_Modes.GetLength(); i++) {
		ufree(m_Modes[i].Parameter);
	}

	m_Modes.Clear();
}

/**
 * AreModesValid
 *
 * Checks whether the modes are valid for the channel.
 */
bool CChannel::AreModesValid(void) const {
	return m_ModesValid;
}

/**
 * SetModesValid
 *
 * Sets whether the modes are valid.
 *
 * @param Valid a boolean flag describing whether the modes are valid
 */
void CChannel::SetModesValid(bool Valid) {
	m_ModesValid = Valid;
}

/**
 * GetBanlist
 *
 * Returns a list of bans for the channel.
 */
CBanlist *CChannel::GetBanlist(void) {
	return m_Banlist;
}

/**
 * SetHasBans
 *
 * Sets whether the banlist is valid.
 */
void CChannel::SetHasBans(void) {
	m_HasBans = true;
}

/**
 * HasBans
 *
 * Checks whether the banlist is valid.
 */
bool CChannel::HasBans(void) const {
	return m_HasBans;
}

/**
 * SendWhoReply
 *
 * Sends a /who reply from the cache. The client connection is left in an
 * undetermined state if Simulate is false and false is returned by the function.
 *
 * @param Client the client
 * @param Simulate determines whether to simulate the operation
 */
bool CChannel::SendWhoReply(CClientConnection *Client, bool Simulate) const {
	char CopyIdent[50];
	char *Ident, *Host, *Site;
	const char *Server, *Realname, *SiteTemp;

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

	if (!HasNames()) {
		return false;
	}

	int a = 0;

	while (hash_t<CNick *> *NickHash = GetNames()->Iterate(a++)) {
		CNick *NickObj = NickHash->Value;

		if ((SiteTemp = NickObj->GetSite()) == NULL) {
			return false;
		}

		Site = const_cast<char *>(SiteTemp);
		Host = strchr(Site, '@');

		if (Host == NULL) {
			ufree(Site);

			return false;
		}

		strmcpy(CopyIdent, Site, min(Host - Site + 1, sizeof(CopyIdent)));

		Ident = CopyIdent;

		Host++;

		Server = NickObj->GetServer();
		if (Server == NULL) {
			Server = "*.unknown.org";
		}

		Realname = NickObj->GetRealname();
		if (Realname == NULL) {
			Realname = "3 Unknown Client";
		}

		if (!Simulate) {
			Client->WriteLine(":%s 352 %s %s %s %s %s %s H :%s", GetOwner()->GetServer(), GetOwner()->GetCurrentNick(),
				m_Name, Ident, Host, Server, NickObj->GetNick(), Realname);
		}
	}

	if (!Simulate) {
		Client->WriteLine(":%s 315 %s %s :End of /WHO list.", GetOwner()->GetServer(), GetOwner()->GetCurrentNick(), m_Name);
	}

	return true;
}

/**
 * GetJoinTimestamp
 *
 * Returns a timestamp which describes when the user has joined this channel.
 */
time_t CChannel::GetJoinTimestamp(void) const {
	return m_Timestamp;
}

/**
 * ChannelTSCompare
 *
 * Compares two channel objects using GetJoinTimestamp()
 *
 * @param p1 first channel
 * @param p2 second channel
 */
int ChannelTSCompare(const void *p1, const void *p2) {
	const CChannel *Channel1 = *(const CChannel **)p1, *Channel2 = *(const CChannel **)p2;

	if (Channel1->GetJoinTimestamp() > Channel2->GetJoinTimestamp()) {
		return 1;
	} else if (Channel1->GetJoinTimestamp() == Channel2->GetJoinTimestamp()) {
		return -1;
	} else {
		return 0;
	}
}

/**
 * ChannelNameCompare
 *
 * Compares two channel objects using GetName()
 *
 * @param p1 first channel
 * @param p2 second channel
 */
int ChannelNameCompare(const void *p1, const void *p2) {
	const CChannel *Channel1 = *(const CChannel **)p1, *Channel2 = *(const CChannel **)p2;

	return strcasecmp(Channel1->GetName(), Channel2->GetName());
}