channel.c 21 KB

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