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