irc-channel.c 22 KB

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