irc-info.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2005 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. static char UNUSED id[] = "$Id: irc-info.c,v 1.30 2005/06/17 19:15:43 fw Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include "ngircd.h"
  23. #include "cvs-version.h"
  24. #include "conn-func.h"
  25. #include "conn-zip.h"
  26. #include "client.h"
  27. #include "channel.h"
  28. #include "resolve.h"
  29. #include "conf.h"
  30. #include "defines.h"
  31. #include "log.h"
  32. #include "messages.h"
  33. #include "tool.h"
  34. #include "parse.h"
  35. #include "irc-write.h"
  36. #include "exp.h"
  37. #include "irc-info.h"
  38. GLOBAL bool
  39. IRC_ADMIN(CLIENT *Client, REQUEST *Req )
  40. {
  41. CLIENT *target, *prefix;
  42. assert( Client != NULL );
  43. assert( Req != NULL );
  44. /* Falsche Anzahl Parameter? */
  45. if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  46. /* Ziel suchen */
  47. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  48. else target = Client_ThisServer( );
  49. /* Prefix ermitteln */
  50. if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
  51. else prefix = Client;
  52. if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  53. /* An anderen Server weiterleiten? */
  54. if( target != Client_ThisServer( ))
  55. {
  56. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
  57. /* forwarden */
  58. IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
  59. return CONNECTED;
  60. }
  61. /* mit Versionsinfo antworten */
  62. if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
  63. if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
  64. if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
  65. if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
  66. IRC_SetPenalty( Client, 1 );
  67. return CONNECTED;
  68. } /* IRC_ADMIN */
  69. GLOBAL bool
  70. IRC_ISON( CLIENT *Client, REQUEST *Req )
  71. {
  72. char rpl[COMMAND_LEN];
  73. CLIENT *c;
  74. char *ptr;
  75. int i;
  76. assert( Client != NULL );
  77. assert( Req != NULL );
  78. /* Falsche Anzahl Parameter? */
  79. if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  80. strlcpy( rpl, RPL_ISON_MSG, sizeof rpl );
  81. for( i = 0; i < Req->argc; i++ )
  82. {
  83. ptr = strtok( Req->argv[i], " " );
  84. while( ptr )
  85. {
  86. ngt_TrimStr( ptr );
  87. c = Client_Search( ptr );
  88. if( c && ( Client_Type( c ) == CLIENT_USER ))
  89. {
  90. /* Dieser Nick ist "online" */
  91. strlcat( rpl, ptr, sizeof( rpl ));
  92. strlcat( rpl, " ", sizeof( rpl ));
  93. }
  94. ptr = strtok( NULL, " " );
  95. }
  96. }
  97. ngt_TrimLastChr(rpl, ' ');
  98. return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
  99. } /* IRC_ISON */
  100. GLOBAL bool
  101. IRC_LINKS( CLIENT *Client, REQUEST *Req )
  102. {
  103. CLIENT *target, *from, *c;
  104. char *mask;
  105. assert( Client != NULL );
  106. assert( Req != NULL );
  107. /* Falsche Anzahl Parameter? */
  108. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  109. /* Server-Mask ermitteln */
  110. if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
  111. else mask = "*";
  112. /* Absender ermitteln */
  113. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  114. else from = Client;
  115. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  116. /* An anderen Server forwarden? */
  117. if( Req->argc == 2 )
  118. {
  119. target = Client_Search( Req->argv[0] );
  120. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  121. else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
  122. }
  123. /* Wer ist der Absender? */
  124. if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
  125. else target = Client;
  126. if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  127. c = Client_First( );
  128. while( c )
  129. {
  130. if( Client_Type( c ) == CLIENT_SERVER )
  131. {
  132. 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;
  133. }
  134. c = Client_Next( c );
  135. }
  136. IRC_SetPenalty( target, 1 );
  137. return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
  138. } /* IRC_LINKS */
  139. GLOBAL bool
  140. IRC_LUSERS( CLIENT *Client, REQUEST *Req )
  141. {
  142. CLIENT *target, *from;
  143. assert( Client != NULL );
  144. assert( Req != NULL );
  145. /* Falsche Anzahl Parameter? */
  146. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  147. /* Absender ermitteln */
  148. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  149. else from = Client;
  150. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  151. /* An anderen Server forwarden? */
  152. if( Req->argc == 2 )
  153. {
  154. target = Client_Search( Req->argv[1] );
  155. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
  156. else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
  157. }
  158. /* Wer ist der Absender? */
  159. if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
  160. else target = Client;
  161. if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  162. IRC_Send_LUSERS( target );
  163. IRC_SetPenalty( target, 1 );
  164. return CONNECTED;
  165. } /* IRC_LUSERS */
  166. GLOBAL bool
  167. IRC_MOTD( CLIENT *Client, REQUEST *Req )
  168. {
  169. CLIENT *from, *target;
  170. assert( Client != NULL );
  171. assert( Req != NULL );
  172. /* Falsche Anzahl Parameter? */
  173. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  174. /* From aus Prefix ermitteln */
  175. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  176. else from = Client;
  177. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  178. if( Req->argc == 1 )
  179. {
  180. /* an anderen Server forwarden */
  181. target = Client_Search( Req->argv[0] );
  182. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  183. if( target != Client_ThisServer( ))
  184. {
  185. /* Ok, anderer Server ist das Ziel: forwarden */
  186. return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
  187. }
  188. }
  189. IRC_SetPenalty( from, 3 );
  190. return IRC_Show_MOTD( from );
  191. } /* IRC_MOTD */
  192. GLOBAL bool
  193. IRC_NAMES( CLIENT *Client, REQUEST *Req )
  194. {
  195. char rpl[COMMAND_LEN], *ptr;
  196. CLIENT *target, *from, *c;
  197. CHANNEL *chan;
  198. assert( Client != NULL );
  199. assert( Req != NULL );
  200. /* Falsche Anzahl Parameter? */
  201. if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  202. /* From aus Prefix ermitteln */
  203. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  204. else from = Client;
  205. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  206. if( Req->argc == 2 )
  207. {
  208. /* an anderen Server forwarden */
  209. target = Client_Search( Req->argv[1] );
  210. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
  211. if( target != Client_ThisServer( ))
  212. {
  213. /* Ok, anderer Server ist das Ziel: forwarden */
  214. return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
  215. }
  216. }
  217. if( Req->argc > 0 )
  218. {
  219. /* bestimmte Channels durchgehen */
  220. ptr = strtok( Req->argv[0], "," );
  221. while( ptr )
  222. {
  223. chan = Channel_Search( ptr );
  224. if( chan )
  225. {
  226. /* Namen ausgeben */
  227. if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
  228. }
  229. if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
  230. /* naechsten Namen ermitteln */
  231. ptr = strtok( NULL, "," );
  232. }
  233. return CONNECTED;
  234. }
  235. /* alle Channels durchgehen */
  236. chan = Channel_First( );
  237. while( chan )
  238. {
  239. /* Namen ausgeben */
  240. if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
  241. /* naechster Channel */
  242. chan = Channel_Next( chan );
  243. }
  244. /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
  245. c = Client_First( );
  246. snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
  247. while( c )
  248. {
  249. if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
  250. {
  251. /* Okay, das ist ein User: anhaengen */
  252. if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
  253. strlcat( rpl, Client_ID( c ), sizeof( rpl ));
  254. if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
  255. {
  256. /* Zeile wird zu lang: senden! */
  257. if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
  258. snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
  259. }
  260. }
  261. /* naechster Client */
  262. c = Client_Next( c );
  263. }
  264. if( rpl[strlen( rpl ) - 1] != ':')
  265. {
  266. /* es wurden User gefunden */
  267. if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
  268. }
  269. IRC_SetPenalty( from, 1 );
  270. return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
  271. } /* IRC_NAMES */
  272. GLOBAL bool
  273. IRC_STATS( CLIENT *Client, REQUEST *Req )
  274. {
  275. CLIENT *from, *target, *cl;
  276. CONN_ID con;
  277. char query;
  278. COMMAND *cmd;
  279. assert( Client != NULL );
  280. assert( Req != NULL );
  281. /* Falsche Anzahl Parameter? */
  282. if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  283. /* From aus Prefix ermitteln */
  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. /* an anderen Server forwarden */
  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. {
  294. /* Ok, anderer Server ist das Ziel: forwarden */
  295. return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
  296. }
  297. }
  298. if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
  299. else query = '*';
  300. switch ( query )
  301. {
  302. case 'l': /* Links */
  303. case 'L':
  304. con = Conn_First( );
  305. while( con != NONE )
  306. {
  307. cl = Client_GetFromConn( con );
  308. if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
  309. {
  310. /* Server link or our own connection */
  311. #ifdef ZLIB
  312. if( Conn_Options( con ) & CONN_ZIP )
  313. {
  314. if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFOZIP_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Zip_SendBytes( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Zip_RecvBytes( con ), Conn_RecvBytes( con ), (long)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
  315. }
  316. else
  317. #endif
  318. {
  319. if( ! IRC_WriteStrClient( from, RPL_STATSLINKINFO_MSG, Client_ID( from ), Client_Mask( cl ), Conn_SendQ( con ), Conn_SendMsg( con ), Conn_SendBytes( con ), Conn_RecvMsg( con ), Conn_RecvBytes( con ), (long)( time( NULL ) - Conn_StartTime( con )))) return DISCONNECTED;
  320. }
  321. }
  322. con = Conn_Next( con );
  323. }
  324. break;
  325. case 'm': /* IRC-Befehle */
  326. case 'M':
  327. cmd = Parse_GetCommandStruct( );
  328. while( cmd->name )
  329. {
  330. if( cmd->lcount > 0 || cmd->rcount > 0 )
  331. {
  332. if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
  333. }
  334. cmd++;
  335. }
  336. break;
  337. }
  338. IRC_SetPenalty( from, 2 );
  339. return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
  340. } /* IRC_STATS */
  341. GLOBAL bool
  342. IRC_TIME( CLIENT *Client, REQUEST *Req )
  343. {
  344. CLIENT *from, *target;
  345. char t_str[64];
  346. time_t t;
  347. assert( Client != NULL );
  348. assert( Req != NULL );
  349. /* Falsche Anzahl Parameter? */
  350. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  351. /* From aus Prefix ermitteln */
  352. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  353. else from = Client;
  354. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  355. if( Req->argc == 1 )
  356. {
  357. /* an anderen Server forwarden */
  358. target = Client_Search( Req->argv[0] );
  359. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
  360. if( target != Client_ThisServer( ))
  361. {
  362. /* Ok, anderer Server ist das Ziel: forwarden */
  363. return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
  364. }
  365. }
  366. t = time( NULL );
  367. (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
  368. return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
  369. } /* IRC_TIME */
  370. GLOBAL bool
  371. IRC_USERHOST( CLIENT *Client, REQUEST *Req )
  372. {
  373. char rpl[COMMAND_LEN];
  374. CLIENT *c;
  375. int max, i;
  376. assert( Client != NULL );
  377. assert( Req != NULL );
  378. /* Falsche Anzahl Parameter? */
  379. if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  380. if( Req->argc > 5 ) max = 5;
  381. else max = Req->argc;
  382. strlcpy( rpl, RPL_USERHOST_MSG, sizeof rpl );
  383. for( i = 0; i < max; i++ )
  384. {
  385. c = Client_Search( Req->argv[i] );
  386. if( c && ( Client_Type( c ) == CLIENT_USER ))
  387. {
  388. /* Dieser Nick ist "online" */
  389. strlcat( rpl, Client_ID( c ), sizeof( rpl ));
  390. if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
  391. strlcat( rpl, "=", sizeof( rpl ));
  392. if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
  393. else strlcat( rpl, "+", sizeof( rpl ));
  394. strlcat( rpl, Client_User( c ), sizeof( rpl ));
  395. strlcat( rpl, "@", sizeof( rpl ));
  396. strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
  397. strlcat( rpl, " ", sizeof( rpl ));
  398. }
  399. }
  400. ngt_TrimLastChr( rpl, ' ');
  401. return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
  402. } /* IRC_USERHOST */
  403. GLOBAL bool
  404. IRC_VERSION( CLIENT *Client, REQUEST *Req )
  405. {
  406. CLIENT *target, *prefix;
  407. #ifdef CVSDATE
  408. char ver[12], vertxt[30];
  409. #endif
  410. assert( Client != NULL );
  411. assert( Req != NULL );
  412. /* Falsche Anzahl Parameter? */
  413. if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  414. /* Ziel suchen */
  415. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  416. else target = Client_ThisServer( );
  417. /* Prefix ermitteln */
  418. if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
  419. else prefix = Client;
  420. if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  421. /* An anderen Server weiterleiten? */
  422. if( target != Client_ThisServer( ))
  423. {
  424. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
  425. /* forwarden */
  426. IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
  427. return CONNECTED;
  428. }
  429. /* mit Versionsinfo antworten */
  430. IRC_SetPenalty( Client, 1 );
  431. #ifdef CVSDATE
  432. strlcpy( ver, CVSDATE, sizeof( ver ));
  433. strncpy( ver + 4, ver + 5, 2 );
  434. strncpy( ver + 6, ver + 8, 3 );
  435. snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
  436. return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
  437. #else
  438. return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
  439. #endif
  440. } /* IRC_VERSION */
  441. GLOBAL bool
  442. IRC_WHO( CLIENT *Client, REQUEST *Req )
  443. {
  444. bool ok, only_ops;
  445. char flags[8], *ptr;
  446. CL2CHAN *cl2chan;
  447. CHANNEL *chan, *cn;
  448. CLIENT *c;
  449. assert( Client != NULL );
  450. assert( Req != NULL );
  451. /* Falsche Anzahl Parameter? */
  452. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  453. only_ops = false;
  454. chan = NULL;
  455. if( Req->argc == 2 )
  456. {
  457. /* Nur OPs anzeigen? */
  458. if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
  459. #ifdef STRICT_RFC
  460. else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  461. #endif
  462. }
  463. if( Req->argc >= 1 )
  464. {
  465. /* wurde ein Channel oder Nick-Mask angegeben? */
  466. chan = Channel_Search( Req->argv[0] );
  467. }
  468. if( chan )
  469. {
  470. /* User eines Channels ausgeben */
  471. if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
  472. }
  473. c = Client_First( );
  474. while( c )
  475. {
  476. if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
  477. {
  478. ok = false;
  479. if( Req->argc == 0 ) ok = true;
  480. else
  481. {
  482. if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
  483. else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
  484. }
  485. if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
  486. {
  487. /* Get flags */
  488. strcpy( flags, "H" );
  489. if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
  490. /* Search suitable channel */
  491. cl2chan = Channel_FirstChannelOf( c );
  492. while( cl2chan )
  493. {
  494. cn = Channel_GetChannel( cl2chan );
  495. if( Channel_IsMemberOf( cn, Client ) ||
  496. ! strchr( Channel_Modes( cn ), 's' ))
  497. {
  498. ptr = Channel_Name( cn );
  499. break;
  500. }
  501. cl2chan = Channel_NextChannelOf( c, cl2chan );
  502. }
  503. if( ! cl2chan ) ptr = "*";
  504. if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), ptr, Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
  505. }
  506. }
  507. /* naechster Client */
  508. c = Client_Next( c );
  509. }
  510. if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
  511. else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
  512. else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
  513. } /* IRC_WHO */
  514. GLOBAL bool
  515. IRC_WHOIS( CLIENT *Client, REQUEST *Req )
  516. {
  517. CLIENT *from, *target, *c;
  518. char str[LINE_LEN + 1];
  519. CL2CHAN *cl2chan;
  520. CHANNEL *chan;
  521. assert( Client != NULL );
  522. assert( Req != NULL );
  523. /* Bad number of parameters? */
  524. if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  525. /* Search client */
  526. c = Client_Search( Req->argv[Req->argc - 1] );
  527. if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
  528. /* Search sender of the WHOIS */
  529. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  530. else from = Client;
  531. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  532. /* Forward to other server? */
  533. if( Req->argc > 1 )
  534. {
  535. /* Search target server (can be specified as nick of that server!) */
  536. target = Client_Search( Req->argv[0] );
  537. if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  538. }
  539. else target = Client_ThisServer( );
  540. assert( target != NULL );
  541. 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] );
  542. /* Nick, user and name */
  543. if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
  544. /* Server */
  545. 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;
  546. /* Channels */
  547. snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
  548. cl2chan = Channel_FirstChannelOf( c );
  549. while( cl2chan )
  550. {
  551. chan = Channel_GetChannel( cl2chan );
  552. assert( chan != NULL );
  553. /* next */
  554. cl2chan = Channel_NextChannelOf( c, cl2chan );
  555. /* Secret channel? */
  556. if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
  557. /* Concatenate channel names */
  558. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  559. if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
  560. else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
  561. strlcat( str, Channel_Name( chan ), sizeof( str ));
  562. if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
  563. {
  564. /* Line becomes too long: send it! */
  565. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  566. snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
  567. }
  568. }
  569. if( str[strlen( str ) - 1] != ':')
  570. {
  571. /* There is data left to send: */
  572. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  573. }
  574. /* IRC-Operator? */
  575. if( Client_HasMode( c, 'o' ))
  576. {
  577. if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
  578. }
  579. /* Idle (only local clients) */
  580. if( Client_Conn( c ) > NONE )
  581. {
  582. if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
  583. }
  584. /* Away? */
  585. if( Client_HasMode( c, 'a' ))
  586. {
  587. if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
  588. }
  589. /* End of Whois */
  590. return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
  591. } /* IRC_WHOIS */
  592. /**
  593. * IRC "WHOWAS" function.
  594. * This function implements the IRC command "WHOWHAS". It handles local
  595. * requests and request that should be forwarded to other servers.
  596. */
  597. GLOBAL bool
  598. IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
  599. {
  600. CLIENT *target, *prefix;
  601. WHOWAS *whowas;
  602. int max, last, count, i;
  603. char t_str[60];
  604. assert( Client != NULL );
  605. assert( Req != NULL );
  606. /* Wrong number of parameters? */
  607. if(( Req->argc < 1 ) || ( Req->argc > 3 ))
  608. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  609. Client_ID( Client ), Req->command );
  610. /* Search taget */
  611. if( Req->argc == 3 )
  612. target = Client_Search( Req->argv[2] );
  613. else
  614. target = Client_ThisServer( );
  615. /* Get prefix */
  616. if( Client_Type( Client ) == CLIENT_SERVER )
  617. prefix = Client_Search( Req->prefix );
  618. else
  619. prefix = Client;
  620. if( ! prefix )
  621. return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
  622. Client_ID( Client ), Req->prefix );
  623. /* Forward to other server? */
  624. if( target != Client_ThisServer( ))
  625. {
  626. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
  627. return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
  628. Client_ID( prefix ),
  629. Req->argv[2] );
  630. /* Forward */
  631. IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
  632. Req->argv[0], Req->argv[1],
  633. Req->argv[2] );
  634. return CONNECTED;
  635. }
  636. whowas = Client_GetWhowas( );
  637. last = Client_GetLastWhowasIndex( );
  638. if( last < 0 ) last = 0;
  639. if( Req->argc > 1 )
  640. {
  641. max = atoi( Req->argv[1] );
  642. if( max < 1 ) max = MAX_WHOWAS;
  643. }
  644. else
  645. max = DEFAULT_WHOWAS;
  646. i = last;
  647. count = 0;
  648. do
  649. {
  650. /* Used entry? */
  651. if( whowas[i].time > 0 &&
  652. strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
  653. {
  654. (void)strftime( t_str, sizeof(t_str),
  655. "%a %b %d %H:%M:%S %Y",
  656. localtime( &whowas[i].time ));
  657. if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
  658. Client_ID( prefix ),
  659. whowas[i].id,
  660. whowas[i].user,
  661. whowas[i].host,
  662. whowas[i].info ))
  663. return DISCONNECTED;
  664. if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
  665. Client_ID( prefix ),
  666. whowas[i].id,
  667. whowas[i].server, t_str ))
  668. return DISCONNECTED;
  669. count++;
  670. if( count >= max ) break;
  671. }
  672. /* previos entry */
  673. i--;
  674. /* "underflow", wrap around */
  675. if( i < 0 ) i = MAX_WHOWAS - 1;
  676. } while( i != last );
  677. return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
  678. Client_ID( prefix ), Req->argv[0] );
  679. } /* IRC_WHOWAS */
  680. GLOBAL bool
  681. IRC_Send_LUSERS( CLIENT *Client )
  682. {
  683. long cnt;
  684. assert( Client != NULL );
  685. /* Users, services and serevers in the network */
  686. if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
  687. /* Number of IRC operators */
  688. cnt = Client_OperCount( );
  689. if( cnt > 0 )
  690. {
  691. if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
  692. }
  693. /* Unknown connections */
  694. cnt = Client_UnknownCount( );
  695. if( cnt > 0 )
  696. {
  697. if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
  698. }
  699. /* Number of created channels */
  700. if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
  701. /* Number of local users, services and servers */
  702. if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
  703. #ifndef STRICT_RFC
  704. /* Maximum number of local users */
  705. if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
  706. /* Maximum number of users in the network */
  707. if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
  708. #endif
  709. return CONNECTED;
  710. } /* IRC_Send_LUSERS */
  711. GLOBAL bool
  712. IRC_Show_MOTD( CLIENT *Client )
  713. {
  714. bool ok;
  715. char line[127];
  716. FILE *fd;
  717. assert( Client != NULL );
  718. if( Conf_MotdPhrase[0] )
  719. {
  720. if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
  721. if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), Conf_MotdPhrase )) return DISCONNECTED;
  722. return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
  723. }
  724. fd = fopen( Conf_MotdFile, "r" );
  725. if( ! fd )
  726. {
  727. Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
  728. return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
  729. }
  730. if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
  731. while( true )
  732. {
  733. if( ! fgets( line, sizeof( line ), fd )) break;
  734. ngt_TrimLastChr( line, '\n');
  735. if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
  736. {
  737. fclose( fd );
  738. return false;
  739. }
  740. }
  741. ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
  742. fclose( fd );
  743. return ok;
  744. } /* IRC_Show_MOTD */
  745. GLOBAL bool
  746. IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
  747. {
  748. bool is_visible, is_member;
  749. char str[LINE_LEN + 1];
  750. CL2CHAN *cl2chan;
  751. CLIENT *cl;
  752. assert( Client != NULL );
  753. assert( Chan != NULL );
  754. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  755. else is_member = false;
  756. /* Secret channel? */
  757. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  758. /* Alle Mitglieder suchen */
  759. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  760. cl2chan = Channel_FirstMember( Chan );
  761. while( cl2chan )
  762. {
  763. cl = Channel_GetClient( cl2chan );
  764. if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
  765. else is_visible = true;
  766. if( is_member || is_visible )
  767. {
  768. /* Nick anhaengen */
  769. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  770. if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
  771. else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
  772. strlcat( str, Client_ID( cl ), sizeof( str ));
  773. if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
  774. {
  775. /* Zeile wird zu lang: senden! */
  776. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  777. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  778. }
  779. }
  780. /* naechstes Mitglied suchen */
  781. cl2chan = Channel_NextMember( Chan, cl2chan );
  782. }
  783. if( str[strlen( str ) - 1] != ':')
  784. {
  785. /* Es sind noch Daten da, die gesendet werden muessen */
  786. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  787. }
  788. return CONNECTED;
  789. } /* IRC_Send_NAMES */
  790. GLOBAL bool
  791. IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
  792. {
  793. bool is_visible, is_member;
  794. CL2CHAN *cl2chan;
  795. char flags[8];
  796. CLIENT *c;
  797. assert( Client != NULL );
  798. assert( Chan != NULL );
  799. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  800. else is_member = false;
  801. /* Secret channel? */
  802. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  803. /* Alle Mitglieder suchen */
  804. cl2chan = Channel_FirstMember( Chan );
  805. while( cl2chan )
  806. {
  807. c = Channel_GetClient( cl2chan );
  808. if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
  809. else is_visible = true;
  810. if( is_member || is_visible )
  811. {
  812. /* Flags zusammenbasteln */
  813. strcpy( flags, "H" );
  814. if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
  815. if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
  816. else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
  817. /* ausgeben */
  818. if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
  819. {
  820. if( ! IRC_WriteStrClient( Client, RPL_WHOREPLY_MSG, Client_ID( Client ), Channel_Name( Chan ), Client_User( c ), Client_Hostname( c ), Client_ID( Client_Introducer( c )), Client_ID( c ), flags, Client_Hops( c ), Client_Info( c ))) return DISCONNECTED;
  821. }
  822. }
  823. /* naechstes Mitglied suchen */
  824. cl2chan = Channel_NextMember( Chan, cl2chan );
  825. }
  826. return CONNECTED;
  827. } /* IRC_Send_WHO */
  828. /* -eof- */