channel.c 25 KB

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