channel.c 29 KB

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