numeric.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. * Handlers for IRC numerics sent to the server
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include "conn-func.h"
  22. #include "conf.h"
  23. #include "channel.h"
  24. #include "class.h"
  25. #include "irc-write.h"
  26. #include "lists.h"
  27. #include "log.h"
  28. #include "parse.h"
  29. #include "numeric.h"
  30. /**
  31. * Announce a channel and its users in the network.
  32. */
  33. static bool
  34. Announce_Channel(CLIENT *Client, CHANNEL *Chan)
  35. {
  36. CL2CHAN *cl2chan;
  37. CLIENT *cl;
  38. char str[COMMAND_LEN], *ptr;
  39. bool njoin, xop;
  40. /* Check features of remote server */
  41. njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
  42. xop = Client_HasFlag(Client, 'X') ? true : false;
  43. /* Get all the members of this channel */
  44. cl2chan = Channel_FirstMember(Chan);
  45. snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
  46. while (cl2chan) {
  47. cl = Channel_GetClient(cl2chan);
  48. assert(cl != NULL);
  49. if (njoin) {
  50. /* RFC 2813: send NJOIN with nicknames and modes
  51. * (if user is channel operator or has voice) */
  52. if (str[strlen(str) - 1] != ':')
  53. strlcat(str, ",", sizeof(str));
  54. /* Prepare user prefix (ChanOp, voiced, ...) */
  55. if (xop && Channel_UserHasMode(Chan, cl, 'q'))
  56. strlcat(str, "~", sizeof(str));
  57. if (xop && Channel_UserHasMode(Chan, cl, 'a'))
  58. strlcat(str, "&", sizeof(str));
  59. if (Channel_UserHasMode(Chan, cl, 'o'))
  60. strlcat(str, "@", sizeof(str));
  61. if (xop && Channel_UserHasMode(Chan, cl, 'h'))
  62. strlcat(str, "%", sizeof(str));
  63. if (Channel_UserHasMode(Chan, cl, 'v'))
  64. strlcat(str, "+", sizeof(str));
  65. strlcat(str, Client_ID(cl), sizeof(str));
  66. /* Send the data if the buffer is "full" */
  67. if (strlen(str) > (sizeof(str) - CLIENT_NICK_LEN - 8)) {
  68. if (!IRC_WriteStrClient(Client, "%s", str))
  69. return DISCONNECTED;
  70. snprintf(str, sizeof(str), "NJOIN %s :",
  71. Channel_Name(Chan));
  72. }
  73. } else {
  74. /* RFC 1459: no NJOIN, send JOIN and MODE */
  75. if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
  76. Channel_Name(Chan)))
  77. return DISCONNECTED;
  78. ptr = Channel_UserModes(Chan, cl);
  79. while (*ptr) {
  80. if (!IRC_WriteStrClientPrefix(Client, cl,
  81. "MODE %s +%c %s",
  82. Channel_Name(Chan), ptr[0],
  83. Client_ID(cl)))
  84. return DISCONNECTED;
  85. ptr++;
  86. }
  87. }
  88. cl2chan = Channel_NextMember(Chan, cl2chan);
  89. }
  90. /* Data left in the buffer? */
  91. if (str[strlen(str) - 1] != ':') {
  92. /* Yes, send it ... */
  93. if (!IRC_WriteStrClient(Client, "%s", str))
  94. return DISCONNECTED;
  95. }
  96. return CONNECTED;
  97. } /* Announce_Channel */
  98. /**
  99. * Announce new server in the network
  100. * @param Client New server
  101. * @param Server Existing server in the network
  102. */
  103. static bool
  104. Announce_Server(CLIENT * Client, CLIENT * Server)
  105. {
  106. CLIENT *c;
  107. if (Client_Conn(Server) > NONE) {
  108. /* Announce the new server to the one already registered
  109. * which is directly connected to the local server */
  110. if (!IRC_WriteStrClient
  111. (Server, "SERVER %s %d %d :%s", Client_ID(Client),
  112. Client_Hops(Client) + 1, Client_MyToken(Client),
  113. Client_Info(Client)))
  114. return DISCONNECTED;
  115. }
  116. if (Client_Hops(Server) == 1)
  117. c = Client_ThisServer();
  118. else
  119. c = Client_TopServer(Server);
  120. /* Inform new server about the one already registered in the network */
  121. return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
  122. Client_ID(Server), Client_Hops(Server) + 1,
  123. Client_MyToken(Server), Client_Info(Server));
  124. } /* Announce_Server */
  125. #ifdef IRCPLUS
  126. /**
  127. * Send a specific list to a remote server.
  128. */
  129. static bool
  130. Send_List(CLIENT *Client, CHANNEL *Chan, struct list_head *Head, char Type)
  131. {
  132. struct list_elem *elem;
  133. elem = Lists_GetFirst(Head);
  134. while (elem) {
  135. if (!IRC_WriteStrClient(Client, "MODE %s +%c %s",
  136. Channel_Name(Chan), Type,
  137. Lists_GetMask(elem))) {
  138. return DISCONNECTED;
  139. }
  140. elem = Lists_GetNext(elem);
  141. }
  142. return CONNECTED;
  143. }
  144. /**
  145. * Synchronize invite, ban, except, and G-Line lists between servers.
  146. *
  147. * @param Client New server.
  148. * @return CONNECTED or DISCONNECTED.
  149. */
  150. static bool
  151. Synchronize_Lists(CLIENT * Client)
  152. {
  153. CHANNEL *c;
  154. struct list_head *head;
  155. struct list_elem *elem;
  156. assert(Client != NULL);
  157. /* g-lines */
  158. head = Class_GetList(CLASS_GLINE);
  159. elem = Lists_GetFirst(head);
  160. while (elem) {
  161. if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
  162. Lists_GetMask(elem),
  163. (long)(Lists_GetValidity(elem) - time(NULL)),
  164. Lists_GetReason(elem)))
  165. return DISCONNECTED;
  166. elem = Lists_GetNext(elem);
  167. }
  168. c = Channel_First();
  169. while (c) {
  170. if (!Send_List(Client, c, Channel_GetListExcepts(c), 'e'))
  171. return DISCONNECTED;
  172. if (!Send_List(Client, c, Channel_GetListBans(c), 'b'))
  173. return DISCONNECTED;
  174. if (!Send_List(Client, c, Channel_GetListInvites(c), 'I'))
  175. return DISCONNECTED;
  176. c = Channel_Next(c);
  177. }
  178. return CONNECTED;
  179. }
  180. /**
  181. * Send CHANINFO commands to a new server (inform it about existing channels).
  182. * @param Client New server
  183. * @param Chan Channel
  184. */
  185. static bool
  186. Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
  187. {
  188. char *modes, *topic;
  189. bool has_k, has_l;
  190. #ifdef DEBUG
  191. Log(LOG_DEBUG, "Sending CHANINFO commands for \"%s\" ...",
  192. Channel_Name(Chan));
  193. #endif
  194. modes = Channel_Modes(Chan);
  195. topic = Channel_Topic(Chan);
  196. if (!*modes && !*topic)
  197. return CONNECTED;
  198. has_k = Channel_HasMode(Chan, 'k');
  199. has_l = Channel_HasMode(Chan, 'l');
  200. /* send CHANINFO */
  201. if (!has_k && !has_l) {
  202. if (!*topic) {
  203. /* "CHANINFO <chan> +<modes>" */
  204. return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
  205. Channel_Name(Chan), modes);
  206. }
  207. /* "CHANINFO <chan> +<modes> :<topic>" */
  208. return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
  209. Channel_Name(Chan), modes, topic);
  210. }
  211. /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
  212. return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
  213. Channel_Name(Chan), modes,
  214. has_k ? Channel_Key(Chan) : "*",
  215. has_l ? Channel_MaxUsers(Chan) : 0, topic);
  216. } /* Send_CHANINFO */
  217. #endif /* IRCPLUS */
  218. /**
  219. * Handle ENDOFMOTD (376) numeric and login remote server.
  220. * The peer is either an IRC server (no IRC+ protocol), or we got the
  221. * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
  222. */
  223. GLOBAL bool
  224. IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
  225. {
  226. int max_hops, i;
  227. CLIENT *c;
  228. CHANNEL *chan;
  229. Client_SetType(Client, CLIENT_SERVER);
  230. Log(LOG_NOTICE | LOG_snotice,
  231. "Server \"%s\" registered (connection %d, 1 hop - direct link).",
  232. Client_ID(Client), Client_Conn(Client));
  233. /* Get highest hop count */
  234. max_hops = 0;
  235. c = Client_First();
  236. while (c) {
  237. if (Client_Hops(c) > max_hops)
  238. max_hops = Client_Hops(c);
  239. c = Client_Next(c);
  240. }
  241. /* Inform the new server about all other servers, and announce the
  242. * new server to all the already registered ones. Important: we have
  243. * to do this "in order" and can't introduce servers of which the
  244. * "toplevel server" isn't known already. */
  245. for (i = 0; i < (max_hops + 1); i++) {
  246. for (c = Client_First(); c != NULL; c = Client_Next(c)) {
  247. if (Client_Type(c) != CLIENT_SERVER)
  248. continue; /* not a server */
  249. if (Client_Hops(c) != i)
  250. continue; /* not actual "nesting level" */
  251. if (c == Client || c == Client_ThisServer())
  252. continue; /* that's us or the peer! */
  253. if (!Announce_Server(Client, c))
  254. return DISCONNECTED;
  255. }
  256. }
  257. /* Announce all the users to the new server */
  258. c = Client_First();
  259. while (c) {
  260. if (Client_Type(c) == CLIENT_USER ||
  261. Client_Type(c) == CLIENT_SERVICE) {
  262. if (!Client_Announce(Client, Client_ThisServer(), c))
  263. return DISCONNECTED;
  264. }
  265. c = Client_Next(c);
  266. }
  267. /* Announce all channels to the new server */
  268. chan = Channel_First();
  269. while (chan) {
  270. if (Channel_IsLocal(chan)) {
  271. chan = Channel_Next(chan);
  272. continue;
  273. }
  274. #ifdef IRCPLUS
  275. /* Send CHANINFO if the peer supports it */
  276. if (Client_HasFlag(Client, 'C')) {
  277. if (!Send_CHANINFO(Client, chan))
  278. return DISCONNECTED;
  279. }
  280. #endif
  281. if (!Announce_Channel(Client, chan))
  282. return DISCONNECTED;
  283. /* Get next channel ... */
  284. chan = Channel_Next(chan);
  285. }
  286. #ifdef IRCPLUS
  287. if (Client_HasFlag(Client, 'L')) {
  288. LogDebug("Synchronizing INVITE- and BAN-lists ...");
  289. if (!Synchronize_Lists(Client))
  290. return DISCONNECTED;
  291. }
  292. #endif
  293. if (!IRC_WriteStrClient(Client, "PING :%s",
  294. Client_ID(Client_ThisServer())))
  295. return DISCONNECTED;
  296. return CONNECTED;
  297. } /* IRC_Num_ENDOFMOTD */
  298. /**
  299. * Handle ISUPPORT (005) numeric.
  300. */
  301. GLOBAL bool
  302. IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
  303. {
  304. int i;
  305. char *key, *value;
  306. for (i = 1; i < Req->argc - 1; i++) {
  307. key = Req->argv[i];
  308. value = strchr(key, '=');
  309. if (value)
  310. *value++ = '\0';
  311. else
  312. value = "";
  313. if (strcmp("NICKLEN", key) == 0) {
  314. if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
  315. continue;
  316. /* Nickname length settings are different! */
  317. Log(LOG_ERR,
  318. "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
  319. Conf_MaxNickLength - 1, atoi(value));
  320. Conn_Close(Client_Conn(Client),
  321. "Incompatible nickname length",
  322. NULL, false);
  323. return DISCONNECTED;
  324. }
  325. }
  326. return CONNECTED;
  327. } /* IRC_Num_ISUPPORT */
  328. /* -eof- */