irc-info.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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.33.2.2 2006/12/02 14:26:53 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 = Conn_GetClient( 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];
  446. const char *ptr;
  447. CL2CHAN *cl2chan;
  448. CHANNEL *chan, *cn;
  449. CLIENT *c;
  450. assert( Client != NULL );
  451. assert( Req != NULL );
  452. /* Falsche Anzahl Parameter? */
  453. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  454. only_ops = false;
  455. chan = NULL;
  456. if( Req->argc == 2 )
  457. {
  458. /* Nur OPs anzeigen? */
  459. if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
  460. #ifdef STRICT_RFC
  461. else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  462. #endif
  463. }
  464. if( Req->argc >= 1 )
  465. {
  466. /* wurde ein Channel oder Nick-Mask angegeben? */
  467. chan = Channel_Search( Req->argv[0] );
  468. }
  469. if( chan )
  470. {
  471. /* User eines Channels ausgeben */
  472. if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
  473. }
  474. c = Client_First( );
  475. while( c )
  476. {
  477. if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
  478. {
  479. ok = false;
  480. if( Req->argc == 0 ) ok = true;
  481. else
  482. {
  483. if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
  484. else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
  485. }
  486. if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
  487. {
  488. /* Get flags */
  489. strcpy( flags, "H" );
  490. if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
  491. /* Search suitable channel */
  492. cl2chan = Channel_FirstChannelOf( c );
  493. while( cl2chan )
  494. {
  495. cn = Channel_GetChannel( cl2chan );
  496. if( Channel_IsMemberOf( cn, Client ) ||
  497. ! strchr( Channel_Modes( cn ), 's' ))
  498. {
  499. ptr = Channel_Name( cn );
  500. break;
  501. }
  502. cl2chan = Channel_NextChannelOf( c, cl2chan );
  503. }
  504. if( ! cl2chan ) ptr = "*";
  505. 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;
  506. }
  507. }
  508. /* naechster Client */
  509. c = Client_Next( c );
  510. }
  511. if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
  512. else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
  513. else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
  514. } /* IRC_WHO */
  515. GLOBAL bool
  516. IRC_WHOIS( CLIENT *Client, REQUEST *Req )
  517. {
  518. CLIENT *from, *target, *c;
  519. char str[LINE_LEN + 1];
  520. CL2CHAN *cl2chan;
  521. CHANNEL *chan;
  522. assert( Client != NULL );
  523. assert( Req != NULL );
  524. /* Bad number of parameters? */
  525. if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  526. /* Search client */
  527. c = Client_Search( Req->argv[Req->argc - 1] );
  528. if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
  529. /* Search sender of the WHOIS */
  530. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  531. else from = Client;
  532. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  533. /* Forward to other server? */
  534. if( Req->argc > 1 )
  535. {
  536. /* Search target server (can be specified as nick of that server!) */
  537. target = Client_Search( Req->argv[0] );
  538. if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  539. }
  540. else target = Client_ThisServer( );
  541. assert( target != NULL );
  542. 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] );
  543. /* Nick, user and name */
  544. if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
  545. /* Server */
  546. 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;
  547. /* Channels */
  548. snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
  549. cl2chan = Channel_FirstChannelOf( c );
  550. while( cl2chan )
  551. {
  552. chan = Channel_GetChannel( cl2chan );
  553. assert( chan != NULL );
  554. /* next */
  555. cl2chan = Channel_NextChannelOf( c, cl2chan );
  556. /* Secret channel? */
  557. if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
  558. /* Concatenate channel names */
  559. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  560. if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
  561. else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
  562. strlcat( str, Channel_Name( chan ), sizeof( str ));
  563. if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
  564. {
  565. /* Line becomes too long: send it! */
  566. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  567. snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
  568. }
  569. }
  570. if( str[strlen( str ) - 1] != ':')
  571. {
  572. /* There is data left to send: */
  573. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  574. }
  575. /* IRC-Operator? */
  576. if( Client_HasMode( c, 'o' ))
  577. {
  578. if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
  579. }
  580. /* Idle (only local clients) */
  581. if( Client_Conn( c ) > NONE )
  582. {
  583. if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
  584. }
  585. /* Away? */
  586. if( Client_HasMode( c, 'a' ))
  587. {
  588. if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
  589. }
  590. /* End of Whois */
  591. return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
  592. } /* IRC_WHOIS */
  593. /**
  594. * IRC "WHOWAS" function.
  595. * This function implements the IRC command "WHOWHAS". It handles local
  596. * requests and request that should be forwarded to other servers.
  597. */
  598. GLOBAL bool
  599. IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
  600. {
  601. CLIENT *target, *prefix;
  602. WHOWAS *whowas;
  603. int max, last, count, i;
  604. char t_str[60];
  605. assert( Client != NULL );
  606. assert( Req != NULL );
  607. /* Wrong number of parameters? */
  608. if(( Req->argc < 1 ) || ( Req->argc > 3 ))
  609. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  610. Client_ID( Client ), Req->command );
  611. /* Search taget */
  612. if( Req->argc == 3 )
  613. target = Client_Search( Req->argv[2] );
  614. else
  615. target = Client_ThisServer( );
  616. /* Get prefix */
  617. if( Client_Type( Client ) == CLIENT_SERVER )
  618. prefix = Client_Search( Req->prefix );
  619. else
  620. prefix = Client;
  621. if( ! prefix )
  622. return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
  623. Client_ID( Client ), Req->prefix );
  624. /* Forward to other server? */
  625. if( target != Client_ThisServer( ))
  626. {
  627. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
  628. return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
  629. Client_ID( prefix ),
  630. Req->argv[2] );
  631. /* Forward */
  632. IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
  633. Req->argv[0], Req->argv[1],
  634. Req->argv[2] );
  635. return CONNECTED;
  636. }
  637. whowas = Client_GetWhowas( );
  638. last = Client_GetLastWhowasIndex( );
  639. if( last < 0 ) last = 0;
  640. if( Req->argc > 1 )
  641. {
  642. max = atoi( Req->argv[1] );
  643. if( max < 1 ) max = MAX_WHOWAS;
  644. }
  645. else
  646. max = DEFAULT_WHOWAS;
  647. i = last;
  648. count = 0;
  649. do
  650. {
  651. /* Used entry? */
  652. if( whowas[i].time > 0 &&
  653. strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
  654. {
  655. (void)strftime( t_str, sizeof(t_str),
  656. "%a %b %d %H:%M:%S %Y",
  657. localtime( &whowas[i].time ));
  658. if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
  659. Client_ID( prefix ),
  660. whowas[i].id,
  661. whowas[i].user,
  662. whowas[i].host,
  663. whowas[i].info ))
  664. return DISCONNECTED;
  665. if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
  666. Client_ID( prefix ),
  667. whowas[i].id,
  668. whowas[i].server, t_str ))
  669. return DISCONNECTED;
  670. count++;
  671. if( count >= max ) break;
  672. }
  673. /* previos entry */
  674. i--;
  675. /* "underflow", wrap around */
  676. if( i < 0 ) i = MAX_WHOWAS - 1;
  677. } while( i != last );
  678. return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
  679. Client_ID( prefix ), Req->argv[0] );
  680. } /* IRC_WHOWAS */
  681. GLOBAL bool
  682. IRC_Send_LUSERS( CLIENT *Client )
  683. {
  684. unsigned long cnt;
  685. #ifndef STRICT_RFC
  686. unsigned long max;
  687. #endif
  688. assert( Client != NULL );
  689. /* Users, services and serevers in the network */
  690. if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
  691. /* Number of IRC operators */
  692. cnt = Client_OperCount( );
  693. if( cnt > 0 )
  694. {
  695. if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
  696. }
  697. /* Unknown connections */
  698. cnt = Client_UnknownCount( );
  699. if( cnt > 0 )
  700. {
  701. if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
  702. }
  703. /* Number of created channels */
  704. if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
  705. /* Number of local users, services and servers */
  706. if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
  707. #ifndef STRICT_RFC
  708. /* Maximum number of local users */
  709. cnt = Client_MyUserCount();
  710. max = Client_MyMaxUserCount();
  711. if (! IRC_WriteStrClient(Client, RPL_LOCALUSERS_MSG, Client_ID(Client),
  712. cnt, max, cnt, max))
  713. return DISCONNECTED;
  714. /* Maximum number of users in the network */
  715. cnt = Client_UserCount();
  716. max = Client_MaxUserCount();
  717. if(! IRC_WriteStrClient(Client, RPL_NETUSERS_MSG, Client_ID(Client),
  718. cnt, max, cnt, max))
  719. return DISCONNECTED;
  720. #endif
  721. return CONNECTED;
  722. } /* IRC_Send_LUSERS */
  723. static bool Show_MOTD_Start(CLIENT *Client)
  724. {
  725. return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
  726. Client_ID( Client ), Client_ID( Client_ThisServer( )));
  727. }
  728. static bool Show_MOTD_Sendline(CLIENT *Client, const char *msg)
  729. {
  730. return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
  731. }
  732. static bool Show_MOTD_End(CLIENT *Client)
  733. {
  734. return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
  735. }
  736. GLOBAL bool
  737. IRC_Show_MOTD( CLIENT *Client )
  738. {
  739. char line[127];
  740. FILE *fd;
  741. assert( Client != NULL );
  742. if (Conf_MotdPhrase[0]) {
  743. if (!Show_MOTD_Start(Client))
  744. return DISCONNECTED;
  745. if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
  746. return DISCONNECTED;
  747. return Show_MOTD_End(Client);
  748. }
  749. fd = fopen( Conf_MotdFile, "r" );
  750. if( ! fd ) {
  751. Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
  752. return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
  753. }
  754. if (!Show_MOTD_Start( Client )) {
  755. fclose(fd);
  756. return false;
  757. }
  758. while (fgets( line, (int)sizeof line, fd )) {
  759. ngt_TrimLastChr( line, '\n');
  760. if( ! Show_MOTD_Sendline( Client, line)) {
  761. fclose( fd );
  762. return false;
  763. }
  764. }
  765. fclose(fd);
  766. return Show_MOTD_End(Client);
  767. } /* IRC_Show_MOTD */
  768. GLOBAL bool
  769. IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
  770. {
  771. bool is_visible, is_member;
  772. char str[LINE_LEN + 1];
  773. CL2CHAN *cl2chan;
  774. CLIENT *cl;
  775. assert( Client != NULL );
  776. assert( Chan != NULL );
  777. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  778. else is_member = false;
  779. /* Secret channel? */
  780. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  781. /* Alle Mitglieder suchen */
  782. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  783. cl2chan = Channel_FirstMember( Chan );
  784. while( cl2chan )
  785. {
  786. cl = Channel_GetClient( cl2chan );
  787. if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
  788. else is_visible = true;
  789. if( is_member || is_visible )
  790. {
  791. /* Nick anhaengen */
  792. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  793. if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
  794. else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
  795. strlcat( str, Client_ID( cl ), sizeof( str ));
  796. if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
  797. {
  798. /* Zeile wird zu lang: senden! */
  799. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  800. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  801. }
  802. }
  803. /* naechstes Mitglied suchen */
  804. cl2chan = Channel_NextMember( Chan, cl2chan );
  805. }
  806. if( str[strlen( str ) - 1] != ':')
  807. {
  808. /* Es sind noch Daten da, die gesendet werden muessen */
  809. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  810. }
  811. return CONNECTED;
  812. } /* IRC_Send_NAMES */
  813. GLOBAL bool
  814. IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
  815. {
  816. bool is_visible, is_member;
  817. CL2CHAN *cl2chan;
  818. char flags[8];
  819. CLIENT *c;
  820. assert( Client != NULL );
  821. assert( Chan != NULL );
  822. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  823. else is_member = false;
  824. /* Secret channel? */
  825. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  826. /* Alle Mitglieder suchen */
  827. cl2chan = Channel_FirstMember( Chan );
  828. while( cl2chan )
  829. {
  830. c = Channel_GetClient( cl2chan );
  831. if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
  832. else is_visible = true;
  833. if( is_member || is_visible )
  834. {
  835. /* Flags zusammenbasteln */
  836. strcpy( flags, "H" );
  837. if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
  838. if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
  839. else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
  840. /* ausgeben */
  841. if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
  842. {
  843. 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;
  844. }
  845. }
  846. /* naechstes Mitglied suchen */
  847. cl2chan = Channel_NextMember( Chan, cl2chan );
  848. }
  849. return CONNECTED;
  850. } /* IRC_Send_WHO */
  851. /* -eof- */