irc-info.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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.1 2006/09/16 13:49:15 alex 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], *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. static bool Show_MOTD_Start(CLIENT *Client)
  712. {
  713. return IRC_WriteStrClient(Client, RPL_MOTDSTART_MSG,
  714. Client_ID( Client ), Client_ID( Client_ThisServer( )));
  715. }
  716. static bool Show_MOTD_Sendline(CLIENT *Client, const char *msg)
  717. {
  718. return IRC_WriteStrClient(Client, RPL_MOTD_MSG, Client_ID( Client ), msg);
  719. }
  720. static bool Show_MOTD_End(CLIENT *Client)
  721. {
  722. return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
  723. }
  724. GLOBAL bool
  725. IRC_Show_MOTD( CLIENT *Client )
  726. {
  727. char line[127];
  728. FILE *fd;
  729. assert( Client != NULL );
  730. if (Conf_MotdPhrase[0]) {
  731. if (!Show_MOTD_Start(Client))
  732. return DISCONNECTED;
  733. if (!Show_MOTD_Sendline(Client, Conf_MotdPhrase))
  734. return DISCONNECTED;
  735. return Show_MOTD_End(Client);
  736. }
  737. fd = fopen( Conf_MotdFile, "r" );
  738. if( ! fd ) {
  739. Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
  740. return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
  741. }
  742. if (!Show_MOTD_Start( Client )) {
  743. fclose(fd);
  744. return false;
  745. }
  746. while (fgets( line, (int)sizeof line, fd )) {
  747. ngt_TrimLastChr( line, '\n');
  748. if( ! Show_MOTD_Sendline( Client, line)) {
  749. fclose( fd );
  750. return false;
  751. }
  752. }
  753. fclose(fd);
  754. return Show_MOTD_End(Client);
  755. } /* IRC_Show_MOTD */
  756. GLOBAL bool
  757. IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
  758. {
  759. bool is_visible, is_member;
  760. char str[LINE_LEN + 1];
  761. CL2CHAN *cl2chan;
  762. CLIENT *cl;
  763. assert( Client != NULL );
  764. assert( Chan != NULL );
  765. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  766. else is_member = false;
  767. /* Secret channel? */
  768. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  769. /* Alle Mitglieder suchen */
  770. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  771. cl2chan = Channel_FirstMember( Chan );
  772. while( cl2chan )
  773. {
  774. cl = Channel_GetClient( cl2chan );
  775. if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
  776. else is_visible = true;
  777. if( is_member || is_visible )
  778. {
  779. /* Nick anhaengen */
  780. if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
  781. if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
  782. else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
  783. strlcat( str, Client_ID( cl ), sizeof( str ));
  784. if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
  785. {
  786. /* Zeile wird zu lang: senden! */
  787. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  788. snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
  789. }
  790. }
  791. /* naechstes Mitglied suchen */
  792. cl2chan = Channel_NextMember( Chan, cl2chan );
  793. }
  794. if( str[strlen( str ) - 1] != ':')
  795. {
  796. /* Es sind noch Daten da, die gesendet werden muessen */
  797. if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
  798. }
  799. return CONNECTED;
  800. } /* IRC_Send_NAMES */
  801. GLOBAL bool
  802. IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
  803. {
  804. bool is_visible, is_member;
  805. CL2CHAN *cl2chan;
  806. char flags[8];
  807. CLIENT *c;
  808. assert( Client != NULL );
  809. assert( Chan != NULL );
  810. if( Channel_IsMemberOf( Chan, Client )) is_member = true;
  811. else is_member = false;
  812. /* Secret channel? */
  813. if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
  814. /* Alle Mitglieder suchen */
  815. cl2chan = Channel_FirstMember( Chan );
  816. while( cl2chan )
  817. {
  818. c = Channel_GetClient( cl2chan );
  819. if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
  820. else is_visible = true;
  821. if( is_member || is_visible )
  822. {
  823. /* Flags zusammenbasteln */
  824. strcpy( flags, "H" );
  825. if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
  826. if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
  827. else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
  828. /* ausgeben */
  829. if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
  830. {
  831. 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;
  832. }
  833. }
  834. /* naechstes Mitglied suchen */
  835. cl2chan = Channel_NextMember( Chan, cl2chan );
  836. }
  837. return CONNECTED;
  838. } /* IRC_Send_WHO */
  839. /* -eof- */