]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
3811ea73d406c7b429eaee389b575bdc78a0a726
[ngircd-alex.git] / src / ngircd / irc-info.c
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  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc-info.c,v 1.27 2005/03/02 16:35:11 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <strings.h>
25
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"
40
41 #include "exp.h"
42 #include "irc-info.h"
43
44
45 GLOBAL BOOLEAN
46 IRC_ADMIN(CLIENT *Client, REQUEST *Req )
47 {
48         CLIENT *target, *prefix;
49
50         assert( Client != NULL );
51         assert( Req != NULL );
52
53         /* Falsche Anzahl Parameter? */
54         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
55
56         /* Ziel suchen */
57         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
58         else target = Client_ThisServer( );
59
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 );
64
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] );
69
70                 /* forwarden */
71                 IRC_WriteStrClientPrefix( target, prefix, "ADMIN %s", Req->argv[0] );
72                 return CONNECTED;
73         }
74
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;
80
81         IRC_SetPenalty( Client, 1 );
82         return CONNECTED;
83 } /* IRC_ADMIN */
84
85
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;
93
94         assert( Client != NULL );
95         assert( Req != NULL );
96
97         /* Falsche Anzahl Parameter? */
98         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
99
100         strcpy( rpl, RPL_ISON_MSG );
101         for( i = 0; i < Req->argc; i++ )
102         {
103                 ptr = strtok( Req->argv[i], " " );
104                 while( ptr )
105                 {
106                         ngt_TrimStr( ptr );
107                         c = Client_Search( ptr );
108                         if( c && ( Client_Type( c ) == CLIENT_USER ))
109                         {
110                                 /* Dieser Nick ist "online" */
111                                 strlcat( rpl, ptr, sizeof( rpl ));
112                                 strlcat( rpl, " ", sizeof( rpl ));
113                         }
114                         ptr = strtok( NULL, " " );
115                 }
116         }
117         ngt_TrimLastChr(rpl, ' ');
118
119         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
120 } /* IRC_ISON */
121
122
123 GLOBAL BOOLEAN
124 IRC_LINKS( CLIENT *Client, REQUEST *Req )
125 {
126         CLIENT *target, *from, *c;
127         CHAR *mask;
128
129         assert( Client != NULL );
130         assert( Req != NULL );
131
132         /* Falsche Anzahl Parameter? */
133         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
134
135         /* Server-Mask ermitteln */
136         if( Req->argc > 0 ) mask = Req->argv[Req->argc - 1];
137         else mask = "*";
138
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 );
143
144         /* An anderen Server forwarden? */
145         if( Req->argc == 2 )
146         {
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] );
150         }
151
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 );
156
157         c = Client_First( );
158         while( c )
159         {
160                 if( Client_Type( c ) == CLIENT_SERVER )
161                 {
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;
163                 }
164                 c = Client_Next( c );
165         }
166         
167         IRC_SetPenalty( target, 1 );
168         return IRC_WriteStrClient( target, RPL_ENDOFLINKS_MSG, Client_ID( target ), mask );
169 } /* IRC_LINKS */
170
171
172 GLOBAL BOOLEAN
173 IRC_LUSERS( CLIENT *Client, REQUEST *Req )
174 {
175         CLIENT *target, *from;
176
177         assert( Client != NULL );
178         assert( Req != NULL );
179
180         /* Falsche Anzahl Parameter? */
181         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
182
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 );
187
188         /* An anderen Server forwarden? */
189         if( Req->argc == 2 )
190         {
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] );
194         }
195
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 );
200
201         IRC_Send_LUSERS( target );
202
203         IRC_SetPenalty( target, 1 );
204         return CONNECTED;
205 } /* IRC_LUSERS */
206
207
208 GLOBAL BOOLEAN
209 IRC_MOTD( CLIENT *Client, REQUEST *Req )
210 {
211         CLIENT *from, *target;
212
213         assert( Client != NULL );
214         assert( Req != NULL );
215
216         /* Falsche Anzahl Parameter? */
217         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
218
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 );
223
224         if( Req->argc == 1 )
225         {
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] );
229
230                 if( target != Client_ThisServer( ))
231                 {
232                         /* Ok, anderer Server ist das Ziel: forwarden */
233                         return IRC_WriteStrClientPrefix( target, from, "MOTD %s", Req->argv[0] );
234                 }
235         }
236
237         IRC_SetPenalty( from, 3 );
238         return IRC_Show_MOTD( from );
239 } /* IRC_MOTD */
240
241
242 GLOBAL BOOLEAN
243 IRC_NAMES( CLIENT *Client, REQUEST *Req )
244 {
245         CHAR rpl[COMMAND_LEN], *ptr;
246         CLIENT *target, *from, *c;
247         CHANNEL *chan;
248
249         assert( Client != NULL );
250         assert( Req != NULL );
251
252         /* Falsche Anzahl Parameter? */
253         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
254
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 );
259
260         if( Req->argc == 2 )
261         {
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] );
265
266                 if( target != Client_ThisServer( ))
267                 {
268                         /* Ok, anderer Server ist das Ziel: forwarden */
269                         return IRC_WriteStrClientPrefix( target, from, "NAMES %s :%s", Req->argv[0], Req->argv[1] );
270                 }
271         }
272
273         if( Req->argc > 0 )
274         {
275                 /* bestimmte Channels durchgehen */
276                 ptr = strtok( Req->argv[0], "," );
277                 while( ptr )
278                 {
279                         chan = Channel_Search( ptr );
280                         if( chan )
281                         {
282                                 /* Namen ausgeben */
283                                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
284                         }
285                         if( ! IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), ptr )) return DISCONNECTED;
286
287                         /* naechsten Namen ermitteln */
288                         ptr = strtok( NULL, "," );
289                 }
290                 return CONNECTED;
291         }
292
293         /* alle Channels durchgehen */
294         chan = Channel_First( );
295         while( chan )
296         {
297                 /* Namen ausgeben */
298                 if( ! IRC_Send_NAMES( from, chan )) return DISCONNECTED;
299
300                 /* naechster Channel */
301                 chan = Channel_Next( chan );
302         }
303
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 )
308         {
309                 if(( Client_Type( c ) == CLIENT_USER ) && ( Channel_FirstChannelOf( c ) == NULL ) && ( ! strchr( Client_Modes( c ), 'i' )))
310                 {
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 ));
314
315                         if( strlen( rpl ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
316                         {
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 ), "*", "*" );
320                         }
321                 }
322
323                 /* naechster Client */
324                 c = Client_Next( c );
325         }
326         if( rpl[strlen( rpl ) - 1] != ':')
327         {
328                 /* es wurden User gefunden */
329                 if( ! IRC_WriteStrClient( from, "%s", rpl )) return DISCONNECTED;
330         }
331
332         IRC_SetPenalty( from, 1 );
333         return IRC_WriteStrClient( from, RPL_ENDOFNAMES_MSG, Client_ID( from ), "*" );
334 } /* IRC_NAMES */
335
336
337 GLOBAL BOOLEAN
338 IRC_STATS( CLIENT *Client, REQUEST *Req )
339 {
340         CLIENT *from, *target, *cl;
341         CONN_ID con;
342         CHAR query;
343         COMMAND *cmd;
344
345         assert( Client != NULL );
346         assert( Req != NULL );
347
348         /* Falsche Anzahl Parameter? */
349         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
350
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
356         if( Req->argc == 2 )
357         {
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] );
361
362                 if( target != Client_ThisServer( ))
363                 {
364                         /* Ok, anderer Server ist das Ziel: forwarden */
365                         return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] );
366                 }
367         }
368
369         if( Req->argc > 0 ) query = Req->argv[0][0] ? Req->argv[0][0] : '*';
370         else query = '*';
371
372         switch ( query )
373         {
374                 case 'l':       /* Links */
375                 case 'L':
376                         con = Conn_First( );
377                         while( con != NONE )
378                         {
379                                 cl = Client_GetFromConn( con );
380                                 if( cl && (( Client_Type( cl ) == CLIENT_SERVER ) || ( cl == Client )))
381                                 {
382                                         /* Server link or our own connection */
383 #ifdef ZLIB
384                                         if( Conn_Options( con ) & CONN_ZIP )
385                                         {
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;
387                                         }
388                                         else
389 #endif
390                                         {
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;
392                                         }
393                                 }
394                                 con = Conn_Next( con );
395                         }
396                         break;
397                 case 'm':       /* IRC-Befehle */
398                 case 'M':
399                         cmd = Parse_GetCommandStruct( );
400                         while( cmd->name )
401                         {
402                                 if( cmd->lcount > 0 || cmd->rcount > 0 )
403                                 {
404                                         if( ! IRC_WriteStrClient( from, RPL_STATSCOMMANDS_MSG, Client_ID( from ), cmd->name, cmd->lcount, cmd->bytes, cmd->rcount )) return DISCONNECTED;
405                                 }
406                                 cmd++;
407                         }
408                         break;
409         }
410
411         IRC_SetPenalty( from, 2 );
412         return IRC_WriteStrClient( from, RPL_ENDOFSTATS_MSG, Client_ID( from ), query );
413 } /* IRC_STATS */
414
415
416 GLOBAL BOOLEAN
417 IRC_TIME( CLIENT *Client, REQUEST *Req )
418 {
419         CLIENT *from, *target;
420         CHAR t_str[64];
421         time_t t;
422
423         assert( Client != NULL );
424         assert( Req != NULL );
425
426         /* Falsche Anzahl Parameter? */
427         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
428
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 );
433
434         if( Req->argc == 1 )
435         {
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] );
439
440                 if( target != Client_ThisServer( ))
441                 {
442                         /* Ok, anderer Server ist das Ziel: forwarden */
443                         return IRC_WriteStrClientPrefix( target, from, "TIME %s", Req->argv[0] );
444                 }
445         }
446
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 */
451
452
453 GLOBAL BOOLEAN
454 IRC_USERHOST( CLIENT *Client, REQUEST *Req )
455 {
456         CHAR rpl[COMMAND_LEN];
457         CLIENT *c;
458         INT max, i;
459
460         assert( Client != NULL );
461         assert( Req != NULL );
462
463         /* Falsche Anzahl Parameter? */
464         if(( Req->argc < 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
465
466         if( Req->argc > 5 ) max = 5;
467         else max = Req->argc;
468
469         strcpy( rpl, RPL_USERHOST_MSG );
470         for( i = 0; i < max; i++ )
471         {
472                 c = Client_Search( Req->argv[i] );
473                 if( c && ( Client_Type( c ) == CLIENT_USER ))
474                 {
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 ));
485                 }
486         }
487         ngt_TrimLastChr( rpl, ' ');
488
489         return IRC_WriteStrClient( Client, rpl, Client_ID( Client ) );
490 } /* IRC_USERHOST */
491
492
493 GLOBAL BOOLEAN
494 IRC_VERSION( CLIENT *Client, REQUEST *Req )
495 {
496         CLIENT *target, *prefix;
497 #ifdef CVSDATE
498         CHAR ver[12], vertxt[30];
499 #endif
500
501         assert( Client != NULL );
502         assert( Req != NULL );
503
504         /* Falsche Anzahl Parameter? */
505         if(( Req->argc > 1 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
506
507         /* Ziel suchen */
508         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
509         else target = Client_ThisServer( );
510
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 );
515
516         /* An anderen Server weiterleiten? */
517         if( target != Client_ThisServer( ))
518         {
519                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( prefix, ERR_NOSUCHSERVER_MSG, Client_ID( prefix ), Req->argv[0] );
520
521                 /* forwarden */
522                 IRC_WriteStrClientPrefix( target, prefix, "VERSION %s", Req->argv[0] );
523                 return CONNECTED;
524         }
525
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 */
538
539
540 GLOBAL BOOLEAN
541 IRC_WHO( CLIENT *Client, REQUEST *Req )
542 {
543         BOOLEAN ok, only_ops;
544         CHAR flags[8], *ptr;
545         CL2CHAN *cl2chan;
546         CHANNEL *chan, *cn;
547         CLIENT *c;
548
549         assert( Client != NULL );
550         assert( Req != NULL );
551
552         /* Falsche Anzahl Parameter? */
553         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
554
555         only_ops = FALSE;
556         chan = NULL;
557
558         if( Req->argc == 2 )
559         {
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
565         }
566
567         if( Req->argc >= 1 )
568         {
569                 /* wurde ein Channel oder Nick-Mask angegeben? */
570                 chan = Channel_Search( Req->argv[0] );
571         }
572
573         if( chan )
574         {
575                 /* User eines Channels ausgeben */
576                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
577         }
578
579         c = Client_First( );
580         while( c )
581         {
582                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
583                 {
584                         ok = FALSE;
585                         if( Req->argc == 0 ) ok = TRUE;
586                         else
587                         {
588                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = TRUE;
589                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = TRUE;
590                         }
591
592                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
593                         {
594                                 /* Get flags */
595                                 strcpy( flags, "H" );
596                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
597
598                                 /* Search suitable channel */
599                                 cl2chan = Channel_FirstChannelOf( c );
600                                 while( cl2chan )
601                                 {
602                                         cn = Channel_GetChannel( cl2chan );
603                                         if( Channel_IsMemberOf( cn, Client ) ||
604                                             ! strchr( Channel_Modes( cn ), 's' ))
605                                         {
606                                                 ptr = Channel_Name( cn );
607                                                 break;
608                                         }
609                                         cl2chan = Channel_NextChannelOf( c, cl2chan );
610                                 }
611                                 if( ! cl2chan ) ptr = "*";
612
613                                 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;
614                         }
615                 }
616
617                 /* naechster Client */
618                 c = Client_Next( c );
619         }
620
621         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
622         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
623         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
624 } /* IRC_WHO */
625
626
627 GLOBAL BOOLEAN
628 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
629 {
630         CLIENT *from, *target, *c;
631         CHAR str[LINE_LEN + 1];
632         CL2CHAN *cl2chan;
633         CHANNEL *chan;
634
635         assert( Client != NULL );
636         assert( Req != NULL );
637
638         /* Bad number of parameters? */
639         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
640
641         /* Search client */
642         c = Client_Search( Req->argv[Req->argc - 1] );
643         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
644
645         /* Search sender of the WHOIS */
646         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
647         else from = Client;
648         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
649
650         /* Forward to other server? */
651         if( Req->argc > 1 )
652         {
653                 /* Search target server (can be specified as nick of that server!) */
654                 target = Client_Search( Req->argv[0] );
655                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
656         }
657         else target = Client_ThisServer( );
658
659         assert( target != NULL );
660
661         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] );
662
663         /* Nick, user and name */
664         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
665
666         /* Server */
667         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;
668
669         /* Channels */
670         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
671         cl2chan = Channel_FirstChannelOf( c );
672         while( cl2chan )
673         {
674                 chan = Channel_GetChannel( cl2chan );
675                 assert( chan != NULL );
676
677                 /* next */
678                 cl2chan = Channel_NextChannelOf( c, cl2chan );
679
680                 /* Secret channel? */
681                 if( strchr( Channel_Modes( chan ), 's' ) && ! Channel_IsMemberOf( chan, Client )) continue;
682
683                 /* Concatenate channel names */
684                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
685                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
686                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
687                 strlcat( str, Channel_Name( chan ), sizeof( str ));
688
689                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
690                 {
691                         /* Line becomes too long: send it! */
692                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
693                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
694                 }
695         }
696         if( str[strlen( str ) - 1] != ':')
697         {
698                 /* There is data left to send: */
699                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
700         }
701
702         /* IRC-Operator? */
703         if( Client_HasMode( c, 'o' ))
704         {
705                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
706         }
707
708         /* Idle (only local clients) */
709         if( Client_Conn( c ) > NONE )
710         {
711                 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
712         }
713
714         /* Away? */
715         if( Client_HasMode( c, 'a' ))
716         {
717                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
718         }
719
720         /* End of Whois */
721         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
722 } /* IRC_WHOIS */
723
724
725 GLOBAL BOOLEAN
726 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
727 {
728         assert( Client != NULL );
729         assert( Req != NULL );
730
731         /* Falsche Anzahl Parameter? */
732         if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
733
734         /* ... */
735
736         return CONNECTED;
737 } /* IRC_WHOWAS */
738
739
740 GLOBAL BOOLEAN
741 IRC_Send_LUSERS( CLIENT *Client )
742 {
743         LONG cnt;
744
745         assert( Client != NULL );
746
747         /* Users, services and serevers in the network */
748         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
749
750         /* Number of IRC operators */
751         cnt = Client_OperCount( );
752         if( cnt > 0 )
753         {
754                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
755         }
756
757         /* Unknown connections */
758         cnt = Client_UnknownCount( );
759         if( cnt > 0 )
760         {
761                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
762         }
763
764         /* Number of created channels */
765         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
766
767         /* Number of local users, services and servers */
768         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
769
770 #ifndef STRICT_RFC
771         /* Maximum number of local users */
772         if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
773         /* Maximum number of users in the network */
774         if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
775 #endif
776         
777         return CONNECTED;
778 } /* IRC_Send_LUSERS */
779
780
781 GLOBAL BOOLEAN
782 IRC_Show_MOTD( CLIENT *Client )
783 {
784         BOOLEAN ok;
785         CHAR line[127];
786         FILE *fd;
787
788         assert( Client != NULL );
789
790         if( Conf_MotdPhrase[0] )
791         {
792                 if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
793                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), Conf_MotdPhrase )) return DISCONNECTED;
794                 return IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ));
795         }
796
797         fd = fopen( Conf_MotdFile, "r" );
798         if( ! fd )
799         {
800                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
801                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
802         }
803
804         if( ! IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return DISCONNECTED;
805         while( TRUE )
806         {
807                 if( ! fgets( line, sizeof( line ), fd )) break;
808
809                 ngt_TrimLastChr( line, '\n');
810
811                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
812                 {
813                         fclose( fd );
814                         return FALSE;
815                 }
816         }
817         ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
818
819         fclose( fd );
820
821         return ok;
822 } /* IRC_Show_MOTD */
823
824
825 GLOBAL BOOLEAN
826 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
827 {
828         BOOLEAN is_visible, is_member;
829         CHAR str[LINE_LEN + 1];
830         CL2CHAN *cl2chan;
831         CLIENT *cl;
832
833         assert( Client != NULL );
834         assert( Chan != NULL );
835
836         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
837         else is_member = FALSE;
838
839         /* Secret channel? */
840         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
841
842         /* Alle Mitglieder suchen */
843         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
844         cl2chan = Channel_FirstMember( Chan );
845         while( cl2chan )
846         {
847                 cl = Channel_GetClient( cl2chan );
848
849                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = FALSE;
850                 else is_visible = TRUE;
851
852                 if( is_member || is_visible )
853                 {
854                         /* Nick anhaengen */
855                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
856                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
857                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
858                         strlcat( str, Client_ID( cl ), sizeof( str ));
859
860                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
861                         {
862                                 /* Zeile wird zu lang: senden! */
863                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
864                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
865                         }
866                 }
867
868                 /* naechstes Mitglied suchen */
869                 cl2chan = Channel_NextMember( Chan, cl2chan );
870         }
871         if( str[strlen( str ) - 1] != ':')
872         {
873                 /* Es sind noch Daten da, die gesendet werden muessen */
874                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
875         }
876
877         return CONNECTED;
878 } /* IRC_Send_NAMES */
879
880
881 GLOBAL BOOLEAN
882 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, BOOLEAN OnlyOps )
883 {
884         BOOLEAN is_visible, is_member;
885         CL2CHAN *cl2chan;
886         CHAR flags[8];
887         CLIENT *c;
888
889         assert( Client != NULL );
890         assert( Chan != NULL );
891
892         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
893         else is_member = FALSE;
894
895         /* Secret channel? */
896         if( ! is_member && strchr( Channel_Modes( Chan ), 's' )) return CONNECTED;
897
898         /* Alle Mitglieder suchen */
899         cl2chan = Channel_FirstMember( Chan );
900         while( cl2chan )
901         {
902                 c = Channel_GetClient( cl2chan );
903
904                 if( strchr( Client_Modes( c ), 'i' )) is_visible = FALSE;
905                 else is_visible = TRUE;
906
907                 if( is_member || is_visible )
908                 {
909                         /* Flags zusammenbasteln */
910                         strcpy( flags, "H" );
911                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
912                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
913                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
914
915                         /* ausgeben */
916                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
917                         {
918                                 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;
919                         }
920                 }
921
922                 /* naechstes Mitglied suchen */
923                 cl2chan = Channel_NextMember( Chan, cl2chan );
924         }
925         return CONNECTED;
926 } /* IRC_Send_WHO */
927
928
929 /* -eof- */