channel.c 29 KB

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