numeric.c 10.0 KB

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