irc-info.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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. * IRC info commands
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <time.h>
  22. #include "ngircd.h"
  23. #include "conn-func.h"
  24. #include "conn-zip.h"
  25. #include "channel.h"
  26. #include "class.h"
  27. #include "conf.h"
  28. #include "lists.h"
  29. #include "messages.h"
  30. #include "match.h"
  31. #include "parse.h"
  32. #include "irc.h"
  33. #include "irc-macros.h"
  34. #include "irc-write.h"
  35. #include "client-cap.h"
  36. #include "op.h"
  37. #include "irc-info.h"
  38. /* Local functions */
  39. static unsigned int
  40. t_diff(time_t *t, const time_t d)
  41. {
  42. time_t diff, remain;
  43. diff = *t / d;
  44. remain = diff * d;
  45. *t -= remain;
  46. return (unsigned int)diff;
  47. }
  48. static unsigned int
  49. uptime_days(time_t *now)
  50. {
  51. return t_diff(now, 60 * 60 * 24);
  52. }
  53. static unsigned int
  54. uptime_hrs(time_t *now)
  55. {
  56. return t_diff(now, 60 * 60);
  57. }
  58. static unsigned int
  59. uptime_mins(time_t *now)
  60. {
  61. return t_diff(now, 60);
  62. }
  63. static bool
  64. write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
  65. {
  66. return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
  67. channelname, Client_User(c),
  68. Client_HostnameDisplayed(c),
  69. Client_ID(Client_Introducer(c)), Client_ID(c),
  70. flags, Client_Hops(c), Client_Info(c));
  71. }
  72. /**
  73. * Return channel user mode prefix(es).
  74. *
  75. * @param Client The client requesting the mode prefixes.
  76. * @param chan_user_modes String with channel user modes.
  77. * @param str String buffer to which the prefix(es) will be appended.
  78. * @param len Size of "str" buffer.
  79. * @return Pointer to "str".
  80. */
  81. static char *
  82. who_flags_qualifier(CLIENT *Client, const char *chan_user_modes,
  83. char *str, size_t len)
  84. {
  85. assert(Client != NULL);
  86. if (Client_Cap(Client) & CLIENT_CAP_MULTI_PREFIX) {
  87. if (strchr(chan_user_modes, 'q'))
  88. strlcat(str, "~", len);
  89. if (strchr(chan_user_modes, 'a'))
  90. strlcat(str, "&", len);
  91. if (strchr(chan_user_modes, 'o'))
  92. strlcat(str, "@", len);
  93. if (strchr(chan_user_modes, 'h'))
  94. strlcat(str, "%", len);
  95. if (strchr(chan_user_modes, 'v'))
  96. strlcat(str, "+", len);
  97. return str;
  98. }
  99. if (strchr(chan_user_modes, 'q'))
  100. strlcat(str, "~", len);
  101. else if (strchr(chan_user_modes, 'a'))
  102. strlcat(str, "&", len);
  103. else if (strchr(chan_user_modes, 'o'))
  104. strlcat(str, "@", len);
  105. else if (strchr(chan_user_modes, 'h'))
  106. strlcat(str, "%", len);
  107. else if (strchr(chan_user_modes, 'v'))
  108. strlcat(str, "+", len);
  109. return str;
  110. }
  111. /**
  112. * Send WHO reply for a "channel target" ("WHO #channel").
  113. *
  114. * @param Client Client requesting the information.
  115. * @param Chan Channel being requested.
  116. * @param OnlyOps Only display IRC operators.
  117. * @return CONNECTED or DISCONNECTED.
  118. */
  119. static bool
  120. IRC_WHO_Channel(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
  121. {
  122. bool is_visible, is_member, is_ircop;
  123. CL2CHAN *cl2chan;
  124. char flags[10];
  125. CLIENT *c;
  126. int count = 0;
  127. assert( Client != NULL );
  128. assert( Chan != NULL );
  129. is_member = Channel_IsMemberOf(Chan, Client);
  130. /* Secret channel? */
  131. if (!is_member && Channel_HasMode(Chan, 's'))
  132. return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG,
  133. Client_ID(Client), Channel_Name(Chan));
  134. cl2chan = Channel_FirstMember(Chan);
  135. for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
  136. c = Channel_GetClient(cl2chan);
  137. is_ircop = Client_HasMode(c, 'o');
  138. if (OnlyOps && !is_ircop)
  139. continue;
  140. is_visible = !Client_HasMode(c, 'i');
  141. if (is_member || is_visible) {
  142. memset(flags, 0, sizeof(flags));
  143. if (Client_HasMode(c, 'a'))
  144. flags[0] = 'G'; /* away */
  145. else
  146. flags[0] = 'H';
  147. if (is_ircop)
  148. flags[1] = '*';
  149. who_flags_qualifier(Client, Channel_UserModes(Chan, c),
  150. flags, sizeof(flags));
  151. if (!write_whoreply(Client, c, Channel_Name(Chan),
  152. flags))
  153. return DISCONNECTED;
  154. count++;
  155. }
  156. }
  157. /* If there are a lot of clients, increase the penalty a bit */
  158. if (count > MAX_RPL_WHO)
  159. IRC_SetPenalty(Client, 1);
  160. return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
  161. Channel_Name(Chan));
  162. }
  163. /**
  164. * Send WHO reply for a "mask target" ("WHO m*sk").
  165. *
  166. * @param Client Client requesting the information.
  167. * @param Mask Mask being requested or NULL for "all" clients.
  168. * @param OnlyOps Only display IRC operators.
  169. * @return CONNECTED or DISCONNECTED.
  170. */
  171. static bool
  172. IRC_WHO_Mask(CLIENT *Client, char *Mask, bool OnlyOps)
  173. {
  174. CLIENT *c;
  175. CL2CHAN *cl2chan;
  176. CHANNEL *chan;
  177. bool client_match, is_visible;
  178. char flags[3];
  179. int count = 0;
  180. assert (Client != NULL);
  181. if (Mask)
  182. ngt_LowerStr(Mask);
  183. IRC_SetPenalty(Client, 3);
  184. for (c = Client_First(); c != NULL; c = Client_Next(c)) {
  185. if (Client_Type(c) != CLIENT_USER)
  186. continue;
  187. if (OnlyOps && !Client_HasMode(c, 'o'))
  188. continue;
  189. if (Mask) {
  190. /* Match pattern against user host/server/name/nick */
  191. client_match = MatchCaseInsensitive(Mask,
  192. Client_Hostname(c));
  193. if (!client_match)
  194. client_match = MatchCaseInsensitive(Mask,
  195. Client_ID(Client_Introducer(c)));
  196. if (!client_match)
  197. client_match = MatchCaseInsensitive(Mask,
  198. Client_Info(c));
  199. if (!client_match)
  200. client_match = MatchCaseInsensitive(Mask,
  201. Client_ID(c));
  202. if (!client_match)
  203. continue; /* no match: skip this client */
  204. }
  205. is_visible = !Client_HasMode(c, 'i');
  206. /* Target client is invisible, but mask matches exactly? */
  207. if (!is_visible && Mask && strcasecmp(Client_ID(c), Mask) == 0)
  208. is_visible = true;
  209. /* Target still invisible, but are both on the same channel? */
  210. if (!is_visible) {
  211. cl2chan = Channel_FirstChannelOf(Client);
  212. while (cl2chan && !is_visible) {
  213. chan = Channel_GetChannel(cl2chan);
  214. if (Channel_IsMemberOf(chan, c))
  215. is_visible = true;
  216. cl2chan = Channel_NextChannelOf(Client, cl2chan);
  217. }
  218. }
  219. if (!is_visible) /* target user is not visible */
  220. continue;
  221. if (IRC_CheckListTooBig(Client, count, MAX_RPL_WHO, "WHO"))
  222. break;
  223. memset(flags, 0, sizeof(flags));
  224. if (Client_HasMode(c, 'a'))
  225. flags[0] = 'G'; /* away */
  226. else
  227. flags[0] = 'H';
  228. if (Client_HasMode(c, 'o'))
  229. flags[1] = '*';
  230. if (!write_whoreply(Client, c, "*", flags))
  231. return DISCONNECTED;
  232. count++;
  233. }
  234. return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client),
  235. Mask ? Mask : "*");
  236. }
  237. /**
  238. * Generate WHOIS reply of one actual client.
  239. *
  240. * @param Client The client from which this command has been received.
  241. * @param from The client requesting the information ("originator").
  242. * @param c The client of which information should be returned.
  243. * @return CONNECTED or DISCONNECTED.
  244. */
  245. static bool
  246. IRC_WHOIS_SendReply(CLIENT *Client, CLIENT *from, CLIENT *c)
  247. {
  248. char str[COMMAND_LEN];
  249. CL2CHAN *cl2chan;
  250. CHANNEL *chan;
  251. assert(Client != NULL);
  252. assert(from != NULL);
  253. assert(c != NULL);
  254. /* Nick, user, hostname and client info */
  255. if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
  256. Client_ID(c), Client_User(c),
  257. Client_HostnameDisplayed(c), Client_Info(c)))
  258. return DISCONNECTED;
  259. /* Server */
  260. if (!IRC_WriteStrClient(from, RPL_WHOISSERVER_MSG, Client_ID(from),
  261. Client_ID(c), Client_ID(Client_Introducer(c)),
  262. Client_Info(Client_Introducer(c))))
  263. return DISCONNECTED;
  264. /* Channels */
  265. snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
  266. Client_ID(from), Client_ID(c));
  267. cl2chan = Channel_FirstChannelOf(c);
  268. while (cl2chan) {
  269. chan = Channel_GetChannel(cl2chan);
  270. assert(chan != NULL);
  271. /* next */
  272. cl2chan = Channel_NextChannelOf(c, cl2chan);
  273. /* Secret channel? */
  274. if (Channel_HasMode(chan, 's')
  275. && !Channel_IsMemberOf(chan, Client))
  276. continue;
  277. /* Local channel and request is not from a user? */
  278. if (Client_Type(Client) == CLIENT_SERVER
  279. && Channel_IsLocal(chan))
  280. continue;
  281. /* Concatenate channel names */
  282. if (str[strlen(str) - 1] != ':')
  283. strlcat(str, " ", sizeof(str));
  284. who_flags_qualifier(Client, Channel_UserModes(chan, c),
  285. str, sizeof(str));
  286. strlcat(str, Channel_Name(chan), sizeof(str));
  287. if (strlen(str) > (COMMAND_LEN - CHANNEL_NAME_LEN - 4)) {
  288. /* Line becomes too long: send it! */
  289. if (!IRC_WriteStrClient(Client, "%s", str))
  290. return DISCONNECTED;
  291. snprintf(str, sizeof(str), RPL_WHOISCHANNELS_MSG,
  292. Client_ID(from), Client_ID(c));
  293. }
  294. }
  295. if(str[strlen(str) - 1] != ':') {
  296. /* There is data left to send: */
  297. if (!IRC_WriteStrClient(Client, "%s", str))
  298. return DISCONNECTED;
  299. }
  300. /* IRC-Services? */
  301. if (Client_Type(c) == CLIENT_SERVICE &&
  302. !IRC_WriteStrClient(from, RPL_WHOISSERVICE_MSG,
  303. Client_ID(from), Client_ID(c)))
  304. return DISCONNECTED;
  305. /* IRC-Operator? */
  306. if (Client_Type(c) != CLIENT_SERVICE &&
  307. Client_HasMode(c, 'o') &&
  308. !IRC_WriteStrClient(from, RPL_WHOISOPERATOR_MSG,
  309. Client_ID(from), Client_ID(c)))
  310. return DISCONNECTED;
  311. /* IRC-Bot? */
  312. if (Client_HasMode(c, 'B') &&
  313. !IRC_WriteStrClient(from, RPL_WHOISBOT_MSG,
  314. Client_ID(from), Client_ID(c)))
  315. return DISCONNECTED;
  316. /* Connected using SSL? */
  317. if (Conn_UsesSSL(Client_Conn(c))) {
  318. if (!IRC_WriteStrClient(from, RPL_WHOISSSL_MSG, Client_ID(from),
  319. Client_ID(c)))
  320. return DISCONNECTED;
  321. /* Certificate fingerprint? */
  322. if (Conn_GetCertFp(Client_Conn(c)) &&
  323. from == c &&
  324. !IRC_WriteStrClient(from, RPL_WHOISCERTFP_MSG,
  325. Client_ID(from), Client_ID(c),
  326. Conn_GetCertFp(Client_Conn(c))))
  327. return DISCONNECTED;
  328. }
  329. /* Registered nickname? */
  330. if (Client_HasMode(c, 'R') &&
  331. !IRC_WriteStrClient(from, RPL_WHOISREGNICK_MSG,
  332. Client_ID(from), Client_ID(c)))
  333. return DISCONNECTED;
  334. /* Account name metadata? */
  335. if (Client_AccountName(c) &&
  336. !IRC_WriteStrClient(from, RPL_WHOISLOGGEDIN_MSG,
  337. Client_ID(from), Client_ID(c),
  338. Client_AccountName(c)))
  339. return DISCONNECTED;
  340. /* Local client and requester is the user itself or an IRC Op? */
  341. if (Client_Conn(c) > NONE &&
  342. (from == c || (!Conf_MorePrivacy && Client_HasMode(from, 'o')))) {
  343. /* Client hostname */
  344. if (!IRC_WriteStrClient(from, RPL_WHOISHOST_MSG,
  345. Client_ID(from), Client_ID(c),
  346. Client_Hostname(c), Client_IPAText(c)))
  347. return DISCONNECTED;
  348. /* Client modes */
  349. if (!IRC_WriteStrClient(from, RPL_WHOISMODES_MSG,
  350. Client_ID(from), Client_ID(c), Client_Modes(c)))
  351. return DISCONNECTED;
  352. }
  353. /* Idle and signon time (local clients only!) */
  354. if (!Conf_MorePrivacy && Client_Conn(c) > NONE &&
  355. !IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
  356. Client_ID(from), Client_ID(c),
  357. (unsigned long)Conn_GetIdle(Client_Conn(c)),
  358. (unsigned long)Conn_GetSignon(Client_Conn(c))))
  359. return DISCONNECTED;
  360. /* Away? */
  361. if (Client_HasMode(c, 'a') &&
  362. !IRC_WriteStrClient(from, RPL_AWAY_MSG,
  363. Client_ID(from), Client_ID(c), Client_Away(c)))
  364. return DISCONNECTED;
  365. return CONNECTED;
  366. }
  367. static bool
  368. WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
  369. {
  370. char t_str[60];
  371. (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
  372. localtime(&entry->time));
  373. if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
  374. entry->id, entry->user, entry->host, entry->info))
  375. return DISCONNECTED;
  376. return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
  377. entry->id, entry->server, t_str);
  378. }
  379. #ifdef SSL_SUPPORT
  380. static bool
  381. Show_MOTD_SSLInfo(CLIENT *Client)
  382. {
  383. char buf[COMMAND_LEN];
  384. char c_str[128];
  385. if (Conn_GetCipherInfo(Client_Conn(Client), c_str, sizeof(c_str))) {
  386. snprintf(buf, sizeof(buf), "Connected using Cipher %s", c_str);
  387. if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
  388. Client_ID(Client), buf))
  389. return false;
  390. }
  391. if (Conn_GetCertFp(Client_Conn(Client))) {
  392. snprintf(buf, sizeof(buf),
  393. "Your client certificate fingerprint is: %s",
  394. Conn_GetCertFp(Client_Conn(Client)));
  395. if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG,
  396. Client_ID(Client), buf))
  397. return false;
  398. }
  399. return true;
  400. }
  401. #else
  402. static bool
  403. Show_MOTD_SSLInfo(UNUSED CLIENT *c)
  404. {
  405. return true;
  406. }
  407. #endif
  408. /* Global functions */
  409. /**
  410. * Handler for the IRC command "ADMIN".
  411. *
  412. * @param Client The client from which this command has been received.
  413. * @param Req Request structure with prefix and all parameters.
  414. * @return CONNECTED or DISCONNECTED.
  415. */
  416. GLOBAL bool
  417. IRC_ADMIN(CLIENT *Client, REQUEST *Req )
  418. {
  419. CLIENT *target, *prefix;
  420. assert( Client != NULL );
  421. assert( Req != NULL );
  422. _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
  423. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
  424. /* Forward? */
  425. if(target != Client_ThisServer()) {
  426. IRC_WriteStrClientPrefix(target, prefix,
  427. "ADMIN %s", Client_ID(target));
  428. return CONNECTED;
  429. }
  430. if (!IRC_WriteStrClient(Client, RPL_ADMINME_MSG, Client_ID(prefix),
  431. Conf_ServerName))
  432. return DISCONNECTED;
  433. if (!IRC_WriteStrClient(Client, RPL_ADMINLOC1_MSG, Client_ID(prefix),
  434. Conf_ServerAdmin1))
  435. return DISCONNECTED;
  436. if (!IRC_WriteStrClient(Client, RPL_ADMINLOC2_MSG, Client_ID(prefix),
  437. Conf_ServerAdmin2))
  438. return DISCONNECTED;
  439. if (!IRC_WriteStrClient(Client, RPL_ADMINEMAIL_MSG, Client_ID(prefix),
  440. Conf_ServerAdminMail))
  441. return DISCONNECTED;
  442. return CONNECTED;
  443. } /* IRC_ADMIN */
  444. /**
  445. * Handler for the IRC command "INFO".
  446. *
  447. * @param Client The client from which this command has been received.
  448. * @param Req Request structure with prefix and all parameters.
  449. * @return CONNECTED or DISCONNECTED.
  450. */
  451. GLOBAL bool
  452. IRC_INFO(CLIENT * Client, REQUEST * Req)
  453. {
  454. CLIENT *target, *prefix;
  455. char msg[510];
  456. assert(Client != NULL);
  457. assert(Req != NULL);
  458. _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
  459. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
  460. /* Forward? */
  461. if (target != Client_ThisServer()) {
  462. IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
  463. Client_ID(target));
  464. return CONNECTED;
  465. }
  466. if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
  467. NGIRCd_Version))
  468. return DISCONNECTED;
  469. #if defined(__DATE__) && defined(__TIME__)
  470. snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
  471. if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
  472. return DISCONNECTED;
  473. #endif
  474. strlcpy(msg, "On-line since ", sizeof(msg));
  475. strlcat(msg, NGIRCd_StartStr, sizeof(msg));
  476. if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
  477. return DISCONNECTED;
  478. if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
  479. return DISCONNECTED;
  480. return CONNECTED;
  481. } /* IRC_INFO */
  482. /**
  483. * Handler for the IRC "ISON" command.
  484. *
  485. * @param Client The client from which this command has been received.
  486. * @param Req Request structure with prefix and all parameters.
  487. * @return CONNECTED or DISCONNECTED.
  488. */
  489. GLOBAL bool
  490. IRC_ISON( CLIENT *Client, REQUEST *Req )
  491. {
  492. char rpl[COMMAND_LEN];
  493. CLIENT *c;
  494. char *ptr;
  495. int i;
  496. assert(Client != NULL);
  497. assert(Req != NULL);
  498. strlcpy(rpl, RPL_ISON_MSG, sizeof rpl);
  499. for (i = 0; i < Req->argc; i++) {
  500. /* "All" ircd even parse ":<x> <y> ..." arguments and split
  501. * them up; so we do the same ... */
  502. ptr = strtok(Req->argv[i], " ");
  503. while (ptr) {
  504. ngt_TrimStr(ptr);
  505. c = Client_Search(ptr);
  506. if (c && Client_Type(c) == CLIENT_USER) {
  507. strlcat(rpl, Client_ID(c), sizeof(rpl));
  508. strlcat(rpl, " ", sizeof(rpl));
  509. }
  510. ptr = strtok(NULL, " ");
  511. }
  512. }
  513. ngt_TrimLastChr(rpl, ' ');
  514. return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
  515. } /* IRC_ISON */
  516. /**
  517. * Handler for the IRC "LINKS" command.
  518. *
  519. * @param Client The client from which this command has been received.
  520. * @param Req Request structure with prefix and all parameters.
  521. * @return CONNECTED or DISCONNECTED.
  522. */
  523. GLOBAL bool
  524. IRC_LINKS(CLIENT *Client, REQUEST *Req)
  525. {
  526. CLIENT *target, *from, *c;
  527. char *mask;
  528. assert(Client != NULL);
  529. assert(Req != NULL);
  530. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  531. /* Get pointer to server mask or "*", if none given */
  532. if (Req->argc > 0)
  533. mask = Req->argv[Req->argc - 1];
  534. else
  535. mask = "*";
  536. /* Forward? */
  537. if (Req->argc == 2) {
  538. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
  539. if (target != Client_ThisServer()) {
  540. IRC_WriteStrClientPrefix(target, from,
  541. "LINKS %s %s", Client_ID(target),
  542. Req->argv[1]);
  543. return CONNECTED;
  544. }
  545. }
  546. c = Client_First();
  547. while (c) {
  548. if (Client_Type(c) == CLIENT_SERVER
  549. && MatchCaseInsensitive(mask, Client_ID(c))) {
  550. if (!IRC_WriteStrClient(from, RPL_LINKS_MSG,
  551. Client_ID(from), Client_ID(c),
  552. Client_ID(Client_TopServer(c)
  553. ? Client_TopServer(c)
  554. : Client_ThisServer()),
  555. Client_Hops(c), Client_Info(c)))
  556. return DISCONNECTED;
  557. }
  558. c = Client_Next(c);
  559. }
  560. return IRC_WriteStrClient(from, RPL_ENDOFLINKS_MSG,
  561. Client_ID(from), mask);
  562. } /* IRC_LINKS */
  563. /**
  564. * Handler for the IRC "LUSERS" command.
  565. *
  566. * @param Client The client from which this command has been received.
  567. * @param Req Request structure with prefix and all parameters.
  568. * @return CONNECTED or DISCONNECTED.
  569. */
  570. GLOBAL bool
  571. IRC_LUSERS( CLIENT *Client, REQUEST *Req )
  572. {
  573. CLIENT *target, *from;
  574. assert( Client != NULL );
  575. assert( Req != NULL );
  576. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  577. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
  578. /* Forward? */
  579. if (target != Client_ThisServer()) {
  580. IRC_WriteStrClientPrefix(target, from,
  581. "LUSERS %s %s", Req->argv[0],
  582. Client_ID(target));
  583. return CONNECTED;
  584. }
  585. return IRC_Send_LUSERS(from);
  586. } /* IRC_LUSERS */
  587. /**
  588. * Handler for the IRC command "SERVLIST".
  589. *
  590. * @param Client The client from which this command has been received.
  591. * @param Req Request structure with prefix and all parameters.
  592. * @return CONNECTED or DISCONNECTED.
  593. */
  594. GLOBAL bool
  595. IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
  596. {
  597. CLIENT *c;
  598. assert(Client != NULL);
  599. assert(Req != NULL);
  600. if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
  601. for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
  602. if (Client_Type(c) != CLIENT_SERVICE)
  603. continue;
  604. if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
  605. Client_ID(c)))
  606. continue;
  607. if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
  608. Client_ID(Client), Client_Mask(c),
  609. Client_Mask(Client_Introducer(c)), "*",
  610. 0, Client_Hops(c), Client_Info(c)))
  611. return DISCONNECTED;
  612. }
  613. }
  614. return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
  615. Req->argc > 0 ? Req->argv[0] : "*",
  616. Req->argc > 1 ? Req->argv[1] : "0");
  617. } /* IRC_SERVLIST */
  618. /**
  619. * Handler for the IRC command "MOTD".
  620. *
  621. * @param Client The client from which this command has been received.
  622. * @param Req Request structure with prefix and all parameters.
  623. * @return CONNECTED or DISCONNECTED.
  624. */
  625. GLOBAL bool
  626. IRC_MOTD( CLIENT *Client, REQUEST *Req )
  627. {
  628. CLIENT *from, *target;
  629. assert( Client != NULL );
  630. assert( Req != NULL );
  631. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  632. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
  633. /* Forward? */
  634. if (target != Client_ThisServer()) {
  635. IRC_WriteStrClientPrefix(target, from, "MOTD %s",
  636. Client_ID(target));
  637. return CONNECTED;
  638. }
  639. return IRC_Show_MOTD(from);
  640. } /* IRC_MOTD */
  641. /**
  642. * Handler for the IRC command "NAMES".
  643. *
  644. * @param Client The client from which this command has been received.
  645. * @param Req Request structure with prefix and all parameters.
  646. * @return CONNECTED or DISCONNECTED.
  647. */
  648. GLOBAL bool
  649. IRC_NAMES( CLIENT *Client, REQUEST *Req )
  650. {
  651. char rpl[COMMAND_LEN], *ptr;
  652. CLIENT *target, *from, *c;
  653. CHANNEL *chan;
  654. assert( Client != NULL );
  655. assert( Req != NULL );
  656. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  657. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
  658. /* Forward? */
  659. if (target != Client_ThisServer()) {
  660. IRC_WriteStrClientPrefix(target, from, "NAMES %s :%s",
  661. Req->argv[0], Client_ID(target));
  662. return CONNECTED;
  663. }
  664. if (Req->argc > 0) {
  665. /* Return NAMES list for specific channels */
  666. ptr = strtok(Req->argv[0], ",");
  667. while(ptr) {
  668. chan = Channel_Search(ptr);
  669. if (chan && !IRC_Send_NAMES(from, chan))
  670. return DISCONNECTED;
  671. if (!IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG,
  672. Client_ID(from), ptr))
  673. return DISCONNECTED;
  674. ptr = strtok( NULL, "," );
  675. }
  676. return CONNECTED;
  677. }
  678. chan = Channel_First();
  679. while (chan) {
  680. if (!IRC_Send_NAMES(from, chan))
  681. return DISCONNECTED;
  682. chan = Channel_Next(chan);
  683. }
  684. /* Now print all clients which are not in any channel */
  685. c = Client_First();
  686. snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG, Client_ID(from), "*", "*");
  687. while (c) {
  688. if (Client_Type(c) == CLIENT_USER
  689. && Channel_FirstChannelOf(c) == NULL
  690. && !Client_HasMode(c, 'i'))
  691. {
  692. /* its a user, concatenate ... */
  693. if (rpl[strlen(rpl) - 1] != ':')
  694. strlcat(rpl, " ", sizeof(rpl));
  695. strlcat(rpl, Client_ID(c), sizeof(rpl));
  696. if (strlen(rpl) > COMMAND_LEN - CLIENT_NICK_LEN - 4) {
  697. /* Line is gwoing too long, send now */
  698. if (!IRC_WriteStrClient(from, "%s", rpl))
  699. return DISCONNECTED;
  700. snprintf(rpl, sizeof(rpl), RPL_NAMREPLY_MSG,
  701. Client_ID(from), "*", "*");
  702. }
  703. }
  704. c = Client_Next(c);
  705. }
  706. if (rpl[strlen(rpl) - 1] != ':' && !IRC_WriteStrClient(from, "%s", rpl))
  707. return DISCONNECTED;
  708. return IRC_WriteStrClient(from, RPL_ENDOFNAMES_MSG, Client_ID(from), "*");
  709. } /* IRC_NAMES */
  710. /**
  711. * Handler for the IRC command "STATS".
  712. *
  713. * @param Client The client from which this command has been received.
  714. * @param Req Request structure with prefix and all parameters.
  715. * @return CONNECTED or DISCONNECTED.
  716. */
  717. GLOBAL bool
  718. IRC_STATS( CLIENT *Client, REQUEST *Req )
  719. {
  720. CLIENT *from, *target, *cl;
  721. CONN_ID con;
  722. char query;
  723. COMMAND *cmd;
  724. time_t time_now;
  725. unsigned int days, hrs, mins;
  726. struct list_head *list;
  727. struct list_elem *list_item;
  728. bool more_links = false;
  729. assert(Client != NULL);
  730. assert(Req != NULL);
  731. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  732. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 1, from)
  733. /* Forward? */
  734. if (target != Client_ThisServer()) {
  735. IRC_WriteStrClientPrefix(target, from, "STATS %s %s",
  736. Req->argv[0], Client_ID(target));
  737. return CONNECTED;
  738. }
  739. if (Req->argc > 0)
  740. query = Req->argv[0][0] ? Req->argv[0][0] : '*';
  741. else
  742. query = '*';
  743. switch (query) {
  744. case 'g': /* Network-wide bans ("G-Lines") */
  745. case 'G':
  746. case 'k': /* Server-local bans ("K-Lines") */
  747. case 'K':
  748. if (!Client_HasMode(from, 'o'))
  749. return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
  750. Client_ID(from));
  751. if (query == 'g' || query == 'G')
  752. list = Class_GetList(CLASS_GLINE);
  753. else
  754. list = Class_GetList(CLASS_KLINE);
  755. list_item = Lists_GetFirst(list);
  756. while (list_item) {
  757. if (!IRC_WriteStrClient(from, RPL_STATSXLINE_MSG,
  758. Client_ID(from), query,
  759. Lists_GetMask(list_item),
  760. Lists_GetValidity(list_item),
  761. Lists_GetReason(list_item)))
  762. return DISCONNECTED;
  763. list_item = Lists_GetNext(list_item);
  764. }
  765. break;
  766. case 'L': /* Link status (servers and user links) */
  767. if (!Op_Check(from, Req))
  768. return Op_NoPrivileges(from, Req);
  769. more_links = true;
  770. case 'l': /* Link status (servers and own link) */
  771. time_now = time(NULL);
  772. for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
  773. cl = Conn_GetClient(con);
  774. if (!cl)
  775. continue;
  776. if (Client_Type(cl) == CLIENT_SERVER ||
  777. cl == Client ||
  778. (more_links && Client_Type(cl) == CLIENT_USER)) {
  779. #ifdef ZLIB
  780. if (Conn_Options(con) & CONN_ZIP) {
  781. if (!IRC_WriteStrClient
  782. (from, RPL_STATSLINKINFOZIP_MSG,
  783. Client_ID(from), Client_Mask(cl),
  784. Conn_SendQ(con), Conn_SendMsg(con),
  785. Zip_SendBytes(con),
  786. Conn_SendBytes(con),
  787. Conn_RecvMsg(con),
  788. Zip_RecvBytes(con),
  789. Conn_RecvBytes(con),
  790. (long)(time_now - Conn_StartTime(con))))
  791. return DISCONNECTED;
  792. continue;
  793. }
  794. #endif
  795. if (!IRC_WriteStrClient
  796. (from, RPL_STATSLINKINFO_MSG,
  797. Client_ID(from), Client_Mask(cl),
  798. Conn_SendQ(con), Conn_SendMsg(con),
  799. Conn_SendBytes(con), Conn_RecvMsg(con),
  800. Conn_RecvBytes(con),
  801. (long)(time_now - Conn_StartTime(con))))
  802. return DISCONNECTED;
  803. }
  804. }
  805. break;
  806. case 'm': /* IRC command status (usage count) */
  807. case 'M':
  808. cmd = Parse_GetCommandStruct();
  809. for (; cmd->name; cmd++) {
  810. if (cmd->lcount == 0 && cmd->rcount == 0)
  811. continue;
  812. if (!IRC_WriteStrClient
  813. (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
  814. cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
  815. return DISCONNECTED;
  816. }
  817. break;
  818. case 'u': /* Server uptime */
  819. case 'U':
  820. time_now = time(NULL) - NGIRCd_Start;
  821. days = uptime_days(&time_now);
  822. hrs = uptime_hrs(&time_now);
  823. mins = uptime_mins(&time_now);
  824. if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
  825. days, hrs, mins, (unsigned int)time_now))
  826. return DISCONNECTED;
  827. break;
  828. }
  829. return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
  830. Client_ID(from), query);
  831. } /* IRC_STATS */
  832. /**
  833. * Handler for the IRC command "SUMMON".
  834. *
  835. * @param Client The client from which this command has been received.
  836. * @param Req Request structure with prefix and all parameters.
  837. * @return CONNECTED or DISCONNECTED.
  838. */
  839. GLOBAL bool
  840. IRC_SUMMON(CLIENT * Client, UNUSED REQUEST * Req)
  841. {
  842. assert(Client != NULL);
  843. return IRC_WriteErrClient(Client, ERR_SUMMONDISABLED_MSG,
  844. Client_ID(Client));
  845. } /* IRC_SUMMON */
  846. /**
  847. * Handler for the IRC command "TIME".
  848. *
  849. * @param Client The client from which this command has been received.
  850. * @param Req Request structure with prefix and all parameters.
  851. * @return CONNECTED or DISCONNECTED.
  852. */
  853. GLOBAL bool
  854. IRC_TIME( CLIENT *Client, REQUEST *Req )
  855. {
  856. CLIENT *from, *target;
  857. char t_str[64];
  858. time_t t;
  859. assert(Client != NULL);
  860. assert(Req != NULL);
  861. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  862. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
  863. /* Forward? */
  864. if (target != Client_ThisServer()) {
  865. IRC_WriteStrClientPrefix(target, from, "TIME %s",
  866. Client_ID(target));
  867. return CONNECTED;
  868. }
  869. t = time( NULL );
  870. (void)strftime(t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime(&t));
  871. return IRC_WriteStrClient(from, RPL_TIME_MSG, Client_ID(from),
  872. Client_ID(Client_ThisServer()), t_str);
  873. } /* IRC_TIME */
  874. /**
  875. * Handler for the IRC command "USERHOST".
  876. *
  877. * @param Client The client from which this command has been received.
  878. * @param Req Request structure with prefix and all parameters.
  879. * @return CONNECTED or DISCONNECTED.
  880. */
  881. GLOBAL bool
  882. IRC_USERHOST(CLIENT *Client, REQUEST *Req)
  883. {
  884. char rpl[COMMAND_LEN];
  885. CLIENT *c;
  886. int max, i;
  887. assert(Client != NULL);
  888. assert(Req != NULL);
  889. if (Req->argc > 5)
  890. max = 5;
  891. else
  892. max = Req->argc;
  893. strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
  894. for (i = 0; i < max; i++) {
  895. c = Client_Search(Req->argv[i]);
  896. if (c && (Client_Type(c) == CLIENT_USER)) {
  897. /* This Nick is "online" */
  898. strlcat(rpl, Client_ID(c), sizeof(rpl));
  899. if (Client_HasMode(c, 'o'))
  900. strlcat(rpl, "*", sizeof(rpl));
  901. strlcat(rpl, "=", sizeof(rpl));
  902. if (Client_HasMode(c, 'a'))
  903. strlcat(rpl, "-", sizeof(rpl));
  904. else
  905. strlcat(rpl, "+", sizeof(rpl));
  906. strlcat(rpl, Client_User(c), sizeof(rpl));
  907. strlcat(rpl, "@", sizeof(rpl));
  908. strlcat(rpl, Client_HostnameDisplayed(c), sizeof(rpl));
  909. strlcat(rpl, " ", sizeof(rpl));
  910. }
  911. }
  912. ngt_TrimLastChr(rpl, ' ');
  913. return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
  914. } /* IRC_USERHOST */
  915. /**
  916. * Handler for the IRC command "USERS".
  917. *
  918. * @param Client The client from which this command has been received.
  919. * @param Req Request structure with prefix and all parameters.
  920. * @return CONNECTED or DISCONNECTED.
  921. */
  922. GLOBAL bool
  923. IRC_USERS(CLIENT * Client, UNUSED REQUEST * Req)
  924. {
  925. assert(Client != NULL);
  926. return IRC_WriteErrClient(Client, ERR_USERSDISABLED_MSG,
  927. Client_ID(Client));
  928. } /* IRC_USERS */
  929. /**
  930. * Handler for the IRC command "VERSION".
  931. *
  932. * @param Client The client from which this command has been received.
  933. * @param Req Request structure with prefix and all parameters.
  934. * @return CONNECTED or DISCONNECTED.
  935. */
  936. GLOBAL bool
  937. IRC_VERSION( CLIENT *Client, REQUEST *Req )
  938. {
  939. CLIENT *target, *prefix;
  940. assert( Client != NULL );
  941. assert( Req != NULL );
  942. _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
  943. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, prefix)
  944. /* Forward? */
  945. if (target != Client_ThisServer()) {
  946. IRC_WriteStrClientPrefix(target, prefix, "VERSION %s",
  947. Client_ID(target));
  948. return CONNECTED;
  949. }
  950. /* send version information */
  951. if (!IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
  952. PACKAGE_NAME, PACKAGE_VERSION,
  953. NGIRCd_DebugLevel, Conf_ServerName,
  954. NGIRCd_VersionAddition))
  955. return DISCONNECTED;
  956. #ifndef STRICT_RFC
  957. /* send RPL_ISUPPORT(005) numerics */
  958. if (!IRC_Send_ISUPPORT(prefix))
  959. return DISCONNECTED;
  960. #endif
  961. return CONNECTED;
  962. } /* IRC_VERSION */
  963. /**
  964. * Handler for the IRC "WHO" command.
  965. *
  966. * @param Client The client from which this command has been received.
  967. * @param Req Request structure with prefix and all parameters.
  968. * @return CONNECTED or DISCONNECTED.
  969. */
  970. GLOBAL bool
  971. IRC_WHO(CLIENT *Client, REQUEST *Req)
  972. {
  973. bool only_ops;
  974. CHANNEL *chan;
  975. assert (Client != NULL);
  976. assert (Req != NULL);
  977. only_ops = false;
  978. if (Req->argc == 2) {
  979. if (strcmp(Req->argv[1], "o") == 0)
  980. only_ops = true;
  981. #ifdef STRICT_RFC
  982. else {
  983. return IRC_WriteErrClient(Client,
  984. ERR_NEEDMOREPARAMS_MSG,
  985. Client_ID(Client),
  986. Req->command);
  987. }
  988. #endif
  989. }
  990. if (Req->argc >= 1) {
  991. /* Channel or mask given */
  992. chan = Channel_Search(Req->argv[0]);
  993. if (chan) {
  994. /* Members of a channel have been requested */
  995. return IRC_WHO_Channel(Client, chan, only_ops);
  996. }
  997. if (strcmp(Req->argv[0], "0") != 0) {
  998. /* A mask has been given. But please note this RFC
  999. * stupidity: "0" is same as no arguments ... */
  1000. return IRC_WHO_Mask(Client, Req->argv[0], only_ops);
  1001. }
  1002. }
  1003. /* No channel or (valid) mask given */
  1004. return IRC_WHO_Mask(Client, NULL, only_ops);
  1005. } /* IRC_WHO */
  1006. /**
  1007. * Handler for the IRC "WHOIS" command.
  1008. *
  1009. * @param Client The client from which this command has been received.
  1010. * @param Req Request structure with prefix and all parameters.
  1011. * @return CONNECTED or DISCONNECTED.
  1012. */
  1013. GLOBAL bool
  1014. IRC_WHOIS( CLIENT *Client, REQUEST *Req )
  1015. {
  1016. CLIENT *from, *target, *c;
  1017. unsigned int match_count = 0, found = 0;
  1018. bool has_wildcards, is_remote;
  1019. bool got_wildcard = false;
  1020. char mask[COMMAND_LEN], *query;
  1021. assert( Client != NULL );
  1022. assert( Req != NULL );
  1023. /* Wrong number of parameters? */
  1024. if (Req->argc < 1)
  1025. return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
  1026. Client_ID(Client));
  1027. _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
  1028. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  1029. /* Get target server for this command */
  1030. if (Req->argc > 1) {
  1031. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, Client)
  1032. } else
  1033. target = Client_ThisServer();
  1034. assert(target != NULL);
  1035. /* Forward? */
  1036. if (target != Client_ThisServer()) {
  1037. IRC_WriteStrClientPrefix(target, from, "WHOIS %s :%s",
  1038. Req->argv[0], Req->argv[1]);
  1039. return CONNECTED;
  1040. }
  1041. is_remote = Client_Conn(from) < 0;
  1042. strlcpy(mask, Req->argv[Req->argc - 1], sizeof(mask));
  1043. for (query = strtok(ngt_LowerStr(mask), ",");
  1044. query && found < 3;
  1045. query = strtok(NULL, ","), found++)
  1046. {
  1047. has_wildcards = query[strcspn(query, "*?")] != 0;
  1048. /*
  1049. * follows ircd 2.10 implementation:
  1050. * - handle up to 3 targets
  1051. * - no wildcards for remote clients
  1052. * - only one wildcard target per local client
  1053. *
  1054. * Also, at most MAX_RPL_WHOIS matches are returned.
  1055. */
  1056. if (!has_wildcards || is_remote) {
  1057. c = Client_Search(query);
  1058. if (c && (Client_Type(c) == CLIENT_USER
  1059. || Client_Type(c) == CLIENT_SERVICE)) {
  1060. if (!IRC_WHOIS_SendReply(Client, from, c))
  1061. return DISCONNECTED;
  1062. } else {
  1063. if (!IRC_WriteErrClient(Client,
  1064. ERR_NOSUCHNICK_MSG,
  1065. Client_ID(Client),
  1066. query))
  1067. return DISCONNECTED;
  1068. }
  1069. continue;
  1070. }
  1071. if (got_wildcard) {
  1072. /* we already handled one wildcard query */
  1073. if (!IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  1074. Client_ID(Client), query))
  1075. return DISCONNECTED;
  1076. continue;
  1077. }
  1078. got_wildcard = true;
  1079. /* Increase penalty for wildcard queries */
  1080. IRC_SetPenalty(Client, 3);
  1081. for (c = Client_First(); c; c = Client_Next(c)) {
  1082. if (IRC_CheckListTooBig(Client, match_count,
  1083. MAX_RPL_WHOIS, "WHOIS"))
  1084. break;
  1085. if (Client_Type(c) != CLIENT_USER)
  1086. continue;
  1087. if (!MatchCaseInsensitive(query, Client_ID(c)))
  1088. continue;
  1089. if (!IRC_WHOIS_SendReply(Client, from, c))
  1090. return DISCONNECTED;
  1091. match_count++;
  1092. }
  1093. if (match_count == 0)
  1094. IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  1095. Client_ID(Client),
  1096. Req->argv[Req->argc - 1]);
  1097. }
  1098. return IRC_WriteStrClient(from, RPL_ENDOFWHOIS_MSG,
  1099. Client_ID(from), Req->argv[Req->argc - 1]);
  1100. } /* IRC_WHOIS */
  1101. /**
  1102. * Handler for the IRC "WHOWAS" command.
  1103. *
  1104. * @param Client The client from which this command has been received.
  1105. * @param Req Request structure with prefix and all parameters.
  1106. * @return CONNECTED or DISCONNECTED.
  1107. */
  1108. GLOBAL bool
  1109. IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
  1110. {
  1111. CLIENT *target, *prefix;
  1112. WHOWAS *whowas;
  1113. char tok_buf[COMMAND_LEN];
  1114. int max, last, count, i, nc;
  1115. const char *nick;
  1116. assert( Client != NULL );
  1117. assert( Req != NULL );
  1118. /* Wrong number of parameters? */
  1119. if (Req->argc < 1)
  1120. return IRC_WriteErrClient(Client, ERR_NONICKNAMEGIVEN_MSG,
  1121. Client_ID(Client));
  1122. _IRC_ARGC_LE_OR_RETURN_(Client, Req, 3)
  1123. _IRC_GET_SENDER_OR_RETURN_(prefix, Req, Client)
  1124. _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 2, prefix)
  1125. /* Do not reveal any info on disconnected users? */
  1126. if (Conf_MorePrivacy)
  1127. return CONNECTED;
  1128. /* Forward? */
  1129. if (target != Client_ThisServer()) {
  1130. IRC_WriteStrClientPrefix(target, prefix, "WHOWAS %s %s %s",
  1131. Req->argv[0], Req->argv[1],
  1132. Client_ID(target));
  1133. return CONNECTED;
  1134. }
  1135. whowas = Client_GetWhowas( );
  1136. last = Client_GetLastWhowasIndex( );
  1137. if (last < 0)
  1138. last = 0;
  1139. max = DEF_RPL_WHOWAS;
  1140. if (Req->argc > 1) {
  1141. max = atoi(Req->argv[1]);
  1142. if (max < 1)
  1143. max = MAX_RPL_WHOWAS;
  1144. }
  1145. /*
  1146. * Break up the nick argument into a list of nicks, if applicable
  1147. * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
  1148. */
  1149. strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
  1150. nick = strtok(tok_buf, ",");
  1151. for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
  1152. nc = 0;
  1153. do {
  1154. /* Used entry? */
  1155. if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
  1156. if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
  1157. return DISCONNECTED;
  1158. nc++;
  1159. count++;
  1160. }
  1161. /* previous entry */
  1162. i--;
  1163. /* "underflow", wrap around */
  1164. if (i < 0)
  1165. i = MAX_WHOWAS - 1;
  1166. if (nc && count >= max)
  1167. break;
  1168. } while (i != last);
  1169. if (nc == 0 && !IRC_WriteErrClient(prefix, ERR_WASNOSUCHNICK_MSG,
  1170. Client_ID(prefix), nick))
  1171. return DISCONNECTED;
  1172. }
  1173. return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG,
  1174. Client_ID(prefix), Req->argv[0]);
  1175. } /* IRC_WHOWAS */
  1176. /**
  1177. * Send LUSERS reply to a client.
  1178. *
  1179. * @param Client The receipient of the information.
  1180. * @return CONNECTED or DISCONNECTED.
  1181. */
  1182. GLOBAL bool
  1183. IRC_Send_LUSERS(CLIENT *Client)
  1184. {
  1185. unsigned long cnt;
  1186. #ifndef STRICT_RFC
  1187. unsigned long max;
  1188. #endif
  1189. assert(Client != NULL);
  1190. /* Users, services and servers in the network */
  1191. if (!IRC_WriteStrClient(Client, RPL_LUSERCLIENT_MSG, Client_ID(Client),
  1192. Client_UserCount(), Client_ServiceCount(),
  1193. Client_ServerCount()))
  1194. return DISCONNECTED;
  1195. /* Number of IRC operators */
  1196. cnt = Client_OperCount( );
  1197. if (cnt > 0) {
  1198. if (!IRC_WriteStrClient(Client, RPL_LUSEROP_MSG,
  1199. Client_ID(Client), cnt))
  1200. return DISCONNECTED;
  1201. }
  1202. /* Unknown connections */
  1203. cnt = Client_UnknownCount( );
  1204. if (cnt > 0) {
  1205. if (!IRC_WriteStrClient(Client, RPL_LUSERUNKNOWN_MSG,
  1206. Client_ID(Client), cnt))
  1207. return DISCONNECTED;
  1208. }
  1209. /* Number of created channels */
  1210. if (!IRC_WriteStrClient(Client, RPL_LUSERCHANNELS_MSG,
  1211. Client_ID(Client),
  1212. Channel_CountVisible(Client)))
  1213. return DISCONNECTED;
  1214. /* Number of local users, services and servers */
  1215. if (!IRC_WriteStrClient(Client, RPL_LUSERME_MSG, Client_ID(Client),
  1216. Client_MyUserCount(), Client_MyServiceCount(),
  1217. Client_MyServerCount()))
  1218. return DISCONNECTED;
  1219. #ifndef STRICT_RFC
  1220. /* Maximum number of local users */
  1221. cnt = Client_MyUserCount();
  1222. max = Client_MyMaxUserCount();
  1223. if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
  1224. cnt, max, cnt, max))
  1225. return DISCONNECTED;
  1226. /* Maximum number of users in the network */
  1227. cnt = Client_UserCount();
  1228. max = Client_MaxUserCount();
  1229. if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
  1230. cnt, max, cnt, max))
  1231. return DISCONNECTED;
  1232. /* Connection counters */
  1233. if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
  1234. Conn_CountMax(), Conn_CountAccepted()))
  1235. return DISCONNECTED;
  1236. #endif
  1237. return CONNECTED;
  1238. } /* IRC_Send_LUSERS */
  1239. GLOBAL bool
  1240. IRC_Show_MOTD( CLIENT *Client )
  1241. {
  1242. const char *line;
  1243. size_t len_tot, len_str;
  1244. assert( Client != NULL );
  1245. len_tot = array_bytes(&Conf_Motd);
  1246. if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
  1247. return IRC_WriteErrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
  1248. if (!IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG, Client_ID(Client),
  1249. Client_ID(Client_ThisServer())))
  1250. return DISCONNECTED;
  1251. line = array_start(&Conf_Motd);
  1252. while (len_tot > 0) {
  1253. len_str = strlen(line) + 1;
  1254. assert(len_tot >= len_str);
  1255. len_tot -= len_str;
  1256. if (!IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID(Client), line))
  1257. return DISCONNECTED;
  1258. line += len_str;
  1259. }
  1260. if (!Show_MOTD_SSLInfo(Client))
  1261. return DISCONNECTED;
  1262. if (!IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG, Client_ID(Client)))
  1263. return DISCONNECTED;
  1264. if (*Conf_CloakHost)
  1265. return IRC_WriteStrClient(Client, RPL_HOSTHIDDEN_MSG,
  1266. Client_ID(Client),
  1267. Client_Hostname(Client));
  1268. return CONNECTED;
  1269. } /* IRC_Show_MOTD */
  1270. /**
  1271. * Send NAMES reply for a specific client and channel.
  1272. *
  1273. * @param Client The client requesting the NAMES information.
  1274. * @param Chan The channel
  1275. * @return CONNECTED or DISCONNECTED.
  1276. */
  1277. GLOBAL bool
  1278. IRC_Send_NAMES(CLIENT * Client, CHANNEL * Chan)
  1279. {
  1280. bool is_visible, is_member;
  1281. char str[COMMAND_LEN];
  1282. CL2CHAN *cl2chan;
  1283. CLIENT *cl;
  1284. assert(Client != NULL);
  1285. assert(Chan != NULL);
  1286. if (Channel_IsMemberOf(Chan, Client))
  1287. is_member = true;
  1288. else
  1289. is_member = false;
  1290. /* Do not print info on channel memberships to anyone that is not member? */
  1291. if (Conf_MorePrivacy && !is_member)
  1292. return CONNECTED;
  1293. /* Secret channel? */
  1294. if (!is_member && Channel_HasMode(Chan, 's'))
  1295. return CONNECTED;
  1296. snprintf(str, sizeof(str), RPL_NAMREPLY_MSG, Client_ID(Client), "=",
  1297. Channel_Name(Chan));
  1298. cl2chan = Channel_FirstMember(Chan);
  1299. while (cl2chan) {
  1300. cl = Channel_GetClient(cl2chan);
  1301. if (Client_HasMode(cl, 'i'))
  1302. is_visible = false;
  1303. else
  1304. is_visible = true;
  1305. if (is_member || is_visible) {
  1306. if (str[strlen(str) - 1] != ':')
  1307. strlcat(str, " ", sizeof(str));
  1308. who_flags_qualifier(Client, Channel_UserModes(Chan, cl),
  1309. str, sizeof(str));
  1310. strlcat(str, Client_ID(cl), sizeof(str));
  1311. if (strlen(str) > (COMMAND_LEN - CLIENT_NICK_LEN - 4)) {
  1312. if (!IRC_WriteStrClient(Client, "%s", str))
  1313. return DISCONNECTED;
  1314. snprintf(str, sizeof(str), RPL_NAMREPLY_MSG,
  1315. Client_ID(Client), "=",
  1316. Channel_Name(Chan));
  1317. }
  1318. }
  1319. cl2chan = Channel_NextMember(Chan, cl2chan);
  1320. }
  1321. if (str[strlen(str) - 1] != ':') {
  1322. if (!IRC_WriteStrClient(Client, "%s", str))
  1323. return DISCONNECTED;
  1324. }
  1325. return CONNECTED;
  1326. } /* IRC_Send_NAMES */
  1327. /**
  1328. * Send the ISUPPORT numeric (005).
  1329. * This numeric indicates the features that are supported by this server.
  1330. * See <http://www.irc.org/tech_docs/005.html> for details.
  1331. */
  1332. GLOBAL bool
  1333. IRC_Send_ISUPPORT(CLIENT * Client)
  1334. {
  1335. if (Conf_Network[0] && !IRC_WriteStrClient(Client, RPL_ISUPPORTNET_MSG,
  1336. Client_ID(Client),
  1337. Conf_Network))
  1338. return DISCONNECTED;
  1339. if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
  1340. CHANTYPES, CHANTYPES, Conf_MaxJoins))
  1341. return DISCONNECTED;
  1342. return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
  1343. CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
  1344. COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
  1345. COMMAND_LEN - 113, MAX_HNDL_MODES_ARG,
  1346. MAX_HNDL_CHANNEL_LISTS);
  1347. } /* IRC_Send_ISUPPORT */
  1348. /* -eof- */