channel.c 28 KB

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