irc-channel.c 21 KB

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