[go: up one dir, main page]

Menu

[ab7a0e]: / src / Channel.cc  Maximize  Restore  History

Download this file

594 lines (519 with data), 12.8 kB

  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
/*
* Channel.cc
* Author: Daniel Karrels (dan@karrels.com)
* Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
*
* 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.
*
* $Id: Channel.cc,v 1.55 2008/04/16 20:29:37 danielaustin Exp $
*/
#include <new>
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include "Channel.h"
#include "iClient.h"
#include "ChannelUser.h"
#include "Network.h"
#include "xparameters.h"
#include "StringTokenizer.h"
#include "ELog.h"
#include "match.h"
#include "server.h"
#include "ConnectionManager.h"
RCSTAG("$Id: Channel.cc,v 1.55 2008/04/16 20:29:37 danielaustin Exp $") ;
namespace gnuworld
{
using std::vector ;
using std::string ;
using std::endl ;
using std::stringstream ;
const Channel::modeType Channel::MODE_T = 0x01 ;
const Channel::modeType Channel::MODE_N = 0x02 ;
const Channel::modeType Channel::MODE_S = 0x04 ;
const Channel::modeType Channel::MODE_P = 0x08 ;
const Channel::modeType Channel::MODE_K = 0x10 ;
const Channel::modeType Channel::MODE_L = 0x20 ;
const Channel::modeType Channel::MODE_M = 0x40 ;
const Channel::modeType Channel::MODE_I = 0x80 ;
const Channel::modeType Channel::MODE_R = 0x100 ;
const Channel::modeType Channel::MODE_D = 0x200 ;
const Channel::modeType Channel::MODE_A = 0x400 ;
const Channel::modeType Channel::MODE_U = 0x800 ;
const Channel::modeType Channel::MODE_REG = 0x1000 ;
Channel::Channel( const string& _name,
const time_t& _creationTime )
: name( _name ),
creationTime( _creationTime ),
modes( 0 ),
limit( 0 )
{}
Channel::~Channel()
{
// Deallocate all ChannelUser's that are left
userIterator currentPtr = userList_begin() ;
userIterator endPtr = userList_end() ;
for( ; currentPtr != endPtr ; ++currentPtr )
{
delete currentPtr->second ;
}
userList.clear() ;
}
bool Channel::addUser( ChannelUser* newUser )
{
assert( newUser != 0 ) ;
//elog << "Channel::addUser> ("
// << getName()
// << ") Number of users: "
// << userList.size()
// << endl ;
if( !userList.insert(
userListType::value_type( newUser->getIntYYXXX(), newUser ) ).second )
{
elog << "Channel::addUser> ("
<< getName()
<< "): "
<< "Unable to add user: "
<< *newUser
<< endl ;
return false ;
}
//elog << "Channel::addUser> " << name << " added user: "
// << *newUser << endl ;
return true ;
}
bool Channel::addUser( iClient* theClient )
{
ChannelUser* addMe = new (std::nothrow) ChannelUser( theClient ) ;
// The signature of addUser() here will verify the pointer
return addUser( addMe ) ;
}
ChannelUser* Channel::removeUser( ChannelUser* theUser )
{
assert( theUser != 0 ) ;
return removeUser( theUser->getClient()->getIntYYXXX() ) ;
}
ChannelUser* Channel::removeUser( iClient* theClient )
{
// This method is public, so the pointer must be validated
assert( theClient != 0 ) ;
return removeUser( theClient->getIntYYXXX() ) ;
}
ChannelUser* Channel::removeUser( const unsigned int& intYYXXX )
{
ChannelUser* tmpUser;
// Attempt to find the user in question
userIterator ptr = userList.find( intYYXXX ) ;
// Was the user found?
if( ptr != userList.end() )
{
// Yup, go ahead and remove the user from the userList
tmpUser = ptr->second;
userList.erase( ptr ) ;
// Return a pointer to the ChannelUser
return tmpUser;
}
// Otherwise, the user was NOT found
// Log the error
// Happens when not handling zombies
//elog << "Channel::removeUser> ("
// << getName() << ") "
// << "Unable to find user: "
// << intYYXXX
// << std::endl ;
// Return error state
return 0 ;
}
ChannelUser* Channel::findUser( const iClient* theClient ) const
{
assert( theClient != 0 ) ;
const_userIterator ptr = userList.find( theClient->getIntYYXXX() ) ;
if( ptr == userList_end() )
{
// User not found
return 0 ;
}
return ptr->second ;
}
bool Channel::removeUserMode( const ChannelUser::modeType& whichMode,
iClient* theClient )
{
ChannelUser* theChanUser = findUser( theClient ) ;
if( NULL == theChanUser )
{
elog << "Channel::removeUserMode> ("
<< getName()
<< ") "
<< "Unable to find user: "
<< theClient->getCharYYXXX()
<< endl ;
return false ;
}
theChanUser->removeMode( whichMode ) ;
return true ;
}
bool Channel::setUserMode( const ChannelUser::modeType& whichMode,
iClient* theClient )
{
// findUser() is also public, and so will verify theClient's pointer
ChannelUser* theChanUser = findUser( theClient ) ;
if( NULL == theChanUser )
{
elog << "Channel::setUserMode> ("
<< getName()
<< ") "
<< "Unable to find user: "
<< theClient->getCharYYXXX()
<< endl ;
return false ;
}
theChanUser->setMode( whichMode ) ;
return true ;
}
bool Channel::getUserMode( const ChannelUser::modeType& whichMode,
iClient* theClient ) const
{
// findUser() is also public, and so will verify theClient's pointer
ChannelUser* theChanUser = findUser( theClient ) ;
if( NULL == theChanUser )
{
elog << "Channel::getUserMode> ("
<< getName()
<< ") "
<< "Unable to find user: "
<< theClient->getCharYYXXX()
<< endl ;
return false ;
}
return theChanUser->getMode( whichMode ) ;
}
void Channel::setBan( const string& newBan )
{
// xServer will worry about removing conflicting bans
banList.push_front( newBan ) ;
}
bool Channel::removeBan( const string& banMask )
{
for( banIterator ptr = banList_begin(), end = banList_end() ;
ptr != end ; ++ptr )
{
if( !strcasecmp( *ptr, banMask ) )
{
banList.erase( ptr ) ;
// Ban found, return true
return true ;
}
}
// Ban not found
return false ;
}
bool Channel::findBan( const string& banMask ) const
{
for( const_banIterator ptr = banList_begin(),
end = banList_end() ; ptr != end ; ++ptr )
{
if( !strcasecmp( *ptr, banMask ) )
{
return true ;
}
}
return false ;
}
bool Channel::matchBan( const string& banMask ) const
{
for( const_banIterator ptr = banList_begin(),
end = banList_end() ; ptr != end ; ++ptr )
{
if( !match( banMask, *ptr ) )
{
// Found a match
return true ;
}
}
return false ;
}
bool Channel::getMatchingBan( const string& banMask,
string& matchingBan ) const
{
for( const_banIterator ptr = banList_begin(),
end = banList_end() ; ptr != end ; ++ptr )
{
if( !match( banMask, *ptr ) )
{
matchingBan = *ptr ;
return true ;
}
}
return false ;
}
void Channel::onMode(
const vector< std::pair< bool, Channel::modeType > >& modeVector )
{
typedef vector< std::pair< bool, Channel::modeType > > modeVectorType ;
for( modeVectorType::const_iterator mItr = modeVector.begin() ;
mItr != modeVector.end() ; ++mItr )
{
bool polarity = (*mItr).first ;
if( polarity ) setMode( (*mItr).second ) ;
else removeMode( (*mItr).second ) ;
} // for()
}
void Channel::onModeL( bool polarity, const unsigned int& newLimit )
{
if( polarity )
{
setMode( MODE_L ) ;
setLimit( newLimit ) ;
}
else
{
removeMode( MODE_L ) ;
setLimit( 0 ) ;
}
}
void Channel::onModeK( bool polarity, const string& newKey )
{
if( polarity )
{
setMode( MODE_K ) ;
setKey( newKey ) ;
}
else
{
removeMode( MODE_K ) ;
setKey( string() ) ;
}
}
void Channel::onModeA( bool polarity, const string& newApass )
{
if( polarity )
{
setMode( MODE_A ) ;
setApass( newApass ) ;
}
else
{
removeMode( MODE_K ) ;
setApass( string() ) ;
}
}
void Channel::onModeU( bool polarity, const string& newUpass )
{
if( polarity )
{
setMode( MODE_U ) ;
setUpass( newUpass ) ;
}
else
{
removeMode( MODE_U ) ;
setUpass( string() ) ;
}
}
void Channel::onModeO( const vector< std::pair< bool, ChannelUser* > >&
opVector )
{
typedef vector< std::pair< bool, ChannelUser* > > opVectorType ;
for( opVectorType::const_iterator ptr = opVector.begin() ;
ptr != opVector.end() ; ++ptr )
{
if( ptr->first )
{
ptr->second->setModeO() ;
}
else
{
ptr->second->removeModeO() ;
}
}
}
void Channel::onModeV( const vector< std::pair< bool, ChannelUser* > >&
voiceVector )
{
typedef vector< std::pair< bool, ChannelUser* > > voiceVectorType ;
for( voiceVectorType::const_iterator ptr = voiceVector.begin() ;
ptr != voiceVector.end() ; ++ptr )
{
if( ptr->first )
{
ptr->second->setModeV() ;
}
else
{
ptr->second->removeModeV() ;
}
}
}
/**
* The banVector passed to this method will be updated to
* include any bans that have been removed as a result
* of overlapping bans being added.
* The order of these additions will be as expected:
* an overlapping ban will be put into the banVector,
* followed by all bans that it overrides.
*/
void Channel::onModeB( xServer::banVectorType& banVector )
{
typedef xServer::banVectorType banVectorType ;
banVectorType origBans( banVector ) ;
banVector.clear() ;
// Walk through the list of bans being removed/added
for( banVectorType::const_iterator newBanPtr = origBans.begin() ;
newBanPtr != origBans.end() ; ++newBanPtr )
{
banVector.push_back( *newBanPtr ) ;
// Is the ban being set or removed?
if( !newBanPtr->first )
{
// Removing a ban
removeBan( newBanPtr->second ) ;
continue ;
}
// Setting a ban
// Need to check the list of current bans for overlaps
// This is grossly inefficient, Im open to suggestions
// Next, search for overlaps
banIterator currentBanPtr = banList_begin() ;
for( ; currentBanPtr != banList_end() ; )
{
if( !match( newBanPtr->second, *currentBanPtr ) )
{
// Add the removed ban to the banVector
// so that the caller can notify the rest
// of the system of the removal
banVector.push_back( banVectorType::value_type(
false, *currentBanPtr ) ) ;
// elog << "Channel::onModeB> Removing "
// << "overlapping ban: "
// << *currentBanPtr
// << endl ;
// Overlap, remove the old ban
currentBanPtr = banList.erase( currentBanPtr ) ;
}
else
{
// Update the iterator
++currentBanPtr ;
}
} // inner for()
// Now set the new ban
// Setting this ban will add the ban into
// later comparisons, but not this comparison,
// which is what we want.
setBan( newBanPtr->second ) ;
} // outer for()
}
const string Channel::getModeString() const
{
string modeString( "+" ) ;
string argString ;
if( modes & MODE_T ) modeString += 't' ;
if( modes & MODE_N ) modeString += 'n' ;
if( modes & MODE_S ) modeString += 's' ;
if( modes & MODE_P ) modeString += 'p' ;
if( modes & MODE_M ) modeString += 'm' ;
if( modes & MODE_I ) modeString += 'i' ;
if( modes & MODE_R ) modeString += 'r' ;
if( modes & MODE_REG ) modeString += 'R' ;
if( modes & MODE_D ) modeString += 'D' ;
if( modes & MODE_K )
{
modeString += 'k' ;
argString += getKey() + ' ' ;
}
if( modes & MODE_L )
{
modeString += 'l' ;
// Can't put numerical variables into a string
stringstream s ;
s << getLimit() ;
argString += s.str() ;
}
if( modes & MODE_A )
{
modeString += 'A' ;
argString += getApass() + ' ' ;
}
if( modes & MODE_U )
{
modeString += 'U' ;
argString += getUpass() + ' ' ;
}
return (modeString + ' ' + argString) ;
}
string Channel::createBan( const iClient* theClient )
{
assert( theClient != 0 ) ;
string theBan = "*!*" ;
// If we're +x, don't bother with the user name either.
if(!theClient->isModeX())
{
// Don't include the '~'
if( (theClient->getUserName().size() >= 2) &&
('~' == theClient->getUserName()[ 0 ]) )
{
theBan += theClient->getUserName().c_str() + 1 ;
}
else if( !theClient->getUserName().empty() )
{
theBan += theClient->getUserName() ;
}
}
theBan += '@' ;
StringTokenizer st( theClient->getInsecureHost(), '.' ) ;
if( ConnectionManager::isIpAddress( theClient->getInsecureHost() ) )
{
theBan += st[ 0 ] + '.' ;
theBan += st[ 1 ] + '.' ;
theBan += st[ 2 ] + ".*" ;
}
else
{
if( (2 == st.size()) || theClient->isModeX() )
{
theBan += theClient->getInsecureHost() ;
}
else
{
theBan += "*." + st.assemble( 1 ) ;
}
}
return theBan ;
}
void Channel::removeAllModes()
{
for( userIterator uItr = userList_begin() ; uItr != userList_end() ;
++uItr )
{
ChannelUser* theUser = uItr->second ;
assert( theUser != 0 ) ;
theUser->removeModeO() ;
theUser->removeModeV() ;
}
modes = 0 ;
limit = 0 ;
#if __GNUC__ == 2
key = "";
Apass = "";
Upass = "";
#else
key.clear();
Apass.clear();
Upass.clear();
#endif
}
} // namespace gnuworld