channel.c 20 KB

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