irc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2015 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 commands
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include <time.h>
  21. #include "ngircd.h"
  22. #include "conn-func.h"
  23. #include "conf.h"
  24. #include "channel.h"
  25. #ifdef ICONV
  26. # include "conn-encoding.h"
  27. #endif
  28. #include "irc-macros.h"
  29. #include "irc-write.h"
  30. #include "log.h"
  31. #include "match.h"
  32. #include "messages.h"
  33. #include "parse.h"
  34. #include "op.h"
  35. #include "irc.h"
  36. static char *Option_String PARAMS((CONN_ID Idx));
  37. static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
  38. bool SendErrors));
  39. static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
  40. char *targetMask, char *message,
  41. bool SendErrors));
  42. static bool Help PARAMS((CLIENT *Client, const char *Topic));
  43. /**
  44. * Check if a list limit is reached and inform client accordingly.
  45. *
  46. * @param From The client.
  47. * @param Count Reply item count.
  48. * @param Limit Reply limit.
  49. * @param Name Name of the list.
  50. * @return true if list limit has been reached; false otherwise.
  51. */
  52. GLOBAL bool
  53. IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
  54. const char *Name)
  55. {
  56. assert(From != NULL);
  57. assert(Count >= 0);
  58. assert(Limit > 0);
  59. assert(Name != NULL);
  60. if (Count < Limit)
  61. return false;
  62. (void)IRC_WriteStrClient(From,
  63. "NOTICE %s :%s list limit (%d) reached!",
  64. Client_ID(From), Name, Limit);
  65. IRC_SetPenalty(From, 2);
  66. return true;
  67. }
  68. /**
  69. * Handler for the IRC "ERROR" command.
  70. *
  71. * @param Client The client from which this command has been received.
  72. * @param Req Request structure with prefix and all parameters.
  73. * @return CONNECTED or DISCONNECTED.
  74. */
  75. GLOBAL bool
  76. IRC_ERROR(CLIENT *Client, REQUEST *Req)
  77. {
  78. assert( Client != NULL );
  79. assert( Req != NULL );
  80. if (Client_Type(Client) != CLIENT_GOTPASS
  81. && Client_Type(Client) != CLIENT_GOTPASS_2813
  82. && Client_Type(Client) != CLIENT_UNKNOWNSERVER
  83. && Client_Type(Client) != CLIENT_SERVER
  84. && Client_Type(Client) != CLIENT_SERVICE) {
  85. LogDebug("Ignored ERROR command from \"%s\" ...",
  86. Client_Mask(Client));
  87. IRC_SetPenalty(Client, 2);
  88. return CONNECTED;
  89. }
  90. if (Req->argc < 1)
  91. Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
  92. Client_Mask(Client));
  93. else
  94. Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
  95. Client_Mask(Client), Req->argv[0]);
  96. return CONNECTED;
  97. } /* IRC_ERROR */
  98. /**
  99. * Handler for the IRC "KILL" command.
  100. *
  101. * This function implements the IRC command "KILL" which is used to selectively
  102. * disconnect clients. It can be used by IRC operators and servers, for example
  103. * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
  104. *
  105. * Please note that this function is also called internally, without a real
  106. * KILL command being received over the network! Client is Client_ThisServer()
  107. * in this case, and the prefix in Req is NULL.
  108. *
  109. * @param Client The client from which this command has been received or
  110. * Client_ThisServer() when generated interanlly.
  111. * @param Req Request structure with prefix and all parameters.
  112. * @return CONNECTED or DISCONNECTED.
  113. */
  114. GLOBAL bool
  115. IRC_KILL(CLIENT *Client, REQUEST *Req)
  116. {
  117. CLIENT *prefix;
  118. char reason[COMMAND_LEN];
  119. assert (Client != NULL);
  120. assert (Req != NULL);
  121. if (Client_Type(Client) != CLIENT_SERVER && !Op_Check(Client, Req))
  122. return Op_NoPrivileges(Client, Req);
  123. /* Get prefix (origin); use the client if no prefix is given. */
  124. if (Req->prefix)
  125. prefix = Client_Search(Req->prefix);
  126. else
  127. prefix = Client;
  128. /* Log a warning message and use this server as origin when the
  129. * prefix (origin) is invalid. And this is the reason why we don't
  130. * use the _IRC_GET_SENDER_OR_RETURN_ macro above! */
  131. if (!prefix) {
  132. Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
  133. Req->prefix );
  134. prefix = Client_ThisServer();
  135. }
  136. if (Client != Client_ThisServer())
  137. Log(LOG_NOTICE|LOG_snotice,
  138. "Got KILL command from \"%s\" for \"%s\": \"%s\".",
  139. Client_Mask(prefix), Req->argv[0], Req->argv[1]);
  140. /* Build reason string: Prefix the "reason" if the originator is a
  141. * regular user, so users can't spoof KILLs of servers. */
  142. if (Client_Type(Client) == CLIENT_USER)
  143. snprintf(reason, sizeof(reason), "KILLed by %s: %s",
  144. Client_ID(Client), Req->argv[1]);
  145. else
  146. strlcpy(reason, Req->argv[1], sizeof(reason));
  147. return IRC_KillClient(Client, prefix, Req->argv[0], reason);
  148. }
  149. /**
  150. * Handler for the IRC "NOTICE" command.
  151. *
  152. * @param Client The client from which this command has been received.
  153. * @param Req Request structure with prefix and all parameters.
  154. * @return CONNECTED or DISCONNECTED.
  155. */
  156. GLOBAL bool
  157. IRC_NOTICE(CLIENT *Client, REQUEST *Req)
  158. {
  159. return Send_Message(Client, Req, CLIENT_USER, false);
  160. } /* IRC_NOTICE */
  161. /**
  162. * Handler for the IRC "PRIVMSG" command.
  163. *
  164. * @param Client The client from which this command has been received.
  165. * @param Req Request structure with prefix and all parameters.
  166. * @return CONNECTED or DISCONNECTED.
  167. */
  168. GLOBAL bool
  169. IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
  170. {
  171. return Send_Message(Client, Req, CLIENT_USER, true);
  172. } /* IRC_PRIVMSG */
  173. /**
  174. * Handler for the IRC "SQUERY" command.
  175. *
  176. * @param Client The client from which this command has been received.
  177. * @param Req Request structure with prefix and all parameters.
  178. * @return CONNECTED or DISCONNECTED.
  179. */
  180. GLOBAL bool
  181. IRC_SQUERY(CLIENT *Client, REQUEST *Req)
  182. {
  183. return Send_Message(Client, Req, CLIENT_SERVICE, true);
  184. } /* IRC_SQUERY */
  185. /*
  186. * Handler for the IRC "TRACE" command.
  187. *
  188. * @param Client The client from which this command has been received.
  189. * @param Req Request structure with prefix and all parameters.
  190. * @return CONNECTED or DISCONNECTED.
  191. */
  192. GLOBAL bool
  193. IRC_TRACE(CLIENT *Client, REQUEST *Req)
  194. {
  195. CLIENT *from, *target, *c;
  196. CONN_ID idx, idx2;
  197. char user[CLIENT_USER_LEN];
  198. assert(Client != NULL);
  199. assert(Req != NULL);
  200. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  201. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
  202. /* Forward command to other server? */
  203. if (target != Client_ThisServer()) {
  204. /* Send RPL_TRACELINK back to initiator */
  205. idx = Client_Conn(Client);
  206. assert(idx > NONE);
  207. idx2 = Client_Conn(Client_NextHop(target));
  208. assert(idx2 > NONE);
  209. if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
  210. Client_ID(from), PACKAGE_NAME,
  211. PACKAGE_VERSION, Client_ID(target),
  212. Client_ID(Client_NextHop(target)),
  213. Option_String(idx2),
  214. (long)(time(NULL) - Conn_StartTime(idx2)),
  215. Conn_SendQ(idx), Conn_SendQ(idx2)))
  216. return DISCONNECTED;
  217. /* Forward command */
  218. IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
  219. return CONNECTED;
  220. }
  221. /* Infos about all connected servers */
  222. c = Client_First();
  223. while (c) {
  224. if (Client_Conn(c) > NONE) {
  225. /* Local client */
  226. if (Client_Type(c) == CLIENT_SERVER) {
  227. /* Server link */
  228. strlcpy(user, Client_User(c), sizeof(user));
  229. if (user[0] == '~')
  230. strlcpy(user, "unknown", sizeof(user));
  231. if (!IRC_WriteStrClient(from,
  232. RPL_TRACESERVER_MSG,
  233. Client_ID(from), Client_ID(c),
  234. user, Client_Hostname(c),
  235. Client_Mask(Client_ThisServer()),
  236. Option_String(Client_Conn(c))))
  237. return DISCONNECTED;
  238. }
  239. if (Client_Type(c) == CLIENT_USER
  240. && Client_HasMode(c, 'o')) {
  241. /* IRC Operator */
  242. if (!IRC_WriteStrClient(from,
  243. RPL_TRACEOPERATOR_MSG,
  244. Client_ID(from), Client_ID(c)))
  245. return DISCONNECTED;
  246. }
  247. }
  248. c = Client_Next( c );
  249. }
  250. return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
  251. Conf_ServerName, PACKAGE_NAME,
  252. PACKAGE_VERSION, NGIRCd_DebugLevel);
  253. } /* IRC_TRACE */
  254. /**
  255. * Handler for the IRC "HELP" command.
  256. *
  257. * @param Client The client from which this command has been received.
  258. * @param Req Request structure with prefix and all parameters.
  259. * @return CONNECTED or DISCONNECTED.
  260. */
  261. GLOBAL bool
  262. IRC_HELP(CLIENT *Client, REQUEST *Req)
  263. {
  264. COMMAND *cmd;
  265. assert(Client != NULL);
  266. assert(Req != NULL);
  267. if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
  268. || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
  269. /* Help text available and requested */
  270. if (Req->argc >= 1)
  271. return Help(Client, Req->argv[0]);
  272. if (!Help(Client, "Intro"))
  273. return DISCONNECTED;
  274. return CONNECTED;
  275. }
  276. cmd = Parse_GetCommandStruct();
  277. while(cmd->name) {
  278. if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
  279. Client_ID(Client), cmd->name))
  280. return DISCONNECTED;
  281. cmd++;
  282. }
  283. return CONNECTED;
  284. } /* IRC_HELP */
  285. /**
  286. * Kill an client identified by its nick name.
  287. *
  288. * Please note that after killig a client, its CLIENT cond CONNECTION
  289. * structures are invalid. So the caller must make sure on its own not to
  290. * access data of probably killed clients after calling this function!
  291. *
  292. * @param Client The client from which the command leading to the KILL has
  293. * been received, or NULL. The KILL will no be forwarded in this
  294. * direction. Only relevant when From is set, too.
  295. * @param From The client from which the command originated, or NULL for
  296. the local server.
  297. * @param Nick The nick name to kill.
  298. * @param Reason Text to send as reason to the client and other servers.
  299. */
  300. GLOBAL bool
  301. IRC_KillClient(CLIENT *Client, CLIENT *From, const char *Nick, const char *Reason)
  302. {
  303. const char *msg;
  304. CONN_ID my_conn, conn;
  305. CLIENT *c;
  306. /* Do we know such a client in the network? */
  307. c = Client_Search(Nick);
  308. if (!c) {
  309. LogDebug("Client with nick \"%s\" is unknown, not forwaring.", Nick);
  310. return CONNECTED;
  311. }
  312. /* Inform other servers */
  313. IRC_WriteStrServersPrefix(From ? Client : NULL,
  314. From ? From : Client_ThisServer(),
  315. "KILL %s :%s", Nick, Reason);
  316. if (Client_Type(c) != CLIENT_USER && Client_Type(c) != CLIENT_GOTNICK) {
  317. /* Target of this KILL is not a regular user, this is
  318. * invalid! So we ignore this case if we received a
  319. * regular KILL from the network and try to kill the
  320. * client/connection anyway (but log an error!) if the
  321. * origin is the local server. */
  322. if (Client != Client_ThisServer()) {
  323. /* Invalid KILL received from remote */
  324. if (Client_Type(c) == CLIENT_SERVER)
  325. msg = ERR_CANTKILLSERVER_MSG;
  326. else
  327. msg = ERR_NOPRIVILEGES_MSG;
  328. return IRC_WriteErrClient(Client, msg, Client_ID(Client));
  329. }
  330. Log(LOG_ERR,
  331. "Got KILL for invalid client type: %d, \"%s\"!",
  332. Client_Type(c), Nick);
  333. }
  334. /* Save ID of this connection */
  335. my_conn = Client_Conn(Client);
  336. /* Kill the client NOW:
  337. * - Close the local connection (if there is one),
  338. * - Destroy the CLIENT structure for remote clients.
  339. * Note: Conn_Close() removes the CLIENT structure as well. */
  340. conn = Client_Conn(c);
  341. if(conn > NONE)
  342. Conn_Close(conn, NULL, Reason, true);
  343. else
  344. Client_Destroy(c, NULL, Reason, false);
  345. /* Are we still connected or were we killed, too? */
  346. if (my_conn > NONE && Conn_GetClient(my_conn))
  347. return CONNECTED;
  348. else
  349. return DISCONNECTED;
  350. }
  351. /**
  352. * Send help for a given topic to the client.
  353. *
  354. * @param Client The client requesting help.
  355. * @param Topoc The help topic requested.
  356. * @return CONNECTED or DISCONNECTED.
  357. */
  358. static bool
  359. Help(CLIENT *Client, const char *Topic)
  360. {
  361. char *line;
  362. size_t helptext_len, len_str, idx_start, lines = 0;
  363. bool in_article = false;
  364. assert(Client != NULL);
  365. assert(Topic != NULL);
  366. helptext_len = array_bytes(&Conf_Helptext);
  367. line = array_start(&Conf_Helptext);
  368. while (helptext_len > 0) {
  369. len_str = strlen(line) + 1;
  370. assert(helptext_len >= len_str);
  371. helptext_len -= len_str;
  372. if (in_article) {
  373. /* The first character in each article text line must
  374. * be a TAB (ASCII 9) character which will be stripped
  375. * in the output. If it is not a TAB, the end of the
  376. * article has been reached. */
  377. if (line[0] != '\t') {
  378. if (lines > 0)
  379. return CONNECTED;
  380. else
  381. break;
  382. }
  383. /* A single '.' character indicates an empty line */
  384. if (line[1] == '.' && line[2] == '\0')
  385. idx_start = 2;
  386. else
  387. idx_start = 1;
  388. if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
  389. Client_ID(Client),
  390. &line[idx_start]))
  391. return DISCONNECTED;
  392. lines++;
  393. } else {
  394. if (line[0] == '-' && line[1] == ' '
  395. && strcasecmp(&line[2], Topic) == 0)
  396. in_article = true;
  397. }
  398. line += len_str;
  399. }
  400. /* Help topic not found (or empty)! */
  401. if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
  402. Client_ID(Client), Topic))
  403. return DISCONNECTED;
  404. return CONNECTED;
  405. }
  406. /**
  407. * Get pointer to a static string representing the connection "options".
  408. *
  409. * @param Idx Connection index.
  410. * @return Pointer to static (global) string buffer.
  411. */
  412. static char *
  413. #ifdef ZLIB
  414. Option_String(CONN_ID Idx)
  415. #else
  416. Option_String(UNUSED CONN_ID Idx)
  417. #endif
  418. {
  419. static char option_txt[8];
  420. UINT16 options;
  421. assert(Idx != NONE);
  422. options = Conn_Options(Idx);
  423. strcpy(option_txt, "F"); /* No idea what this means, but the
  424. * original ircd sends it ... */
  425. #ifdef SSL_SUPPORT
  426. if(options & CONN_SSL) /* SSL encrypted link */
  427. strlcat(option_txt, "s", sizeof(option_txt));
  428. #endif
  429. #ifdef ZLIB
  430. if(options & CONN_ZIP) /* zlib compression enabled */
  431. strlcat(option_txt, "z", sizeof(option_txt));
  432. #endif
  433. LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
  434. return option_txt;
  435. } /* Option_String */
  436. static bool
  437. Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
  438. {
  439. CLIENT *cl, *from;
  440. CL2CHAN *cl2chan;
  441. CHANNEL *chan;
  442. char *currentTarget = Req->argv[0];
  443. char *lastCurrentTarget = NULL;
  444. char *message = NULL;
  445. assert(Client != NULL);
  446. assert(Req != NULL);
  447. if (Req->argc == 0) {
  448. if (!SendErrors)
  449. return CONNECTED;
  450. return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
  451. Client_ID(Client), Req->command);
  452. }
  453. if (Req->argc == 1) {
  454. if (!SendErrors)
  455. return CONNECTED;
  456. return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
  457. Client_ID(Client));
  458. }
  459. if (Req->argc > 2) {
  460. if (!SendErrors)
  461. return CONNECTED;
  462. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  463. Client_ID(Client), Req->command);
  464. }
  465. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
  466. from = Client_Search(Req->prefix);
  467. else
  468. from = Client;
  469. if (!from)
  470. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  471. Client_ID(Client), Req->prefix);
  472. #ifdef ICONV
  473. if (Client_Conn(Client) > NONE)
  474. message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
  475. else
  476. #endif
  477. message = Req->argv[1];
  478. /* handle msgtarget = msgto *("," msgto) */
  479. currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
  480. ngt_UpperStr(Req->command);
  481. while (currentTarget) {
  482. /* Check for and handle valid <msgto> of form:
  483. * RFC 2812 2.3.1:
  484. * msgto = channel / ( user [ "%" host ] "@" servername )
  485. * msgto =/ ( user "%" host ) / targetmask
  486. * msgto =/ nickname / ( nickname "!" user "@" host )
  487. */
  488. if (strchr(currentTarget, '!') == NULL)
  489. /* nickname */
  490. cl = Client_Search(currentTarget);
  491. else
  492. cl = NULL;
  493. if (cl == NULL) {
  494. /* If currentTarget isn't a nickname check for:
  495. * user ["%" host] "@" servername
  496. * user "%" host
  497. * nickname "!" user "@" host
  498. */
  499. char target[COMMAND_LEN];
  500. char * nick = NULL;
  501. char * user = NULL;
  502. char * host = NULL;
  503. char * server = NULL;
  504. strlcpy(target, currentTarget, COMMAND_LEN);
  505. server = strchr(target, '@');
  506. if (server) {
  507. *server = '\0';
  508. server++;
  509. }
  510. host = strchr(target, '%');
  511. if (host) {
  512. *host = '\0';
  513. host++;
  514. }
  515. user = strchr(target, '!');
  516. if (user) {
  517. /* msgto form: nick!user@host */
  518. *user = '\0';
  519. user++;
  520. nick = target;
  521. host = server; /* not "@server" but "@host" */
  522. } else {
  523. user = target;
  524. }
  525. for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
  526. if (Client_Type(cl) != CLIENT_USER &&
  527. Client_Type(cl) != CLIENT_SERVICE)
  528. continue;
  529. if (nick != NULL && host != NULL) {
  530. if (strcasecmp(nick, Client_ID(cl)) == 0 &&
  531. strcasecmp(user, Client_User(cl)) == 0 &&
  532. strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
  533. break;
  534. else
  535. continue;
  536. }
  537. if (strcasecmp(user, Client_User(cl)) != 0)
  538. continue;
  539. if (host != NULL && strcasecmp(host,
  540. Client_HostnameDisplayed(cl)) != 0)
  541. continue;
  542. if (server != NULL && strcasecmp(server,
  543. Client_ID(Client_Introducer(cl))) != 0)
  544. continue;
  545. break;
  546. }
  547. }
  548. if (cl) {
  549. /* Target is a user, enforce type */
  550. #ifndef STRICT_RFC
  551. if (Client_Type(cl) != ForceType &&
  552. !(ForceType == CLIENT_USER &&
  553. (Client_Type(cl) == CLIENT_USER ||
  554. Client_Type(cl) == CLIENT_SERVICE))) {
  555. #else
  556. if (Client_Type(cl) != ForceType) {
  557. #endif
  558. if (SendErrors && !IRC_WriteErrClient(
  559. from, ERR_NOSUCHNICK_MSG,Client_ID(from),
  560. currentTarget))
  561. return DISCONNECTED;
  562. goto send_next_target;
  563. }
  564. #ifndef STRICT_RFC
  565. if (ForceType == CLIENT_SERVICE &&
  566. (Conn_Options(Client_Conn(Client_NextHop(cl)))
  567. & CONN_RFC1459)) {
  568. /* SQUERY command but RFC 1459 link: convert
  569. * request to PRIVMSG command */
  570. Req->command = "PRIVMSG";
  571. }
  572. #endif
  573. if (Client_HasMode(cl, 'b') &&
  574. !Client_HasMode(from, 'R') &&
  575. !Client_HasMode(from, 'o') &&
  576. !(Client_Type(from) == CLIENT_SERVER) &&
  577. !(Client_Type(from) == CLIENT_SERVICE)) {
  578. if (SendErrors && !IRC_WriteErrClient(from,
  579. ERR_NONONREG_MSG,
  580. Client_ID(from), Client_ID(cl)))
  581. return DISCONNECTED;
  582. goto send_next_target;
  583. }
  584. if (Client_HasMode(cl, 'C')) {
  585. cl2chan = Channel_FirstChannelOf(cl);
  586. while (cl2chan) {
  587. chan = Channel_GetChannel(cl2chan);
  588. if (Channel_IsMemberOf(chan, from))
  589. break;
  590. cl2chan = Channel_NextChannelOf(cl, cl2chan);
  591. }
  592. if (!cl2chan) {
  593. if (SendErrors && !IRC_WriteErrClient(
  594. from, ERR_NOTONSAMECHANNEL_MSG,
  595. Client_ID(from), Client_ID(cl)))
  596. return DISCONNECTED;
  597. goto send_next_target;
  598. }
  599. }
  600. if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
  601. && Client_HasMode(cl, 'a')) {
  602. /* Target is away */
  603. if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
  604. Client_ID(from),
  605. Client_ID(cl),
  606. Client_Away(cl)))
  607. return DISCONNECTED;
  608. }
  609. if (Client_Conn(from) > NONE) {
  610. Conn_UpdateIdle(Client_Conn(from));
  611. }
  612. if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
  613. Req->command, Client_ID(cl),
  614. message))
  615. return DISCONNECTED;
  616. } else if (ForceType != CLIENT_SERVICE
  617. && (chan = Channel_Search(currentTarget))) {
  618. if (!Channel_Write(chan, from, Client, Req->command,
  619. SendErrors, message))
  620. return DISCONNECTED;
  621. } else if (ForceType != CLIENT_SERVICE
  622. /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
  623. && strchr("$#", currentTarget[0])
  624. && strchr(currentTarget, '.')) {
  625. /* targetmask */
  626. if (!Send_Message_Mask(from, Req->command, currentTarget,
  627. message, SendErrors))
  628. return DISCONNECTED;
  629. } else {
  630. if (!SendErrors)
  631. return CONNECTED;
  632. if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
  633. Client_ID(from), currentTarget))
  634. return DISCONNECTED;
  635. }
  636. send_next_target:
  637. currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
  638. if (currentTarget)
  639. Conn_SetPenalty(Client_Conn(Client), 1);
  640. }
  641. return CONNECTED;
  642. } /* Send_Message */
  643. static bool
  644. Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
  645. char * message, bool SendErrors)
  646. {
  647. CLIENT *cl;
  648. bool client_match;
  649. char *mask = targetMask + 1;
  650. const char *check_wildcards;
  651. cl = NULL;
  652. if (!Client_HasMode(from, 'o')) {
  653. if (!SendErrors)
  654. return true;
  655. return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
  656. Client_ID(from));
  657. }
  658. /*
  659. * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
  660. * dot (".") and no wildcards ("*", "?") following the last one.
  661. */
  662. check_wildcards = strrchr(targetMask, '.');
  663. assert(check_wildcards != NULL);
  664. if (check_wildcards &&
  665. check_wildcards[strcspn(check_wildcards, "*?")])
  666. {
  667. if (!SendErrors)
  668. return true;
  669. return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
  670. }
  671. /* #: hostmask, see RFC 2812, sec. 3.3.1 */
  672. if (targetMask[0] == '#') {
  673. for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
  674. if (Client_Type(cl) != CLIENT_USER)
  675. continue;
  676. client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
  677. if (client_match)
  678. if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
  679. command, Client_ID(cl), message))
  680. return false;
  681. }
  682. } else {
  683. assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
  684. for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
  685. if (Client_Type(cl) != CLIENT_USER)
  686. continue;
  687. client_match = MatchCaseInsensitive(mask,
  688. Client_ID(Client_Introducer(cl)));
  689. if (client_match)
  690. if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
  691. command, Client_ID(cl), message))
  692. return false;
  693. }
  694. }
  695. return CONNECTED;
  696. } /* Send_Message_Mask */
  697. /* -eof- */