Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by 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.22 2004/05/07 11:19:21 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <strings.h>
26 #include "ngircd.h"
27 #include "cvs-version.h"
28 #include "conn-func.h"
29 #include "conn-zip.h"
30 #include "client.h"
31 #include "channel.h"
32 #include "resolve.h"
33 #include "conf.h"
34 #include "defines.h"
35 #include "log.h"
36 #include "messages.h"
37 #include "tool.h"
38 #include "parse.h"
39 #include "irc-write.h"
41 #include "exp.h"
42 #include "irc-info.h"
45 GLOBAL BOOLEAN
46 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
47 {
48 CLIENT *target, *prefix;
50 assert( Client != NULL );
51 assert( Req != NULL );
53 /* Falsche Anzahl Parameter? */
54 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
56 /* Ziel suchen */
57 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
58 else target = Client_ThisServer( );
60 /* Prefix ermitteln */
61 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
62 else prefix = Client;
63 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
65 /* An anderen Server weiterleiten? */
66 if( target != Client_ThisServer( ))
67 {
68 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
70 /* forwarden */
71 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
72 return CONNECTED;
73 }
75 /* mit Versionsinfo antworten */
76 if( ! IRC_WriteStrClient( Client, RPL_ADMINME_MSG, Client_ID( prefix ), Conf_ServerName )) return DISCONNECTED;
77 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC1_MSG, Client_ID( prefix ), Conf_ServerAdmin1 )) return DISCONNECTED;
78 if( ! IRC_WriteStrClient( Client, RPL_ADMINLOC2_MSG, Client_ID( prefix ), Conf_ServerAdmin2 )) return DISCONNECTED;
79 if( ! IRC_WriteStrClient( Client, RPL_ADMINEMAIL_MSG, Client_ID( prefix ), Conf_ServerAdminMail )) return DISCONNECTED;
81 IRC_SetPenalty( Client, 1 );
82 return CONNECTED;
83 } /* IRC_ADMIN */
86 GLOBAL BOOLEAN
87 IRC_ISON( CLIENT *Client, REQUEST *Req )
88 {
89 CHAR rpl[COMMAND_LEN];
90 CLIENT *c;
91 CHAR *ptr;
92 INT i;
94 assert( Client != NULL );
95 assert( Req != NULL );
97 /* Falsche Anzahl Parameter? */
98 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
100 strcpy( rpl, RPL_ISON_MSG );
101 for( i = 0; i < Req->argc; i++ )
103 ptr = strtok( Req->argv[i], " " );
104 while( ptr )
106 ngt_TrimStr( ptr );
107 c = Client_Search( ptr );
108 if( c && ( Client_Type( c ) == CLIENT_USER ))
110 /* Dieser Nick ist "online" */
111 strlcat( rpl, ptr, sizeof( rpl ));
112 strlcat( rpl, " ", sizeof( rpl ));
114 ptr = strtok( NULL, " " );
117 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
119 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
120 } /* IRC_ISON */
123 GLOBAL BOOLEAN
124 IRC_LINKS( CLIENT *Client, REQUEST *Req )
126 CLIENT *target, *from, *c;
127 CHAR *mask;
129 assert( Client != NULL );
130 assert( Req != NULL );
132 /* Falsche Anzahl Parameter? */
133 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
135 /* Server-Mask ermitteln */
136 if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
137 else mask = "*";
139 /* Absender ermitteln */
140 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
141 else from = Client;
142 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
144 /* An anderen Server forwarden? */
145 if( Req->argc == 2 )
147 target = Client_Search( Req->argv[0] );
148 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
149 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LINKS %s %s", Req->argv[0], Req->argv[1] );
152 /* Wer ist der Absender? */
153 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
154 else target = Client;
155 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
157 c = Client_First( );
158 while( c )
160 if( Client_Type( c ) == CLIENT_SERVER )
162 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;
164 c = Client_Next( c );
167 IRC_SetPenalty( target, 1 );
168 return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
169 } /* IRC_LINKS */
172 GLOBAL BOOLEAN
173 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
175 CLIENT *target, *from;
177 assert( Client != NULL );
178 assert( Req != NULL );
180 /* Falsche Anzahl Parameter? */
181 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
183 /* Absender ermitteln */
184 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
185 else from = Client;
186 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
188 /* An anderen Server forwarden? */
189 if( Req->argc == 2 )
191 target = Client_Search( Req->argv[1] );
192 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
193 else if( target != Client_ThisServer( )) return IRC_WriteStrClientPrefix( target, from, "LUSERS %s %s", Req->argv[0], Req->argv[1] );
196 /* Wer ist der Absender? */
197 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
198 else target = Client;
199 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
201 IRC_Send_LUSERS( target );
203 IRC_SetPenalty( target, 1 );
204 return CONNECTED;
205 } /* IRC_LUSERS */
208 GLOBAL BOOLEAN
209 IRC_MOTD( CLIENT *Client, REQUEST *Req )
211 CLIENT *from, *target;
213 assert( Client != NULL );
214 assert( Req != NULL );
216 /* Falsche Anzahl Parameter? */
217 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
219 /* From aus Prefix ermitteln */
220 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
221 else from = Client;
222 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224 if( Req->argc == 1 )
226 /* an anderen Server forwarden */
227 target = Client_Search( Req->argv[0] );
228 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
230 if( target != Client_ThisServer( ))
232 /* Ok, anderer Server ist das Ziel: forwarden */
233 return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
237 IRC_SetPenalty( from, 3 );
238 return IRC_Show_MOTD( from );
239 } /* IRC_MOTD */
242 GLOBAL BOOLEAN
243 IRC_NAMES( CLIENT *Client, REQUEST *Req )
245 CHAR rpl[COMMAND_LEN], *ptr;
246 CLIENT *target, *from, *c;
247 CHANNEL *chan;
249 assert( Client != NULL );
250 assert( Req != NULL );
252 /* Falsche Anzahl Parameter? */
253 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
255 /* From aus Prefix ermitteln */
256 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
257 else from = Client;
258 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
260 if( Req->argc == 2 )
262 /* an anderen Server forwarden */
263 target = Client_Search( Req->argv[1] );
264 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
266 if( target != Client_ThisServer( ))
268 /* Ok, anderer Server ist das Ziel: forwarden */
269 return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
273 if( Req->argc > 0 )
275 /* bestimmte Channels durchgehen */
276 ptr = strtok( Req->argv[0], "," );
277 while( ptr )
279 chan = Channel_Search( ptr );
280 if( chan )
282 /* Namen ausgeben */
283 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
285 if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
287 /* naechsten Namen ermitteln */
288 ptr = strtok( NULL, "," );
290 return CONNECTED;
293 /* alle Channels durchgehen */
294 chan = Channel_First( );
295 while( chan )
297 /* Namen ausgeben */
298 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
300 /* naechster Channel */
301 chan = Channel_Next( chan );
304 /* Nun noch alle Clients ausgeben, die in keinem Channel sind */
305 c = Client_First( );
306 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
307 while( c )
309 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
311 /* Okay, das ist ein User: anhaengen */
312 if( rpl[strlen( rpl ) - 1] != ':' ) strlcat( rpl, " ", sizeof( rpl ));
313 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
315 if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
317 /* Zeile wird zu lang: senden! */
318 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
319 snprintf( rpl, sizeof( rpl ), RPL_NAMREPLY_MSG, Client_ID( from ), "*", "*" );
323 /* naechster Client */
324 c = Client_Next( c );
326 if( rpl[strlen( rpl ) - 1] != ':')
328 /* es wurden User gefunden */
329 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
332 IRC_SetPenalty( from, 1 );
333 return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
334 } /* IRC_NAMES */
337 GLOBAL BOOLEAN
338 IRC_STATS( CLIENT *Client, REQUEST *Req )
340 CLIENT *from, *target, *cl;
341 CONN_ID con;
342 CHAR query;
343 COMMAND *cmd;
345 assert( Client != NULL );
346 assert( Req != NULL );
348 /* Falsche Anzahl Parameter? */
349 if( Req->argc > 2 ) 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 );
356 if( Req->argc == 2 )
358 /* an anderen Server forwarden */
359 target = Client_Search( Req->argv[1] );
360 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] );
362 if( target != Client_ThisServer( ))
364 /* Ok, anderer Server ist das Ziel: forwarden */
365 return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
369 if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
370 else query = '*';
372 switch ( query )
374 case 'l': /* Links */
375 case 'L':
376 con = Conn_First( );
377 while( con != NONE )
379 cl = Client_GetFromConn( con );
380 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
382 /* Server link or our own connection */
383 #ifdef ZLIB
384 if( Conn_Options( con ) & CONN_ZIP )
386 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;
388 else
389 #endif
391 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;
394 con = Conn_Next( con );
396 break;
397 case 'm': /* IRC-Befehle */
398 case 'M':
399 cmd = Parse_GetCommandStruct( );
400 while( cmd->name )
402 if( cmd->lcount > 0 || cmd->rcount > 0 )
404 if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
406 cmd++;
408 break;
411 IRC_SetPenalty( from, 2 );
412 return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
413 } /* IRC_STATS */
416 GLOBAL BOOLEAN
417 IRC_TIME( CLIENT *Client, REQUEST *Req )
419 CLIENT *from, *target;
420 CHAR t_str[64];
421 time_t t;
423 assert( Client != NULL );
424 assert( Req != NULL );
426 /* Falsche Anzahl Parameter? */
427 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
429 /* From aus Prefix ermitteln */
430 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
431 else from = Client;
432 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
434 if( Req->argc == 1 )
436 /* an anderen Server forwarden */
437 target = Client_Search( Req->argv[0] );
438 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[0] );
440 if( target != Client_ThisServer( ))
442 /* Ok, anderer Server ist das Ziel: forwarden */
443 return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
447 t = time( NULL );
448 (VOID)strftime( t_str, 60, "%A %B %d %Y -- %H:%M %Z", localtime( &t ));
449 return IRC_WriteStrClient( from, RPL_TIME_MSG, Client_ID( from ), Client_ID( Client_ThisServer( )), t_str );
450 } /* IRC_TIME */
453 GLOBAL BOOLEAN
454 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
456 CHAR rpl[COMMAND_LEN];
457 CLIENT *c;
458 INT max, i;
460 assert( Client != NULL );
461 assert( Req != NULL );
463 /* Falsche Anzahl Parameter? */
464 if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
466 if( Req->argc > 5 ) max = 5;
467 else max = Req->argc;
469 strcpy( rpl, RPL_USERHOST_MSG );
470 for( i = 0; i < max; i++ )
472 c = Client_Search( Req->argv[i] );
473 if( c && ( Client_Type( c ) == CLIENT_USER ))
475 /* Dieser Nick ist "online" */
476 strlcat( rpl, Client_ID( c ), sizeof( rpl ));
477 if( Client_HasMode( c, 'o' )) strlcat( rpl, "*", sizeof( rpl ));
478 strlcat( rpl, "=", sizeof( rpl ));
479 if( Client_HasMode( c, 'a' )) strlcat( rpl, "-", sizeof( rpl ));
480 else strlcat( rpl, "+", sizeof( rpl ));
481 strlcat( rpl, Client_User( c ), sizeof( rpl ));
482 strlcat( rpl, "@", sizeof( rpl ));
483 strlcat( rpl, Client_Hostname( c ), sizeof( rpl ));
484 strlcat( rpl, " ", sizeof( rpl ));
487 if( rpl[strlen( rpl ) - 1] == ' ' ) rpl[strlen( rpl ) - 1] = '\0';
489 return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
490 } /* IRC_USERHOST */
493 GLOBAL BOOLEAN
494 IRC_VERSION( CLIENT *Client, REQUEST *Req )
496 CLIENT *target, *prefix;
497 #ifdef CVSDATE
498 CHAR ver[12], vertxt[30];
499 #endif
501 assert( Client != NULL );
502 assert( Req != NULL );
504 /* Falsche Anzahl Parameter? */
505 if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
507 /* Ziel suchen */
508 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
509 else target = Client_ThisServer( );
511 /* Prefix ermitteln */
512 if( Client_Type( Client ) == CLIENT_SERVER ) prefix = Client_Search( Req->prefix );
513 else prefix = Client;
514 if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
516 /* An anderen Server weiterleiten? */
517 if( target != Client_ThisServer( ))
519 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
521 /* forwarden */
522 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
523 return CONNECTED;
526 /* mit Versionsinfo antworten */
527 IRC_SetPenalty( Client, 1 );
528 #ifdef CVSDATE
529 strlcpy( ver, CVSDATE, sizeof( ver ));
530 strncpy( ver + 4, ver + 5, 2 );
531 strncpy( ver + 6, ver + 8, 3 );
532 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
533 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
534 #else
535 return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
536 #endif
537 } /* IRC_VERSION */
540 GLOBAL BOOLEAN
541 IRC_WHO( CLIENT *Client, REQUEST *Req )
543 BOOLEAN ok, only_ops;
544 CHAR flags[8], *ptr;
545 CL2CHAN *cl2chan;
546 CHANNEL *chan;
547 CLIENT *c;
549 assert( Client != NULL );
550 assert( Req != NULL );
552 /* Falsche Anzahl Parameter? */
553 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
555 only_ops = FALSE;
556 chan = NULL;
558 if( Req->argc == 2 )
560 /* Nur OPs anzeigen? */
561 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = TRUE;
562 #ifdef STRICT_RFC
563 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
564 #endif
567 if( Req->argc >= 1 )
569 /* wurde ein Channel oder Nick-Mask angegeben? */
570 chan = Channel_Search( Req->argv[0] );
573 if( chan )
575 /* User eines Channels ausgeben */
576 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
579 c = Client_First( );
580 while( c )
582 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
584 ok = FALSE;
585 if( Req->argc == 0 ) ok = TRUE;
586 else
588 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = TRUE;
589 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = TRUE;
592 if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
594 /* Flags zusammenbasteln */
595 strcpy( flags, "H" );
596 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
598 /* ausgeben */
599 cl2chan = Channel_FirstChannelOf( c );
600 if( cl2chan ) ptr = Channel_Name( Channel_GetChannel( cl2chan ));
601 else ptr = "*";
602 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;
606 /* naechster Client */
607 c = Client_Next( c );
610 if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
611 else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
612 else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
613 } /* IRC_WHO */
616 GLOBAL BOOLEAN
617 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
619 CLIENT *from, *target, *c;
620 CHAR str[LINE_LEN + 1];
621 CL2CHAN *cl2chan;
622 CHANNEL *chan;
624 assert( Client != NULL );
625 assert( Req != NULL );
627 /* Bad number of parameters? */
628 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
630 /* Search client */
631 c = Client_Search( Req->argv[Req->argc - 1] );
632 if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
634 /* Search sender of the WHOIS */
635 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
636 else from = Client;
637 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
639 /* Forward to other server? */
640 if( Req->argc > 1 )
642 /* Search target server (can be specified as nick of that server!) */
643 target = Client_Search( Req->argv[0] );
644 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
646 else target = Client_ThisServer( );
648 assert( target != NULL );
650 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] );
652 /* Nick, user and name */
653 if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
655 /* Server */
656 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;
658 /* Channels */
659 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
660 cl2chan = Channel_FirstChannelOf( c );
661 while( cl2chan )
663 chan = Channel_GetChannel( cl2chan );
664 assert( chan != NULL );
666 /* Concatenate channel names */
667 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
668 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
669 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
670 strlcat( str, Channel_Name( chan ), sizeof( str ));
672 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
674 /* Line becomes too long: send it! */
675 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
676 snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
679 /* next */
680 cl2chan = Channel_NextChannelOf( c, cl2chan );
682 if( str[strlen( str ) - 1] != ':')
684 /* There is data left to send: */
685 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
688 /* IRC-Operator? */
689 if( Client_HasMode( c, 'o' ))
691 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
694 /* Idle (only local clients) */
695 if( Client_Conn( c ) > NONE )
697 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
700 /* Away? */
701 if( Client_HasMode( c, 'a' ))
703 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
706 /* End of Whois */
707 return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
708 } /* IRC_WHOIS */
711 GLOBAL BOOLEAN
712 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
714 assert( Client != NULL );
715 assert( Req != NULL );
717 /* Falsche Anzahl Parameter? */
718 if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
720 /* ... */
722 return CONNECTED;
723 } /* IRC_WHOWAS */
726 GLOBAL BOOLEAN
727 IRC_Send_LUSERS( CLIENT *Client )
729 LONG cnt;
731 assert( Client != NULL );
733 /* Users, services and serevers in the network */
734 if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
736 /* Number of IRC operators */
737 cnt = Client_OperCount( );
738 if( cnt > 0 )
740 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
743 /* Unknown connections */
744 cnt = Client_UnknownCount( );
745 if( cnt > 0 )
747 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
750 /* Number of created channels */
751 if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
753 /* Number of local users, services and servers */
754 if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
756 #ifndef STRICT_RFC
757 /* Maximum number of local users */
758 if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
759 /* Maximum number of users in the network */
760 if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
761 #endif
763 return CONNECTED;
764 } /* IRC_Send_LUSERS */
767 GLOBAL BOOLEAN
768 IRC_Show_MOTD( CLIENT *Client )
770 BOOLEAN ok;
771 CHAR line[127];
772 FILE *fd;
774 assert( Client != NULL );
776 if( Conf_MotdPhrase[0] )
778 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
779 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), Conf_MotdPhrase )) return DISCONNECTED;
780 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
783 fd = fopen( Conf_MotdFile, "r" );
784 if( ! fd )
786 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
787 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
790 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
791 while( TRUE )
793 if( ! fgets( line, 126, fd )) break;
794 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
795 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
797 fclose( fd );
798 return FALSE;
801 ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
803 fclose( fd );
805 return ok;
806 } /* IRC_Show_MOTD */
809 GLOBAL BOOLEAN
810 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
812 BOOLEAN is_visible, is_member;
813 CHAR str[LINE_LEN + 1];
814 CL2CHAN *cl2chan;
815 CLIENT *cl;
817 assert( Client != NULL );
818 assert( Chan != NULL );
820 if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
821 else is_member = FALSE;
823 /* Alle Mitglieder suchen */
824 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
825 cl2chan = Channel_FirstMember( Chan );
826 while( cl2chan )
828 cl = Channel_GetClient( cl2chan );
830 if( strchr( Client_Modes( cl ), 'i' )) is_visible = FALSE;
831 else is_visible = TRUE;
833 if( is_member || is_visible )
835 /* Nick anhaengen */
836 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
837 if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
838 else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
839 strlcat( str, Client_ID( cl ), sizeof( str ));
841 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
843 /* Zeile wird zu lang: senden! */
844 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
845 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
849 /* naechstes Mitglied suchen */
850 cl2chan = Channel_NextMember( Chan, cl2chan );
852 if( str[strlen( str ) - 1] != ':')
854 /* Es sind noch Daten da, die gesendet werden muessen */
855 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
858 return CONNECTED;
859 } /* IRC_Send_NAMES */
862 GLOBAL BOOLEAN
863 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, BOOLEAN OnlyOps )
865 BOOLEAN is_visible, is_member;
866 CL2CHAN *cl2chan;
867 CHAR flags[8];
868 CLIENT *c;
870 assert( Client != NULL );
871 assert( Chan != NULL );
873 if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
874 else is_member = FALSE;
876 /* Alle Mitglieder suchen */
877 cl2chan = Channel_FirstMember( Chan );
878 while( cl2chan )
880 c = Channel_GetClient( cl2chan );
882 if( strchr( Client_Modes( c ), 'i' )) is_visible = FALSE;
883 else is_visible = TRUE;
885 if( is_member || is_visible )
887 /* Flags zusammenbasteln */
888 strcpy( flags, "H" );
889 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
890 if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
891 else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
893 /* ausgeben */
894 if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
896 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;
900 /* naechstes Mitglied suchen */
901 cl2chan = Channel_NextMember( Chan, cl2chan );
903 return CONNECTED;
904 } /* IRC_Send_WHO */
907 /* -eof- */