numeric.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 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. 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 ...");
  192. #endif
  193. modes = Channel_Modes(Chan);
  194. topic = Channel_Topic(Chan);
  195. if (!*modes && !*topic)
  196. return CONNECTED;
  197. has_k = Channel_HasMode(Chan, 'k');
  198. has_l = Channel_HasMode(Chan, 'l');
  199. /* send CHANINFO */
  200. if (!has_k && !has_l) {
  201. if (!*topic) {
  202. /* "CHANINFO <chan> +<modes>" */
  203. return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
  204. Channel_Name(Chan), modes);
  205. }
  206. /* "CHANINFO <chan> +<modes> :<topic>" */
  207. return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
  208. Channel_Name(Chan), modes, topic);
  209. }
  210. /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
  211. return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
  212. Channel_Name(Chan), modes,
  213. has_k ? Channel_Key(Chan) : "*",
  214. has_l ? Channel_MaxUsers(Chan) : 0, topic);
  215. } /* Send_CHANINFO */
  216. #endif /* IRCPLUS */
  217. /**
  218. * Handle ENDOFMOTD (376) numeric and login remote server.
  219. * The peer is either an IRC server (no IRC+ protocol), or we got the
  220. * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
  221. */
  222. GLOBAL bool
  223. IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
  224. {
  225. int max_hops, i;
  226. CLIENT *c;
  227. CHANNEL *chan;
  228. Client_SetType(Client, CLIENT_SERVER);
  229. Log(LOG_NOTICE | LOG_snotice,
  230. "Server \"%s\" registered (connection %d, 1 hop - direct link).",
  231. Client_ID(Client), Client_Conn(Client));
  232. /* Get highest hop count */
  233. max_hops = 0;
  234. c = Client_First();
  235. while (c) {
  236. if (Client_Hops(c) > max_hops)
  237. max_hops = Client_Hops(c);
  238. c = Client_Next(c);
  239. }
  240. /* Inform the new server about all other servers, and announce the
  241. * new server to all the already registered ones. Important: we have
  242. * to do this "in order" and can't introduce servers of which the
  243. * "toplevel server" isn't known already. */
  244. for (i = 0; i < (max_hops + 1); i++) {
  245. for (c = Client_First(); c != NULL; c = Client_Next(c)) {
  246. if (Client_Type(c) != CLIENT_SERVER)
  247. continue; /* not a server */
  248. if (Client_Hops(c) != i)
  249. continue; /* not actual "nesting level" */
  250. if (c == Client || c == Client_ThisServer())
  251. continue; /* that's us or the peer! */
  252. if (!Announce_Server(Client, c))
  253. return DISCONNECTED;
  254. }
  255. }
  256. /* Announce all the users to the new server */
  257. c = Client_First();
  258. while (c) {
  259. if (Client_Type(c) == CLIENT_USER ||
  260. Client_Type(c) == CLIENT_SERVICE) {
  261. if (!Client_Announce(Client, Client_ThisServer(), c))
  262. return DISCONNECTED;
  263. }
  264. c = Client_Next(c);
  265. }
  266. /* Announce all channels to the new server */
  267. chan = Channel_First();
  268. while (chan) {
  269. if (Channel_IsLocal(chan)) {
  270. chan = Channel_Next(chan);
  271. continue;
  272. }
  273. #ifdef IRCPLUS
  274. /* Send CHANINFO if the peer supports it */
  275. if (Client_HasFlag(Client, 'C')) {
  276. if (!Send_CHANINFO(Client, chan))
  277. return DISCONNECTED;
  278. }
  279. #endif
  280. if (!Announce_Channel(Client, chan))
  281. return DISCONNECTED;
  282. /* Get next channel ... */
  283. chan = Channel_Next(chan);
  284. }
  285. #ifdef IRCPLUS
  286. if (Client_HasFlag(Client, 'L')) {
  287. LogDebug("Synchronizing INVITE- and BAN-lists ...");
  288. if (!Synchronize_Lists(Client))
  289. return DISCONNECTED;
  290. }
  291. #endif
  292. if (!IRC_WriteStrClient(Client, "PING :%s",
  293. Client_ID(Client_ThisServer())))
  294. return DISCONNECTED;
  295. return CONNECTED;
  296. } /* IRC_Num_ENDOFMOTD */
  297. /**
  298. * Handle ISUPPORT (005) numeric.
  299. */
  300. GLOBAL bool
  301. IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
  302. {
  303. int i;
  304. char *key, *value;
  305. for (i = 1; i < Req->argc - 1; i++) {
  306. key = Req->argv[i];
  307. value = strchr(key, '=');
  308. if (value)
  309. *value++ = '\0';
  310. else
  311. value = "";
  312. if (strcmp("NICKLEN", key) == 0) {
  313. if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
  314. continue;
  315. /* Nickname length settings are different! */
  316. Log(LOG_ERR,
  317. "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
  318. Conf_MaxNickLength - 1, atoi(value));
  319. Conn_Close(Client_Conn(Client),
  320. "Incompatible nickname length",
  321. NULL, false);
  322. return DISCONNECTED;
  323. }
  324. }
  325. return CONNECTED;
  326. } /* IRC_Num_ISUPPORT */
  327. /* -eof- */