channel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. *
  11. * Channel management
  12. */
  13. #define __channel_c__
  14. #include "portab.h"
  15. static char UNUSED id[] = "$Id: channel.c,v 1.50 2005/06/18 08:57:37 fw Exp $";
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include "defines.h"
  22. #include "conn-func.h"
  23. #include "client.h"
  24. #include "exp.h"
  25. #include "channel.h"
  26. #include "imp.h"
  27. #include "irc-write.h"
  28. #include "resolve.h"
  29. #include "conf.h"
  30. #include "hash.h"
  31. #include "lists.h"
  32. #include "log.h"
  33. #include "messages.h"
  34. #include "exp.h"
  35. #define REMOVE_PART 0
  36. #define REMOVE_QUIT 1
  37. #define REMOVE_KICK 2
  38. LOCAL CHANNEL *My_Channels;
  39. LOCAL CL2CHAN *My_Cl2Chan;
  40. LOCAL CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
  41. LOCAL CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
  42. LOCAL bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
  43. LOCAL CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
  44. LOCAL CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
  45. LOCAL bool Delete_Channel PARAMS(( CHANNEL *Chan ));
  46. GLOBAL void
  47. Channel_Init( void )
  48. {
  49. My_Channels = NULL;
  50. My_Cl2Chan = NULL;
  51. } /* Channel_Init */
  52. GLOBAL void
  53. Channel_InitPredefined( void )
  54. {
  55. /* Vordefinierte persistente Channels erzeugen */
  56. CHANNEL *chan;
  57. char *c;
  58. int i;
  59. for( i = 0; i < Conf_Channel_Count; i++ )
  60. {
  61. /* Ist ein Name konfiguriert? */
  62. if( ! Conf_Channel[i].name[0] ) continue;
  63. /* Gueltiger Channel-Name? */
  64. if( ! Channel_IsValidName( Conf_Channel[i].name ))
  65. {
  66. Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
  67. continue;
  68. }
  69. /* Gibt es den Channel bereits? */
  70. chan = Channel_Search( Conf_Channel[i].name );
  71. if( chan )
  72. {
  73. Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
  74. continue;
  75. }
  76. /* Channel anlegen */
  77. chan = Channel_Create( Conf_Channel[i].name );
  78. if( chan )
  79. {
  80. Channel_ModeAdd( chan, 'P' );
  81. Channel_SetTopic( chan, Conf_Channel[i].topic );
  82. c = Conf_Channel[i].modes;
  83. while( *c ) Channel_ModeAdd( chan, *c++ );
  84. Log( LOG_INFO, "Created pre-defined channel \"%s\".", Conf_Channel[i].name );
  85. }
  86. else Log( LOG_ERR, "Can't create pre-defined channel \"%s\"!", Conf_Channel[i].name );
  87. }
  88. } /* Channel_InitPredefined */
  89. GLOBAL void
  90. Channel_Exit( void )
  91. {
  92. CHANNEL *c, *c_next;
  93. CL2CHAN *cl2chan, *cl2chan_next;
  94. /* Channel-Strukturen freigeben */
  95. c = My_Channels;
  96. while( c )
  97. {
  98. c_next = c->next;
  99. free( c );
  100. c = c_next;
  101. }
  102. /* Channel-Zuordnungstabelle freigeben */
  103. cl2chan = My_Cl2Chan;
  104. while( c )
  105. {
  106. cl2chan_next = cl2chan->next;
  107. free( cl2chan );
  108. cl2chan = cl2chan_next;
  109. }
  110. } /* Channel_Exit */
  111. GLOBAL bool
  112. Channel_Join( CLIENT *Client, char *Name )
  113. {
  114. CHANNEL *chan;
  115. assert( Client != NULL );
  116. assert( Name != NULL );
  117. /* Valider Channel-Name? */
  118. if( ! Channel_IsValidName( Name ))
  119. {
  120. IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
  121. return false;
  122. }
  123. /* Channel suchen */
  124. chan = Channel_Search( Name );
  125. if( chan )
  126. {
  127. /* Ist der Client bereits Mitglied? */
  128. if( Get_Cl2Chan( chan, Client )) return false;
  129. }
  130. else
  131. {
  132. /* Gibt es noch nicht? Dann neu anlegen: */
  133. chan = Channel_Create( Name );
  134. if( ! chan ) return false;
  135. }
  136. /* User dem Channel hinzufuegen */
  137. if( ! Add_Client( chan, Client )) return false;
  138. else return true;
  139. } /* Channel_Join */
  140. GLOBAL bool
  141. Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
  142. {
  143. CHANNEL *chan;
  144. assert( Client != NULL );
  145. assert( Name != NULL );
  146. assert( Reason != NULL );
  147. /* Channel suchen */
  148. chan = Channel_Search( Name );
  149. if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
  150. {
  151. IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
  152. return false;
  153. }
  154. /* User aus Channel entfernen */
  155. if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
  156. else return true;
  157. } /* Channel_Part */
  158. GLOBAL void
  159. Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
  160. {
  161. CHANNEL *chan;
  162. assert( Client != NULL );
  163. assert( Origin != NULL );
  164. assert( Name != NULL );
  165. assert( Reason != NULL );
  166. /* Channel suchen */
  167. chan = Channel_Search( Name );
  168. if( ! chan )
  169. {
  170. IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
  171. return;
  172. }
  173. /* Ist der User Mitglied in dem Channel? */
  174. if( ! Channel_IsMemberOf( chan, Origin ))
  175. {
  176. IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
  177. return;
  178. }
  179. /* Ist der User Channel-Operator? */
  180. if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
  181. {
  182. IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
  183. return;
  184. }
  185. /* Ist der Ziel-User Mitglied im Channel? */
  186. if( ! Channel_IsMemberOf( chan, Client ))
  187. {
  188. IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
  189. return;
  190. }
  191. Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
  192. } /* Channel_Kick */
  193. GLOBAL void
  194. Channel_Quit( CLIENT *Client, char *Reason )
  195. {
  196. CHANNEL *c, *next_c;
  197. assert( Client != NULL );
  198. assert( Reason != NULL );
  199. IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
  200. c = My_Channels;
  201. while( c )
  202. {
  203. next_c = c->next;
  204. Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
  205. c = next_c;
  206. }
  207. } /* Channel_Quit */
  208. GLOBAL long
  209. Channel_Count( void )
  210. {
  211. CHANNEL *c;
  212. long count = 0;
  213. c = My_Channels;
  214. while( c )
  215. {
  216. count++;
  217. c = c->next;
  218. }
  219. return count;
  220. } /* Channel_Count */
  221. GLOBAL long
  222. Channel_MemberCount( CHANNEL *Chan )
  223. {
  224. CL2CHAN *cl2chan;
  225. long count = 0;
  226. assert( Chan != NULL );
  227. cl2chan = My_Cl2Chan;
  228. while( cl2chan )
  229. {
  230. if( cl2chan->channel == Chan ) count++;
  231. cl2chan = cl2chan->next;
  232. }
  233. return count;
  234. } /* Channel_MemberCount */
  235. GLOBAL int
  236. Channel_CountForUser( CLIENT *Client )
  237. {
  238. /* Count number of channels a user is member of. */
  239. CL2CHAN *cl2chan;
  240. int count = 0;
  241. assert( Client != NULL );
  242. cl2chan = My_Cl2Chan;
  243. while( cl2chan )
  244. {
  245. if( cl2chan->client == Client ) count++;
  246. cl2chan = cl2chan->next;
  247. }
  248. return count;
  249. } /* Channel_CountForUser */
  250. GLOBAL int
  251. Channel_PCount( void )
  252. {
  253. /* Count the number of persistent (mode 'P') channels */
  254. CHANNEL *chan;
  255. int count = 0;
  256. chan = My_Channels;
  257. while( chan )
  258. {
  259. if( strchr( chan->modes, 'P' )) count++;
  260. chan = chan->next;
  261. }
  262. return count;
  263. } /* Channel_PCount */
  264. GLOBAL char *
  265. Channel_Name( CHANNEL *Chan )
  266. {
  267. assert( Chan != NULL );
  268. return Chan->name;
  269. } /* Channel_Name */
  270. GLOBAL char *
  271. Channel_Modes( CHANNEL *Chan )
  272. {
  273. assert( Chan != NULL );
  274. return Chan->modes;
  275. } /* Channel_Modes */
  276. GLOBAL char *
  277. Channel_Key( CHANNEL *Chan )
  278. {
  279. assert( Chan != NULL );
  280. return Chan->key;
  281. } /* Channel_Key */
  282. GLOBAL long
  283. Channel_MaxUsers( CHANNEL *Chan )
  284. {
  285. assert( Chan != NULL );
  286. return Chan->maxusers;
  287. } /* Channel_MaxUsers */
  288. GLOBAL CHANNEL *
  289. Channel_First( void )
  290. {
  291. return My_Channels;
  292. } /* Channel_First */
  293. GLOBAL CHANNEL *
  294. Channel_Next( CHANNEL *Chan )
  295. {
  296. assert( Chan != NULL );
  297. return Chan->next;
  298. } /* Channel_Next */
  299. GLOBAL CHANNEL *
  300. Channel_Search( char *Name )
  301. {
  302. /* Channel-Struktur suchen */
  303. CHANNEL *c;
  304. UINT32 search_hash;
  305. assert( Name != NULL );
  306. search_hash = Hash( Name );
  307. c = My_Channels;
  308. while( c )
  309. {
  310. if( search_hash == c->hash )
  311. {
  312. /* lt. Hash-Wert: Treffer! */
  313. if( strcasecmp( Name, c->name ) == 0 ) return c;
  314. }
  315. c = c->next;
  316. }
  317. return NULL;
  318. } /* Channel_Search */
  319. GLOBAL CL2CHAN *
  320. Channel_FirstMember( CHANNEL *Chan )
  321. {
  322. assert( Chan != NULL );
  323. return Get_First_Cl2Chan( NULL, Chan );
  324. } /* Channel_FirstMember */
  325. GLOBAL CL2CHAN *
  326. Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
  327. {
  328. assert( Chan != NULL );
  329. assert( Cl2Chan != NULL );
  330. return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
  331. } /* Channel_NextMember */
  332. GLOBAL CL2CHAN *
  333. Channel_FirstChannelOf( CLIENT *Client )
  334. {
  335. assert( Client != NULL );
  336. return Get_First_Cl2Chan( Client, NULL );
  337. } /* Channel_FirstChannelOf */
  338. GLOBAL CL2CHAN *
  339. Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
  340. {
  341. assert( Client != NULL );
  342. assert( Cl2Chan != NULL );
  343. return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
  344. } /* Channel_NextChannelOf */
  345. GLOBAL CLIENT *
  346. Channel_GetClient( CL2CHAN *Cl2Chan )
  347. {
  348. assert( Cl2Chan != NULL );
  349. return Cl2Chan->client;
  350. } /* Channel_GetClient */
  351. GLOBAL CHANNEL *
  352. Channel_GetChannel( CL2CHAN *Cl2Chan )
  353. {
  354. assert( Cl2Chan != NULL );
  355. return Cl2Chan->channel;
  356. } /* Channel_GetChannel */
  357. GLOBAL bool
  358. Channel_IsValidName( char *Name )
  359. {
  360. /* Pruefen, ob Name als Channelname gueltig */
  361. char *ptr, badchars[10];
  362. assert( Name != NULL );
  363. if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
  364. ptr = Name;
  365. strcpy( badchars, " ,:\007" );
  366. while( *ptr )
  367. {
  368. if( strchr( badchars, *ptr )) return false;
  369. ptr++;
  370. }
  371. return true;
  372. } /* Channel_IsValidName */
  373. GLOBAL bool
  374. Channel_ModeAdd( CHANNEL *Chan, char Mode )
  375. {
  376. /* set Mode.
  377. * If the channel already had this mode, return false.
  378. * If the channel mode was newly set return true.
  379. */
  380. char x[2];
  381. assert( Chan != NULL );
  382. x[0] = Mode; x[1] = '\0';
  383. if( ! strchr( Chan->modes, x[0] ))
  384. {
  385. /* Channel does not have this mode yet, set it */
  386. strlcat( Chan->modes, x, sizeof( Chan->modes ));
  387. return true;
  388. }
  389. else return false;
  390. } /* Channel_ModeAdd */
  391. GLOBAL bool
  392. Channel_ModeDel( CHANNEL *Chan, char Mode )
  393. {
  394. /* Delete mode.
  395. * if the mode was removed return true.
  396. * if the channel did not have the mode, return false.
  397. */
  398. char x[2], *p;
  399. assert( Chan != NULL );
  400. x[0] = Mode; x[1] = '\0';
  401. p = strchr( Chan->modes, x[0] );
  402. if( ! p ) return false;
  403. /* Channel has mode -> delete */
  404. while( *p )
  405. {
  406. *p = *(p + 1);
  407. p++;
  408. }
  409. return true;
  410. } /* Channel_ModeDel */
  411. GLOBAL bool
  412. Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
  413. {
  414. /* Set Channel-User-Mode.
  415. * if mode was newly set, return true.
  416. * if the User already had this channel-mode, return false.
  417. */
  418. CL2CHAN *cl2chan;
  419. char x[2];
  420. assert( Chan != NULL );
  421. assert( Client != NULL );
  422. cl2chan = Get_Cl2Chan( Chan, Client );
  423. assert( cl2chan != NULL );
  424. x[0] = Mode; x[1] = '\0';
  425. if( ! strchr( cl2chan->modes, x[0] ))
  426. {
  427. /* mode not set, -> set it */
  428. strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
  429. return true;
  430. }
  431. else return false;
  432. } /* Channel_UserModeAdd */
  433. GLOBAL bool
  434. Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
  435. {
  436. /* Delete Channel-User-Mode.
  437. * If Mode was removed, return true.
  438. * If User did not have the Channel-Mode, return false.
  439. */
  440. CL2CHAN *cl2chan;
  441. char x[2], *p;
  442. assert( Chan != NULL );
  443. assert( Client != NULL );
  444. cl2chan = Get_Cl2Chan( Chan, Client );
  445. assert( cl2chan != NULL );
  446. x[0] = Mode; x[1] = '\0';
  447. p = strchr( cl2chan->modes, x[0] );
  448. if( ! p ) return false;
  449. /* Client has Mode -> delete */
  450. while( *p )
  451. {
  452. *p = *(p + 1);
  453. p++;
  454. }
  455. return true;
  456. } /* Channel_UserModeDel */
  457. GLOBAL char *
  458. Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
  459. {
  460. /* return Users' Channel-Modes */
  461. CL2CHAN *cl2chan;
  462. assert( Chan != NULL );
  463. assert( Client != NULL );
  464. cl2chan = Get_Cl2Chan( Chan, Client );
  465. assert( cl2chan != NULL );
  466. return cl2chan->modes;
  467. } /* Channel_UserModes */
  468. GLOBAL bool
  469. Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
  470. {
  471. /* Test if Client is on Channel Chan */
  472. assert( Chan != NULL );
  473. assert( Client != NULL );
  474. if( Get_Cl2Chan( Chan, Client )) return true;
  475. else return false;
  476. } /* Channel_IsMemberOf */
  477. GLOBAL char *
  478. Channel_Topic( CHANNEL *Chan )
  479. {
  480. assert( Chan != NULL );
  481. return Chan->topic;
  482. } /* Channel_Topic */
  483. GLOBAL void
  484. Channel_SetTopic( CHANNEL *Chan, char *Topic )
  485. {
  486. assert( Chan != NULL );
  487. assert( Topic != NULL );
  488. strlcpy( Chan->topic, Topic, sizeof( Chan->topic ));
  489. } /* Channel_SetTopic */
  490. GLOBAL void
  491. Channel_SetModes( CHANNEL *Chan, char *Modes )
  492. {
  493. assert( Chan != NULL );
  494. assert( Modes != NULL );
  495. strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
  496. } /* Channel_SetModes */
  497. GLOBAL void
  498. Channel_SetKey( CHANNEL *Chan, char *Key )
  499. {
  500. assert( Chan != NULL );
  501. assert( Key != NULL );
  502. strlcpy( Chan->key, Key, sizeof( Chan->key ));
  503. Log( LOG_DEBUG, "Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
  504. } /* Channel_SetKey */
  505. GLOBAL void
  506. Channel_SetMaxUsers( CHANNEL *Chan, long Count )
  507. {
  508. assert( Chan != NULL );
  509. Chan->maxusers = Count;
  510. Log( LOG_DEBUG, "Channel %s: Member limit is now %ld.", Chan->name, Chan->maxusers );
  511. } /* Channel_SetMaxUsers */
  512. GLOBAL bool
  513. Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
  514. {
  515. bool is_member, has_voice, is_op, ok;
  516. /* Okay, target is a channel */
  517. is_member = has_voice = is_op = false;
  518. if( Channel_IsMemberOf( Chan, From ))
  519. {
  520. is_member = true;
  521. if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
  522. if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
  523. }
  524. /* Is the client allowed to write to channel? */
  525. ok = true;
  526. if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
  527. if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
  528. /* Is the client banned? */
  529. if( Lists_CheckBanned( From, Chan ))
  530. {
  531. /* Client is banned, bus is he channel operator or has voice? */
  532. if(( ! has_voice ) && ( ! is_op )) ok = false;
  533. }
  534. if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
  535. /* Send text */
  536. if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
  537. return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
  538. } /* Channel_Write */
  539. GLOBAL CHANNEL *
  540. Channel_Create( char *Name )
  541. {
  542. /* Create new CHANNEL structure and add it to linked list */
  543. CHANNEL *c;
  544. assert( Name != NULL );
  545. c = (CHANNEL *)malloc( sizeof( CHANNEL ));
  546. if( ! c )
  547. {
  548. Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
  549. return NULL;
  550. }
  551. memset( c, 0, sizeof( CHANNEL ));
  552. strlcpy( c->name, Name, sizeof( c->name ));
  553. c->hash = Hash( c->name );
  554. c->next = My_Channels;
  555. My_Channels = c;
  556. Log( LOG_DEBUG, "Created new channel structure for \"%s\".", Name );
  557. return c;
  558. } /* Channel_Create */
  559. LOCAL CL2CHAN *
  560. Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
  561. {
  562. CL2CHAN *cl2chan;
  563. assert( Chan != NULL );
  564. assert( Client != NULL );
  565. cl2chan = My_Cl2Chan;
  566. while( cl2chan )
  567. {
  568. if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
  569. cl2chan = cl2chan->next;
  570. }
  571. return NULL;
  572. } /* Get_Cl2Chan */
  573. LOCAL CL2CHAN *
  574. Add_Client( CHANNEL *Chan, CLIENT *Client )
  575. {
  576. CL2CHAN *cl2chan;
  577. assert( Chan != NULL );
  578. assert( Client != NULL );
  579. /* neue CL2CHAN-Struktur anlegen */
  580. cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
  581. if( ! cl2chan )
  582. {
  583. Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
  584. return NULL;
  585. }
  586. cl2chan->channel = Chan;
  587. cl2chan->client = Client;
  588. strcpy( cl2chan->modes, "" );
  589. /* Verketten */
  590. cl2chan->next = My_Cl2Chan;
  591. My_Cl2Chan = cl2chan;
  592. Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
  593. return cl2chan;
  594. } /* Add_Client */
  595. LOCAL bool
  596. Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
  597. {
  598. CL2CHAN *cl2chan, *last_cl2chan;
  599. CHANNEL *c;
  600. assert( Chan != NULL );
  601. assert( Client != NULL );
  602. assert( Origin != NULL );
  603. assert( Reason != NULL );
  604. last_cl2chan = NULL;
  605. cl2chan = My_Cl2Chan;
  606. while( cl2chan )
  607. {
  608. if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
  609. last_cl2chan = cl2chan;
  610. cl2chan = cl2chan->next;
  611. }
  612. if( ! cl2chan ) return false;
  613. c = cl2chan->channel;
  614. assert( c != NULL );
  615. /* Aus Verkettung loesen und freigeben */
  616. if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
  617. else My_Cl2Chan = cl2chan->next;
  618. free( cl2chan );
  619. switch( Type )
  620. {
  621. case REMOVE_QUIT:
  622. /* QUIT: andere Server wurden bereits informiert, vgl. Client_Destroy();
  623. * hier also "nur" noch alle User in betroffenen Channeln infomieren */
  624. assert( InformServer == false );
  625. Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
  626. break;
  627. case REMOVE_KICK:
  628. /* User wurde geKICKed: ggf. andere Server sowie alle betroffenen User
  629. * im entsprechenden Channel informieren */
  630. if( InformServer ) IRC_WriteStrServersPrefix( Client_NextHop( Origin ), Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
  631. IRC_WriteStrChannelPrefix( Client, c, Origin, false, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
  632. if(( Client_Conn( Client ) > NONE ) && ( Client_Type( Client ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Client, Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
  633. Log( LOG_DEBUG, "User \"%s\" has been kicked of \"%s\" by \"%s\": %s.", Client_Mask( Client ), c->name, Client_ID( Origin ), Reason );
  634. break;
  635. default:
  636. /* PART */
  637. if( InformServer ) IRC_WriteStrServersPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
  638. IRC_WriteStrChannelPrefix( Origin, c, Client, false, "PART %s :%s", c->name, Reason );
  639. if(( Client_Conn( Origin ) > NONE ) && ( Client_Type( Origin ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
  640. Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
  641. }
  642. /* Wenn Channel nun leer und nicht pre-defined: loeschen */
  643. if( ! strchr( Channel_Modes( Chan ), 'P' ))
  644. {
  645. if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
  646. }
  647. return true;
  648. } /* Remove_Client */
  649. LOCAL CL2CHAN *
  650. Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
  651. {
  652. return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
  653. } /* Get_First_Cl2Chan */
  654. LOCAL CL2CHAN *
  655. Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
  656. {
  657. CL2CHAN *cl2chan;
  658. assert( Client != NULL || Channel != NULL );
  659. cl2chan = Start;
  660. while( cl2chan )
  661. {
  662. if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
  663. if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
  664. cl2chan = cl2chan->next;
  665. }
  666. return NULL;
  667. } /* Get_Next_Cl2Chan */
  668. LOCAL bool
  669. Delete_Channel( CHANNEL *Chan )
  670. {
  671. /* Channel-Struktur loeschen */
  672. CHANNEL *chan, *last_chan;
  673. last_chan = NULL;
  674. chan = My_Channels;
  675. while( chan )
  676. {
  677. if( chan == Chan ) break;
  678. last_chan = chan;
  679. chan = chan->next;
  680. }
  681. if( ! chan ) return false;
  682. Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
  683. /* Invite- und Ban-Lists aufraeumen */
  684. Lists_DeleteChannel( chan );
  685. /* Neu verketten und freigeben */
  686. if( last_chan ) last_chan->next = chan->next;
  687. else My_Channels = chan->next;
  688. free( chan );
  689. return true;
  690. } /* Delete_Channel */
  691. /* -eof- */