channel.c 25 KB

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