channel.c 31 KB

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