irc-channel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 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. #include "portab.h"
  12. /**
  13. * @file
  14. * IRC channel commands
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "defines.h"
  22. #include "conn.h"
  23. #include "channel.h"
  24. #include "conn-func.h"
  25. #include "lists.h"
  26. #include "log.h"
  27. #include "match.h"
  28. #include "messages.h"
  29. #include "parse.h"
  30. #include "irc-info.h"
  31. #include "irc-write.h"
  32. #include "conf.h"
  33. #include "exp.h"
  34. #include "irc-channel.h"
  35. /**
  36. * Part from all channels.
  37. *
  38. * RFC 2812, (3.2.1 Join message Command):
  39. * Note that this message accepts a special argument ("0"), which is a
  40. * special request to leave all channels the user is currently a member of.
  41. * The server will process this message as if the user had sent a PART
  42. * command (See Section 3.2.2) for each channel he is a member of.
  43. *
  44. * @param client Client that initiated the part request
  45. * @param target Client that should part all joined channels
  46. * @returns CONNECTED or DISCONNECTED
  47. */
  48. static bool
  49. part_from_all_channels(CLIENT* client, CLIENT *target)
  50. {
  51. CL2CHAN *cl2chan;
  52. CHANNEL *chan;
  53. while ((cl2chan = Channel_FirstChannelOf(target))) {
  54. chan = Channel_GetChannel(cl2chan);
  55. assert( chan != NULL );
  56. Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
  57. }
  58. return CONNECTED;
  59. } /* part_from_all_channels */
  60. /**
  61. * Check weather a local client is allowed to join an already existing
  62. * channel or not.
  63. *
  64. * @param Client Client that sent the JOIN command
  65. * @param chan Channel to check
  66. * @param channame Name of the channel
  67. * @param key Provided channel key (or NULL)
  68. * @returns true if client is allowed to join, false otherwise
  69. */
  70. static bool
  71. join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
  72. const char *key)
  73. {
  74. bool is_invited, is_banned;
  75. const char *channel_modes;
  76. /* Allow IRC operators to overwrite channel limits */
  77. if (strchr(Client_Modes(Client), 'o'))
  78. return true;
  79. is_banned = Lists_Check(Channel_GetListBans(chan), Client);
  80. is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
  81. if (is_banned && !is_invited) {
  82. /* Client is banned from channel (and not on invite list) */
  83. IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
  84. Client_ID(Client), channame);
  85. return false;
  86. }
  87. channel_modes = Channel_Modes(chan);
  88. if (strchr(channel_modes, 'i') && !is_invited) {
  89. /* Channel is "invite-only" and client is not on invite list */
  90. IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
  91. Client_ID(Client), channame);
  92. return false;
  93. }
  94. if (!Channel_CheckKey(chan, Client, key ? key : "")) {
  95. /* Channel is protected by a channel key and the client
  96. * didn't specify the correct one */
  97. IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
  98. Client_ID(Client), channame);
  99. return false;
  100. }
  101. if (strchr(channel_modes, 'l') &&
  102. (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
  103. /* There are more clints joined to this channel than allowed */
  104. IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
  105. Client_ID(Client), channame);
  106. return false;
  107. }
  108. if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
  109. /* Only "secure" clients are allowed, but clients doesn't
  110. * use SSL encryption */
  111. IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
  112. Client_ID(Client), channame);
  113. return false;
  114. }
  115. if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
  116. /* Only IRC operators are allowed! */
  117. IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
  118. Client_ID(Client), channame);
  119. return false;
  120. }
  121. return true;
  122. } /* join_allowed */
  123. /**
  124. * Set user channel modes.
  125. *
  126. * @param chan Channel
  127. * @param target User to set modes for
  128. * @param flags Channel modes to add
  129. */
  130. static void
  131. join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
  132. {
  133. if (flags) {
  134. while (*flags) {
  135. Channel_UserModeAdd(chan, target, *flags);
  136. flags++;
  137. }
  138. }
  139. /* If channel persistent and client is ircop: make client chanop */
  140. if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
  141. Channel_UserModeAdd(chan, target, 'o');
  142. } /* join_set_channelmodes */
  143. /**
  144. * Forward JOIN command to a specific server
  145. *
  146. * This function diffentiates between servers using RFC 2813 mode that
  147. * support the JOIN command with appended ASCII 7 character and channel
  148. * modes, and servers using RFC 1459 protocol which require separate JOIN
  149. * and MODE commands.
  150. *
  151. * @param To Forward JOIN (and MODE) command to this peer server
  152. * @param Prefix Client used to prefix the genrated commands
  153. * @param Data Parameters of JOIN command to forward, probably
  154. * containing channel modes separated by ASCII 7.
  155. */
  156. static void
  157. cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
  158. {
  159. CONN_ID conn;
  160. char str[COMMAND_LEN], *ptr = NULL;
  161. strlcpy(str, (char *)Data, sizeof(str));
  162. conn = Client_Conn(To);
  163. if (Conn_Options(conn) & CONN_RFC1459) {
  164. /* RFC 1459 compatibility mode, appended modes are NOT
  165. * supported, so strip them off! */
  166. ptr = strchr(str, 0x7);
  167. if (ptr)
  168. *ptr++ = '\0';
  169. }
  170. IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
  171. if (ptr && *ptr)
  172. IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
  173. Client_ID(Prefix));
  174. } /* cb_join_forward */
  175. /**
  176. * Forward JOIN command to all servers
  177. *
  178. * This function calls cb_join_forward(), which differentiates between
  179. * protocol implementations (e.g. RFC 2812, RFC 1459).
  180. *
  181. * @param Client Client used to prefix the genrated commands
  182. * @param target Forward JOIN (and MODE) command to this peer server
  183. * @param chan Channel structure
  184. * @param channame Channel name
  185. */
  186. static void
  187. join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
  188. const char *channame)
  189. {
  190. char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
  191. /* RFC 2813, 4.2.1: channel modes are separated from the channel
  192. * name with ASCII 7, if any, and not spaces: */
  193. strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
  194. if (modes[1])
  195. modes[0] = 0x7;
  196. else
  197. modes[0] = '\0';
  198. /* forward to other servers (if it is not a local channel) */
  199. if (!Channel_IsLocal(chan)) {
  200. snprintf(str, sizeof(str), "%s%s", channame, modes);
  201. IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
  202. cb_join_forward, str);
  203. }
  204. /* tell users in this channel about the new client */
  205. IRC_WriteStrChannelPrefix(Client, chan, target, false,
  206. "JOIN :%s", channame);
  207. /* syncronize channel modes */
  208. if (modes[1]) {
  209. IRC_WriteStrChannelPrefix(Client, chan, target, false,
  210. "MODE %s +%s %s", channame,
  211. &modes[1], Client_ID(target));
  212. }
  213. } /* join_forward */
  214. /**
  215. * Aknowledge user JOIN request and send "channel info" numerics.
  216. *
  217. * @param Client Client used to prefix the genrated commands
  218. * @param target Forward commands/numerics to this user
  219. * @param chan Channel structure
  220. * @param channame Channel name
  221. */
  222. static bool
  223. join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
  224. const char *channame)
  225. {
  226. const char *topic;
  227. if (Client_Type(Client) != CLIENT_USER)
  228. return true;
  229. /* acknowledge join */
  230. if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
  231. return false;
  232. /* Send topic to client, if any */
  233. topic = Channel_Topic(chan);
  234. assert(topic != NULL);
  235. if (*topic) {
  236. if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
  237. Client_ID(Client), channame, topic))
  238. return false;
  239. #ifndef STRICT_RFC
  240. if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
  241. Client_ID(Client), channame,
  242. Channel_TopicWho(chan),
  243. Channel_TopicTime(chan)))
  244. return false;
  245. #endif
  246. }
  247. /* send list of channel members to client */
  248. if (!IRC_Send_NAMES(Client, chan))
  249. return false;
  250. return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
  251. Channel_Name(chan));
  252. } /* join_send_topic */
  253. /**
  254. * Handler for the IRC "JOIN" command.
  255. *
  256. * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
  257. *
  258. * @param Client The client from which this command has been received
  259. * @param Req Request structure with prefix and all parameters
  260. * @returns CONNECTED or DISCONNECTED
  261. */
  262. GLOBAL bool
  263. IRC_JOIN( CLIENT *Client, REQUEST *Req )
  264. {
  265. char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
  266. CLIENT *target;
  267. CHANNEL *chan;
  268. assert( Client != NULL );
  269. assert( Req != NULL );
  270. /* Bad number of arguments? */
  271. if (Req->argc < 1 || Req->argc > 2)
  272. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  273. Client_ID(Client), Req->command);
  274. /* Who is the sender? */
  275. if (Client_Type(Client) == CLIENT_SERVER)
  276. target = Client_Search(Req->prefix);
  277. else
  278. target = Client;
  279. if (!target)
  280. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
  281. /* Is argument "0"? */
  282. if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
  283. return part_from_all_channels(Client, target);
  284. /* Are channel keys given? */
  285. if (Req->argc > 1)
  286. key = strtok_r(Req->argv[1], ",", &lastkey);
  287. channame = Req->argv[0];
  288. channame = strtok_r(channame, ",", &lastchan);
  289. /* Make sure that "channame" is not the empty string ("JOIN :") */
  290. if (! channame)
  291. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  292. Client_ID(Client), Req->command);
  293. while (channame) {
  294. flags = NULL;
  295. /* Did the server include channel-user-modes? */
  296. if (Client_Type(Client) == CLIENT_SERVER) {
  297. flags = strchr(channame, 0x7);
  298. if (flags) {
  299. *flags = '\0';
  300. flags++;
  301. }
  302. }
  303. chan = Channel_Search(channame);
  304. if (!chan && Conf_PredefChannelsOnly) {
  305. /* channel must be created, but server does not allow this */
  306. IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
  307. break;
  308. }
  309. /* Local client? */
  310. if (Client_Type(Client) == CLIENT_USER) {
  311. /* Test if the user has reached the channel limit */
  312. if ((Conf_MaxJoins > 0) &&
  313. (Channel_CountForUser(Client) >= Conf_MaxJoins))
  314. return IRC_WriteStrClient(Client,
  315. ERR_TOOMANYCHANNELS_MSG,
  316. Client_ID(Client), channame);
  317. if (chan) {
  318. /* Already existing channel: check if the
  319. * client is allowed to join */
  320. if (!join_allowed(Client, chan, channame, key))
  321. break;
  322. } else {
  323. /* New channel: first user will become channel
  324. * operator unless this is a modeless channel */
  325. if (*channame != '+')
  326. flags = "o";
  327. }
  328. /* Local client: update idle time */
  329. Conn_UpdateIdle(Client_Conn(Client));
  330. } else {
  331. /* Remote server: we don't need to know whether the
  332. * client is invited or not, but we have to make sure
  333. * that the "one shot" entries (generated by INVITE
  334. * commands) in this list become deleted when a user
  335. * joins a channel this way. */
  336. if (chan)
  337. (void)Lists_Check(Channel_GetListInvites(chan),
  338. target);
  339. }
  340. /* Join channel (and create channel if it doesn't exist) */
  341. if (!Channel_Join(target, channame))
  342. break;
  343. if (!chan) { /* channel is new; it has been created above */
  344. chan = Channel_Search(channame);
  345. assert(chan != NULL);
  346. if (Channel_IsModeless(chan)) {
  347. Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
  348. Channel_ModeAdd(chan, 'n'); /* no external msgs */
  349. }
  350. }
  351. assert(chan != NULL);
  352. join_set_channelmodes(chan, target, flags);
  353. join_forward(Client, target, chan, channame);
  354. if (!join_send_topic(Client, target, chan, channame))
  355. break; /* write error */
  356. /* next channel? */
  357. channame = strtok_r(NULL, ",", &lastchan);
  358. if (channame && key)
  359. key = strtok_r(NULL, ",", &lastkey);
  360. }
  361. return CONNECTED;
  362. } /* IRC_JOIN */
  363. /**
  364. * Handler for the IRC "PART" command.
  365. *
  366. * See RFC 2812, 3.2.2: "Part message".
  367. *
  368. * @param Client The client from which this command has been received
  369. * @param Req Request structure with prefix and all parameters
  370. * @returns CONNECTED or DISCONNECTED
  371. */
  372. GLOBAL bool
  373. IRC_PART(CLIENT * Client, REQUEST * Req)
  374. {
  375. CLIENT *target;
  376. char *chan;
  377. assert(Client != NULL);
  378. assert(Req != NULL);
  379. if (Req->argc < 1 || Req->argc > 2)
  380. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  381. Client_ID(Client), Req->command);
  382. /* Get the sender */
  383. if (Client_Type(Client) == CLIENT_SERVER)
  384. target = Client_Search(Req->prefix);
  385. else
  386. target = Client;
  387. if (!target)
  388. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  389. Client_ID(Client), Req->prefix);
  390. /* Loop over all the given channel names */
  391. chan = strtok(Req->argv[0], ",");
  392. /* Make sure that "chan" is not the empty string ("PART :") */
  393. if (! chan)
  394. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  395. Client_ID(Client), Req->command);
  396. while (chan) {
  397. Channel_Part(target, Client, chan,
  398. Req->argc > 1 ? Req->argv[1] : Client_ID(target));
  399. chan = strtok(NULL, ",");
  400. }
  401. /* Update idle time, if local client */
  402. if (Client_Conn(Client) > NONE)
  403. Conn_UpdateIdle(Client_Conn(Client));
  404. return CONNECTED;
  405. } /* IRC_PART */
  406. /**
  407. * Handler for the IRC "TOPIC" command.
  408. *
  409. * See RFC 2812, 3.2.4 "Topic message".
  410. *
  411. * @param Client The client from which this command has been received
  412. * @param Req Request structure with prefix and all parameters
  413. * @returns CONNECTED or DISCONNECTED
  414. */
  415. GLOBAL bool
  416. IRC_TOPIC( CLIENT *Client, REQUEST *Req )
  417. {
  418. CHANNEL *chan;
  419. CLIENT *from;
  420. char *topic;
  421. bool onchannel, topicok, use_servermode, r;
  422. assert( Client != NULL );
  423. assert( Req != NULL );
  424. if (Req->argc < 1 || Req->argc > 2)
  425. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  426. Client_ID(Client), Req->command);
  427. if (Client_Type(Client) == CLIENT_SERVER)
  428. from = Client_Search(Req->prefix);
  429. else
  430. from = Client;
  431. if (!from)
  432. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  433. Client_ID(Client), Req->prefix);
  434. chan = Channel_Search(Req->argv[0]);
  435. if (!chan)
  436. return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
  437. Client_ID(from), Req->argv[0]);
  438. Channel_CheckAdminRights(chan, Client, from,
  439. &onchannel, &topicok, &use_servermode);
  440. if (!onchannel && !topicok)
  441. return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
  442. Client_ID(from), Req->argv[0]);
  443. if (Req->argc == 1) {
  444. /* Request actual topic */
  445. topic = Channel_Topic(chan);
  446. if (*topic) {
  447. r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
  448. Client_ID(Client),
  449. Channel_Name(chan), topic);
  450. #ifndef STRICT_RFC
  451. if (!r)
  452. return r;
  453. r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
  454. Client_ID(Client),
  455. Channel_Name(chan),
  456. Channel_TopicWho(chan),
  457. Channel_TopicTime(chan));
  458. #endif
  459. return r;
  460. }
  461. else
  462. return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
  463. Client_ID(from),
  464. Channel_Name(chan));
  465. }
  466. if (strchr(Channel_Modes(chan), 't')) {
  467. /* Topic Lock. Is the user a channel or IRC operator? */
  468. if (!topicok)
  469. return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
  470. Client_ID(from),
  471. Channel_Name(chan));
  472. }
  473. /* Set new topic */
  474. Channel_SetTopic(chan, from, Req->argv[1]);
  475. LogDebug("%s \"%s\" set topic on \"%s\": %s",
  476. Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
  477. Req->argv[1][0] ? Req->argv[1] : "<none>");
  478. if (use_servermode)
  479. from = Client_ThisServer();
  480. /* Update channel and forward new topic to other servers */
  481. if (!Channel_IsLocal(chan))
  482. IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
  483. Req->argv[0], Req->argv[1]);
  484. IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
  485. Req->argv[0], Req->argv[1]);
  486. if (Client_Type(Client) == CLIENT_USER)
  487. return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
  488. Req->argv[0], Req->argv[1]);
  489. else
  490. return CONNECTED;
  491. } /* IRC_TOPIC */
  492. /**
  493. * Handler for the IRC "LIST" command.
  494. *
  495. * See RFC 2812, 3.2.6 "List message".
  496. *
  497. * This implementation handles the local case as well as the forwarding of the
  498. * LIST command to other servers in the IRC network.
  499. *
  500. * @param Client The client from which this command has been received
  501. * @param Req Request structure with prefix and all parameters
  502. * @returns CONNECTED or DISCONNECTED
  503. */
  504. GLOBAL bool
  505. IRC_LIST( CLIENT *Client, REQUEST *Req )
  506. {
  507. char *pattern;
  508. CHANNEL *chan;
  509. CLIENT *from, *target;
  510. assert( Client != NULL );
  511. assert( Req != NULL );
  512. /* Bad number of prameters? */
  513. if( Req->argc > 2 )
  514. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  515. Client_ID( Client ), Req->command );
  516. if( Req->argc > 0 )
  517. pattern = strtok( Req->argv[0], "," );
  518. else
  519. pattern = "*";
  520. /* Get sender from prefix, if any */
  521. if( Client_Type( Client ) == CLIENT_SERVER )
  522. from = Client_Search( Req->prefix );
  523. else
  524. from = Client;
  525. if( ! from )
  526. return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
  527. Client_ID( Client ), Req->prefix );
  528. if( Req->argc == 2 )
  529. {
  530. /* Forward to other server? */
  531. target = Client_Search( Req->argv[1] );
  532. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
  533. return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
  534. Client_ID( Client ), Req->argv[1] );
  535. if( target != Client_ThisServer( ))
  536. {
  537. /* Target is indeed an other server, forward it! */
  538. return IRC_WriteStrClientPrefix( target, from,
  539. "LIST %s :%s", Client_ID( from ),
  540. Req->argv[1] );
  541. }
  542. }
  543. while( pattern )
  544. {
  545. /* Loop through all the channels */
  546. chan = Channel_First( );
  547. while( chan )
  548. {
  549. /* Check search pattern */
  550. if( Match( pattern, Channel_Name( chan )))
  551. {
  552. /* Gotcha! */
  553. if( ! strchr( Channel_Modes( chan ), 's' ) ||
  554. Channel_IsMemberOf( chan, from ))
  555. {
  556. if( ! IRC_WriteStrClient( from,
  557. RPL_LIST_MSG, Client_ID( from ),
  558. Channel_Name( chan ),
  559. Channel_MemberCount( chan ),
  560. Channel_Topic( chan )))
  561. return DISCONNECTED;
  562. }
  563. }
  564. chan = Channel_Next( chan );
  565. }
  566. /* Get next name ... */
  567. if( Req->argc > 0 )
  568. pattern = strtok( NULL, "," );
  569. else
  570. pattern = NULL;
  571. }
  572. return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
  573. } /* IRC_LIST */
  574. /**
  575. * Handler for the IRC+ command "CHANINFO".
  576. *
  577. * See doc/Protocol.txt, section II.3:
  578. * "Exchange channel-modes, topics, and persistent channels".
  579. *
  580. * @param Client The client from which this command has been received
  581. * @param Req Request structure with prefix and all parameters
  582. * @returns CONNECTED or DISCONNECTED
  583. */
  584. GLOBAL bool
  585. IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
  586. {
  587. char modes_add[COMMAND_LEN], l[16], *ptr;
  588. CLIENT *from;
  589. CHANNEL *chan;
  590. int arg_topic;
  591. assert( Client != NULL );
  592. assert( Req != NULL );
  593. /* Bad number of parameters? */
  594. if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
  595. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  596. Client_ID(Client), Req->command);
  597. /* Compatibility kludge */
  598. if( Req->argc == 5 ) arg_topic = 4;
  599. else if( Req->argc == 3 ) arg_topic = 2;
  600. else arg_topic = -1;
  601. /* Search origin */
  602. from = Client_Search( Req->prefix );
  603. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  604. /* Search or create channel */
  605. chan = Channel_Search( Req->argv[0] );
  606. if( ! chan ) chan = Channel_Create( Req->argv[0] );
  607. if( ! chan ) return CONNECTED;
  608. if( Req->argv[1][0] == '+' )
  609. {
  610. ptr = Channel_Modes( chan );
  611. if( ! *ptr )
  612. {
  613. /* OK, this channel doesn't have modes jet, set the received ones: */
  614. Channel_SetModes( chan, &Req->argv[1][1] );
  615. if( Req->argc == 5 )
  616. {
  617. if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
  618. if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
  619. }
  620. else
  621. {
  622. /* Delete modes which we never want to inherit */
  623. Channel_ModeDel( chan, 'l' );
  624. Channel_ModeDel( chan, 'k' );
  625. }
  626. strcpy( modes_add, "" );
  627. ptr = Channel_Modes( chan );
  628. while( *ptr )
  629. {
  630. if( *ptr == 'l' )
  631. {
  632. snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
  633. strlcat( modes_add, l, sizeof( modes_add ));
  634. }
  635. if( *ptr == 'k' )
  636. {
  637. strlcat( modes_add, " ", sizeof( modes_add ));
  638. strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
  639. }
  640. ptr++;
  641. }
  642. /* Inform members of this channel */
  643. IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
  644. }
  645. }
  646. else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
  647. if( arg_topic > 0 )
  648. {
  649. /* We got a topic */
  650. ptr = Channel_Topic( chan );
  651. if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
  652. {
  653. /* OK, there is no topic jet */
  654. Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
  655. IRC_WriteStrChannelPrefix(Client, chan, from, false,
  656. "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
  657. }
  658. }
  659. /* Forward CHANINFO to other serevrs */
  660. if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
  661. else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
  662. else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
  663. return CONNECTED;
  664. } /* IRC_CHANINFO */
  665. /* -eof- */