irc-channel.c 22 KB

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