channel.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
  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. #define __channel_c__
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Channel management
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <strings.h>
  24. #include "defines.h"
  25. #include "conn-func.h"
  26. #include "exp.h"
  27. #include "channel.h"
  28. #include "imp.h"
  29. #include "irc-write.h"
  30. #include "conf.h"
  31. #include "hash.h"
  32. #include "lists.h"
  33. #include "log.h"
  34. #include "messages.h"
  35. #include "match.h"
  36. #include "exp.h"
  37. #define REMOVE_PART 0
  38. #define REMOVE_QUIT 1
  39. #define REMOVE_KICK 2
  40. static CHANNEL *My_Channels;
  41. static CL2CHAN *My_Cl2Chan;
  42. static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
  43. static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
  44. static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
  45. static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
  46. static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
  47. static void Delete_Channel PARAMS(( CHANNEL *Chan ));
  48. static void Free_Channel PARAMS(( CHANNEL *Chan ));
  49. static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
  50. GLOBAL void
  51. Channel_Init( void )
  52. {
  53. CHANNEL *sc;
  54. My_Channels = NULL;
  55. My_Cl2Chan = NULL;
  56. sc = Channel_Create("&SERVER");
  57. if (sc) {
  58. Channel_SetModes(sc, "mnPt");
  59. Channel_SetTopic(sc, Client_ThisServer(), "Server Messages");
  60. }
  61. } /* Channel_Init */
  62. GLOBAL struct list_head *
  63. Channel_GetListBans(CHANNEL *c)
  64. {
  65. assert(c != NULL);
  66. return &c->list_bans;
  67. }
  68. GLOBAL struct list_head *
  69. Channel_GetListInvites(CHANNEL *c)
  70. {
  71. assert(c != NULL);
  72. return &c->list_invites;
  73. }
  74. GLOBAL void
  75. Channel_InitPredefined( void )
  76. {
  77. /* Generate predefined persistent channels */
  78. CHANNEL *new_chan;
  79. const struct Conf_Channel *conf_chan;
  80. const char *c;
  81. size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
  82. conf_chan = array_start(&Conf_Channels);
  83. assert(channel_count == 0 || conf_chan != NULL);
  84. for (i = 0; i < channel_count; i++, conf_chan++) {
  85. if (!conf_chan->name[0] || !Channel_IsValidName(conf_chan->name)) {
  86. Log(LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"",
  87. conf_chan->name);
  88. continue;
  89. }
  90. new_chan = Channel_Search(conf_chan->name);
  91. if (new_chan) {
  92. Log(LOG_INFO,
  93. "Can't create pre-defined channel \"%s\": name already in use.",
  94. conf_chan->name);
  95. Set_KeyFile(new_chan, conf_chan->keyfile);
  96. continue;
  97. }
  98. new_chan = Channel_Create(conf_chan->name);
  99. if (!new_chan) {
  100. Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
  101. conf_chan->name);
  102. continue;
  103. }
  104. Log(LOG_INFO, "Created pre-defined channel \"%s\"",
  105. conf_chan->name);
  106. Channel_ModeAdd(new_chan, 'P');
  107. if (conf_chan->topic[0])
  108. Channel_SetTopic(new_chan, NULL, conf_chan->topic);
  109. c = conf_chan->modes;
  110. while (*c)
  111. Channel_ModeAdd(new_chan, *c++);
  112. Channel_SetKey(new_chan, conf_chan->key);
  113. Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
  114. Set_KeyFile(new_chan, conf_chan->keyfile);
  115. }
  116. if (channel_count)
  117. array_free(&Conf_Channels);
  118. } /* Channel_InitPredefined */
  119. static void
  120. Free_Channel(CHANNEL *chan)
  121. {
  122. array_free(&chan->topic);
  123. array_free(&chan->keyfile);
  124. Lists_Free(&chan->list_bans);
  125. Lists_Free(&chan->list_invites);
  126. free(chan);
  127. }
  128. GLOBAL void
  129. Channel_Exit( void )
  130. {
  131. CHANNEL *c, *c_next;
  132. CL2CHAN *cl2chan, *cl2chan_next;
  133. /* free struct Channel */
  134. c = My_Channels;
  135. while (c) {
  136. c_next = c->next;
  137. Free_Channel(c);
  138. c = c_next;
  139. }
  140. /* Free Channel allocation table */
  141. cl2chan = My_Cl2Chan;
  142. while (cl2chan) {
  143. cl2chan_next = cl2chan->next;
  144. free(cl2chan);
  145. cl2chan = cl2chan_next;
  146. }
  147. } /* Channel_Exit */
  148. /**
  149. * Join Channel
  150. * This function lets a client join a channel. First, the function
  151. * checks that the specified channel name is valid and that the client
  152. * isn't already a member. If the specified channel doesn't exist,
  153. * a new channel is created. Client is added to channel by function
  154. * Add_Client().
  155. */
  156. GLOBAL bool
  157. Channel_Join( CLIENT *Client, const char *Name )
  158. {
  159. CHANNEL *chan;
  160. assert(Client != NULL);
  161. assert(Name != NULL);
  162. /* Check that the channel name is valid */
  163. if (! Channel_IsValidName(Name)) {
  164. IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
  165. Client_ID(Client), Name);
  166. return false;
  167. }
  168. chan = Channel_Search(Name);
  169. if(chan) {
  170. /* Check if the client is already in the channel */
  171. if (Get_Cl2Chan(chan, Client))
  172. return false;
  173. } else {
  174. /* If the specified channel does not exist, the channel
  175. * is now created */
  176. chan = Channel_Create(Name);
  177. if (!chan)
  178. return false;
  179. }
  180. /* Add user to Channel */
  181. if (! Add_Client(chan, Client))
  182. return false;
  183. return true;
  184. } /* Channel_Join */
  185. /**
  186. * Part client from channel.
  187. * This function lets a client part from a channel. First, the function checks
  188. * if the channel exists and the client is a member of it and sends out
  189. * appropriate error messages if not. The real work is done by the function
  190. * Remove_Client().
  191. */
  192. GLOBAL bool
  193. Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
  194. {
  195. CHANNEL *chan;
  196. assert(Client != NULL);
  197. assert(Name != NULL);
  198. assert(Reason != NULL);
  199. /* Check that specified channel exists */
  200. chan = Channel_Search(Name);
  201. if (!chan) {
  202. IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
  203. Client_ID(Client), Name);
  204. return false;
  205. }
  206. /* Check that the client is in the channel */
  207. if (!Get_Cl2Chan(chan, Client)) {
  208. IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
  209. Client_ID(Client), Name);
  210. return false;
  211. }
  212. if (Conf_MorePrivacy)
  213. Reason = "";
  214. /* Part client from channel */
  215. if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
  216. return false;
  217. else
  218. return true;
  219. } /* Channel_Part */
  220. /**
  221. * Kick user from Channel
  222. */
  223. GLOBAL void
  224. Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
  225. const char *Reason )
  226. {
  227. CHANNEL *chan;
  228. assert(Peer != NULL);
  229. assert(Target != NULL);
  230. assert(Origin != NULL);
  231. assert(Name != NULL);
  232. assert(Reason != NULL);
  233. /* Check that channel exists */
  234. chan = Channel_Search( Name );
  235. if( ! chan )
  236. {
  237. IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
  238. return;
  239. }
  240. if (Client_Type(Peer) != CLIENT_SERVER &&
  241. Client_Type(Origin) != CLIENT_SERVICE) {
  242. /* Check that user is on the specified channel */
  243. if (!Channel_IsMemberOf(chan, Origin)) {
  244. IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
  245. Client_ID(Origin), Name);
  246. return;
  247. }
  248. /* Check if user has operator status */
  249. if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
  250. IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
  251. Client_ID(Origin), Name);
  252. return;
  253. }
  254. }
  255. /* Check that the client to be kicked is on the specified channel */
  256. if (!Channel_IsMemberOf(chan, Target)) {
  257. IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
  258. Client_ID(Origin), Client_ID(Target), Name );
  259. return;
  260. }
  261. /* Kick Client from channel */
  262. Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
  263. } /* Channel_Kick */
  264. GLOBAL void
  265. Channel_Quit( CLIENT *Client, const char *Reason )
  266. {
  267. CHANNEL *c, *next_c;
  268. assert( Client != NULL );
  269. assert( Reason != NULL );
  270. if (Conf_MorePrivacy)
  271. Reason = "";
  272. IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
  273. c = My_Channels;
  274. while( c )
  275. {
  276. next_c = c->next;
  277. Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
  278. c = next_c;
  279. }
  280. } /* Channel_Quit */
  281. GLOBAL unsigned long
  282. Channel_Count( void )
  283. {
  284. CHANNEL *c;
  285. unsigned long count = 0;
  286. c = My_Channels;
  287. while( c )
  288. {
  289. count++;
  290. c = c->next;
  291. }
  292. return count;
  293. } /* Channel_Count */
  294. GLOBAL unsigned long
  295. Channel_MemberCount( CHANNEL *Chan )
  296. {
  297. CL2CHAN *cl2chan;
  298. unsigned long count = 0;
  299. assert( Chan != NULL );
  300. cl2chan = My_Cl2Chan;
  301. while( cl2chan )
  302. {
  303. if( cl2chan->channel == Chan ) count++;
  304. cl2chan = cl2chan->next;
  305. }
  306. return count;
  307. } /* Channel_MemberCount */
  308. GLOBAL int
  309. Channel_CountForUser( CLIENT *Client )
  310. {
  311. /* Count number of channels a user is member of. */
  312. CL2CHAN *cl2chan;
  313. int count = 0;
  314. assert( Client != NULL );
  315. cl2chan = My_Cl2Chan;
  316. while( cl2chan )
  317. {
  318. if( cl2chan->client == Client ) count++;
  319. cl2chan = cl2chan->next;
  320. }
  321. return count;
  322. } /* Channel_CountForUser */
  323. GLOBAL const char *
  324. Channel_Name( const CHANNEL *Chan )
  325. {
  326. assert( Chan != NULL );
  327. return Chan->name;
  328. } /* Channel_Name */
  329. GLOBAL char *
  330. Channel_Modes( CHANNEL *Chan )
  331. {
  332. assert( Chan != NULL );
  333. return Chan->modes;
  334. } /* Channel_Modes */
  335. GLOBAL char *
  336. Channel_Key( CHANNEL *Chan )
  337. {
  338. assert( Chan != NULL );
  339. return Chan->key;
  340. } /* Channel_Key */
  341. GLOBAL unsigned long
  342. Channel_MaxUsers( CHANNEL *Chan )
  343. {
  344. assert( Chan != NULL );
  345. return Chan->maxusers;
  346. } /* Channel_MaxUsers */
  347. GLOBAL CHANNEL *
  348. Channel_First( void )
  349. {
  350. return My_Channels;
  351. } /* Channel_First */
  352. GLOBAL CHANNEL *
  353. Channel_Next( CHANNEL *Chan )
  354. {
  355. assert( Chan != NULL );
  356. return Chan->next;
  357. } /* Channel_Next */
  358. GLOBAL CHANNEL *
  359. Channel_Search( const char *Name )
  360. {
  361. /* Search channel structure */
  362. CHANNEL *c;
  363. UINT32 search_hash;
  364. assert( Name != NULL );
  365. search_hash = Hash( Name );
  366. c = My_Channels;
  367. while( c )
  368. {
  369. if( search_hash == c->hash )
  370. {
  371. /* hash hit */
  372. if( strcasecmp( Name, c->name ) == 0 ) return c;
  373. }
  374. c = c->next;
  375. }
  376. return NULL;
  377. } /* Channel_Search */
  378. GLOBAL CL2CHAN *
  379. Channel_FirstMember( CHANNEL *Chan )
  380. {
  381. assert( Chan != NULL );
  382. return Get_First_Cl2Chan( NULL, Chan );
  383. } /* Channel_FirstMember */
  384. GLOBAL CL2CHAN *
  385. Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
  386. {
  387. assert( Chan != NULL );
  388. assert( Cl2Chan != NULL );
  389. return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
  390. } /* Channel_NextMember */
  391. GLOBAL CL2CHAN *
  392. Channel_FirstChannelOf( CLIENT *Client )
  393. {
  394. assert( Client != NULL );
  395. return Get_First_Cl2Chan( Client, NULL );
  396. } /* Channel_FirstChannelOf */
  397. GLOBAL CL2CHAN *
  398. Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
  399. {
  400. assert( Client != NULL );
  401. assert( Cl2Chan != NULL );
  402. return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
  403. } /* Channel_NextChannelOf */
  404. GLOBAL CLIENT *
  405. Channel_GetClient( CL2CHAN *Cl2Chan )
  406. {
  407. assert( Cl2Chan != NULL );
  408. return Cl2Chan->client;
  409. } /* Channel_GetClient */
  410. GLOBAL CHANNEL *
  411. Channel_GetChannel( CL2CHAN *Cl2Chan )
  412. {
  413. assert( Cl2Chan != NULL );
  414. return Cl2Chan->channel;
  415. } /* Channel_GetChannel */
  416. GLOBAL bool
  417. Channel_IsValidName( const char *Name )
  418. {
  419. assert( Name != NULL );
  420. #ifdef STRICT_RFC
  421. if (strlen(Name) <= 1)
  422. return false;
  423. #endif
  424. if (strchr("#&+", Name[0]) == NULL)
  425. return false;
  426. if (strlen(Name) >= CHANNEL_NAME_LEN)
  427. return false;
  428. return Name[strcspn(Name, " ,:\007")] == 0;
  429. } /* Channel_IsValidName */
  430. GLOBAL bool
  431. Channel_ModeAdd( CHANNEL *Chan, char Mode )
  432. {
  433. /* set Mode.
  434. * If the channel already had this mode, return false.
  435. * If the channel mode was newly set return true.
  436. */
  437. char x[2];
  438. assert( Chan != NULL );
  439. x[0] = Mode; x[1] = '\0';
  440. if( ! strchr( Chan->modes, x[0] ))
  441. {
  442. /* Channel does not have this mode yet, set it */
  443. strlcat( Chan->modes, x, sizeof( Chan->modes ));
  444. return true;
  445. }
  446. else return false;
  447. } /* Channel_ModeAdd */
  448. GLOBAL bool
  449. Channel_ModeDel( CHANNEL *Chan, char Mode )
  450. {
  451. /* Delete mode.
  452. * if the mode was removed return true.
  453. * if the channel did not have the mode, return false.
  454. */
  455. char *p;
  456. assert( Chan != NULL );
  457. p = strchr( Chan->modes, Mode );
  458. if( ! p ) return false;
  459. /* Channel has mode -> delete */
  460. while( *p )
  461. {
  462. *p = *(p + 1);
  463. p++;
  464. }
  465. return true;
  466. } /* Channel_ModeDel */
  467. GLOBAL bool
  468. Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
  469. {
  470. /* Set Channel-User-Mode.
  471. * if mode was newly set, return true.
  472. * if the User already had this channel-mode, return false.
  473. */
  474. CL2CHAN *cl2chan;
  475. char x[2];
  476. assert( Chan != NULL );
  477. assert( Client != NULL );
  478. cl2chan = Get_Cl2Chan( Chan, Client );
  479. assert( cl2chan != NULL );
  480. x[0] = Mode; x[1] = '\0';
  481. if( ! strchr( cl2chan->modes, x[0] ))
  482. {
  483. /* mode not set, -> set it */
  484. strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
  485. return true;
  486. }
  487. else return false;
  488. } /* Channel_UserModeAdd */
  489. GLOBAL bool
  490. Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
  491. {
  492. /* Delete Channel-User-Mode.
  493. * If Mode was removed, return true.
  494. * If User did not have the Channel-Mode, return false.
  495. */
  496. CL2CHAN *cl2chan;
  497. char *p;
  498. assert( Chan != NULL );
  499. assert( Client != NULL );
  500. cl2chan = Get_Cl2Chan( Chan, Client );
  501. assert( cl2chan != NULL );
  502. p = strchr( cl2chan->modes, Mode );
  503. if( ! p ) return false;
  504. /* Client has Mode -> delete */
  505. while( *p )
  506. {
  507. *p = *(p + 1);
  508. p++;
  509. }
  510. return true;
  511. } /* Channel_UserModeDel */
  512. GLOBAL char *
  513. Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
  514. {
  515. /* return Users' Channel-Modes */
  516. CL2CHAN *cl2chan;
  517. assert( Chan != NULL );
  518. assert( Client != NULL );
  519. cl2chan = Get_Cl2Chan( Chan, Client );
  520. assert( cl2chan != NULL );
  521. return cl2chan->modes;
  522. } /* Channel_UserModes */
  523. GLOBAL bool
  524. Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
  525. {
  526. /* Test if Client is on Channel Chan */
  527. assert( Chan != NULL );
  528. assert( Client != NULL );
  529. return Get_Cl2Chan(Chan, Client) != NULL;
  530. } /* Channel_IsMemberOf */
  531. GLOBAL char *
  532. Channel_Topic( CHANNEL *Chan )
  533. {
  534. char *ret;
  535. assert( Chan != NULL );
  536. ret = array_start(&Chan->topic);
  537. return ret ? ret : "";
  538. } /* Channel_Topic */
  539. #ifndef STRICT_RFC
  540. GLOBAL unsigned int
  541. Channel_TopicTime(CHANNEL *Chan)
  542. {
  543. assert(Chan != NULL);
  544. return (unsigned int) Chan->topic_time;
  545. } /* Channel_TopicTime */
  546. GLOBAL char *
  547. Channel_TopicWho(CHANNEL *Chan)
  548. {
  549. assert(Chan != NULL);
  550. return Chan->topic_who;
  551. } /* Channel_TopicWho */
  552. GLOBAL unsigned int
  553. Channel_CreationTime(CHANNEL *Chan)
  554. {
  555. assert(Chan != NULL);
  556. return (unsigned int) Chan->creation_time;
  557. } /* Channel_CreationTime */
  558. #endif
  559. GLOBAL void
  560. Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
  561. {
  562. size_t len;
  563. assert( Chan != NULL );
  564. assert( Topic != NULL );
  565. len = strlen(Topic);
  566. if (len < array_bytes(&Chan->topic))
  567. array_free(&Chan->topic);
  568. if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
  569. Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
  570. Topic, Chan->name, strerror(errno));
  571. #ifndef STRICT_RFC
  572. Chan->topic_time = time(NULL);
  573. if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
  574. strlcpy(Chan->topic_who, Client_ID(Client),
  575. sizeof Chan->topic_who);
  576. else
  577. strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
  578. sizeof Chan->topic_who);
  579. #else
  580. (void) Client;
  581. #endif
  582. } /* Channel_SetTopic */
  583. GLOBAL void
  584. Channel_SetModes( CHANNEL *Chan, const char *Modes )
  585. {
  586. assert( Chan != NULL );
  587. assert( Modes != NULL );
  588. strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
  589. } /* Channel_SetModes */
  590. GLOBAL void
  591. Channel_SetKey( CHANNEL *Chan, const char *Key )
  592. {
  593. assert( Chan != NULL );
  594. assert( Key != NULL );
  595. strlcpy( Chan->key, Key, sizeof( Chan->key ));
  596. LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
  597. } /* Channel_SetKey */
  598. GLOBAL void
  599. Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
  600. {
  601. assert( Chan != NULL );
  602. Chan->maxusers = Count;
  603. LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
  604. } /* Channel_SetMaxUsers */
  605. static bool
  606. Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
  607. {
  608. bool is_member, has_voice, is_op;
  609. is_member = has_voice = is_op = false;
  610. /* The server itself always can send messages :-) */
  611. if (Client_ThisServer() == From)
  612. return true;
  613. if (Channel_IsMemberOf(Chan, From)) {
  614. is_member = true;
  615. if (strchr(Channel_UserModes(Chan, From), 'v'))
  616. has_voice = true;
  617. if (strchr(Channel_UserModes(Chan, From), 'o'))
  618. is_op = true;
  619. }
  620. /*
  621. * Is the client allowed to write to channel?
  622. *
  623. * If channel mode n set: non-members cannot send to channel.
  624. * If channel mode m set: need voice.
  625. */
  626. if (strchr(Channel_Modes(Chan), 'n') && !is_member)
  627. return false;
  628. if (is_op || has_voice)
  629. return true;
  630. if (strchr(Channel_Modes(Chan), 'm'))
  631. return false;
  632. return !Lists_Check(&Chan->list_bans, From);
  633. }
  634. GLOBAL bool
  635. Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
  636. bool SendErrors, const char *Text)
  637. {
  638. if (!Can_Send_To_Channel(Chan, From)) {
  639. if (! SendErrors)
  640. return CONNECTED; /* no error, see RFC 2812 */
  641. return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
  642. Client_ID(From), Channel_Name(Chan));
  643. }
  644. if (Client_Conn(From) > NONE)
  645. Conn_UpdateIdle(Client_Conn(From));
  646. return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
  647. "%s %s :%s", Command, Channel_Name(Chan), Text);
  648. }
  649. GLOBAL CHANNEL *
  650. Channel_Create( const char *Name )
  651. {
  652. /* Create new CHANNEL structure and add it to linked list */
  653. CHANNEL *c;
  654. assert( Name != NULL );
  655. c = (CHANNEL *)malloc( sizeof( CHANNEL ));
  656. if( ! c )
  657. {
  658. Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
  659. return NULL;
  660. }
  661. memset( c, 0, sizeof( CHANNEL ));
  662. strlcpy( c->name, Name, sizeof( c->name ));
  663. c->hash = Hash( c->name );
  664. c->next = My_Channels;
  665. #ifndef STRICT_RFC
  666. c->creation_time = time(NULL);
  667. #endif
  668. My_Channels = c;
  669. LogDebug("Created new channel structure for \"%s\".", Name);
  670. return c;
  671. } /* Channel_Create */
  672. static CL2CHAN *
  673. Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
  674. {
  675. CL2CHAN *cl2chan;
  676. assert( Chan != NULL );
  677. assert( Client != NULL );
  678. cl2chan = My_Cl2Chan;
  679. while( cl2chan )
  680. {
  681. if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
  682. cl2chan = cl2chan->next;
  683. }
  684. return NULL;
  685. } /* Get_Cl2Chan */
  686. static CL2CHAN *
  687. Add_Client( CHANNEL *Chan, CLIENT *Client )
  688. {
  689. CL2CHAN *cl2chan;
  690. assert( Chan != NULL );
  691. assert( Client != NULL );
  692. /* Create new CL2CHAN structure */
  693. cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
  694. if( ! cl2chan )
  695. {
  696. Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
  697. return NULL;
  698. }
  699. cl2chan->channel = Chan;
  700. cl2chan->client = Client;
  701. strcpy( cl2chan->modes, "" );
  702. /* concatenate */
  703. cl2chan->next = My_Cl2Chan;
  704. My_Cl2Chan = cl2chan;
  705. LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
  706. return cl2chan;
  707. } /* Add_Client */
  708. static bool
  709. Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
  710. {
  711. CL2CHAN *cl2chan, *last_cl2chan;
  712. CHANNEL *c;
  713. assert( Chan != NULL );
  714. assert( Client != NULL );
  715. assert( Origin != NULL );
  716. assert( Reason != NULL );
  717. /* Do not inform other servers if the channel is local to this server,
  718. * regardless of what the caller requested! */
  719. if(InformServer)
  720. InformServer = !Channel_IsLocal(Chan);
  721. last_cl2chan = NULL;
  722. cl2chan = My_Cl2Chan;
  723. while( cl2chan )
  724. {
  725. if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
  726. last_cl2chan = cl2chan;
  727. cl2chan = cl2chan->next;
  728. }
  729. if( ! cl2chan ) return false;
  730. c = cl2chan->channel;
  731. assert( c != NULL );
  732. /* maintain cl2chan list */
  733. if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
  734. else My_Cl2Chan = cl2chan->next;
  735. free( cl2chan );
  736. switch( Type )
  737. {
  738. case REMOVE_QUIT:
  739. /* QUIT: other servers have already been notified,
  740. * see Client_Destroy(); so only inform other clients
  741. * in same channel. */
  742. assert( InformServer == false );
  743. LogDebug("User \"%s\" left channel \"%s\" (%s).",
  744. Client_Mask( Client ), c->name, Reason );
  745. break;
  746. case REMOVE_KICK:
  747. /* User was KICKed: inform other servers (public
  748. * channels) and all users in the channel */
  749. if( InformServer )
  750. IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
  751. Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
  752. IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
  753. c->name, Client_ID( Client ), Reason );
  754. if ((Client_Conn(Client) > NONE) &&
  755. (Client_Type(Client) == CLIENT_USER))
  756. {
  757. IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
  758. c->name, Client_ID( Client ), Reason);
  759. }
  760. LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
  761. Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
  762. break;
  763. default: /* PART */
  764. if (Conf_MorePrivacy)
  765. Reason = "";
  766. if (InformServer)
  767. IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
  768. IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
  769. c->name, Reason);
  770. if ((Client_Conn(Origin) > NONE) &&
  771. (Client_Type(Origin) == CLIENT_USER))
  772. {
  773. IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
  774. LogDebug("User \"%s\" left channel \"%s\" (%s).",
  775. Client_Mask(Client), c->name, Reason);
  776. }
  777. }
  778. /* When channel is empty and is not pre-defined, delete */
  779. if( ! strchr( Channel_Modes( Chan ), 'P' ))
  780. {
  781. if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
  782. }
  783. return true;
  784. } /* Remove_Client */
  785. GLOBAL bool
  786. Channel_AddBan(CHANNEL *c, const char *mask )
  787. {
  788. struct list_head *h = Channel_GetListBans(c);
  789. LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "ban");
  790. return Lists_Add(h, mask, false);
  791. }
  792. GLOBAL bool
  793. Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
  794. {
  795. struct list_head *h = Channel_GetListInvites(c);
  796. LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "invite");
  797. return Lists_Add(h, mask, onlyonce);
  798. }
  799. static bool
  800. ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
  801. {
  802. struct list_elem *e;
  803. char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
  804. char *msg_end;
  805. assert( Client != NULL );
  806. assert( Channel != NULL );
  807. e = Lists_GetFirst(head);
  808. while (e) {
  809. if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
  810. Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
  811. e = Lists_GetNext(e);
  812. }
  813. msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
  814. return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
  815. }
  816. GLOBAL bool
  817. Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
  818. {
  819. struct list_head *h;
  820. assert( Channel != NULL );
  821. h = Channel_GetListBans(Channel);
  822. return ShowInvitesBans(h, Client, Channel, false);
  823. }
  824. GLOBAL bool
  825. Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
  826. {
  827. struct list_head *h;
  828. assert( Channel != NULL );
  829. h = Channel_GetListInvites(Channel);
  830. return ShowInvitesBans(h, Client, Channel, true);
  831. }
  832. /**
  833. * Log a message to the local &SERVER channel, if it exists.
  834. */
  835. GLOBAL void
  836. Channel_LogServer(const char *msg)
  837. {
  838. CHANNEL *sc;
  839. CLIENT *c;
  840. assert(msg != NULL);
  841. sc = Channel_Search("&SERVER");
  842. if (!sc)
  843. return;
  844. c = Client_ThisServer();
  845. Channel_Write(sc, c, c, "PRIVMSG", false, msg);
  846. } /* Channel_LogServer */
  847. GLOBAL bool
  848. Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
  849. {
  850. char *file_name, line[COMMAND_LEN], *nick, *pass;
  851. FILE *fd;
  852. assert(Chan != NULL);
  853. assert(Client != NULL);
  854. assert(Key != NULL);
  855. if (!strchr(Chan->modes, 'k'))
  856. return true;
  857. if (*Key == '\0')
  858. return false;
  859. if (strcmp(Chan->key, Key) == 0)
  860. return true;
  861. file_name = array_start(&Chan->keyfile);
  862. if (!file_name)
  863. return false;
  864. fd = fopen(file_name, "r");
  865. if (!fd) {
  866. Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
  867. file_name, Chan->name, strerror(errno));
  868. return false;
  869. }
  870. while (fgets(line, (int)sizeof(line), fd) != NULL) {
  871. ngt_TrimStr(line);
  872. if (! (nick = strchr(line, ':')))
  873. continue;
  874. *nick++ = '\0';
  875. if (!Match(line, Client_User(Client)))
  876. continue;
  877. if (! (pass = strchr(nick, ':')))
  878. continue;
  879. *pass++ = '\0';
  880. if (!Match(nick, Client_ID(Client)))
  881. continue;
  882. if (strcmp(Key, pass) != 0)
  883. continue;
  884. fclose(fd);
  885. return true;
  886. }
  887. fclose(fd);
  888. return false;
  889. } /* Channel_CheckKey */
  890. /**
  891. * Check wether a client is allowed to administer a channel or not.
  892. *
  893. * @param Chan The channel to test.
  894. * @param Client The client from which the command has been received.
  895. * @param Origin The originator of the command (or NULL).
  896. * @param OnChannel Set to true if the originator is member of the channel.
  897. * @param AdminOk Set to true if the client is allowed to do
  898. * administrative tasks on this channel.
  899. * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
  900. * that is send commands as if originating from a server
  901. * and not the originator of the command.
  902. */
  903. GLOBAL void
  904. Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
  905. bool *OnChannel, bool *AdminOk, bool *UseServerMode)
  906. {
  907. assert (Chan != NULL);
  908. assert (Client != NULL);
  909. assert (OnChannel != NULL);
  910. assert (AdminOk != NULL);
  911. assert (UseServerMode != NULL);
  912. /* Use the client as origin, if no origin has been given (no prefix?) */
  913. if (!Origin)
  914. Origin = Client;
  915. *OnChannel = false;
  916. *AdminOk = false;
  917. *UseServerMode = false;
  918. if (Client_Type(Client) != CLIENT_USER
  919. && Client_Type(Client) != CLIENT_SERVER
  920. && Client_Type(Client) != CLIENT_SERVICE)
  921. return;
  922. /* Allow channel administration if the client is a server or service */
  923. if (Client_Type(Client) != CLIENT_USER) {
  924. *AdminOk = true;
  925. return;
  926. }
  927. *OnChannel = Channel_IsMemberOf(Chan, Origin);
  928. if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
  929. /* User is a channel operator */
  930. *AdminOk = true;
  931. } else if (Conf_OperCanMode) {
  932. /* IRC operators are allowed to administer channels as well */
  933. if (Client_OperByMe(Origin)) {
  934. *AdminOk = true;
  935. if (Conf_OperServerMode)
  936. *UseServerMode = true;
  937. }
  938. }
  939. } /* Channel_CheckAdminRights */
  940. static CL2CHAN *
  941. Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
  942. {
  943. return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
  944. } /* Get_First_Cl2Chan */
  945. static CL2CHAN *
  946. Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
  947. {
  948. CL2CHAN *cl2chan;
  949. assert( Client != NULL || Channel != NULL );
  950. cl2chan = Start;
  951. while( cl2chan )
  952. {
  953. if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
  954. if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
  955. cl2chan = cl2chan->next;
  956. }
  957. return NULL;
  958. } /* Get_Next_Cl2Chan */
  959. /**
  960. * Remove a channel and free all of its data structures.
  961. */
  962. static void
  963. Delete_Channel(CHANNEL *Chan)
  964. {
  965. CHANNEL *chan, *last_chan;
  966. last_chan = NULL;
  967. chan = My_Channels;
  968. while (chan) {
  969. if (chan == Chan)
  970. break;
  971. last_chan = chan;
  972. chan = chan->next;
  973. }
  974. assert(chan != NULL);
  975. if (!chan)
  976. return;
  977. /* maintain channel list */
  978. if (last_chan)
  979. last_chan->next = chan->next;
  980. else
  981. My_Channels = chan->next;
  982. LogDebug("Freed channel structure for \"%s\".", Chan->name);
  983. Free_Channel(Chan);
  984. } /* Delete_Channel */
  985. static void
  986. Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
  987. {
  988. size_t len;
  989. assert(Chan != NULL);
  990. assert(KeyFile != NULL);
  991. len = strlen(KeyFile);
  992. if (len < array_bytes(&Chan->keyfile)) {
  993. Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
  994. array_free(&Chan->keyfile);
  995. }
  996. if (len < 1)
  997. return;
  998. if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
  999. Log(LOG_WARNING,
  1000. "Could not set new channel key file \"%s\" for %s: %s",
  1001. KeyFile, Chan->name, strerror(errno));
  1002. else
  1003. Log(LOG_INFO|LOG_snotice,
  1004. "New local channel key file \"%s\" for %s activated.",
  1005. KeyFile, Chan->name);
  1006. } /* Set_KeyFile */
  1007. /* -eof- */