channel.c 29 KB

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