numeric.c 10 KB

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