]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-info.c
Removed "USE_" prefixes of configuration #defines.
[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.20 2003/12/26 15:55:07 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 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], vertxt[30];
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         snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
532         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, vertxt, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
533 #else
534         return IRC_WriteStrClient( Client, RPL_VERSION_MSG, Client_ID( prefix ), PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel, Conf_ServerName, NGIRCd_VersionAddition( ));
535 #endif
536 } /* IRC_VERSION */
537
538
539 GLOBAL BOOLEAN
540 IRC_WHO( CLIENT *Client, REQUEST *Req )
541 {
542         BOOLEAN ok, only_ops;
543         CHAR flags[8], *ptr;
544         CL2CHAN *cl2chan;
545         CHANNEL *chan;
546         CLIENT *c;
547
548         assert( Client != NULL );
549         assert( Req != NULL );
550
551         /* Falsche Anzahl Parameter? */
552         if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
553
554         only_ops = FALSE;
555         chan = NULL;
556
557         if( Req->argc == 2 )
558         {
559                 /* Nur OPs anzeigen? */
560                 if( strcmp( Req->argv[1], "o" ) == 0 ) only_ops = TRUE;
561 #ifdef STRICT_RFC
562                 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
563 #endif
564         }
565
566         if( Req->argc >= 1 )
567         {
568                 /* wurde ein Channel oder Nick-Mask angegeben? */
569                 chan = Channel_Search( Req->argv[0] );
570         }
571
572         if( chan )
573         {
574                 /* User eines Channels ausgeben */
575                 if( ! IRC_Send_WHO( Client, chan, only_ops )) return DISCONNECTED;
576         }
577
578         c = Client_First( );
579         while( c )
580         {
581                 if(( Client_Type( c ) == CLIENT_USER ) && ( ! strchr( Client_Modes( c ), 'i' )))
582                 {
583                         ok = FALSE;
584                         if( Req->argc == 0 ) ok = TRUE;
585                         else
586                         {
587                                 if( strcasecmp( Req->argv[0], Client_ID( c )) == 0 ) ok = TRUE;
588                                 else if( strcmp( Req->argv[0], "0" ) == 0 ) ok = TRUE;
589                         }
590
591                         if( ok && (( ! only_ops ) || ( strchr( Client_Modes( c ), 'o' ))))
592                         {
593                                 /* Flags zusammenbasteln */
594                                 strcpy( flags, "H" );
595                                 if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
596
597                                 /* ausgeben */
598                                 cl2chan = Channel_FirstChannelOf( c );
599                                 if( cl2chan ) ptr = Channel_Name( Channel_GetChannel( cl2chan ));
600                                 else ptr = "*";
601                                 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;
602                         }
603                 }
604
605                 /* naechster Client */
606                 c = Client_Next( c );
607         }
608
609         if( chan ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Channel_Name( chan ));
610         else if( Req->argc == 0 ) return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), "*" );
611         else return IRC_WriteStrClient( Client, RPL_ENDOFWHO_MSG, Client_ID( Client ), Req->argv[0] );
612 } /* IRC_WHO */
613
614
615 GLOBAL BOOLEAN
616 IRC_WHOIS( CLIENT *Client, REQUEST *Req )
617 {
618         CLIENT *from, *target, *c;
619         CHAR str[LINE_LEN + 1];
620         CL2CHAN *cl2chan;
621         CHANNEL *chan;
622
623         assert( Client != NULL );
624         assert( Req != NULL );
625
626         /* Bad number of parameters? */
627         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
628
629         /* Search client */
630         c = Client_Search( Req->argv[Req->argc - 1] );
631         if(( ! c ) || ( Client_Type( c ) != CLIENT_USER )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[Req->argc - 1] );
632
633         /* Search sender of the WHOIS */
634         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
635         else from = Client;
636         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
637
638         /* Forward to other server? */
639         if( Req->argc > 1 )
640         {
641                 /* Search target server (can be specified as nick of that server!) */
642                 target = Client_Search( Req->argv[0] );
643                 if( ! target ) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
644         }
645         else target = Client_ThisServer( );
646
647         assert( target != NULL );
648
649         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] );
650
651         /* Nick, user and name */
652         if( ! IRC_WriteStrClient( from, RPL_WHOISUSER_MSG, Client_ID( from ), Client_ID( c ), Client_User( c ), Client_Hostname( c ), Client_Info( c ))) return DISCONNECTED;
653
654         /* Server */
655         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;
656
657         /* Channels */
658         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
659         cl2chan = Channel_FirstChannelOf( c );
660         while( cl2chan )
661         {
662                 chan = Channel_GetChannel( cl2chan );
663                 assert( chan != NULL );
664
665                 /* Concatenate channel names */
666                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
667                 if( strchr( Channel_UserModes( chan, c ), 'o' )) strlcat( str, "@", sizeof( str ));
668                 else if( strchr( Channel_UserModes( chan, c ), 'v' )) strlcat( str, "+", sizeof( str ));
669                 strlcat( str, Channel_Name( chan ), sizeof( str ));
670
671                 if( strlen( str ) > ( LINE_LEN - CHANNEL_NAME_LEN - 4 ))
672                 {
673                         /* Line becomes too long: send it! */
674                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
675                         snprintf( str, sizeof( str ), RPL_WHOISCHANNELS_MSG, Client_ID( from ), Client_ID( c ));
676                 }
677
678                 /* next */
679                 cl2chan = Channel_NextChannelOf( c, cl2chan );
680         }
681         if( str[strlen( str ) - 1] != ':')
682         {
683                 /* There is data left to send: */
684                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
685         }
686
687         /* IRC-Operator? */
688         if( Client_HasMode( c, 'o' ))
689         {
690                 if( ! IRC_WriteStrClient( from, RPL_WHOISOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
691         }
692
693         /* Idle (only local clients) */
694         if( Client_Conn( c ) > NONE )
695         {
696                 if( ! IRC_WriteStrClient( from, RPL_WHOISIDLE_MSG, Client_ID( from ), Client_ID( c ), Conn_GetIdle( Client_Conn ( c )))) return DISCONNECTED;
697         }
698
699         /* Away? */
700         if( Client_HasMode( c, 'a' ))
701         {
702                 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( c ), Client_Away( c ))) return DISCONNECTED;
703         }
704
705         /* End of Whois */
706         return IRC_WriteStrClient( from, RPL_ENDOFWHOIS_MSG, Client_ID( from ), Client_ID( c ));
707 } /* IRC_WHOIS */
708
709
710 GLOBAL BOOLEAN
711 IRC_WHOWAS( CLIENT *Client, REQUEST *Req )
712 {
713         assert( Client != NULL );
714         assert( Req != NULL );
715
716         /* Falsche Anzahl Parameter? */
717         if(( Req->argc < 1 ) || ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
718
719         /* ... */
720
721         return CONNECTED;
722 } /* IRC_WHOWAS */
723
724
725 GLOBAL BOOLEAN
726 IRC_Send_LUSERS( CLIENT *Client )
727 {
728         LONG cnt;
729
730         assert( Client != NULL );
731
732         /* Users, services and serevers in the network */
733         if( ! IRC_WriteStrClient( Client, RPL_LUSERCLIENT_MSG, Client_ID( Client ), Client_UserCount( ), Client_ServiceCount( ), Client_ServerCount( ))) return DISCONNECTED;
734
735         /* Number of IRC operators */
736         cnt = Client_OperCount( );
737         if( cnt > 0 )
738         {
739                 if( ! IRC_WriteStrClient( Client, RPL_LUSEROP_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
740         }
741
742         /* Unknown connections */
743         cnt = Client_UnknownCount( );
744         if( cnt > 0 )
745         {
746                 if( ! IRC_WriteStrClient( Client, RPL_LUSERUNKNOWN_MSG, Client_ID( Client ), cnt )) return DISCONNECTED;
747         }
748
749         /* Number of created channels */
750         if( ! IRC_WriteStrClient( Client, RPL_LUSERCHANNELS_MSG, Client_ID( Client ), Channel_Count( ))) return DISCONNECTED;
751
752         /* Number of local users, services and servers */
753         if( ! IRC_WriteStrClient( Client, RPL_LUSERME_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyServiceCount( ), Client_MyServerCount( ))) return DISCONNECTED;
754
755 #ifndef STRICT_RFC
756         /* Maximum number of local users */
757         if( ! IRC_WriteStrClient( Client, RPL_LOCALUSERS_MSG, Client_ID( Client ), Client_MyUserCount( ), Client_MyMaxUserCount( ))) return DISCONNECTED;
758         /* Maximum number of users in the network */
759         if( ! IRC_WriteStrClient( Client, RPL_NETUSERS_MSG, Client_ID( Client ), Client_UserCount( ), Client_MaxUserCount( ))) return DISCONNECTED;
760 #endif
761         
762         return CONNECTED;
763 } /* IRC_Send_LUSERS */
764
765
766 GLOBAL BOOLEAN
767 IRC_Show_MOTD( CLIENT *Client )
768 {
769         BOOLEAN ok;
770         CHAR line[127];
771         FILE *fd;
772
773         assert( Client != NULL );
774
775         fd = fopen( Conf_MotdFile, "r" );
776         if( ! fd )
777         {
778                 Log( LOG_WARNING, "Can't read MOTD file \"%s\": %s", Conf_MotdFile, strerror( errno ));
779                 return IRC_WriteStrClient( Client, ERR_NOMOTD_MSG, Client_ID( Client ) );
780         }
781
782         IRC_WriteStrClient( Client, RPL_MOTDSTART_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )));
783         while( TRUE )
784         {
785                 if( ! fgets( line, 126, fd )) break;
786                 if( line[strlen( line ) - 1] == '\n' ) line[strlen( line ) - 1] = '\0';
787                 if( ! IRC_WriteStrClient( Client, RPL_MOTD_MSG, Client_ID( Client ), line ))
788                 {
789                         fclose( fd );
790                         return FALSE;
791                 }
792         }
793         ok = IRC_WriteStrClient( Client, RPL_ENDOFMOTD_MSG, Client_ID( Client ) );
794
795         fclose( fd );
796
797         return ok;
798 } /* IRC_Show_MOTD */
799
800
801 GLOBAL BOOLEAN
802 IRC_Send_NAMES( CLIENT *Client, CHANNEL *Chan )
803 {
804         BOOLEAN is_visible, is_member;
805         CHAR str[LINE_LEN + 1];
806         CL2CHAN *cl2chan;
807         CLIENT *cl;
808
809         assert( Client != NULL );
810         assert( Chan != NULL );
811
812         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
813         else is_member = FALSE;
814
815         /* Alle Mitglieder suchen */
816         snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
817         cl2chan = Channel_FirstMember( Chan );
818         while( cl2chan )
819         {
820                 cl = Channel_GetClient( cl2chan );
821
822                 if( strchr( Client_Modes( cl ), 'i' )) is_visible = FALSE;
823                 else is_visible = TRUE;
824
825                 if( is_member || is_visible )
826                 {
827                         /* Nick anhaengen */
828                         if( str[strlen( str ) - 1] != ':' ) strlcat( str, " ", sizeof( str ));
829                         if( strchr( Channel_UserModes( Chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
830                         else if( strchr( Channel_UserModes( Chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
831                         strlcat( str, Client_ID( cl ), sizeof( str ));
832
833                         if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 4 ))
834                         {
835                                 /* Zeile wird zu lang: senden! */
836                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
837                                 snprintf( str, sizeof( str ), RPL_NAMREPLY_MSG, Client_ID( Client ), "=", Channel_Name( Chan ));
838                         }
839                 }
840
841                 /* naechstes Mitglied suchen */
842                 cl2chan = Channel_NextMember( Chan, cl2chan );
843         }
844         if( str[strlen( str ) - 1] != ':')
845         {
846                 /* Es sind noch Daten da, die gesendet werden muessen */
847                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
848         }
849
850         return CONNECTED;
851 } /* IRC_Send_NAMES */
852
853
854 GLOBAL BOOLEAN
855 IRC_Send_WHO( CLIENT *Client, CHANNEL *Chan, BOOLEAN OnlyOps )
856 {
857         BOOLEAN is_visible, is_member;
858         CL2CHAN *cl2chan;
859         CHAR flags[8];
860         CLIENT *c;
861
862         assert( Client != NULL );
863         assert( Chan != NULL );
864
865         if( Channel_IsMemberOf( Chan, Client )) is_member = TRUE;
866         else is_member = FALSE;
867
868         /* Alle Mitglieder suchen */
869         cl2chan = Channel_FirstMember( Chan );
870         while( cl2chan )
871         {
872                 c = Channel_GetClient( cl2chan );
873
874                 if( strchr( Client_Modes( c ), 'i' )) is_visible = FALSE;
875                 else is_visible = TRUE;
876
877                 if( is_member || is_visible )
878                 {
879                         /* Flags zusammenbasteln */
880                         strcpy( flags, "H" );
881                         if( strchr( Client_Modes( c ), 'o' )) strlcat( flags, "*", sizeof( flags ));
882                         if( strchr( Channel_UserModes( Chan, c ), 'o' )) strlcat( flags, "@", sizeof( flags ));
883                         else if( strchr( Channel_UserModes( Chan, c ), 'v' )) strlcat( flags, "+", sizeof( flags ));
884
885                         /* ausgeben */
886                         if(( ! OnlyOps ) || ( strchr( Client_Modes( c ), 'o' )))
887                         {
888                                 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;
889                         }
890                 }
891
892                 /* naechstes Mitglied suchen */
893                 cl2chan = Channel_NextMember( Chan, cl2chan );
894         }
895         return CONNECTED;
896 } /* IRC_Send_WHO */
897
898
899 /* -eof- */