irc-info.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 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. * IRC info commands
  12. */
  13. #include "portab.h"
  14. #include "imp.h"
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include "ngircd.h"
  22. #include "conn-func.h"
  23. #include "conn-zip.h"
  24. #include "channel.h"
  25. #include "conf.h"
  26. #include "defines.h"
  27. #include "log.h"
  28. #include "messages.h"
  29. #include "match.h"
  30. #include "tool.h"
  31. #include "parse.h"
  32. #include "irc-write.h"
  33. #include "exp.h"
  34. #include "irc-info.h"
  35. GLOBAL bool
  36. IRC_ADMIN(CLIENT *Client, REQUEST *Req )
  37. {
  38. CLIENT *target, *prefix;
  39. assert( Client != NULL );
  40. assert( Req != NULL );
  41. if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  42. /* find target ... */
  43. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  44. else target = Client_ThisServer( );
  45. /* find Prefix */
  46. if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
  47. else prefix = Client;
  48. if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  49. /* forwad message to another server? */
  50. if( target != Client_ThisServer( ))
  51. {
  52. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
  53. /* forward */
  54. IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
  55. return CONNECTED;
  56. }
  57. /* mit Versionsinfo antworten */
  58. if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
  59. if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
  60. if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
  61. if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
  62. IRC_SetPenalty( Client, 1 );
  63. return CONNECTED;
  64. } /* IRC_ADMIN */
  65. /**
  66. * Handler for the IRC command "INFO".
  67. * See RFC 2812 section 3.4.10.
  68. */
  69. GLOBAL bool
  70. IRC_INFO(CLIENT * Client, REQUEST * Req)
  71. {
  72. CLIENT *target, *prefix;
  73. char msg[510];
  74. assert(Client != NULL);
  75. assert(Req != NULL);
  76. /* Wrong number of parameters? */
  77. if (Req->argc > 1)
  78. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  79. Client_ID(Client), Req->command);
  80. /* Determine prefix */
  81. if (Client_Type(Client) == CLIENT_SERVER)
  82. prefix = Client_Search(Req->prefix);
  83. else
  84. prefix = Client;
  85. if (!prefix)
  86. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  87. Client_ID(Client), Req->prefix);
  88. /* Look for a target */
  89. if (Req->argc > 0)
  90. target = Client_Search(Req->argv[0]);
  91. else
  92. target = Client_ThisServer();
  93. /* Make sure that the target is a server */
  94. if (target && Client_Type(target) != CLIENT_SERVER)
  95. target = Client_Introducer(target);
  96. if (!target)
  97. return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
  98. Client_ID(prefix), Req->argv[0]);
  99. /* Pass on to another server? */
  100. if (target != Client_ThisServer()) {
  101. IRC_WriteStrClientPrefix(target, prefix, "INFO %s",
  102. Req->argv[0]);
  103. return CONNECTED;
  104. }
  105. if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix),
  106. NGIRCd_Version))
  107. return DISCONNECTED;
  108. #if defined(__DATE__) && defined(__TIME__)
  109. snprintf(msg, sizeof(msg), "Birth Date: %s at %s", __DATE__, __TIME__);
  110. if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
  111. return DISCONNECTED;
  112. #endif
  113. strlcpy(msg, "On-line since ", sizeof(msg));
  114. strlcat(msg, NGIRCd_StartStr, sizeof(msg));
  115. if (!IRC_WriteStrClient(Client, RPL_INFO_MSG, Client_ID(prefix), msg))
  116. return DISCONNECTED;
  117. if (!IRC_WriteStrClient(Client, RPL_ENDOFINFO_MSG, Client_ID(prefix)))
  118. return DISCONNECTED;
  119. IRC_SetPenalty(Client, 2);
  120. return CONNECTED;
  121. } /* IRC_INFO */
  122. GLOBAL bool
  123. IRC_ISON( CLIENT *Client, REQUEST *Req )
  124. {
  125. char rpl[COMMAND_LEN];
  126. CLIENT *c;
  127. char *ptr;
  128. int i;
  129. assert( Client != NULL );
  130. assert( Req != NULL );
  131. /* Falsche Anzahl Parameter? */
  132. if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  133. strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
  134. for( i = 0; i < Req->argc; i++ )
  135. {
  136. ptr = strtok( Req->argv[i], " " );
  137. while( ptr )
  138. {
  139. ngt_TrimStr( ptr );
  140. c = Client_Search( ptr );
  141. if( c && ( Client_Type( c ) == CLIENT_USER ))
  142. {
  143. /* Dieser Nick ist "online" */
  144. strlcat( rpl, ptr, sizeof( rpl ));
  145. strlcat( rpl, " ", sizeof( rpl ));
  146. }
  147. ptr = strtok( NULL, " " );
  148. }
  149. }
  150. ngt_TrimLastChr(rpl, ' ');
  151. return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
  152. } /* IRC_ISON */
  153. GLOBAL bool
  154. IRC_LINKS( CLIENT *Client, REQUEST *Req )
  155. {
  156. CLIENT *target, *from, *c;
  157. char *mask;
  158. assert( Client != NULL );
  159. assert( Req != NULL );
  160. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  161. /* Server-Mask ermitteln */
  162. if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
  163. else mask = "*";
  164. /* Absender ermitteln */
  165. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  166. else from = Client;
  167. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  168. /* An anderen Server forwarden? */
  169. if( Req->argc == 2 )
  170. {
  171. target = Client_Search( Req->argv[0] );
  172. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  173. else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
  174. }
  175. /* Wer ist der Absender? */
  176. if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
  177. else target = Client;
  178. if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  179. c = Client_First( );
  180. while( c )
  181. {
  182. if( Client_Type( c ) == CLIENT_SERVER )
  183. {
  184. if( ! IRC_WriteStrClient( target, RPL_LINKS_MSG, Client_ID( target ), Client_ID( c ), Client_ID( Client_TopServer( c ) ? Client_TopServer( c ) : Client_ThisServer( )), Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
  185. }
  186. c = Client_Next( c );
  187. }
  188. IRC_SetPenalty( target, 1 );
  189. return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
  190. } /* IRC_LINKS */
  191. GLOBAL bool
  192. IRC_LUSERS( CLIENT *Client, REQUEST *Req )
  193. {
  194. CLIENT *target, *from;
  195. assert( Client != NULL );
  196. assert( Req != NULL );
  197. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  198. /* Absender ermitteln */
  199. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  200. else from = Client;
  201. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  202. /* An anderen Server forwarden? */
  203. if( Req->argc == 2 )
  204. {
  205. target = Client_Search( Req->argv[1] );
  206. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
  207. else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
  208. }
  209. /* Wer ist der Absender? */
  210. if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
  211. else target = Client;
  212. if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  213. IRC_Send_LUSERS( target );
  214. IRC_SetPenalty( target, 1 );
  215. return CONNECTED;
  216. } /* IRC_LUSERS */
  217. /**
  218. * Handler for the IRC command "SERVLIST".
  219. * List registered services, see RFC 2811, section 3.5.1: the syntax is
  220. * "SERVLIST [<mask> [<type>]]".
  221. */
  222. GLOBAL bool
  223. IRC_SERVLIST(CLIENT *Client, REQUEST *Req)
  224. {
  225. CLIENT *c;
  226. assert(Client != NULL);
  227. assert(Req != NULL);
  228. if (Req->argc > 2)
  229. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  230. Client_ID(Client), Req->command);
  231. if (Req->argc < 2 || strcmp(Req->argv[1], "0") == 0) {
  232. for (c = Client_First(); c!= NULL; c = Client_Next(c)) {
  233. if (Client_Type(c) != CLIENT_SERVICE)
  234. continue;
  235. if (Req->argc > 0 && !MatchCaseInsensitive(Req->argv[0],
  236. Client_ID(c)))
  237. continue;
  238. if (!IRC_WriteStrClient(Client, RPL_SERVLIST_MSG,
  239. Client_ID(Client), Client_Mask(c),
  240. Client_Mask(Client_Introducer(c)), "*",
  241. 0, Client_Hops(c), Client_Info(c)))
  242. return DISCONNECTED;
  243. }
  244. }
  245. return IRC_WriteStrClient(Client, RPL_SERVLISTEND_MSG, Client_ID(Client),
  246. Req->argc > 0 ? Req->argv[0] : "*",
  247. Req->argc > 1 ? Req->argv[1] : "0");
  248. } /* IRC_SERVLIST */
  249. GLOBAL bool
  250. IRC_MOTD( CLIENT *Client, REQUEST *Req )
  251. {
  252. CLIENT *from, *target;
  253. assert( Client != NULL );
  254. assert( Req != NULL );
  255. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  256. /* From aus Prefix ermitteln */
  257. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  258. else from = Client;
  259. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  260. if( Req->argc == 1 )
  261. {
  262. /* forward? */
  263. target = Client_Search( Req->argv[0] );
  264. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  265. if( target != Client_ThisServer( ))
  266. {
  267. /* Ok, anderer Server ist das Ziel: forwarden */
  268. return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
  269. }
  270. }
  271. IRC_SetPenalty( from, 3 );
  272. return IRC_Show_MOTD( from );
  273. } /* IRC_MOTD */
  274. GLOBAL bool
  275. IRC_NAMES( CLIENT *Client, REQUEST *Req )
  276. {
  277. char rpl[COMMAND_LEN], *ptr;
  278. CLIENT *target, *from, *c;
  279. CHANNEL *chan;
  280. assert( Client != NULL );
  281. assert( Req != NULL );
  282. if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  283. /* use prefix to determine "From" */
  284. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  285. else from = Client;
  286. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  287. if( Req->argc == 2 )
  288. {
  289. /* forward to another server? */
  290. target = Client_Search( Req->argv[1] );
  291. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
  292. if( target != Client_ThisServer( )) {
  293. /* target is another server, forward */
  294. return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
  295. }
  296. }
  297. if( Req->argc > 0 )
  298. {
  299. /* bestimmte Channels durchgehen */
  300. ptr = strtok( Req->argv[0], "," );
  301. while( ptr )
  302. {
  303. chan = Channel_Search( ptr );
  304. if( chan )
  305. {
  306. /* print name */
  307. if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
  308. }
  309. if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
  310. /* get next channel name */
  311. ptr = strtok( NULL, "," );
  312. }
  313. return CONNECTED;
  314. }
  315. chan = Channel_First( );
  316. while( chan )
  317. {
  318. if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
  319. chan = Channel_Next( chan );
  320. }
  321. /* Now print all clients which are not in any channel */
  322. c = Client_First( );
  323. snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
  324. while( c )
  325. {
  326. if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
  327. {
  328. /* its a user, concatenate ... */
  329. if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
  330. strlcat( rpl, Client_ID( c ), sizeof( rpl ));
  331. if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
  332. {
  333. /* Line is gwoing too long, send now */
  334. if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
  335. snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
  336. }
  337. }
  338. c = Client_Next( c );
  339. }
  340. if( rpl[strlen( rpl ) - 1] != ':')
  341. {
  342. if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
  343. }
  344. IRC_SetPenalty( from, 1 );
  345. return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
  346. } /* IRC_NAMES */
  347. static unsigned int
  348. t_diff(time_t *t, const time_t d)
  349. {
  350. time_t diff, remain;
  351. diff = *t / d;
  352. remain = diff * d;
  353. *t -= remain;
  354. return (unsigned int)diff;
  355. }
  356. static unsigned int
  357. uptime_days(time_t *now)
  358. {
  359. return t_diff(now, 60 * 60 * 24);
  360. }
  361. static unsigned int
  362. uptime_hrs(time_t *now)
  363. {
  364. return t_diff(now, 60 * 60);
  365. }
  366. static unsigned int
  367. uptime_mins(time_t *now)
  368. {
  369. return t_diff(now, 60);
  370. }
  371. /**
  372. * Handler for the IRC command "STATS".
  373. * See RFC 2812 section 3.4.4.
  374. */
  375. GLOBAL bool
  376. IRC_STATS( CLIENT *Client, REQUEST *Req )
  377. {
  378. CLIENT *from, *target, *cl;
  379. CONN_ID con;
  380. char query;
  381. COMMAND *cmd;
  382. time_t time_now;
  383. unsigned int days, hrs, mins;
  384. assert(Client != NULL);
  385. assert(Req != NULL);
  386. if (Req->argc > 2)
  387. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  388. Client_ID(Client), Req->command);
  389. /* use prefix to determine "From" */
  390. if (Client_Type(Client) == CLIENT_SERVER)
  391. from = Client_Search(Req->prefix);
  392. else
  393. from = Client;
  394. if (!from)
  395. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  396. Client_ID(Client), Req->prefix);
  397. if (Req->argc == 2) {
  398. /* forward to another server? */
  399. target = Client_Search(Req->argv[1]);
  400. if ((!target) || (Client_Type(target) != CLIENT_SERVER))
  401. return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
  402. Client_ID(from), Req->argv[1]);
  403. if (target != Client_ThisServer()) {
  404. /* forward to another server */
  405. return IRC_WriteStrClientPrefix(target, from,
  406. "STATS %s %s", Req->argv[0], Req->argv[1]);
  407. }
  408. }
  409. if (Req->argc > 0)
  410. query = Req->argv[0][0] ? Req->argv[0][0] : '*';
  411. else
  412. query = '*';
  413. switch (query) {
  414. case 'l': /* Link status (servers and own link) */
  415. case 'L':
  416. time_now = time(NULL);
  417. for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
  418. cl = Conn_GetClient(con);
  419. if (!cl)
  420. continue;
  421. if ((Client_Type(cl) == CLIENT_SERVER)
  422. || (cl == Client)) {
  423. /* Server link or our own connection */
  424. #ifdef ZLIB
  425. if (Conn_Options(con) & CONN_ZIP) {
  426. if (!IRC_WriteStrClient
  427. (from, RPL_STATSLINKINFOZIP_MSG,
  428. Client_ID(from), Client_Mask(cl),
  429. Conn_SendQ(con), Conn_SendMsg(con),
  430. Zip_SendBytes(con),
  431. Conn_SendBytes(con),
  432. Conn_RecvMsg(con),
  433. Zip_RecvBytes(con),
  434. Conn_RecvBytes(con),
  435. (long)(time_now - Conn_StartTime(con))))
  436. return DISCONNECTED;
  437. continue;
  438. }
  439. #endif
  440. if (!IRC_WriteStrClient
  441. (from, RPL_STATSLINKINFO_MSG,
  442. Client_ID(from), Client_Mask(cl),
  443. Conn_SendQ(con), Conn_SendMsg(con),
  444. Conn_SendBytes(con), Conn_RecvMsg(con),
  445. Conn_RecvBytes(con),
  446. (long)(time_now - Conn_StartTime(con))))
  447. return DISCONNECTED;
  448. }
  449. }
  450. break;
  451. case 'm': /* IRC command status (usage count) */
  452. case 'M':
  453. cmd = Parse_GetCommandStruct();
  454. for (; cmd->name; cmd++) {
  455. if (cmd->lcount == 0 && cmd->rcount == 0)
  456. continue;
  457. if (!IRC_WriteStrClient
  458. (from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
  459. cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
  460. return DISCONNECTED;
  461. }
  462. break;
  463. case 'u': /* Server uptime */
  464. case 'U':
  465. time_now = time(NULL) - NGIRCd_Start;
  466. days = uptime_days(&time_now);
  467. hrs = uptime_hrs(&time_now);
  468. mins = uptime_mins(&time_now);
  469. if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
  470. days, hrs, mins, (unsigned int)time_now))
  471. return DISCONNECTED;
  472. break;
  473. }
  474. IRC_SetPenalty(from, 2);
  475. return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
  476. Client_ID(from), query);
  477. } /* IRC_STATS */
  478. /**
  479. * Handler for the IRC command "SUMMON".
  480. * See RFC 2812 section 4.5. ngIRCd doesn't implement this functionality and
  481. * therefore answers with ERR_SUMMONDISABLED.
  482. */
  483. GLOBAL bool
  484. IRC_SUMMON(CLIENT * Client, REQUEST * Req)
  485. {
  486. return IRC_WriteStrClient(Client, ERR_SUMMONDISABLED_MSG,
  487. Client_ID(Client), Req->command);
  488. } /* IRC_SUMMON */
  489. GLOBAL bool
  490. IRC_TIME( CLIENT *Client, REQUEST *Req )
  491. {
  492. CLIENT *from, *target;
  493. char t_str[64];
  494. time_t t;
  495. assert( Client != NULL );
  496. assert( Req != NULL );
  497. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  498. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  499. else from = Client;
  500. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  501. if( Req->argc == 1 )
  502. {
  503. target = Client_Search( Req->argv[0] );
  504. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
  505. if( target != Client_ThisServer( ))
  506. {
  507. return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
  508. }
  509. }
  510. t = time( NULL );
  511. (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
  512. return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
  513. } /* IRC_TIME */
  514. /**
  515. * Handler for the IRC command "USERHOST".
  516. * See RFC 2812 section 4.8.
  517. */
  518. GLOBAL bool
  519. IRC_USERHOST(CLIENT *Client, REQUEST *Req)
  520. {
  521. char rpl[COMMAND_LEN];
  522. CLIENT *c;
  523. int max, i;
  524. assert(Client != NULL);
  525. assert(Req != NULL);
  526. if ((Req->argc < 1))
  527. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  528. Client_ID(Client), Req->command);
  529. if (Req->argc > 5)
  530. max = 5;
  531. else
  532. max = Req->argc;
  533. strlcpy(rpl, RPL_USERHOST_MSG, sizeof rpl);
  534. for (i = 0; i < max; i++) {
  535. c = Client_Search(Req->argv[i]);
  536. if (c && (Client_Type(c) == CLIENT_USER)) {
  537. /* This Nick is "online" */
  538. strlcat(rpl, Client_ID(c), sizeof(rpl));
  539. if (Client_HasMode(c, 'o'))
  540. strlcat(rpl, "*", sizeof(rpl));
  541. strlcat(rpl, "=", sizeof(rpl));
  542. if (Client_HasMode(c, 'a'))
  543. strlcat(rpl, "-", sizeof(rpl));
  544. else
  545. strlcat(rpl, "+", sizeof(rpl));
  546. strlcat(rpl, Client_User(c), sizeof(rpl));
  547. strlcat(rpl, "@", sizeof(rpl));
  548. strlcat(rpl, Client_HostnameCloaked(c), sizeof(rpl));
  549. strlcat(rpl, " ", sizeof(rpl));
  550. }
  551. }
  552. ngt_TrimLastChr(rpl, ' ');
  553. return IRC_WriteStrClient(Client, rpl, Client_ID(Client));
  554. } /* IRC_USERHOST */
  555. /**
  556. * Handler for the IRC command "USERS".
  557. * See RFC 2812 section 4.6. As suggested there the command is disabled.
  558. */
  559. GLOBAL bool
  560. IRC_USERS(CLIENT * Client, REQUEST * Req)
  561. {
  562. return IRC_WriteStrClient(Client, ERR_USERSDISABLED_MSG,
  563. Client_ID(Client), Req->command);
  564. } /* IRC_USERS */
  565. GLOBAL bool
  566. IRC_VERSION( CLIENT *Client, REQUEST *Req )
  567. {
  568. CLIENT *target, *prefix;
  569. assert( Client != NULL );
  570. assert( Req != NULL );
  571. if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  572. /* Ziel suchen */
  573. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  574. else target = Client_ThisServer( );
  575. /* Prefix ermitteln */
  576. if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
  577. else prefix = Client;
  578. if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  579. /* An anderen Server weiterleiten? */
  580. if( target != Client_ThisServer( ))
  581. {
  582. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
  583. /* forwarden */
  584. IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
  585. return CONNECTED;
  586. }
  587. /* send version information */
  588. IRC_SetPenalty(Client, 1);
  589. return IRC_WriteStrClient(Client, RPL_VERSION_MSG, Client_ID(prefix),
  590. PACKAGE_NAME, PACKAGE_VERSION,
  591. NGIRCd_DebugLevel, Conf_ServerName,
  592. NGIRCd_VersionAddition);
  593. } /* IRC_VERSION */
  594. static bool
  595. write_whoreply(CLIENT *Client, CLIENT *c, const char *channelname, const char *flags)
  596. {
  597. return IRC_WriteStrClient(Client, RPL_WHOREPLY_MSG, Client_ID(Client),
  598. channelname, Client_User(c),
  599. Client_HostnameCloaked(c),
  600. Client_ID(Client_Introducer(c)), Client_ID(c),
  601. flags, Client_Hops(c), Client_Info(c));
  602. }
  603. static const char *
  604. who_flags_status(const char *client_modes)
  605. {
  606. if (strchr(client_modes, 'a'))
  607. return "G"; /* away */
  608. return "H";
  609. }
  610. static const char *
  611. who_flags_qualifier(const char *chan_user_modes)
  612. {
  613. if (strchr(chan_user_modes, 'o'))
  614. return "@";
  615. else if (strchr(chan_user_modes, 'v'))
  616. return "+";
  617. return "";
  618. }
  619. static bool
  620. IRC_Send_WHO(CLIENT *Client, CHANNEL *Chan, bool OnlyOps)
  621. {
  622. bool is_visible, is_member, is_ircop;
  623. CL2CHAN *cl2chan;
  624. const char *client_modes;
  625. const char *chan_user_modes;
  626. char flags[8];
  627. CLIENT *c;
  628. assert( Client != NULL );
  629. assert( Chan != NULL );
  630. is_member = Channel_IsMemberOf(Chan, Client);
  631. /* Secret channel? */
  632. if (!is_member && strchr(Channel_Modes(Chan), 's'))
  633. return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
  634. cl2chan = Channel_FirstMember(Chan);
  635. for (; cl2chan ; cl2chan = Channel_NextMember(Chan, cl2chan)) {
  636. c = Channel_GetClient(cl2chan);
  637. client_modes = Client_Modes(c);
  638. is_ircop = strchr(client_modes, 'o') != NULL;
  639. if (OnlyOps && !is_ircop)
  640. continue;
  641. is_visible = strchr(client_modes, 'i') == NULL;
  642. if (is_member || is_visible) {
  643. strcpy(flags, who_flags_status(client_modes));
  644. if (is_ircop)
  645. strlcat(flags, "*", sizeof(flags));
  646. chan_user_modes = Channel_UserModes(Chan, c);
  647. strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
  648. if (!write_whoreply(Client, c, Channel_Name(Chan), flags))
  649. return DISCONNECTED;
  650. }
  651. }
  652. return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), Channel_Name(Chan));
  653. } /* IRC_Send_WHO */
  654. GLOBAL bool
  655. IRC_WHO( CLIENT *Client, REQUEST *Req )
  656. {
  657. bool only_ops, have_arg, client_match;
  658. const char *channelname, *client_modes, *chan_user_modes;
  659. char pattern[COMMAND_LEN];
  660. char flags[4];
  661. CL2CHAN *cl2chan;
  662. CHANNEL *chan, *cn;
  663. CLIENT *c;
  664. assert( Client != NULL );
  665. assert( Req != NULL );
  666. if (Req->argc > 2)
  667. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  668. only_ops = false;
  669. have_arg = false;
  670. if (Req->argc == 2) {
  671. if (strcmp(Req->argv[1], "o") == 0)
  672. only_ops = true;
  673. #ifdef STRICT_RFC
  674. else return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
  675. #endif
  676. }
  677. IRC_SetPenalty(Client, 1);
  678. if (Req->argc >= 1) { /* Channel or Mask. */
  679. chan = Channel_Search(Req->argv[0]);
  680. if (chan)
  681. return IRC_Send_WHO(Client, chan, only_ops);
  682. if (strcmp(Req->argv[0], "0") != 0) { /* RFC stupidity, same as no arguments */
  683. have_arg = true;
  684. strlcpy(pattern, Req->argv[0], sizeof(pattern));
  685. ngt_LowerStr(pattern);
  686. IRC_SetPenalty(Client, 3);
  687. }
  688. }
  689. for (c = Client_First(); c != NULL; c = Client_Next(c)) {
  690. if (Client_Type(c) != CLIENT_USER)
  691. continue;
  692. /*
  693. * RFC 2812, 3.6.1:
  694. * In the absence of the parameter, all visible (users who aren't
  695. * invisible (user mode +i) and who don't have a common channel
  696. * with the requesting client) are listed.
  697. *
  698. * The same result can be achieved by using a [sic] of "0"
  699. * or any wildcard which will end up matching every visible user.
  700. *
  701. * The [sic] passed to WHO is matched against users' host, server, real name and
  702. * nickname if the channel cannot be found.
  703. */
  704. client_modes = Client_Modes(c);
  705. if (strchr(client_modes, 'i'))
  706. continue;
  707. if (only_ops && !strchr(client_modes, 'o'))
  708. continue;
  709. if (have_arg) { /* match pattern against user host/server/name/nick */
  710. client_match = MatchCaseInsensitive(pattern, Client_Hostname(c)); /* user's host */
  711. if (!client_match)
  712. client_match = MatchCaseInsensitive(pattern, Client_ID(Client_Introducer(c))); /* server */
  713. if (!client_match)
  714. client_match = Match(Req->argv[0], Client_Info(c)); /* realname */
  715. if (!client_match)
  716. client_match = MatchCaseInsensitive(pattern, Client_ID(c)); /* nick name */
  717. if (!client_match) /* This isn't the client you're looking for */
  718. continue;
  719. }
  720. strcpy(flags, who_flags_status(client_modes));
  721. if (strchr(client_modes, 'o')) /* this client is an operator */
  722. strlcat(flags, "*", sizeof(flags));
  723. /* Search suitable channel */
  724. cl2chan = Channel_FirstChannelOf(c);
  725. while (cl2chan) {
  726. cn = Channel_GetChannel(cl2chan);
  727. if (Channel_IsMemberOf(cn, Client) ||
  728. !strchr(Channel_Modes(cn), 's'))
  729. {
  730. channelname = Channel_Name(cn);
  731. break;
  732. }
  733. cl2chan = Channel_NextChannelOf(c, cl2chan);
  734. }
  735. if (cl2chan) {
  736. chan = Channel_GetChannel(cl2chan);
  737. chan_user_modes = Channel_UserModes(chan, c);
  738. strlcat(flags, who_flags_qualifier(chan_user_modes), sizeof(flags));
  739. } else
  740. channelname = "*";
  741. if (!write_whoreply(Client, c, channelname, flags))
  742. return DISCONNECTED;
  743. }
  744. if (Req->argc > 0)
  745. channelname = Req->argv[0];
  746. else
  747. channelname = "*";
  748. return IRC_WriteStrClient(Client, RPL_ENDOFWHO_MSG, Client_ID(Client), channelname);
  749. } /* IRC_WHO */
  750. GLOBAL bool
  751. IRC_WHOIS( CLIENT *Client, REQUEST *Req )
  752. {
  753. CLIENT *from, *target, *c;
  754. char str[LINE_LEN + 1];
  755. CL2CHAN *cl2chan;
  756. CHANNEL *chan;
  757. assert( Client != NULL );
  758. assert( Req != NULL );
  759. /* Bad number of parameters? */
  760. if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  761. /* Search client */
  762. c = Client_Search( Req->argv[Req->argc - 1] );
  763. if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
  764. /* Search sender of the WHOIS */
  765. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  766. else from = Client;
  767. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  768. /* Forward to other server? */
  769. if( Req->argc > 1 )
  770. {
  771. /* Search target server (can be specified as nick of that server!) */
  772. target = Client_Search( Req->argv[0] );
  773. if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  774. }
  775. else target = Client_ThisServer( );
  776. assert( target != NULL );
  777. if(( Client_NextHop( target ) != Client_ThisServer( )) && ( Client_Type( Client_NextHop( target )) == CLIENT_SERVER )) return IRC_WriteStrClientPrefix( target, from, "WHOIS %s :%s", Req->argv[0], Req->argv[1] );
  778. /* Nick, user, hostname and client info */
  779. if (!IRC_WriteStrClient(from, RPL_WHOISUSER_MSG, Client_ID(from),
  780. Client_ID(c), Client_User(c),
  781. Client_HostnameCloaked(c), Client_Info(c)))
  782. return DISCONNECTED;
  783. /* Server */
  784. if( ! IRC_WriteStrClient( from, RPL_WHOISSERVER_MSG, Client_ID( from ), Client_ID( c ), Client_ID( Client_Introducer( c )), Client_Info( Client_Introducer( c )))) return DISCONNECTED;
  785. /* Channels */
  786. snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
  787. cl2chan = Channel_FirstChannelOf( c );
  788. while( cl2chan )
  789. {
  790. chan = Channel_GetChannel( cl2chan );
  791. assert( chan != NULL );
  792. /* next */
  793. cl2chan = Channel_NextChannelOf( c, cl2chan );
  794. /* Secret channel? */
  795. if (strchr(Channel_Modes(chan), 's')
  796. && !Channel_IsMemberOf(chan, Client))
  797. continue;
  798. /* Local channel and request is not from a user? */
  799. if (Client_Type(Client) == CLIENT_SERVER
  800. && Channel_IsLocal(chan))
  801. continue;
  802. /* Concatenate channel names */
  803. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  804. if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
  805. else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
  806. strlcat( str, Channel_Name( chan ), sizeof( str ));
  807. if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
  808. {
  809. /* Line becomes too long: send it! */
  810. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  811. snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
  812. }
  813. }
  814. if( str[strlen( str ) - 1] != ':')
  815. {
  816. /* There is data left to send: */
  817. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  818. }
  819. /* IRC-Operator? */
  820. if( Client_HasMode( c, 'o' ))
  821. {
  822. if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
  823. }
  824. /* Connected using SSL? */
  825. if (Conn_UsesSSL(Client_Conn(c))) {
  826. if (!IRC_WriteStrClient
  827. (from, RPL_WHOISSSL_MSG, Client_ID(from), Client_ID(c)))
  828. return DISCONNECTED;
  829. }
  830. /* Idle and signon time (local clients only!) */
  831. if (Client_Conn(c) > NONE ) {
  832. if (! IRC_WriteStrClient(from, RPL_WHOISIDLE_MSG,
  833. Client_ID(from), Client_ID(c),
  834. (unsigned long)Conn_GetIdle(Client_Conn(c)),
  835. (unsigned long)Conn_GetSignon(Client_Conn(c))))
  836. return DISCONNECTED;
  837. }
  838. /* Away? */
  839. if( Client_HasMode( c, 'a' ))
  840. {
  841. if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
  842. }
  843. /* End of Whois */
  844. return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
  845. } /* IRC_WHOIS */
  846. static bool
  847. WHOWAS_EntryWrite(CLIENT *prefix, WHOWAS *entry)
  848. {
  849. char t_str[60];
  850. (void)strftime(t_str, sizeof(t_str), "%a %b %d %H:%M:%S %Y",
  851. localtime(&entry->time));
  852. if (!IRC_WriteStrClient(prefix, RPL_WHOWASUSER_MSG, Client_ID(prefix),
  853. entry->id, entry->user, entry->host, entry->info))
  854. return DISCONNECTED;
  855. return IRC_WriteStrClient(prefix, RPL_WHOISSERVER_MSG, Client_ID(prefix),
  856. entry->id, entry->server, t_str);
  857. }
  858. /**
  859. * IRC "WHOWAS" function.
  860. * This function implements the IRC command "WHOWHAS". It handles local
  861. * requests and request that should be forwarded to other servers.
  862. */
  863. GLOBAL bool
  864. IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
  865. {
  866. CLIENT *target, *prefix;
  867. WHOWAS *whowas;
  868. char tok_buf[COMMAND_LEN];
  869. int max, last, count, i, nc;
  870. const char *nick;
  871. assert( Client != NULL );
  872. assert( Req != NULL );
  873. /* Wrong number of parameters? */
  874. if (Req->argc > 3)
  875. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  876. Client_ID(Client), Req->command);
  877. if (Req->argc < 1)
  878. return IRC_WriteStrClient(Client, ERR_NONICKNAMEGIVEN_MSG, Client_ID(Client));
  879. /* Search target */
  880. if (Req->argc == 3)
  881. target = Client_Search(Req->argv[2]);
  882. else
  883. target = Client_ThisServer();
  884. /* Get prefix */
  885. if (Client_Type(Client) == CLIENT_SERVER)
  886. prefix = Client_Search(Req->prefix);
  887. else
  888. prefix = Client;
  889. if (!prefix)
  890. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  891. Client_ID(Client), Req->prefix);
  892. /* Forward to other server? */
  893. if (target != Client_ThisServer()) {
  894. if (!target || (Client_Type(target) != CLIENT_SERVER))
  895. return IRC_WriteStrClient(prefix, ERR_NOSUCHSERVER_MSG,
  896. Client_ID(prefix), Req->argv[2]);
  897. /* Forward */
  898. IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
  899. Req->argv[0], Req->argv[1],
  900. Req->argv[2] );
  901. return CONNECTED;
  902. }
  903. whowas = Client_GetWhowas( );
  904. last = Client_GetLastWhowasIndex( );
  905. if (last < 0)
  906. last = 0;
  907. max = DEFAULT_WHOWAS;
  908. if (Req->argc > 1) {
  909. max = atoi(Req->argv[1]);
  910. if (max < 1)
  911. max = MAX_WHOWAS;
  912. }
  913. /*
  914. * Break up the nick argument into a list of nicks, if applicable
  915. * Can't modify Req->argv[0] because we need it for RPL_ENDOFWHOWAS_MSG.
  916. */
  917. strlcpy(tok_buf, Req->argv[0], sizeof(tok_buf));
  918. nick = strtok(tok_buf, ",");
  919. for (i=last, count=0; nick != NULL ; nick = strtok(NULL, ",")) {
  920. nc = 0;
  921. do {
  922. /* Used entry? */
  923. if (whowas[i].time > 0 && strcasecmp(nick, whowas[i].id) == 0) {
  924. if (!WHOWAS_EntryWrite(prefix, &whowas[i]))
  925. return DISCONNECTED;
  926. nc++;
  927. count++;
  928. }
  929. /* previous entry */
  930. i--;
  931. /* "underflow", wrap around */
  932. if (i < 0)
  933. i = MAX_WHOWAS - 1;
  934. if (nc && count >= max)
  935. break;
  936. } while (i != last);
  937. if (nc == 0 && !IRC_WriteStrClient(prefix, ERR_WASNOSUCHNICK_MSG,
  938. Client_ID(prefix), nick))
  939. return DISCONNECTED;
  940. }
  941. return IRC_WriteStrClient(prefix, RPL_ENDOFWHOWAS_MSG, Client_ID(prefix), Req->argv[0]);
  942. } /* IRC_WHOWAS */
  943. GLOBAL bool
  944. IRC_Send_LUSERS( CLIENT *Client )
  945. {
  946. unsigned long cnt;
  947. #ifndef STRICT_RFC
  948. unsigned long max;
  949. #endif
  950. assert( Client != NULL );
  951. /* Users, services and serevers in the network */
  952. if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
  953. /* Number of IRC operators */
  954. cnt = Client_OperCount( );
  955. if( cnt > 0 )
  956. {
  957. if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
  958. }
  959. /* Unknown connections */
  960. cnt = Client_UnknownCount( );
  961. if( cnt > 0 )
  962. {
  963. if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
  964. }
  965. /* Number of created channels */
  966. if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
  967. /* Number of local users, services and servers */
  968. if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
  969. #ifndef STRICT_RFC
  970. /* Maximum number of local users */
  971. cnt = Client_MyUserCount();
  972. max = Client_MyMaxUserCount();
  973. if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
  974. cnt, max, cnt, max))
  975. return DISCONNECTED;
  976. /* Maximum number of users in the network */
  977. cnt = Client_UserCount();
  978. max = Client_MaxUserCount();
  979. if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
  980. cnt, max, cnt, max))
  981. return DISCONNECTED;
  982. /* Connection counters */
  983. if (! IRC_WriteStrClient(Client, RPL_STATSCONN_MSG, Client_ID(Client),
  984. Conn_CountMax(), Conn_CountAccepted()))
  985. return DISCONNECTED;
  986. #endif
  987. return CONNECTED;
  988. } /* IRC_Send_LUSERS */
  989. static bool
  990. Show_MOTD_Start(CLIENT *Client)
  991. {
  992. return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
  993. Client_ID( Client ), Client_ID( Client_ThisServer( )));
  994. }
  995. static bool
  996. Show_MOTD_Sendline(CLIENT *Client, const char *msg)
  997. {
  998. return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
  999. }
  1000. static bool
  1001. Show_MOTD_End(CLIENT *Client)
  1002. {
  1003. return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
  1004. }
  1005. #ifdef SSL_SUPPORT
  1006. static bool Show_MOTD_SSLInfo(CLIENT *Client)
  1007. {
  1008. bool ret = true;
  1009. char buf[COMMAND_LEN] = "Connected using Cipher ";
  1010. if (!Conn_GetCipherInfo(Client_Conn(Client), buf + 23, sizeof buf - 23))
  1011. return true;
  1012. if (!Show_MOTD_Sendline(Client, buf))
  1013. ret = false;
  1014. return ret;
  1015. }
  1016. #else
  1017. static inline bool
  1018. Show_MOTD_SSLInfo(UNUSED CLIENT *c)
  1019. { return true; }
  1020. #endif
  1021. GLOBAL bool
  1022. IRC_Show_MOTD( CLIENT *Client )
  1023. {
  1024. const char *line;
  1025. size_t len_tot, len_str;
  1026. assert( Client != NULL );
  1027. len_tot = array_bytes(&Conf_Motd);
  1028. if (len_tot == 0 && !Conn_UsesSSL(Client_Conn(Client)))
  1029. return IRC_WriteStrClient(Client, ERR_NOMOTD_MSG, Client_ID(Client));
  1030. if (!Show_MOTD_Start(Client))
  1031. return DISCONNECTED;
  1032. line = array_start(&Conf_Motd);
  1033. while (len_tot > 0) {
  1034. len_str = strlen(line) + 1;
  1035. assert(len_tot >= len_str);
  1036. len_tot -= len_str;
  1037. if (!Show_MOTD_Sendline(Client, line))
  1038. return DISCONNECTED;
  1039. line += len_str;
  1040. }
  1041. if (!Show_MOTD_SSLInfo(Client))
  1042. return DISCONNECTED;
  1043. return Show_MOTD_End(Client);
  1044. } /* IRC_Show_MOTD */
  1045. GLOBAL bool
  1046. IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
  1047. {
  1048. bool is_visible, is_member;
  1049. char str[LINE_LEN + 1];
  1050. CL2CHAN *cl2chan;
  1051. CLIENT *cl;
  1052. assert( Client != NULL );
  1053. assert( Chan != NULL );
  1054. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  1055. else is_member = false;
  1056. /* Secret channel? */
  1057. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  1058. /* Alle Mitglieder suchen */
  1059. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  1060. cl2chan = Channel_FirstMember( Chan );
  1061. while( cl2chan )
  1062. {
  1063. cl = Channel_GetClient( cl2chan );
  1064. if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
  1065. else is_visible = true;
  1066. if( is_member || is_visible )
  1067. {
  1068. /* Nick anhaengen */
  1069. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  1070. if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
  1071. else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
  1072. strlcat( str, Client_ID( cl ), sizeof( str ));
  1073. if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
  1074. {
  1075. /* Zeile wird zu lang: senden! */
  1076. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  1077. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  1078. }
  1079. }
  1080. /* naechstes Mitglied suchen */
  1081. cl2chan = Channel_NextMember( Chan, cl2chan );
  1082. }
  1083. if( str[strlen( str ) - 1] != ':')
  1084. {
  1085. /* Es sind noch Daten da, die gesendet werden muessen */
  1086. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  1087. }
  1088. return CONNECTED;
  1089. } /* IRC_Send_NAMES */
  1090. /**
  1091. * Send the ISUPPORT numeric (005).
  1092. * This numeric indicates the features that are supported by this server.
  1093. * See <http://www.irc.org/tech_docs/005.html> for details.
  1094. */
  1095. GLOBAL bool
  1096. IRC_Send_ISUPPORT(CLIENT * Client)
  1097. {
  1098. if (!IRC_WriteStrClient(Client, RPL_ISUPPORT1_MSG, Client_ID(Client),
  1099. Conf_MaxJoins))
  1100. return DISCONNECTED;
  1101. return IRC_WriteStrClient(Client, RPL_ISUPPORT2_MSG, Client_ID(Client),
  1102. CHANNEL_NAME_LEN - 1, Conf_MaxNickLength - 1,
  1103. COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1,
  1104. COMMAND_LEN - 113);
  1105. } /* IRC_Send_ISUPPORT */
  1106. /* -eof- */