Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc-info.c,v 1.29 2005/05/16 12:25:15 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <strings.h>
27 #include "ngircd.h"
28 #include "cvs-version.h"
29 #include "conn-func.h"
30 #include "conn-zip.h"
31 #include "client.h"
32 #include "channel.h"
33 #include "resolve.h"
34 #include "conf.h"
35 #include "defines.h"
36 #include "log.h"
37 #include "messages.h"
38 #include "tool.h"
39 #include "parse.h"
40 #include "irc-write.h"
42 #include "exp.h"
43 #include "irc-info.h"
46 GLOBAL bool
47 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
48 {
49 CLIENT *target, *prefix;
51 assert( Client != NULL );
52 assert( Req != NULL );
54 /* Falsche Anzahl Parameter? */
55 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
57 /* Ziel suchen */
58 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
59 else target = Client_ThisServer( );
61 /* Prefix ermitteln */
62 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
63 else prefix = Client;
64 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
66 /* An anderen Server weiterleiten? */
67 if( target != Client_ThisServer( ))
68 {
69 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
71 /* forwarden */
72 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
73 return CONNECTED;
74 }
76 /* mit Versionsinfo antworten */
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
80 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
82 IRC_SetPenalty( Client, 1 );
83 return CONNECTED;
84 } /* IRC_ADMIN */
87 GLOBAL bool
88 IRC_ISON( CLIENT *Client, REQUEST *Req )
89 {
90 char rpl[COMMAND_LEN];
91 CLIENT *c;
92 char *ptr;
93 int i;
95 assert( Client != NULL );
96 assert( Req != NULL );
98 /* Falsche Anzahl Parameter? */
99 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
101 strcpy( rpl, RPL_ISON_MSG );
102 for( i = 0; i < Req->argc; i++ )
104 ptr = strtok( Req->argv[i], " " );
105 while( ptr )
107 ngt_TrimStr( ptr );
108 c = Client_Search( ptr );
109 if( c && ( Client_Type( c ) == CLIENT_USER ))
111 /* Dieser Nick ist "online" */
112 strlcat( rpl, ptr, sizeof( rpl ));
113 strlcat( rpl, " ", sizeof( rpl ));
115 ptr = strtok( NULL, " " );
118 ngt_TrimLastChr(rpl, ' ');
120 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
121 } /* IRC_ISON */
124 GLOBAL bool
125 IRC_LINKS( CLIENT *Client, REQUEST *Req )
127 CLIENT *target, *from, *c;
128 char *mask;
130 assert( Client != NULL );
131 assert( Req != NULL );
133 /* Falsche Anzahl Parameter? */
134 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
136 /* Server-Mask ermitteln */
137 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
138 else mask = "*";
140 /* Absender ermitteln */
141 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
142 else from = Client;
143 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
145 /* An anderen Server forwarden? */
146 if( Req->argc == 2 )
148 target = Client_Search( Req->argv[0] );
149 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
150 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
153 /* Wer ist der Absender? */
154 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
155 else target = Client;
156 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
158 c = Client_First( );
159 while( c )
161 if( Client_Type( c ) == CLIENT_SERVER )
163 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;
165 c = Client_Next( c );
168 IRC_SetPenalty( target, 1 );
169 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
170 } /* IRC_LINKS */
173 GLOBAL bool
174 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
176 CLIENT *target, *from;
178 assert( Client != NULL );
179 assert( Req != NULL );
181 /* Falsche Anzahl Parameter? */
182 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
184 /* Absender ermitteln */
185 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
186 else from = Client;
187 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
189 /* An anderen Server forwarden? */
190 if( Req->argc == 2 )
192 target = Client_Search( Req->argv[1] );
193 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
194 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
197 /* Wer ist der Absender? */
198 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
199 else target = Client;
200 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
202 IRC_Send_LUSERS( target );
204 IRC_SetPenalty( target, 1 );
205 return CONNECTED;
206 } /* IRC_LUSERS */
209 GLOBAL bool
210 IRC_MOTD( CLIENT *Client, REQUEST *Req )
212 CLIENT *from, *target;
214 assert( Client != NULL );
215 assert( Req != NULL );
217 /* Falsche Anzahl Parameter? */
218 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
220 /* From aus Prefix ermitteln */
221 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
222 else from = Client;
223 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
225 if( Req->argc == 1 )
227 /* an anderen Server forwarden */
228 target = Client_Search( Req->argv[0] );
229 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
231 if( target != Client_ThisServer( ))
233 /* Ok, anderer Server ist das Ziel: forwarden */
234 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
238 IRC_SetPenalty( from, 3 );
239 return IRC_Show_MOTD( from );
240 } /* IRC_MOTD */
243 GLOBAL bool
244 IRC_NAMES( CLIENT *Client, REQUEST *Req )
246 char rpl[COMMAND_LEN], *ptr;
247 CLIENT *target, *from, *c;
248 CHANNEL *chan;
250 assert( Client != NULL );
251 assert( Req != NULL );
253 /* Falsche Anzahl Parameter? */
254 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
256 /* From aus Prefix ermitteln */
257 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
258 else from = Client;
259 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
261 if( Req->argc == 2 )
263 /* an anderen Server forwarden */
264 target = Client_Search( Req->argv[1] );
265 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
267 if( target != Client_ThisServer( ))
269 /* Ok, anderer Server ist das Ziel: forwarden */
270 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
274 if( Req->argc > 0 )
276 /* bestimmte Channels durchgehen */
277 ptr = strtok( Req->argv[0], "," );
278 while( ptr )
280 chan = Channel_Search( ptr );
281 if( chan )
283 /* Namen ausgeben */
284 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
286 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
288 /* naechsten Namen ermitteln */
289 ptr = strtok( NULL, "," );
291 return CONNECTED;
294 /* alle Channels durchgehen */
295 chan = Channel_First( );
296 while( chan )
298 /* Namen ausgeben */
299 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
301 /* naechster Channel */
302 chan = Channel_Next( chan );
305 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
306 c = Client_First( );
307 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
308 while( c )
310 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
312 /* Okay, das ist ein User: anhaengen */
313 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
314 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
316 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
318 /* Zeile wird zu lang: senden! */
319 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
320 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
324 /* naechster Client */
325 c = Client_Next( c );
327 if( rpl[strlen( rpl ) - 1] != ':')
329 /* es wurden User gefunden */
330 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
333 IRC_SetPenalty( from, 1 );
334 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
335 } /* IRC_NAMES */
338 GLOBAL bool
339 IRC_STATS( CLIENT *Client, REQUEST *Req )
341 CLIENT *from, *target, *cl;
342 CONN_ID con;
343 char query;
344 COMMAND *cmd;
346 assert( Client != NULL );
347 assert( Req != NULL );
349 /* Falsche Anzahl Parameter? */
350 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
352 /* From aus Prefix ermitteln */
353 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
354 else from = Client;
355 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
357 if( Req->argc == 2 )
359 /* an anderen Server forwarden */
360 target = Client_Search( Req->argv[1] );
361 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
363 if( target != Client_ThisServer( ))
365 /* Ok, anderer Server ist das Ziel: forwarden */
366 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
370 if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
371 else query = '*';
373 switch ( query )
375 case 'l': /* Links */
376 case 'L':
377 con = Conn_First( );
378 while( con != NONE )
380 cl = Client_GetFromConn( con );
381 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
383 /* Server link or our own connection */
384 #ifdef ZLIB
385 if( Conn_Options( con ) & CONN_ZIP )
387 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;
389 else
390 #endif
392 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;
395 con = Conn_Next( con );
397 break;
398 case 'm': /* IRC-Befehle */
399 case 'M':
400 cmd = Parse_GetCommandStruct( );
401 while( cmd->name )
403 if( cmd->lcount > 0 || cmd->rcount > 0 )
405 if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
407 cmd++;
409 break;
412 IRC_SetPenalty( from, 2 );
413 return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
414 } /* IRC_STATS */
417 GLOBAL bool
418 IRC_TIME( CLIENT *Client, REQUEST *Req )
420 CLIENT *from, *target;
421 char t_str[64];
422 time_t t;
424 assert( Client != NULL );
425 assert( Req != NULL );
427 /* Falsche Anzahl Parameter? */
428 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
430 /* From aus Prefix ermitteln */
431 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
432 else from = Client;
433 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
435 if( Req->argc == 1 )
437 /* an anderen Server forwarden */
438 target = Client_Search( Req->argv[0] );
439 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
441 if( target != Client_ThisServer( ))
443 /* Ok, anderer Server ist das Ziel: forwarden */
444 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
448 t = time( NULL );
449 (void)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
450 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
451 } /* IRC_TIME */
454 GLOBAL bool
455 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
457 char rpl[COMMAND_LEN];
458 CLIENT *c;
459 int max, i;
461 assert( Client != NULL );
462 assert( Req != NULL );
464 /* Falsche Anzahl Parameter? */
465 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
467 if( Req->argc > 5 ) max = 5;
468 else max = Req->argc;
470 strcpy( rpl, RPL_USERHOST_MSG );
471 for( i = 0; i < max; i++ )
473 c = Client_Search( Req->argv[i] );
474 if( c && ( Client_Type( c ) == CLIENT_USER ))
476 /* Dieser Nick ist "online" */
477 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
478 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
479 strlcat( rpl, "=", sizeof( rpl ));
480 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
481 else strlcat( rpl, "+", sizeof( rpl ));
482 strlcat( rpl, Client_User( c ), sizeof( rpl ));
483 strlcat( rpl, "@", sizeof( rpl ));
484 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
485 strlcat( rpl, " ", sizeof( rpl ));
488 ngt_TrimLastChr( rpl, ' ');
490 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
491 } /* IRC_USERHOST */
494 GLOBAL bool
495 IRC_VERSION( CLIENT *Client, REQUEST *Req )
497 CLIENT *target, *prefix;
498 #ifdef CVSDATE
499 char ver[12], vertxt[30];
500 #endif
502 assert( Client != NULL );
503 assert( Req != NULL );
505 /* Falsche Anzahl Parameter? */
506 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
508 /* Ziel suchen */
509 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
510 else target = Client_ThisServer( );
512 /* Prefix ermitteln */
513 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
514 else prefix = Client;
515 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
517 /* An anderen Server weiterleiten? */
518 if( target != Client_ThisServer( ))
520 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
522 /* forwarden */
523 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
524 return CONNECTED;
527 /* mit Versionsinfo antworten */
528 IRC_SetPenalty( Client, 1 );
529 #ifdef CVSDATE
530 strlcpy( ver, CVSDATE, sizeof( ver ));
531 strncpy( ver + 4, ver + 5, 2 );
532 strncpy( ver + 6, ver + 8, 3 );
533 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
534 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
535 #else
536 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition );
537 #endif
538 } /* IRC_VERSION */
541 GLOBAL bool
542 IRC_WHO( CLIENT *Client, REQUEST *Req )
544 bool ok, only_ops;
545 char flags[8], *ptr;
546 CL2CHAN *cl2chan;
547 CHANNEL *chan, *cn;
548 CLIENT *c;
550 assert( Client != NULL );
551 assert( Req != NULL );
553 /* Falsche Anzahl Parameter? */
554 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
556 only_ops = false;
557 chan = NULL;
559 if( Req->argc == 2 )
561 /* Nur OPs anzeigen? */
562 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = true;
563 #ifdef STRICT_RFC
564 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
565 #endif
568 if( Req->argc >= 1 )
570 /* wurde ein Channel oder Nick-Mask angegeben? */
571 chan = Channel_Search( Req->argv[0] );
574 if( chan )
576 /* User eines Channels ausgeben */
577 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
580 c = Client_First( );
581 while( c )
583 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
585 ok = false;
586 if( Req->argc == 0 ) ok = true;
587 else
589 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = true;
590 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = true;
593 if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
595 /* Get flags */
596 strcpy( flags, "H" );
597 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
599 /* Search suitable channel */
600 cl2chan = Channel_FirstChannelOf( c );
601 while( cl2chan )
603 cn = Channel_GetChannel( cl2chan );
604 if( Channel_IsMemberOf( cn, Client ) ||
605 ! strchr( Channel_Modes( cn ), 's' ))
607 ptr = Channel_Name( cn );
608 break;
610 cl2chan = Channel_NextChannelOf( c, cl2chan );
612 if( ! cl2chan ) ptr = "*";
614 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;
618 /* naechster Client */
619 c = Client_Next( c );
622 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
623 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
624 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
625 } /* IRC_WHO */
628 GLOBAL bool
629 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
631 CLIENT *from, *target, *c;
632 char str[LINE_LEN + 1];
633 CL2CHAN *cl2chan;
634 CHANNEL *chan;
636 assert( Client != NULL );
637 assert( Req != NULL );
639 /* Bad number of parameters? */
640 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
642 /* Search client */
643 c = Client_Search( Req->argv[Req->argc - 1] );
644 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
646 /* Search sender of the WHOIS */
647 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
648 else from = Client;
649 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
651 /* Forward to other server? */
652 if( Req->argc > 1 )
654 /* Search target server (can be specified as nick of that server!) */
655 target = Client_Search( Req->argv[0] );
656 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
658 else target = Client_ThisServer( );
660 assert( target != NULL );
662 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] );
664 /* Nick, user and name */
665 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
667 /* Server */
668 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;
670 /* Channels */
671 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
672 cl2chan = Channel_FirstChannelOf( c );
673 while( cl2chan )
675 chan = Channel_GetChannel( cl2chan );
676 assert( chan != NULL );
678 /* next */
679 cl2chan = Channel_NextChannelOf( c, cl2chan );
681 /* Secret channel? */
682 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
684 /* Concatenate channel names */
685 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
686 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
687 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
688 strlcat( str, Channel_Name( chan ), sizeof( str ));
690 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
692 /* Line becomes too long: send it! */
693 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
694 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
697 if( str[strlen( str ) - 1] != ':')
699 /* There is data left to send: */
700 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
703 /* IRC-Operator? */
704 if( Client_HasMode( c, 'o' ))
706 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
709 /* Idle (only local clients) */
710 if( Client_Conn( c ) > NONE )
712 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
715 /* Away? */
716 if( Client_HasMode( c, 'a' ))
718 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
721 /* End of Whois */
722 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
723 } /* IRC_WHOIS */
726 /**
727 * IRC "WHOWAS" function.
728 * This function implements the IRC command "WHOWHAS". It handles local
729 * requests and request that should be forwarded to other servers.
730 */
731 GLOBAL bool
732 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
734 CLIENT *target, *prefix;
735 WHOWAS *whowas;
736 int max, last, count, i;
737 char t_str[60];
739 assert( Client != NULL );
740 assert( Req != NULL );
742 /* Wrong number of parameters? */
743 if(( Req->argc < 1 ) || ( Req->argc > 3 ))
744 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
745 Client_ID( Client ), Req->command );
747 /* Search taget */
748 if( Req->argc == 3 )
749 target = Client_Search( Req->argv[2] );
750 else
751 target = Client_ThisServer( );
753 /* Get prefix */
754 if( Client_Type( Client ) == CLIENT_SERVER )
755 prefix = Client_Search( Req->prefix );
756 else
757 prefix = Client;
759 if( ! prefix )
760 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG,
761 Client_ID( Client ), Req->prefix );
763 /* Forward to other server? */
764 if( target != Client_ThisServer( ))
766 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
767 return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG,
768 Client_ID( prefix ),
769 Req->argv[2] );
771 /* Forward */
772 IRC_WriteStrClientPrefix( target, prefix, "WHOWAS %s %s %s",
773 Req->argv[0], Req->argv[1],
774 Req->argv[2] );
775 return CONNECTED;
778 whowas = Client_GetWhowas( );
779 last = Client_GetLastWhowasIndex( );
780 if( last < 0 ) last = 0;
782 if( Req->argc > 1 )
784 max = atoi( Req->argv[1] );
785 if( max < 1 ) max = MAX_WHOWAS;
787 else
788 max = DEFAULT_WHOWAS;
790 i = last;
791 count = 0;
792 do
794 /* Used entry? */
795 if( whowas[i].time > 0 &&
796 strcasecmp( Req->argv[0], whowas[i].id ) == 0 )
798 (void)strftime( t_str, sizeof(t_str),
799 "%a %b %d %H:%M:%S %Y",
800 localtime( &whowas[i].time ));
802 if( ! IRC_WriteStrClient( prefix, RPL_WHOWASUSER_MSG,
803 Client_ID( prefix ),
804 whowas[i].id,
805 whowas[i].user,
806 whowas[i].host,
807 whowas[i].info ))
808 return DISCONNECTED;
810 if( ! IRC_WriteStrClient( prefix, RPL_WHOISSERVER_MSG,
811 Client_ID( prefix ),
812 whowas[i].id,
813 whowas[i].server, t_str ))
814 return DISCONNECTED;
816 count++;
817 if( count >= max ) break;
820 /* previos entry */
821 i--;
823 /* "underflow", wrap around */
824 if( i < 0 ) i = MAX_WHOWAS - 1;
825 } while( i != last );
827 return IRC_WriteStrClient( prefix, RPL_ENDOFWHOWAS_MSG,
828 Client_ID( prefix ), Req->argv[0] );
829 } /* IRC_WHOWAS */
832 GLOBAL bool
833 IRC_Send_LUSERS( CLIENT *Client )
835 long cnt;
837 assert( Client != NULL );
839 /* Users, services and serevers in the network */
840 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
842 /* Number of IRC operators */
843 cnt = Client_OperCount( );
844 if( cnt > 0 )
846 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
849 /* Unknown connections */
850 cnt = Client_UnknownCount( );
851 if( cnt > 0 )
853 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
856 /* Number of created channels */
857 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
859 /* Number of local users, services and servers */
860 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
862 #ifndef STRICT_RFC
863 /* Maximum number of local users */
864 if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
865 /* Maximum number of users in the network */
866 if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
867 #endif
869 return CONNECTED;
870 } /* IRC_Send_LUSERS */
873 GLOBAL bool
874 IRC_Show_MOTD( CLIENT *Client )
876 bool ok;
877 char line[127];
878 FILE *fd;
880 assert( Client != NULL );
882 if( Conf_MotdPhrase[0] )
884 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
885 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), Conf_MotdPhrase )) return DISCONNECTED;
886 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
889 fd = fopen( Conf_MotdFile, "r" );
890 if( ! fd )
892 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
893 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
896 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
897 while( true )
899 if( ! fgets( line, sizeof( line ), fd )) break;
901 ngt_TrimLastChr( line, '\n');
903 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
905 fclose( fd );
906 return false;
909 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
911 fclose( fd );
913 return ok;
914 } /* IRC_Show_MOTD */
917 GLOBAL bool
918 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
920 bool is_visible, is_member;
921 char str[LINE_LEN + 1];
922 CL2CHAN *cl2chan;
923 CLIENT *cl;
925 assert( Client != NULL );
926 assert( Chan != NULL );
928 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
929 else is_member = false;
931 /* Secret channel? */
932 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
934 /* Alle Mitglieder suchen */
935 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
936 cl2chan = Channel_FirstMember( Chan );
937 while( cl2chan )
939 cl = Channel_GetClient( cl2chan );
941 if( strchr( Client_Modes( cl ), 'i' )) is_visible = false;
942 else is_visible = true;
944 if( is_member || is_visible )
946 /* Nick anhaengen */
947 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
948 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
949 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
950 strlcat( str, Client_ID( cl ), sizeof( str ));
952 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
954 /* Zeile wird zu lang: senden! */
955 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
956 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
960 /* naechstes Mitglied suchen */
961 cl2chan = Channel_NextMember( Chan, cl2chan );
963 if( str[strlen( str ) - 1] != ':')
965 /* Es sind noch Daten da, die gesendet werden muessen */
966 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
969 return CONNECTED;
970 } /* IRC_Send_NAMES */
973 GLOBAL bool
974 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, bool OnlyOps )
976 bool is_visible, is_member;
977 CL2CHAN *cl2chan;
978 char flags[8];
979 CLIENT *c;
981 assert( Client != NULL );
982 assert( Chan != NULL );
984 if( Channel_IsMemberOf( Chan, Client )) is_member = true;
985 else is_member = false;
987 /* Secret channel? */
988 if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
990 /* Alle Mitglieder suchen */
991 cl2chan = Channel_FirstMember( Chan );
992 while( cl2chan )
994 c = Channel_GetClient( cl2chan );
996 if( strchr( Client_Modes( c ), 'i' )) is_visible = false;
997 else is_visible = true;
999 if( is_member || is_visible )
1001 /* Flags zusammenbasteln */
1002 strcpy( flags, "H" );
1003 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
1004 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
1005 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
1007 /* ausgeben */
1008 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
1010 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;
1014 /* naechstes Mitglied suchen */
1015 cl2chan = Channel_NextMember( Chan, cl2chan );
1017 return CONNECTED;
1018 } /* IRC_Send_WHO */
1021 /* -eof- */